well...
[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                                 eDVBCISlot **usedby = &it->usedby;
193                                 *usedby = ci_it;
194                                 (*usedby)->resetPrevSentCAPMTVersion();
195                                 break;
196                                 
197                         }
198                 }
199                 if (it->usedby)
200                         it->usedby->sendCAPMT(pmthandler);
201         }
202 }
203
204 int eDVBCIInterfaces::getMMIState(int slotid)
205 {
206         eDVBCISlot *slot;
207
208         if( (slot = getSlot(slotid)) == 0 )
209                 return -1;
210         
211         return slot->getMMIState();
212 }
213
214 int eDVBCISlot::send(const unsigned char *data, size_t len)
215 {
216         int res;
217         //int i;
218         //printf("< ");
219         //for(i=0;i<len;i++)
220         //      printf("%02x ",data[i]);
221         //printf("\n");
222
223         res = ::write(fd, data, len);
224
225         //printf("write() %d\n",res);
226
227         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
228
229         return res;
230 }
231
232 void eDVBCISlot::data(int what)
233 {
234         if(what == eSocketNotifier::Priority) {
235                 if(state != stateRemoved) {
236                         state = stateRemoved;
237                         printf("ci removed\n");
238                         notifier->setRequested(eSocketNotifier::Read);
239                         //HACK
240                         eDVBCI_UI::getInstance()->setState(0,0);
241                 }
242                 return;
243         }
244
245         __u8 data[4096];
246         int r;
247         r = ::read(fd, data, 4096);
248
249         if(state != stateInserted) {
250                 state = stateInserted;
251                 eDebug("ci inserted");
252
253                 //HACK
254                 eDVBCI_UI::getInstance()->setState(0,1);
255
256                 /* enable PRI to detect removal or errors */
257                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
258         }
259
260         if(r > 0) {
261                 //int i;
262                 //printf("> ");
263                 //for(i=0;i<r;i++)
264                 //      printf("%02x ",data[i]);
265                 //printf("\n");
266                 eDVBCISession::receiveData(this, data, r);
267                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
268                 return;
269         }
270
271         if(what == eSocketNotifier::Write) {
272                 if(eDVBCISession::pollAll() == 0) {
273                         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
274                 }
275         }
276 }
277
278 DEFINE_REF(eDVBCISlot);
279
280 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
281 {
282         char filename[128];
283
284         application_manager = 0;
285         mmi_session = 0;
286         ca_manager = 0;
287         
288         slotid = nr;
289
290         sprintf(filename, "/dev/ci%d", nr);
291
292         fd = ::open(filename, O_RDWR | O_NONBLOCK);
293
294         eDebug("eDVBCISlot has fd %d", fd);
295         
296         state = stateInserted;
297
298         if (fd >= 0)
299         {
300                 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
301                 CONNECT(notifier->activated, eDVBCISlot::data);
302         } else
303         {
304                 perror(filename);
305         }
306 }
307
308 eDVBCISlot::~eDVBCISlot()
309 {
310 }
311
312 int eDVBCISlot::getSlotID()
313 {
314         return slotid;
315 }
316
317 int eDVBCISlot::reset()
318 {
319         printf("edvbcislot: reset requested\n");
320
321         ioctl(fd, 0);
322
323         return 0;
324 }
325
326 int eDVBCISlot::initialize()
327 {
328         printf("edvbcislot: initialize()\n");
329         return 0;
330 }
331
332 int eDVBCISlot::startMMI()
333 {
334         printf("edvbcislot: startMMI()\n");
335         
336         if(application_manager)
337                 application_manager->startMMI();
338         
339         return 0;
340 }
341
342 int eDVBCISlot::stopMMI()
343 {
344         printf("edvbcislot: stopMMI()\n");
345
346         if(mmi_session)
347                 mmi_session->stopMMI();
348         
349         return 0;
350 }
351
352 int eDVBCISlot::answerText(int answer)
353 {
354         printf("edvbcislot: answerText(%d)\n", answer);
355
356         if(mmi_session)
357                 mmi_session->answerText(answer);
358
359         return 0;
360 }
361
362 int eDVBCISlot::getMMIState()
363 {
364         if(mmi_session)
365                 return 1;
366
367         return 0;
368 }
369
370 int eDVBCISlot::answerEnq(int answer, char *value)
371 {
372         printf("edvbcislot: answerMMI(%d,%s)\n", answer, value);
373         return 0;
374 }
375
376 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
377 {
378         const std::vector<uint16_t> &caids = ids.empty() && ca_manager ? ca_manager->getCAIDs() : ids;
379         ePtr<eTable<ProgramMapSection> > ptr;
380         if (pmthandler->getPMT(ptr))
381                 return -1;
382         else
383         {
384                 eDVBTableSpec table_spec;
385                 ptr->getSpec(table_spec);
386                 int pmt_version = table_spec.version & 0x1F; // just 5 bits
387                 if ( pmt_version == prev_sent_capmt_version )
388                 {
389                         eDebug("[eDVBCISlot] dont sent self capmt version twice");
390                         return -1;
391                 }
392                 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
393                 if ( i == ptr->getSections().end() )
394                         return -1;
395                 else
396                 {
397                         unsigned char raw_data[2048];
398                         CaProgramMapSection capmt(*i++, prev_sent_capmt_version != 0xFF ? 0x05 /*update*/ : 0x03 /*only*/, 0x01, caids );
399                         while( i != ptr->getSections().end() )
400                         {
401                 //                      eDebug("append");
402                                 capmt.append(*i++);
403                         }
404                         capmt.writeToBuffer(raw_data);
405 #if 1
406 // begin calc capmt length
407                         int wp=0;
408                         int hlen;
409                         if ( raw_data[3] & 0x80 )
410                         {
411                                 int i=0;
412                                 int lenbytes = raw_data[3] & ~0x80;
413                                 while(i < lenbytes)
414                                         wp |= (raw_data[4+i] << (8 * i++));
415                                 wp+=4;
416                                 wp+=lenbytes;
417                                 hlen = 4 + lenbytes;
418                         }
419                         else
420                         {
421                                 wp = raw_data[3];
422                                 wp+=4;
423                                 hlen = 4;
424                         }
425 // end calc capmt length
426                         if (!ca_manager)
427                                 eDebug("no ca_manager !!! dump unfiltered capmt:");
428                         else
429                                 eDebug("ca_manager %p dump capmt:", ca_manager);
430                         for(int i=0;i<wp;i++)
431                                 eDebugNoNewLine("%02x ", raw_data[i]);
432                         eDebug("");
433 #endif
434                         if (ca_manager)
435                         {
436                                 //dont need tag and lenfield
437                                 ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
438                                 prev_sent_capmt_version = pmt_version;
439                         }
440                 }
441         }
442         
443 }
444
445 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");