source: trunk/module-dvbapi.c@ 1739

Last change on this file since 1739 was 1739, checked in by merek, 10 years ago

Merge from UMP r1806 - dingo35: Autodetect boxtype for DVBAPI

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