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