source: trunk/module-dvbapi.c@ 1657

Last change on this file since 1657 was 1657, checked in by _network, 11 years ago

module-dvbapi: fix crash on mips architecture

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