4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/ebase.h>
8 #include <lib/base/eerror.h>
9 #include <lib/dvb/pmt.h>
10 #include <lib/dvb_ci/dvbci.h>
11 #include <lib/dvb_ci/dvbci_session.h>
12 #include <lib/dvb_ci/dvbci_camgr.h>
13 #include <lib/dvb_ci/dvbci_ui.h>
14 #include <lib/dvb_ci/dvbci_appmgr.h>
15 #include <lib/dvb_ci/dvbci_mmi.h>
17 #include <dvbsi++/ca_program_map_section.h>
19 eDVBCIInterfaces *eDVBCIInterfaces::instance = 0;
21 eDVBCIInterfaces::eDVBCIInterfaces()
27 eDebug("scanning for common interfaces..");
33 sprintf(filename, "/dev/ci%d", num_ci);
35 if (stat(filename, &s))
38 ePtr<eDVBCISlot> cislot;
40 cislot = new eDVBCISlot(eApp, num_ci);
41 m_slots.push_back(cislot);
46 eDebug("done, found %d common interface slots", num_ci);
49 eDVBCIInterfaces::~eDVBCIInterfaces()
53 eDVBCIInterfaces *eDVBCIInterfaces::getInstance()
58 eDVBCISlot *eDVBCIInterfaces::getSlot(int slotid)
60 for(eSmartPtrList<eDVBCISlot>::iterator i(m_slots.begin()); i != m_slots.end(); ++i)
61 if(i->getSlotID() == slotid)
64 printf("FIXME: request for unknown slot\n");
69 int eDVBCIInterfaces::reset(int slotid)
73 if( (slot = getSlot(slotid)) == 0 )
76 eDVBCISession::deleteSessions(slot);
81 int eDVBCIInterfaces::enableTS(int slotid, int enable)
85 if( (slot = getSlot(slotid)) == 0 )
91 PMTHandlerList::iterator it = m_pmt_handlers.begin();
92 while (it != m_pmt_handlers.end())
94 if ( it->cislot == slot )
96 eDVBServicePMTHandler *pmthandler = it->pmthandler;
97 eUsePtr<iDVBChannel> channel;
98 if (!pmthandler->getChannel(channel))
100 ePtr<iDVBFrontend> frontend;
101 if (!channel->getFrontend(frontend))
103 eDVBFrontend *fe = (eDVBFrontend*) &(*frontend);
104 tunernum = fe->getID();
112 return slot->enableTS(enable, tunernum);
115 int eDVBCIInterfaces::initialize(int slotid)
119 if( (slot = getSlot(slotid)) == 0 )
122 slot->resetPrevSentCAPMTVersion();
123 PMTHandlerList::iterator it = m_pmt_handlers.begin();
124 while (it != m_pmt_handlers.end())
126 if ( it->cislot == slot )
128 slot->sendCAPMT(it->pmthandler); // send capmt
134 return slot->initialize();
137 int eDVBCIInterfaces::sendCAPMT(int slotid)
141 if( (slot = getSlot(slotid)) == 0 )
144 slot->resetPrevSentCAPMTVersion();
145 PMTHandlerList::iterator it = m_pmt_handlers.begin();
146 while (it != m_pmt_handlers.end())
148 if ( it->cislot == slot )
150 slot->sendCAPMT(it->pmthandler); // send capmt
159 int eDVBCIInterfaces::startMMI(int slotid)
163 if( (slot = getSlot(slotid)) == 0 )
166 return slot->startMMI();
169 int eDVBCIInterfaces::stopMMI(int slotid)
173 if( (slot = getSlot(slotid)) == 0 )
176 return slot->stopMMI();
179 int eDVBCIInterfaces::answerText(int slotid, int answer)
183 if( (slot = getSlot(slotid)) == 0 )
186 return slot->answerText(answer);
189 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
193 if( (slot = getSlot(slotid)) == 0 )
196 return slot->answerEnq(value);
199 int eDVBCIInterfaces::cancelEnq(int slotid)
203 if( (slot = getSlot(slotid)) == 0 )
206 return slot->cancelEnq();
209 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
211 CIPmtHandler new_handler(pmthandler);
213 eServiceReferenceDVB service;
214 pmthandler->getService(service);
216 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
218 // HACK the first service get the CI..
219 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
220 for (; ci_it != m_slots.end(); ++ci_it)
222 if (ci_it->use_count)
225 new_handler.cislot = ci_it;
226 new_handler.cislot->resetPrevSentCAPMTVersion();
229 if (ci_it == m_slots.end())
231 PMTHandlerList::iterator it = m_pmt_handlers.begin();
232 while (it != m_pmt_handlers.end())
234 eServiceReferenceDVB ref;
235 it->pmthandler->getService(ref);
236 if ( service == ref && it->cislot )
238 new_handler.cislot = it->cislot;
239 ++new_handler.cislot->use_count;
246 m_pmt_handlers.push_back(new_handler);
249 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
251 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
252 if (it != m_pmt_handlers.end())
254 eDVBCISlot *slot = it->cislot;
255 // eDVBServicePMTHandler *pmthandler = it->pmthandler;
256 m_pmt_handlers.erase(it);
257 if (slot && !--slot->use_count)
260 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
261 std::vector<uint16_t> caids;
262 caids.push_back(0xFFFF);
263 slot->resetPrevSentCAPMTVersion();
264 slot->sendCAPMT(pmthandler, caids);
266 // check if another service is running
267 it = m_pmt_handlers.begin();
268 while (it != m_pmt_handlers.end())
274 slot->resetPrevSentCAPMTVersion();
275 slot->sendCAPMT(it->pmthandler);
284 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
286 eDebug("[eDVBCIInterfaces] gotPMT");
287 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
288 eServiceReferenceDVB service;
289 if ( it != m_pmt_handlers.end() && it->cislot)
290 it->cislot->sendCAPMT(pmthandler);
293 int eDVBCIInterfaces::getMMIState(int slotid)
297 if( (slot = getSlot(slotid)) == 0 )
300 return slot->getMMIState();
303 int eDVBCISlot::send(const unsigned char *data, size_t len)
309 // printf("%02x ",data[i]);
312 res = ::write(fd, data, len);
314 //printf("write() %d\n",res);
316 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
321 void eDVBCISlot::data(int what)
323 if(what == eSocketNotifier::Priority) {
324 if(state != stateRemoved) {
325 state = stateRemoved;
327 printf("ci removed\n");
328 eDVBCISession::deleteSessions(this);
329 notifier->setRequested(eSocketNotifier::Read);
331 eDVBCI_UI::getInstance()->setState(0,0);
338 r = ::read(fd, data, 4096);
340 if(state != stateInserted) {
341 state = stateInserted;
342 eDebug("ci inserted");
345 eDVBCI_UI::getInstance()->setState(0,1);
347 /* enable PRI to detect removal or errors */
348 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
355 // printf("%02x ",data[i]);
357 eDVBCISession::receiveData(this, data, r);
358 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
362 if(what == eSocketNotifier::Write) {
363 if(eDVBCISession::pollAll() == 0) {
364 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
369 DEFINE_REF(eDVBCISlot);
371 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
375 application_manager = 0;
382 sprintf(filename, "/dev/ci%d", nr);
384 fd = ::open(filename, O_RDWR | O_NONBLOCK);
386 eDebug("eDVBCISlot has fd %d", fd);
388 state = stateInserted;
392 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
393 CONNECT(notifier->activated, eDVBCISlot::data);
400 eDVBCISlot::~eDVBCISlot()
405 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
407 application_manager=session;
410 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
412 mmi_session = session;
415 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
417 ca_manager = session;
420 int eDVBCISlot::getSlotID()
425 int eDVBCISlot::reset()
427 printf("edvbcislot: reset requested\n");
436 int eDVBCISlot::initialize()
438 printf("edvbcislot: initialize()\n");
442 int eDVBCISlot::startMMI()
444 printf("edvbcislot: startMMI()\n");
446 if(application_manager)
447 application_manager->startMMI();
452 int eDVBCISlot::stopMMI()
454 printf("edvbcislot: stopMMI()\n");
457 mmi_session->stopMMI();
462 int eDVBCISlot::answerText(int answer)
464 printf("edvbcislot: answerText(%d)\n", answer);
467 mmi_session->answerText(answer);
472 int eDVBCISlot::getMMIState()
480 int eDVBCISlot::answerEnq(char *value)
482 printf("edvbcislot: answerENQ(%s)\n", value);
485 mmi_session->answerEnq(value);
490 int eDVBCISlot::cancelEnq()
492 printf("edvbcislot: cancelENQ\n");
495 mmi_session->cancelEnq();
500 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
504 eDebug("no ca_manager (no CI plugged?)");
507 const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
508 ePtr<eTable<ProgramMapSection> > ptr;
509 if (pmthandler->getPMT(ptr))
513 eDVBTableSpec table_spec;
514 ptr->getSpec(table_spec);
515 int pmt_version = table_spec.version & 0x1F; // just 5 bits
516 if ( pmt_version == prev_sent_capmt_version )
518 eDebug("[eDVBCISlot] dont sent self capmt version twice");
521 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
522 if ( i == ptr->getSections().end() )
526 unsigned char raw_data[2048];
527 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
528 while( i != ptr->getSections().end() )
533 capmt.writeToBuffer(raw_data);
535 // begin calc capmt length
538 if ( raw_data[3] & 0x80 )
541 int lenbytes = raw_data[3] & ~0x80;
543 wp = (wp << 8) | raw_data[4 + i++];
554 // end calc capmt length
555 eDebug("ca_manager %p dump capmt:", ca_manager);
556 for(int i=0;i<wp;i++)
557 eDebugNoNewLine("%02x ", raw_data[i]);
560 //dont need tag and lenfield
561 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
562 prev_sent_capmt_version = pmt_version;
568 int eDVBCISlot::enableTS(int enable, int tuner)
570 printf("eDVBCISlot::enableTS(%d %d)\n", enable, tuner);
572 FILE *input0, *input1, *ci;
573 if((input0 = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
574 printf("cannot open /proc/stb/tsmux/input0\n");
577 if((input1 = fopen("/proc/stb/tsmux/input1", "wb")) == NULL) {
578 printf("cannot open /proc/stb/tsmux/input1\n");
581 if((ci = fopen("/proc/stb/tsmux/input2", "wb")) == NULL) {
582 printf("cannot open /proc/stb/tsmux/input2\n");
586 fprintf(ci, "%s", tuner==0 ? "A" : "B"); // configure CI data source (TunerA, TunerB)
587 fprintf(input0, "%s", tuner==0 && enable ? "CI" : "A"); // configure ATI input 0 data source
588 fprintf(input1, "%s", tuner==1 && enable ? "CI" : "B"); // configure ATI input 1 data source
596 void eDVBCISlot::resendCAPMT()
598 eDVBCIInterfaces::getInstance()->sendCAPMT(slotid);
601 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");