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, int answer, char *value)
123 if( (slot = getSlot(slotid)) == 0 )
126 return slot->answerEnq(answer, value);
129 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
131 CIPmtHandler new_handler(pmthandler);
133 eServiceReferenceDVB service;
134 pmthandler->getService(service);
136 PMTHandlerSet::iterator it = m_pmt_handlers.begin();
137 while (it != m_pmt_handlers.end())
139 eServiceReferenceDVB ref;
140 it->pmthandler->getService(ref);
141 if ( service == ref && it->usedby )
142 new_handler.usedby = it->usedby;
145 m_pmt_handlers.insert(new_handler);
148 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
150 PMTHandlerSet::iterator it=m_pmt_handlers.find(pmthandler);
151 if (it != m_pmt_handlers.end())
153 eDVBCISlot *slot = it->usedby;
154 eDVBServicePMTHandler *pmthandler = it->pmthandler;
155 m_pmt_handlers.erase(it);
158 eServiceReferenceDVB removed_service;
159 pmthandler->getService(removed_service);
160 PMTHandlerSet::iterator it=m_pmt_handlers.begin();
161 while (it != m_pmt_handlers.end())
163 eServiceReferenceDVB ref;
164 it->pmthandler->getService(ref);
165 if (ref == removed_service)
169 if ( it == m_pmt_handlers.end() && slot->getPrevSentCAPMTVersion() != 0xFF )
171 std::vector<uint16_t> caids;
172 caids.push_back(0xFFFF);
173 slot->sendCAPMT(pmthandler, caids);
179 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
181 eDebug("[eDVBCIInterfaces] gotPMT");
182 PMTHandlerSet::iterator it=m_pmt_handlers.find(pmthandler);
183 eServiceReferenceDVB service;
184 if ( it != m_pmt_handlers.end() )
186 eDebug("[eDVBCIInterfaces] usedby %p", it->usedby);
189 // HACK this assigns ALL RUNNING SERVICES to the first free CI !!!
190 for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
192 /* eDVBCISlot **usedby = &it->usedby;
194 (*usedby)->resetPrevSentCAPMTVersion();
200 it->usedby->sendCAPMT(pmthandler);
204 int eDVBCIInterfaces::getMMIState(int slotid)
208 if( (slot = getSlot(slotid)) == 0 )
211 return slot->getMMIState();
214 int eDVBCISlot::send(const unsigned char *data, size_t len)
220 // printf("%02x ",data[i]);
223 res = ::write(fd, data, len);
225 //printf("write() %d\n",res);
227 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
232 void eDVBCISlot::data(int what)
234 if(what == eSocketNotifier::Priority) {
235 if(state != stateRemoved) {
236 state = stateRemoved;
237 printf("ci removed\n");
238 notifier->setRequested(eSocketNotifier::Read);
240 eDVBCI_UI::getInstance()->setState(0,0);
247 r = ::read(fd, data, 4096);
249 if(state != stateInserted) {
250 state = stateInserted;
251 eDebug("ci inserted");
254 eDVBCI_UI::getInstance()->setState(0,1);
256 /* enable PRI to detect removal or errors */
257 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
264 // printf("%02x ",data[i]);
266 eDVBCISession::receiveData(this, data, r);
267 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
271 if(what == eSocketNotifier::Write) {
272 if(eDVBCISession::pollAll() == 0) {
273 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
278 DEFINE_REF(eDVBCISlot);
280 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
284 application_manager = 0;
290 sprintf(filename, "/dev/ci%d", nr);
292 fd = ::open(filename, O_RDWR | O_NONBLOCK);
294 eDebug("eDVBCISlot has fd %d", fd);
296 state = stateInserted;
300 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
301 CONNECT(notifier->activated, eDVBCISlot::data);
308 eDVBCISlot::~eDVBCISlot()
312 int eDVBCISlot::getSlotID()
317 int eDVBCISlot::reset()
319 printf("edvbcislot: reset requested\n");
326 int eDVBCISlot::initialize()
328 printf("edvbcislot: initialize()\n");
332 int eDVBCISlot::startMMI()
334 printf("edvbcislot: startMMI()\n");
336 if(application_manager)
337 application_manager->startMMI();
342 int eDVBCISlot::stopMMI()
344 printf("edvbcislot: stopMMI()\n");
347 mmi_session->stopMMI();
352 int eDVBCISlot::answerText(int answer)
354 printf("edvbcislot: answerText(%d)\n", answer);
357 mmi_session->answerText(answer);
362 int eDVBCISlot::getMMIState()
370 int eDVBCISlot::answerEnq(int answer, char *value)
372 printf("edvbcislot: answerMMI(%d,%s)\n", answer, value);
376 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
378 const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
379 ePtr<eTable<ProgramMapSection> > ptr;
380 if (pmthandler->getPMT(ptr))
384 eDVBTableSpec table_spec;
385 ptr->getSpec(table_spec);
386 int pmt_version = table_spec.version & 0x1F; // just 5 bits
387 if ( pmt_version == prev_sent_capmt_version )
389 eDebug("[eDVBCISlot] dont sent self capmt version twice");
392 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
393 if ( i == ptr->getSections().end() )
397 unsigned char raw_data[2048];
398 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
399 while( i != ptr->getSections().end() )
404 capmt.writeToBuffer(raw_data);
406 // begin calc capmt length
409 if ( raw_data[3] & 0x80 )
412 int lenbytes = raw_data[3] & ~0x80;
414 wp |= (raw_data[4+i] << (8 * i++));
425 // end calc capmt length
427 eDebug("no ca_manager !!! dump unfiltered capmt:");
429 eDebug("ca_manager %p dump capmt:", ca_manager);
430 for(int i=0;i<wp;i++)
431 eDebugNoNewLine("%02x ", raw_data[i]);
436 //dont need tag and lenfield
437 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
438 prev_sent_capmt_version = pmt_version;
445 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");