-
Notifications
You must be signed in to change notification settings - Fork 8
/
dvbdevice.c
2489 lines (2339 loc) · 90.8 KB
/
dvbdevice.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
/*
* dvbdevice.c: The DVB device tuner interface
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: dvbdevice.c 5.8 2024/09/09 08:53:57 kls Exp $
*/
#include "dvbdevice.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/frontend.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "channels.h"
#include "diseqc.h"
#include "dvbci.h"
#include "menuitems.h"
#include "sourceparams.h"
static int DvbApiVersion = 0x0000; // the version of the DVB driver actually in use (will be determined by the first device created)
#define BANDWIDTH_HZ_AUTO 0 // missing in DVB API 5
#define DVBS_TUNE_TIMEOUT 9000 //ms
#define DVBS_LOCK_TIMEOUT 2000 //ms
#define DVBC_TUNE_TIMEOUT 9000 //ms
#define DVBC_LOCK_TIMEOUT 2000 //ms
#define DVBT_TUNE_TIMEOUT 9000 //ms
#define DVBT_LOCK_TIMEOUT 2000 //ms
#define ATSC_TUNE_TIMEOUT 9000 //ms
#define ATSC_LOCK_TIMEOUT 2000 //ms
#define SCR_RANDOM_TIMEOUT 500 // ms (add random value up to this when tuning SCR device to avoid lockups)
#define TSBUFFERSIZE MEGABYTE(16)
// --- DVB Parameter Maps ----------------------------------------------------
const tDvbParameterMap PilotValues[] = {
{ 0, PILOT_OFF, trNOOP("off") },
{ 1, PILOT_ON, trNOOP("on") },
{ 999, PILOT_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap InversionValues[] = {
{ 0, INVERSION_OFF, trNOOP("off") },
{ 1, INVERSION_ON, trNOOP("on") },
{ 999, INVERSION_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap BandwidthValues[] = {
{ 5, 5000000, "5 MHz" },
{ 6, 6000000, "6 MHz" },
{ 7, 7000000, "7 MHz" },
{ 8, 8000000, "8 MHz" },
{ 10, 10000000, "10 MHz" },
{ 1712, 1712000, "1.712 MHz" },
{ 999, BANDWIDTH_HZ_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap CoderateValues[] = {
{ 0, FEC_NONE, trNOOP("none") },
{ 12, FEC_1_2, "1/2" },
{ 23, FEC_2_3, "2/3" },
{ 34, FEC_3_4, "3/4" },
{ 35, FEC_3_5, "3/5" },
{ 45, FEC_4_5, "4/5" },
{ 56, FEC_5_6, "5/6" },
{ 67, FEC_6_7, "6/7" },
{ 78, FEC_7_8, "7/8" },
{ 89, FEC_8_9, "8/9" },
{ 910, FEC_9_10, "9/10" },
{ 999, FEC_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap ModulationValues[] = {
{ 16, QAM_16, "QAM16" },
{ 32, QAM_32, "QAM32" },
{ 64, QAM_64, "QAM64" },
{ 128, QAM_128, "QAM128" },
{ 256, QAM_256, "QAM256" },
{ 2, QPSK, "QPSK" },
{ 5, PSK_8, "8PSK" },
{ 6, APSK_16, "16APSK" },
{ 7, APSK_32, "32APSK" },
{ 10, VSB_8, "VSB8" },
{ 11, VSB_16, "VSB16" },
{ 12, DQPSK, "DQPSK" },
{ 999, QAM_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
#define DVB_SYSTEM_1 0 // see also nit.c
#define DVB_SYSTEM_2 1
const tDvbParameterMap SystemValuesSat[] = {
{ 0, DVB_SYSTEM_1, "DVB-S" },
{ 1, DVB_SYSTEM_2, "DVB-S2" },
{ -1, 0, NULL }
};
const tDvbParameterMap SystemValuesTerr[] = {
{ 0, DVB_SYSTEM_1, "DVB-T" },
{ 1, DVB_SYSTEM_2, "DVB-T2" },
{ -1, 0, NULL }
};
const tDvbParameterMap TransmissionValues[] = {
{ 1, TRANSMISSION_MODE_1K, "1K" },
{ 2, TRANSMISSION_MODE_2K, "2K" },
{ 4, TRANSMISSION_MODE_4K, "4K" },
{ 8, TRANSMISSION_MODE_8K, "8K" },
{ 16, TRANSMISSION_MODE_16K, "16K" },
{ 32, TRANSMISSION_MODE_32K, "32K" },
{ 999, TRANSMISSION_MODE_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap GuardValues[] = {
{ 4, GUARD_INTERVAL_1_4, "1/4" },
{ 8, GUARD_INTERVAL_1_8, "1/8" },
{ 16, GUARD_INTERVAL_1_16, "1/16" },
{ 32, GUARD_INTERVAL_1_32, "1/32" },
{ 128, GUARD_INTERVAL_1_128, "1/128" },
{ 19128, GUARD_INTERVAL_19_128, "19/128" },
{ 19256, GUARD_INTERVAL_19_256, "19/256" },
{ 999, GUARD_INTERVAL_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap HierarchyValues[] = {
{ 0, HIERARCHY_NONE, trNOOP("none") },
{ 1, HIERARCHY_1, "1" },
{ 2, HIERARCHY_2, "2" },
{ 4, HIERARCHY_4, "4" },
{ 999, HIERARCHY_AUTO, trNOOP("auto") },
{ -1, 0, NULL }
};
const tDvbParameterMap RollOffValues[] = {
{ 0, ROLLOFF_AUTO, trNOOP("auto") },
{ 20, ROLLOFF_20, "0.20" },
{ 25, ROLLOFF_25, "0.25" },
{ 35, ROLLOFF_35, "0.35" },
{ -1, 0, NULL }
};
int UserIndex(int Value, const tDvbParameterMap *Map)
{
const tDvbParameterMap *map = Map;
while (map && map->userValue != -1) {
if (map->userValue == Value)
return map - Map;
map++;
}
return -1;
}
int DriverIndex(int Value, const tDvbParameterMap *Map)
{
const tDvbParameterMap *map = Map;
while (map && map->userValue != -1) {
if (map->driverValue == Value)
return map - Map;
map++;
}
return -1;
}
int MapToUser(int Value, const tDvbParameterMap *Map, const char **String)
{
int n = DriverIndex(Value, Map);
if (n >= 0) {
if (String)
*String = tr(Map[n].userString);
return Map[n].userValue;
}
return -1;
}
const char *MapToUserString(int Value, const tDvbParameterMap *Map)
{
int n = DriverIndex(Value, Map);
if (n >= 0)
return Map[n].userString;
return "???";
}
int MapToDriver(int Value, const tDvbParameterMap *Map)
{
int n = UserIndex(Value, Map);
if (n >= 0)
return Map[n].driverValue;
return -1;
}
// --- cDvbTransponderParameters ---------------------------------------------
cDvbTransponderParameters::cDvbTransponderParameters(const char *Parameters)
{
Parse(Parameters);
}
int cDvbTransponderParameters::PrintParameter(char *p, char Name, int Value) const
{
return Value >= 0 && Value != 999 ? sprintf(p, "%c%d", Name, Value) : 0;
}
cString cDvbTransponderParameters::ToString(char Type) const
{
#define ST(s) if (strchr(s, Type) && (strchr(s, '0' + system + 1) || strchr(s, '*')))
char buffer[64];
char *q = buffer;
*q = 0;
ST(" S *") q += sprintf(q, "%c", polarization);
ST(" T*") q += PrintParameter(q, 'B', MapToUser(bandwidth, BandwidthValues));
ST(" CST*") q += PrintParameter(q, 'C', MapToUser(coderateH, CoderateValues));
ST(" T*") q += PrintParameter(q, 'D', MapToUser(coderateL, CoderateValues));
ST(" T*") q += PrintParameter(q, 'G', MapToUser(guard, GuardValues));
ST("ACST*") q += PrintParameter(q, 'I', MapToUser(inversion, InversionValues));
ST("ACST*") q += PrintParameter(q, 'M', MapToUser(modulation, ModulationValues));
ST(" S 2") q += PrintParameter(q, 'N', MapToUser(pilot, PilotValues));
ST(" S 2") q += PrintParameter(q, 'O', MapToUser(rollOff, RollOffValues));
ST(" ST2") q += PrintParameter(q, 'P', streamId);
ST(" T2") q += PrintParameter(q, 'Q', t2systemId);
ST(" ST*") q += PrintParameter(q, 'S', MapToUser(system, SystemValuesSat)); // we only need the numerical value, so Sat or Terr doesn't matter
ST(" T*") q += PrintParameter(q, 'T', MapToUser(transmission, TransmissionValues));
ST(" T2") q += PrintParameter(q, 'X', sisoMiso);
ST(" T*") q += PrintParameter(q, 'Y', MapToUser(hierarchy, HierarchyValues));
return buffer;
}
const char *cDvbTransponderParameters::ParseParameter(const char *s, int &Value, const tDvbParameterMap *Map)
{
if (*++s) {
char *p = NULL;
errno = 0;
int n = strtol(s, &p, 10);
if (!errno && p != s) {
Value = Map ? MapToDriver(n, Map) : n;
if (Value >= 0)
return p;
}
}
esyslog("ERROR: invalid value for parameter '%c'", *(s - 1));
return NULL;
}
bool cDvbTransponderParameters::Parse(const char *s)
{
polarization = 0;
inversion = INVERSION_AUTO;
bandwidth = BANDWIDTH_HZ_AUTO;
coderateH = FEC_AUTO;
coderateL = FEC_AUTO;
modulation = QAM_AUTO;
system = DVB_SYSTEM_1;
transmission = TRANSMISSION_MODE_AUTO;
guard = GUARD_INTERVAL_AUTO;
hierarchy = HIERARCHY_AUTO;
rollOff = ROLLOFF_AUTO;
streamId = 0;
t2systemId = 0;
sisoMiso = 0;
pilot = PILOT_AUTO;
while (s && *s) {
switch (toupper(*s)) {
case 'B': s = ParseParameter(s, bandwidth, BandwidthValues); break;
case 'C': s = ParseParameter(s, coderateH, CoderateValues); break;
case 'D': s = ParseParameter(s, coderateL, CoderateValues); break;
case 'G': s = ParseParameter(s, guard, GuardValues); break;
case 'H': polarization = 'H'; s++; break;
case 'I': s = ParseParameter(s, inversion, InversionValues); break;
case 'L': polarization = 'L'; s++; break;
case 'M': s = ParseParameter(s, modulation, ModulationValues); break;
case 'N': s = ParseParameter(s, pilot, PilotValues); break;
case 'O': s = ParseParameter(s, rollOff, RollOffValues); break;
case 'P': s = ParseParameter(s, streamId); break;
case 'Q': s = ParseParameter(s, t2systemId); break;
case 'R': polarization = 'R'; s++; break;
case 'S': s = ParseParameter(s, system, SystemValuesSat); break; // we only need the numerical value, so Sat or Terr doesn't matter
case 'T': s = ParseParameter(s, transmission, TransmissionValues); break;
case 'V': polarization = 'V'; s++; break;
case 'X': s = ParseParameter(s, sisoMiso); break;
case 'Y': s = ParseParameter(s, hierarchy, HierarchyValues); break;
default: esyslog("ERROR: unknown parameter key '%c'", *s);
return false;
}
}
return true;
}
// --- cDvbFrontend ----------------------------------------------------------
const char *DeliverySystemNames[] = {
"???",
"DVB-C",
"DVB-C",
"DVB-T",
"DSS",
"DVB-S",
"DVB-S2",
"DVB-H",
"ISDBT",
"ISDBS",
"ISDBC",
"ATSC",
"ATSCMH",
"DTMB",
"CMMB",
"DAB",
"DVB-T2",
"TURBO",
"DVB-C",
"DVB-C2",
NULL
};
static const int DeliverySystemNamesMax = sizeof(DeliverySystemNames) / sizeof(DeliverySystemNames[0]) - 2; // -1 to get the maximum allowed index & -1 for the NULL => -2
static const char *GetDeliverySystemName(int Index)
{
if (Index > DeliverySystemNamesMax)
Index = 0;
return DeliverySystemNames[Index];
};
#define MAXFRONTENDCMDS 16
#define SETCMD(c, d) { Props[CmdSeq.num].cmd = (c);\
Props[CmdSeq.num].u.data = (d);\
if (CmdSeq.num++ > MAXFRONTENDCMDS) {\
esyslog("ERROR: too many tuning commands on frontend %d/%d", adapter, frontend);\
return false;\
}\
}
class cDvbFrontend {
private:
int adapter, frontend;
int fd_frontend;
uint32_t subsystemId;
dvb_frontend_info frontendInfo;
cVector<int> deliverySystems;
int numModulations;
bool QueryDeliverySystems(void);
public:
cDvbFrontend(int Adapter, int Frontend);
~cDvbFrontend();
int Open(void);
void Close(void);
const char *FrontendName(void) { return frontendInfo.name; }
bool ProvidesDeliverySystem(int DeliverySystem) const;
bool ProvidesModulation(int System, int StreamId, int Modulation) const;
int NumDeliverySystems(void) const { return deliverySystems.Size(); }
int NumModulations(void) const { return numModulations; }
uint32_t SubsystemId(void) const { return subsystemId; }
};
cDvbFrontend::cDvbFrontend(int Adapter, int Frontend)
{
adapter = Adapter;
frontend = Frontend;
fd_frontend = -1;
subsystemId = cDvbDeviceProbe::GetSubsystemId(adapter, frontend);
memset(&frontendInfo, 0, sizeof(frontendInfo));
strn0cpy(frontendInfo.name, "???", sizeof(frontendInfo.name));
numModulations = 0;
Open();
QueryDeliverySystems();
Close();
}
cDvbFrontend::~cDvbFrontend()
{
Close();
}
int cDvbFrontend::Open(void)
{
Close();
fd_frontend = DvbOpen(DEV_DVB_FRONTEND, adapter, frontend, O_RDWR | O_NONBLOCK, true);
return fd_frontend;
}
void cDvbFrontend::Close(void)
{
if (fd_frontend >= 0) {
if (close(fd_frontend) != 0)
esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
fd_frontend = -1;
}
}
bool cDvbFrontend::ProvidesDeliverySystem(int DeliverySystem) const
{
for (int i = 0; i < deliverySystems.Size(); i++) {
if (deliverySystems[i] == DeliverySystem)
return true;
}
return false;
}
bool cDvbFrontend::ProvidesModulation(int System, int StreamId, int Modulation) const
{
if (StreamId != 0 && !(frontendInfo.caps & FE_CAN_MULTISTREAM))
return false;
if (Modulation == QPSK && !(frontendInfo.caps & FE_CAN_QPSK) ||
Modulation == QAM_16 && !(frontendInfo.caps & FE_CAN_QAM_16) ||
Modulation == QAM_32 && !(frontendInfo.caps & FE_CAN_QAM_32) ||
Modulation == QAM_64 && !(frontendInfo.caps & FE_CAN_QAM_64) ||
Modulation == QAM_128 && !(frontendInfo.caps & FE_CAN_QAM_128) ||
Modulation == QAM_256 && !(frontendInfo.caps & FE_CAN_QAM_256) ||
Modulation == QAM_AUTO && !(frontendInfo.caps & FE_CAN_QAM_AUTO) ||
Modulation == VSB_8 && !(frontendInfo.caps & FE_CAN_8VSB) ||
Modulation == VSB_16 && !(frontendInfo.caps & FE_CAN_16VSB) ||
Modulation == PSK_8 && !(frontendInfo.caps & FE_CAN_TURBO_FEC) && System == SYS_DVBS) // "turbo fec" is a non standard FEC used by North American broadcasters - this is a best guess to de
return false;
return true;
}
bool cDvbFrontend::QueryDeliverySystems(void)
{
deliverySystems.Clear();
numModulations = 0;
if (ioctl(fd_frontend, FE_GET_INFO, &frontendInfo) < 0) {
LOG_ERROR;
return false;
}
dtv_property Props[1];
dtv_properties CmdSeq;
// Determine the version of the running DVB API:
if (!DvbApiVersion) {
memset(&Props, 0, sizeof(Props));
memset(&CmdSeq, 0, sizeof(CmdSeq));
CmdSeq.props = Props;
SETCMD(DTV_API_VERSION, 0);
if (ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
LOG_ERROR;
return false;
}
DvbApiVersion = Props[0].u.data;
isyslog("DVB API version is 0x%04X (VDR was built with 0x%04X)", DvbApiVersion, DVBAPIVERSION);
}
// Determine the types of delivery systems this device provides:
bool LegacyMode = true;
if (DvbApiVersion >= 0x0505) {
memset(&Props, 0, sizeof(Props));
memset(&CmdSeq, 0, sizeof(CmdSeq));
CmdSeq.props = Props;
SETCMD(DTV_ENUM_DELSYS, 0);
int Result = ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq);
if (Result == 0) {
for (uint i = 0; i < Props[0].u.buffer.len; i++) {
// activate this line to simulate a multi-frontend device if you only have a single-frontend device with DVB-S and DVB-S2:
//if (frontend == 0 && Props[0].u.buffer.data[i] != SYS_DVBS || frontend == 1 && Props[0].u.buffer.data[i] != SYS_DVBS2)
deliverySystems.Append(Props[0].u.buffer.data[i]);
}
LegacyMode = false;
}
else {
esyslog("ERROR: can't query delivery systems on frontend %d/%d - falling back to legacy mode", adapter, frontend);
}
}
if (LegacyMode) {
// Legacy mode (DVB-API < 5.5):
switch (frontendInfo.type) {
case FE_QPSK: deliverySystems.Append(SYS_DVBS);
if (frontendInfo.caps & FE_CAN_2G_MODULATION)
deliverySystems.Append(SYS_DVBS2);
break;
case FE_OFDM: deliverySystems.Append(SYS_DVBT);
if (frontendInfo.caps & FE_CAN_2G_MODULATION)
deliverySystems.Append(SYS_DVBT2);
break;
case FE_QAM: deliverySystems.Append(SYS_DVBC_ANNEX_AC); break;
case FE_ATSC: deliverySystems.Append(SYS_ATSC); break;
default: esyslog("ERROR: unknown frontend type %d on frontend %d/%d", frontendInfo.type, adapter, frontend);
}
}
if (deliverySystems.Size() > 0) {
cString ds("");
for (int i = 0; i < deliverySystems.Size(); i++)
ds = cString::sprintf("%s%s%s", *ds, i ? "," : "", GetDeliverySystemName(deliverySystems[i]));
cString ms("");
if (frontendInfo.caps & FE_CAN_QPSK) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QPSK, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_QAM_16) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_16, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_QAM_32) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_32, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_QAM_64) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_64, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_QAM_128) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_128, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_QAM_256) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_256, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_8VSB) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(VSB_8, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_16VSB) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(VSB_16, ModulationValues)); }
if (frontendInfo.caps & FE_CAN_TURBO_FEC) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", "TURBO_FEC"); }
if (!**ms)
ms = "unknown modulations";
isyslog("frontend %d/%d provides %s with %s (\"%s\")", adapter, frontend, *ds, *ms, frontendInfo.name);
return true;
}
else
esyslog("ERROR: frontend %d/%d doesn't provide any delivery systems", adapter, frontend);
return false;
}
// --- cDvbTuner -------------------------------------------------------------
#define TUNER_POLL_TIMEOUT 10 // ms
static int GetRequiredDeliverySystem(const cChannel *Channel, const cDvbTransponderParameters *Dtp)
{
int ds = SYS_UNDEFINED;
if (Channel->IsAtsc())
ds = SYS_ATSC;
else if (Channel->IsCable())
ds = SYS_DVBC_ANNEX_AC;
else if (Channel->IsSat())
ds = Dtp->System() == DVB_SYSTEM_1 ? SYS_DVBS : SYS_DVBS2;
else if (Channel->IsTerr())
ds = Dtp->System() == DVB_SYSTEM_1 ? SYS_DVBT : SYS_DVBT2;
else
esyslog("ERROR: can't determine frontend type for channel %d (%s)", Channel->Number(), Channel->Name());
return ds;
}
class cDvbTuner : public cThread {
private:
static cMutex bondMutex;
enum eTunerStatus { tsIdle, tsSet, tsPositioning, tsTuned, tsLocked };
int frontendType;
const cDvbDevice *device;
mutable int fd_frontend;
int adapter;
mutable int frontend;
cVector<cDvbFrontend *> dvbFrontends;
mutable cDvbFrontend *dvbFrontend;
int numDeliverySystems;
int numModulations;
int tuneTimeout;
int lockTimeout;
time_t lastTimeoutReport;
mutable uint32_t lastUncValue;
mutable uint32_t lastUncDelta;
mutable time_t lastUncChange;
cChannel channel;
mutable const cDiseqc *lastDiseqc;
int diseqcOffset;
mutable int lastSource;
cPositioner *positioner;
const cScr *scr;
mutable bool lnbPowerTurnedOn;
eTunerStatus tunerStatus;
mutable cMutex mutex;
cCondVar locked;
cCondVar newSet;
cDvbTuner *bondedTuner;
bool bondedMaster;
cString GetBondingParams(const cChannel *Channel = NULL) const;
cDvbTuner *GetBondedMaster(void);
bool IsBondedMaster(void) const { return !bondedTuner || bondedMaster; }
void ClearEventQueue(void) const;
bool GetFrontendStatus(fe_status_t &Status) const;
cPositioner *GetPositioner(void);
void ExecuteDiseqc(const cDiseqc *Diseqc, int *Frequency);
void ResetToneAndVoltage(void);
bool SetFrontend(void);
virtual void Action(void);
public:
cDvbTuner(const cDvbDevice *Device, int Adapter, int Frontend);
virtual ~cDvbTuner();
bool ProvidesDeliverySystem(int DeliverySystem) const;
bool ProvidesModulation(int System, int StreamId, int Modulation) const;
bool ProvidesFrontend(const cChannel *Channel, bool Activate = false) const;
int Frontend(void) const { return frontend; }
int FrontendType(void) const { return frontendType; }
const char *FrontendName(void) { return dvbFrontend->FrontendName(); }
int NumProvidedSystems(void) const { return numDeliverySystems + numModulations; }
bool Bond(cDvbTuner *Tuner);
void UnBond(void);
bool BondingOk(const cChannel *Channel, bool ConsiderOccupied = false) const;
const cChannel *GetTransponder(void) const { return &channel; }
uint32_t SubsystemId(void) const { return dvbFrontend->SubsystemId(); }
bool IsTunedTo(const cChannel *Channel) const;
void SetChannel(const cChannel *Channel);
bool Locked(int TimeoutMs = 0);
const cPositioner *Positioner(void) const { return positioner; }
bool GetSignalStats(int &Valid, double *Strength = NULL, double *Cnr = NULL, double *BerPre = NULL, double *BerPost = NULL, double *Per = NULL, int *Status = NULL) const;
int GetSignalStrength(void) const;
int GetSignalQuality(void) const;
void SetPowerSaveMode(bool On);
};
cMutex cDvbTuner::bondMutex;
cDvbTuner::cDvbTuner(const cDvbDevice *Device, int Adapter, int Frontend)
{
frontendType = SYS_UNDEFINED;
device = Device;
fd_frontend = -1;
adapter = Adapter;
frontend = Frontend;
dvbFrontend = NULL;
tuneTimeout = 0;
lockTimeout = 0;
lastTimeoutReport = 0;
lastUncValue = 0;
lastUncDelta = 0;
lastUncChange = 0;
lastDiseqc = NULL;
diseqcOffset = 0;
lastSource = 0;
positioner = NULL;
scr = NULL;
lnbPowerTurnedOn = false;
tunerStatus = tsIdle;
bondedTuner = NULL;
bondedMaster = false;
cDvbFrontend *fe = new cDvbFrontend(adapter, frontend);
dvbFrontends.Append(fe);
numDeliverySystems = fe->NumDeliverySystems();
numModulations = fe->NumModulations();
cString FrontendNumbers = cString::sprintf("%d", frontend);
// Check for multiple frontends:
if (frontend == 0) {
for (int i = 1; ; i++) {
if (access(DvbName(DEV_DVB_FRONTEND, adapter, i), F_OK) == 0) {
if (access(DvbName(DEV_DVB_DEMUX, adapter, i), F_OK) != 0) {
fe = new cDvbFrontend(adapter, i);
dvbFrontends.Append(fe);
numDeliverySystems += fe->NumDeliverySystems();
//numModulations += fe->NumModulations(); // looks like in multi frontend devices all frontends report the same modulations
FrontendNumbers = cString::sprintf("%s+%d", *FrontendNumbers, i);
}
}
else
break;
}
}
// Open default frontend:
dvbFrontend = dvbFrontends[0];
fd_frontend = dvbFrontend->Open();
SetDescription("frontend %d/%s tuner", adapter, *FrontendNumbers);
Start();
}
cDvbTuner::~cDvbTuner()
{
tunerStatus = tsIdle;
newSet.Broadcast();
locked.Broadcast();
Cancel(3);
UnBond();
/* looks like this irritates the SCR switch, so let's leave it out for now
if (lastDiseqc && lastDiseqc->IsScr()) {
unsigned int Frequency = 0;
ExecuteDiseqc(lastDiseqc, &Frequency);
}
*/
for (int i = 0; i < dvbFrontends.Size(); i++)
delete dvbFrontends[i];
}
bool cDvbTuner::ProvidesDeliverySystem(int DeliverySystem) const
{
for (int i = 0; i < dvbFrontends.Size(); i++) {
if (dvbFrontends[i]->ProvidesDeliverySystem(DeliverySystem))
return true;
}
return false;
}
bool cDvbTuner::ProvidesModulation(int System, int StreamId, int Modulation) const
{
for (int i = 0; i < dvbFrontends.Size(); i++) {
if (dvbFrontends[i]->ProvidesModulation(System, StreamId, Modulation))
return true;
}
return false;
}
bool cDvbTuner::ProvidesFrontend(const cChannel *Channel, bool Activate) const
{
cDvbTransponderParameters dtp(Channel->Parameters());
int DeliverySystem = GetRequiredDeliverySystem(Channel, &dtp);
for (int i = 0; i < dvbFrontends.Size(); i++) {
if (dvbFrontends[i]->ProvidesDeliverySystem(DeliverySystem) && dvbFrontends[i]->ProvidesModulation(dtp.System(), dtp.StreamId(), dtp.Modulation())) {
if (Activate && dvbFrontend != dvbFrontends[i]) {
cMutexLock MutexLock(&mutex);
dvbFrontend->Close();
dvbFrontend = dvbFrontends[i];
fd_frontend = dvbFrontend->Open();
frontend = i;
dsyslog("using frontend %d/%d", adapter, frontend);
lastDiseqc = NULL;
lastSource = 0;
lastUncValue = 0;
lastUncDelta = 0;
lastUncChange = 0;
lnbPowerTurnedOn = false;
}
return true;
}
}
return false;
}
bool cDvbTuner::Bond(cDvbTuner *Tuner)
{
cMutexLock MutexLock(&bondMutex);
if (!bondedTuner) {
ResetToneAndVoltage();
bondedMaster = false; // makes sure we don't disturb an existing master
bondedTuner = Tuner->bondedTuner ? Tuner->bondedTuner : Tuner;
Tuner->bondedTuner = this;
dsyslog("tuner %d/%d bonded with tuner %d/%d", adapter, frontend, bondedTuner->adapter, bondedTuner->frontend);
return true;
}
else
esyslog("ERROR: tuner %d/%d already bonded with tuner %d/%d, can't bond with tuner %d/%d", adapter, frontend, bondedTuner->adapter, bondedTuner->frontend, Tuner->adapter, Tuner->frontend);
return false;
}
void cDvbTuner::UnBond(void)
{
cMutexLock MutexLock(&bondMutex);
if (cDvbTuner *t = bondedTuner) {
dsyslog("tuner %d/%d unbonded from tuner %d/%d", adapter, frontend, bondedTuner->adapter, bondedTuner->frontend);
while (t->bondedTuner != this)
t = t->bondedTuner;
if (t == bondedTuner)
t->bondedTuner = NULL;
else
t->bondedTuner = bondedTuner;
bondedMaster = false; // another one will automatically become master whenever necessary
bondedTuner = NULL;
}
}
cString cDvbTuner::GetBondingParams(const cChannel *Channel) const
{
if (!Channel)
Channel = &channel;
cDvbTransponderParameters dtp(Channel->Parameters());
if (Setup.DiSEqC) {
if (const cDiseqc *diseqc = Diseqcs.Get(device->DeviceNumber() + 1, Channel->Source(), Channel->Frequency(), dtp.Polarization(), NULL))
return diseqc->Commands();
}
else {
bool ToneOff = Channel->Frequency() < Setup.LnbSLOF;
bool VoltOff = dtp.Polarization() == 'V' || dtp.Polarization() == 'R';
return cString::sprintf("%c %c", ToneOff ? 't' : 'T', VoltOff ? 'v' : 'V');
}
return "";
}
bool cDvbTuner::BondingOk(const cChannel *Channel, bool ConsiderOccupied) const
{
cMutexLock MutexLock(&bondMutex);
if (cDvbTuner *t = bondedTuner) {
cString BondingParams = GetBondingParams(Channel);
do {
if (t->device->Priority() > IDLEPRIORITY || ConsiderOccupied && t->device->Occupied()) {
if (strcmp(BondingParams, t->GetBondedMaster()->GetBondingParams()) != 0)
return false;
}
t = t->bondedTuner;
} while (t != bondedTuner);
}
return true;
}
cDvbTuner *cDvbTuner::GetBondedMaster(void)
{
if (!bondedTuner)
return this; // an unbonded tuner is always "master"
cMutexLock MutexLock(&bondMutex);
if (bondedMaster)
return this;
// This tuner is bonded, but it's not the master, so let's see if there is a master at all:
if (cDvbTuner *t = bondedTuner) {
while (t != this) {
if (t->bondedMaster)
return t;
t = t->bondedTuner;
}
}
// None of the other bonded tuners is master, so make this one the master:
bondedMaster = true;
dsyslog("tuner %d/%d is now bonded master", adapter, frontend);
return this;
}
bool cDvbTuner::IsTunedTo(const cChannel *Channel) const
{
if (tunerStatus == tsIdle)
return false; // not tuned to
if (channel.Source() != Channel->Source() || channel.Transponder() != Channel->Transponder() || channel.Srate() != Channel->Srate())
return false; // sufficient mismatch
// Polarization is already checked as part of the Transponder.
return strcmp(channel.Parameters(), Channel->Parameters()) == 0;
}
void cDvbTuner::SetChannel(const cChannel *Channel)
{
if (Channel) {
if (bondedTuner) {
cMutexLock MutexLock(&bondMutex);
cDvbTuner *BondedMaster = GetBondedMaster();
if (BondedMaster == this) {
if (strcmp(GetBondingParams(Channel), GetBondingParams()) != 0) {
// switching to a completely different band, so set all others to idle:
for (cDvbTuner *t = bondedTuner; t && t != this; t = t->bondedTuner)
t->SetChannel(NULL);
}
}
else if (strcmp(GetBondingParams(Channel), BondedMaster->GetBondingParams()) != 0)
BondedMaster->SetChannel(Channel);
}
cMutexLock MutexLock(&mutex);
if (!IsTunedTo(Channel))
tunerStatus = tsSet;
diseqcOffset = 0;
channel = *Channel;
lastTimeoutReport = 0;
newSet.Broadcast();
}
else {
cMutexLock MutexLock(&mutex);
tunerStatus = tsIdle;
ResetToneAndVoltage();
}
if (bondedTuner && device->IsPrimaryDevice())
cDevice::PrimaryDevice()->DelLivePids(); // 'device' is const, so we must do it this way
}
bool cDvbTuner::Locked(int TimeoutMs)
{
bool isLocked = (tunerStatus >= tsLocked);
if (isLocked || !TimeoutMs)
return isLocked;
cMutexLock MutexLock(&mutex);
if (TimeoutMs && tunerStatus < tsLocked)
locked.TimedWait(mutex, TimeoutMs);
return tunerStatus >= tsLocked;
}
void cDvbTuner::ClearEventQueue(void) const
{
cPoller Poller(fd_frontend);
if (Poller.Poll(TUNER_POLL_TIMEOUT)) {
dvb_frontend_event Event;
while (ioctl(fd_frontend, FE_GET_EVENT, &Event) == 0)
; // just to clear the event queue - we'll read the actual status below
}
}
bool cDvbTuner::GetFrontendStatus(fe_status_t &Status) const
{
if (fd_frontend == -1)
return false;
ClearEventQueue();
Status = (fe_status_t)0; // initialize here to fix buggy drivers
while (1) {
if (ioctl(fd_frontend, FE_READ_STATUS, &Status) != -1)
return true;
if (errno != EINTR)
break;
}
return false;
}
//#define DEBUG_SIGNALSTATS
//#define DEBUG_SIGNALSTRENGTH
//#define DEBUG_SIGNALQUALITY
bool cDvbTuner::GetSignalStats(int &Valid, double *Strength, double *Cnr, double *BerPre, double *BerPost, double *Per, int *Status) const
{
if (fd_frontend == -1)
return false;
ClearEventQueue();
fe_status_t FeStatus = (fe_status_t)0; // initialize here to fix buggy drivers
dtv_property Props[MAXFRONTENDCMDS];
dtv_properties CmdSeq;
memset(&Props, 0, sizeof(Props));
memset(&CmdSeq, 0, sizeof(CmdSeq));
CmdSeq.props = Props;
Valid = DTV_STAT_VALID_NONE;
if (ioctl(fd_frontend, FE_READ_STATUS, &FeStatus) != 0) {
esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
return false;
}
if (Status) {
*Status = DTV_STAT_HAS_NONE;
if (FeStatus & FE_HAS_SIGNAL) *Status |= DTV_STAT_HAS_SIGNAL;
if (FeStatus & FE_HAS_CARRIER) *Status |= DTV_STAT_HAS_CARRIER;
if (FeStatus & FE_HAS_VITERBI) *Status |= DTV_STAT_HAS_VITERBI;
if (FeStatus & FE_HAS_SYNC) *Status |= DTV_STAT_HAS_SYNC;
if (FeStatus & FE_HAS_LOCK) *Status |= DTV_STAT_HAS_LOCK;
Valid |= DTV_STAT_VALID_STATUS;
}
if (Strength) SETCMD(DTV_STAT_SIGNAL_STRENGTH, 0);
if (Cnr) SETCMD(DTV_STAT_CNR, 0);
if (BerPre) { SETCMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0);
SETCMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0); }
if (BerPost) { SETCMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0);
SETCMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0); }
if (Per) { SETCMD(DTV_STAT_ERROR_BLOCK_COUNT, 0);
SETCMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0); }
if (CmdSeq.num && ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
return false;
}
int i = 0;
if (Strength) {
if (Props[i].u.st.len > 0) {
switch (Props[i].u.st.stat[0].scale) {
case FE_SCALE_DECIBEL: *Strength = double(Props[i].u.st.stat[0].svalue) / 1000;
Valid |= DTV_STAT_VALID_STRENGTH;
break;
default: ;
}
}
i++;
}
if (Cnr) {
if (Props[i].u.st.len > 0) {
switch (Props[i].u.st.stat[0].scale) {
case FE_SCALE_DECIBEL: *Cnr = double(Props[i].u.st.stat[0].svalue) / 1000;
Valid |= DTV_STAT_VALID_CNR;
break;
default: ;
}
}
i++;
}
if (BerPre) {
if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error bit count
uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total bit count
if (tbc > 0) {
*BerPre = double(ebc) / tbc;
Valid |= DTV_STAT_VALID_BERPRE;
}
}
}
i += 2;
}
if (BerPost) {
if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error bit count
uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total bit count
if (tbc > 0) {
*BerPost = double(ebc) / tbc;
Valid |= DTV_STAT_VALID_BERPOST;
}
}
}
i += 2;
}
if (Per) {
if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error block count
uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total block count
if (tbc > 0) {
*Per = double(ebc) / tbc;
Valid |= DTV_STAT_VALID_PER;
}
}
}
i += 2;
}
#ifdef DEBUG_SIGNALSTATS
fprintf(stderr, "FE %d/%d: API5 %04X", adapter, frontend, Valid);
if ((Valid & DTV_STAT_VALID_STATUS) != 0) fprintf(stderr, " STAT=%04X", *Status);
if ((Valid & DTV_STAT_VALID_STRENGTH) != 0) fprintf(stderr, " STR=%1.1fdBm", *Strength);
if ((Valid & DTV_STAT_VALID_CNR) != 0) fprintf(stderr, " CNR=%1.1fdB", *Cnr);
if ((Valid & DTV_STAT_VALID_BERPRE) != 0) fprintf(stderr, " BERPRE=%1.1e", *BerPre);
if ((Valid & DTV_STAT_VALID_BERPOST) != 0) fprintf(stderr, " BERPOST=%1.1e", *BerPost);
if ((Valid & DTV_STAT_VALID_PER) != 0) fprintf(stderr, " PER=%1.1e", *Per);
fprintf(stderr, "\n");
#endif
return Valid != DTV_STAT_VALID_NONE;
}
int dB1000toPercent(int dB1000, int Low, int High)
{
// Convert the given value, which is in 1/1000 dBm, to a percentage in the
// range 0..100. Anything below Low is considered 0%, and anything above
// High counts as 100%.
if (dB1000 < Low)