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 )
86 return slot->enableTS(enable);
89 int eDVBCIInterfaces::initialize(int slotid)
93 if( (slot = getSlot(slotid)) == 0 )
96 slot->resetPrevSentCAPMTVersion();
97 PMTHandlerList::iterator it = m_pmt_handlers.begin();
98 while (it != m_pmt_handlers.end())
100 if ( it->cislot == slot )
102 slot->sendCAPMT(it->pmthandler); // send capmt
108 return slot->initialize();
111 int eDVBCIInterfaces::sendCAPMT(int slotid)
115 if( (slot = getSlot(slotid)) == 0 )
118 slot->resetPrevSentCAPMTVersion();
119 PMTHandlerList::iterator it = m_pmt_handlers.begin();
120 while (it != m_pmt_handlers.end())
122 if ( it->cislot == slot )
124 slot->sendCAPMT(it->pmthandler); // send capmt
133 int eDVBCIInterfaces::startMMI(int slotid)
137 if( (slot = getSlot(slotid)) == 0 )
140 return slot->startMMI();
143 int eDVBCIInterfaces::stopMMI(int slotid)
147 if( (slot = getSlot(slotid)) == 0 )
150 return slot->stopMMI();
153 int eDVBCIInterfaces::answerText(int slotid, int answer)
157 if( (slot = getSlot(slotid)) == 0 )
160 return slot->answerText(answer);
163 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
167 if( (slot = getSlot(slotid)) == 0 )
170 return slot->answerEnq(value);
173 int eDVBCIInterfaces::cancelEnq(int slotid)
177 if( (slot = getSlot(slotid)) == 0 )
180 return slot->cancelEnq();
183 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
185 CIPmtHandler new_handler(pmthandler);
187 eServiceReferenceDVB service;
188 pmthandler->getService(service);
190 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
192 // HACK the first service get the CI..
193 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
194 for (; ci_it != m_slots.end(); ++ci_it)
196 if (ci_it->use_count)
199 new_handler.cislot = ci_it;
200 new_handler.cislot->resetPrevSentCAPMTVersion();
203 if (ci_it == m_slots.end())
205 PMTHandlerList::iterator it = m_pmt_handlers.begin();
206 while (it != m_pmt_handlers.end())
208 eServiceReferenceDVB ref;
209 it->pmthandler->getService(ref);
210 if ( service == ref && it->cislot )
212 new_handler.cislot = it->cislot;
213 ++new_handler.cislot->use_count;
220 m_pmt_handlers.push_back(new_handler);
223 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
225 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
226 if (it != m_pmt_handlers.end())
228 eDVBCISlot *slot = it->cislot;
229 // eDVBServicePMTHandler *pmthandler = it->pmthandler;
230 m_pmt_handlers.erase(it);
231 if (slot && !--slot->use_count)
234 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
235 std::vector<uint16_t> caids;
236 caids.push_back(0xFFFF);
237 slot->resetPrevSentCAPMTVersion();
238 slot->sendCAPMT(pmthandler, caids);
240 // check if another service is running
241 it = m_pmt_handlers.begin();
242 while (it != m_pmt_handlers.end())
248 slot->resetPrevSentCAPMTVersion();
249 slot->sendCAPMT(it->pmthandler);
258 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
260 eDebug("[eDVBCIInterfaces] gotPMT");
261 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
262 eServiceReferenceDVB service;
263 if ( it != m_pmt_handlers.end() && it->cislot)
264 it->cislot->sendCAPMT(pmthandler);
267 int eDVBCIInterfaces::getMMIState(int slotid)
271 if( (slot = getSlot(slotid)) == 0 )
274 return slot->getMMIState();
277 int eDVBCISlot::send(const unsigned char *data, size_t len)
283 // printf("%02x ",data[i]);
286 res = ::write(fd, data, len);
288 //printf("write() %d\n",res);
290 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
295 void eDVBCISlot::data(int what)
297 if(what == eSocketNotifier::Priority) {
298 if(state != stateRemoved) {
299 state = stateRemoved;
301 printf("ci removed\n");
302 notifier->setRequested(eSocketNotifier::Read);
304 eDVBCI_UI::getInstance()->setState(0,0);
311 r = ::read(fd, data, 4096);
313 if(state != stateInserted) {
314 state = stateInserted;
315 eDebug("ci inserted");
318 eDVBCI_UI::getInstance()->setState(0,1);
320 /* enable PRI to detect removal or errors */
321 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
328 // printf("%02x ",data[i]);
330 eDVBCISession::receiveData(this, data, r);
331 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
335 if(what == eSocketNotifier::Write) {
336 if(eDVBCISession::pollAll() == 0) {
337 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
342 DEFINE_REF(eDVBCISlot);
344 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
348 application_manager = 0;
355 sprintf(filename, "/dev/ci%d", nr);
357 fd = ::open(filename, O_RDWR | O_NONBLOCK);
359 eDebug("eDVBCISlot has fd %d", fd);
361 state = stateInserted;
365 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
366 CONNECT(notifier->activated, eDVBCISlot::data);
373 eDVBCISlot::~eDVBCISlot()
378 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
380 application_manager=session;
383 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
385 mmi_session = session;
388 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
390 ca_manager = session;
393 int eDVBCISlot::getSlotID()
398 int eDVBCISlot::reset()
400 printf("edvbcislot: reset requested\n");
409 int eDVBCISlot::initialize()
411 printf("edvbcislot: initialize()\n");
415 int eDVBCISlot::startMMI()
417 printf("edvbcislot: startMMI()\n");
419 if(application_manager)
420 application_manager->startMMI();
425 int eDVBCISlot::stopMMI()
427 printf("edvbcislot: stopMMI()\n");
430 mmi_session->stopMMI();
435 int eDVBCISlot::answerText(int answer)
437 printf("edvbcislot: answerText(%d)\n", answer);
440 mmi_session->answerText(answer);
445 int eDVBCISlot::getMMIState()
453 int eDVBCISlot::answerEnq(char *value)
455 printf("edvbcislot: answerENQ(%s)\n", value);
459 int eDVBCISlot::cancelEnq()
461 printf("edvbcislot: cancelENQ\n");
464 mmi_session->cancelEnq();
469 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
473 eDebug("no ca_manager (no CI plugged?)");
476 const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
477 ePtr<eTable<ProgramMapSection> > ptr;
478 if (pmthandler->getPMT(ptr))
482 eDVBTableSpec table_spec;
483 ptr->getSpec(table_spec);
484 int pmt_version = table_spec.version & 0x1F; // just 5 bits
485 if ( pmt_version == prev_sent_capmt_version )
487 eDebug("[eDVBCISlot] dont sent self capmt version twice");
490 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
491 if ( i == ptr->getSections().end() )
495 unsigned char raw_data[2048];
496 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
497 while( i != ptr->getSections().end() )
502 capmt.writeToBuffer(raw_data);
504 // begin calc capmt length
507 if ( raw_data[3] & 0x80 )
510 int lenbytes = raw_data[3] & ~0x80;
512 wp = (wp << 8) | raw_data[4 + i++];
523 // end calc capmt length
524 eDebug("ca_manager %p dump capmt:", ca_manager);
525 for(int i=0;i<wp;i++)
526 eDebugNoNewLine("%02x ", raw_data[i]);
529 //dont need tag and lenfield
530 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
531 prev_sent_capmt_version = pmt_version;
537 int eDVBCISlot::enableTS(int enable)
539 printf("eDVBCISlot::enableTS(%d)\n", enable);
542 if((f = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
543 printf("cannot open /proc/stb/tsmux/input0\n");
547 fprintf(f, "%s", enable?"CI":"A");
554 void eDVBCISlot::resendCAPMT()
556 eDVBCIInterfaces::getInstance()->sendCAPMT(slotid);
559 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");