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

Last change on this file since 1117 was 1117, checked in by alno, 12 years ago

WebIf:

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