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