-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-dvbapi.c
5014 lines (4444 loc) · 165 KB
/
module-dvbapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "globals.h"
#ifdef HAVE_DVBAPI
#include "module-dvbapi.h"
#include "module-cacheex.h"
#include "module-dvbapi-azbox.h"
#include "module-dvbapi-mca.h"
#include "module-dvbapi-coolapi.h"
#include "module-dvbapi-stapi.h"
#include "module-stat.h"
#include "oscam-chk.h"
#include "oscam-client.h"
#include "oscam-config.h"
#include "oscam-ecm.h"
#include "oscam-emm.h"
#include "oscam-files.h"
#include "oscam-net.h"
#include "oscam-reader.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "reader-irdeto.h"
#ifdef DVBAPI_SAMYGO
static int _ioctl(int fd, int request, ...)
{
typedef struct dmx_sct_filter_params dmx_sct_filter_params_t;
typedef struct dmxSctFilterParams dmxSctFilterParams_t;
va_list ap;
va_start(ap, request);
int ret = -1;
switch(request)
{
case DMX_SET_FILTER:
{
dmx_sct_filter_params_t *sFP2 = va_arg(ap, dmx_sct_filter_params_t*);
// preparing packet
unsigned char packet[sizeof(request) + sizeof(dmx_sct_filter_params_t)];
memcpy(&packet, &request, sizeof(request));
memcpy(&packet[sizeof(request)], sFP2, sizeof(dmx_sct_filter_params_t));
ret = send(fd, packet, sizeof(packet), 0);
//ret = send(fd, sFP2, sizeof(dmx_sct_filter_params_t), 0);
//cs_log("****** send DMX_SET_FILTER %d", ret);
}
break;
case DMX_SET_FILTER1:
{
dmxSctFilterParams_t *sFP1 = va_arg(ap, dmxSctFilterParams_t*);
ret = send(fd, sFP1, sizeof(dmxSctFilterParams_t), 0);
//cs_log("****** send DMX_SET_FILTER1 %d", ret);
}
break;
case DMX_STOP:
{
ret = send(fd, &request, sizeof(request), 0);
//cs_log("****** send DMX_STOP %d", ret);
ret = 1;
}
break;
default:
cs_log("ERROR: Invalid ioctl request");
}
va_end(ap);
if(ret > 0)
ret = 1;
return ret;
}
#define ioctl _ioctl
#endif
// tunemm_caid_map
#define FROM_TO 0
#define TO_FROM 1
// These are declared in module-dvbapi-mca.c
extern int32_t openxcas_provid;
extern uint16_t openxcas_sid, openxcas_caid, openxcas_ecm_pid;
int32_t pausecam = 0, disable_pmt_files = 0, pmt_stopmarking = 0, pmthandling = 0;
DEMUXTYPE demux[MAX_DEMUX];
struct s_dvbapi_priority *dvbapi_priority;
struct s_client *dvbapi_client;
const char *boxdesc[] = { "none", "dreambox", "duckbox", "ufs910", "dbox2", "ipbox", "ipbox-pmt", "dm7000", "qboxhd", "coolstream", "neumo", "pc", "pc-nodmx" };
static const struct box_devices devices[BOX_COUNT] =
{
/* QboxHD (dvb-api-3)*/ { "/tmp/virtual_adapter/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
/* dreambox (dvb-api-3)*/ { "/dev/dvb/adapter%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_3 },
/* dreambox (dvb-api-1)*/ { "/dev/dvb/card%d/", "ca%d", "demux%d", "/tmp/camd.socket", DVBAPI_1 },
/* neumo (dvb-api-1)*/ { "/dev/", "demuxapi", "demuxapi", "/tmp/camd.socket", DVBAPI_1 },
/* sh4 (stapi)*/ { "/dev/stapi/", "stpti4_ioctl", "stpti4_ioctl", "/tmp/camd.socket", STAPI },
/* coolstream*/ { "/dev/cnxt/", "null", "null", "/tmp/camd.socket", COOLAPI }
};
static int32_t selected_box = -1;
static int32_t selected_api = -1;
static int32_t dir_fd = -1;
char *client_name = NULL;
static uint16_t client_proto_version = 0;
static int32_t ca_fd[8]; // holds fd handle of each ca device 0 = not in use
static LLIST * ll_activestreampids; // list of all enabled streampids on ca devices
static int32_t unassoc_fd[MAX_DEMUX];
static LLIST *channel_cache;
struct s_emm_filter
{
int32_t demux_id;
uchar filter[32];
uint16_t caid;
uint32_t provid;
uint16_t pid;
uint32_t num;
struct timeb time_started;
};
static LLIST *ll_emm_active_filter;
static LLIST *ll_emm_inactive_filter;
static LLIST *ll_emm_pending_filter;
struct s_channel_cache
{
uint16_t caid;
uint32_t prid;
uint16_t srvid;
uint16_t pid;
uint32_t chid;
};
struct s_channel_cache *find_channel_cache(int32_t demux_id, int32_t pidindex, int8_t caid_and_prid_only)
{
struct s_ecmpids *p = &demux[demux_id].ECMpids[pidindex];
struct s_channel_cache *c;
LL_ITER it;
if(!channel_cache)
{ channel_cache = ll_create("channel cache"); }
it = ll_iter_create(channel_cache);
while((c = ll_iter_next(&it)))
{
if(caid_and_prid_only)
{
if(p->CAID == c->caid && (p->PROVID == c->prid || p->PROVID == 0)) // PROVID ==0 some provider no provid in PMT table
{ return c; }
}
else
{
if(demux[demux_id].program_number == c->srvid
&& p->CAID == c->caid
&& p->ECM_PID == c->pid
&& (p->PROVID == c->prid || p->PROVID == 0)) // PROVID ==0 some provider no provid in PMT table
{
#ifdef WITH_DEBUG
char buf[ECM_FMT_LEN];
ecmfmt(c->caid, 0, c->prid, c->chid, c->pid, c->srvid, 0, 0, 0, 0, buf, ECM_FMT_LEN, 0, 0);
cs_debug_mask(D_DVBAPI, "[DVBAPI] found in channel cache: %s", buf);
#endif
return c;
}
}
}
return NULL;
}
int32_t edit_channel_cache(int32_t demux_id, int32_t pidindex, uint8_t add)
{
struct s_ecmpids *p = &demux[demux_id].ECMpids[pidindex];
struct s_channel_cache *c;
LL_ITER it;
int32_t count = 0;
if(!channel_cache)
{ channel_cache = ll_create("channel cache"); }
it = ll_iter_create(channel_cache);
while((c = ll_iter_next(&it)))
{
if(demux[demux_id].program_number == c->srvid
&& p->CAID == c->caid
&& p->ECM_PID == c->pid
&& (p->PROVID == c->prid || p->PROVID == 0)
&& (!add || p->CHID == c->chid))
{
if(add)
{ return 0; } //already added
ll_iter_remove_data(&it);
count++;
}
}
if(add)
{
if(!cs_malloc(&c, sizeof(struct s_channel_cache)))
{ return count; }
c->srvid = demux[demux_id].program_number;
c->caid = p->CAID;
c->pid = p->ECM_PID;
c->prid = p->PROVID;
c->chid = p->CHID;
ll_append(channel_cache, c);
#ifdef WITH_DEBUG
char buf[ECM_FMT_LEN];
ecmfmt(c->caid, 0, c->prid, c->chid, c->pid, c->srvid, 0, 0, 0, 0, buf, ECM_FMT_LEN, 0, 0);
cs_debug_mask(D_DVBAPI, "[DVBAPI] added to channel cache: %s", buf);
#endif
count++;
}
return count;
}
int32_t add_emmfilter_to_list(int32_t demux_id, uchar *filter, uint16_t caid, uint32_t provid, uint16_t emmpid, int32_t num, bool enable)
{
if(!ll_emm_active_filter)
{ ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
if(!ll_emm_inactive_filter)
{ ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
if(!ll_emm_pending_filter)
{ ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
struct s_emm_filter *filter_item;
if(!cs_malloc(&filter_item, sizeof(struct s_emm_filter)))
{ return 0; }
filter_item->demux_id = demux_id;
memcpy(filter_item->filter, filter, 32);
filter_item->caid = caid;
filter_item->provid = provid;
filter_item->pid = emmpid;
filter_item->num = num;
if (enable){
cs_ftime(&filter_item->time_started);
}
else {
memset(&filter_item->time_started, 0, sizeof(filter_item->time_started));
}
if(num > 0)
{
ll_append(ll_emm_active_filter, filter_item);
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d Filter #%d added to active emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
}
else if(num < 0)
{
ll_append(ll_emm_pending_filter, filter_item);
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d Filter #%d added to pending emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
}
else
{
ll_append(ll_emm_inactive_filter, filter_item);
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d Filter #%d added to inactive emmfilters (CAID %04X PROVID %06X EMMPID %04X)",
filter_item->demux_id, filter_item->num, filter_item->caid, filter_item->provid, filter_item->pid);
}
return 1;
}
int32_t is_emmfilter_in_list_internal(LLIST *ll, uchar *filter, uint16_t emmpid, uint32_t provid)
{
struct s_emm_filter *filter_item;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter_item = ll_iter_next(&itr)))
{
if(!memcmp(filter_item->filter, filter, 32) && filter_item->pid == emmpid && filter_item->provid == provid)
{ return 1; }
}
}
return 0;
}
int32_t is_emmfilter_in_list(uchar *filter, uint16_t emmpid, uint32_t provid)
{
if(!ll_emm_active_filter)
{ ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
if(!ll_emm_inactive_filter)
{ ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
if(!ll_emm_pending_filter)
{ ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
if(is_emmfilter_in_list_internal(ll_emm_active_filter, filter, emmpid, provid))
{ return 1; }
if(is_emmfilter_in_list_internal(ll_emm_inactive_filter, filter, emmpid, provid))
{ return 1; }
if(is_emmfilter_in_list_internal(ll_emm_pending_filter, filter, emmpid, provid))
{ return 1; }
return 0;
}
struct s_emm_filter *get_emmfilter_by_filternum_internal(LLIST *ll, int32_t demux_id, uint32_t num)
{
struct s_emm_filter *filter;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter = ll_iter_next(&itr)))
{
if(filter->demux_id == demux_id && filter->num == num)
{ return filter; }
}
}
return NULL;
}
struct s_emm_filter *get_emmfilter_by_filternum(int32_t demux_id, uint32_t num)
{
if(!ll_emm_active_filter)
{ ll_emm_active_filter = ll_create("ll_emm_active_filter"); }
if(!ll_emm_inactive_filter)
{ ll_emm_inactive_filter = ll_create("ll_emm_inactive_filter"); }
if(!ll_emm_pending_filter)
{ ll_emm_pending_filter = ll_create("ll_emm_pending_filter"); }
struct s_emm_filter *emm_filter = NULL;
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_active_filter, demux_id, num);
if(emm_filter)
{ return emm_filter; }
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_inactive_filter, demux_id, num);
if(emm_filter)
{ return emm_filter; }
emm_filter = get_emmfilter_by_filternum_internal(ll_emm_pending_filter, demux_id, num);
if(emm_filter)
{ return emm_filter; }
return NULL;
}
int8_t remove_emmfilter_from_list_internal(LLIST *ll, int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
{
struct s_emm_filter *filter;
LL_ITER itr;
if(ll_count(ll) > 0)
{
itr = ll_iter_create(ll);
while((filter = ll_iter_next(&itr)))
{
if(filter->demux_id == demux_id && filter->caid == caid && filter->provid == provid && filter->pid == pid && filter->num == num)
{
ll_iter_remove_data(&itr);
return 1;
}
}
}
return 0;
}
void remove_emmfilter_from_list(int32_t demux_id, uint16_t caid, uint32_t provid, uint16_t pid, uint32_t num)
{
if(ll_emm_active_filter && remove_emmfilter_from_list_internal(ll_emm_active_filter, demux_id, caid, provid, pid, num))
{ return; }
if(ll_emm_inactive_filter && remove_emmfilter_from_list_internal(ll_emm_inactive_filter, demux_id, caid, provid, pid, num))
{ return; }
if(ll_emm_pending_filter && remove_emmfilter_from_list_internal(ll_emm_pending_filter, demux_id, caid, provid, pid, num))
{ return; }
}
int32_t dvbapi_net_send(uint32_t request, int32_t socket_fd, int32_t demux_index, uint32_t filter_number, unsigned char *data)
{
unsigned char packet[262]; //maximum possible packet size
int32_t size = 0;
// not connected?
if (socket_fd <= 0)
return 0;
// preparing packet - header
// in old protocol client expect this first byte as adapter index, changed in the new protocol
// to be always after request type (opcode)
if (client_proto_version <= 0)
packet[size++] = demux[demux_index].adapter_index; //adapter index - 1 byte
// type of request
uint32_t req = request;
if (client_proto_version >= 1)
req = htonl(req);
memcpy(&packet[size], &req, 4); //request - 4 bytes
size += 4;
// preparing packet - adapter index for proto >= 1
if ((request != DVBAPI_SERVER_INFO) && client_proto_version >= 1)
packet[size++] = demux[demux_index].adapter_index; //adapter index - 1 byte
// struct with data
switch (request)
{
case DVBAPI_SERVER_INFO:
{
int16_t proto_version = htons(DVBAPI_PROTOCOL_VERSION); //our protocol version
memcpy(&packet[size], &proto_version, 2);
size += 2;
unsigned char *info_len = &packet[size]; //info string length
size += 1;
*info_len = snprintf((char *) &packet[size], sizeof(packet) - size, "OSCam v%s, build r%s (%s)", CS_VERSION, CS_SVN_VERSION, CS_TARGET);
size += *info_len;
break;
}
case DVBAPI_CA_SET_PID:
{
int sct_capid_size = sizeof(ca_pid_t);
if (client_proto_version >= 1)
{
ca_pid_t *capid = (ca_pid_t *) data;
capid->pid = htonl(capid->pid);
capid->index = htonl(capid->index);
}
memcpy(&packet[size], data, sct_capid_size);
size += sct_capid_size;
break;
}
case DVBAPI_CA_SET_DESCR:
{
int sct_cadescr_size = sizeof(ca_descr_t);
if (client_proto_version >= 1)
{
ca_descr_t *cadesc = (ca_descr_t *) data;
cadesc->index = htonl(cadesc->index);
cadesc->parity = htonl(cadesc->parity);
}
memcpy(&packet[size], data, sct_cadescr_size);
size += sct_cadescr_size;
break;
}
case DVBAPI_DMX_SET_FILTER:
case DVBAPI_DMX_STOP:
{
int32_t sct_filter_size = sizeof(struct dmx_sct_filter_params);
packet[size++] = demux_index; //demux index - 1 byte
packet[size++] = filter_number; //filter number - 1 byte
if (data) // filter data when starting
{
if (client_proto_version >= 1)
{
struct dmx_sct_filter_params *fp = (struct dmx_sct_filter_params *) data;
// adding all dmx_sct_filter_params structure fields
// one by one to avoid padding problems
uint16_t pid = htons(fp->pid);
memcpy(&packet[size], &pid, 2);
size += 2;
memcpy(&packet[size], fp->filter.filter, 16);
size += 16;
memcpy(&packet[size], fp->filter.mask, 16);
size += 16;
memcpy(&packet[size], fp->filter.mode, 16);
size += 16;
uint32_t timeout = htonl(fp->timeout);
memcpy(&packet[size], &timeout, 4);
size += 4;
uint32_t flags = htonl(fp->flags);
memcpy(&packet[size], &flags, 4);
size += 4;
}
else
{
memcpy(&packet[size], data, sct_filter_size); //dmx_sct_filter_params struct
size += sct_filter_size;
}
}
else // pid when stopping
{
if (client_proto_version >= 1)
{
uint16_t pid = htons(demux[demux_index].demux_fd[filter_number].pid);
memcpy(&packet[size], &pid, 2);
size += 2;
}
else
{
uint16_t pid = demux[demux_index].demux_fd[filter_number].pid;
packet[size++] = pid >> 8;
packet[size++] = pid & 0xff;
}
}
break;
}
default: //unknown request
{
cs_log("ERROR: dvbapi_net_send: invalid request");
return 0;
}
}
// sending
cs_ddump_mask(D_DVBAPI, packet, size, "[DVBAPI] Sending packet to dvbapi client (fd=%d):", socket_fd);
send(socket_fd, &packet, size, MSG_DONTWAIT);
// always returning success as the client could close socket
return 0;
}
int32_t dvbapi_set_filter(int32_t demux_id, int32_t api, uint16_t pid, uint16_t caid, uint32_t provid, uchar *filt, uchar *mask, int32_t timeout, int32_t pidindex, int32_t type,
int8_t add_to_emm_list)
{
#if defined WITH_AZBOX || defined WITH_MCA
openxcas_caid = demux[demux_id].ECMpids[pidindex].CAID;
openxcas_ecm_pid = pid;
return 1;
#endif
int32_t ret = -1, n = -1, i;
for(i = 0; i < MAX_FILTER && demux[demux_id].demux_fd[i].fd > 0; i++) { ; }
if(i >= MAX_FILTER)
{
cs_debug_mask(D_DVBAPI, "no free filter");
return -1;
}
n = i;
demux[demux_id].demux_fd[n].pidindex = pidindex;
demux[demux_id].demux_fd[n].pid = pid;
demux[demux_id].demux_fd[n].caid = caid;
demux[demux_id].demux_fd[n].provid = provid;
demux[demux_id].demux_fd[n].type = type;
switch(api)
{
case DVBAPI_3:
if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
ret = demux[demux_id].demux_fd[n].fd = DUMMY_FD;
else
ret = demux[demux_id].demux_fd[n].fd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
if(ret < 0) { return ret; } // return if device cant be opened!
struct dmx_sct_filter_params sFP2;
memset(&sFP2, 0, sizeof(sFP2));
sFP2.pid = pid;
sFP2.timeout = timeout;
sFP2.flags = DMX_IMMEDIATE_START;
if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
{
//DeepThought: on dgs/cubestation and neumo images, perhaps others
//the following code is needed to descramble
sFP2.filter.filter[0] = filt[0];
sFP2.filter.mask[0] = mask[0];
sFP2.filter.filter[1] = 0;
sFP2.filter.mask[1] = 0;
sFP2.filter.filter[2] = 0;
sFP2.filter.mask[2] = 0;
memcpy(sFP2.filter.filter + 3, filt + 1, 16 - 3);
memcpy(sFP2.filter.mask + 3, mask + 1, 16 - 3);
//DeepThought: in the drivers of the dgs/cubestation and neumo images,
//dvbapi 1 and 3 are somehow mixed. In the kernel drivers, the DMX_SET_FILTER
//ioctl expects to receive a dmx_sct_filter_params structure (DVBAPI 3) but
//due to a bug its sets the "positive mask" wrongly (they should be all 0).
//On the other hand, the DMX_SET_FILTER1 ioctl also uses the dmx_sct_filter_params
//structure, which is incorrect (it should be dmxSctFilterParams).
//The only way to get it right is to call DMX_SET_FILTER1 with the argument
//expected by DMX_SET_FILTER. Otherwise, the timeout parameter is not passed correctly.
ret = ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER1, &sFP2);
}
else
{
memcpy(sFP2.filter.filter, filt, 16);
memcpy(sFP2.filter.mask, mask, 16);
if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
ret = dvbapi_net_send(DVBAPI_DMX_SET_FILTER, demux[demux_id].socket_fd, demux_id, n, (unsigned char *) &sFP2);
else
ret = ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER, &sFP2);
}
break;
case DVBAPI_1:
ret = demux[demux_id].demux_fd[n].fd = dvbapi_open_device(0, demux[demux_id].demux_index, demux[demux_id].adapter_index);
if(ret < 0) { return ret; } // return if device cant be opened!
struct dmxSctFilterParams sFP1;
memset(&sFP1, 0, sizeof(sFP1));
sFP1.pid = pid;
sFP1.timeout = timeout;
sFP1.flags = DMX_IMMEDIATE_START;
memcpy(sFP1.filter.filter, filt, 16);
memcpy(sFP1.filter.mask, mask, 16);
ret = ioctl(demux[demux_id].demux_fd[n].fd, DMX_SET_FILTER1, &sFP1);
break;
#ifdef WITH_STAPI
case STAPI:
ret = stapi_set_filter(demux_id, pid, filt, mask, n, demux[demux_id].pmt_file);
if(ret != 0)
{ demux[demux_id].demux_fd[n].fd = ret; }
else
{ ret = -1; } // error setting filter!
break;
#endif
#ifdef WITH_COOLAPI
case COOLAPI:
demux[demux_id].demux_fd[n].fd = coolapi_open_device(demux[demux_id].demux_index, demux_id);
if(demux[demux_id].demux_fd[n].fd > 0)
{ ret = coolapi_set_filter(demux[demux_id].demux_fd[n].fd, n, pid, filt, mask, type); }
break;
#endif
default:
break;
}
if(ret != -1) // filter set successful
{
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d Filter #%d started successfully (caid %04X provid %06X pid %04X)", demux_id, n + 1, caid, provid, pid);
if(type == TYPE_EMM && add_to_emm_list){
add_emmfilter_to_list(demux_id, filt, caid, provid, pid, n + 1, true);
}
}
else
{
cs_log("ERROR: Could not start demux filter (api: %d errno=%d %s)", selected_api, errno, strerror(errno));
}
return ret;
}
static int32_t dvbapi_detect_api(void)
{
#ifdef DVBAPI_SAMYGO
selected_api = DVBAPI_3;
selected_box = 0;
disable_pmt_files = 1;
cs_log("Using SamyGO dvbapi v0.1");
return 1;
#endif
#ifdef WITH_COOLAPI
selected_api = COOLAPI;
selected_box = 5;
disable_pmt_files = 1;
cs_log("Detected Coolstream API");
return 1;
#else
if (cfg.dvbapi_listenport)
{
selected_api = DVBAPI_3;
selected_box = 1;
cs_log("[DVBAPI] Using TCP listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d", selected_api, cfg.dvbapi_boxtype);
return 1;
}
if (cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX) {
selected_api = DVBAPI_3;
selected_box = 1;
cs_log("[DVBAPI] Using %s listen socket, API forced to DVBAPIv3 (%d), userconfig boxtype: %d", devices[selected_box].cam_socket_path, selected_api, cfg.dvbapi_boxtype);
return 1;
}
int32_t i = 0, n = 0, devnum = -1, dmx_fd = 0, boxnum = sizeof(devices) / sizeof(struct box_devices);
char device_path[128], device_path2[128];
while (i < boxnum)
{
snprintf(device_path2, sizeof(device_path2), devices[i].demux_device, 0);
snprintf(device_path, sizeof(device_path), devices[i].path, n);
strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
if((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) > 0)
{
devnum = i;
int32_t ret = close(dmx_fd);
if(ret < 0) { cs_log("ERROR: Could not close demuxer fd (errno=%d %s)", errno, strerror(errno)); }
break;
}
/* try at least 8 adapters */
if ((strchr(devices[i].path, '%') != NULL) && (n < 8)) n++; else { n = 0; i++; }
}
if(devnum == -1) { return 0; }
selected_box = devnum;
if(selected_box > -1)
{ selected_api = devices[selected_box].api; }
#ifdef WITH_STAPI
if(devnum == 4 && stapi_open() == 0)
{
cs_log("ERROR: stapi: setting up stapi failed.");
return 0;
}
#endif
if(cfg.dvbapi_boxtype == BOXTYPE_NEUMO)
{
selected_api = DVBAPI_3; //DeepThought
}
cs_log("[DVBAPI] Detected %s Api: %d, userconfig boxtype: %d", device_path, selected_api, cfg.dvbapi_boxtype);
#endif
return 1;
}
static int32_t dvbapi_read_device(int32_t dmx_fd, unsigned char *buf, int32_t length)
{
int32_t len, rc;
struct pollfd pfd[1];
pfd[0].fd = dmx_fd;
pfd[0].events = (POLLIN | POLLPRI);
rc = poll(pfd, 1, 7000);
if(rc < 1)
{
cs_log("ERROR: Read on %d timed out (errno=%d %s)", dmx_fd, errno, strerror(errno));
return -1;
}
len = read(dmx_fd, buf, length);
if(len < 1)
{ cs_log("ERROR: Read error on fd %d (errno=%d %s)", dmx_fd, errno, strerror(errno)); }
else { cs_ddump_mask(D_TRACE, buf, len, "[DVBAPI] Readed:"); }
return len;
}
int32_t dvbapi_open_device(int32_t type, int32_t num, int32_t adapter)
{
int32_t dmx_fd;
int32_t ca_offset = 0;
char device_path[128], device_path2[128];
if(cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
return DUMMY_FD;
if(type == 0)
{
snprintf(device_path2, sizeof(device_path2), devices[selected_box].demux_device, num);
snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
}
else
{
if(cfg.dvbapi_boxtype == BOXTYPE_DUCKBOX || cfg.dvbapi_boxtype == BOXTYPE_DBOX2 || cfg.dvbapi_boxtype == BOXTYPE_UFS910)
{ ca_offset = 1; }
if(cfg.dvbapi_boxtype == BOXTYPE_QBOXHD)
{ num = 0; }
if(cfg.dvbapi_boxtype == BOXTYPE_PC)
{ num = 0; }
snprintf(device_path2, sizeof(device_path2), devices[selected_box].ca_device, num + ca_offset);
snprintf(device_path, sizeof(device_path), devices[selected_box].path, adapter);
strncat(device_path, device_path2, sizeof(device_path) - strlen(device_path) - 1);
}
#ifdef DVBAPI_SAMYGO
struct sockaddr_un saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sun_family = AF_UNIX;
strncpy(saddr.sun_path, device_path, sizeof(saddr.sun_path) - 1);
dmx_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(connect(dmx_fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
dmx_fd = -1;
}
#else
if((dmx_fd = open(device_path, O_RDWR | O_NONBLOCK)) < 0)
{
cs_log("ERROR: Can't open device %s (errno=%d %s)", device_path, errno, strerror(errno));
return -1;
}
#endif
cs_debug_mask(D_DVBAPI, "DEVICE open (%s) fd %d", device_path, dmx_fd);
return dmx_fd;
}
uint16_t tunemm_caid_map(uint8_t direct, uint16_t caid, uint16_t srvid)
{
int32_t i;
struct s_client *cl = cur_client();
TUNTAB *ttab;
ttab = &cl->ttab;
if(direct)
{
for(i = 0; i < ttab->n; i++)
{
if(caid == ttab->bt_caidto[i]
&& (srvid == ttab->bt_srvid[i] || ttab->bt_srvid[i] == 0xFFFF || !ttab->bt_srvid[i]))
{ return ttab->bt_caidfrom[i]; }
}
}
else
{
for(i = 0; i < ttab->n; i++)
{
if(caid == ttab->bt_caidfrom[i]
&& (srvid == ttab->bt_srvid[i] || ttab->bt_srvid[i] == 0xFFFF || !ttab->bt_srvid[i]))
{ return ttab->bt_caidto[i]; }
}
}
return caid;
}
int32_t dvbapi_stop_filter(int32_t demux_index, int32_t type)
{
int32_t g, ret = -1;
for(g = 0; g < MAX_FILTER; g++)
{
if(demux[demux_index].demux_fd[g].type == type)
{
ret = dvbapi_stop_filternum(demux_index, g);
}
}
if(ret == -1) { return 0; } // on error return 0
else { return 1; }
}
int32_t dvbapi_stop_filternum(int32_t demux_index, int32_t num)
{
int32_t retfilter = -1, retfd = -1, fd = demux[demux_index].demux_fd[num].fd;
if(fd > 0)
{
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d stop Filter #%d (fd: %d api: %d, caid: %04X, provid: %06X, %spid: %04X)",
demux_index, num + 1, fd, selected_api, demux[demux_index].demux_fd[num].caid, demux[demux_index].demux_fd[num].provid,
(demux[demux_index].demux_fd[num].type == TYPE_ECM ? "ecm" : "emm"), demux[demux_index].demux_fd[num].pid);
switch(selected_api)
{
case DVBAPI_3:
if (cfg.dvbapi_listenport || cfg.dvbapi_boxtype == BOXTYPE_PC_NODMX)
retfilter = dvbapi_net_send(DVBAPI_DMX_STOP, demux[demux_index].socket_fd, demux_index, num, NULL);
else
retfilter = ioctl(fd, DMX_STOP); // for modern dvbapi boxes, they do give filter status back to us
break;
case DVBAPI_1:
#if defined(__powerpc__)
ioctl(fd, DMX_STOP); // for old boxes dvbapi1 complaint like dm500 ppcold, no action feedback.
retfilter = 1; // set always successful, but we will never know for sure
#else
retfilter = ioctl(fd, DMX_STOP); // for modern dvbapi boxes, they do give filter status back to us
#endif
break;
#ifdef WITH_STAPI
case STAPI:
retfilter = stapi_remove_filter(demux_index, num, demux[demux_index].pmt_file);
if(retfilter != 1) // stapi returns 0 for error, 1 for all ok
{
retfilter = -1;
}
break;
#endif
#ifdef WITH_COOLAPI
case COOLAPI:
retfilter = coolapi_remove_filter(fd, num);
retfd = coolapi_close_device(fd);
break;
#endif
default:
break;
}
if(retfilter < 0)
{
cs_log("ERROR: Demuxer #%d could not stop Filter #%d (fd:%d api:%d errno=%d %s)", demux_index, num + 1, fd, selected_api, errno, strerror(errno));
}
#ifndef WITH_COOLAPI // no fd close for coolapi and stapi, all others do close fd!
if (!cfg.dvbapi_listenport && cfg.dvbapi_boxtype != BOXTYPE_PC_NODMX)
{
retfd = close(fd);
if(errno == 9) { retfd = 0; } // no error on bad file descriptor
if(selected_api == STAPI) { retfd = 0; } // stapi closes its own filter fd!
}
else
retfd = 0;
#endif
if(retfd)
{
cs_log("ERROR: Demuxer #%d could not close fd of Filter #%d (fd=%d api:%d errno=%d %s)", demux_index, num + 1, fd,
selected_api, errno, strerror(errno));
}
if(demux[demux_index].demux_fd[num].type == TYPE_ECM) //ecm filter stopped: reset index!
{
int32_t idx = demux[demux_index].ECMpids[demux[demux_index].demux_fd[num].pidindex].index;
demux[demux_index].ECMpids[demux[demux_index].demux_fd[num].pidindex].index = 0;
int32_t i;
for(i = 0; i < demux[demux_index].STREAMpidcount && idx; i++){
dvbapi_set_pid(demux_index, i, idx - 1, false); // disable all streampids for this index!
}
}
if(demux[demux_index].demux_fd[num].type == TYPE_EMM && demux[demux_index].demux_fd[num].pid != 0x001) // If emm type remove from emm filterlist
{
remove_emmfilter_from_list(demux_index, demux[demux_index].demux_fd[num].caid, demux[demux_index].demux_fd[num].provid, demux[demux_index].demux_fd[num].pid, num + 1);
}
demux[demux_index].demux_fd[num].fd = 0;
demux[demux_index].demux_fd[num].type = 0;
}
if(retfilter < 0) { return retfilter; } // error on remove filter
if(retfd < 0) { return retfd; } // error on close filter fd
return 1; // all ok!
}
void dvbapi_start_filter(int32_t demux_id, int32_t pidindex, uint16_t pid, uint16_t caid, uint32_t provid, uchar table, uchar mask, int32_t timeout, int32_t type)
{
uchar filter[32];
memset(filter, 0, 32);
filter[0] = table;
filter[16] = mask;
cs_debug_mask(D_DVBAPI, "[DVBAPI] Demuxer #%d try to start new filter for caid: %04X, provid: %06X, pid: %04X", demux_id, caid, provid, pid);
dvbapi_set_filter(demux_id, selected_api, pid, caid, provid, filter, filter + 16, timeout, pidindex, type, 0);
}
static int32_t dvbapi_find_emmpid(int32_t demux_id, uint8_t type, uint16_t caid, uint32_t provid)
{
int32_t k;
int32_t bck = -1;
for(k = 0; k < demux[demux_id].EMMpidcount; k++)
{
if(demux[demux_id].EMMpids[k].CAID == caid
&& demux[demux_id].EMMpids[k].PROVID == provid
&& (demux[demux_id].EMMpids[k].type & type))
{ return k; }
}
return bck;
}
void dvbapi_start_emm_filter(int32_t demux_index)
{
unsigned int j;
if(!demux[demux_index].EMMpidcount)
{ return; }
//if (demux[demux_index].emm_filter)
// return;
struct s_csystem_emm_filter *dmx_filter = NULL;
unsigned int filter_count = 0;
uint16_t caid, ncaid;
struct s_reader *rdr = NULL;
struct s_client *cl = cur_client();
if(!cl || !cl->aureader_list)
{ return; }
LL_ITER itr = ll_iter_create(cl->aureader_list);
while((rdr = ll_iter_next(&itr)))
{
if(!rdr->client || rdr->audisabled != 0 || !rdr->enable || (!is_network_reader(rdr) && rdr->card_status != CARD_INSERTED))
{ continue; }
struct s_cardsystem *cs;
uint16_t c;
for(c = 0; c < demux[demux_index].EMMpidcount; c++)
{
caid = ncaid = demux[demux_index].EMMpids[c].CAID;
if(!caid) continue;
if(emm_reader_match(rdr, caid, 0))
{
cs = get_cardsystem_by_caid(caid);
if(cs)
{
if(chk_is_betatunnel_caid(caid) == 1)
{
ncaid = tunemm_caid_map(TO_FROM, caid, demux[demux_index].program_number);
}
if(caid != ncaid && dvbapi_find_emmpid(demux_index, EMM_UNIQUE | EMM_SHARED | EMM_GLOBAL, ncaid, 0) > -1)
{
cs->get_tunemm_filter(rdr, &dmx_filter, &filter_count);
cs_debug_mask(D_DVBAPI, "[EMM Filter] setting emm filter for betatunnel: %04X -> %04X", ncaid, caid);