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);
82 int eDVBCIInterfaces::enableTS(int slotid, int enable)
86 if( (slot = getSlot(slotid)) == 0 )
90 PMTHandlerList::iterator it = m_pmt_handlers.begin();
91 while (it != m_pmt_handlers.end())
93 if ( it->cislot == slot )
95 eDVBServicePMTHandler *pmthandler = it->pmthandler;
96 eUsePtr<iDVBChannel> channel;
97 if (!pmthandler->getChannel(channel))
99 ePtr<iDVBFrontend> frontend;
100 if (!channel->getFrontend(frontend))
102 eDVBFrontend *fe = (eDVBFrontend*) &(*frontend);
103 tunernum = fe->getID();
110 return slot->enableTS(enable, tunernum);
113 int eDVBCIInterfaces::initialize(int slotid)
117 if( (slot = getSlot(slotid)) == 0 )
120 slot->removeService();
122 return sendCAPMT(slotid);
125 int eDVBCIInterfaces::sendCAPMT(int slotid)
129 if( (slot = getSlot(slotid)) == 0 )
132 PMTHandlerList::iterator it = m_pmt_handlers.begin();
133 while (it != m_pmt_handlers.end())
135 if ( it->cislot == slot )
136 slot->sendCAPMT(it->pmthandler); // send capmt
143 int eDVBCIInterfaces::startMMI(int slotid)
147 if( (slot = getSlot(slotid)) == 0 )
150 return slot->startMMI();
153 int eDVBCIInterfaces::stopMMI(int slotid)
157 if( (slot = getSlot(slotid)) == 0 )
160 return slot->stopMMI();
163 int eDVBCIInterfaces::answerText(int slotid, int answer)
167 if( (slot = getSlot(slotid)) == 0 )
170 return slot->answerText(answer);
173 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
177 if( (slot = getSlot(slotid)) == 0 )
180 return slot->answerEnq(value);
183 int eDVBCIInterfaces::cancelEnq(int slotid)
187 if( (slot = getSlot(slotid)) == 0 )
190 return slot->cancelEnq();
193 void eDVBCIInterfaces::ciRemoved(eDVBCISlot *slot)
195 for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
196 it != m_pmt_handlers.end(); ++it)
198 if (it->cislot == slot)
200 eServiceReferenceDVB ref;
201 it->pmthandler->getServiceReference(ref);
202 slot->removeService(ref.getServiceID().get());
203 if (!--slot->use_count)
204 enableTS(slot->getSlotID(), 0);
210 void eDVBCIInterfaces::recheckPMTHandlers()
212 // eDebug("recheckPMTHAndlers()");
213 for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
214 it != m_pmt_handlers.end(); ++it)
217 ePtr<eDVBService> service;
218 eServiceReferenceDVB ref;
219 eDVBServicePMTHandler *pmthandler = it->pmthandler;
220 eDVBServicePMTHandler::program p;
222 pmthandler->getServiceReference(ref);
223 pmthandler->getService(service);
224 if (!pmthandler->getProgramInfo(p))
227 for (std::set<uint16_t>::reverse_iterator x(p.caids.rbegin()); x != p.caids.rend(); ++x, ++cnt)
228 caids.push_front(*x);
230 service->m_ca = caids;
234 continue; // already running
237 caids = service->m_ca;
241 for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
243 if (ci_it->getState() == eDVBCISlot::stateInvalid)
247 eDVBCICAManagerSession *ca_manager = ci_it->getCAManager();
250 const std::vector<uint16_t> &ci_caids = ca_manager->getCAIDs();
251 for (CAID_LIST::iterator ca(caids.begin()); ca != caids.end(); ++ca)
253 std::vector<uint16_t>::const_iterator z =
254 std::lower_bound(ci_caids.begin(), ci_caids.end(), *ca);
255 if ( z != ci_caids.end() && *z == *ca )
257 eDebug("found ci for caid %04x", *z);
266 bool send_ca_pmt = false;
267 if (ci_it->use_count) // check if this CI can descramble more than one service
269 PMTHandlerList::iterator tmp = m_pmt_handlers.begin();
270 while (tmp != m_pmt_handlers.end())
274 bool canHandleMultipleServices=false;
275 eServiceReferenceDVB ref2;
276 tmp->pmthandler->getServiceReference(ref2);
277 eDVBChannelID s1, s2;
280 ref.getChannelID(s1);
281 ref2.getChannelID(s2);
282 // FIXME .. build a "ci can handle multiple services" config entry
284 if ( eDVBCI_UI::getInstance()->getAppName(ci_it->getSlotID()) == "AlphaCrypt" )
286 canHandleMultipleServices = true;
287 eDebug("Alphacrypt can handle multiple services");
290 if (ref == ref2 || (s1 == s2 && canHandleMultipleServices) )
292 it->cislot = tmp->cislot;
293 ++it->cislot->use_count;
295 // eDebug("usecount now %d", it->cislot->use_count);
306 // eDebug("usecount now %d", it->cislot->use_count);
307 enableTS(ci_it->getSlotID(), 1);
318 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
320 // check if this pmthandler is already registered
321 PMTHandlerList::iterator it = m_pmt_handlers.begin();
322 while (it != m_pmt_handlers.end())
324 if ( *it++ == pmthandler )
328 eServiceReferenceDVB ref;
329 pmthandler->getServiceReference(ref);
330 eDebug("[eDVBCIInterfaces] addPMTHandler %s", ref.toString().c_str());
332 m_pmt_handlers.push_back(CIPmtHandler(pmthandler));
333 recheckPMTHandlers();
336 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
338 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
339 if (it != m_pmt_handlers.end())
341 eDVBCISlot *slot = it->cislot;
342 eDVBServicePMTHandler *pmthandler = it->pmthandler;
343 m_pmt_handlers.erase(it);
345 eServiceReferenceDVB service_to_remove;
346 pmthandler->getServiceReference(service_to_remove);
348 bool sameServiceExist=false;
349 for (PMTHandlerList::iterator i=m_pmt_handlers.begin(); i != m_pmt_handlers.end(); ++i)
351 eServiceReferenceDVB ref;
352 i->pmthandler->getServiceReference(ref);
353 if ( ref == service_to_remove )
355 sameServiceExist=true;
360 if (slot && !sameServiceExist)
362 if (slot->getNumOfServices() > 1) // fixme make it dependend of "ci can handle more than one service"
364 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt",
365 service_to_remove.toString().c_str());
366 std::vector<uint16_t> caids;
367 caids.push_back(0xFFFF);
368 slot->sendCAPMT(pmthandler, caids); // send a capmt without caids to remove a running service
370 slot->removeService(service_to_remove.getServiceID().get());
373 if (slot && !--slot->use_count)
375 ASSERT(!slot->getNumOfServices());
376 enableTS(slot->getSlotID(),0);
379 // check if another service is waiting for the CI
380 recheckPMTHandlers();
383 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
385 eDebug("[eDVBCIInterfaces] gotPMT");
386 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
387 if (it != m_pmt_handlers.end() && it->cislot)
388 it->cislot->sendCAPMT(pmthandler);
391 int eDVBCIInterfaces::getMMIState(int slotid)
395 if( (slot = getSlot(slotid)) == 0 )
398 return slot->getMMIState();
401 int eDVBCISlot::send(const unsigned char *data, size_t len)
407 // printf("%02x ",data[i]);
410 if (sendqueue.empty())
411 res = ::write(fd, data, len);
413 if (res < 0 || (unsigned int)res != len)
415 unsigned char *d = new unsigned char[len];
416 memcpy(d, data, len);
417 sendqueue.push( queueData(d, len) );
418 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
424 void eDVBCISlot::data(int what)
426 if (state == stateInvalid)
428 if(what == eSocketNotifier::Priority) {
429 if(state != stateRemoved) {
430 state = stateRemoved;
431 printf("ci removed\n");
432 while(sendqueue.size())
434 delete [] sendqueue.top().data;
437 eDVBCIInterfaces::getInstance()->ciRemoved(this);
438 eDVBCISession::deleteSessions(this);
439 notifier->setRequested(eSocketNotifier::Read);
441 eDVBCI_UI::getInstance()->setState(0,0);
446 if(state != stateInserted) {
447 eDebug("ci inserted");
448 state = stateInserted;
450 eDVBCI_UI::getInstance()->setState(0,1);
451 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority);
452 /* enable PRI to detect removal or errors */
455 if (what & eSocketNotifier::Read) {
458 r = ::read(fd, data, 4096);
463 // printf("%02x ",data[i]);
465 eDVBCISession::receiveData(this, data, r);
466 eDVBCISession::pollAll();
470 else if (what & eSocketNotifier::Write) {
471 if (!sendqueue.empty()) {
472 const queueData &qe = sendqueue.top();
473 int res = ::write(fd, qe.data, qe.len);
474 if (res >= 0 && (unsigned int)res == qe.len)
481 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority);
485 DEFINE_REF(eDVBCISlot);
487 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
491 application_manager = 0;
498 sprintf(filename, "/dev/ci%d", nr);
500 fd = ::open(filename, O_RDWR | O_NONBLOCK);
502 eDebug("eDVBCISlot has fd %d", fd);
503 state = stateInvalid;
507 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
508 CONNECT(notifier->activated, eDVBCISlot::data);
517 eDVBCISlot::~eDVBCISlot()
521 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
523 application_manager=session;
526 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
528 mmi_session = session;
531 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
533 ca_manager = session;
536 int eDVBCISlot::getSlotID()
541 int eDVBCISlot::reset()
543 printf("edvbcislot: reset requested\n");
545 if (state == stateInvalid)
547 unsigned char buf[256];
549 while(::read(fd, buf, 256)>0);
550 state = stateResetted;
553 while(sendqueue.size())
555 delete [] sendqueue.top().data;
564 int eDVBCISlot::startMMI()
566 printf("edvbcislot: startMMI()\n");
568 if(application_manager)
569 application_manager->startMMI();
574 int eDVBCISlot::stopMMI()
576 printf("edvbcislot: stopMMI()\n");
579 mmi_session->stopMMI();
584 int eDVBCISlot::answerText(int answer)
586 printf("edvbcislot: answerText(%d)\n", answer);
589 mmi_session->answerText(answer);
594 int eDVBCISlot::getMMIState()
602 int eDVBCISlot::answerEnq(char *value)
604 printf("edvbcislot: answerENQ(%s)\n", value);
607 mmi_session->answerEnq(value);
612 int eDVBCISlot::cancelEnq()
614 printf("edvbcislot: cancelENQ\n");
617 mmi_session->cancelEnq();
622 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
626 eDebug("no ca_manager (no CI plugged?)");
629 const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
630 ePtr<eTable<ProgramMapSection> > ptr;
631 if (pmthandler->getPMT(ptr))
635 eDVBTableSpec table_spec;
636 ptr->getSpec(table_spec);
637 int pmt_version = table_spec.version & 0x1F; // just 5 bits
639 eServiceReferenceDVB ref;
640 pmthandler->getServiceReference(ref);
641 uint16_t program_number = ref.getServiceID().get();
642 std::map<uint16_t, uint8_t>::iterator it =
643 running_services.find(program_number);
645 if ( it != running_services.end() &&
646 (pmt_version == it->second) &&
647 !(caids.size() == 1 && caids[0] == 0xFFFF) )
649 eDebug("[eDVBCISlot] dont sent self capmt version twice");
653 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
654 if ( i == ptr->getSections().end() )
658 unsigned char raw_data[2048];
660 // eDebug("send %s capmt for service %04x",
661 // it != running_services.end() ? "UPDATE" : running_services.empty() ? "ONLY" : "ADD",
664 CaProgramMapSection capmt(*i++,
665 it != running_services.end() ? 0x05 /*update*/ : running_services.empty() ? 0x03 /*only*/ : 0x04 /*add*/, 0x01, caids );
666 while( i != ptr->getSections().end() )
671 capmt.writeToBuffer(raw_data);
673 // begin calc capmt length
676 if ( raw_data[3] & 0x80 )
679 int lenbytes = raw_data[3] & ~0x80;
681 wp = (wp << 8) | raw_data[4 + i++];
692 // end calc capmt length
693 // eDebug("ca_manager %p dump capmt:", ca_manager);
694 // for(int i=0;i<wp;i++)
695 // eDebugNoNewLine("%02x ", raw_data[i]);
698 if (caids.size() == 1 && caids[0] == 0xFFFF)
700 // eDebugNoNewLine("SEND EMPTY CAPMT.. old version is %02x", raw_data[hlen+3]);
701 raw_data[hlen+3] &= ~0x3E;
702 raw_data[hlen+3] |= ((pmt_version+1) & 0x1F) << 1;
703 // eDebug(" new version is %02x", raw_data[hlen+3]);
706 //dont need tag and lenfield
707 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
708 running_services[program_number] = pmt_version;
714 void eDVBCISlot::removeService(uint16_t program_number)
716 if (program_number == 0xFFFF)
717 running_services.clear(); // remove all
719 running_services.erase(program_number); // remove single service
722 int eDVBCISlot::enableTS(int enable, int tuner)
724 // printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
725 // printf("eDVBCISlot::enableTS(%d %d)\n", enable, tuner);
727 FILE *input0, *input1, *ci;
728 if((input0 = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
729 printf("cannot open /proc/stb/tsmux/input0\n");
732 if((input1 = fopen("/proc/stb/tsmux/input1", "wb")) == NULL) {
733 printf("cannot open /proc/stb/tsmux/input1\n");
736 if((ci = fopen("/proc/stb/tsmux/input2", "wb")) == NULL) {
737 printf("cannot open /proc/stb/tsmux/input2\n");
741 fprintf(ci, "%s", tuner==0 ? "A" : "B"); // configure CI data source (TunerA, TunerB)
742 fprintf(input0, "%s", tuner==0 && enable ? "CI" : "A"); // configure ATI input 0 data source
743 fprintf(input1, "%s", tuner==1 && enable ? "CI" : "B"); // configure ATI input 1 data source
751 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");