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::initialize(int slotid)
83 if( (slot = getSlot(slotid)) == 0 )
86 return slot->initialize();
89 int eDVBCIInterfaces::startMMI(int slotid)
93 if( (slot = getSlot(slotid)) == 0 )
96 return slot->startMMI();
99 int eDVBCIInterfaces::stopMMI(int slotid)
103 if( (slot = getSlot(slotid)) == 0 )
106 return slot->stopMMI();
109 int eDVBCIInterfaces::answerText(int slotid, int answer)
113 if( (slot = getSlot(slotid)) == 0 )
116 return slot->answerText(answer);
119 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
123 if( (slot = getSlot(slotid)) == 0 )
126 return slot->answerEnq(value);
129 int eDVBCIInterfaces::cancelEnq(int slotid)
133 if( (slot = getSlot(slotid)) == 0 )
136 return slot->cancelEnq();
139 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
141 CIPmtHandler new_handler(pmthandler);
143 eServiceReferenceDVB service;
144 pmthandler->getService(service);
146 eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
148 // HACK the first service get the CI..
149 eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
150 for (; ci_it != m_slots.end(); ++ci_it)
152 if (ci_it->use_count)
155 new_handler.cislot = ci_it;
156 new_handler.cislot->resetPrevSentCAPMTVersion();
159 if (ci_it == m_slots.end())
161 PMTHandlerList::iterator it = m_pmt_handlers.begin();
162 while (it != m_pmt_handlers.end())
164 eServiceReferenceDVB ref;
165 it->pmthandler->getService(ref);
166 if ( service == ref && it->cislot )
168 new_handler.cislot = it->cislot;
169 ++new_handler.cislot->use_count;
176 m_pmt_handlers.push_back(new_handler);
179 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
181 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
182 if (it != m_pmt_handlers.end())
184 eDVBCISlot *slot = it->cislot;
185 eDVBServicePMTHandler *pmthandler = it->pmthandler;
186 m_pmt_handlers.erase(it);
187 if (slot && !--slot->use_count)
190 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
191 std::vector<uint16_t> caids;
192 caids.push_back(0xFFFF);
193 slot->resetPrevSentCAPMTVersion();
194 slot->sendCAPMT(pmthandler, caids);
196 // check if another service is running
197 it = m_pmt_handlers.begin();
198 while (it != m_pmt_handlers.end())
204 slot->resetPrevSentCAPMTVersion();
205 slot->sendCAPMT(it->pmthandler);
214 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
216 eDebug("[eDVBCIInterfaces] gotPMT");
217 PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
218 eServiceReferenceDVB service;
219 if ( it != m_pmt_handlers.end() && it->cislot)
220 it->cislot->sendCAPMT(pmthandler);
223 int eDVBCIInterfaces::getMMIState(int slotid)
227 if( (slot = getSlot(slotid)) == 0 )
230 return slot->getMMIState();
233 int eDVBCISlot::send(const unsigned char *data, size_t len)
239 // printf("%02x ",data[i]);
242 res = ::write(fd, data, len);
244 //printf("write() %d\n",res);
246 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
251 void eDVBCISlot::data(int what)
253 if(what == eSocketNotifier::Priority) {
254 if(state != stateRemoved) {
255 state = stateRemoved;
256 printf("ci removed\n");
257 notifier->setRequested(eSocketNotifier::Read);
259 eDVBCI_UI::getInstance()->setState(0,0);
266 r = ::read(fd, data, 4096);
268 if(state != stateInserted) {
269 state = stateInserted;
270 eDebug("ci inserted");
273 eDVBCI_UI::getInstance()->setState(0,1);
275 /* enable PRI to detect removal or errors */
276 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
283 // printf("%02x ",data[i]);
285 eDVBCISession::receiveData(this, data, r);
286 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
290 if(what == eSocketNotifier::Write) {
291 if(eDVBCISession::pollAll() == 0) {
292 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
297 DEFINE_REF(eDVBCISlot);
299 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
303 application_manager = 0;
310 sprintf(filename, "/dev/ci%d", nr);
312 fd = ::open(filename, O_RDWR | O_NONBLOCK);
314 eDebug("eDVBCISlot has fd %d", fd);
316 state = stateInserted;
320 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
321 CONNECT(notifier->activated, eDVBCISlot::data);
328 eDVBCISlot::~eDVBCISlot()
332 int eDVBCISlot::getSlotID()
337 int eDVBCISlot::reset()
339 printf("edvbcislot: reset requested\n");
346 int eDVBCISlot::initialize()
348 printf("edvbcislot: initialize()\n");
352 int eDVBCISlot::startMMI()
354 printf("edvbcislot: startMMI()\n");
356 if(application_manager)
357 application_manager->startMMI();
362 int eDVBCISlot::stopMMI()
364 printf("edvbcislot: stopMMI()\n");
367 mmi_session->stopMMI();
372 int eDVBCISlot::answerText(int answer)
374 printf("edvbcislot: answerText(%d)\n", answer);
377 mmi_session->answerText(answer);
382 int eDVBCISlot::getMMIState()
390 int eDVBCISlot::answerEnq(char *value)
392 printf("edvbcislot: answerENQ(%s)\n", value);
396 int eDVBCISlot::cancelEnq()
398 printf("edvbcislot: cancelENQ\n");
401 mmi_session->cancelEnq();
406 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
408 const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
409 ePtr<eTable<ProgramMapSection> > ptr;
410 if (pmthandler->getPMT(ptr))
414 eDVBTableSpec table_spec;
415 ptr->getSpec(table_spec);
416 int pmt_version = table_spec.version & 0x1F; // just 5 bits
417 if ( pmt_version == prev_sent_capmt_version )
419 eDebug("[eDVBCISlot] dont sent self capmt version twice");
422 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
423 if ( i == ptr->getSections().end() )
427 unsigned char raw_data[2048];
428 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
429 while( i != ptr->getSections().end() )
434 capmt.writeToBuffer(raw_data);
436 // begin calc capmt length
439 if ( raw_data[3] & 0x80 )
442 int lenbytes = raw_data[3] & ~0x80;
444 wp = (wp << 8) | raw_data[4 + i++];
455 // end calc capmt length
457 eDebug("no ca_manager !!! dump unfiltered capmt:");
459 eDebug("ca_manager %p dump capmt:", ca_manager);
460 for(int i=0;i<wp;i++)
461 eDebugNoNewLine("%02x ", raw_data[i]);
466 //dont need tag and lenfield
467 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
468 prev_sent_capmt_version = pmt_version;
475 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");