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