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 )
92 PMTHandlerList::iterator it = m_pmt_handlers.begin();
93 while (it != m_pmt_handlers.end())
95 if ( it->cislot == slot )
97 eDVBServicePMTHandler *pmthandler = it->pmthandler;
98 eUsePtr<iDVBChannel> channel;
99 if (!pmthandler->getChannel(channel))
101 ePtr<iDVBFrontend> frontend;
102 if (!channel->getFrontend(frontend))
104 eDVBFrontend *fe = (eDVBFrontend*) &(*frontend);
105 tunernum = fe->getID();
112 if ( tunernum == -1 )
114 eFatal("couldn't find the correct tuner num in enableTS");
118 return slot->enableTS(enable, tunernum);
121 int eDVBCIInterfaces::initialize(int slotid)
125 if( (slot = getSlot(slotid)) == 0 )
128 slot->resetPrevSentCAPMTVersion();
129 PMTHandlerList::iterator it = m_pmt_handlers.begin();
130 while (it != m_pmt_handlers.end())
132 if ( it->cislot == slot )
134 slot->sendCAPMT(it->pmthandler); // send capmt
140 return slot->initialize();
143 int eDVBCIInterfaces::sendCAPMT(int slotid)
147 if( (slot = getSlot(slotid)) == 0 )
150 slot->resetPrevSentCAPMTVersion();
151 PMTHandlerList::iterator it = m_pmt_handlers.begin();
152 while (it != m_pmt_handlers.end())
154 if ( it->cislot == slot )
156 slot->sendCAPMT(it->pmthandler); // send capmt
165 int eDVBCIInterfaces::startMMI(int slotid)
169 if( (slot = getSlot(slotid)) == 0 )
172 return slot->startMMI();
175 int eDVBCIInterfaces::stopMMI(int slotid)
179 if( (slot = getSlot(slotid)) == 0 )
182 return slot->stopMMI();
185 int eDVBCIInterfaces::answerText(int slotid, int answer)
189 if( (slot = getSlot(slotid)) == 0 )
192 return slot->answerText(answer);
195 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
199 if( (slot = getSlot(slotid)) == 0 )
202 return slot->answerEnq(value);
205 int eDVBCIInterfaces::cancelEnq(int slotid)
209 if( (slot = getSlot(slotid)) == 0 )
212 return slot->cancelEnq();
215 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
217 CIPmtHandler new_handler(pmthandler);
219 eServiceReferenceDVB service;
220 pmthandler->getService(service);
222 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
224 // HACK the first service get the CI..
225 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
226 for (; ci_it != m_slots.end(); ++ci_it)
228 if (ci_it->use_count)
231 new_handler.cislot = ci_it;
232 new_handler.cislot->resetPrevSentCAPMTVersion();
235 if (ci_it == m_slots.end())
237 PMTHandlerList::iterator it = m_pmt_handlers.begin();
238 while (it != m_pmt_handlers.end())
240 eServiceReferenceDVB ref;
241 it->pmthandler->getService(ref);
242 if ( service == ref && it->cislot )
244 new_handler.cislot = it->cislot;
245 ++new_handler.cislot->use_count;
252 m_pmt_handlers.push_back(new_handler);
255 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
257 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
258 if (it != m_pmt_handlers.end())
260 eDVBCISlot *slot = it->cislot;
261 // eDVBServicePMTHandler *pmthandler = it->pmthandler;
262 m_pmt_handlers.erase(it);
263 if (slot && !--slot->use_count)
266 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
267 std::vector<uint16_t> caids;
268 caids.push_back(0xFFFF);
269 slot->resetPrevSentCAPMTVersion();
270 slot->sendCAPMT(pmthandler, caids);
272 // check if another service is running
273 it = m_pmt_handlers.begin();
274 while (it != m_pmt_handlers.end())
280 slot->resetPrevSentCAPMTVersion();
281 slot->sendCAPMT(it->pmthandler);
290 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
292 eDebug("[eDVBCIInterfaces] gotPMT");
293 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
294 eServiceReferenceDVB service;
295 if ( it != m_pmt_handlers.end() && it->cislot)
296 it->cislot->sendCAPMT(pmthandler);
299 int eDVBCIInterfaces::getMMIState(int slotid)
303 if( (slot = getSlot(slotid)) == 0 )
306 return slot->getMMIState();
309 int eDVBCISlot::send(const unsigned char *data, size_t len)
315 // printf("%02x ",data[i]);
318 res = ::write(fd, data, len);
320 //printf("write() %d\n",res);
322 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
327 void eDVBCISlot::data(int what)
329 if(what == eSocketNotifier::Priority) {
330 if(state != stateRemoved) {
331 state = stateRemoved;
333 printf("ci removed\n");
334 eDVBCISession::deleteSessions(this);
335 notifier->setRequested(eSocketNotifier::Read);
337 eDVBCI_UI::getInstance()->setState(0,0);
344 r = ::read(fd, data, 4096);
346 if(state != stateInserted) {
347 state = stateInserted;
348 eDebug("ci inserted");
351 eDVBCI_UI::getInstance()->setState(0,1);
353 /* enable PRI to detect removal or errors */
354 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
361 // printf("%02x ",data[i]);
363 eDVBCISession::receiveData(this, data, r);
364 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
368 if(what == eSocketNotifier::Write) {
369 if(eDVBCISession::pollAll() == 0) {
370 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
375 DEFINE_REF(eDVBCISlot);
377 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
381 application_manager = 0;
388 sprintf(filename, "/dev/ci%d", nr);
390 fd = ::open(filename, O_RDWR | O_NONBLOCK);
392 eDebug("eDVBCISlot has fd %d", fd);
394 state = stateInserted;
398 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
399 CONNECT(notifier->activated, eDVBCISlot::data);
406 eDVBCISlot::~eDVBCISlot()
411 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
413 application_manager=session;
416 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
418 mmi_session = session;
421 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
423 ca_manager = session;
426 int eDVBCISlot::getSlotID()
431 int eDVBCISlot::reset()
433 printf("edvbcislot: reset requested\n");
442 int eDVBCISlot::initialize()
444 printf("edvbcislot: initialize()\n");
448 int eDVBCISlot::startMMI()
450 printf("edvbcislot: startMMI()\n");
452 if(application_manager)
453 application_manager->startMMI();
458 int eDVBCISlot::stopMMI()
460 printf("edvbcislot: stopMMI()\n");
463 mmi_session->stopMMI();
468 int eDVBCISlot::answerText(int answer)
470 printf("edvbcislot: answerText(%d)\n", answer);
473 mmi_session->answerText(answer);
478 int eDVBCISlot::getMMIState()
486 int eDVBCISlot::answerEnq(char *value)
488 printf("edvbcislot: answerENQ(%s)\n", value);
492 int eDVBCISlot::cancelEnq()
494 printf("edvbcislot: cancelENQ\n");
497 mmi_session->cancelEnq();
502 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
506 eDebug("no ca_manager (no CI plugged?)");
509 const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
510 ePtr<eTable<ProgramMapSection> > ptr;
511 if (pmthandler->getPMT(ptr))
515 eDVBTableSpec table_spec;
516 ptr->getSpec(table_spec);
517 int pmt_version = table_spec.version & 0x1F; // just 5 bits
518 if ( pmt_version == prev_sent_capmt_version )
520 eDebug("[eDVBCISlot] dont sent self capmt version twice");
523 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
524 if ( i == ptr->getSections().end() )
528 unsigned char raw_data[2048];
529 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
530 while( i != ptr->getSections().end() )
535 capmt.writeToBuffer(raw_data);
537 // begin calc capmt length
540 if ( raw_data[3] & 0x80 )
543 int lenbytes = raw_data[3] & ~0x80;
545 wp = (wp << 8) | raw_data[4 + i++];
556 // end calc capmt length
557 eDebug("ca_manager %p dump capmt:", ca_manager);
558 for(int i=0;i<wp;i++)
559 eDebugNoNewLine("%02x ", raw_data[i]);
562 //dont need tag and lenfield
563 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
564 prev_sent_capmt_version = pmt_version;
570 int eDVBCISlot::enableTS(int enable, int tuner)
572 printf("eDVBCISlot::enableTS(%d %d)\n", enable, tuner);
574 FILE *input0, *input1, *ci;
575 if((input0 = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
576 printf("cannot open /proc/stb/tsmux/input0\n");
579 if((input1 = fopen("/proc/stb/tsmux/input1", "wb")) == NULL) {
580 printf("cannot open /proc/stb/tsmux/input1\n");
583 if((ci = fopen("/proc/stb/tsmux/input2", "wb")) == NULL) {
584 printf("cannot open /proc/stb/tsmux/input2\n");
588 fprintf(ci, "%s", tuner==0 ? "A" : "B"); // configure CI data source (TunerA, TunerB)
589 fprintf(input0, "%s", tuner==0 && enable ? "CI" : "A"); // configure ATI input 0 data source
590 fprintf(input1, "%s", tuner==1 && enable ? "CI" : "B"); // configure ATI input 1 data source
599 void eDVBCISlot::resendCAPMT()
601 eDVBCIInterfaces::getInstance()->sendCAPMT(slotid);
604 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");