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();
199 it->usedby->sendCAPMT(pmthandler);
203 int eDVBCIInterfaces::getMMIState(int slotid)
207 if( (slot = getSlot(slotid)) == 0 )
210 return slot->getMMIState();
213 int eDVBCISlot::send(const unsigned char *data, size_t len)
219 // printf("%02x ",data[i]);
222 res = ::write(fd, data, len);
224 //printf("write() %d\n",res);
226 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
231 void eDVBCISlot::data(int what)
233 if(what == eSocketNotifier::Priority) {
234 if(state != stateRemoved) {
235 state = stateRemoved;
236 printf("ci removed\n");
237 notifier->setRequested(eSocketNotifier::Read);
239 eDVBCI_UI::getInstance()->setState(0,0);
246 r = ::read(fd, data, 4096);
248 if(state != stateInserted) {
249 state = stateInserted;
250 eDebug("ci inserted");
253 eDVBCI_UI::getInstance()->setState(0,1);
255 /* enable PRI to detect removal or errors */
256 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
263 // printf("%02x ",data[i]);
265 eDVBCISession::receiveData(this, data, r);
266 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
270 if(what == eSocketNotifier::Write) {
271 if(eDVBCISession::pollAll() == 0) {
272 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
277 DEFINE_REF(eDVBCISlot);
279 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
283 application_manager = 0;
289 sprintf(filename, "/dev/ci%d", nr);
291 fd = ::open(filename, O_RDWR | O_NONBLOCK);
293 eDebug("eDVBCISlot has fd %d", fd);
295 state = stateInserted;
299 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
300 CONNECT(notifier->activated, eDVBCISlot::data);
307 eDVBCISlot::~eDVBCISlot()
311 int eDVBCISlot::getSlotID()
316 int eDVBCISlot::reset()
318 printf("edvbcislot: reset requested\n");
325 int eDVBCISlot::initialize()
327 printf("edvbcislot: initialize()\n");
331 int eDVBCISlot::startMMI()
333 printf("edvbcislot: startMMI()\n");
335 if(application_manager)
336 application_manager->startMMI();
341 int eDVBCISlot::stopMMI()
343 printf("edvbcislot: stopMMI()\n");
346 mmi_session->stopMMI();
351 int eDVBCISlot::answerText(int answer)
353 printf("edvbcislot: answerText(%d)\n", answer);
356 mmi_session->answerText(answer);
361 int eDVBCISlot::getMMIState()
369 int eDVBCISlot::answerEnq(int answer, char *value)
371 printf("edvbcislot: answerMMI(%d,%s)\n", answer, value);
375 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
377 const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
378 ePtr<eTable<ProgramMapSection> > ptr;
379 if (pmthandler->getPMT(ptr))
383 eDVBTableSpec table_spec;
384 ptr->getSpec(table_spec);
385 int pmt_version = table_spec.version & 0x1F; // just 5 bits
386 if ( pmt_version == prev_sent_capmt_version )
388 eDebug("[eDVBCISlot] dont sent self capmt version twice");
391 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
392 if ( i == ptr->getSections().end() )
396 unsigned char raw_data[2048];
397 CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
398 while( i != ptr->getSections().end() )
403 capmt.writeToBuffer(raw_data);
405 // begin calc capmt length
408 if ( raw_data[3] & 0x80 )
411 int lenbytes = raw_data[3] & ~0x80;
413 wp |= (raw_data[4+i] << (8 * i++));
424 // end calc capmt length
426 eDebug("no ca_manager !!! dump unfiltered capmt:");
428 eDebug("ca_manager %p dump capmt:", ca_manager);
429 for(int i=0;i<wp;i++)
430 eDebugNoNewLine("%02x ", raw_data[i]);
435 //dont need tag and lenfield
436 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
437 prev_sent_capmt_version = pmt_version;
444 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");