source: trunk/module-dvbapi.c@ 1676

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

module-dvbapi: trying to fix pip on dm7025 / add some experimental emm code

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