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 return slot->initialize();
99 int eDVBCIInterfaces::startMMI(int slotid)
103 if( (slot = getSlot(slotid)) == 0 )
106 return slot->startMMI();
109 int eDVBCIInterfaces::stopMMI(int slotid)
113 if( (slot = getSlot(slotid)) == 0 )
116 return slot->stopMMI();
119 int eDVBCIInterfaces::answerText(int slotid, int answer)
123 if( (slot = getSlot(slotid)) == 0 )
126 return slot->answerText(answer);
129 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
133 if( (slot = getSlot(slotid)) == 0 )
136 return slot->answerEnq(value);
139 int eDVBCIInterfaces::cancelEnq(int slotid)
143 if( (slot = getSlot(slotid)) == 0 )
146 return slot->cancelEnq();
149 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
151 CIPmtHandler new_handler(pmthandler);
153 eServiceReferenceDVB service;
154 pmthandler->getService(service);
156 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
158 // HACK the first service get the CI..
159 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
160 for (; ci_it != m_slots.end(); ++ci_it)
162 if (ci_it->use_count)
165 new_handler.cislot = ci_it;
166 new_handler.cislot->resetPrevSentCAPMTVersion();
169 if (ci_it == m_slots.end())
171 PMTHandlerList::iterator it = m_pmt_handlers.begin();
172 while (it != m_pmt_handlers.end())
174 eServiceReferenceDVB ref;
175 it->pmthandler->getService(ref);
176 if ( service == ref && it->cislot )
178 new_handler.cislot = it->cislot;
179 ++new_handler.cislot->use_count;
186 m_pmt_handlers.push_back(new_handler);
189 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
191 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
192 if (it != m_pmt_handlers.end())
194 eDVBCISlot *slot = it->cislot;
195 eDVBServicePMTHandler *pmthandler = it->pmthandler;
196 m_pmt_handlers.erase(it);
197 if (slot && !--slot->use_count)
200 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
201 std::vector<uint16_t> caids;
202 caids.push_back(0xFFFF);
203 slot->resetPrevSentCAPMTVersion();
204 slot->sendCAPMT(pmthandler, caids);
206 // check if another service is running
207 it = m_pmt_handlers.begin();
208 while (it != m_pmt_handlers.end())
214 slot->resetPrevSentCAPMTVersion();
215 slot->sendCAPMT(it->pmthandler);
224 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
226 eDebug("[eDVBCIInterfaces] gotPMT");
227 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
228 eServiceReferenceDVB service;
229 if ( it != m_pmt_handlers.end() && it->cislot)
230 it->cislot->sendCAPMT(pmthandler);
233 int eDVBCIInterfaces::getMMIState(int slotid)
237 if( (slot = getSlot(slotid)) == 0 )
240 return slot->getMMIState();
243 int eDVBCISlot::send(const unsigned char *data, size_t len)
249 // printf("%02x ",data[i]);
252 res = ::write(fd, data, len);
254 //printf("write() %d\n",res);
256 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
261 void eDVBCISlot::data(int what)
263 if(what == eSocketNotifier::Priority) {
264 if(state != stateRemoved) {
265 state = stateRemoved;
267 printf("ci removed\n");
268 notifier->setRequested(eSocketNotifier::Read);
270 eDVBCI_UI::getInstance()->setState(0,0);
277 r = ::read(fd, data, 4096);
279 if(state != stateInserted) {
280 state = stateInserted;
281 eDebug("ci inserted");
284 eDVBCI_UI::getInstance()->setState(0,1);
286 /* enable PRI to detect removal or errors */
287 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
294 // printf("%02x ",data[i]);
296 eDVBCISession::receiveData(this, data, r);
297 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
301 if(what == eSocketNotifier::Write) {
302 if(eDVBCISession::pollAll() == 0) {
303 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
308 DEFINE_REF(eDVBCISlot);
310 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
314 application_manager = 0;
321 sprintf(filename, "/dev/ci%d", nr);
323 fd = ::open(filename, O_RDWR | O_NONBLOCK);
325 eDebug("eDVBCISlot has fd %d", fd);
327 state = stateInserted;
331 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
332 CONNECT(notifier->activated, eDVBCISlot::data);
339 eDVBCISlot::~eDVBCISlot()
344 int eDVBCISlot::getSlotID()
349 int eDVBCISlot::reset()
351 printf("edvbcislot: reset requested\n");
360 int eDVBCISlot::initialize()
362 printf("edvbcislot: initialize()\n");
366 int eDVBCISlot::startMMI()
368 printf("edvbcislot: startMMI()\n");
370 if(application_manager)
371 application_manager->startMMI();
376 int eDVBCISlot::stopMMI()
378 printf("edvbcislot: stopMMI()\n");
381 mmi_session->stopMMI();
386 int eDVBCISlot::answerText(int answer)
388 printf("edvbcislot: answerText(%d)\n", answer);
391 mmi_session->answerText(answer);
396 int eDVBCISlot::getMMIState()
404 int eDVBCISlot::answerEnq(char *value)
406 printf("edvbcislot: answerENQ(%s)\n", value);
410 int eDVBCISlot::cancelEnq()
412 printf("edvbcislot: cancelENQ\n");
415 mmi_session->cancelEnq();
420 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
422 const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
423 ePtr<eTable<ProgramMapSection> > ptr;
424 if (pmthandler->getPMT(ptr))
428 eDVBTableSpec table_spec;
429 ptr->getSpec(table_spec);
430 int pmt_version = table_spec.version & 0x1F; // just 5 bits
431 if ( pmt_version == prev_sent_capmt_version )
433 eDebug("[eDVBCISlot] dont sent self capmt version twice");
436 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
437 if ( i == ptr->getSections().end() )
441 unsigned char raw_data[2048];
442 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
443 while( i != ptr->getSections().end() )
448 capmt.writeToBuffer(raw_data);
450 // begin calc capmt length
453 if ( raw_data[3] & 0x80 )
456 int lenbytes = raw_data[3] & ~0x80;
458 wp = (wp << 8) | raw_data[4 + i++];
469 // end calc capmt length
471 eDebug("no ca_manager !!! dump unfiltered capmt:");
473 eDebug("ca_manager %p dump capmt:", ca_manager);
474 for(int i=0;i<wp;i++)
475 eDebugNoNewLine("%02x ", raw_data[i]);
480 //dont need tag and lenfield
481 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
482 prev_sent_capmt_version = pmt_version;
489 int eDVBCISlot::enableTS(int enable)
491 printf("eDVBCISlot::enableTS(%d)\n", enable);
494 if((f = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
495 printf("cannot open /proc/stb/audio/j1_mute\n");
499 fprintf(f, "%s", enable?"CI":"A");
507 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");