forked from catalinii/minisatip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.c
1857 lines (1638 loc) · 40.7 KB
/
adapter.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
/*
* Copyright (C) 2014-2020 Catalin Toda <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
//#include <linux/ioctl.h>
#include <sys/ioctl.h>
#include "socketworks.h"
#include "dvb.h"
#include "adapter.h"
#include "dvbapi.h"
#include "utils.h"
#include "tables.h"
#ifndef DISABLE_SATIPCLIENT
#include "satipc.h"
#endif
#ifndef DISABLE_NETCVCLIENT
#include "netceiver.h"
#endif
adapter *a[MAX_ADAPTERS];
int a_count;
char disabled[MAX_ADAPTERS]; // disabled adapters
int sock_signal;
char do_dump_pids = 1;
SMutex a_mutex;
extern struct struct_opts opts;
int tuner_s2, tuner_t, tuner_c, tuner_t2, tuner_c2;
char fe_map[2 * MAX_ADAPTERS];
void find_dvb_adapter(adapter **a);
adapter *adapter_alloc()
{
adapter *ad = malloc1(sizeof(adapter));
memset(ad, 0, sizeof(adapter));
/* diseqc setup */
ad->diseqc_param.fast = opts.diseqc_fast;
ad->diseqc_param.committed_no = opts.diseqc_committed_no;
ad->diseqc_param.uncommitted_no = opts.diseqc_uncommitted_no;
/* diseqc default timing */
ad->diseqc_param.before_cmd = opts.diseqc_before_cmd;
ad->diseqc_param.after_cmd = opts.diseqc_after_cmd;
ad->diseqc_param.after_repeated_cmd = opts.diseqc_after_repeated_cmd;
ad->diseqc_param.after_switch = opts.diseqc_after_switch;
ad->diseqc_param.after_burst = opts.diseqc_after_burst;
ad->diseqc_param.after_tone = opts.diseqc_after_tone;
/* diseqc state control */
ad->old_diseqc = -1;
ad->old_hiband = -1;
ad->old_pol = -1;
ad->dmx_source = -1;
ad->slow_dev = opts.nopm;
/* LOF setup */
ad->diseqc_param.lnb_low = opts.lnb_low;
ad->diseqc_param.lnb_high = opts.lnb_high;
ad->diseqc_param.lnb_circular = opts.lnb_circular;
ad->diseqc_param.lnb_switch = opts.lnb_switch;
return ad;
}
void find_adapters()
{
static int init_find_adapter;
if (init_find_adapter == 1)
return;
init_find_adapter = 1;
a_count = 0;
#ifndef DISABLE_LINUXDVB
find_dvb_adapter(a);
#endif
#ifndef DISABLE_SATIPCLIENT
find_satip_adapter(a);
#endif
#ifndef DISABLE_NETCVCLIENT
find_netcv_adapter(a);
#endif
}
// avoid adapter close unless all the adapters can be closed
int adapter_timeout(sockets *s)
{
int do_close = 1, i;
int64_t rtime = getTick(), max_close = 0;
adapter *ad = get_adapter(s->sid);
if (!ad)
return 1;
if (ad->force_close)
return 1;
if (ad->slow_dev)
{
LOG("keeping the adapter %d open as the initialization is slow", ad->id);
s->rtime = getTick();
return 0;
}
if (get_streams_for_adapter(ad->id) > 0)
{
ad->rtime = getTick();
s->rtime = ad->rtime;
LOG("Keeping the adapter %d open as there are active streams", ad->id);
return 0;
}
if (opts.no_threads)
{
for (i = 0; i < MAX_ADAPTERS; i++)
if ((ad = get_adapter_nw(i)))
{
if (rtime - ad->rtime < s->timeout_ms)
do_close = 0;
if (ad && max_close < ad->rtime)
max_close = ad->rtime;
}
}
LOG("Requested adapter %d close due to timeout, result %d max_rtime %jd",
s->sid, do_close, max_close);
if (!do_close)
s->rtime = max_close;
return do_close;
}
void request_adapter_close(adapter *ad)
{
ad->force_close = 1;
sockets_timeout(ad->sock, 1);
}
int close_adapter_for_socket(sockets * s)
{
int aid = s->sid;
adapter *ad = get_adapter(aid);
LOG("closing DVR socket %d pos %d aid %d", s->sock, s->id, aid);
if(ad)
ad->rtime = getTick();
if (ad)
return close_adapter(aid);
return 1;
}
int init_complete = 0;
int num_adapters = 0;
int init_hw(int i)
{
char name[100];
int64_t st, et;
adapter *ad;
if (i < 0 || i >= MAX_ADAPTERS)
return 3;
if (a[i] && a[i]->enabled)
return 0;
if (!a[i])
return 2;
ad = a[i];
mutex_init(&ad->mutex);
mutex_lock(&ad->mutex);
if (is_adapter_disabled(i))
goto NOK;
if (ad->enabled)
goto NOK;
ad->sock = -1;
ad->id = i;
ad->fe_sock = -1;
ad->sock = -1;
st = getTick();
if (!ad->open)
goto NOK;
#ifdef ENIGMA
ad->dmx_source = ad->id;
#endif
if (ad->open(ad))
{
init_complete = 0;
goto NOK;
}
ad->enabled = 1;
if (!ad->buf)
ad->buf = malloc1(opts.adapter_buffer + 10);
if (!ad->buf)
{
LOG(
"memory allocation failed for %d bytes failed, adapter %d, trying %d bytes",
opts.adapter_buffer, i, ADAPTER_BUFFER);
opts.adapter_buffer = ADAPTER_BUFFER;
ad->buf = malloc1(opts.adapter_buffer + 10);
if (!ad->buf)
{
LOG("memory allocation failed for %d bytes failed, adapter %d",
opts.adapter_buffer, i);
close_adapter(i);
}
goto NOK;
}
memset(ad->buf, 0, opts.adapter_buffer + 1);
init_dvb_parameters(&ad->tp);
mark_pids_deleted(i, -1, NULL);
update_pids(i);
if (!ad->sys[0])
ad->delsys(i, ad->fe, ad->sys);
ad->master_sid = -1;
ad->sid_cnt = 0;
ad->pid_err = ad->dec_err = 0;
ad->new_gs = 0;
ad->force_close = 0;
ad->ca_mask = 0;
ad->tune_time = 0;
ad->wait_new_stream = 0;
ad->rtime = getTick();
ad->sock = sockets_add(ad->dvr, NULL, i, TYPE_DVR, (socket_action) read_dmx,
(socket_action) close_adapter_for_socket,
(socket_action) adapter_timeout);
memset(ad->buf, 0, opts.adapter_buffer + 1);
set_socket_buffer(ad->sock, (unsigned char*) ad->buf, opts.adapter_buffer);
sockets_timeout(ad->sock, ADAPTER_TIMEOUT);
snprintf(ad->name, sizeof(ad->name), "AD%d", i);
set_socket_thread(ad->sock, start_new_thread(ad->name));
set_thread_prio(get_socket_thread(ad->sock), opts.th_priority);
#ifndef DISABLE_TABLES
tables_init_device(ad);
#endif
if (ad->post_init)
ad->post_init(ad);
et = getTick();
if(et - st > 1000000)
{
LOG("Slow adapter %d detected", ad->id);
ad->slow_dev = 1;
}
// set_sock_lock(ad->sock, &ad->mutex); // locks automatically the adapter on reading from the DVR
LOG("done opening adapter %i delivery systems: %s %s %s %s", i,
get_delsys(ad->sys[0]), get_delsys(ad->sys[1]),
get_delsys(ad->sys[2]), get_delsys(ad->sys[3]));
OK:
mutex_unlock(&ad->mutex);
return 0;
NOK:
mutex_unlock(&ad->mutex);
return 1;
}
int init_all_hw()
{
int i, rv;
char name[50];
LOG("starting init_all_hw %d", init_complete);
if (init_complete)
return num_adapters;
mutex_init(&a_mutex);
mutex_lock(&a_mutex);
find_adapters();
num_adapters = 0;
init_complete = 1;
for (i = 0; i < MAX_ADAPTERS; i++)
if (!a[i]
|| ((!a[i]->enabled || a[i]->fe <= 0)
&& ((a[i]->pa >= 0 && a[i]->fn >= 0)
|| a[i]->type > ADAPTER_DVB))) // ADAPTER is intialized and not DVB
{
if (!(rv = init_hw(i)))
num_adapters++;
else if (rv != 2)
LOG("Failed to init device %d with return value %d", i, rv);
}
else if (a[i]->enabled)
num_adapters++;
if (num_adapters == 0)
init_complete = 0;
LOG("done init_hw %d", init_complete);
getAdaptersCount();
mutex_unlock(&a_mutex);
return num_adapters;
}
int close_adapter(int na)
{
adapter *ad;
init_complete = 0;
ad = get_adapter_nw(na);
if (!ad)
return 1;
mutex_lock(&ad->mutex);
if (!ad->enabled)
{
mutex_unlock(&ad->mutex);
return 1;
}
LOG("closing adapter %d -> fe:%d dvr:%d", na, ad->fe, ad->dvr);
ad->enabled = 0;
if (ad->close)
ad->close(ad);
//close all streams attached to this adapter
// close_streams_for_adapter (na, -1);
mark_pids_deleted(na, -1, NULL);
update_pids(na);
// if(ad->dmx>0)close(ad->dmx);
if (ad->fe > 0)
close(ad->fe);
if (ad->sock > 0)
sockets_del(ad->sock);
#ifndef DISABLE_TABLES
if (ad->ca_mask > 0)
tables_close_device(ad);
#endif
ad->ca_mask = 0;
ad->fe = 0;
ad->dvr = 0;
ad->sock = -1;
ad->strength = 0;
ad->snr = 0;
ad->old_diseqc = -1;
ad->old_hiband = -1;
ad->old_pol = -1;
mutex_destroy(&ad->mutex);
// if(a[na]->buf)free1(a[na]->buf);a[na]->buf=NULL;
LOG("done closing adapter %d", na);
return 1;
}
int getAdaptersCount()
{
int i, j, k;
char sys;
adapter *ad;
char fes[20][MAX_ADAPTERS];
char ifes[20];
char order[] =
{ SYS_DVBS2, SYS_DVBT, SYS_DVBC_ANNEX_A, SYS_DVBT2, SYS_DVBC2 };
memset(&ifes, 0, sizeof(ifes));
memset(&fe_map, -1, sizeof(fe_map));
tuner_s2 = tuner_c2 = tuner_t2 = tuner_c = tuner_t = 0;
if (opts.force_sadapter)
tuner_s2 = opts.force_sadapter;
if (opts.force_tadapter)
tuner_t = opts.force_tadapter;
if (opts.force_cadapter)
tuner_c = opts.force_cadapter;
for (i = 0; i < MAX_ADAPTERS; i++)
if ((ad = get_adapter_nw(i)))
{
sys = ad->sys[0];
if (!opts.force_sadapter
&& (delsys_match(ad, SYS_DVBS)
|| delsys_match(ad, SYS_DVBS2)))
{
tuner_s2++;
fes[SYS_DVBS2][ifes[SYS_DVBS2]++] = i;
}
if (!opts.force_tadapter && delsys_match(ad, SYS_DVBT)
&& !delsys_match(ad, SYS_DVBT2))
{
tuner_t++;
fes[SYS_DVBT][ifes[SYS_DVBT]++] = i;
}
if (!opts.force_cadapter && delsys_match(ad, SYS_DVBC_ANNEX_A)
&& !delsys_match(ad, SYS_DVBC2))
{
tuner_c++;
fes[SYS_DVBC_ANNEX_A][ifes[SYS_DVBC_ANNEX_A]++] = i;
}
if (delsys_match(ad, SYS_DVBT2))
{
tuner_t2++;
fes[SYS_DVBT2][ifes[SYS_DVBT2]++] = i;
}
if (delsys_match(ad, SYS_DVBC2))
{
tuner_c2++;
fes[SYS_DVBC2][ifes[SYS_DVBC2]++] = i;
}
}
k = 0;
for (i = 0; i < sizeof(order); i++)
{
sys = order[i];
for (j = 0; j < ifes[sys]; j++)
{
fe_map[k++] = fes[sys][j];
LOG("FE %d mapped to Adapter %d, sys %s", k, fes[sys][j],
get_delsys(sys));
}
}
return tuner_s2 + tuner_c2 + tuner_t2 + tuner_c + tuner_t;
}
void dump_adapters()
{
int i;
adapter *ad;
if (!opts.log)
return;
LOG("Dumping adapters:");
for (i = 0; i < MAX_ADAPTERS; i++)
if ((ad = get_adapter_nw(i)))
LOG("%d|f: %d sid_cnt:%d master_sid:%d del_sys: %s %s %s", i,
ad->tp.freq, ad->sid_cnt, ad->master_sid,
get_delsys(ad->sys[0]), get_delsys(ad->sys[1]),
get_delsys(ad->sys[2]));
dump_streams();
}
void dump_pids(int aid)
{
int i, dp = 1;
if (!opts.log)
return;
if(!do_dump_pids)
return;
adapter *p = get_adapter(aid);
if (!p)
return;
for (i = 0; i < MAX_PIDS; i++)
if (p->pids[i].flags > 0)
{
if (dp)
LOGL(2, "Dumping pids table for adapter %d, pid errors %d", aid,
p->pid_err - p->dec_err);
dp = 0;
LOGL(2,
"pid %d, fd %d, type %d packets %d, d/c errs %d/%d, flags %d, key %d, sids: %d %d %d %d %d %d %d %d",
p->pids[i].pid, p->pids[i].fd, p->pids[i].type,
p->pids[i].cnt, p->pids[i].dec_err, p->pids[i].err,
p->pids[i].flags, p->pids[i].key, p->pids[i].sid[0],
p->pids[i].sid[1], p->pids[i].sid[2], p->pids[i].sid[3],
p->pids[i].sid[4], p->pids[i].sid[5], p->pids[i].sid[6],
p->pids[i].sid[7]);
}
}
int get_free_adapter(transponder *tp)
{
int i;
int match = 0;
int msys = tp->sys;
int fe = tp->fe;
adapter *ad = a[0];
if ((fe > 0) && (fe <= sizeof(fe_map)) && (fe_map[fe - 1] >= 0))
{
fe = fe_map[fe - 1];
ad = a[fe];
}
else
fe = -1;
if (ad)
LOG(
"get free adapter %d - a[%d] => e:%d m:%d sid_cnt:%d f:%d pol=%d sys: %s %s",
tp->fe, ad->id, ad->enabled, ad->master_sid, ad->sid_cnt,
ad->tp.freq, ad->tp.pol, get_delsys(ad->sys[0]),
get_delsys(ad->sys[1]))
else
LOG("get free adapter %d msys %s requested %s", fe, get_delsys(fe),
get_delsys(msys));
if (fe >= 0)
{
if (ad && delsys_match(ad, msys))
{
match = 0;
if (ad->sid_cnt == 0)
match = 1;
if (!ad->enabled || !compare_tunning_parameters(ad->id, tp))
match = 1;
if (match && !init_hw(fe))
return fe;
}
goto noadapter;
}
// provide an already existing adapter
for (i = 0; i < MAX_ADAPTERS; i++)
if ((ad = get_adapter_nw(i)) && delsys_match(ad, msys))
if (!compare_tunning_parameters(ad->id, tp))
return i;
for (i = 0; i < MAX_ADAPTERS; i++)
{
//first free adapter that has the same msys
if ((ad = get_adapter_nw(i)) && ad->sid_cnt == 0
&& delsys_match(ad, msys))
return i;
if (!ad && delsys_match(a[i], msys)) // device is not initialized
{
if (!init_hw(i))
return i;
}
}
noadapter:
LOG("no adapter found for f:%d pol:%d msys:%d", tp->freq, tp->pol, tp->sys);
dump_adapters();
return -1;
}
int set_adapter_for_stream(int sid, int aid)
{
adapter *ad;
if (!(ad = get_adapter(aid)))
return -1;
mutex_lock(&ad->mutex);
if (ad->master_sid == -1)
ad->master_sid = sid;
ad->sid_cnt++;
LOG("set adapter %d for stream %d m:%d s:%d", aid, sid, ad->master_sid,
ad->sid_cnt);
mutex_unlock(&ad->mutex);
return 0;
}
void close_adapter_for_stream(int sid, int aid)
{
adapter *ad;
if (!(ad = get_adapter(aid)))
return;
mutex_lock(&ad->mutex);
if (ad->master_sid == sid)
{
ad->master_sid = -1;
fix_master_sid(aid);
}
if (ad->sid_cnt > 0)
ad->sid_cnt--;
LOG("closed adapter %d for stream %d m:%d s:%d", aid, sid, ad->master_sid,
ad->sid_cnt);
// delete the attached PIDs as well
if (ad->sid_cnt == 0)
mark_pids_deleted(aid, -1, NULL);
else
mark_pids_deleted(aid, sid, NULL);
update_pids(aid);
// if (a[aid]->sid_cnt == 0)
// close_adapter (aid);
mutex_unlock(&ad->mutex);
}
int update_pids(int aid)
{
int i, dp = 1;
adapter *ad;
ad = get_adapter(aid);
if (!ad)
return 0;
#ifndef DISABLE_TABLES
for (i = 0; i < MAX_PIDS; i++)
if ((ad->pids[i].flags == 3))
tables_pid_del(ad, ad->pids[i].pid);
#endif
for (i = 0; i < MAX_PIDS; i++)
if ((ad->pids[i].flags == 3))
{
if (dp)
dump_pids(aid);
dp = 0;
ad->pids[i].flags = 0;
if (ad->pids[i].fd > 0)
ad->del_filters(ad->pids[i].fd, ad->pids[i].pid);
ad->pids[i].fd = 0;
ad->pids[i].type = 0;
ad->pids[i].filter = ad->pids[i].key = 255;
ad->pids[i].csid = 0;
ad->pids[i].version = -1;
ad->pids[i].enabled_channels = 0;
}
for (i = 0; i < MAX_PIDS; i++)
if (ad->pids[i].flags == 2)
{
if(opts.max_pids && (opts.max_pids < get_active_pids_number(ad)))
{
LOG("maximum number of pids %d out of %d reached", get_active_pids_number(ad), opts.max_pids);
break;
}
if (dp)
dump_pids(aid);
dp = 0;
if (ad->pids[i].fd <= 0)
if ((ad->pids[i].fd = ad->set_pid(ad, ad->pids[i].pid)) < 0)
{
int new_max_pids = get_active_pids_number(ad) - 5;
if(new_max_pids > 0)
opts.max_pids = new_max_pids;
LOG("Maximum pid filter reached, lowering the value to %d", opts.max_pids);
break;
}
ad->pids[i].flags = 1;
if (ad->pids[i].pid == 0)
ad->pat_processed = 0;
ad->pids[i].cnt = 0;
ad->pids[i].cc = 255;
ad->pids[i].err = 0;
ad->pids[i].dec_err = 0;
ad->pids[i].version = -1;
ad->pids[i].csid = -1;
}
ad->commit(ad);
#ifndef DISABLE_TABLES
int ep;
if (!get_all_pids(ad, &ep, 1)) // no pids enabled, set pids type to 0
{
reset_pids_type(ad->id, 0);
}
#endif
return 0;
}
int tune(int aid, int sid)
{
adapter *ad = get_adapter(aid);
int rv = 0, flush_data = 0;
SPid *p;
if (!ad)
return -400;
mutex_lock(&ad->mutex);
ad->last_sort = getTick();
if (sid == ad->master_sid && ad->do_tune)
{
ad->tp.diseqc_param = ad->diseqc_param;
rv = ad->tune(ad->id, &ad->tp);
ad->status = -1;
ad->status_cnt = 0;
ad->wait_new_stream = 1;
ad->tune_time = getTick();
flush_data = 1;
set_socket_pos(ad->sock, 0); // flush the existing buffer
ad->rlen = 0;
if (ad->sid_cnt > 1) // the master changed the frequency
{
close_streams_for_adapter(aid, sid);
if (update_pids(aid))
{
mutex_unlock(&ad->mutex);
return -503;
}
}
#ifndef DISABLE_TABLES
p = find_pid(aid, 0);
SPid *p_all = find_pid(aid, 8192);
if ((!p || p->flags == 3) && (!p_all || p_all->flags == 3)) // add pid 0 if not explicitly added
{
LOG(
"Adding pid 0 to the list of pids as not explicitly added for adapter %d",
aid);
mark_pid_add(-1, aid, 0);
}
#endif
}
else
LOG("not tuning for SID %d (do_tune=%d, master_sid=%d)", sid,
ad->do_tune, ad->master_sid);
if (rv < 0)
mark_pids_deleted(aid, sid, NULL);
if (update_pids(aid))
{
mutex_unlock(&ad->mutex);
return -503;
}
if(flush_data)
{
ad->tune_time = getTick();
get_socket_iteration(ad->sock,0);
}
mutex_unlock(&ad->mutex);
return rv;
}
SPid *find_pid(int aid, int p)
{
int i;
adapter *ad = get_adapter(aid);
if (!ad)
return NULL;
for (i = 0; i < MAX_PIDS; i++)
if ((ad->pids[i].flags > 0) && (ad->pids[i].pid == p))
return &ad->pids[i];
return NULL;
}
void mark_pid_deleted(int aid, int sid, int _pid, SPid *p)
{
int j;
int cnt = 0, sort = 0;
if (!p)
p = find_pid(aid, _pid);
if (!p)
return;
if (sid == -1) // delete all sids and the pid
{
if (p->flags != 0)
p->flags = 3;
for (j = 0; j < MAX_STREAMS_PER_PID; j++)
if (sid == -1) // delete all pids if sid = -1
p->sid[j] = -1;
return;
}
// sid != -1
for (j = 0; j < MAX_STREAMS_PER_PID; j++)
if (p->sid[j] == sid) // delete all pids where .sid == sid
{
p->sid[j] = -1;
if ((j + 1 < MAX_PIDS) && (p->sid[j + 1] >= 0))
sort = 1;
}
for (j = 0; j < MAX_STREAMS_PER_PID; j++)
if (p->sid[j] >= 0)
cnt++;
// if ((cnt == 0) && (p->flags != 0) && (p->type == 0 || didit))
if ((cnt == 0) && (p->flags != 0) && (p->type == 0))
p->flags = 3;
if (sort)
{
for (j = 0; j < MAX_STREAMS_PER_PID - 1; j++)
if (p->sid[j + 1] > p->sid[j])
{
unsigned char t = p->sid[j];
p->sid[j] = p->sid[j + 1];
p->sid[j + 1] = t;
}
}
}
void mark_pids_deleted(int aid, int sid, char *pids) //pids==NULL -> delete all pids
{
int i, la, pid;
adapter *ad;
char *arg[MAX_PIDS];
ad = get_adapter(aid);
if (!ad)
return;
LOG("deleting pids on adapter %d, sid %d, pids=%s", aid, sid,
pids ? pids : "NULL");
if (pids)
{
la = split(arg, pids, MAX_PIDS, ',');
for (i = 0; i < la; i++)
{
pid = map_int(arg[i], NULL);
mark_pid_deleted(aid, sid, pid, NULL);
}
return;
}
for (i = 0; i < MAX_PIDS; i++)
mark_pid_deleted(aid, sid, ad->pids[i].pid, &ad->pids[i]);
// if(sid == -1)
// reset_pids_type(aid);
dump_pids(aid);
}
int mark_pid_add(int sid, int aid, int _pid)
{
adapter *ad;
int k, i;
ad = get_adapter(aid);
int found = 0;
SPid *p;
if (!ad)
return 1;
// check if the pid already exists, if yes add the sid
if ((p = find_pid(aid, _pid)))
{
LOG("found already existing pid %d flags %d", _pid, p->flags);
for (k = 0; k < MAX_STREAMS_PER_PID; k++)
if (p->sid[k] == -1 || p->sid[k] == sid)
{
if (p->flags == 3)
p->flags = 2;
p->sid[k] = sid;
found = 1;
break;
}
if (!found)
{
LOG("too many streams for PID %d adapter %d", _pid, aid);
return -1;
}
#ifndef DISABLE_TABLES
tables_pid_add(ad, _pid, 1);
#endif
return 0;
}
// add the new pid in a new position
for (i = 0; i < MAX_PIDS; i++)
if (ad->pids[i].flags <= 0)
{
ad->pids[i].flags = 2;
ad->pids[i].pid = _pid;
ad->pids[i].sid[0] = sid;
ad->pids[i].filter = ad->pids[i].key = ad->pids[i].ecm_parity = 255;
#ifndef DISABLE_TABLES
tables_pid_add(ad, _pid, 0);
#endif
return 0;
}
LOG("MAX_PIDS (%d) reached for adapter %d in adding PID: %d", MAX_PIDS, aid,
_pid);
dump_pids(aid);
dump_adapters();
return -1;
}
int mark_pids_add(int sid, int aid, char *pids)
{
int i, la;
adapter *ad;
char *arg[MAX_PIDS + 2];
int pid;
ad = get_adapter(aid);
if (!ad)
return -1;
if (!pids)
return -1;
LOG("adding pids to adapter %d, sid %d, pids=%s", aid, sid,
pids ? pids : "NULL");
la = split(arg, pids, MAX_PIDS, ',');
for (i = 0; i < la; i++)
{
pid = map_intd(arg[i], NULL, -1);
if (pid == -1)
continue;
if (mark_pid_add(sid, aid, pid) < 0)
return -1;
}
dump_pids(aid);
return 0;
}
int compare_tunning_parameters(int aid, transponder * tp)
{
int same = 0;
adapter *ad = get_adapter(aid);
if (!ad)
return -1;
LOGL(4, "new parameters: f:%d, plp:%d, diseqc:%d, pol:%d, sr:%d, mtype:%d",
tp->freq, tp->plp, tp->diseqc, tp->pol, tp->sr, tp->mtype);
LOGL(4, "old parameters: f:%d, plp:%d, diseqc:%d, pol:%d, sr:%d, mtype:%d",
ad->tp.freq, ad->tp.plp, ad->tp.diseqc, ad->tp.pol, ad->tp.sr, ad->tp.mtype);
if (tp->freq != ad->tp.freq || tp->plp != ad->tp.plp
|| tp->diseqc != ad->tp.diseqc
|| (tp->pol > 0 && tp->pol != ad->tp.pol)
|| (tp->sr > 1000 && tp->sr != ad->tp.sr)
|| (tp->mtype > 0 && tp->mtype != ad->tp.mtype))
return 1;
return 0;
}
int set_adapter_parameters(int aid, int sid, transponder * tp)
{
adapter *ad = get_adapter(aid);
if (!ad)
return -1;
LOG("setting DVB parameters for adapter %d - master_sid %d sid %d old f:%d",
aid, ad->master_sid, sid, ad->tp.freq);
mutex_lock(&ad->mutex);
if (ad->master_sid == -1)
ad->master_sid = sid; // master sid was closed
ad->do_tune = 0;
if(compare_tunning_parameters(aid, tp))
{
if (sid != ad->master_sid) // slave sid requesting to tune to a different frequency
{
mutex_unlock(&ad->mutex);
LOG(
"secondary stream requested tune, not gonna happen ad: f:%d sr:%d pol:%d plp:%d src:%d mod %d -> \
new: f:%d sr:%d pol:%d plp:%d src:%d mod %d",
ad->tp.freq, ad->tp.sr, ad->tp.pol, ad->tp.plp,
ad->tp.diseqc, ad->tp.mtype, tp->freq, tp->sr, tp->pol,
tp->plp, tp->diseqc, tp->mtype);
return -1;
}
mark_pids_deleted(aid, -1, NULL);
if (update_pids(aid))
{
mutex_unlock(&ad->mutex);
return -1;
}
ad->do_tune = 1;
}
copy_dvb_parameters(tp, &ad->tp);
if (ad->tp.pids) // pids can be specified in SETUP and then followed by a delpids in PLAY, make sure the behaviour is right
{
mark_pids_deleted(aid, sid, NULL); // delete all the pids for this
if (mark_pids_add(sid, aid, ad->tp.pids) < 0)
{
mutex_unlock(&ad->mutex);
return -1;
}
}
if (ad->tp.dpids)
{
mark_pids_deleted(aid, sid, ad->tp.dpids);
}
if (ad->tp.apids)
{
if (mark_pids_add(sid, aid, ad->tp.apids ? ad->tp.apids : ad->tp.pids)
< 0)
{