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