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 )
88 return slot->enableTS(enable);
91 int eDVBCIInterfaces::initialize(int slotid)
95 if( (slot = getSlot(slotid)) == 0 )
98 slot->resetPrevSentCAPMTVersion();
99 PMTHandlerList::iterator it = m_pmt_handlers.begin();
100 while (it != m_pmt_handlers.end())
102 if ( it->cislot == slot )
104 slot->sendCAPMT(it->pmthandler); // send capmt
110 return slot->initialize();
113 int eDVBCIInterfaces::sendCAPMT(int slotid)
117 if( (slot = getSlot(slotid)) == 0 )
120 slot->resetPrevSentCAPMTVersion();
121 PMTHandlerList::iterator it = m_pmt_handlers.begin();
122 while (it != m_pmt_handlers.end())
124 if ( it->cislot == slot )
126 slot->sendCAPMT(it->pmthandler); // send capmt
135 int eDVBCIInterfaces::startMMI(int slotid)
139 if( (slot = getSlot(slotid)) == 0 )
142 return slot->startMMI();
145 int eDVBCIInterfaces::stopMMI(int slotid)
149 if( (slot = getSlot(slotid)) == 0 )
152 return slot->stopMMI();
155 int eDVBCIInterfaces::answerText(int slotid, int answer)
159 if( (slot = getSlot(slotid)) == 0 )
162 return slot->answerText(answer);
165 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
169 if( (slot = getSlot(slotid)) == 0 )
172 return slot->answerEnq(value);
175 int eDVBCIInterfaces::cancelEnq(int slotid)
179 if( (slot = getSlot(slotid)) == 0 )
182 return slot->cancelEnq();
185 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
187 CIPmtHandler new_handler(pmthandler);
189 eServiceReferenceDVB service;
190 pmthandler->getService(service);
192 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
194 // HACK the first service get the CI..
195 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
196 for (; ci_it != m_slots.end(); ++ci_it)
198 if (ci_it->use_count)
201 new_handler.cislot = ci_it;
202 new_handler.cislot->resetPrevSentCAPMTVersion();
205 if (ci_it == m_slots.end())
207 PMTHandlerList::iterator it = m_pmt_handlers.begin();
208 while (it != m_pmt_handlers.end())
210 eServiceReferenceDVB ref;
211 it->pmthandler->getService(ref);
212 if ( service == ref && it->cislot )
214 new_handler.cislot = it->cislot;
215 ++new_handler.cislot->use_count;
222 m_pmt_handlers.push_back(new_handler);
225 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
227 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
228 if (it != m_pmt_handlers.end())
230 eDVBCISlot *slot = it->cislot;
231 // eDVBServicePMTHandler *pmthandler = it->pmthandler;
232 m_pmt_handlers.erase(it);
233 if (slot && !--slot->use_count)
236 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
237 std::vector<uint16_t> caids;
238 caids.push_back(0xFFFF);
239 slot->resetPrevSentCAPMTVersion();
240 slot->sendCAPMT(pmthandler, caids);
242 // check if another service is running
243 it = m_pmt_handlers.begin();
244 while (it != m_pmt_handlers.end())
250 slot->resetPrevSentCAPMTVersion();
251 slot->sendCAPMT(it->pmthandler);
260 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
262 eDebug("[eDVBCIInterfaces] gotPMT");
263 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
264 eServiceReferenceDVB service;
265 if ( it != m_pmt_handlers.end() && it->cislot)
266 it->cislot->sendCAPMT(pmthandler);
269 int eDVBCIInterfaces::getMMIState(int slotid)
273 if( (slot = getSlot(slotid)) == 0 )
276 return slot->getMMIState();
279 int eDVBCISlot::send(const unsigned char *data, size_t len)
285 // printf("%02x ",data[i]);
288 res = ::write(fd, data, len);
290 //printf("write() %d\n",res);
292 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
297 void eDVBCISlot::data(int what)
299 if(what == eSocketNotifier::Priority) {
300 if(state != stateRemoved) {
301 state = stateRemoved;
303 printf("ci removed\n");
304 eDVBCISession::deleteSessions(this);
305 notifier->setRequested(eSocketNotifier::Read);
307 eDVBCI_UI::getInstance()->setState(0,0);
314 r = ::read(fd, data, 4096);
316 if(state != stateInserted) {
317 state = stateInserted;
318 eDebug("ci inserted");
321 eDVBCI_UI::getInstance()->setState(0,1);
323 /* enable PRI to detect removal or errors */
324 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
331 // printf("%02x ",data[i]);
333 eDVBCISession::receiveData(this, data, r);
334 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
338 if(what == eSocketNotifier::Write) {
339 if(eDVBCISession::pollAll() == 0) {
340 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
345 DEFINE_REF(eDVBCISlot);
347 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
351 application_manager = 0;
358 sprintf(filename, "/dev/ci%d", nr);
360 fd = ::open(filename, O_RDWR | O_NONBLOCK);
362 eDebug("eDVBCISlot has fd %d", fd);
364 state = stateInserted;
368 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
369 CONNECT(notifier->activated, eDVBCISlot::data);
376 eDVBCISlot::~eDVBCISlot()
381 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
383 application_manager=session;
386 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
388 mmi_session = session;
391 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
393 ca_manager = session;
396 int eDVBCISlot::getSlotID()
401 int eDVBCISlot::reset()
403 printf("edvbcislot: reset requested\n");
412 int eDVBCISlot::initialize()
414 printf("edvbcislot: initialize()\n");
418 int eDVBCISlot::startMMI()
420 printf("edvbcislot: startMMI()\n");
422 if(application_manager)
423 application_manager->startMMI();
428 int eDVBCISlot::stopMMI()
430 printf("edvbcislot: stopMMI()\n");
433 mmi_session->stopMMI();
438 int eDVBCISlot::answerText(int answer)
440 printf("edvbcislot: answerText(%d)\n", answer);
443 mmi_session->answerText(answer);
448 int eDVBCISlot::getMMIState()
456 int eDVBCISlot::answerEnq(char *value)
458 printf("edvbcislot: answerENQ(%s)\n", value);
462 int eDVBCISlot::cancelEnq()
464 printf("edvbcislot: cancelENQ\n");
467 mmi_session->cancelEnq();
472 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
476 eDebug("no ca_manager (no CI plugged?)");
479 const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
480 ePtr<eTable<ProgramMapSection> > ptr;
481 if (pmthandler->getPMT(ptr))
485 eDVBTableSpec table_spec;
486 ptr->getSpec(table_spec);
487 int pmt_version = table_spec.version & 0x1F; // just 5 bits
488 if ( pmt_version == prev_sent_capmt_version )
490 eDebug("[eDVBCISlot] dont sent self capmt version twice");
493 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
494 if ( i == ptr->getSections().end() )
498 unsigned char raw_data[2048];
499 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
500 while( i != ptr->getSections().end() )
505 capmt.writeToBuffer(raw_data);
507 // begin calc capmt length
510 if ( raw_data[3] & 0x80 )
513 int lenbytes = raw_data[3] & ~0x80;
515 wp = (wp << 8) | raw_data[4 + i++];
526 // end calc capmt length
527 eDebug("ca_manager %p dump capmt:", ca_manager);
528 for(int i=0;i<wp;i++)
529 eDebugNoNewLine("%02x ", raw_data[i]);
532 //dont need tag and lenfield
533 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
534 prev_sent_capmt_version = pmt_version;
540 int eDVBCISlot::enableTS(int enable)
542 printf("eDVBCISlot::enableTS(%d)\n", enable);
545 if((f = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
546 printf("cannot open /proc/stb/tsmux/input0\n");
550 fprintf(f, "%s", enable?"CI":"A");
557 void eDVBCISlot::resendCAPMT()
559 eDVBCIInterfaces::getInstance()->sendCAPMT(slotid);
562 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");