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