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