source: branches/monitor-improvement/module-dvbapi.c@ 1624

Last change on this file since 1624 was 1624, checked in by alno, 11 years ago

WebIf:

  • Merging revisions 1619-1623 of trunk (ump?)
File size: 27.8 KB
Line 
1/*
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19#ifdef HAVE_DVBAPI
20
21#include <sys/un.h>
22
23#include "globals.h"
24
25#define BUFSIZE 512
26#define MAX_DEMUX 5
27#define MAX_CAID 50
28#define ECM_PIDS 20
29
30static int listenfd = -1;
31
32typedef struct ECMPIDS
33{
34 unsigned short CA_PID;
35 unsigned short CA_System_ID;
36 unsigned short EMM_PID;
37} ECMPIDSTYPE;
38
39typedef struct demux_s
40{
41 unsigned short demux_index;
42 unsigned short demux_ecm_fd;
43 unsigned short demux_emm_fd;
44 unsigned short cadev_index;
45 unsigned short ca_fd;
46 unsigned short active;
47 unsigned short ECMpidcount;
48 ECMPIDSTYPE ECMpids[ECM_PIDS];
49 unsigned short program_number;
50 unsigned short ca_system_id;
51 unsigned short ca_pid;
52 unsigned int provider_id;
53 unsigned short emm_pid;
54 unsigned short STREAMpidcount;
55 unsigned short STREAMpids[ECM_PIDS];
56 unsigned char buffer_cache_dmx[12];
57 unsigned char lastcw0[8];
58 unsigned char lastcw1[8];
59 pthread_t descramble_thread;
60 unsigned int thread_active;
61} DEMUXTYPE;
62#define DMX_FILTER_SIZE 16
63
64//dvbapi 1
65typedef struct dmxFilter
66{
67 uint8_t filter[DMX_FILTER_SIZE];
68 uint8_t mask[DMX_FILTER_SIZE];
69} dmxFilter_t;
70
71struct dmxSctFilterParams
72{
73 uint16_t pid;
74 dmxFilter_t filter;
75 uint32_t timeout;
76 uint32_t flags;
77#define DMX_CHECK_CRC 1
78#define DMX_ONESHOT 2
79#define DMX_IMMEDIATE_START 4
80#define DMX_BUCKET 0x1000 /* added in 2005.05.18 */
81#define DMX_KERNEL_CLIENT 0x8000
82};
83
84#define DMX_START1 _IOW('o',41,int)
85#define DMX_STOP1 _IOW('o',42,int)
86#define DMX_SET_FILTER1 _IOW('o',43,struct dmxSctFilterParams *)
87
88
89//dbox2+ufs
90typedef struct dmx_filter
91{
92 uint8_t filter[DMX_FILTER_SIZE];
93 uint8_t mask[DMX_FILTER_SIZE];
94 uint8_t mode[DMX_FILTER_SIZE];
95} dmx_filter_t;
96
97
98struct dmx_sct_filter_params
99{
100 uint16_t pid;
101 dmx_filter_t filter;
102 uint32_t timeout;
103 uint32_t flags;
104#define DMX_CHECK_CRC 1
105#define DMX_ONESHOT 2
106#define DMX_IMMEDIATE_START 4
107#define DMX_KERNEL_CLIENT 0x8000
108};
109
110typedef struct ca_descr {
111 unsigned int index;
112 unsigned int parity; /* 0 == even, 1 == odd */
113 unsigned char cw[8];
114} ca_descr_t;
115
116typedef struct ca_pid {
117 unsigned int pid;
118 int index; /* -1 == disable*/
119} ca_pid_t;
120
121#define DMX_START _IO('o', 41)
122#define DMX_STOP _IO('o', 42)
123#define DMX_SET_FILTER _IOW('o', 43, struct dmx_sct_filter_params)
124
125#define CA_SET_DESCR _IOW('o', 134, ca_descr_t)
126#define CA_SET_PID _IOW('o', 135, ca_pid_t)
127
128DEMUXTYPE demux[MAX_DEMUX];
129
130unsigned short global_caid_list[MAX_CAID];
131CAIDTAB prioritytab,ignoretab;
132
133#define BOX_COUNT 2
134struct box_devices
135{
136 char ca_device_path[32];
137 char demux_device_path[32];
138 char cam_socket_path[32];
139};
140
141struct box_devices devices[BOX_COUNT] = {
142 /* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter0/ca%d", "/dev/dvb/adapter0/demux%d", "/var/tmp/camd.socket" },
143 /* dreambox (dvb-api-1)*/ { "/dev/dvb/card0/ca%d", "/dev/dvb/card0/demux%d", "/var/tmp/camd.socket" }
144};
145
146int selected_box=-1;
147int selected_api=-1;
148
149int dvbapi_set_filter(int dmx_fd, int api, unsigned short pid, unsigned char filt, unsigned char mask, int timeout)
150{
151 int command,ret=-1;
152
153 cs_debug("dvbapi: set filter pid:%04x, value:%04x",pid, filt);
154
155 switch(api)
156 {
157 case 0:
158 //dvbapi 3
159 command=0;
160 struct dmx_sct_filter_params sFP2;
161
162 memset(&sFP2,0,sizeof(sFP2));
163
164 sFP2.pid = pid;
165 sFP2.timeout = timeout;
166 sFP2.flags = DMX_IMMEDIATE_START;
167 sFP2.filter.filter[0] = filt;
168 sFP2.filter.mask[0] = mask;
169 ret=ioctl(dmx_fd, DMX_SET_FILTER, &sFP2);
170
171 break;
172 case 1:
173 //dvbapi 1
174 command=0;
175 struct dmxSctFilterParams sFP1;
176
177 memset(&sFP1,0,sizeof(sFP1));
178
179 sFP1.pid = pid;
180 sFP1.timeout = timeout;
181 sFP1.flags = DMX_IMMEDIATE_START;
182 sFP1.filter.filter[0] = filt;
183 sFP1.filter.mask[0] = mask;
184 ret=ioctl(dmx_fd, DMX_SET_FILTER1, &sFP1);
185
186 break;
187 default:
188 break;
189 }
190
191 if (ret < 0)
192 cs_debug("dvbapi: could not start demux filter (Errno: %d)", errno);
193
194 return ret;
195}
196
197
198int dvbapi_check_array(unsigned short *array, int len, unsigned short match)
199{
200 int i;
201 for (i=0; i<len; i++) {
202 if (array[i]==match) {
203 return i;
204 }
205 }
206 return -1;
207}
208
209int dvbapi_detect_api()
210{
211 int num_apis=2;
212 int i,devnum=-1;
213 int apinum=-1;
214 int dmx_fd=0;
215
216 char device_path[128];
217
218 for (i=0;i<BOX_COUNT;i++)
219 {
220 sprintf(device_path, devices[i].demux_device_path, 0);
221 if ((dmx_fd = open(device_path, O_RDWR)) > 0) {
222 devnum=i;
223 break;
224 }
225 }
226
227 if (dmx_fd < 0) return 0;
228
229 int command,ret=0;
230
231 for (i=0;i<num_apis;i++)
232 {
233 ret=dvbapi_set_filter(dmx_fd, i, 0x0001, 0x01, 0xFF, 1);
234
235 if (ret >= 0)
236 {
237 apinum=i;
238 break;
239 }
240 }
241 close(dmx_fd);
242
243 selected_box=devnum;
244 selected_api=apinum;
245 cs_debug("dvbapi: Detected %s Api: %d", device_path, apinum);
246
247 return 1;
248}
249
250int dvbapi_read_device(int dmx_fd, unsigned char *buf, int length, int debug)
251{
252 int len, rc;
253 struct pollfd pfd[1];
254
255 pfd[0].fd = dmx_fd;
256 pfd[0].events = (POLLIN | POLLPRI);
257
258 rc = poll(pfd, 1, 5000);
259 if (rc<1)
260 return -1;
261
262 len = read(dmx_fd, buf, length);
263
264 if (len==-1)
265 cs_log("dvbapi: read error %d", errno);
266
267 if (debug==1)
268 cs_debug("dvbapi: Read %d bytes from demux", len);
269
270 return len;
271}
272
273int dvbapi_open_device(int index_demux, int type)
274{
275 int dmx_fd,i;
276 int ca_offset=0;
277 char device_path[128];
278
279 if (type==0)
280 sprintf(device_path, devices[selected_box].demux_device_path, demux[index_demux].demux_index);
281 else
282 {
283 if (strcmp(cfg->dvbapi_boxtype, "ufs910")==0 || strcmp(cfg->dvbapi_boxtype, "dbox2")==0)
284 ca_offset=1;
285
286 sprintf(device_path, devices[selected_box].ca_device_path, demux[index_demux].cadev_index+ca_offset);
287 }
288
289 if ((dmx_fd = open(device_path, O_RDWR)) < 0) {
290 if (type==1 && errno==16) // ca device already open
291 for (i=0;i<MAX_DEMUX;i++)
292 if (demux[i].cadev_index==demux[index_demux].cadev_index && demux[i].ca_fd>0)
293 dmx_fd=demux[i].ca_fd;
294
295 if (dmx_fd<=0)
296 cs_debug("dvbapi: error opening device %s (Errno: %d)", device_path, errno);
297 }
298
299 cs_debug("dvbapi: DEVICE open (%s)", device_path);
300 return dmx_fd;
301}
302
303int dvbapi_stop_filter(int demux_index, int type)
304{
305 int dmx_fd;
306
307 if (type==0) {
308 dmx_fd=demux[demux_index].demux_ecm_fd;
309 } else {
310 dmx_fd=demux[demux_index].demux_emm_fd;
311 }
312
313 ioctl(dmx_fd,DMX_STOP);
314 return 1;
315}
316
317unsigned long dvbapi_get_provid(int demux_index, unsigned short pid)
318{
319 unsigned char buf[BUFSIZE];
320 int dmx_fd, len;
321
322 dmx_fd=demux[demux_index].demux_emm_fd;
323
324 dvbapi_set_filter(dmx_fd, selected_api, pid, 0x80, 0xF0, 4000);
325
326 len=dvbapi_read_device(dmx_fd, buf, BUFSIZE, 1);
327
328 if (len > 0) {
329 unsigned long provid = (buf[10] << 8) | buf[11];
330 return provid;
331 }
332
333 return 0;
334}
335
336unsigned short dvbapi_get_single_ecm(int demux_index, unsigned short caid, unsigned short pid, unsigned long provid)
337{
338 unsigned char buf[BUFSIZE];
339 int dmx_fd, len;
340
341 dmx_fd=demux[demux_index].demux_ecm_fd;
342
343 dvbapi_set_filter(dmx_fd, selected_api, pid, 0x80, 0xF0, 2000);
344
345 len=dvbapi_read_device(dmx_fd, buf, BUFSIZE, 1);
346
347 if (len > 0) {
348 ECM_REQUEST *er;
349
350 if (!(er=get_ecmtask()))
351 return 0;
352
353 er->srvid = demux[demux_index].program_number;
354 er->caid = caid;
355 er->pid = pid;
356 er->prid = provid;
357
358 er->l=len;
359 memcpy(er->ecm, buf, er->l);
360
361 get_cw(er);
362
363 memcpy(demux[demux_index].buffer_cache_dmx, buf, 12);
364 }
365 return 0;
366}
367
368void dvbapi_parse_cat(int demux_index)
369{
370 unsigned char buf[BUFSIZE];
371 unsigned short i, j;
372 int dmx_fd, len;
373
374 dmx_fd=demux[demux_index].demux_emm_fd;
375
376 dvbapi_set_filter(dmx_fd, selected_api, 0x0001, 0x01, 0xFF, 2000);
377
378 len=dvbapi_read_device(dmx_fd, buf, BUFSIZE, 1);
379
380 if (len<=0)
381 return;
382
383 cs_ddump(buf, len, "cat:");
384
385 for (i = 8; i < (((buf[1] & 0x0F) << 8) | buf[2]) - 1; i += buf[i + 1] + 2)
386 {
387 if (buf[i] != 0x09) continue;
388 unsigned short cat_sys_id=(((buf[i + 2] & 0x1F) << 8) | buf[i + 3]);
389 unsigned short emm_pid=(((buf[i + 4] & 0x1F) << 8) | buf[i + 5]);
390 cs_debug("cat: ca_system_id: %04x\temm_pid %04x", cat_sys_id, emm_pid);
391 for (j=0;j<demux[demux_index].ECMpidcount;j++) {
392 if (demux[demux_index].ECMpids[j].CA_System_ID==(((buf[i + 2] & 0x1F) << 8) | buf[i + 3])) {
393 demux[demux_index].ECMpids[j].EMM_PID=emm_pid;
394 break;
395 }
396 }
397 }
398
399 dvbapi_stop_filter(demux_index, 1);
400
401 return;
402}
403
404void dvbapi_stop_descrambling(int demux_id)
405{
406 int i;
407
408 cs_debug("dvbapi: Stop descrambling CAID: %04x", demux[demux_id].ca_system_id);
409
410 demux[demux_id].ca_system_id=0;
411 demux[demux_id].ca_pid=0;
412 demux[demux_id].emm_pid=0;
413 demux[demux_id].provider_id=0;
414 demux[demux_id].demux_index=-1;
415 demux[demux_id].program_number=0;
416
417 if (demux[demux_id].demux_ecm_fd>0) {
418 dvbapi_stop_filter(demux_id, 0);
419 close(demux[demux_id].demux_ecm_fd);
420 demux[demux_id].demux_ecm_fd=0;
421 }
422
423 if (demux[demux_id].demux_emm_fd>0) {
424 dvbapi_stop_filter(demux_id, 1);
425 close(demux[demux_id].demux_emm_fd);
426 demux[demux_id].demux_emm_fd=0;
427 }
428
429 if (demux[demux_id].thread_active == 0) {
430 pthread_cancel(demux[demux_id].descramble_thread);
431 pthread_join(demux[demux_id].descramble_thread, NULL);
432 demux[demux_id].thread_active = -1;
433 }
434
435 if (demux[demux_id].ca_fd>0) {
436 for (i=0;i<demux[demux_id].STREAMpidcount;i++)
437 {
438 ca_pid_t ca_pid2;
439 memset(&ca_pid2,0,sizeof(ca_pid2));
440 ca_pid2.pid = demux[demux_id].STREAMpids[i];
441 ca_pid2.index = -1;
442 if (ioctl(demux[demux_id].ca_fd, CA_SET_PID, &ca_pid2)==-1)
443 cs_debug("dvbapi: Error Stream Unset SET_PID");
444 }
445
446 int needed=0;
447 for (i=0;i<MAX_DEMUX;i++) {
448 if (demux[demux_id].ca_fd==demux[i].ca_fd && i != demux_id)
449 needed=1;
450 }
451 if (needed==0) {
452 close(demux[demux_id].ca_fd);
453 cs_debug("dvbapi: closing ca device");
454 }
455 demux[demux_id].ca_fd=0;
456 }
457
458 return;
459}
460
461void dvbapi_stop_descrambling_all(int demux_index)
462{
463 int j;
464 for (j=0;j<MAX_DEMUX;j++) {
465 if (demux[j].demux_index != demux_index)
466 continue;
467
468 dvbapi_stop_descrambling(j);
469 }
470
471 return;
472}
473
474void dvbapi_start_descrambling(int demux_index, unsigned short caid, unsigned short capid, unsigned long provider_id)
475{
476 int i;
477
478 cs_log("dvbapi: Start descrambling CAID: %04x", caid);
479
480 demux[demux_index].ca_pid=capid;
481 demux[demux_index].ca_system_id=caid;
482 demux[demux_index].provider_id=provider_id;
483
484 int camfd=dvbapi_open_device(demux_index,1);
485 if (camfd<=0) {
486 return;
487 }
488
489 demux[demux_index].ca_fd=camfd;
490
491 for (i=0;i<demux[demux_index].STREAMpidcount;i++)
492 {
493 ca_pid_t ca_pid2;
494 memset(&ca_pid2,0,sizeof(ca_pid2));
495 ca_pid2.pid = demux[demux_index].STREAMpids[i];
496 ca_pid2.index = demux_index;
497 if (ioctl(camfd, CA_SET_PID, &ca_pid2)==-1)
498 cs_debug("dvbapi: Error Stream SET_PID");
499 }
500
501}
502
503void dvbapi_try_descrambling (int demux_index)
504{
505 int n;
506 unsigned int program_number;
507
508 program_number=demux[demux_index].program_number;
509 for (n=0; n<demux[demux_index].ECMpidcount; n++) {
510
511 if (demux[demux_index].ca_system_id!=0) // got valid cw -> stop trying
512 break;
513
514 if (program_number!=demux[demux_index].program_number) //channel change -> stop trying
515 break;
516
517 dvbapi_stop_filter(demux_index,0);
518
519 cs_debug("dvbapi: trying CA_System_ID: %04x CA_PID: %04x EEM_PID: %04x", demux[demux_index].ECMpids[n].CA_System_ID, demux[demux_index].ECMpids[n].CA_PID, demux[demux_index].ECMpids[n].EMM_PID);
520
521 //grep emm provid
522 unsigned long provid=0;
523
524 int pid=dvbapi_check_array(prioritytab.caid, CS_MAXCAIDTAB, demux[demux_index].ECMpids[n].CA_System_ID);
525 if (pid>=0) {
526 if (prioritytab.mask[pid]>0)
527 provid=prioritytab.mask[pid];
528 }
529
530 if (cfg->dvbapi_au==1) {
531 dvbapi_stop_filter(demux_index,1);
532 if (demux[demux_index].ECMpids[n].EMM_PID>0)
533 provid=dvbapi_get_provid(demux_index, demux[demux_index].ECMpids[n].EMM_PID);
534 }
535
536 //grep ecm
537 dvbapi_get_single_ecm(demux_index, demux[demux_index].ECMpids[n].CA_System_ID, demux[demux_index].ECMpids[n].CA_PID, provid);
538
539 cs_sleepms(3000); //try next if no cw for .. secs
540 }
541}
542
543void *thread_descrambling(void *di)
544{
545 int demux_index=(int)di;
546 int dmx1_fd,dmx2_fd=0,n=30,k=0;
547 unsigned int program_number;
548
549 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
550 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
551
552 cs_debug("dvbapi: Start descrambling Thread %d", demux_index);
553
554 dmx1_fd = dvbapi_open_device(demux_index,0); //ECM
555 demux[demux_index].demux_ecm_fd=dmx1_fd;
556
557 if (cfg->dvbapi_au==1) {
558 dmx2_fd = dvbapi_open_device(demux_index,0); //EMM,CAT
559 demux[demux_index].demux_emm_fd=dmx2_fd;
560 dvbapi_parse_cat(demux_index);
561 }
562
563 program_number=demux[demux_index].program_number;
564
565 while(1)
566 {
567 if (demux[demux_index].ca_system_id!=0) // got valid cw -> stop trying
568 break;
569
570 if (program_number!=demux[demux_index].program_number) //channel change -> new thread
571 return 0;
572
573 if (n>=30)
574 {
575 if (demux[demux_index].ca_system_id==0)
576 dvbapi_try_descrambling(demux_index);
577 n=0;
578 k++;
579 continue;
580 }
581 n++;
582
583 if (k==5) // give up
584 return 0;
585
586 cs_sleepms(10000);
587 }
588
589 struct pollfd pfd2[2];
590 int rc,len,i,pfdcount;
591 unsigned char buffer[BUFSIZE];
592
593 while(1)
594 {
595 pfdcount=0;
596
597 if (program_number!=demux[demux_index].program_number || demux[demux_index].ca_system_id==0) //channel change -> new thread
598 break;
599
600 if (demux[demux_index].demux_ecm_fd>0) {
601 pfd2[pfdcount].fd = demux[demux_index].demux_ecm_fd;
602 pfd2[pfdcount].events = (POLLIN | POLLPRI);
603 pfdcount++;
604 } else break;
605 if (cfg->dvbapi_au==1) {
606 if (demux[demux_index].demux_emm_fd>0) {
607 pfd2[pfdcount].fd = demux[demux_index].demux_emm_fd;
608 pfd2[pfdcount].events = (POLLIN | POLLPRI);
609 pfdcount++;
610 } else break;
611 }
612
613 rc=poll(pfd2, pfdcount, 3000);
614
615 if (rc<0)
616 break;
617
618 for (i = 0; i < pfdcount; i++) {
619 if (pfd2[i].revents & (POLLIN | POLLPRI)) {
620
621 if ((len=dvbapi_read_device(pfd2[i].fd, buffer, BUFSIZE, 0)) <= 0)
622 continue;
623
624 if (pfd2[i].fd==demux[demux_index].demux_ecm_fd) {
625 //ECM
626
627 if (len != (((buffer[1] & 0xf) << 8) | buffer[2]) + 3) //invaild CAT length
628 continue;
629
630 if (buffer[0] == 0x80 || buffer[0] == 0x81)
631 {
632 if (memcmp(buffer, demux[demux_index].buffer_cache_dmx, 12) != 0) {
633 memcpy(demux[demux_index].buffer_cache_dmx, buffer, 12);
634 cs_debug("Read %d bytes\tTable-id: %02x\tCA section length: %d", len, buffer[0], len);
635
636 ECM_REQUEST *er;
637
638 if (!(er=get_ecmtask()))
639 continue;
640
641 er->srvid = demux[demux_index].program_number;
642 er->caid = demux[demux_index].ca_system_id;
643 er->prid = demux[demux_index].provider_id;
644
645 er->l=len;
646 memcpy(er->ecm, buffer, er->l);
647
648 get_cw(er);
649 }
650 }
651 }
652 if (pfd2[i].fd==demux[demux_index].demux_emm_fd) {
653 //EMM
654
655 cs_debug("EMM Type: 0x%02x", buffer[0]);
656 cs_ddump(buffer, len, "emm:");
657
658 //force emm output
659 reader[ridx].logemm=9999;
660
661 memset(&epg, 0, sizeof(epg));
662
663 epg.caid[0] = (uchar)(demux[demux_index].ca_system_id>>8);
664 epg.caid[1] = (uchar)(demux[demux_index].ca_system_id);
665
666 epg.provid[2] = (uchar)(demux[demux_index].provider_id>>8);
667 epg.provid[3] = (uchar)(demux[demux_index].provider_id);
668
669 epg.l=len;
670 memcpy(epg.emm, buffer, epg.l);
671 memcpy(epg.hexserial, reader[client[cs_idx].au].hexserial, 8);
672
673 do_emm(&epg);
674 }
675 }
676 }
677 }
678
679 cs_debug("dvbapi: Stop descrambling Thread %d", demux_index);
680
681 pthread_detach(pthread_self());
682 pthread_exit(0);
683}
684
685
686
687void dvbapi_resort_ecmpids(int demux_index)
688{
689 ECMPIDSTYPE tmppids[ECM_PIDS];
690 ECMPIDSTYPE tmppids2[ECM_PIDS];
691
692 int tmppidcount=0,tmppid2count=0,n;
693
694 int i,k,j;
695
696 for (i=0;i<MAX_CAID;i++)
697 global_caid_list[i]=0;
698
699 k=0;
700 for (i=0;i<CS_MAXREADER;i++) {
701 for (j=0;j<16;j++) {
702 if (reader[i].caid[j] != 0) {
703 if (k+1>=MAX_CAID) break;
704 global_caid_list[k]=reader[i].caid[j];
705 k++;
706 }
707 }
708 }
709 for (n=0; n<demux[demux_index].ECMpidcount; n++) {
710 if (dvbapi_check_array(ignoretab.caid, CS_MAXCAIDTAB, demux[demux_index].ECMpids[n].CA_System_ID)>=0) {
711 cs_debug("-> ignore %04x", demux[demux_index].ECMpids[n].CA_System_ID);
712 } else if (dvbapi_check_array(global_caid_list, MAX_CAID, demux[demux_index].ECMpids[n].CA_System_ID)>=0) {
713 cs_debug("-> caid list %04x", demux[demux_index].ECMpids[n].CA_System_ID);
714 tmppids[tmppidcount]=demux[demux_index].ECMpids[n];
715 tmppidcount++;
716 } else if (dvbapi_check_array(prioritytab.caid, CS_MAXCAIDTAB, demux[demux_index].ECMpids[n].CA_System_ID)>=0) {
717 cs_debug("-> priority %04x", demux[demux_index].ECMpids[n].CA_System_ID);
718 tmppids[tmppidcount]=demux[demux_index].ECMpids[n];
719 tmppidcount++;
720 } else {
721 tmppids2[tmppid2count]=demux[demux_index].ECMpids[n];
722 tmppid2count++;
723 }
724 }
725
726 for (n=0;n<tmppid2count;n++) {
727 tmppids[tmppidcount]=tmppids2[n];
728 tmppidcount++;
729 }
730
731 for (n=0; n<tmppidcount; n++) {
732 demux[demux_index].ECMpids[n]=tmppids[n];
733 }
734
735 demux[demux_index].ECMpidcount=tmppidcount;
736 cs_debug("dvbapi: ECMpidscount is now %d", demux[demux_index].ECMpidcount);
737
738 return;
739}
740
741void dvbapi_parse_descriptor(int demux_id, int i, unsigned int info_length, unsigned char *buffer) {
742 //int ca_pmt_cmd_id = buffer[i + 5];
743 unsigned int descriptor_length=0;
744 ushort added,j,n;
745
746 for (j = 0; j < info_length - 1; j += descriptor_length + 2) {
747 descriptor_length = buffer[i + j + 7];
748 int descriptor_ca_system_id = (buffer[i + j + 8] << 8) | buffer[i + j + 9];
749 int descriptor_ca_pid = ((buffer[i + j + 10] & 0x1F) << 8) | buffer[i + j + 11];
750
751 cs_debug("typ: %02x\tca_system_id: %04x\t ca_pid: %04x", buffer[i + j + 6], descriptor_ca_system_id, descriptor_ca_pid);
752
753 if (buffer[i + j + 6] == 0x09) {
754 added=0;
755 for (n=0;n<demux[demux_id].ECMpidcount;n++) {
756 if (demux[demux_id].ECMpids[n].CA_System_ID==descriptor_ca_system_id)
757 added=1;
758 }
759 if (added==0) {
760 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CA_PID=descriptor_ca_pid;
761 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].CA_System_ID=descriptor_ca_system_id;
762 demux[demux_id].ECMpids[demux[demux_id].ECMpidcount].EMM_PID=0;
763 demux[demux_id].ECMpidcount++;
764 }
765 }
766 }
767}
768
769// from tuxbox camd
770int dvbapi_parse_capmt(unsigned char *buffer, unsigned int length)
771{
772 unsigned short i, u;
773 unsigned short ca_mask=0x01, demux_index2=0x00;
774
775 int ca_pmt_list_management = buffer[0];
776 unsigned int program_number = (buffer[1] << 8) | buffer[2];
777 int program_info_length = ((buffer[4] & 0x0F) << 8) | buffer[5];
778
779 switch (ca_pmt_list_management)
780 {
781 case 0x01:
782 //(first)
783 break;
784 case 0x03:
785 //default (only)
786 break;
787 case 0x04:
788 //(add)
789 break;
790 default:
791 //FIXME?? (unknown)
792 break;
793 }
794
795 if (buffer[17]==0x82 && buffer[18]==0x02) {
796 //enigma2
797 ca_mask = buffer[19];
798 demux_index2 = buffer[20];
799 } else {
800 //neutrino workaround
801 dvbapi_stop_descrambling_all(0);
802 }
803
804 for (i=0;i<MAX_DEMUX;i++) {
805 if (demux[i].demux_index==demux_index2 && demux[i].program_number==((buffer[1] << 8) | buffer[2])) { // already descrambling prog on this device
806 if (demux[i].active==1) {
807 //remove any inactive program
808 for (u=0;u<MAX_DEMUX;u++) {
809 if (demux[u].demux_index==demux_index2 && demux[u].active==0)
810 dvbapi_stop_descrambling(u);
811 demux[u].active=0;
812 }
813 } else
814 demux[i].active=1;
815 return 0;
816 }
817 }
818
819 //get free id
820 int demux_id=-1;
821 for (i=0;i<MAX_DEMUX;i++) {
822 if (demux[i].program_number==0)
823 {
824 demux_id=i;
825 break;
826 }
827 }
828
829 if (demux_id<0) {
830 cs_log("dvbapi: error no free id (MAX_DEMUX)");
831 return 0;
832 }
833
834 cs_ddump(buffer, length, "capmt:");
835 cs_log("dvbapi: new program number: %04x", program_number);
836 //cs_debug("program_info_length: %d", program_info_length);
837
838 demux[demux_id].program_number=((buffer[1] << 8) | buffer[2]);
839 demux[demux_id].demux_index=demux_index2;
840 demux[demux_id].ECMpidcount=0;
841 demux[demux_id].STREAMpidcount=0;
842 demux[demux_id].active=1;
843 demux[demux_id].cadev_index=demux_index2;
844
845 for (i=0;i<8;i++) {
846 if (ca_mask & (1 << i)) {
847 demux[demux_id].cadev_index=i;
848 break;
849 }
850 }
851
852 cs_debug("dvbapi: demux index: %d ca index: %d", demux[demux_id].demux_index, demux[demux_id].cadev_index);
853
854 //CA_PIDS for all streams
855 if (program_info_length != 0) {
856 dvbapi_parse_descriptor(demux_id, 1, program_info_length, buffer);
857 }
858
859 //CA_PIDs for a single stream
860 unsigned int es_info_length=0;
861 for (i = program_info_length + 6; i < length; i += es_info_length + 5)
862 {
863 int stream_type = buffer[i];
864 unsigned short elementary_pid = ((buffer[i + 1] & 0x1F) << 8) | buffer[i + 2];
865 es_info_length = ((buffer[i + 3] & 0x0F) << 8) | buffer[i + 4];
866
867 cs_debug("stream_type: %02x\telementary_pid: %04x\tes_info_length: %04x", stream_type, elementary_pid, es_info_length);
868
869 demux[demux_id].STREAMpids[demux[demux_id].STREAMpidcount]=elementary_pid;
870 demux[demux_id].STREAMpidcount++;
871
872 if (es_info_length != 0) {
873 dvbapi_parse_descriptor(demux_id, i, es_info_length, buffer);
874 }
875 }
876
877 cs_debug("dvbapi: Found %d ECMpids and %d STREAMpids in PMT", demux[demux_id].ECMpidcount);
878
879 if (demux[demux_id].ECMpidcount>0) {
880 dvbapi_resort_ecmpids(demux_id);
881 if (demux[demux_id].ECMpidcount>0) {}
882 demux[demux_id].thread_active=pthread_create(&demux[demux_id].descramble_thread, NULL, thread_descrambling, (void*)demux_id);
883 }
884
885 return 0;
886}
887
888
889void dvbapi_handlesockmsg (unsigned char *buffer, unsigned int len)
890{
891 unsigned int val, size, i;
892
893 //cs_dump(buffer, len, "handlesockmsg:");
894
895 if (buffer[0] != 0x9F || buffer[1] != 0x80) {
896 cs_log("dvbapi: unknown socket command: %02x", buffer[0]);
897 return;
898 }
899
900 if (buffer[3] & 0x80) {
901 val = 0;
902 size = buffer[3] & 0x7F;
903 for (i = 0; i < size; i++)
904 val = (val << 8) | buffer[i + 1 + 3];
905 size++;
906 } else
907 {
908 val = buffer[3] & 0x7F;
909 size = 1;
910 }
911
912 switch(buffer[2])
913 {
914 case 0x30:
915 cs_debug("ca_info!!");
916 break;
917 case 0x32:
918 if ((3 + size + val) == len)
919 dvbapi_parse_capmt(buffer + 3 + size, val);
920 else {
921 cs_log("dvbapi: ca_pmt invalid length");
922 cs_dump(buffer, len, "invalid length:");
923 }
924 break;
925 case 0x3f:
926 //9F 80 3f 04 83 02 00 <demux index>
927 cs_ddump(buffer, len, "capmt 3f:");
928 int demux_index=buffer[7];
929 dvbapi_stop_descrambling_all(demux_index);
930 break;
931 default:
932 cs_log("dvbapi: handlesockmsg() unknown command");
933 cs_dump(buffer, len, "unknown command:");
934 break;
935 }
936}
937
938int dvbapi_init_listenfd() {
939
940 int clilen;
941 struct sockaddr_un servaddr;
942
943 memset(&servaddr, 0, sizeof(struct sockaddr_un));
944 servaddr.sun_family = AF_UNIX;
945 strcpy(servaddr.sun_path, devices[selected_box].cam_socket_path);
946 clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
947
948 if ((unlink(devices[selected_box].cam_socket_path) < 0) && (errno != ENOENT))
949 return 0;
950 if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
951 return 0;
952 if (bind(listenfd, (struct sockaddr *) &servaddr, clilen) < 0)
953 return 0;
954 if (listen(listenfd, 5) < 0)
955 return 0;
956
957 return 1;
958}
959
960void *thread_check_zap(void *arg) {
961 unsigned char buffer[BUFSIZE];
962 struct sockaddr_un servaddr;
963 ssize_t len;
964 int connfd,clilen;
965
966 while(1)
967 {
968 cs_sleepms(500);
969 //cs_debug("dvbapi: check zap");
970
971 connfd = accept(listenfd, (struct sockaddr *)&servaddr, (socklen_t *)&clilen);
972
973 if (connfd <= 0) { //socket not available
974 cs_debug("dvbapi: socket not available");
975 break;
976 }
977
978 len = read(connfd, buffer, sizeof(buffer));
979
980 if (len < 3) {
981 cs_debug("dvbapi: camd.socket: too short message received");
982 continue;
983 }
984
985 // if message begins with an apdu_tag and is longer than three bytes
986 if ((buffer[0] == 0x9F) && ((buffer[1] >> 7) == 0x01) && ((buffer[2] >> 7) == 0x00)) {
987 dvbapi_handlesockmsg(buffer, len);
988 } else {
989 cs_debug("dvbapi: invaild capmt");
990 }
991
992 close(connfd);
993
994 }
995
996 cs_log("dvbapi: check_zap() aborted");
997 return 0;
998}
999
1000void dvbapi_chk_caidtab(char *caidasc, CAIDTAB *ctab) {
1001
1002 char *ptr1, *ptr3;
1003 int i;
1004
1005 for (i=0, ptr1=strtok(caidasc, ","); (i<CS_MAXCAIDTAB) && (ptr1); ptr1=strtok(NULL, ","))
1006 {
1007 unsigned long caid, prov;
1008 if( (ptr3=strchr(trim(ptr1), ':')) )
1009 *ptr3++='\0';
1010 else
1011 ptr3="";
1012
1013 if (((caid=a2i(ptr1, 2))|(prov=a2i(ptr3, 3))) < 0x10000)
1014 {
1015 ctab->caid[i]=caid;
1016 ctab->mask[i]=prov;
1017 i++;
1018 }
1019 }
1020}
1021
1022int dvbapi_main_local()
1023{
1024 struct pollfd pfd2[1];
1025 int i,rc;
1026 pthread_t p1;
1027
1028 if (cfg->dvbapi_usr[0]==0) {
1029 //
1030 }
1031
1032 if (cfg->dvbapi_boxtype[0]==0) {
1033 strncpy(cfg->dvbapi_boxtype, "dreambox", sizeof(cfg->dvbapi_boxtype)-1);
1034 cs_log("dvbapi: boxtype not set. Assume boxtype=%s.", cfg->dvbapi_boxtype);
1035 } else
1036 cs_log("dvbapi: boxtype=%s.", cfg->dvbapi_boxtype);
1037
1038 for (i=0;i<MAX_DEMUX;i++)
1039 {
1040 demux[i].program_number=0;
1041 demux[i].demux_ecm_fd=0;
1042 demux[i].demux_emm_fd=0;
1043 demux[i].ca_system_id=0;
1044 demux[i].ca_pid=0;
1045 demux[i].emm_pid=0;
1046 demux[i].cadev_index=-1;
1047 demux[i].ca_fd=0;
1048 demux[i].demux_index=-1;
1049 demux[i].thread_active = -1;
1050 memset(demux[i].buffer_cache_dmx,0 ,12);
1051 }
1052
1053 dvbapi_detect_api();
1054
1055 if (selected_box == -1 || selected_api==-1) {
1056 cs_log("dvbapi: could not detect api version");
1057 return 1;
1058 }
1059
1060 if (!dvbapi_init_listenfd())
1061 {
1062 cs_log("dvbapi: could not init camd.socket.");
1063 return 1;
1064 }
1065
1066 dvbapi_chk_caidtab(cfg->dvbapi_priority, &prioritytab);
1067 dvbapi_chk_caidtab(cfg->dvbapi_ignore, &ignoretab);
1068
1069 pfd2[0].fd = fd_m2c;
1070 pfd2[0].events = (POLLIN | POLLPRI);
1071
1072 pthread_create (&p1, NULL, thread_check_zap, NULL);
1073
1074 struct timeb tp;
1075 cs_ftime(&tp);
1076 tp.time+=500;
1077
1078 while (1) {
1079 if (master_pid!=getppid())
1080 cs_exit(0);
1081
1082 chk_pending(tp);
1083
1084 rc=poll(pfd2, 1, 1000);
1085
1086 if (rc<0)
1087 break;
1088
1089 if (pfd2[0].revents & (POLLIN | POLLPRI)) {
1090 chk_dcw(fd_m2c);
1091
1092 }
1093
1094 }
1095 return 0;
1096}
1097
1098
1099void dvbapi_send_dcw(ECM_REQUEST *er)
1100{
1101 unsigned char cw_0[8], cw_1[8];
1102 int i;
1103 unsigned char nullcw[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1104
1105 cs_debug("dvbapi: ECM rc: %d", er->rc);
1106
1107 memcpy(cw_0, er->cw, 8);
1108 memcpy(cw_1, er->cw+8, 8);
1109
1110 for (i=0;i<MAX_DEMUX;i++) {
1111 if (demux[i].program_number==er->srvid) {
1112 if (er->rc<=2 && demux[i].ca_system_id==0 && er->caid!=0) {
1113 dvbapi_start_descrambling(i, er->caid, er->pid, er->prid);
1114 }
1115
1116 if (er->rc==4 && cfg->dvbapi_au==1 && dvbapi_check_array(global_caid_list, MAX_CAID, er->caid)>=0) {
1117 //local card and not found -> maybe card need emm
1118 dvbapi_start_descrambling(i, er->caid, er->pid, er->prid);
1119 }
1120
1121 if (er->rc>3) {
1122 cs_debug("dvbapi: cw not found");
1123 return;
1124 }
1125
1126 ca_descr_t ca_descr;
1127 memset(&ca_descr,0,sizeof(ca_descr));
1128
1129 if (demux[i].ca_fd<=0) {
1130 cs_log("dvbapi: could not write cw.");
1131 return;
1132 }
1133
1134 if (memcmp(cw_0,demux[i].lastcw0,8)!=0 && memcmp(cw_0,nullcw,8)!=0) {
1135 ca_descr.index = i;
1136 ca_descr.parity = 0;
1137 memcpy(demux[i].lastcw0,cw_0,8);
1138 memcpy(ca_descr.cw,cw_0,8);
1139 cs_debug("dvbapi: write cw1 index: %d", i);
1140 if (ioctl(demux[i].ca_fd, CA_SET_DESCR, &ca_descr) < 0)
1141 cs_debug("dvbapi: Error CA_SET_DESCR");
1142 }
1143
1144 if (memcmp(cw_1,demux[i].lastcw1,8)!=0 && memcmp(cw_1,nullcw,8)!=0) {
1145 ca_descr.index = i;
1146 ca_descr.parity = 1;
1147 memcpy(demux[i].lastcw1,cw_1,8);
1148 memcpy(ca_descr.cw,cw_1,8);
1149 cs_debug("dvbapi: write cw2 index: %d", i);
1150 if (ioctl(demux[i].ca_fd, CA_SET_DESCR, &ca_descr) < 0)
1151 cs_debug("dvbapi: Error CA_SET_DESCR");
1152 }
1153 }
1154 }
1155}
1156
1157static void dvbapi_handler(int idx)
1158{
1159 static struct s_auth *account=0;
1160
1161 if (cfg->dvbapi_enabled != 1) {
1162 cs_log("dvbapi disabled");
1163 return;
1164 }
1165
1166 //cs_log("dvbapi loaded fd=%d", idx);
1167
1168 switch(cs_fork(0, idx))
1169 {
1170 case 0: //master
1171 case -1:
1172 return;
1173 default:
1174 wait4master();
1175 }
1176
1177 int ok=0;
1178 if( !account )
1179 {
1180 client[cs_idx].usr[0]=0;
1181 for (ok=0, account=cfg->account; (account) && (!ok); account=account->next)
1182 if( (ok=!strcmp(cfg->dvbapi_usr, account->usr)) )
1183 break;
1184 }
1185
1186 cs_auth_client(ok ? account : (struct s_auth *)(-1), "unknown");
1187
1188 dvbapi_main_local();
1189
1190 cs_log("Module dvbapi error");
1191 cs_exit(0);
1192
1193 return;
1194}
1195
1196
1197
1198/*
1199 * protocol structure
1200 */
1201
1202void module_dvbapi(struct s_module *ph)
1203{
1204 strcpy(ph->desc, "dvbapi");
1205 ph->type=MOD_CONN_SERIAL;
1206 ph->multi=1;
1207 ph->watchdog=0;
1208 ph->s_handler=dvbapi_handler;
1209 ph->send_dcw=dvbapi_send_dcw;
1210}
1211#endif // HAVE_DVBAPI
Note: See TracBrowser for help on using the repository browser.