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