add a small ipkg upgrade plugin
[enigma2.git] / lib / dvb_ci / dvbci.cpp
1 #include <fcntl.h>
2 #include <sys/ioctl.h>
3
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/ebase.h>
7
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>
16
17 #include <dvbsi++/ca_program_map_section.h>
18
19 eDVBCIInterfaces *eDVBCIInterfaces::instance = 0;
20
21 eDVBCIInterfaces::eDVBCIInterfaces()
22 {
23         int num_ci = 0;
24         
25         instance = this;
26         
27         eDebug("scanning for common interfaces..");
28
29         while (1)
30         {
31                 struct stat s;
32                 char filename[128];
33                 sprintf(filename, "/dev/ci%d", num_ci);
34
35                 if (stat(filename, &s))
36                         break;
37
38                 ePtr<eDVBCISlot> cislot;
39
40                 cislot = new eDVBCISlot(eApp, num_ci);
41                 m_slots.push_back(cislot);
42
43                 ++num_ci;
44         }
45
46         eDebug("done, found %d common interface slots", num_ci);
47 }
48
49 eDVBCIInterfaces::~eDVBCIInterfaces()
50 {
51 }
52
53 eDVBCIInterfaces *eDVBCIInterfaces::getInstance()
54 {
55         return instance;
56 }
57
58 eDVBCISlot *eDVBCIInterfaces::getSlot(int slotid)
59 {
60         for(eSmartPtrList<eDVBCISlot>::iterator i(m_slots.begin()); i != m_slots.end(); ++i)
61                 if(i->getSlotID() == slotid)
62                         return i;
63
64         printf("FIXME: request for unknown slot\n");
65                         
66         return 0;
67 }
68
69 int eDVBCIInterfaces::reset(int slotid)
70 {
71         eDVBCISlot *slot;
72
73         if( (slot = getSlot(slotid)) == 0 )
74                 return -1;
75
76         return slot->reset();
77 }
78
79 int eDVBCIInterfaces::enableTS(int slotid, int enable)
80 {
81         eDVBCISlot *slot;
82
83         if( (slot = getSlot(slotid)) == 0 )
84                 return -1;
85
86         return slot->enableTS(enable);
87 }
88
89 int eDVBCIInterfaces::initialize(int slotid)
90 {
91         eDVBCISlot *slot;
92
93         if( (slot = getSlot(slotid)) == 0 )
94                 return -1;
95
96         slot->resetPrevSentCAPMTVersion();
97         PMTHandlerList::iterator it = m_pmt_handlers.begin();
98         while (it != m_pmt_handlers.end())
99         {
100                 if ( it->cislot == slot )
101                 {
102                         slot->sendCAPMT(it->pmthandler);  // send capmt
103                         break;
104                 }
105                 ++it;
106         }
107
108         return slot->initialize();
109 }
110
111 int eDVBCIInterfaces::sendCAPMT(int slotid)
112 {
113         eDVBCISlot *slot;
114
115         if( (slot = getSlot(slotid)) == 0 )
116                 return -1;
117
118         slot->resetPrevSentCAPMTVersion();
119         PMTHandlerList::iterator it = m_pmt_handlers.begin();
120         while (it != m_pmt_handlers.end())
121         {
122                 if ( it->cislot == slot )
123                 {
124                         slot->sendCAPMT(it->pmthandler);  // send capmt
125                         return 0;
126                 }
127                 ++it;
128         }
129
130         return -1;
131 }
132
133 int eDVBCIInterfaces::startMMI(int slotid)
134 {
135         eDVBCISlot *slot;
136
137         if( (slot = getSlot(slotid)) == 0 )
138                 return -1;
139         
140         return slot->startMMI();
141 }
142
143 int eDVBCIInterfaces::stopMMI(int slotid)
144 {
145         eDVBCISlot *slot;
146
147         if( (slot = getSlot(slotid)) == 0 )
148                 return -1;
149         
150         return slot->stopMMI();
151 }
152
153 int eDVBCIInterfaces::answerText(int slotid, int answer)
154 {
155         eDVBCISlot *slot;
156
157         if( (slot = getSlot(slotid)) == 0 )
158                 return -1;
159         
160         return slot->answerText(answer);
161 }
162
163 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
164 {
165         eDVBCISlot *slot;
166
167         if( (slot = getSlot(slotid)) == 0 )
168                 return -1;
169         
170         return slot->answerEnq(value);
171 }
172
173 int eDVBCIInterfaces::cancelEnq(int slotid)
174 {
175         eDVBCISlot *slot;
176
177         if( (slot = getSlot(slotid)) == 0 )
178                 return -1;
179         
180         return slot->cancelEnq();
181 }
182
183 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
184 {
185         CIPmtHandler new_handler(pmthandler);
186
187         eServiceReferenceDVB service;
188         pmthandler->getService(service);
189
190         eDebug("[eDVBCIInterfaces] addPMTHandler %s", service.toString().c_str());
191
192         // HACK the first service get the CI..
193         eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin());
194         for (; ci_it != m_slots.end(); ++ci_it)
195         {
196                 if (ci_it->use_count)
197                         continue;
198                 ci_it->use_count=1;
199                 new_handler.cislot = ci_it;
200                 new_handler.cislot->resetPrevSentCAPMTVersion();
201         }
202
203         if (ci_it == m_slots.end())
204         {
205                 PMTHandlerList::iterator it = m_pmt_handlers.begin();
206                 while (it != m_pmt_handlers.end())
207                 {
208                         eServiceReferenceDVB ref;
209                         it->pmthandler->getService(ref);
210                         if ( service == ref && it->cislot )
211                         {
212                                 new_handler.cislot = it->cislot;
213                                 ++new_handler.cislot->use_count;
214                                 break;
215                         }
216                         ++it;
217                 }
218         }
219
220         m_pmt_handlers.push_back(new_handler);
221 }
222
223 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
224 {
225         PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
226         if (it != m_pmt_handlers.end())
227         {
228                 eDVBCISlot *slot = it->cislot;
229 //              eDVBServicePMTHandler *pmthandler = it->pmthandler;
230                 m_pmt_handlers.erase(it);
231                 if (slot && !--slot->use_count)
232                 {
233 #if 0
234                         eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt");
235                         std::vector<uint16_t> caids;
236                         caids.push_back(0xFFFF);
237                         slot->resetPrevSentCAPMTVersion();
238                         slot->sendCAPMT(pmthandler, caids);
239 #endif
240         // check if another service is running
241                         it = m_pmt_handlers.begin();
242                         while (it != m_pmt_handlers.end())
243                         {
244                                 if ( !it->cislot )
245                                 {
246                                         it->cislot = slot;
247                                         ++slot->use_count;
248                                         slot->resetPrevSentCAPMTVersion();
249                                         slot->sendCAPMT(it->pmthandler);
250                                         break;
251                                 }
252                                 ++it;
253                         }
254                 }
255         }
256 }
257
258 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
259 {
260         eDebug("[eDVBCIInterfaces] gotPMT");
261         PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
262         eServiceReferenceDVB service;
263         if ( it != m_pmt_handlers.end() && it->cislot)
264                 it->cislot->sendCAPMT(pmthandler);
265 }
266
267 int eDVBCIInterfaces::getMMIState(int slotid)
268 {
269         eDVBCISlot *slot;
270
271         if( (slot = getSlot(slotid)) == 0 )
272                 return -1;
273         
274         return slot->getMMIState();
275 }
276
277 int eDVBCISlot::send(const unsigned char *data, size_t len)
278 {
279         int res;
280         //int i;
281         //printf("< ");
282         //for(i=0;i<len;i++)
283         //      printf("%02x ",data[i]);
284         //printf("\n");
285
286         res = ::write(fd, data, len);
287
288         //printf("write() %d\n",res);
289
290         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
291
292         return res;
293 }
294
295 void eDVBCISlot::data(int what)
296 {
297         if(what == eSocketNotifier::Priority) {
298                 if(state != stateRemoved) {
299                         state = stateRemoved;
300                         enableTS(0);
301                         printf("ci removed\n");
302                         notifier->setRequested(eSocketNotifier::Read);
303                         //HACK
304                         eDVBCI_UI::getInstance()->setState(0,0);
305                 }
306                 return;
307         }
308
309         __u8 data[4096];
310         int r;
311         r = ::read(fd, data, 4096);
312
313         if(state != stateInserted) {
314                 state = stateInserted;
315                 eDebug("ci inserted");
316
317                 //HACK
318                 eDVBCI_UI::getInstance()->setState(0,1);
319
320                 /* enable PRI to detect removal or errors */
321                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
322         }
323
324         if(r > 0) {
325                 //int i;
326                 //printf("> ");
327                 //for(i=0;i<r;i++)
328                 //      printf("%02x ",data[i]);
329                 //printf("\n");
330                 eDVBCISession::receiveData(this, data, r);
331                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
332                 return;
333         }
334
335         if(what == eSocketNotifier::Write) {
336                 if(eDVBCISession::pollAll() == 0) {
337                         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
338                 }
339         }
340 }
341
342 DEFINE_REF(eDVBCISlot);
343
344 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
345 {
346         char filename[128];
347
348         application_manager = 0;
349         mmi_session = 0;
350         ca_manager = 0;
351         use_count = 0;
352         
353         slotid = nr;
354
355         sprintf(filename, "/dev/ci%d", nr);
356
357         fd = ::open(filename, O_RDWR | O_NONBLOCK);
358
359         eDebug("eDVBCISlot has fd %d", fd);
360         
361         state = stateInserted;
362
363         if (fd >= 0)
364         {
365                 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
366                 CONNECT(notifier->activated, eDVBCISlot::data);
367         } else
368         {
369                 perror(filename);
370         }
371 }
372
373 eDVBCISlot::~eDVBCISlot()
374 {
375         enableTS(0);
376 }
377
378 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
379 {
380         application_manager=session;
381 }
382
383 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
384 {
385         mmi_session = session;
386 }
387
388 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
389 {
390         ca_manager = session;
391 }
392
393 int eDVBCISlot::getSlotID()
394 {
395         return slotid;
396 }
397
398 int eDVBCISlot::reset()
399 {
400         printf("edvbcislot: reset requested\n");
401
402         enableTS(0);
403
404         ioctl(fd, 0);
405
406         return 0;
407 }
408
409 int eDVBCISlot::initialize()
410 {
411         printf("edvbcislot: initialize()\n");
412         return 0;
413 }
414
415 int eDVBCISlot::startMMI()
416 {
417         printf("edvbcislot: startMMI()\n");
418         
419         if(application_manager)
420                 application_manager->startMMI();
421         
422         return 0;
423 }
424
425 int eDVBCISlot::stopMMI()
426 {
427         printf("edvbcislot: stopMMI()\n");
428
429         if(mmi_session)
430                 mmi_session->stopMMI();
431         
432         return 0;
433 }
434
435 int eDVBCISlot::answerText(int answer)
436 {
437         printf("edvbcislot: answerText(%d)\n", answer);
438
439         if(mmi_session)
440                 mmi_session->answerText(answer);
441
442         return 0;
443 }
444
445 int eDVBCISlot::getMMIState()
446 {
447         if(mmi_session)
448                 return 1;
449
450         return 0;
451 }
452
453 int eDVBCISlot::answerEnq(char *value)
454 {
455         printf("edvbcislot: answerENQ(%s)\n", value);
456         return 0;
457 }
458
459 int eDVBCISlot::cancelEnq()
460 {
461         printf("edvbcislot: cancelENQ\n");
462
463         if(mmi_session)
464                 mmi_session->cancelEnq();
465
466         return 0;
467 }
468
469 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
470 {
471         if (!ca_manager)
472         {
473                 eDebug("no ca_manager (no CI plugged?)");
474                 return -1;
475         }
476         const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
477         ePtr<eTable<ProgramMapSection> > ptr;
478         if (pmthandler->getPMT(ptr))
479                 return -1;
480         else
481         {
482                 eDVBTableSpec table_spec;
483                 ptr->getSpec(table_spec);
484                 int pmt_version = table_spec.version & 0x1F; // just 5 bits
485                 if ( pmt_version == prev_sent_capmt_version )
486                 {
487                         eDebug("[eDVBCISlot] dont sent self capmt version twice");
488                         return -1;
489                 }
490                 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
491                 if ( i == ptr->getSections().end() )
492                         return -1;
493                 else
494                 {
495                         unsigned char raw_data[2048];
496                         CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
497                         while( i != ptr->getSections().end() )
498                         {
499                 //                      eDebug("append");
500                                 capmt.append(*i++);
501                         }
502                         capmt.writeToBuffer(raw_data);
503 #if 1
504 // begin calc capmt length
505                         int wp=0;
506                         int hlen;
507                         if ( raw_data[3] & 0x80 )
508                         {
509                                 int i=0;
510                                 int lenbytes = raw_data[3] & ~0x80;
511                                 while(i < lenbytes)
512                                         wp = (wp << 8) | raw_data[4 + i++];
513                                 wp+=4;
514                                 wp+=lenbytes;
515                                 hlen = 4 + lenbytes;
516                         }
517                         else
518                         {
519                                 wp = raw_data[3];
520                                 wp+=4;
521                                 hlen = 4;
522                         }
523 // end calc capmt length
524                         eDebug("ca_manager %p dump capmt:", ca_manager);
525                         for(int i=0;i<wp;i++)
526                                 eDebugNoNewLine("%02x ", raw_data[i]);
527                         eDebug("");
528 #endif
529                         //dont need tag and lenfield
530                         ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
531                         prev_sent_capmt_version = pmt_version;
532                 }
533         }
534         
535 }
536
537 int eDVBCISlot::enableTS(int enable)
538 {
539         printf("eDVBCISlot::enableTS(%d)\n", enable);
540
541         FILE *f;
542         if((f = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
543                 printf("cannot open /proc/stb/tsmux/input0\n");
544                 return 0;
545         }
546
547         fprintf(f, "%s", enable?"CI":"A");
548
549         fclose(f);
550
551         return 0;
552 }
553
554 void eDVBCISlot::resendCAPMT()
555 {
556         eDVBCIInterfaces::getInstance()->sendCAPMT(slotid);
557 }
558
559 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");