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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
|
/*
* 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 2.57 2012/01/16 12:43:33 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"
#define FE_CAN_TURBO_FEC 0x8000000 // TODO: remove this once it is defined in the driver
#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)
// --- DVB Parameter Maps ----------------------------------------------------
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" },
{ -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)
{
polarization = 0;
inversion = INVERSION_AUTO;
bandwidth = 8000000;
coderateH = FEC_AUTO;
coderateL = FEC_AUTO;
modulation = QPSK;
system = DVB_SYSTEM_1;
transmission = TRANSMISSION_MODE_AUTO;
guard = GUARD_INTERVAL_AUTO;
hierarchy = HIERARCHY_AUTO;
rollOff = ROLLOFF_AUTO;
plpId = 0;
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, 'O', MapToUser(rollOff, RollOffValues));
ST(" T2") q += PrintParameter(q, 'P', plpId);
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(" 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)
{
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 = *s++; break;
case 'I': s = ParseParameter(s, inversion, InversionValues); break;
case 'L': polarization = *s++; break;
case 'M': s = ParseParameter(s, modulation, ModulationValues); break;
case 'O': s = ParseParameter(s, rollOff, RollOffValues); break;
case 'P': s = ParseParameter(s, plpId); break;
case 'R': polarization = *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 = *s++; break;
case 'Y': s = ParseParameter(s, hierarchy, HierarchyValues); break;
default: esyslog("ERROR: unknown parameter key '%c'", *s);
return false;
}
}
return true;
}
// --- cDvbTuner -------------------------------------------------------------
#define TUNER_POLL_TIMEOUT 10 // ms
class cDvbTuner : public cThread {
private:
static cMutex bondMutex;
enum eTunerStatus { tsIdle, tsSet, tsTuned, tsLocked };
const cDvbDevice *device;
int fd_frontend;
int adapter, frontend;
uint32_t subsystemId;
int tuneTimeout;
int lockTimeout;
time_t lastTimeoutReport;
cChannel channel;
const cDiseqc *lastDiseqc;
const cScr *scr;
bool lnbPowerTurnedOn;
eTunerStatus tunerStatus;
cMutex mutex;
cCondVar locked;
cCondVar newSet;
cDvbTuner *bondedTuner;
bool bondedMaster;
bool bondedMasterFailed;
bool SetFrontendType(const cChannel *Channel);
cString GetBondingParams(const cChannel *Channel = NULL) const;
void ClearEventQueue(void) const;
bool GetFrontendStatus(fe_status_t &Status) const;
void ExecuteDiseqc(const cDiseqc *Diseqc, unsigned int *Frequency);
void ResetToneAndVoltage(void);
bool SetFrontend(void);
virtual void Action(void);
public:
cDvbTuner(const cDvbDevice *Device, int Fd_Frontend, int Adapter, int Frontend);
virtual ~cDvbTuner();
bool Bond(cDvbTuner *Tuner);
void UnBond(void);
bool BondingOk(const cChannel *Channel, bool ConsiderOccupied = false) const;
cDvbTuner *GetBondedMaster(void);
const cChannel *GetTransponder(void) const { return &channel; }
uint32_t SubsystemId(void) const { return subsystemId; }
bool IsTunedTo(const cChannel *Channel) const;
void SetChannel(const cChannel *Channel);
bool Locked(int TimeoutMs = 0);
int GetSignalStrength(void) const;
int GetSignalQuality(void) const;
};
cMutex cDvbTuner::bondMutex;
cDvbTuner::cDvbTuner(const cDvbDevice *Device, int Fd_Frontend, int Adapter, int Frontend)
{
device = Device;
fd_frontend = Fd_Frontend;
adapter = Adapter;
frontend = Frontend;
subsystemId = cDvbDeviceProbe::GetSubsystemId(adapter, frontend);
tuneTimeout = 0;
lockTimeout = 0;
lastTimeoutReport = 0;
lastDiseqc = NULL;
scr = NULL;
lnbPowerTurnedOn = false;
tunerStatus = tsIdle;
bondedTuner = NULL;
bondedMaster = false;
bondedMasterFailed = false;
SetDescription("tuner on frontend %d/%d", adapter, frontend);
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);
}
*/
}
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->CardIndex() + 1, Channel->Source(), Channel->Frequency(), dtp.Polarization(), NULL))
return diseqc->Commands();
}
else {
bool ToneOff = Channel->Frequency() < (unsigned int)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->Receiving() || t->tunerStatus != tsIdle && (t->device == cDevice::ActualDevice() || ConsiderOccupied && t->device->Occupied())) {
if (strcmp(BondingParams, t->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) {
if (!bondedMasterFailed)
return this;
else
bondedMaster = false;
}
// 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:
cDvbTuner *t = this;
if (bondedMasterFailed) {
// This one has failed, so switch to the next one:
t = bondedTuner;
t->bondedMasterFailed = false;
cMutexLock MutexLock(&t->mutex);
t->channel = channel;
t->tunerStatus = tsSet;
}
t->bondedMaster = true;
dsyslog("tuner %d/%d is now bonded master", t->adapter, t->frontend);
return t;
}
bool cDvbTuner::IsTunedTo(const cChannel *Channel) const
{
if (tunerStatus == tsIdle)
return false; // not tuned to
if (channel.Source() != Channel->Source() || channel.Transponder() != Channel->Transponder())
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;
channel = *Channel;
lastTimeoutReport = 0;
newSet.Broadcast();
}
else {
cMutexLock MutexLock(&mutex);
tunerStatus = tsIdle;
ResetToneAndVoltage();
}
}
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
{
ClearEventQueue();
while (1) {
if (ioctl(fd_frontend, FE_READ_STATUS, &Status) != -1)
return true;
if (errno != EINTR)
break;
}
return false;
}
//#define DEBUG_SIGNALSTRENGTH
//#define DEBUG_SIGNALQUALITY
int cDvbTuner::GetSignalStrength(void) const
{
ClearEventQueue();
uint16_t Signal;
while (1) {
if (ioctl(fd_frontend, FE_READ_SIGNAL_STRENGTH, &Signal) != -1)
break;
if (errno != EINTR)
return -1;
}
uint16_t MaxSignal = 0xFFFF; // Let's assume the default is using the entire range.
// Use the subsystemId to identify individual devices in case they need
// special treatment to map their Signal value into the range 0...0xFFFF.
switch (subsystemId) {
case 0x13C21019: MaxSignal = 670; break; // TT-budget S2-3200 (DVB-S/DVB-S2)
}
int s = int(Signal) * 100 / MaxSignal;
if (s > 100)
s = 100;
#ifdef DEBUG_SIGNALSTRENGTH
fprintf(stderr, "FE %d/%d: %08X S = %04X %04X %3d%%\n", adapter, frontend, subsystemId, MaxSignal, Signal, s);
#endif
return s;
}
#define LOCK_THRESHOLD 5 // indicates that all 5 FE_HAS_* flags are set
int cDvbTuner::GetSignalQuality(void) const
{
fe_status_t Status;
if (GetFrontendStatus(Status)) {
// Actually one would expect these checks to be done from FE_HAS_SIGNAL to FE_HAS_LOCK, but some drivers (like the stb0899) are broken, so FE_HAS_LOCK is the only one that (hopefully) is generally reliable...
if ((Status & FE_HAS_LOCK) == 0) {
if ((Status & FE_HAS_SIGNAL) == 0)
return 0;
if ((Status & FE_HAS_CARRIER) == 0)
return 1;
if ((Status & FE_HAS_VITERBI) == 0)
return 2;
if ((Status & FE_HAS_SYNC) == 0)
return 3;
return 4;
}
bool HasSnr = true;
uint16_t Snr;
while (1) {
if (ioctl(fd_frontend, FE_READ_SNR, &Snr) != -1)
break;
if (errno == EOPNOTSUPP) {
Snr = 0xFFFF;
HasSnr = false;
break;
}
if (errno != EINTR)
return -1;
}
bool HasBer = true;
uint32_t Ber;
while (1) {
if (ioctl(fd_frontend, FE_READ_BER, &Ber) != -1)
break;
if (errno == EOPNOTSUPP) {
Ber = 0;
HasBer = false;
break;
}
if (errno != EINTR)
return -1;
}
bool HasUnc = true;
uint32_t Unc;
while (1) {
if (ioctl(fd_frontend, FE_READ_UNCORRECTED_BLOCKS, &Unc) != -1)
break;
if (errno == EOPNOTSUPP) {
Unc = 0;
HasUnc = false;
break;
}
if (errno != EINTR)
return -1;
}
uint16_t MaxSnr = 0xFFFF; // Let's assume the default is using the entire range.
// Use the subsystemId to identify individual devices in case they need
// special treatment to map their Snr value into the range 0...0xFFFF.
switch (subsystemId) {
case 0x13C21019: MaxSnr = 200; break; // TT-budget S2-3200 (DVB-S/DVB-S2)
}
int a = int(Snr) * 100 / MaxSnr;
int b = 100 - (Unc * 10 + (Ber / 256) * 5);
if (b < 0)
b = 0;
int q = LOCK_THRESHOLD + a * b * (100 - LOCK_THRESHOLD) / 100 / 100;
if (q > 100)
q = 100;
#ifdef DEBUG_SIGNALQUALITY
fprintf(stderr, "FE %d/%d: %08X Q = %04X %04X %d %5d %5d %3d%%\n", adapter, frontend, subsystemId, MaxSnr, Snr, HasSnr, HasBer ? int(Ber) : -1, HasUnc ? int(Unc) : -1, q);
#endif
return q;
}
return -1;
}
static unsigned int FrequencyToHz(unsigned int f)
{
while (f && f < 1000000)
f *= 1000;
return f;
}
void cDvbTuner::ExecuteDiseqc(const cDiseqc *Diseqc, unsigned int *Frequency)
{
if (!lnbPowerTurnedOn) {
CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_13)); // must explicitly turn on LNB power
lnbPowerTurnedOn = true;
}
static cMutex Mutex;
if (Diseqc->IsScr())
Mutex.Lock();
struct dvb_diseqc_master_cmd cmd;
const char *CurrentAction = NULL;
for (;;) {
cmd.msg_len = sizeof(cmd.msg);
cDiseqc::eDiseqcActions da = Diseqc->Execute(&CurrentAction, cmd.msg, &cmd.msg_len, scr, Frequency);
if (da == cDiseqc::daNone)
break;
switch (da) {
case cDiseqc::daToneOff: CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_OFF)); break;
case cDiseqc::daToneOn: CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_ON)); break;
case cDiseqc::daVoltage13: CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_13)); break;
case cDiseqc::daVoltage18: CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_18)); break;
case cDiseqc::daMiniA: CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_BURST, SEC_MINI_A)); break;
case cDiseqc::daMiniB: CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_BURST, SEC_MINI_B)); break;
case cDiseqc::daCodes: CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd)); break;
default: esyslog("ERROR: unknown diseqc command %d", da);
}
}
if (scr)
ResetToneAndVoltage(); // makes sure we don't block the bus!
if (Diseqc->IsScr())
Mutex.Unlock();
}
void cDvbTuner::ResetToneAndVoltage(void)
{
CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_13));
CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_OFF));
}
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", Channel->Number());
return ds;
}
bool cDvbTuner::SetFrontend(void)
{
#define MAXFRONTENDCMDS 16
#define SETCMD(c, d) { Frontend[CmdSeq.num].cmd = (c);\
Frontend[CmdSeq.num].u.data = (d);\
if (CmdSeq.num++ > MAXFRONTENDCMDS) {\
esyslog("ERROR: too many tuning commands on frontend %d/%d", adapter, frontend);\
return false;\
}\
}
dtv_property Frontend[MAXFRONTENDCMDS];
memset(&Frontend, 0, sizeof(Frontend));
dtv_properties CmdSeq;
memset(&CmdSeq, 0, sizeof(CmdSeq));
CmdSeq.props = Frontend;
SETCMD(DTV_CLEAR, 0);
if (ioctl(fd_frontend, FE_SET_PROPERTY, &CmdSeq) < 0) {
esyslog("ERROR: frontend %d/%d: %m", adapter, frontend);
return false;
}
CmdSeq.num = 0;
cDvbTransponderParameters dtp(channel.Parameters());
// Determine the required frontend type:
int frontendType = GetRequiredDeliverySystem(&channel, &dtp);
if (frontendType == SYS_UNDEFINED)
return false;
SETCMD(DTV_DELIVERY_SYSTEM, frontendType);
if (frontendType == SYS_DVBS || frontendType == SYS_DVBS2) {
unsigned int frequency = channel.Frequency();
if (Setup.DiSEqC) {
if (const cDiseqc *diseqc = Diseqcs.Get(device->CardIndex() + 1, channel.Source(), frequency, dtp.Polarization(), &scr)) {
frequency -= diseqc->Lof();
if (diseqc != lastDiseqc || diseqc->IsScr()) {
if (GetBondedMaster() == this) {
ExecuteDiseqc(diseqc, &frequency);
if (frequency == 0)
return false;
}
else
ResetToneAndVoltage();
lastDiseqc = diseqc;
}
}
else {
esyslog("ERROR: no DiSEqC parameters found for channel %d", channel.Number());
return false;
}
}
else {
int tone = SEC_TONE_OFF;
if (frequency < (unsigned int)Setup.LnbSLOF) {
frequency -= Setup.LnbFrequLo;
tone = SEC_TONE_OFF;
}
else {
frequency -= Setup.LnbFrequHi;
tone = SEC_TONE_ON;
}
int volt = (dtp.Polarization() == 'V' || dtp.Polarization() == 'R') ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18;
if (GetBondedMaster() != this) {
tone = SEC_TONE_OFF;
volt = SEC_VOLTAGE_13;
}
CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, volt));
CHECK(ioctl(fd_frontend, FE_SET_TONE, tone));
}
frequency = abs(frequency); // Allow for C-band, where the frequency is less than the LOF
// DVB-S/DVB-S2 (common parts)
SETCMD(DTV_FREQUENCY, frequency * 1000UL);
SETCMD(DTV_MODULATION, dtp.Modulation());
SETCMD(DTV_SYMBOL_RATE, channel.Srate() * 1000UL);
SETCMD(DTV_INNER_FEC, dtp.CoderateH());
SETCMD(DTV_INVERSION, dtp.Inversion());
if (frontendType == SYS_DVBS2) {
// DVB-S2
SETCMD(DTV_PILOT, PILOT_AUTO);
SETCMD(DTV_ROLLOFF, dtp.RollOff());
}
else {
// DVB-S
SETCMD(DTV_ROLLOFF, ROLLOFF_35); // DVB-S always has a ROLLOFF of 0.35
}
tuneTimeout = DVBS_TUNE_TIMEOUT;
lockTimeout = DVBS_LOCK_TIMEOUT;
}
else if (frontendType == SYS_DVBC_ANNEX_AC || frontendType == SYS_DVBC_ANNEX_B) {
// DVB-C
SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
SETCMD(DTV_INVERSION, dtp.Inversion());
SETCMD(DTV_SYMBOL_RATE, channel.Srate() * 1000UL);
SETCMD(DTV_INNER_FEC, dtp.CoderateH());
SETCMD(DTV_MODULATION, dtp.Modulation());
tuneTimeout = DVBC_TUNE_TIMEOUT;
lockTimeout = DVBC_LOCK_TIMEOUT;
}
else if (frontendType == SYS_DVBT || frontendType == SYS_DVBT2) {
// DVB-T/DVB-T2 (common parts)
SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
SETCMD(DTV_INVERSION, dtp.Inversion());
SETCMD(DTV_BANDWIDTH_HZ, dtp.Bandwidth());
SETCMD(DTV_CODE_RATE_HP, dtp.CoderateH());
SETCMD(DTV_CODE_RATE_LP, dtp.CoderateL());
SETCMD(DTV_MODULATION, dtp.Modulation());
SETCMD(DTV_TRANSMISSION_MODE, dtp.Transmission());
SETCMD(DTV_GUARD_INTERVAL, dtp.Guard());
SETCMD(DTV_HIERARCHY, dtp.Hierarchy());
if (frontendType == SYS_DVBT2) {
// DVB-T2
SETCMD(DTV_DVBT2_PLP_ID, dtp.PlpId());
}
tuneTimeout = DVBT_TUNE_TIMEOUT;
lockTimeout = DVBT_LOCK_TIMEOUT;
}
else if (frontendType == SYS_ATSC) {
// ATSC
SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
SETCMD(DTV_INVERSION, dtp.Inversion());
SETCMD(DTV_MODULATION, dtp.Modulation());
tuneTimeout = ATSC_TUNE_TIMEOUT;
lockTimeout = ATSC_LOCK_TIMEOUT;
}
else {
esyslog("ERROR: attempt to set channel with unknown DVB frontend type");
return false;
}
SETCMD(DTV_TUNE, 0);
if (ioctl(fd_frontend, FE_SET_PROPERTY, &CmdSeq) < 0) {
esyslog("ERROR: frontend %d/%d: %m", adapter, frontend);
return false;
}
return true;
}
void cDvbTuner::Action(void)
{
cTimeMs Timer;
bool LostLock = false;
fe_status_t Status = (fe_status_t)0;
while (Running()) {
fe_status_t NewStatus;
if (GetFrontendStatus(NewStatus))
Status = NewStatus;
cMutexLock MutexLock(&mutex);
int WaitTime = 1000;
switch (tunerStatus) {
case tsIdle:
break;
case tsSet:
tunerStatus = SetFrontend() ? tsTuned : tsIdle;
Timer.Set(tuneTimeout + (scr ? rand() % SCR_RANDOM_TIMEOUT : 0));
continue;
case tsTuned:
if (Timer.TimedOut()) {
tunerStatus = tsSet;
lastDiseqc = NULL;
if (time(NULL) - lastTimeoutReport > 60) { // let's not get too many of these
isyslog("frontend %d/%d timed out while tuning to channel %d, tp %d", adapter, frontend, channel.Number(), channel.Transponder());
lastTimeoutReport = time(NULL);
}
cMutexLock MutexLock(&bondMutex);
if (bondedTuner && bondedMaster)
bondedMasterFailed = true; // give an other tuner a chance in case the sat cable was disconnected
continue;
}
WaitTime = 100; // allows for a quick change from tsTuned to tsLocked
case tsLocked:
if (Status & FE_REINIT) {
tunerStatus = tsSet;
lastDiseqc = NULL;
isyslog("frontend %d/%d was reinitialized", adapter, frontend);
lastTimeoutReport = 0;
continue;
}
else if (Status & FE_HAS_LOCK) {
if (LostLock) {
isyslog("frontend %d/%d regained lock on channel %d, tp %d", adapter, frontend, channel.Number(), channel.Transponder());
LostLock = false;
}
tunerStatus = tsLocked;
locked.Broadcast();
lastTimeoutReport = 0;
}
else if (tunerStatus == tsLocked) {
LostLock = true;
isyslog("frontend %d/%d lost lock on channel %d, tp %d", adapter, frontend, channel.Number(), channel.Transponder());
tunerStatus = tsTuned;
Timer.Set(lockTimeout);
lastTimeoutReport = 0;
continue;
}
break;
default: esyslog("ERROR: unknown tuner status %d", tunerStatus);
}
newSet.TimedWait(mutex, WaitTime);
}
}
// --- cDvbSourceParam -------------------------------------------------------
class cDvbSourceParam : public cSourceParam {
private:
int param;
int srate;
cDvbTransponderParameters dtp;
public:
cDvbSourceParam(char Source, const char *Description);
virtual void SetData(cChannel *Channel);
virtual void GetData(cChannel *Channel);
virtual cOsdItem *GetOsdItem(void);
};
cDvbSourceParam::cDvbSourceParam(char Source, const char *Description)
:cSourceParam(Source, Description)
{
param = 0;
srate = 0;
}
void cDvbSourceParam::SetData(cChannel *Channel)
{
srate = Channel->Srate();
dtp.Parse(Channel->Parameters());
param = 0;
}
void cDvbSourceParam::GetData(cChannel *Channel)
{
Channel->SetTransponderData(Channel->Source(), Channel->Frequency(), srate, dtp.ToString(Source()), true);
}
cOsdItem *cDvbSourceParam::GetOsdItem(void)
{
char type = Source();
const tDvbParameterMap *SystemValues = type == 'S' ? SystemValuesSat : SystemValuesTerr;
#undef ST
#define ST(s) if (strchr(s, type))
switch (param++) {
case 0: ST(" S ") return new cMenuEditChrItem( tr("Polarization"), &dtp.polarization, "HVLR"); else return GetOsdItem();
case 1: ST(" ST") return new cMenuEditMapItem( tr("System"), &dtp.system, SystemValues); else return GetOsdItem();
case 2: ST(" CS ") return new cMenuEditIntItem( tr("Srate"), &srate); else return GetOsdItem();
case 3: ST("ACST") return new cMenuEditMapItem( tr("Inversion"), &dtp.inversion, InversionValues); else return GetOsdItem();
case 4: ST(" CST") return new cMenuEditMapItem( tr("CoderateH"), &dtp.coderateH, CoderateValues); else return GetOsdItem();
case 5: ST(" T") return new cMenuEditMapItem( tr("CoderateL"), &dtp.coderateL, CoderateValues); else return GetOsdItem();
case 6: ST("ACST") return new cMenuEditMapItem( tr("Modulation"), &dtp.modulation, ModulationValues); else return GetOsdItem();
case 7: ST(" T") return new cMenuEditMapItem( tr("Bandwidth"), &dtp.bandwidth, BandwidthValues); else return GetOsdItem();
case 8: ST(" T") return new cMenuEditMapItem( tr("Transmission"), &dtp.transmission, TransmissionValues); else return GetOsdItem();
case 9: ST(" T") return new cMenuEditMapItem( tr("Guard"), &dtp.guard, GuardValues); else return GetOsdItem();
case 10: ST(" T") return new cMenuEditMapItem( tr("Hierarchy"), &dtp.hierarchy, HierarchyValues); else return GetOsdItem();
case 11: ST(" S ") return new cMenuEditMapItem( tr("Rolloff"), &dtp.rollOff, RollOffValues); else return GetOsdItem();
case 12: ST(" T") return new cMenuEditIntItem( tr("PlpId"), &dtp.plpId, 0, 255); else return GetOsdItem();
default: return NULL;
}
return NULL;
}
// --- cDvbDevice ------------------------------------------------------------
int cDvbDevice::setTransferModeForDolbyDigital = 1;
cMutex cDvbDevice::bondMutex;
const char *DeliverySystemNames[] = {
"UNDEFINED",
"DVB-C",
"DVB-C",
"DVB-T",
"DSS",
"DVB-S",
"DVB-S2",
"DVB-H",
"ISDBT",
"ISDBS",
"ISDBC",
"ATSC",
"ATSCMH",
"DMBTH",
"CMMB",
"DAB",
"DVB-T2",
"TURBO",
NULL
};
cDvbDevice::cDvbDevice(int Adapter, int Frontend)
{
adapter = Adapter;
frontend = Frontend;
ciAdapter = NULL;
dvbTuner = NULL;
numDeliverySystems = 0;
numModulations = 0;
bondedDevice = NULL;
needsDetachBondedReceivers = false;
// Devices that are present on all card types:
int fd_frontend = DvbOpen(DEV_DVB_FRONTEND, adapter, frontend, O_RDWR | O_NONBLOCK);
// Common Interface:
fd_ca = DvbOpen(DEV_DVB_CA, adapter, frontend, O_RDWR);
if (fd_ca >= 0)
ciAdapter = cDvbCiAdapter::CreateCiAdapter(this, fd_ca);
// The DVR device (will be opened and closed as needed):
fd_dvr = -1;
// We only check the devices that must be present - the others will be checked before accessing them://XXX
if (fd_frontend >= 0) {
if (QueryDeliverySystems(fd_frontend))
dvbTuner = new cDvbTuner(this, fd_frontend, adapter, frontend);
}
else
esyslog("ERROR: can't open DVB device %d/%d", adapter, frontend);
StartSectionHandler();
}
cDvbDevice::~cDvbDevice()
{
StopSectionHandler();
delete dvbTuner;
delete ciAdapter;
UnBond();
// We're not explicitly closing any device files here, since this sometimes
// caused segfaults. Besides, the program is about to terminate anyway...
}
cString cDvbDevice::DvbName(const char *Name, int Adapter, int Frontend)
{
return cString::sprintf("%s%d/%s%d", DEV_DVB_ADAPTER, Adapter, Name, Frontend);
}
int cDvbDevice::DvbOpen(const char *Name, int Adapter, int Frontend, int Mode, bool ReportError)
{
cString FileName = DvbName(Name, Adapter, Frontend);
int fd = open(FileName, Mode);
if (fd < 0 && ReportError)
LOG_ERROR_STR(*FileName);
return fd;
}
bool cDvbDevice::Exists(int Adapter, int Frontend)
{
cString FileName = DvbName(DEV_DVB_FRONTEND, Adapter, Frontend);
if (access(FileName, F_OK) == 0) {
int f = open(FileName, O_RDONLY);
if (f >= 0) {
close(f);
return true;
}
else if (errno != ENODEV && errno != EINVAL)
LOG_ERROR_STR(*FileName);
}
else if (errno != ENOENT)
LOG_ERROR_STR(*FileName);
return false;
}
bool cDvbDevice::Probe(int Adapter, int Frontend)
{
cString FileName = DvbName(DEV_DVB_FRONTEND, Adapter, Frontend);
dsyslog("probing %s", *FileName);
for (cDvbDeviceProbe *dp = DvbDeviceProbes.First(); dp; dp = DvbDeviceProbes.Next(dp)) {
if (dp->Probe(Adapter, Frontend))
return true; // a plugin has created the actual device
}
dsyslog("creating cDvbDevice");
new cDvbDevice(Adapter, Frontend); // it's a "budget" device
return true;
}
bool cDvbDevice::Initialize(void)
{
new cDvbSourceParam('A', "ATSC");
new cDvbSourceParam('C', "DVB-C");
new cDvbSourceParam('S', "DVB-S");
new cDvbSourceParam('T', "DVB-T");
int Checked = 0;
int Found = 0;
for (int Adapter = 0; ; Adapter++) {
for (int Frontend = 0; ; Frontend++) {
if (Exists(Adapter, Frontend)) {
if (Checked++ < MAXDVBDEVICES) {
if (UseDevice(NextCardIndex())) {
if (Probe(Adapter, Frontend))
Found++;
}
else
NextCardIndex(1); // skips this one
}
}
else if (Frontend == 0)
goto LastAdapter;
else
goto NextAdapter;
}
NextAdapter: ;
}
LastAdapter:
NextCardIndex(MAXDVBDEVICES - Checked); // skips the rest
if (Found > 0)
isyslog("found %d DVB device%s", Found, Found > 1 ? "s" : "");
else
isyslog("no DVB device found");
return Found > 0;
}
bool cDvbDevice::QueryDeliverySystems(int fd_frontend)
{
numDeliverySystems = 0;
if (ioctl(fd_frontend, FE_GET_INFO, &frontendInfo) < 0) {
LOG_ERROR;
return false;
}
#if DVB_API_VERSION > 5 || DVB_API_VERSION_MINOR >= 5
dtv_property Frontend[1];
memset(&Frontend, 0, sizeof(Frontend));
dtv_properties CmdSeq;
memset(&CmdSeq, 0, sizeof(CmdSeq));
CmdSeq.props = Frontend;
SETCMD(DTV_ENUM_DELSYS, 0);
int Result = ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq);
if (Result == 0) {
for (uint i = 0; i < Frontend[0].u.buffer.len; i++) {
if (numDeliverySystems >= MAXDELIVERYSYSTEMS) {
esyslog("ERROR: too many delivery systems on frontend %d/%d", adapter, frontend);
break;
}
deliverySystems[numDeliverySystems++] = Frontend[0].u.buffer.data[i];
}
}
else {
esyslog("ERROR: can't query delivery systems on frontend %d/%d - falling back to legacy mode", adapter, frontend);
#else
{
#endif
// Legacy mode (DVB-API < 5.5):
switch (frontendInfo.type) {
case FE_QPSK: deliverySystems[numDeliverySystems++] = SYS_DVBS;
if (frontendInfo.caps & FE_CAN_2G_MODULATION)
deliverySystems[numDeliverySystems++] = SYS_DVBS2;
break;
case FE_OFDM: deliverySystems[numDeliverySystems++] = SYS_DVBT;
if (frontendInfo.caps & FE_CAN_2G_MODULATION)
deliverySystems[numDeliverySystems++] = SYS_DVBT2;
break;
case FE_QAM: deliverySystems[numDeliverySystems++] = SYS_DVBC_ANNEX_AC; break;
case FE_ATSC: deliverySystems[numDeliverySystems++] = SYS_ATSC; break;
default: esyslog("ERROR: unknown frontend type %d on frontend %d/%d", frontendInfo.type, adapter, frontend);
}
}
if (numDeliverySystems > 0) {
cString ds("");
for (int i = 0; i < numDeliverySystems; i++)
ds = cString::sprintf("%s%s%s", *ds, i ? "," : "", DeliverySystemNames[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;
}
bool cDvbDevice::Ready(void)
{
if (ciAdapter)
return ciAdapter->Ready();
return true;
}
bool cDvbDevice::BondDevices(const char *Bondings)
{
UnBondDevices();
if (Bondings) {
cSatCableNumbers SatCableNumbers(MAXDEVICES, Bondings);
for (int i = 0; i < cDevice::NumDevices(); i++) {
int d = SatCableNumbers.FirstDeviceIndex(i);
if (d >= 0) {
int ErrorDevice = 0;
if (cDevice *Device1 = cDevice::GetDevice(i)) {
if (cDevice *Device2 = cDevice::GetDevice(d)) {
if (cDvbDevice *DvbDevice1 = dynamic_cast<cDvbDevice *>(Device1)) {
if (cDvbDevice *DvbDevice2 = dynamic_cast<cDvbDevice *>(Device2)) {
if (!DvbDevice1->Bond(DvbDevice2))
return false; // Bond() has already logged the error
}
else
ErrorDevice = d + 1;
}
else
ErrorDevice = i + 1;
if (ErrorDevice) {
esyslog("ERROR: device '%d' in device bondings '%s' is not a cDvbDevice", ErrorDevice, Bondings);
return false;
}
}
else
ErrorDevice = d + 1;
}
else
ErrorDevice = i + 1;
if (ErrorDevice) {
esyslog("ERROR: unknown device '%d' in device bondings '%s'", ErrorDevice, Bondings);
return false;
}
}
}
}
return true;
}
void cDvbDevice::UnBondDevices(void)
{
for (int i = 0; i < cDevice::NumDevices(); i++) {
if (cDvbDevice *d = dynamic_cast<cDvbDevice *>(cDevice::GetDevice(i)))
d->UnBond();
}
}
bool cDvbDevice::Bond(cDvbDevice *Device)
{
cMutexLock MutexLock(&bondMutex);
if (!bondedDevice) {
if (Device != this) {
if ((ProvidesDeliverySystem(SYS_DVBS) || ProvidesDeliverySystem(SYS_DVBS2)) && (Device->ProvidesDeliverySystem(SYS_DVBS) || Device->ProvidesDeliverySystem(SYS_DVBS2))) {
if (dvbTuner && Device->dvbTuner && dvbTuner->Bond(Device->dvbTuner)) {
bondedDevice = Device->bondedDevice ? Device->bondedDevice : Device;
Device->bondedDevice = this;
dsyslog("device %d bonded with device %d", CardIndex() + 1, bondedDevice->CardIndex() + 1);
return true;
}
}
else
esyslog("ERROR: can't bond device %d with device %d (only DVB-S(2) devices can be bonded)", CardIndex() + 1, Device->CardIndex() + 1);
}
else
esyslog("ERROR: can't bond device %d with itself", CardIndex() + 1);
}
else
esyslog("ERROR: device %d already bonded with device %d, can't bond with device %d", CardIndex() + 1, bondedDevice->CardIndex() + 1, Device->CardIndex() + 1);
return false;
}
void cDvbDevice::UnBond(void)
{
cMutexLock MutexLock(&bondMutex);
if (cDvbDevice *d = bondedDevice) {
if (dvbTuner)
dvbTuner->UnBond();
dsyslog("device %d unbonded from device %d", CardIndex() + 1, bondedDevice->CardIndex() + 1);
while (d->bondedDevice != this)
d = d->bondedDevice;
if (d == bondedDevice)
d->bondedDevice = NULL;
else
d->bondedDevice = bondedDevice;
bondedDevice = NULL;
}
}
bool cDvbDevice::BondingOk(const cChannel *Channel, bool ConsiderOccupied) const
{
cMutexLock MutexLock(&bondMutex);
if (bondedDevice)
return dvbTuner && dvbTuner->BondingOk(Channel, ConsiderOccupied);
return true;
}
bool cDvbDevice::HasCi(void)
{
return ciAdapter;
}
bool cDvbDevice::SetPid(cPidHandle *Handle, int Type, bool On)
{
if (Handle->pid) {
dmx_pes_filter_params pesFilterParams;
memset(&pesFilterParams, 0, sizeof(pesFilterParams));
if (On) {
if (Handle->handle < 0) {
Handle->handle = DvbOpen(DEV_DVB_DEMUX, adapter, frontend, O_RDWR | O_NONBLOCK, true);
if (Handle->handle < 0) {
LOG_ERROR;
return false;
}
}
pesFilterParams.pid = Handle->pid;
pesFilterParams.input = DMX_IN_FRONTEND;
pesFilterParams.output = DMX_OUT_TS_TAP;
pesFilterParams.pes_type= DMX_PES_OTHER;
pesFilterParams.flags = DMX_IMMEDIATE_START;
if (ioctl(Handle->handle, DMX_SET_PES_FILTER, &pesFilterParams) < 0) {
LOG_ERROR;
return false;
}
}
else if (!Handle->used) {
CHECK(ioctl(Handle->handle, DMX_STOP));
if (Type <= ptTeletext) {
pesFilterParams.pid = 0x1FFF;
pesFilterParams.input = DMX_IN_FRONTEND;
pesFilterParams.output = DMX_OUT_DECODER;
pesFilterParams.pes_type= DMX_PES_OTHER;
pesFilterParams.flags = DMX_IMMEDIATE_START;
CHECK(ioctl(Handle->handle, DMX_SET_PES_FILTER, &pesFilterParams));
}
close(Handle->handle);
Handle->handle = -1;
}
}
return true;
}
int cDvbDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask)
{
cString FileName = DvbName(DEV_DVB_DEMUX, adapter, frontend);
int f = open(FileName, O_RDWR | O_NONBLOCK);
if (f >= 0) {
dmx_sct_filter_params sctFilterParams;
memset(&sctFilterParams, 0, sizeof(sctFilterParams));
sctFilterParams.pid = Pid;
sctFilterParams.timeout = 0;
sctFilterParams.flags = DMX_IMMEDIATE_START;
sctFilterParams.filter.filter[0] = Tid;
sctFilterParams.filter.mask[0] = Mask;
if (ioctl(f, DMX_SET_FILTER, &sctFilterParams) >= 0)
return f;
else {
esyslog("ERROR: can't set filter (pid=%d, tid=%02X, mask=%02X): %m", Pid, Tid, Mask);
close(f);
}
}
else
esyslog("ERROR: can't open filter handle on '%s'", *FileName);
return -1;
}
void cDvbDevice::CloseFilter(int Handle)
{
close(Handle);
}
bool cDvbDevice::ProvidesDeliverySystem(int DeliverySystem) const
{
for (int i = 0; i < numDeliverySystems; i++) {
if (deliverySystems[i] == DeliverySystem)
return true;
}
return false;
}
bool cDvbDevice::ProvidesSource(int Source) const
{
int type = Source & cSource::st_Mask;
return type == cSource::stNone
|| type == cSource::stAtsc && ProvidesDeliverySystem(SYS_ATSC)
|| type == cSource::stCable && (ProvidesDeliverySystem(SYS_DVBC_ANNEX_AC) || ProvidesDeliverySystem(SYS_DVBC_ANNEX_B))
|| type == cSource::stSat && (ProvidesDeliverySystem(SYS_DVBS) || ProvidesDeliverySystem(SYS_DVBS2))
|| type == cSource::stTerr && (ProvidesDeliverySystem(SYS_DVBT) || ProvidesDeliverySystem(SYS_DVBT2));
}
bool cDvbDevice::ProvidesTransponder(const cChannel *Channel) const
{
if (!ProvidesSource(Channel->Source()))
return false; // doesn't provide source
cDvbTransponderParameters dtp(Channel->Parameters());
if (!ProvidesDeliverySystem(GetRequiredDeliverySystem(Channel, &dtp)) ||
dtp.Modulation() == QPSK && !(frontendInfo.caps & FE_CAN_QPSK) ||
dtp.Modulation() == QAM_16 && !(frontendInfo.caps & FE_CAN_QAM_16) ||
dtp.Modulation() == QAM_32 && !(frontendInfo.caps & FE_CAN_QAM_32) ||
dtp.Modulation() == QAM_64 && !(frontendInfo.caps & FE_CAN_QAM_64) ||
dtp.Modulation() == QAM_128 && !(frontendInfo.caps & FE_CAN_QAM_128) ||
dtp.Modulation() == QAM_256 && !(frontendInfo.caps & FE_CAN_QAM_256) ||
dtp.Modulation() == QAM_AUTO && !(frontendInfo.caps & FE_CAN_QAM_AUTO) ||
dtp.Modulation() == VSB_8 && !(frontendInfo.caps & FE_CAN_8VSB) ||
dtp.Modulation() == VSB_16 && !(frontendInfo.caps & FE_CAN_16VSB) ||
dtp.Modulation() == PSK_8 && !(frontendInfo.caps & FE_CAN_TURBO_FEC) && dtp.System() == SYS_DVBS) // "turbo fec" is a non standard FEC used by North American broadcasters - this is a best guess to determine this condition
return false; // requires modulation system which frontend doesn't provide
if (!cSource::IsSat(Channel->Source()) ||
(!Setup.DiSEqC || Diseqcs.Get(CardIndex() + 1, Channel->Source(), Channel->Frequency(), dtp.Polarization(), NULL)))
return DeviceHooksProvidesTransponder(Channel);
return false;
}
bool cDvbDevice::ProvidesChannel(const cChannel *Channel, int Priority, bool *NeedsDetachReceivers) const
{
bool result = false;
bool hasPriority = Priority < 0 || Priority > this->Priority();
bool needsDetachReceivers = false;
needsDetachBondedReceivers = false;
if (dvbTuner && ProvidesTransponder(Channel)) {
result = hasPriority;
if (Priority >= 0) {
if (Receiving(true)) {
if (dvbTuner->IsTunedTo(Channel)) {
if (Channel->Vpid() && !HasPid(Channel->Vpid()) || Channel->Apid(0) && !HasPid(Channel->Apid(0))) {
if (CamSlot() && Channel->Ca() >= CA_ENCRYPTED_MIN) {
if (CamSlot()->CanDecrypt(Channel))
result = true;
else
needsDetachReceivers = true;
}
else if (!IsPrimaryDevice())
result = true;
else
result = Priority >= Setup.PrimaryLimit;
}
else
result = !IsPrimaryDevice() || Priority >= Setup.PrimaryLimit;
}
else
needsDetachReceivers = true;
}
if (result) {
if (!BondingOk(Channel)) {
// This device is bonded, so we need to check the priorities of the others:
for (cDvbDevice *d = bondedDevice; d && d != this; d = d->bondedDevice) {
if (d->Priority() >= Priority) {
result = false;
break;
}
}
needsDetachBondedReceivers = true;
needsDetachReceivers = true;
}
}
}
}
if (NeedsDetachReceivers)
*NeedsDetachReceivers = needsDetachReceivers;
return result;
}
bool cDvbDevice::ProvidesEIT(void) const
{
return dvbTuner != NULL;
}
int cDvbDevice::NumProvidedSystems(void) const
{
return numDeliverySystems + numModulations;
}
int cDvbDevice::SignalStrength(void) const
{
return dvbTuner ? dvbTuner->GetSignalStrength() : -1;
}
int cDvbDevice::SignalQuality(void) const
{
return dvbTuner ? dvbTuner->GetSignalQuality() : -1;
}
const cChannel *cDvbDevice::GetCurrentlyTunedTransponder(void) const
{
return dvbTuner ? dvbTuner->GetTransponder() : NULL;
}
bool cDvbDevice::IsTunedToTransponder(const cChannel *Channel) const
{
return dvbTuner ? dvbTuner->IsTunedTo(Channel) : false;
}
bool cDvbDevice::MaySwitchTransponder(const cChannel *Channel) const
{
return BondingOk(Channel, true) && cDevice::MaySwitchTransponder(Channel);
}
bool cDvbDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
{
if (dvbTuner)
dvbTuner->SetChannel(Channel);
return true;
}
bool cDvbDevice::HasLock(int TimeoutMs)
{
return dvbTuner ? dvbTuner->Locked(TimeoutMs) : false;
}
void cDvbDevice::SetTransferModeForDolbyDigital(int Mode)
{
setTransferModeForDolbyDigital = Mode;
}
bool cDvbDevice::OpenDvr(void)
{
CloseDvr();
fd_dvr = DvbOpen(DEV_DVB_DVR, adapter, frontend, O_RDONLY | O_NONBLOCK, true);
if (fd_dvr >= 0)
tsBuffer = new cTSBuffer(fd_dvr, MEGABYTE(2), CardIndex() + 1);
return fd_dvr >= 0;
}
void cDvbDevice::CloseDvr(void)
{
if (fd_dvr >= 0) {
delete tsBuffer;
tsBuffer = NULL;
close(fd_dvr);
fd_dvr = -1;
}
}
bool cDvbDevice::GetTSPacket(uchar *&Data)
{
if (tsBuffer) {
Data = tsBuffer->Get();
return true;
}
return false;
}
void cDvbDevice::DetachAllReceivers(void)
{
cMutexLock MutexLock(&bondMutex);
cDvbDevice *d = this;
do {
d->cDevice::DetachAllReceivers();
d = d->bondedDevice;
} while (d && d != this && needsDetachBondedReceivers);
needsDetachBondedReceivers = false;
}
// --- cDvbDeviceProbe -------------------------------------------------------
cList<cDvbDeviceProbe> DvbDeviceProbes;
cDvbDeviceProbe::cDvbDeviceProbe(void)
{
DvbDeviceProbes.Add(this);
}
cDvbDeviceProbe::~cDvbDeviceProbe()
{
DvbDeviceProbes.Del(this, false);
}
uint32_t cDvbDeviceProbe::GetSubsystemId(int Adapter, int Frontend)
{
cString FileName;
cReadLine ReadLine;
FILE *f = NULL;
uint32_t SubsystemId = 0;
FileName = cString::sprintf("/sys/class/dvb/dvb%d.frontend%d/device/subsystem_vendor", Adapter, Frontend);
if ((f = fopen(FileName, "r")) != NULL) {
if (char *s = ReadLine.Read(f))
SubsystemId = strtoul(s, NULL, 0) << 16;
fclose(f);
}
FileName = cString::sprintf("/sys/class/dvb/dvb%d.frontend%d/device/subsystem_device", Adapter, Frontend);
if ((f = fopen(FileName, "r")) != NULL) {
if (char *s = ReadLine.Read(f))
SubsystemId |= strtoul(s, NULL, 0);
fclose(f);
}
return SubsystemId;
}
|