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 )
79 int eDVBCIInterfaces::enableTS(int slotid, int enable)
83 if( (slot = getSlot(slotid)) == 0 )
88 slot->resetPrevSentCAPMTVersion();
89 PMTHandlerList::iterator it = m_pmt_handlers.begin();
90 while (it != m_pmt_handlers.end())
92 if ( it->cislot == slot )
93 slot->sendCAPMT(it->pmthandler); // send capmt
98 return slot->enableTS(enable);
101 int eDVBCIInterfaces::initialize(int slotid)
105 if( (slot = getSlot(slotid)) == 0 )
108 slot->resetPrevSentCAPMTVersion();
109 PMTHandlerList::iterator it = m_pmt_handlers.begin();
110 while (it != m_pmt_handlers.end())
112 if ( it->cislot == slot )
113 slot->sendCAPMT(it->pmthandler); // send capmt
117 return slot->initialize();
120 int eDVBCIInterfaces::startMMI(int slotid)
124 if( (slot = getSlot(slotid)) == 0 )
127 return slot->startMMI();
130 int eDVBCIInterfaces::stopMMI(int slotid)
134 if( (slot = getSlot(slotid)) == 0 )
137 return slot->stopMMI();
140 int eDVBCIInterfaces::answerText(int slotid, int answer)
144 if( (slot = getSlot(slotid)) == 0 )
147 return slot->answerText(answer);
150 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
154 if( (slot = getSlot(slotid)) == 0 )
157 return slot->answerEnq(value);
160 int eDVBCIInterfaces::cancelEnq(int slotid)
164 if( (slot = getSlot(slotid)) == 0 )
167 return slot->cancelEnq();
170 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
172 CIPmtHandler new_handler(pmthandler);
174 eServiceReferenceDVB service;
175 pmthandler->getService(service);
177 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
179 // HACK the first service get the CI..
180 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
181 for (; ci_it != m_slots.end(); ++ci_it)
183 if (ci_it->use_count)
186 new_handler.cislot = ci_it;
187 new_handler.cislot->resetPrevSentCAPMTVersion();
190 if (ci_it == m_slots.end())
192 PMTHandlerList::iterator it = m_pmt_handlers.begin();
193 while (it != m_pmt_handlers.end())
195 eServiceReferenceDVB ref;
196 it->pmthandler->getService(ref);
197 if ( service == ref && it->cislot )
199 new_handler.cislot = it->cislot;
200 ++new_handler.cislot->use_count;
207 m_pmt_handlers.push_back(new_handler);
210 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
212 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
213 if (it != m_pmt_handlers.end())
215 eDVBCISlot *slot = it->cislot;
216 eDVBServicePMTHandler *pmthandler = it->pmthandler;
217 m_pmt_handlers.erase(it);
218 if (slot && !--slot->use_count)
221 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
222 std::vector<uint16_t> caids;
223 caids.push_back(0xFFFF);
224 slot->resetPrevSentCAPMTVersion();
225 slot->sendCAPMT(pmthandler, caids);
227 // check if another service is running
228 it = m_pmt_handlers.begin();
229 while (it != m_pmt_handlers.end())
235 slot->resetPrevSentCAPMTVersion();
236 slot->sendCAPMT(it->pmthandler);
245 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
247 eDebug("[eDVBCIInterfaces] gotPMT");
248 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
249 eServiceReferenceDVB service;
250 if ( it != m_pmt_handlers.end() && it->cislot)
251 it->cislot->sendCAPMT(pmthandler);
254 int eDVBCIInterfaces::getMMIState(int slotid)
258 if( (slot = getSlot(slotid)) == 0 )
261 return slot->getMMIState();
264 int eDVBCISlot::send(const unsigned char *data, size_t len)
270 // printf("%02x ",data[i]);
273 res = ::write(fd, data, len);
275 //printf("write() %d\n",res);
277 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
282 void eDVBCISlot::data(int what)
284 if(what == eSocketNotifier::Priority) {
285 if(state != stateRemoved) {
286 state = stateRemoved;
288 printf("ci removed\n");
289 notifier->setRequested(eSocketNotifier::Read);
291 eDVBCI_UI::getInstance()->setState(0,0);
298 r = ::read(fd, data, 4096);
300 if(state != stateInserted) {
301 state = stateInserted;
302 eDebug("ci inserted");
305 eDVBCI_UI::getInstance()->setState(0,1);
307 /* enable PRI to detect removal or errors */
308 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
315 // printf("%02x ",data[i]);
317 eDVBCISession::receiveData(this, data, r);
318 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
322 if(what == eSocketNotifier::Write) {
323 if(eDVBCISession::pollAll() == 0) {
324 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
329 DEFINE_REF(eDVBCISlot);
331 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
335 application_manager = 0;
342 sprintf(filename, "/dev/ci%d", nr);
344 fd = ::open(filename, O_RDWR | O_NONBLOCK);
346 eDebug("eDVBCISlot has fd %d", fd);
348 state = stateInserted;
352 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
353 CONNECT(notifier->activated, eDVBCISlot::data);
360 eDVBCISlot::~eDVBCISlot()
365 int eDVBCISlot::getSlotID()
370 int eDVBCISlot::reset()
372 printf("edvbcislot: reset requested\n");
381 int eDVBCISlot::initialize()
383 printf("edvbcislot: initialize()\n");
387 int eDVBCISlot::startMMI()
389 printf("edvbcislot: startMMI()\n");
391 if(application_manager)
392 application_manager->startMMI();
397 int eDVBCISlot::stopMMI()
399 printf("edvbcislot: stopMMI()\n");
402 mmi_session->stopMMI();
407 int eDVBCISlot::answerText(int answer)
409 printf("edvbcislot: answerText(%d)\n", answer);
412 mmi_session->answerText(answer);
417 int eDVBCISlot::getMMIState()
425 int eDVBCISlot::answerEnq(char *value)
427 printf("edvbcislot: answerENQ(%s)\n", value);
431 int eDVBCISlot::cancelEnq()
433 printf("edvbcislot: cancelENQ\n");
436 mmi_session->cancelEnq();
441 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
445 eDebug("no ca_manager (no CI plugged?)");
448 const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
449 ePtr<eTable<ProgramMapSection> > ptr;
450 if (pmthandler->getPMT(ptr))
454 eDVBTableSpec table_spec;
455 ptr->getSpec(table_spec);
456 int pmt_version = table_spec.version & 0x1F; // just 5 bits
457 if ( pmt_version == prev_sent_capmt_version )
459 eDebug("[eDVBCISlot] dont sent self capmt version twice");
462 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
463 if ( i == ptr->getSections().end() )
467 unsigned char raw_data[2048];
468 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
469 while( i != ptr->getSections().end() )
474 capmt.writeToBuffer(raw_data);
476 // begin calc capmt length
479 if ( raw_data[3] & 0x80 )
482 int lenbytes = raw_data[3] & ~0x80;
484 wp = (wp << 8) | raw_data[4 + i++];
495 // end calc capmt length
496 eDebug("ca_manager %p dump capmt:", ca_manager);
497 for(int i=0;i<wp;i++)
498 eDebugNoNewLine("%02x ", raw_data[i]);
501 //dont need tag and lenfield
502 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
503 prev_sent_capmt_version = pmt_version;
509 int eDVBCISlot::enableTS(int enable)
511 printf("eDVBCISlot::enableTS(%d)\n", enable);
514 if((f = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
515 printf("cannot open /proc/stb/tsmux/input0\n");
519 fprintf(f, "%s", enable?"CI":"A");
527 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");