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