vdr 2.6.3
dvbdevice.c
Go to the documentation of this file.
1/*
2 * dvbdevice.c: The DVB device tuner interface
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: dvbdevice.c 5.3 2022/12/05 14:04:10 kls Exp $
8 */
9
10#include "dvbdevice.h"
11#include <ctype.h>
12#include <errno.h>
13#include <limits.h>
14#include <linux/dvb/dmx.h>
15#include <linux/dvb/frontend.h>
16#include <sys/ioctl.h>
17#include <sys/mman.h>
18#include "channels.h"
19#include "diseqc.h"
20#include "dvbci.h"
21#include "menuitems.h"
22#include "sourceparams.h"
23
24static int DvbApiVersion = 0x0000; // the version of the DVB driver actually in use (will be determined by the first device created)
25
26#define BANDWIDTH_HZ_AUTO 0 // missing in DVB API 5
27
28#define DVBS_TUNE_TIMEOUT 9000 //ms
29#define DVBS_LOCK_TIMEOUT 2000 //ms
30#define DVBC_TUNE_TIMEOUT 9000 //ms
31#define DVBC_LOCK_TIMEOUT 2000 //ms
32#define DVBT_TUNE_TIMEOUT 9000 //ms
33#define DVBT_LOCK_TIMEOUT 2000 //ms
34#define ATSC_TUNE_TIMEOUT 9000 //ms
35#define ATSC_LOCK_TIMEOUT 2000 //ms
36
37#define SCR_RANDOM_TIMEOUT 500 // ms (add random value up to this when tuning SCR device to avoid lockups)
38
39#define TSBUFFERSIZE MEGABYTE(16)
40
41// --- DVB Parameter Maps ----------------------------------------------------
42
44 { 0, PILOT_OFF, trNOOP("off") },
45 { 1, PILOT_ON, trNOOP("on") },
46 { 999, PILOT_AUTO, trNOOP("auto") },
47 { -1, 0, NULL }
48 };
49
51 { 0, INVERSION_OFF, trNOOP("off") },
52 { 1, INVERSION_ON, trNOOP("on") },
53 { 999, INVERSION_AUTO, trNOOP("auto") },
54 { -1, 0, NULL }
55 };
56
58 { 5, 5000000, "5 MHz" },
59 { 6, 6000000, "6 MHz" },
60 { 7, 7000000, "7 MHz" },
61 { 8, 8000000, "8 MHz" },
62 { 10, 10000000, "10 MHz" },
63 { 1712, 1712000, "1.712 MHz" },
64 { 999, BANDWIDTH_HZ_AUTO, trNOOP("auto") },
65 { -1, 0, NULL }
66 };
67
69 { 0, FEC_NONE, trNOOP("none") },
70 { 12, FEC_1_2, "1/2" },
71 { 23, FEC_2_3, "2/3" },
72 { 34, FEC_3_4, "3/4" },
73 { 35, FEC_3_5, "3/5" },
74 { 45, FEC_4_5, "4/5" },
75 { 56, FEC_5_6, "5/6" },
76 { 67, FEC_6_7, "6/7" },
77 { 78, FEC_7_8, "7/8" },
78 { 89, FEC_8_9, "8/9" },
79 { 910, FEC_9_10, "9/10" },
80 { 999, FEC_AUTO, trNOOP("auto") },
81 { -1, 0, NULL }
82 };
83
85 { 16, QAM_16, "QAM16" },
86 { 32, QAM_32, "QAM32" },
87 { 64, QAM_64, "QAM64" },
88 { 128, QAM_128, "QAM128" },
89 { 256, QAM_256, "QAM256" },
90 { 2, QPSK, "QPSK" },
91 { 5, PSK_8, "8PSK" },
92 { 6, APSK_16, "16APSK" },
93 { 7, APSK_32, "32APSK" },
94 { 10, VSB_8, "VSB8" },
95 { 11, VSB_16, "VSB16" },
96 { 12, DQPSK, "DQPSK" },
97 { 999, QAM_AUTO, trNOOP("auto") },
98 { -1, 0, NULL }
99 };
100
101#define DVB_SYSTEM_1 0 // see also nit.c
102#define DVB_SYSTEM_2 1
103
105 { 0, DVB_SYSTEM_1, "DVB-S" },
106 { 1, DVB_SYSTEM_2, "DVB-S2" },
107 { -1, 0, NULL }
108 };
109
111 { 0, DVB_SYSTEM_1, "DVB-T" },
112 { 1, DVB_SYSTEM_2, "DVB-T2" },
113 { -1, 0, NULL }
114 };
115
117 { 1, TRANSMISSION_MODE_1K, "1K" },
118 { 2, TRANSMISSION_MODE_2K, "2K" },
119 { 4, TRANSMISSION_MODE_4K, "4K" },
120 { 8, TRANSMISSION_MODE_8K, "8K" },
121 { 16, TRANSMISSION_MODE_16K, "16K" },
122 { 32, TRANSMISSION_MODE_32K, "32K" },
123 { 999, TRANSMISSION_MODE_AUTO, trNOOP("auto") },
124 { -1, 0, NULL }
125 };
126
128 { 4, GUARD_INTERVAL_1_4, "1/4" },
129 { 8, GUARD_INTERVAL_1_8, "1/8" },
130 { 16, GUARD_INTERVAL_1_16, "1/16" },
131 { 32, GUARD_INTERVAL_1_32, "1/32" },
132 { 128, GUARD_INTERVAL_1_128, "1/128" },
133 { 19128, GUARD_INTERVAL_19_128, "19/128" },
134 { 19256, GUARD_INTERVAL_19_256, "19/256" },
135 { 999, GUARD_INTERVAL_AUTO, trNOOP("auto") },
136 { -1, 0, NULL }
137 };
138
140 { 0, HIERARCHY_NONE, trNOOP("none") },
141 { 1, HIERARCHY_1, "1" },
142 { 2, HIERARCHY_2, "2" },
143 { 4, HIERARCHY_4, "4" },
144 { 999, HIERARCHY_AUTO, trNOOP("auto") },
145 { -1, 0, NULL }
146 };
147
149 { 0, ROLLOFF_AUTO, trNOOP("auto") },
150 { 20, ROLLOFF_20, "0.20" },
151 { 25, ROLLOFF_25, "0.25" },
152 { 35, ROLLOFF_35, "0.35" },
153 { -1, 0, NULL }
154 };
155
156int UserIndex(int Value, const tDvbParameterMap *Map)
157{
158 const tDvbParameterMap *map = Map;
159 while (map && map->userValue != -1) {
160 if (map->userValue == Value)
161 return map - Map;
162 map++;
163 }
164 return -1;
165}
166
167int DriverIndex(int Value, const tDvbParameterMap *Map)
168{
169 const tDvbParameterMap *map = Map;
170 while (map && map->userValue != -1) {
171 if (map->driverValue == Value)
172 return map - Map;
173 map++;
174 }
175 return -1;
176}
177
178int MapToUser(int Value, const tDvbParameterMap *Map, const char **String)
179{
180 int n = DriverIndex(Value, Map);
181 if (n >= 0) {
182 if (String)
183 *String = tr(Map[n].userString);
184 return Map[n].userValue;
185 }
186 return -1;
187}
188
189const char *MapToUserString(int Value, const tDvbParameterMap *Map)
190{
191 int n = DriverIndex(Value, Map);
192 if (n >= 0)
193 return Map[n].userString;
194 return "???";
195}
196
197int MapToDriver(int Value, const tDvbParameterMap *Map)
198{
199 int n = UserIndex(Value, Map);
200 if (n >= 0)
201 return Map[n].driverValue;
202 return -1;
203}
204
205// --- cDvbTransponderParameters ---------------------------------------------
206
208{
209 Parse(Parameters);
210}
211
212int cDvbTransponderParameters::PrintParameter(char *p, char Name, int Value) const
213{
214 return Value >= 0 && Value != 999 ? sprintf(p, "%c%d", Name, Value) : 0;
215}
216
218{
219#define ST(s) if (strchr(s, Type) && (strchr(s, '0' + system + 1) || strchr(s, '*')))
220 char buffer[64];
221 char *q = buffer;
222 *q = 0;
223 ST(" S *") q += sprintf(q, "%c", polarization);
224 ST(" T*") q += PrintParameter(q, 'B', MapToUser(bandwidth, BandwidthValues));
225 ST(" CST*") q += PrintParameter(q, 'C', MapToUser(coderateH, CoderateValues));
226 ST(" T*") q += PrintParameter(q, 'D', MapToUser(coderateL, CoderateValues));
227 ST(" T*") q += PrintParameter(q, 'G', MapToUser(guard, GuardValues));
228 ST("ACST*") q += PrintParameter(q, 'I', MapToUser(inversion, InversionValues));
229 ST("ACST*") q += PrintParameter(q, 'M', MapToUser(modulation, ModulationValues));
230 ST(" S 2") q += PrintParameter(q, 'N', MapToUser(pilot, PilotValues));
231 ST(" S 2") q += PrintParameter(q, 'O', MapToUser(rollOff, RollOffValues));
232 ST(" ST2") q += PrintParameter(q, 'P', streamId);
233 ST(" T2") q += PrintParameter(q, 'Q', t2systemId);
234 ST(" ST*") q += PrintParameter(q, 'S', MapToUser(system, SystemValuesSat)); // we only need the numerical value, so Sat or Terr doesn't matter
236 ST(" T2") q += PrintParameter(q, 'X', sisoMiso);
237 ST(" T*") q += PrintParameter(q, 'Y', MapToUser(hierarchy, HierarchyValues));
238 return buffer;
239}
240
241const char *cDvbTransponderParameters::ParseParameter(const char *s, int &Value, const tDvbParameterMap *Map)
242{
243 if (*++s) {
244 char *p = NULL;
245 errno = 0;
246 int n = strtol(s, &p, 10);
247 if (!errno && p != s) {
248 Value = Map ? MapToDriver(n, Map) : n;
249 if (Value >= 0)
250 return p;
251 }
252 }
253 esyslog("ERROR: invalid value for parameter '%c'", *(s - 1));
254 return NULL;
255}
256
258{
259 polarization = 0;
260 inversion = INVERSION_AUTO;
262 coderateH = FEC_AUTO;
263 coderateL = FEC_AUTO;
264 modulation = QAM_AUTO;
266 transmission = TRANSMISSION_MODE_AUTO;
267 guard = GUARD_INTERVAL_AUTO;
268 hierarchy = HIERARCHY_AUTO;
269 rollOff = ROLLOFF_AUTO;
270 streamId = 0;
271 t2systemId = 0;
272 sisoMiso = 0;
273 pilot = PILOT_AUTO;
274 while (s && *s) {
275 switch (toupper(*s)) {
276 case 'B': s = ParseParameter(s, bandwidth, BandwidthValues); break;
277 case 'C': s = ParseParameter(s, coderateH, CoderateValues); break;
278 case 'D': s = ParseParameter(s, coderateL, CoderateValues); break;
279 case 'G': s = ParseParameter(s, guard, GuardValues); break;
280 case 'H': polarization = 'H'; s++; break;
281 case 'I': s = ParseParameter(s, inversion, InversionValues); break;
282 case 'L': polarization = 'L'; s++; break;
283 case 'M': s = ParseParameter(s, modulation, ModulationValues); break;
284 case 'N': s = ParseParameter(s, pilot, PilotValues); break;
285 case 'O': s = ParseParameter(s, rollOff, RollOffValues); break;
286 case 'P': s = ParseParameter(s, streamId); break;
287 case 'Q': s = ParseParameter(s, t2systemId); break;
288 case 'R': polarization = 'R'; s++; break;
289 case 'S': s = ParseParameter(s, system, SystemValuesSat); break; // we only need the numerical value, so Sat or Terr doesn't matter
290 case 'T': s = ParseParameter(s, transmission, TransmissionValues); break;
291 case 'V': polarization = 'V'; s++; break;
292 case 'X': s = ParseParameter(s, sisoMiso); break;
293 case 'Y': s = ParseParameter(s, hierarchy, HierarchyValues); break;
294 default: esyslog("ERROR: unknown parameter key '%c'", *s);
295 return false;
296 }
297 }
298 return true;
299}
300
301// --- cDvbFrontend ----------------------------------------------------------
302
303const char *DeliverySystemNames[] = {
304 "???",
305 "DVB-C",
306 "DVB-C",
307 "DVB-T",
308 "DSS",
309 "DVB-S",
310 "DVB-S2",
311 "DVB-H",
312 "ISDBT",
313 "ISDBS",
314 "ISDBC",
315 "ATSC",
316 "ATSCMH",
317 "DTMB",
318 "CMMB",
319 "DAB",
320 "DVB-T2",
321 "TURBO",
322 "DVB-C",
323 "DVB-C2",
324 NULL
325 };
326
327static const int DeliverySystemNamesMax = sizeof(DeliverySystemNames) / sizeof(DeliverySystemNames[0]) - 2; // -1 to get the maximum allowed index & -1 for the NULL => -2
328
329static const char *GetDeliverySystemName(int Index)
330{
331 if (Index > DeliverySystemNamesMax)
332 Index = 0;
333 return DeliverySystemNames[Index];
334};
335
336#define MAXFRONTENDCMDS 16
337#define SETCMD(c, d) { Props[CmdSeq.num].cmd = (c);\
338 Props[CmdSeq.num].u.data = (d);\
339 if (CmdSeq.num++ > MAXFRONTENDCMDS) {\
340 esyslog("ERROR: too many tuning commands on frontend %d/%d", adapter, frontend);\
341 return false;\
342 }\
343 }
344
346private:
349 uint32_t subsystemId;
350 dvb_frontend_info frontendInfo;
353 bool QueryDeliverySystems(void);
354public:
355 cDvbFrontend(int Adapter, int Frontend);
357 int Open(void);
358 void Close(void);
359 const char *FrontendName(void) { return frontendInfo.name; }
360 bool ProvidesDeliverySystem(int DeliverySystem) const;
361 bool ProvidesModulation(int System, int StreamId, int Modulation) const;
362 int NumDeliverySystems(void) const { return deliverySystems.Size(); }
363 int NumModulations(void) const { return numModulations; }
364 uint32_t SubsystemId(void) const { return subsystemId; }
365 };
366
367cDvbFrontend::cDvbFrontend(int Adapter, int Frontend)
368{
369 adapter = Adapter;
370 frontend = Frontend;
371 fd_frontend = -1;
373 memset(&frontendInfo, 0, sizeof(frontendInfo));
374 strn0cpy(frontendInfo.name, "???", sizeof(frontendInfo.name));
375 numModulations = 0;
376 Open();
378 Close();
379}
380
382{
383 Close();
384}
385
387{
388 Close();
389 fd_frontend = DvbOpen(DEV_DVB_FRONTEND, adapter, frontend, O_RDWR | O_NONBLOCK, true);
390 return fd_frontend;
391}
392
394{
395 if (fd_frontend >= 0) {
396 if (close(fd_frontend) != 0)
397 esyslog("ERROR: frontend %d/%d", adapter, frontend);
398 fd_frontend = -1;
399 }
400}
401
402bool cDvbFrontend::ProvidesDeliverySystem(int DeliverySystem) const
403{
404 for (int i = 0; i < deliverySystems.Size(); i++) {
405 if (deliverySystems[i] == DeliverySystem)
406 return true;
407 }
408 return false;
409}
410
411bool cDvbFrontend::ProvidesModulation(int System, int StreamId, int Modulation) const
412{
413 if (StreamId != 0 && !(frontendInfo.caps & FE_CAN_MULTISTREAM))
414 return false;
415 if (Modulation == QPSK && !(frontendInfo.caps & FE_CAN_QPSK) ||
416 Modulation == QAM_16 && !(frontendInfo.caps & FE_CAN_QAM_16) ||
417 Modulation == QAM_32 && !(frontendInfo.caps & FE_CAN_QAM_32) ||
418 Modulation == QAM_64 && !(frontendInfo.caps & FE_CAN_QAM_64) ||
419 Modulation == QAM_128 && !(frontendInfo.caps & FE_CAN_QAM_128) ||
420 Modulation == QAM_256 && !(frontendInfo.caps & FE_CAN_QAM_256) ||
421 Modulation == QAM_AUTO && !(frontendInfo.caps & FE_CAN_QAM_AUTO) ||
422 Modulation == VSB_8 && !(frontendInfo.caps & FE_CAN_8VSB) ||
423 Modulation == VSB_16 && !(frontendInfo.caps & FE_CAN_16VSB) ||
424 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
425 return false;
426 return true;
427}
428
430{
432 numModulations = 0;
433 if (ioctl(fd_frontend, FE_GET_INFO, &frontendInfo) < 0) {
434 LOG_ERROR;
435 return false;
436 }
437 dtv_property Props[1];
438 dtv_properties CmdSeq;
439 // Determine the version of the running DVB API:
440 if (!DvbApiVersion) {
441 memset(&Props, 0, sizeof(Props));
442 memset(&CmdSeq, 0, sizeof(CmdSeq));
443 CmdSeq.props = Props;
444 SETCMD(DTV_API_VERSION, 0);
445 if (ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
446 LOG_ERROR;
447 return false;
448 }
449 DvbApiVersion = Props[0].u.data;
450 isyslog("DVB API version is 0x%04X (VDR was built with 0x%04X)", DvbApiVersion, DVBAPIVERSION);
451 }
452 // Determine the types of delivery systems this device provides:
453 bool LegacyMode = true;
454 if (DvbApiVersion >= 0x0505) {
455 memset(&Props, 0, sizeof(Props));
456 memset(&CmdSeq, 0, sizeof(CmdSeq));
457 CmdSeq.props = Props;
459 int Result = ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq);
460 if (Result == 0) {
461 for (uint i = 0; i < Props[0].u.buffer.len; i++) {
462 // activate this line to simulate a multi-frontend device if you only have a single-frontend device with DVB-S and DVB-S2:
463 //if (frontend == 0 && Props[0].u.buffer.data[i] != SYS_DVBS || frontend == 1 && Props[0].u.buffer.data[i] != SYS_DVBS2)
464 deliverySystems.Append(Props[0].u.buffer.data[i]);
465 }
466 LegacyMode = false;
467 }
468 else {
469 esyslog("ERROR: can't query delivery systems on frontend %d/%d - falling back to legacy mode", adapter, frontend);
470 }
471 }
472 if (LegacyMode) {
473 // Legacy mode (DVB-API < 5.5):
474 switch (frontendInfo.type) {
475 case FE_QPSK: deliverySystems.Append(SYS_DVBS);
477 deliverySystems.Append(SYS_DVBS2);
478 break;
479 case FE_OFDM: deliverySystems.Append(SYS_DVBT);
482 break;
483 case FE_QAM: deliverySystems.Append(SYS_DVBC_ANNEX_AC); break;
484 case FE_ATSC: deliverySystems.Append(SYS_ATSC); break;
485 default: esyslog("ERROR: unknown frontend type %d on frontend %d/%d", frontendInfo.type, adapter, frontend);
486 }
487 }
488 if (deliverySystems.Size() > 0) {
489 cString ds("");
490 for (int i = 0; i < deliverySystems.Size(); i++)
491 ds = cString::sprintf("%s%s%s", *ds, i ? "," : "", GetDeliverySystemName(deliverySystems[i]));
492 cString ms("");
493 if (frontendInfo.caps & FE_CAN_QPSK) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QPSK, ModulationValues)); }
494 if (frontendInfo.caps & FE_CAN_QAM_16) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_16, ModulationValues)); }
495 if (frontendInfo.caps & FE_CAN_QAM_32) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_32, ModulationValues)); }
496 if (frontendInfo.caps & FE_CAN_QAM_64) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_64, ModulationValues)); }
497 if (frontendInfo.caps & FE_CAN_QAM_128) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_128, ModulationValues)); }
498 if (frontendInfo.caps & FE_CAN_QAM_256) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(QAM_256, ModulationValues)); }
499 if (frontendInfo.caps & FE_CAN_8VSB) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(VSB_8, ModulationValues)); }
500 if (frontendInfo.caps & FE_CAN_16VSB) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", MapToUserString(VSB_16, ModulationValues)); }
501 if (frontendInfo.caps & FE_CAN_TURBO_FEC) { numModulations++; ms = cString::sprintf("%s%s%s", *ms, **ms ? "," : "", "TURBO_FEC"); }
502 if (!**ms)
503 ms = "unknown modulations";
504 isyslog("frontend %d/%d provides %s with %s (\"%s\")", adapter, frontend, *ds, *ms, frontendInfo.name);
505 return true;
506 }
507 else
508 esyslog("ERROR: frontend %d/%d doesn't provide any delivery systems", adapter, frontend);
509 return false;
510}
511
512// --- cDvbTuner -------------------------------------------------------------
513
514#define TUNER_POLL_TIMEOUT 10 // ms
515
517{
518 int ds = SYS_UNDEFINED;
519 if (Channel->IsAtsc())
520 ds = SYS_ATSC;
521 else if (Channel->IsCable())
522 ds = SYS_DVBC_ANNEX_AC;
523 else if (Channel->IsSat())
524 ds = Dtp->System() == DVB_SYSTEM_1 ? SYS_DVBS : SYS_DVBS2;
525 else if (Channel->IsTerr())
526 ds = Dtp->System() == DVB_SYSTEM_1 ? SYS_DVBT : SYS_DVBT2;
527 else
528 esyslog("ERROR: can't determine frontend type for channel %d (%s)", Channel->Number(), Channel->Name());
529 return ds;
530}
531
532class cDvbTuner : public cThread {
533private:
538 mutable int fd_frontend;
540 mutable int frontend;
548 mutable uint32_t lastUncValue;
549 mutable uint32_t lastUncDelta;
550 mutable time_t lastUncChange;
556 const cScr *scr;
559 mutable cMutex mutex;
564 cString GetBondingParams(const cChannel *Channel = NULL) const;
566 bool IsBondedMaster(void) const { return !bondedTuner || bondedMaster; }
567 void ClearEventQueue(void) const;
568 bool GetFrontendStatus(fe_status_t &Status) const;
570 void ExecuteDiseqc(const cDiseqc *Diseqc, int *Frequency);
571 void ResetToneAndVoltage(void);
572 bool SetFrontend(void);
573 virtual void Action(void);
574public:
575 cDvbTuner(const cDvbDevice *Device, int Adapter, int Frontend);
576 virtual ~cDvbTuner();
577 bool ProvidesDeliverySystem(int DeliverySystem) const;
578 bool ProvidesModulation(int System, int StreamId, int Modulation) const;
579 bool ProvidesFrontend(const cChannel *Channel, bool Activate = false) const;
580 int Frontend(void) const { return frontend; }
581 int FrontendType(void) const { return frontendType; }
582 const char *FrontendName(void) { return dvbFrontend->FrontendName(); }
584 bool Bond(cDvbTuner *Tuner);
585 void UnBond(void);
586 bool BondingOk(const cChannel *Channel, bool ConsiderOccupied = false) const;
587 const cChannel *GetTransponder(void) const { return &channel; }
588 uint32_t SubsystemId(void) const { return dvbFrontend->SubsystemId(); }
589 bool IsTunedTo(const cChannel *Channel) const;
590 void SetChannel(const cChannel *Channel);
591 bool Locked(int TimeoutMs = 0);
592 const cPositioner *Positioner(void) const { return positioner; }
593 bool GetSignalStats(int &Valid, double *Strength = NULL, double *Cnr = NULL, double *BerPre = NULL, double *BerPost = NULL, double *Per = NULL, int *Status = NULL) const;
594 int GetSignalStrength(void) const;
595 int GetSignalQuality(void) const;
596 };
597
599
600cDvbTuner::cDvbTuner(const cDvbDevice *Device, int Adapter, int Frontend)
601{
602 frontendType = SYS_UNDEFINED;
603 device = Device;
604 fd_frontend = -1;
605 adapter = Adapter;
607 dvbFrontend = NULL;
608 tuneTimeout = 0;
609 lockTimeout = 0;
611 lastUncValue = 0;
612 lastUncDelta = 0;
613 lastUncChange = 0;
614 lastDiseqc = NULL;
615 diseqcOffset = 0;
616 lastSource = 0;
617 positioner = NULL;
618 scr = NULL;
619 lnbPowerTurnedOn = false;
621 bondedTuner = NULL;
622 bondedMaster = false;
627 cString FrontendNumbers = cString::sprintf("%d", frontend);
628 // Check for multiple frontends:
629 if (frontend == 0) {
630 for (int i = 1; ; i++) {
631 if (access(DvbName(DEV_DVB_FRONTEND, adapter, i), F_OK) == 0) {
632 if (access(DvbName(DEV_DVB_DEMUX, adapter, i), F_OK) != 0) {
633 fe = new cDvbFrontend(adapter, i);
636 //numModulations += fe->NumModulations(); // looks like in multi frontend devices all frontends report the same modulations
637 FrontendNumbers = cString::sprintf("%s+%d", *FrontendNumbers, i);
638 }
639 }
640 else
641 break;
642 }
643 }
644 // Open default frontend:
647 SetDescription("frontend %d/%s tuner", adapter, *FrontendNumbers);
648 Start();
649}
650
652{
656 Cancel(3);
657 UnBond();
658 /* looks like this irritates the SCR switch, so let's leave it out for now
659 if (lastDiseqc && lastDiseqc->IsScr()) {
660 unsigned int Frequency = 0;
661 ExecuteDiseqc(lastDiseqc, &Frequency);
662 }
663 */
664 for (int i = 0; i < dvbFrontends.Size(); i++)
665 delete dvbFrontends[i];
666}
667
668bool cDvbTuner::ProvidesDeliverySystem(int DeliverySystem) const
669{
670 for (int i = 0; i < dvbFrontends.Size(); i++) {
671 if (dvbFrontends[i]->ProvidesDeliverySystem(DeliverySystem))
672 return true;
673 }
674 return false;
675}
676
677bool cDvbTuner::ProvidesModulation(int System, int StreamId, int Modulation) const
678{
679 for (int i = 0; i < dvbFrontends.Size(); i++) {
680 if (dvbFrontends[i]->ProvidesModulation(System, StreamId, Modulation))
681 return true;
682 }
683 return false;
684}
685
686bool cDvbTuner::ProvidesFrontend(const cChannel *Channel, bool Activate) const
687{
689 int DeliverySystem = GetRequiredDeliverySystem(Channel, &dtp);
690 for (int i = 0; i < dvbFrontends.Size(); i++) {
691 if (dvbFrontends[i]->ProvidesDeliverySystem(DeliverySystem) && dvbFrontends[i]->ProvidesModulation(dtp.System(), dtp.StreamId(), dtp.Modulation())) {
692 if (Activate && dvbFrontend != dvbFrontends[i]) {
693 cMutexLock MutexLock(&mutex);
697 frontend = i;
698 dsyslog("using frontend %d/%d", adapter, frontend);
699 lastUncValue = 0;
700 lastUncDelta = 0;
701 lastUncChange = 0;
702 }
703 return true;
704 }
705 }
706 return false;
707}
708
710{
711 cMutexLock MutexLock(&bondMutex);
712 if (!bondedTuner) {
714 bondedMaster = false; // makes sure we don't disturb an existing master
715 bondedTuner = Tuner->bondedTuner ? Tuner->bondedTuner : Tuner;
716 Tuner->bondedTuner = this;
717 dsyslog("tuner %d/%d bonded with tuner %d/%d", adapter, frontend, bondedTuner->adapter, bondedTuner->frontend);
718 return true;
719 }
720 else
721 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);
722 return false;
723}
724
726{
727 cMutexLock MutexLock(&bondMutex);
728 if (cDvbTuner *t = bondedTuner) {
729 dsyslog("tuner %d/%d unbonded from tuner %d/%d", adapter, frontend, bondedTuner->adapter, bondedTuner->frontend);
730 while (t->bondedTuner != this)
731 t = t->bondedTuner;
732 if (t == bondedTuner)
733 t->bondedTuner = NULL;
734 else
735 t->bondedTuner = bondedTuner;
736 bondedMaster = false; // another one will automatically become master whenever necessary
737 bondedTuner = NULL;
738 }
739}
740
742{
743 if (!Channel)
744 Channel = &channel;
746 if (Setup.DiSEqC) {
747 if (const cDiseqc *diseqc = Diseqcs.Get(device->DeviceNumber() + 1, Channel->Source(), Channel->Frequency(), dtp.Polarization(), NULL))
748 return diseqc->Commands();
749 }
750 else {
751 bool ToneOff = Channel->Frequency() < Setup.LnbSLOF;
752 bool VoltOff = dtp.Polarization() == 'V' || dtp.Polarization() == 'R';
753 return cString::sprintf("%c %c", ToneOff ? 't' : 'T', VoltOff ? 'v' : 'V');
754 }
755 return "";
756}
757
758bool cDvbTuner::BondingOk(const cChannel *Channel, bool ConsiderOccupied) const
759{
760 cMutexLock MutexLock(&bondMutex);
761 if (cDvbTuner *t = bondedTuner) {
762 cString BondingParams = GetBondingParams(Channel);
763 do {
764 if (t->device->Priority() > IDLEPRIORITY || ConsiderOccupied && t->device->Occupied()) {
765 if (strcmp(BondingParams, t->GetBondedMaster()->GetBondingParams()) != 0)
766 return false;
767 }
768 t = t->bondedTuner;
769 } while (t != bondedTuner);
770 }
771 return true;
772}
773
775{
776 if (!bondedTuner)
777 return this; // an unbonded tuner is always "master"
778 cMutexLock MutexLock(&bondMutex);
779 if (bondedMaster)
780 return this;
781 // This tuner is bonded, but it's not the master, so let's see if there is a master at all:
782 if (cDvbTuner *t = bondedTuner) {
783 while (t != this) {
784 if (t->bondedMaster)
785 return t;
786 t = t->bondedTuner;
787 }
788 }
789 // None of the other bonded tuners is master, so make this one the master:
790 bondedMaster = true;
791 dsyslog("tuner %d/%d is now bonded master", adapter, frontend);
792 return this;
793}
794
795bool cDvbTuner::IsTunedTo(const cChannel *Channel) const
796{
797 if (tunerStatus == tsIdle)
798 return false; // not tuned to
799 if (channel.Source() != Channel->Source() || channel.Transponder() != Channel->Transponder() || channel.Srate() != Channel->Srate())
800 return false; // sufficient mismatch
801 // Polarization is already checked as part of the Transponder.
802 return strcmp(channel.Parameters(), Channel->Parameters()) == 0;
803}
804
805void cDvbTuner::SetChannel(const cChannel *Channel)
806{
807 if (Channel) {
808 if (bondedTuner) {
809 cMutexLock MutexLock(&bondMutex);
810 cDvbTuner *BondedMaster = GetBondedMaster();
811 if (BondedMaster == this) {
812 if (strcmp(GetBondingParams(Channel), GetBondingParams()) != 0) {
813 // switching to a completely different band, so set all others to idle:
814 for (cDvbTuner *t = bondedTuner; t && t != this; t = t->bondedTuner)
815 t->SetChannel(NULL);
816 }
817 }
818 else if (strcmp(GetBondingParams(Channel), BondedMaster->GetBondingParams()) != 0)
819 BondedMaster->SetChannel(Channel);
820 }
821 cMutexLock MutexLock(&mutex);
822 if (!IsTunedTo(Channel))
824 diseqcOffset = 0;
825 channel = *Channel;
828 }
829 else {
830 cMutexLock MutexLock(&mutex);
833 }
835 cDevice::PrimaryDevice()->DelLivePids(); // 'device' is const, so we must do it this way
836}
837
838bool cDvbTuner::Locked(int TimeoutMs)
839{
840 bool isLocked = (tunerStatus >= tsLocked);
841 if (isLocked || !TimeoutMs)
842 return isLocked;
843
844 cMutexLock MutexLock(&mutex);
845 if (TimeoutMs && tunerStatus < tsLocked)
846 locked.TimedWait(mutex, TimeoutMs);
847 return tunerStatus >= tsLocked;
848}
849
851{
852 cPoller Poller(fd_frontend);
853 if (Poller.Poll(TUNER_POLL_TIMEOUT)) {
854 dvb_frontend_event Event;
855 while (ioctl(fd_frontend, FE_GET_EVENT, &Event) == 0)
856 ; // just to clear the event queue - we'll read the actual status below
857 }
858}
859
860bool cDvbTuner::GetFrontendStatus(fe_status_t &Status) const
861{
863 Status = (fe_status_t)0; // initialize here to fix buggy drivers
864 while (1) {
865 if (ioctl(fd_frontend, FE_READ_STATUS, &Status) != -1)
866 return true;
867 if (errno != EINTR)
868 break;
869 }
870 return false;
871}
872
873//#define DEBUG_SIGNALSTATS
874//#define DEBUG_SIGNALSTRENGTH
875//#define DEBUG_SIGNALQUALITY
876
877bool cDvbTuner::GetSignalStats(int &Valid, double *Strength, double *Cnr, double *BerPre, double *BerPost, double *Per, int *Status) const
878{
880 fe_status_t FeStatus = (fe_status_t)0; // initialize here to fix buggy drivers
881 dtv_property Props[MAXFRONTENDCMDS];
882 dtv_properties CmdSeq;
883 memset(&Props, 0, sizeof(Props));
884 memset(&CmdSeq, 0, sizeof(CmdSeq));
885 CmdSeq.props = Props;
886 Valid = DTV_STAT_VALID_NONE;
887 if (ioctl(fd_frontend, FE_READ_STATUS, &FeStatus) != 0) {
888 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
889 return false;
890 }
891 if (Status) {
892 *Status = DTV_STAT_HAS_NONE;
893 if (FeStatus & FE_HAS_SIGNAL) *Status |= DTV_STAT_HAS_SIGNAL;
894 if (FeStatus & FE_HAS_CARRIER) *Status |= DTV_STAT_HAS_CARRIER;
895 if (FeStatus & FE_HAS_VITERBI) *Status |= DTV_STAT_HAS_VITERBI;
896 if (FeStatus & FE_HAS_SYNC) *Status |= DTV_STAT_HAS_SYNC;
897 if (FeStatus & FE_HAS_LOCK) *Status |= DTV_STAT_HAS_LOCK;
898 Valid |= DTV_STAT_VALID_STATUS;
899 }
900 if (Strength) SETCMD(DTV_STAT_SIGNAL_STRENGTH, 0);
901 if (Cnr) SETCMD(DTV_STAT_CNR, 0);
902 if (BerPre) { SETCMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0);
903 SETCMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0); }
904 if (BerPost) { SETCMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0);
905 SETCMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0); }
906 if (Per) { SETCMD(DTV_STAT_ERROR_BLOCK_COUNT, 0);
907 SETCMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0); }
908 if (CmdSeq.num && ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
909 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
910 return false;
911 }
912 int i = 0;
913 if (Strength) {
914 if (Props[i].u.st.len > 0) {
915 switch (Props[i].u.st.stat[0].scale) {
916 case FE_SCALE_DECIBEL: *Strength = double(Props[i].u.st.stat[0].svalue) / 1000;
918 break;
919 default: ;
920 }
921 }
922 i++;
923 }
924 if (Cnr) {
925 if (Props[i].u.st.len > 0) {
926 switch (Props[i].u.st.stat[0].scale) {
927 case FE_SCALE_DECIBEL: *Cnr = double(Props[i].u.st.stat[0].svalue) / 1000;
928 Valid |= DTV_STAT_VALID_CNR;
929 break;
930 default: ;
931 }
932 }
933 i++;
934 }
935 if (BerPre) {
936 if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
937 if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
938 uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error bit count
939 uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total bit count
940 if (tbc > 0) {
941 *BerPre = double(ebc) / tbc;
942 Valid |= DTV_STAT_VALID_BERPRE;
943 }
944 }
945 }
946 i += 2;
947 }
948 if (BerPost) {
949 if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
950 if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
951 uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error bit count
952 uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total bit count
953 if (tbc > 0) {
954 *BerPost = double(ebc) / tbc;
955 Valid |= DTV_STAT_VALID_BERPOST;
956 }
957 }
958 }
959 i += 2;
960 }
961 if (Per) {
962 if (Props[i].u.st.len > 0 && Props[i + 1].u.st.len > 0) {
963 if (Props[i].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[i + 1].u.st.stat[0].scale == FE_SCALE_COUNTER) {
964 uint64_t ebc = Props[i].u.st.stat[0].uvalue; // error block count
965 uint64_t tbc = Props[i + 1].u.st.stat[0].uvalue; // total block count
966 if (tbc > 0) {
967 *Per = double(ebc) / tbc;
968 Valid |= DTV_STAT_VALID_PER;
969 }
970 }
971 }
972 i += 2;
973 }
974#ifdef DEBUG_SIGNALSTATS
975 fprintf(stderr, "FE %d/%d: API5 %04X", adapter, frontend, Valid);
976 if ((Valid & DTV_STAT_VALID_STATUS) != 0) fprintf(stderr, " STAT=%04X", *Status);
977 if ((Valid & DTV_STAT_VALID_STRENGTH) != 0) fprintf(stderr, " STR=%1.1fdBm", *Strength);
978 if ((Valid & DTV_STAT_VALID_CNR) != 0) fprintf(stderr, " CNR=%1.1fdB", *Cnr);
979 if ((Valid & DTV_STAT_VALID_BERPRE) != 0) fprintf(stderr, " BERPRE=%1.1e", *BerPre);
980 if ((Valid & DTV_STAT_VALID_BERPOST) != 0) fprintf(stderr, " BERPOST=%1.1e", *BerPost);
981 if ((Valid & DTV_STAT_VALID_PER) != 0) fprintf(stderr, " PER=%1.1e", *Per);
982 fprintf(stderr, "\n");
983#endif
984 return Valid != DTV_STAT_VALID_NONE;
985}
986
987int dB1000toPercent(int dB1000, int Low, int High)
988{
989 // Convert the given value, which is in 1/1000 dBm, to a percentage in the
990 // range 0..100. Anything below Low is considered 0%, and anything above
991 // High counts as 100%.
992 if (dB1000 < Low)
993 return 0;
994 if (dB1000 > High)
995 return 100;
996 // return 100 - 100 * (High - dB1000) / (High - Low); // linear conversion
997 // return 100 - 100 * sqr(dB1000 - High) / sqr(Low - High); // quadratic conversion, see https://www.adriangranados.com/blog/dbm-to-percent-conversion
998 double v = 10.0 * (dB1000 - High) / (Low - High); // avoids the sqr() function
999 return 100 - v * v;
1000}
1001
1002#define REF_S1(q1) (mod == QPSK) ? q1 : 0
1003#define REF_S2(q1, q2, q3, q4) (mod == QPSK) ? q1 : (mod == PSK_8) ? q2 : (mod == APSK_16) ? q3 : (mod == APSK_32) ? q4 : 0
1004#define REF_T1(q1, q2, q3) (mod == QPSK) ? q1 : (mod == QAM_16) ? q2 : (mod == QAM_64) ? q3 : 0
1005#define REF_T2(q1, q2, q3, q4) (mod == QPSK) ? q1 : (mod == QAM_16) ? q2 : (mod == QAM_64) ? q3 : (mod == QAM_256) ? q4 : 0
1006#define REF_C1(q1, q2, q3, q4, q5) (mod == QAM_16) ? q1 : (mod == QAM_32) ? q2 : (mod == QAM_64) ? q3 : (mod == QAM_128) ? q4 : (mod == QAM_256) ? q5: 0
1007
1008int StrengthToSSI(const cChannel *Channel, int Strength, int FeModulation, int FeCoderateH, int FeFec)
1009{
1010 // Strength in 0.001dBm (dBm x 1000)
1011 cDvbTransponderParameters dtp(Channel->Parameters());
1012 int ssi = 0; // 0-100
1013 int mod = (FeModulation >= 0) ? FeModulation : dtp.Modulation();
1014 int cod = (FeCoderateH >= 0) ? FeCoderateH : dtp.CoderateH(); // DVB-T
1015 int fec = (FeFec >= 0) ? FeFec : dtp.CoderateH();
1016 if (Channel->IsTerr()) {
1017 int pref = 0;
1018 // NorDig Unified Ver. 2.6 - 3.4.4.6 Page 43 ff.
1019 // reference values : pref-15dBm = 0%, pref+35dBm = 100%
1020 if (dtp.System() == DVB_SYSTEM_1) { // DVB-T
1021 fec = cod; // adjustment for DVB-T
1022 if (mod == QAM_AUTO) mod = QPSK;
1023 switch (fec) { // dBm: Q4 Q16 Q64
1024 case FEC_1_2: pref = REF_T1(-93, -87, -82); break;
1025 default:
1026 case FEC_2_3: pref = REF_T1(-91, -85, -80); break;
1027 case FEC_3_4: pref = REF_T1(-90, -84, -78); break;
1028 case FEC_5_6: pref = REF_T1(-89, -83, -77); break;
1029 case FEC_7_8: pref = REF_T1(-88, -82, -76); break;
1030 }
1031 }
1032 else { // DVB-T2
1033 if (mod == QAM_AUTO) mod = QAM_64;
1034 switch (fec) { // dBm: Q4 Q16 Q64 Q256
1035 case FEC_1_2: pref = REF_T2(-96, -91, -86, -82); break;
1036 default:
1037 case FEC_3_5: pref = REF_T2(-95, -89, -85, -80); break;
1038 case FEC_2_3: pref = REF_T2(-94, -88, -83, -78); break;
1039 case FEC_3_4: pref = REF_T2(-93, -87, -82, -76); break;
1040 case FEC_4_5: pref = REF_T2(-92, -86, -81, -75); break;
1041 case FEC_5_6: pref = REF_T2(-92, -86, -80, -74); break;
1042 }
1043 }
1044 if (pref) {
1045 int prel = (Strength / 1000) - pref;
1046 ssi = (prel < -15) ? 0 :
1047 (prel < 0) ? (prel + 15) * 2 / 3 : // 0% - 10%
1048 (prel < 20) ? prel * 4 + 10 : // 10% - 90%
1049 (prel < 35) ? (prel - 20) * 2 / 3 + 90 : // 90% - 100%
1050 100;
1051#ifdef DEBUG_SIGNALSTRENGTH
1052 fprintf(stderr, "SSI-T: STR:%d, Pref:%d, Prel:%d, ssi:%d%%(sys:%d, mod:%d, fec:%d)\n", Strength, pref, prel, ssi, dtp.System(), mod, fec);
1053#endif
1054 }
1055 }
1056 else if (Channel->IsCable()) { // ! COMPLETELY UNTESTED !
1057 // Formula: pref(dB) = -174.0 + NoiseFigure + SymRef + CnRef
1058 // NoiseFigure = 6.5 dB; -> Tuner specific - range: 3.5 .. 9.0 dB
1059 // SymRef = 10*log(6900000) = 68.5 dB; -> for Symbolrate of 6900 kSym/sec (TV: 6900, 6750 or 6111 kSym/sec)
1060 // ==> pref(dB) = -174.0 + 6.5 + 68.5 + CnRef[modulation]{20,23,26,29,32}; (+/- 3 dB tuner specific)
1061 if (mod == QAM_AUTO) mod = QAM_256;
1062 // Q16 Q32 Q64 Q128 Q256
1063 int pref = REF_C1(-79, -76, -73, -70, -67);
1064 if (pref) {
1065 int prel = (Strength / 1000) - pref;
1066 ssi = (prel < -15) ? 0 :
1067 (prel < 0) ? (prel + 15) * 2 / 3 : // 0% - 10%
1068 (prel < 20) ? prel * 4 + 10 : // 10% - 90%
1069 (prel < 35) ? (prel - 20) * 2 / 3 + 90 : // 90% - 100%
1070 100;
1071#ifdef DEBUG_SIGNALSTRENGTH
1072 fprintf(stderr, "SSI-C: STR:%d, Pref:%d, Prel:%d, ssi:%d%%(mod:%d)\n", Strength, pref, prel, ssi, mod);
1073#endif
1074 }
1075 }
1076 else if (Channel->IsSat())
1077 ssi = dB1000toPercent(Strength, -95000, -20000); // defaults
1078 return ssi;
1079}
1080
1081// Due to missing values or the different meanings of the reported error rate, ber_sqi is currently not taken into account
1082#define IGNORE_BER 1
1083#define BER_ERROR_FREE (1000*1000*1000) // 1/10^-9
1084
1085int SignalToSQI(const cChannel *Channel, int Signal, int Ber, int FeModulation, int FeCoderateH, int FeFec)
1086{
1087#if IGNORE_BER
1088 Ber = BER_ERROR_FREE; // assume/pretend to be biterror free
1089#endif
1090 // Signal in 0.001dB (dB x 1000)
1091 cDvbTransponderParameters dtp(Channel->Parameters());
1092 int sqi = 0; // 0-100
1093 int mod = (FeModulation >= 0) ? FeModulation : dtp.Modulation();
1094 int cod = (FeCoderateH >= 0) ? FeCoderateH : dtp.CoderateH(); // DVB-T
1095 int fec = (FeFec >= 0) ? FeFec : dtp.CoderateH();
1096 if (Channel->IsTerr()) { // QEF: BER 10^-6
1097 int cnref = 0;
1098 // NorDig Unified Ver. 2.6 - 3.4.4.7 Page 45 ff.
1099 // reference values for QEF (BER 10^-11 at MPEG2 demux input)
1100 if (dtp.System() == DVB_SYSTEM_1) { // DVB-T
1101 fec = cod; // adjustment for DVB-T
1102 if (mod == QAM_AUTO) mod = QPSK;
1103 switch (fec) { // 0.1 dB Q4 Q16 Q64 (Hierarchy=None)
1104 case FEC_1_2: cnref = REF_T1(51, 108, 165); break;
1105 default:
1106 case FEC_2_3: cnref = REF_T1(69, 131, 187); break;
1107 case FEC_3_4: cnref = REF_T1(79, 146, 202); break;
1108 case FEC_5_6: cnref = REF_T1(89, 156, 216); break;
1109 case FEC_7_8: cnref = REF_T1(97, 160, 225); break;
1110 }
1111 }
1112 else { // DVB-T2
1113 if (mod == QAM_AUTO) mod = QAM_64;
1114 switch (fec) { // 0.1 dB Q4 Q16 Q64 Q256
1115 case FEC_1_2: cnref = REF_T2(35, 87, 130, 170); break;
1116 default:
1117 case FEC_3_5: cnref = REF_T2(47, 101, 148, 194); break;
1118 case FEC_2_3: cnref = REF_T2(56, 114, 162, 208); break;
1119 case FEC_3_4: cnref = REF_T2(66, 125, 177, 229); break;
1120 case FEC_4_5: cnref = REF_T2(72, 133, 187, 243); break;
1121 case FEC_5_6: cnref = REF_T2(77, 138, 194, 251); break;
1122 }
1123 }
1124 if (cnref) {
1125 int cnrel = (Signal/100) - cnref; // 0.1 dB
1126 int ber_sqi = 100; // 100%
1127 int cnr_sqi = 0; // 0%
1128 if (dtp.System() == DVB_SYSTEM_1) { // DVB-T
1129 ber_sqi = (Ber < 1000) ? 0 : // > 10^-3
1130 (Ber >= 10000000) ? 100 : // <= 10^-7
1131 (int)(20 * log10(Ber)) - 40; // 20*log10(1/BER)-40 -> 20% .. 100%
1132 // scale: -7dB/+3dB to reference-value
1133 cnr_sqi = (cnrel < -70) ? 0 :
1134 (cnrel < +30) ? (100 + (cnrel - 30)) :
1135 100;
1136 sqi = (cnr_sqi * ber_sqi) / 100;
1137 // alternative: stretched scale: cnref-7dB = 0%, 30dB = 100%
1138 // sqi = dB1000toPercent(Signal, (100*cnref)-7000, 30000);
1139 }
1140 else { // DVB-T2
1141 ber_sqi = (Ber < 10000) ? 0 : // > 10^-4
1142 (Ber >= 10000000) ? 100 * 100 / 6 : // <= 10^-7 : 16.67% -> SQI 0% .. 100%
1143 (100 * 100 / 15); // 6.67% -> SQI 0% .. 40% || 100%
1144 // scale: -3dB/+3dB to reference-value
1145 sqi = (cnrel < -30) ? 0 :
1146 (cnrel <= +30) ? (cnrel + 30) * ber_sqi / 1000 : // (0 .. 6) * 16,67 || 6.67
1147 100;
1148 // alternative: stretched scale: cnref-3dB = 0%, 32dB = 100%
1149 // sqi = dB1000toPercent(Signal, (100*cnref)-3000, 32000);
1150 }
1151#ifdef DEBUG_SIGNALQUALITY
1152 fprintf(stderr, "SQI-T: SIG:%d, BER:%d, CNref:%d, CNrel:%d, bersqi:%d, sqi:%d%%(sys:%d, mod:%d, fec:%d)\n", Signal, Ber, cnref, cnrel, ber_sqi, sqi, dtp.System(), mod, fec);
1153#endif
1154 }
1155 }
1156 else if (Channel->IsCable()) { // ! COMPLETELY UNTESTED !
1157 if (mod == QAM_AUTO) mod = QAM_256;
1158 // 0.1 dB Q16 Q32 Q64 Q128 Q256
1159 int cnref = REF_C1(200, 230, 260, 290, 320); // minimum for BER<10^-4
1160 if (cnref) {
1161 int cnrel = (Signal / 100) - cnref; // 0.1 dB
1162 int ber_sqi = (Ber < 1000) ? 0 : // > 10^-3
1163 (Ber >= 10000000) ? 100 : // <= 10^-7
1164 (int)(20 * log10(Ber)) - 40; // 20*log10(1/BER)-40 -> 20% .. 100%
1165 // scale: -7dB/+3dB to reference-value
1166 int cnr_sqi = (cnrel < -70) ? 0 :
1167 (cnrel < +30) ? (100 + (cnrel - 30)) :
1168 100;
1169 sqi = (cnr_sqi * ber_sqi) / 100;
1170 // alternative: stretched scale: cnref-7dB = 0%, 40dB = 100%
1171 // sqi = dB1000toPercent(Signal, (100*cnref)-7000, 40000);
1172#ifdef DEBUG_SIGNALQUALITY
1173 dsyslog("SQI-C: SIG:%d, BER:%d, CNref:%d, CNrel:%d, bersqi:%d, sqi:%d%%(sys:%d, mod:%d, fec:%d)\n", Signal, Ber, cnref, cnrel, ber_sqi, sqi, dtp.System(), mod, fec);
1174#endif
1175 }
1176 }
1177 else if (Channel->IsSat()) {
1178 int cnref = 0;
1179 if (dtp.System() == DVB_SYSTEM_1) { // DVB-S
1180 if (mod == QAM_AUTO) mod = QPSK;
1181 switch (fec) { // 0.1 dB: Q4 : 10^-7
1182 case FEC_1_2: cnref = REF_S1(38); break;
1183 default:
1184 case FEC_2_3: cnref = REF_S1(56); break;
1185 case FEC_3_4: cnref = REF_S1(67); break;
1186 case FEC_5_6: cnref = REF_S1(77); break;
1187 case FEC_7_8: cnref = REF_S1(84); break;
1188 }
1189 if (cnref) {
1190 //cnrel = (Signal/100) - cnref; // 0.1 dB
1191 // scale: cnref-4dB = 0%, 15dB = 100%
1192 sqi = dB1000toPercent(Signal, (100*cnref)-4000, 15000);
1193#ifdef DEBUG_SIGNALQUALITY
1194 dsyslog("SQI-S1: SIG:%d, BER:%d, CNref:%d, sqi:%d%%(mod:%d, fec:%d)\n", Signal, Ber, cnref, sqi, mod, fec);
1195#endif
1196 }
1197 }
1198 else { // DVB-S2
1199 if (mod == QAM_AUTO) mod = QAM_64;
1200 switch (fec) { // 0.1 dB Q4 Q8 16A* 32A*
1201 //case FEC_1_4: cnref = REF_S2(-14, 65, 90, 126); break;
1202 //case FEC_1_3: cnref = REF_S2( -2, 65, 90, 126); break;
1203 case FEC_2_5: cnref = REF_S2( 7, 65, 90, 126); break;
1204 case FEC_1_2: cnref = REF_S2( 20, 65, 90, 126); break;
1205 case FEC_3_5: cnref = REF_S2( 32, 65, 90, 126); break;
1206 default:
1207 case FEC_2_3: cnref = REF_S2( 41, 76, 90, 126); break;
1208 case FEC_3_4: cnref = REF_S2( 50, 66, 102, 126); break;
1209 case FEC_4_5: cnref = REF_S2( 57, 89, 110, 136); break;
1210 case FEC_5_6: cnref = REF_S2( 62, 104, 116, 143); break;
1211 case FEC_8_9: cnref = REF_S2( 72, 117, 129, 157); break;
1212 case FEC_9_10: cnref = REF_S2( 74, 120, 131, 161); break;
1213 }
1214 if (cnref) {
1215 // cnrel = (Signal/100) - cnref; // 0.1 dB
1216 // scale: cnref-4dB = 0%, 20dB = 100%
1217 sqi = dB1000toPercent(Signal, (100*cnref)-4000, 20000);
1218#ifdef DEBUG_SIGNALQUALITY
1219 dsyslog("SQI-S2: SIG:%d, BER:%d, CNref:%d, sqi:%d%%(mod:%d, fec:%d)\n", Signal, Ber, cnref, sqi, mod, fec);
1220#endif
1221 }
1222 }
1223 }
1224 return sqi;
1225}
1226
1228{
1230 // Try DVB API 5:
1231 for (int i = 0; i < 1; i++) { // just a trick to break out with 'continue' ;-)
1232 dtv_property Props[MAXFRONTENDCMDS];
1233 dtv_properties CmdSeq;
1234 memset(&Props, 0, sizeof(Props));
1235 memset(&CmdSeq, 0, sizeof(CmdSeq));
1236 CmdSeq.props = Props;
1237 SETCMD(DTV_STAT_SIGNAL_STRENGTH, 0);
1238 SETCMD(DTV_MODULATION, 0);
1239 SETCMD(DTV_CODE_RATE_HP, 0); // DVB-T only
1240 SETCMD(DTV_INNER_FEC, 0);
1241 if (ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
1242 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
1243 return -1;
1244 }
1245 int FeMod = (Props[1].u.st.len > 0) ? (int)Props[1].u.data : -1;
1246 int FeCod = (Props[2].u.st.len > 0) ? (int)Props[2].u.data : -1;
1247 int FeFec = (Props[3].u.st.len > 0) ? (int)Props[3].u.data : -1;
1248 int Signal = 0;
1249 if (Props[0].u.st.len > 0) {
1250 switch (Props[0].u.st.stat[0].scale) {
1251 case FE_SCALE_DECIBEL: Signal = StrengthToSSI(&channel, Props[0].u.st.stat[0].svalue, FeMod, FeCod, FeFec);
1252 break;
1253 case FE_SCALE_RELATIVE: Signal = 100 * Props[0].u.st.stat[0].uvalue / 0xFFFF;
1254 break;
1255 default: ;
1256 }
1257#ifdef DEBUG_SIGNALSTRENGTH
1258 fprintf(stderr, "FE %d/%d: API5 %d %08X %.1f S = %d\n", adapter, frontend, Props[0].u.st.stat[0].scale, int(Props[0].u.st.stat[0].svalue), int(Props[0].u.st.stat[0].svalue) / 1000.0, Signal);
1259#endif
1260 }
1261 else
1262 continue;
1263 return Signal;
1264 }
1265 // Fall back to DVB API 3:
1266 uint16_t Signal;
1267 while (1) {
1268 if (ioctl(fd_frontend, FE_READ_SIGNAL_STRENGTH, &Signal) != -1)
1269 break;
1270 if (errno != EINTR)
1271 return -1;
1272 }
1273 uint16_t MaxSignal = 0xFFFF; // Let's assume the default is using the entire range.
1274 // Use the subsystemId to identify individual devices in case they need
1275 // special treatment to map their Signal value into the range 0...0xFFFF.
1276 switch (dvbFrontend->SubsystemId()) {
1277 case 0x13C21019: // TT-budget S2-3200 (DVB-S/DVB-S2)
1278 case 0x1AE40001: // TechniSat SkyStar HD2 (DVB-S/DVB-S2)
1279 MaxSignal = 670; break;
1280 }
1281 int s = int(Signal) * 100 / MaxSignal;
1282 if (s > 100)
1283 s = 100;
1284#ifdef DEBUG_SIGNALSTRENGTH
1285 fprintf(stderr, "FE %d/%d: API3 %08X S = %04X %04X %3d%%\n", adapter, frontend, dvbFrontend->SubsystemId(), MaxSignal, Signal, s);
1286#endif
1287 return s;
1288}
1289
1290#define LOCK_THRESHOLD 5 // indicates that all 5 FE_HAS_* flags are set
1291
1293{
1294 // Try DVB API 5:
1295 for (int i = 0; i < 1; i++) { // just a trick to break out with 'continue' ;-)
1296 dtv_property Props[MAXFRONTENDCMDS];
1297 dtv_properties CmdSeq;
1298 memset(&Props, 0, sizeof(Props));
1299 memset(&CmdSeq, 0, sizeof(CmdSeq));
1300 CmdSeq.props = Props;
1301 SETCMD(DTV_STAT_CNR, 0);
1302 SETCMD(DTV_MODULATION, 0);
1303 SETCMD(DTV_CODE_RATE_HP, 0); // DVB-T only
1304 SETCMD(DTV_INNER_FEC, 0);
1305 SETCMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0);
1306 SETCMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0);
1307 if (ioctl(fd_frontend, FE_GET_PROPERTY, &CmdSeq) != 0) {
1308 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
1309 return -1;
1310 }
1311 int FeMod = (Props[1].u.st.len > 0) ? (int)Props[1].u.data : -1;
1312 int FeCod = (Props[2].u.st.len > 0) ? (int)Props[2].u.data : -1;
1313 int FeFec = (Props[3].u.st.len > 0) ? (int)Props[3].u.data : -1;
1314 int Ber = BER_ERROR_FREE; // 1/10^-9
1315 if (Props[4].u.st.len > 0 && Props[4].u.st.stat[0].scale == FE_SCALE_COUNTER && Props[5].u.st.len > 0 && Props[5].u.st.stat[0].scale == FE_SCALE_COUNTER) {
1316 uint64_t ebc = Props[4].u.st.stat[0].uvalue; // error bit count
1317 uint64_t tbc = Props[5].u.st.stat[0].uvalue; // total bit count
1318 if (ebc > 0) {
1319 uint64_t BerRev = tbc / ebc; // reversed, for integer arithmetic
1320 if (BerRev < BER_ERROR_FREE)
1321 Ber = (int)BerRev;
1322 }
1323 }
1324 int Cnr = 0;
1325 if (Props[0].u.st.len > 0) {
1326 switch (Props[0].u.st.stat[0].scale) {
1327 case FE_SCALE_DECIBEL: Cnr = SignalToSQI(&channel, Props[0].u.st.stat[0].svalue, Ber, FeMod, FeCod, FeFec);
1328 break;
1329 case FE_SCALE_RELATIVE: Cnr = 100 * Props[0].u.st.stat[0].uvalue / 0xFFFF;
1330 break;
1331 default: ;
1332 }
1333#ifdef DEBUG_SIGNALQUALITY
1334 fprintf(stderr, "FE %d/%d: API5 %d %08X %.1f Q = %d\n", adapter, frontend, Props[0].u.st.stat[0].scale, int(Props[0].u.st.stat[0].svalue), int(Props[0].u.st.stat[0].svalue) / 1000.0, Cnr);
1335#endif
1336 }
1337 else
1338 continue;
1339 return Cnr;
1340 }
1341 // Fall back to DVB API 3:
1342 fe_status_t Status;
1343 if (GetFrontendStatus(Status)) {
1344 // 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...
1345 if ((Status & FE_HAS_LOCK) == 0) {
1346 if ((Status & FE_HAS_SIGNAL) == 0)
1347 return 0;
1348 if ((Status & FE_HAS_CARRIER) == 0)
1349 return 1;
1350 if ((Status & FE_HAS_VITERBI) == 0)
1351 return 2;
1352 if ((Status & FE_HAS_SYNC) == 0)
1353 return 3;
1354 return 4;
1355 }
1356#ifdef DEBUG_SIGNALQUALITY
1357 bool HasSnr = true;
1358#endif
1359 uint16_t Snr;
1360 while (1) {
1361 if (ioctl(fd_frontend, FE_READ_SNR, &Snr) != -1)
1362 break;
1363 if (errno != EINTR) {
1364 Snr = 0xFFFF;
1365#ifdef DEBUG_SIGNALQUALITY
1366 HasSnr = false;
1367#endif
1368 break;
1369 }
1370 }
1371#ifdef DEBUG_SIGNALQUALITY
1372 bool HasBer = true;
1373#endif
1374 uint32_t Ber;
1375 while (1) {
1376 if (ioctl(fd_frontend, FE_READ_BER, &Ber) != -1)
1377 break;
1378 if (errno != EINTR) {
1379 Ber = 0;
1380#ifdef DEBUG_SIGNALQUALITY
1381 HasBer = false;
1382#endif
1383 break;
1384 }
1385 }
1386#ifdef DEBUG_SIGNALQUALITY
1387 bool HasUnc = true;
1388#endif
1389 uint32_t Unc;
1390 while (1) {
1391 if (ioctl(fd_frontend, FE_READ_UNCORRECTED_BLOCKS, &Unc) != -1) {
1392 if (Unc != lastUncValue) {
1393#ifdef DEBUG_SIGNALQUALITY
1394 fprintf(stderr, "FE %d/%d: API3 UNC = %u %u %u\n", adapter, frontend, Unc, lastUncValue, lastUncDelta);
1395#endif
1396 lastUncDelta = (Unc >= lastUncValue) ? Unc - lastUncValue : lastUncValue - Unc;
1397 lastUncValue = Unc;
1398 lastUncChange = time(NULL);
1399 }
1400 // The number of uncorrected blocks is a counter, which is normally
1401 // at a constant value and only increases if there are new uncorrected
1402 // blocks. So a change in the Unc value indicates reduced signal quality.
1403 // Whenever the Unc counter changes, we take the delta between the old
1404 // and new value into account for calculating the overall signal quality.
1405 // The impact of Unc is considered for 2 seconds, and after that it is
1406 // bisected with every passing second in order to phase it out. Otherwise
1407 // once one or more uncorrected blocks occur, the signal quality would
1408 // be considered low even if there haven't been any more uncorrected bocks
1409 // for quite a while.
1410 Unc = lastUncDelta;
1411 if (Unc > 0) {
1412 int t = time(NULL) - lastUncChange - 2;
1413 if (t > 0)
1414 Unc >>= min(t, int(sizeof(Unc) * 8 - 1));
1415 if (Unc == 0)
1416 lastUncDelta = 0;
1417#ifdef DEBUG_SIGNALQUALITY
1418 fprintf(stderr, "FE %d/%d: API3 UNC = %u\n", adapter, frontend, Unc);
1419#endif
1420 }
1421 break;
1422 }
1423 if (errno != EINTR) {
1424 Unc = 0;
1425#ifdef DEBUG_SIGNALQUALITY
1426 HasUnc = false;
1427#endif
1428 break;
1429 }
1430 }
1431 uint16_t MinSnr = 0x0000;
1432 uint16_t MaxSnr = 0xFFFF; // Let's assume the default is using the entire range.
1433 // Use the subsystemId to identify individual devices in case they need
1434 // special treatment to map their Snr value into the range 0...0xFFFF.
1435 switch (dvbFrontend->SubsystemId()) {
1436 case 0x13C21019: // TT-budget S2-3200 (DVB-S/DVB-S2)
1437 case 0x1AE40001: // TechniSat SkyStar HD2 (DVB-S/DVB-S2)
1438 if (frontendType == SYS_DVBS2) {
1439 MinSnr = 10;
1440 MaxSnr = 70;
1441 }
1442 else
1443 MaxSnr = 200;
1444 break;
1445 case 0x20130245: // PCTV Systems PCTV 73ESE
1446 case 0x2013024F: // PCTV Systems nanoStick T2 290e
1447 MaxSnr = 255; break;
1448 }
1449 int a = int(constrain(Snr, MinSnr, MaxSnr)) * 100 / (MaxSnr - MinSnr);
1450 int b = 100 - (Unc * 10 + (Ber / 256) * 5);
1451 if (b < 0)
1452 b = 0;
1453 int q = LOCK_THRESHOLD + a * b * (100 - LOCK_THRESHOLD) / 100 / 100;
1454 if (q > 100)
1455 q = 100;
1456#ifdef DEBUG_SIGNALQUALITY
1457 fprintf(stderr, "FE %d/%d: API3 %08X Q = %04X %04X %d %5d %5d %3d%%\n", adapter, frontend, dvbFrontend->SubsystemId(), MaxSnr, Snr, HasSnr, HasBer ? int(Ber) : -1, HasUnc ? int(Unc) : -1, q);
1458#endif
1459 return q;
1460 }
1461 return -1;
1462}
1463
1464static unsigned int FrequencyToHz(unsigned int f)
1465{
1466 while (f && f < 1000000)
1467 f *= 1000;
1468 return f;
1469}
1470
1472{
1473 if (!positioner) {
1476 }
1477 return positioner;
1478}
1479
1480void cDvbTuner::ExecuteDiseqc(const cDiseqc *Diseqc, int *Frequency)
1481{
1482 if (!lnbPowerTurnedOn) {
1483 CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_13)); // must explicitly turn on LNB power
1484 lnbPowerTurnedOn = true;
1485 }
1486 static cMutex Mutex;
1487 if (Diseqc->IsScr())
1488 Mutex.Lock();
1489 struct dvb_diseqc_master_cmd cmd;
1490 const char *CurrentAction = NULL;
1491 cPositioner *Positioner = NULL;
1492 bool Break = false;
1493 for (int i = 0; !Break; i++) {
1494 cmd.msg_len = sizeof(cmd.msg);
1495 cDiseqc::eDiseqcActions da = Diseqc->Execute(&CurrentAction, cmd.msg, &cmd.msg_len, scr, Frequency);
1496 if (da == cDiseqc::daNone) {
1497 diseqcOffset = 0;
1498 break;
1499 }
1500 bool d = i >= diseqcOffset;
1501 switch (da) {
1502 case cDiseqc::daToneOff: if (d) CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_OFF)); break;
1503 case cDiseqc::daToneOn: if (d) CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_ON)); break;
1504 case cDiseqc::daVoltage13: if (d) CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_13)); break;
1505 case cDiseqc::daVoltage18: if (d) CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, SEC_VOLTAGE_18)); break;
1506 case cDiseqc::daMiniA: if (d) CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_BURST, SEC_MINI_A)); break;
1507 case cDiseqc::daMiniB: if (d) CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_BURST, SEC_MINI_B)); break;
1508 case cDiseqc::daCodes: if (d) CHECK(ioctl(fd_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd)); break;
1509 case cDiseqc::daPositionN: if ((Positioner = GetPositioner()) != NULL) {
1510 if (d) {
1512 Break = Positioner->IsMoving();
1513 }
1514 }
1515 break;
1516 case cDiseqc::daPositionA: if ((Positioner = GetPositioner()) != NULL) {
1517 if (d) {
1519 Break = Positioner->IsMoving();
1520 }
1521 }
1522 break;
1523 case cDiseqc::daScr:
1524 case cDiseqc::daWait: break;
1525 default: esyslog("ERROR: unknown diseqc command %d", da);
1526 }
1527 if (Break)
1528 diseqcOffset = i + 1;
1529 }
1531 if (scr && !Break)
1532 ResetToneAndVoltage(); // makes sure we don't block the bus!
1533 if (Diseqc->IsScr())
1534 Mutex.Unlock();
1535}
1536
1538{
1539 CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, bondedTuner ? SEC_VOLTAGE_OFF : SEC_VOLTAGE_13));
1540 CHECK(ioctl(fd_frontend, FE_SET_TONE, SEC_TONE_OFF));
1541}
1542
1544{
1545 dtv_property Props[MAXFRONTENDCMDS];
1546 memset(&Props, 0, sizeof(Props));
1547 dtv_properties CmdSeq;
1548 memset(&CmdSeq, 0, sizeof(CmdSeq));
1549 CmdSeq.props = Props;
1550 SETCMD(DTV_CLEAR, 0);
1551 if (ioctl(fd_frontend, FE_SET_PROPERTY, &CmdSeq) < 0) {
1552 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
1553 return false;
1554 }
1555 CmdSeq.num = 0;
1556
1558
1559 // Determine the required frontend type:
1561 if (frontendType == SYS_UNDEFINED)
1562 return false;
1563
1564 SETCMD(DTV_DELIVERY_SYSTEM, frontendType);
1565 if (frontendType == SYS_DVBS || frontendType == SYS_DVBS2) {
1566 int frequency = channel.Frequency();
1567 if (Setup.DiSEqC) {
1568 if (const cDiseqc *diseqc = Diseqcs.Get(device->DeviceNumber() + 1, channel.Source(), frequency, dtp.Polarization(), &scr)) {
1569 frequency -= diseqc->Lof();
1570 if (diseqc != lastDiseqc || diseqc->IsScr() || diseqc->Position() >= 0 && channel.Source() != lastSource) {
1571 if (IsBondedMaster()) {
1572 ExecuteDiseqc(diseqc, &frequency);
1573 if (frequency == 0)
1574 return false;
1575 }
1576 else
1578 lastDiseqc = diseqc;
1580 }
1581 }
1582 else {
1583 esyslog("ERROR: no DiSEqC parameters found for channel %d (%s)", channel.Number(), channel.Name());
1584 return false;
1585 }
1586 }
1587 else {
1588 int tone = SEC_TONE_OFF;
1589 if (frequency < Setup.LnbSLOF) {
1590 frequency -= Setup.LnbFrequLo;
1591 tone = SEC_TONE_OFF;
1592 }
1593 else {
1594 frequency -= Setup.LnbFrequHi;
1595 tone = SEC_TONE_ON;
1596 }
1597 int volt = (dtp.Polarization() == 'V' || dtp.Polarization() == 'R') ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18;
1598 if (!IsBondedMaster()) {
1599 tone = SEC_TONE_OFF;
1600 volt = SEC_VOLTAGE_13;
1601 }
1602 CHECK(ioctl(fd_frontend, FE_SET_VOLTAGE, volt));
1603 CHECK(ioctl(fd_frontend, FE_SET_TONE, tone));
1604 }
1605 frequency = abs(frequency); // Allow for C-band, where the frequency is less than the LOF
1606
1607 // DVB-S/DVB-S2 (common parts)
1608 SETCMD(DTV_FREQUENCY, frequency * 1000UL);
1609 SETCMD(DTV_MODULATION, dtp.Modulation());
1610 SETCMD(DTV_SYMBOL_RATE, channel.Srate() * 1000UL);
1611 SETCMD(DTV_INNER_FEC, dtp.CoderateH());
1612 SETCMD(DTV_INVERSION, dtp.Inversion());
1613 if (frontendType == SYS_DVBS2) {
1614 // DVB-S2
1615 SETCMD(DTV_PILOT, dtp.Pilot());
1616 SETCMD(DTV_ROLLOFF, dtp.RollOff());
1617 if (DvbApiVersion >= 0x0508)
1619 }
1620 else {
1621 // DVB-S
1622 SETCMD(DTV_ROLLOFF, ROLLOFF_35); // DVB-S always has a ROLLOFF of 0.35
1623 }
1624
1627 }
1628 else if (frontendType == SYS_DVBC_ANNEX_AC || frontendType == SYS_DVBC_ANNEX_B) {
1629 // DVB-C
1630 SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
1631 SETCMD(DTV_INVERSION, dtp.Inversion());
1632 SETCMD(DTV_SYMBOL_RATE, channel.Srate() * 1000UL);
1633 SETCMD(DTV_INNER_FEC, dtp.CoderateH());
1634 SETCMD(DTV_MODULATION, dtp.Modulation());
1635
1638 }
1639 else if (frontendType == SYS_DVBT || frontendType == SYS_DVBT2) {
1640 // DVB-T/DVB-T2 (common parts)
1641 SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
1642 SETCMD(DTV_INVERSION, dtp.Inversion());
1643 SETCMD(DTV_BANDWIDTH_HZ, dtp.Bandwidth());
1644 SETCMD(DTV_CODE_RATE_HP, dtp.CoderateH());
1645 SETCMD(DTV_CODE_RATE_LP, dtp.CoderateL());
1646 SETCMD(DTV_MODULATION, dtp.Modulation());
1647 SETCMD(DTV_TRANSMISSION_MODE, dtp.Transmission());
1648 SETCMD(DTV_GUARD_INTERVAL, dtp.Guard());
1649 SETCMD(DTV_HIERARCHY, dtp.Hierarchy());
1650 if (frontendType == SYS_DVBT2) {
1651 // DVB-T2
1652 SETCMD(DTV_INNER_FEC, dtp.CoderateH());
1653 if (DvbApiVersion >= 0x0508) {
1655 }
1656 else if (DvbApiVersion >= 0x0503)
1658 }
1661 }
1662 else if (frontendType == SYS_ATSC) {
1663 // ATSC
1664 SETCMD(DTV_FREQUENCY, FrequencyToHz(channel.Frequency()));
1665 SETCMD(DTV_INVERSION, dtp.Inversion());
1666 SETCMD(DTV_MODULATION, dtp.Modulation());
1667
1670 }
1671 else {
1672 esyslog("ERROR: attempt to set channel with unknown DVB frontend type");
1673 return false;
1674 }
1675 SETCMD(DTV_TUNE, 0);
1676 if (ioctl(fd_frontend, FE_SET_PROPERTY, &CmdSeq) < 0) {
1677 esyslog("ERROR: frontend %d/%d: %m (%s:%d)", adapter, frontend, __FILE__, __LINE__);
1678 return false;
1679 }
1680 return true;
1681}
1682
1684{
1685 cTimeMs Timer;
1686 bool LostLock = false;
1687 fe_status_t Status = (fe_status_t)0;
1688 while (Running()) {
1689 int WaitTime = 1000;
1690 fe_status_t NewStatus;
1691 if (GetFrontendStatus(NewStatus))
1692 Status = NewStatus;
1693 cMutexLock MutexLock(&mutex);
1694 switch (tunerStatus) {
1695 case tsIdle:
1696 break; // we want the TimedWait() below!
1697 case tsSet:
1699 continue;
1700 case tsPositioning:
1701 if (positioner) {
1702 if (positioner->IsMoving())
1703 break; // we want the TimedWait() below!
1704 else if (diseqcOffset) {
1705 lastDiseqc = NULL;
1706 tunerStatus = tsSet; // have it process the rest of the DiSEqC sequence
1707 continue;
1708 }
1709 }
1711 device->SectionHandler()->SetStatus(true); // may have been turned off when retuning
1712 Timer.Set(tuneTimeout + (scr ? rand() % SCR_RANDOM_TIMEOUT : 0));
1713 if (positioner)
1714 continue;
1715 // otherwise run directly into tsTuned...
1716 case tsTuned:
1717 if (Timer.TimedOut()) {
1719 lastDiseqc = NULL;
1720 lastSource = 0;
1721 if (time(NULL) - lastTimeoutReport > 60) { // let's not get too many of these
1722 if (channel.Number()) // no need to log this for transponders that are announced in the NIT but are not currently broadcasting
1723 isyslog("frontend %d/%d timed out while tuning to channel %d (%s), tp %d", adapter, frontend, channel.Number(), channel.Name(), channel.Transponder());
1724 lastTimeoutReport = time(NULL);
1725 }
1726 continue;
1727 }
1728 WaitTime = 100; // allows for a quick change from tsTuned to tsLocked
1729 // run into tsLocked...
1730 case tsLocked:
1731 if (Status & FE_REINIT) {
1733 lastDiseqc = NULL;
1734 lastSource = 0;
1735 isyslog("frontend %d/%d was reinitialized", adapter, frontend);
1737 continue;
1738 }
1739 else if (Status & FE_HAS_LOCK) {
1740 if (LostLock) {
1741 isyslog("frontend %d/%d regained lock on channel %d (%s), tp %d", adapter, frontend, channel.Number(), channel.Name(), channel.Transponder());
1742 LostLock = false;
1743 }
1744 if (device->SdtFilter()->TransponderWrong()) {
1745 isyslog("frontend %d/%d is not receiving transponder %d for channel %d (%s) - retuning", adapter, frontend, channel.Transponder(), channel.Number(), channel.Name());
1746 device->SectionHandler()->SetStatus(false);
1748 lastDiseqc = NULL;
1749 lastSource = 0;
1750 continue;
1751 }
1753 locked.Broadcast();
1755 }
1756 else if (tunerStatus == tsLocked) {
1757 LostLock = true;
1758 isyslog("frontend %d/%d lost lock on channel %d (%s), tp %d", adapter, frontend, channel.Number(), channel.Name(), channel.Transponder());
1760 Timer.Set(lockTimeout);
1762 continue;
1763 }
1764 break;
1765 default: esyslog("ERROR: unknown tuner status %d", tunerStatus);
1766 }
1767 newSet.TimedWait(mutex, WaitTime);
1768 }
1769}
1770
1771// --- cDvbSourceParam -------------------------------------------------------
1772
1774private:
1778public:
1779 cDvbSourceParam(char Source, const char *Description);
1780 virtual void SetData(cChannel *Channel);
1781 virtual void GetData(cChannel *Channel);
1782 virtual cOsdItem *GetOsdItem(void);
1783 };
1784
1785cDvbSourceParam::cDvbSourceParam(char Source, const char *Description)
1786:cSourceParam(Source, Description)
1787{
1788 param = 0;
1789 srate = 0;
1790}
1791
1793{
1794 srate = Channel->Srate();
1795 dtp.Parse(Channel->Parameters());
1796 param = 0;
1797}
1798
1800{
1801 Channel->SetTransponderData(Channel->Source(), Channel->Frequency(), srate, dtp.ToString(Source()), true);
1802}
1803
1805{
1806 char type = Source();
1807 const tDvbParameterMap *SystemValues = type == 'S' ? SystemValuesSat : SystemValuesTerr;
1808#undef ST
1809#define ST(s) if (strchr(s, type))
1810 switch (param++) {
1811 case 0: ST(" S ") return new cMenuEditChrItem( tr("Polarization"), &dtp.polarization, "HVLR"); else return GetOsdItem();
1812 case 1: ST(" ST") return new cMenuEditMapItem( tr("System"), &dtp.system, SystemValues); else return GetOsdItem();
1813 case 2: ST(" CS ") return new cMenuEditIntItem( tr("Srate"), &srate); else return GetOsdItem();
1814 case 3: ST("ACST") return new cMenuEditMapItem( tr("Inversion"), &dtp.inversion, InversionValues); else return GetOsdItem();
1815 case 4: ST(" CST") return new cMenuEditMapItem( tr("CoderateH"), &dtp.coderateH, CoderateValues); else return GetOsdItem();
1816 case 5: ST(" T") return new cMenuEditMapItem( tr("CoderateL"), &dtp.coderateL, CoderateValues); else return GetOsdItem();
1817 case 6: ST("ACST") return new cMenuEditMapItem( tr("Modulation"), &dtp.modulation, ModulationValues); else return GetOsdItem();
1818 case 7: ST(" T") return new cMenuEditMapItem( tr("Bandwidth"), &dtp.bandwidth, BandwidthValues); else return GetOsdItem();
1819 case 8: ST(" T") return new cMenuEditMapItem( tr("Transmission"), &dtp.transmission, TransmissionValues); else return GetOsdItem();
1820 case 9: ST(" T") return new cMenuEditMapItem( tr("Guard"), &dtp.guard, GuardValues); else return GetOsdItem();
1821 case 10: ST(" T") return new cMenuEditMapItem( tr("Hierarchy"), &dtp.hierarchy, HierarchyValues); else return GetOsdItem();
1822 case 11: ST(" S ") return new cMenuEditMapItem( tr("Rolloff"), &dtp.rollOff, RollOffValues); else return GetOsdItem();
1823 case 12: ST(" ST") return new cMenuEditIntItem( tr("StreamId"), &dtp.streamId, 0, 255); else return GetOsdItem();
1824 case 13: ST(" S ") return new cMenuEditMapItem( tr("Pilot"), &dtp.pilot, PilotValues); else return GetOsdItem();
1825 case 14: ST(" T") return new cMenuEditIntItem( tr("T2SystemId"), &dtp.t2systemId, 0, 65535); else return GetOsdItem();
1826 case 15: ST(" T") return new cMenuEditIntItem( tr("SISO/MISO"), &dtp.sisoMiso, 0, 1); else return GetOsdItem();
1827 default: return NULL;
1828 }
1829 return NULL;
1830}
1831
1832// --- cDvbDevice ------------------------------------------------------------
1833
1834bool cDvbDevice::useDvbDevices = true;
1837
1838cDvbDevice::cDvbDevice(int Adapter, int Frontend)
1839{
1840 adapter = Adapter;
1842 ciAdapter = NULL;
1843 dvbTuner = NULL;
1844 bondedDevice = NULL;
1846 tsBuffer = NULL;
1847
1848 // Common Interface:
1849
1851 if (fd_ca >= 0)
1853 checkTsBuffer = false;
1854
1855 // The DVR device (will be opened and closed as needed):
1856
1857 fd_dvr = -1;
1858
1859 // We only check the devices that must be present - the others will be checked before accessing them://XXX
1860
1862
1863 dvbTuner = new cDvbTuner(this, adapter, frontend);
1864}
1865
1867{
1868 delete dvbTuner;
1869 delete ciAdapter;
1871 UnBond();
1872 // We're not explicitly closing any device files here, since this sometimes
1873 // caused segfaults. Besides, the program is about to terminate anyway...
1874}
1875
1876cString DvbName(const char *Name, int Adapter, int Frontend)
1877{
1878 return cString::sprintf("%s/%s%d/%s%d", DEV_DVB_BASE, DEV_DVB_ADAPTER, Adapter, Name, Frontend);
1879}
1880
1881int DvbOpen(const char *Name, int Adapter, int Frontend, int Mode, bool ReportError)
1882{
1883 cString FileName = DvbName(Name, Adapter, Frontend);
1884 int fd = open(FileName, Mode);
1885 if (fd < 0 && ReportError)
1886 LOG_ERROR_STR(*FileName);
1887 return fd;
1888}
1889
1891{
1892 return dvbTuner ? dvbTuner->Frontend() : frontend;
1893}
1894
1895bool cDvbDevice::Exists(int Adapter, int Frontend)
1896{
1898 if (access(FileName, F_OK) == 0) {
1899 int f = open(FileName, O_RDONLY);
1900 if (f >= 0) {
1901 close(f);
1902 return true;
1903 }
1904 else if (errno != ENODEV && errno != EINVAL)
1905 LOG_ERROR_STR(*FileName);
1906 }
1907 else if (errno != ENOENT)
1908 LOG_ERROR_STR(*FileName);
1909 return false;
1910}
1911
1912bool cDvbDevice::Probe(int Adapter, int Frontend)
1913{
1915 dsyslog("probing %s", *FileName);
1916 for (cDvbDeviceProbe *dp = DvbDeviceProbes.First(); dp; dp = DvbDeviceProbes.Next(dp)) {
1917 if (dp->Probe(Adapter, Frontend))
1918 return true; // a plugin has created the actual device
1919 }
1920 dsyslog("creating cDvbDevice");
1921 new cDvbDevice(Adapter, Frontend); // it's a "budget" device
1922 return true;
1923}
1924
1926{
1927 if (dvbTuner)
1929 return "";
1930}
1931
1933{
1934 if (dvbTuner)
1935 return dvbTuner->FrontendName();
1936 return "";
1937}
1938
1940{
1941 new cDvbSourceParam('A', "ATSC");
1942 new cDvbSourceParam('C', "DVB-C");
1943 new cDvbSourceParam('S', "DVB-S");
1944 new cDvbSourceParam('T', "DVB-T");
1945 cStringList Nodes;
1946 cReadDir DvbDir(DEV_DVB_BASE);
1947 if (DvbDir.Ok()) {
1948 struct dirent *a;
1949 while ((a = DvbDir.Next()) != NULL) {
1950 if (strstr(a->d_name, DEV_DVB_ADAPTER) == a->d_name) {
1951 int Adapter = strtol(a->d_name + strlen(DEV_DVB_ADAPTER), NULL, 10);
1952 cReadDir AdapterDir(AddDirectory(DEV_DVB_BASE, a->d_name));
1953 if (AdapterDir.Ok()) {
1954 struct dirent *f;
1955 while ((f = AdapterDir.Next()) != NULL) {
1956 if (strstr(f->d_name, DEV_DVB_FRONTEND) == f->d_name) {
1957 int Frontend = strtol(f->d_name + strlen(DEV_DVB_FRONTEND), NULL, 10);
1958 if (access(DvbName(DEV_DVB_DEMUX, Adapter, Frontend), F_OK) == 0) { // we only create devices for actual demuxes
1959 dsyslog("detected /dev/dvb/adapter%d/frontend%d", Adapter, Frontend);
1960 Nodes.Append(strdup(cString::sprintf("%2d %2d", Adapter, Frontend)));
1961 }
1962 }
1963 }
1964 }
1965 }
1966 }
1967 }
1968 int Found = 0;
1969 int Used = 0;
1970 if (Nodes.Size() > 0) {
1971 Nodes.Sort();
1972 for (int i = 0; i < Nodes.Size(); i++) {
1973 int Adapter;
1974 int Frontend;
1975 if (2 == sscanf(Nodes[i], "%d %d", &Adapter, &Frontend)) {
1976 if (Exists(Adapter, Frontend)) {
1977 if (Found < MAXDEVICES) {
1978 Found++;
1980 if (Probe(Adapter, Frontend))
1981 Used++;
1982 }
1983 else {
1984 dsyslog("skipped /dev/dvb/adapter%d/frontend%d", Adapter, Frontend);
1985 NextCardIndex(1); // skips this one
1986 }
1987 }
1988 }
1989 }
1990 }
1991 }
1992 if (Found > 0) {
1993 isyslog("found %d DVB device%s", Found, Found > 1 ? "s" : "");
1994 if (Used != Found)
1995 isyslog("using only %d DVB device%s", Used, Used != 1 ? "s" : "");
1996 }
1997 else
1998 isyslog("no DVB device found");
1999 return Found > 0;
2000}
2001
2002bool cDvbDevice::BondDevices(const char *Bondings)
2003{
2004 UnBondDevices();
2005 if (Bondings) {
2006 cSatCableNumbers SatCableNumbers(MAXDEVICES, Bondings);
2007 for (int i = 0; i < cDevice::NumDevices(); i++) {
2008 int d = SatCableNumbers.FirstDeviceIndex(i);
2009 if (d >= 0) {
2010 int ErrorDevice = 0;
2011 if (cDevice *Device1 = cDevice::GetDevice(i)) {
2012 if (cDevice *Device2 = cDevice::GetDevice(d)) {
2013 if (cDvbDevice *DvbDevice1 = dynamic_cast<cDvbDevice *>(Device1)) {
2014 if (cDvbDevice *DvbDevice2 = dynamic_cast<cDvbDevice *>(Device2)) {
2015 if (!DvbDevice1->Bond(DvbDevice2))
2016 return false; // Bond() has already logged the error
2017 }
2018 else
2019 ErrorDevice = d + 1;
2020 }
2021 else
2022 ErrorDevice = i + 1;
2023 if (ErrorDevice) {
2024 esyslog("ERROR: device '%d' in device bondings '%s' is not a cDvbDevice", ErrorDevice, Bondings);
2025 return false;
2026 }
2027 }
2028 else
2029 ErrorDevice = d + 1;
2030 }
2031 else
2032 ErrorDevice = i + 1;
2033 if (ErrorDevice) {
2034 esyslog("ERROR: unknown device '%d' in device bondings '%s'", ErrorDevice, Bondings);
2035 return false;
2036 }
2037 }
2038 }
2039 }
2040 return true;
2041}
2042
2044{
2045 for (int i = 0; i < cDevice::NumDevices(); i++) {
2046 if (cDvbDevice *d = dynamic_cast<cDvbDevice *>(cDevice::GetDevice(i)))
2047 d->UnBond();
2048 }
2049}
2050
2052{
2053 cMutexLock MutexLock(&bondMutex);
2054 if (!bondedDevice) {
2055 if (Device != this) {
2056 if ((ProvidesDeliverySystem(SYS_DVBS) || ProvidesDeliverySystem(SYS_DVBS2)) && (Device->ProvidesDeliverySystem(SYS_DVBS) || Device->ProvidesDeliverySystem(SYS_DVBS2))) {
2057 if (dvbTuner && Device->dvbTuner && dvbTuner->Bond(Device->dvbTuner)) {
2058 bondedDevice = Device->bondedDevice ? Device->bondedDevice : Device;
2059 Device->bondedDevice = this;
2060 dsyslog("device %d bonded with device %d", DeviceNumber() + 1, bondedDevice->DeviceNumber() + 1);
2061 return true;
2062 }
2063 }
2064 else
2065 esyslog("ERROR: can't bond device %d with device %d (only DVB-S(2) devices can be bonded)", DeviceNumber() + 1, Device->DeviceNumber() + 1);
2066 }
2067 else
2068 esyslog("ERROR: can't bond device %d with itself", DeviceNumber() + 1);
2069 }
2070 else
2071 esyslog("ERROR: device %d already bonded with device %d, can't bond with device %d", DeviceNumber() + 1, bondedDevice->DeviceNumber() + 1, Device->DeviceNumber() + 1);
2072 return false;
2073}
2074
2076{
2077 cMutexLock MutexLock(&bondMutex);
2078 if (cDvbDevice *d = bondedDevice) {
2079 if (dvbTuner)
2080 dvbTuner->UnBond();
2081 dsyslog("device %d unbonded from device %d", DeviceNumber() + 1, bondedDevice->DeviceNumber() + 1);
2082 while (d->bondedDevice != this)
2083 d = d->bondedDevice;
2084 if (d == bondedDevice)
2085 d->bondedDevice = NULL;
2086 else
2087 d->bondedDevice = bondedDevice;
2088 bondedDevice = NULL;
2089 }
2090}
2091
2092bool cDvbDevice::BondingOk(const cChannel *Channel, bool ConsiderOccupied) const
2093{
2094 cMutexLock MutexLock(&bondMutex);
2095 if (bondedDevice || Positioner())
2096 return dvbTuner && dvbTuner->BondingOk(Channel, ConsiderOccupied);
2097 return true;
2098}
2099
2101{
2102 return ciAdapter;
2103}
2104
2105bool cDvbDevice::SetPid(cPidHandle *Handle, int Type, bool On)
2106{
2107 if (Handle->pid) {
2108 dmx_pes_filter_params pesFilterParams;
2109 memset(&pesFilterParams, 0, sizeof(pesFilterParams));
2110 if (On) {
2111 if (Handle->handle < 0) {
2112 Handle->handle = DvbOpen(DEV_DVB_DEMUX, adapter, frontend, O_RDWR | O_NONBLOCK, true);
2113 if (Handle->handle < 0) {
2114 LOG_ERROR;
2115 return false;
2116 }
2117 }
2118 pesFilterParams.pid = Handle->pid;
2119 pesFilterParams.input = DMX_IN_FRONTEND;
2120 pesFilterParams.output = DMX_OUT_TS_TAP;
2121 pesFilterParams.pes_type= DMX_PES_OTHER;
2122 pesFilterParams.flags = DMX_IMMEDIATE_START;
2123 if (ioctl(Handle->handle, DMX_SET_PES_FILTER, &pesFilterParams) < 0) {
2124 LOG_ERROR;
2125 return false;
2126 }
2127 }
2128 else if (!Handle->used) {
2129 CHECK(ioctl(Handle->handle, DMX_STOP));
2130 if (Type <= ptTeletext) {
2131 pesFilterParams.pid = 0x1FFF;
2132 pesFilterParams.input = DMX_IN_FRONTEND;
2133 pesFilterParams.output = DMX_OUT_DECODER;
2134 pesFilterParams.pes_type= DMX_PES_OTHER;
2135 pesFilterParams.flags = DMX_IMMEDIATE_START;
2136 CHECK(ioctl(Handle->handle, DMX_SET_PES_FILTER, &pesFilterParams));
2137 }
2138 close(Handle->handle);
2139 Handle->handle = -1;
2140 }
2141 }
2142 return true;
2143}
2144
2145#define RB_NUM_SECTIONS 8 // default: 2 sections = 8192 bytes
2146
2147int cDvbDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask)
2148{
2150 int f = open(FileName, O_RDWR | O_NONBLOCK);
2151 if (f >= 0) {
2152 if (Pid == EITPID) { // increase ringbuffer size for EIT
2153 if (ioctl(f, DMX_SET_BUFFER_SIZE, MAX_SECTION_SIZE * RB_NUM_SECTIONS) < 0)
2154 dsyslog("OpenFilter (pid=%d, tid=%02X): ioctl DMX_SET_BUFFER_SIZE failed", Pid, Tid);
2155 }
2156 dmx_sct_filter_params sctFilterParams;
2157 memset(&sctFilterParams, 0, sizeof(sctFilterParams));
2158 sctFilterParams.pid = Pid;
2159 sctFilterParams.timeout = 0;
2160 sctFilterParams.flags = DMX_IMMEDIATE_START;
2161 sctFilterParams.filter.filter[0] = Tid;
2162 sctFilterParams.filter.mask[0] = Mask;
2163 if (ioctl(f, DMX_SET_FILTER, &sctFilterParams) >= 0)
2164 return f;
2165 else {
2166 esyslog("ERROR: can't set filter (pid=%d, tid=%02X, mask=%02X): %m", Pid, Tid, Mask);
2167 close(f);
2168 }
2169 }
2170 else
2171 esyslog("ERROR: can't open filter handle on '%s'", *FileName);
2172 return -1;
2173}
2174
2176{
2177 close(Handle);
2178}
2179
2180bool cDvbDevice::ProvidesDeliverySystem(int DeliverySystem) const
2181{
2182 return dvbTuner->ProvidesDeliverySystem(DeliverySystem);
2183}
2184
2185bool cDvbDevice::ProvidesSource(int Source) const
2186{
2187 int type = Source & cSource::st_Mask;
2188 return type == cSource::stNone
2189 || type == cSource::stAtsc && ProvidesDeliverySystem(SYS_ATSC)
2190 || type == cSource::stCable && (ProvidesDeliverySystem(SYS_DVBC_ANNEX_AC) || ProvidesDeliverySystem(SYS_DVBC_ANNEX_B))
2191 || type == cSource::stSat && (ProvidesDeliverySystem(SYS_DVBS) || ProvidesDeliverySystem(SYS_DVBS2))
2193}
2194
2196{
2197 if (!ProvidesSource(Channel->Source()))
2198 return false; // doesn't provide source
2199 if (!dvbTuner->ProvidesFrontend(Channel))
2200 return false; // requires modulation system which frontend doesn't provide
2201 cDvbTransponderParameters dtp(Channel->Parameters());
2202 if (!cSource::IsSat(Channel->Source()) ||
2203 (!Setup.DiSEqC || Diseqcs.Get(DeviceNumber() + 1, Channel->Source(), Channel->Frequency(), dtp.Polarization(), NULL)))
2204 return DeviceHooksProvidesTransponder(Channel);
2205 return false;
2206}
2207
2208bool cDvbDevice::ProvidesChannel(const cChannel *Channel, int Priority, bool *NeedsDetachReceivers) const
2209{
2210 bool result = false;
2211 bool hasPriority = Priority == IDLEPRIORITY || Priority > this->Priority();
2212 bool needsDetachReceivers = false;
2214
2215 if (ProvidesTransponder(Channel)) {
2216 result = hasPriority;
2217 if (Priority > IDLEPRIORITY) {
2218 if (Receiving()) {
2219 if (dvbTuner->IsTunedTo(Channel)) {
2220 if (Channel->Vpid() && !HasPid(Channel->Vpid()) || Channel->Apid(0) && !HasPid(Channel->Apid(0)) || Channel->Dpid(0) && !HasPid(Channel->Dpid(0))) {
2221 if (CamSlot() && Channel->Ca() >= CA_ENCRYPTED_MIN) {
2222 if (CamSlot()->CanDecrypt(Channel))
2223 result = true;
2224 else
2225 needsDetachReceivers = true;
2226 }
2227 else
2228 result = true;
2229 }
2230 else
2231 result = true;
2232 }
2233 else
2234 needsDetachReceivers = Receiving();
2235 }
2236 if (result) {
2237 cMutexLock MutexLock(&bondMutex);
2238 if (!BondingOk(Channel)) {
2239 // This device is bonded, so we need to check the priorities of the others:
2240 for (cDvbDevice *d = bondedDevice; d && d != this; d = d->bondedDevice) {
2241 if (d->Priority() >= Priority) {
2242 result = false;
2243 break;
2244 }
2245 needsDetachReceivers |= d->Receiving();
2246 }
2248 needsDetachReceivers |= Receiving();
2249 }
2250 }
2251 }
2252 }
2253 if (NeedsDetachReceivers)
2254 *NeedsDetachReceivers = needsDetachReceivers;
2255 return result;
2256}
2257
2259{
2260 return dvbTuner != NULL && DeviceHooksProvidesEIT();
2261}
2262
2264{
2265 return dvbTuner ? dvbTuner->NumProvidedSystems() : 0;
2266}
2267
2269{
2270 return dvbTuner ? dvbTuner->Positioner() : NULL;
2271}
2272
2273bool cDvbDevice::SignalStats(int &Valid, double *Strength, double *Cnr, double *BerPre, double *BerPost, double *Per, int *Status) const
2274{
2275 return dvbTuner ? dvbTuner->GetSignalStats(Valid, Strength, Cnr, BerPre, BerPost, Per, Status) : false;
2276}
2277
2279{
2280 return dvbTuner ? dvbTuner->GetSignalStrength() : -1;
2281}
2282
2284{
2285 return dvbTuner ? dvbTuner->GetSignalQuality() : -1;
2286}
2287
2289{
2290 return dvbTuner ? dvbTuner->GetTransponder() : NULL;
2291}
2292
2294{
2295 return dvbTuner ? dvbTuner->IsTunedTo(Channel) : false;
2296}
2297
2299{
2300 return BondingOk(Channel, true) && cDevice::MaySwitchTransponder(Channel);
2301}
2302
2303bool cDvbDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
2304{
2305 if (dvbTuner->ProvidesFrontend(Channel, true)) {
2306 dvbTuner->SetChannel(Channel);
2307 return true;
2308 }
2309 return false;
2310}
2311
2312bool cDvbDevice::HasLock(int TimeoutMs) const
2313{
2314 return dvbTuner ? dvbTuner->Locked(TimeoutMs) : false;
2315}
2316
2318{
2320}
2321
2323{
2324 CloseDvr();
2325 fd_dvr = DvbOpen(DEV_DVB_DVR, adapter, frontend, O_RDONLY | O_NONBLOCK, true);
2326 if (fd_dvr >= 0)
2328 return fd_dvr >= 0;
2329}
2330
2332{
2333 if (fd_dvr >= 0) {
2334 delete tsBuffer;
2335 tsBuffer = NULL;
2336 close(fd_dvr);
2337 fd_dvr = -1;
2338 }
2339}
2340
2342{
2343 if (tsBuffer) {
2344 if (cCamSlot *cs = CamSlot()) {
2345 if (cs->WantsTsData()) {
2346 int Available;
2347 Data = tsBuffer->Get(&Available, checkTsBuffer);
2348 if (!Data)
2349 Available = 0;
2350 Data = cs->Decrypt(Data, Available);
2351 tsBuffer->Skip(Available);
2352 checkTsBuffer = Data != NULL;
2353 return true;
2354 }
2355 }
2356 Data = tsBuffer->Get();
2357 return true;
2358 }
2359 return false;
2360}
2361
2363{
2364 cMutexLock MutexLock(&bondMutex);
2365 cDvbDevice *d = this;
2366 do {
2367 d->cDevice::DetachAllReceivers();
2368 d = d->bondedDevice;
2369 } while (d && d != this && needsDetachBondedReceivers);
2371}
2372
2373// --- cDvbDeviceProbe -------------------------------------------------------
2374
2376
2378{
2379 DvbDeviceProbes.Add(this);
2380}
2381
2383{
2384 DvbDeviceProbes.Del(this, false);
2385}
2386
2387uint32_t cDvbDeviceProbe::GetSubsystemId(int Adapter, int Frontend)
2388{
2389 uint32_t SubsystemId = 0;
2390 cString FileName = cString::sprintf("/dev/dvb/adapter%d/frontend%d", Adapter, Frontend);
2391 struct stat st;
2392 if (stat(FileName, &st) == 0) {
2393 cReadDir d("/sys/class/dvb");
2394 if (d.Ok()) {
2395 struct dirent *e;
2396 while ((e = d.Next()) != NULL) {
2397 if (strstr(e->d_name, "frontend")) {
2398 FileName = cString::sprintf("/sys/class/dvb/%s/dev", e->d_name);
2399 if (FILE *f = fopen(FileName, "r")) {
2400 cReadLine ReadLine;
2401 char *s = ReadLine.Read(f);
2402 fclose(f);
2403 unsigned Major;
2404 unsigned Minor;
2405 if (s && 2 == sscanf(s, "%u:%u", &Major, &Minor)) {
2406 if (((Major << 8) | Minor) == st.st_rdev) {
2407 FileName = cString::sprintf("/sys/class/dvb/%s/device/subsystem_vendor", e->d_name);
2408 if ((f = fopen(FileName, "r")) != NULL) {
2409 if (char *s = ReadLine.Read(f))
2410 SubsystemId = strtoul(s, NULL, 0) << 16;
2411 fclose(f);
2412 }
2413 else {
2414 FileName = cString::sprintf("/sys/class/dvb/%s/device/idVendor", e->d_name);
2415 if ((f = fopen(FileName, "r")) != NULL) {
2416 if (char *s = ReadLine.Read(f))
2417 SubsystemId = strtoul(s, NULL, 16) << 16;
2418 fclose(f);
2419 }
2420 }
2421 FileName = cString::sprintf("/sys/class/dvb/%s/device/subsystem_device", e->d_name);
2422 if ((f = fopen(FileName, "r")) != NULL) {
2423 if (char *s = ReadLine.Read(f))
2424 SubsystemId |= strtoul(s, NULL, 0);
2425 fclose(f);
2426 }
2427 else {
2428 FileName = cString::sprintf("/sys/class/dvb/%s/device/idProduct", e->d_name);
2429 if ((f = fopen(FileName, "r")) != NULL) {
2430 if (char *s = ReadLine.Read(f))
2431 SubsystemId |= strtoul(s, NULL, 16);
2432 fclose(f);
2433 }
2434 }
2435 break;
2436 }
2437 }
2438 }
2439 }
2440 }
2441 }
2442 }
2443 return SubsystemId;
2444}
#define CA_ENCRYPTED_MIN
Definition: channels.h:44
Definition: ci.h:232
int Vpid(void) const
Definition: channels.h:153
int Source(void) const
Definition: channels.h:151
int Number(void) const
Definition: channels.h:178
const char * Name(void) const
Definition: channels.c:107
int Dpid(int i) const
Definition: channels.h:160
int Apid(int i) const
Definition: channels.h:159
const char * Parameters(void) const
Definition: channels.h:181
int Ca(int Index=0) const
Definition: channels.h:172
int Frequency(void) const
Returns the actual frequency, as given in 'channels.conf'.
Definition: channels.h:148
bool IsSat(void) const
Definition: channels.h:187
bool IsCable(void) const
Definition: channels.h:186
bool SetTransponderData(int Source, int Frequency, int Srate, const char *Parameters, bool Quiet=false)
Definition: channels.c:180
bool IsTerr(void) const
Definition: channels.h:188
int Transponder(void) const
Returns the transponder frequency in MHz, plus the polarization in case of sat.
Definition: channels.c:146
int Srate(void) const
Definition: channels.h:152
bool IsAtsc(void) const
Definition: channels.h:185
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition: thread.c:132
void Broadcast(void)
Definition: thread.c:150
static int NextCardIndex(int n=0)
Calculates the next card index.
Definition: device.c:153
void StopSectionHandler(void)
A device that has called StartSectionHandler() must call this function (typically in its destructor) ...
Definition: device.c:673
static bool UseDevice(int n)
Tells whether the device with the given card index shall be used in this instance of VDR.
Definition: device.h:141
bool IsPrimaryDevice(void) const
Definition: device.h:220
void StartSectionHandler(void)
A derived device that provides section data must call this function (typically in its constructor) to...
Definition: device.c:662
@ ptTeletext
Definition: device.h:398
bool HasPid(int Pid) const
Returns true if this device is currently receiving the given PID.
Definition: device.c:542
static cDevice * PrimaryDevice(void)
Returns the primary device.
Definition: device.h:148
virtual bool MaySwitchTransponder(const cChannel *Channel) const
Returns true if it is ok to switch to the Channel's transponder on this device, without disturbing an...
Definition: device.c:802
static cDevice * GetDevice(int Index)
Gets the device with the given Index.
Definition: device.c:228
bool DeviceHooksProvidesEIT(void) const
Definition: device.c:732
int DeviceNumber(void) const
Returns the number of this device (0 ... numDevices - 1).
Definition: device.c:165
bool Receiving(bool Dummy=false) const
Returns true if we are currently receiving. The parameter has no meaning (for backwards compatibility...
Definition: device.c:1678
const cSdtFilter * SdtFilter(void) const
Definition: device.h:461
cSectionHandler * SectionHandler(void) const
Definition: device.h:462
static int NumDevices(void)
Returns the total number of devices.
Definition: device.h:129
int Priority(void) const
Returns the priority of the current receiving session (-MAXPRIORITY..MAXPRIORITY),...
Definition: device.c:1660
cCamSlot * CamSlot(void) const
Returns the CAM slot that is currently used with this device, or NULL if no CAM slot is in use.
Definition: device.h:481
void DelLivePids(void)
Deletes the live viewing PIDs.
Definition: device.c:653
bool DeviceHooksProvidesTransponder(const cChannel *Channel) const
Definition: device.c:721
Definition: diseqc.h:62
eDiseqcActions Execute(const char **CurrentAction, uchar *Codes, uint8_t *MaxCodes, const cScr *Scr, int *Frequency) const
Parses the DiSEqC commands and returns the appropriate action code with every call.
Definition: diseqc.c:402
int Position(void) const
Indicates which positioning mode to use in order to move the dish to a given satellite position.
Definition: diseqc.h:126
bool IsScr(void) const
Returns true if this DiSEqC sequence uses Satellite Channel Routing.
Definition: diseqc.h:132
eDiseqcActions
Definition: diseqc.h:64
@ daMiniB
Definition: diseqc.h:71
@ daNone
Definition: diseqc.h:65
@ daPositionN
Definition: diseqc.h:72
@ daMiniA
Definition: diseqc.h:70
@ daCodes
Definition: diseqc.h:75
@ daVoltage13
Definition: diseqc.h:68
@ daWait
Definition: diseqc.h:76
@ daToneOff
Definition: diseqc.h:66
@ daToneOn
Definition: diseqc.h:67
@ daVoltage18
Definition: diseqc.h:69
@ daPositionA
Definition: diseqc.h:73
@ daScr
Definition: diseqc.h:74
const cDiseqc * Get(int Device, int Source, int Frequency, char Polarization, const cScr **Scr) const
Selects a DiSEqC entry suitable for the given Device and tuning parameters.
Definition: diseqc.c:447
static cDvbCiAdapter * CreateCiAdapter(cDevice *Device, int Fd)
Definition: dvbci.c:102
cDvbDeviceProbe(void)
Definition: dvbdevice.c:2377
static uint32_t GetSubsystemId(int Adapter, int Frontend)
Definition: dvbdevice.c:2387
virtual ~cDvbDeviceProbe()
Definition: dvbdevice.c:2382
The cDvbDevice implements a DVB device which can be accessed through the Linux DVB driver API.
Definition: dvbdevice.h:168
virtual cString DeviceType(void) const
Returns a string identifying the type of this device (like "DVB-S").
Definition: dvbdevice.c:1925
virtual int OpenFilter(u_short Pid, u_char Tid, u_char Mask)
Opens a file handle for the given filter data.
Definition: dvbdevice.c:2147
static void UnBondDevices(void)
Unbonds all devices.
Definition: dvbdevice.c:2043
virtual bool IsTunedToTransponder(const cChannel *Channel) const
Returns true if this device is currently tuned to the given Channel's transponder.
Definition: dvbdevice.c:2293
static void SetTransferModeForDolbyDigital(int Mode)
Definition: dvbdevice.c:2317
cDvbDevice(int Adapter, int Frontend)
Definition: dvbdevice.c:1838
virtual int SignalStrength(void) const
Returns the "strength" of the currently received signal.
Definition: dvbdevice.c:2278
virtual bool HasLock(int TimeoutMs=0) const
Returns true if the device has a lock on the requested transponder.
Definition: dvbdevice.c:2312
virtual void CloseFilter(int Handle)
Closes a file handle that has previously been opened by OpenFilter().
Definition: dvbdevice.c:2175
static bool Exists(int Adapter, int Frontend)
Checks whether the given adapter/frontend exists.
Definition: dvbdevice.c:1895
virtual bool SetPid(cPidHandle *Handle, int Type, bool On)
Does the actual PID setting on this device.
Definition: dvbdevice.c:2105
static bool BondDevices(const char *Bondings)
Bonds the devices as defined in the given Bondings string.
Definition: dvbdevice.c:2002
static bool useDvbDevices
Definition: dvbdevice.h:175
virtual int NumProvidedSystems(void) const
Returns the number of individual "delivery systems" this device provides.
Definition: dvbdevice.c:2263
virtual void DetachAllReceivers(void)
Detaches all receivers from this device.
Definition: dvbdevice.c:2362
bool checkTsBuffer
Definition: dvbdevice.h:185
virtual bool ProvidesEIT(void) const
Returns true if this device provides EIT data and thus wants to be tuned to the channels it can recei...
Definition: dvbdevice.c:2258
cCiAdapter * ciAdapter
Definition: dvbdevice.h:227
virtual bool ProvidesTransponder(const cChannel *Channel) const
Returns true if this device can provide the transponder of the given Channel (which implies that it c...
Definition: dvbdevice.c:2195
cDvbTuner * dvbTuner
Definition: dvbdevice.h:232
virtual bool MaySwitchTransponder(const cChannel *Channel) const
Returns true if it is ok to switch to the Channel's transponder on this device, without disturbing an...
Definition: dvbdevice.c:2298
virtual bool OpenDvr(void)
Opens the DVR of this device and prepares it to deliver a Transport Stream for use in a cReceiver.
Definition: dvbdevice.c:2322
virtual void CloseDvr(void)
Shuts down the DVR.
Definition: dvbdevice.c:2331
static bool Initialize(void)
Initializes the DVB devices.
Definition: dvbdevice.c:1939
void UnBond(void)
Removes this device from any bonding it might have with other devices.
Definition: dvbdevice.c:2075
int adapter
Definition: dvbdevice.h:181
virtual bool SetChannelDevice(const cChannel *Channel, bool LiveView)
Sets the device to the given channel (actual physical setup).
Definition: dvbdevice.c:2303
static cMutex bondMutex
Definition: dvbdevice.h:186
bool BondingOk(const cChannel *Channel, bool ConsiderOccupied=false) const
Returns true if this device is either not bonded to any other device, or the given Channel is on the ...
Definition: dvbdevice.c:2092
cTSBuffer * tsBuffer
< Controls how the DVB device handles Transfer Mode when replaying Dolby Digital audio.
Definition: dvbdevice.h:283
bool needsDetachBondedReceivers
Definition: dvbdevice.h:188
virtual ~cDvbDevice()
Definition: dvbdevice.c:1866
virtual bool SignalStats(int &Valid, double *Strength=NULL, double *Cnr=NULL, double *BerPre=NULL, double *BerPost=NULL, double *Per=NULL, int *Status=NULL) const
Returns statistics about the currently received signal (if available).
Definition: dvbdevice.c:2273
static int setTransferModeForDolbyDigital
Definition: dvbdevice.h:271
virtual cString DeviceName(void) const
Returns a string identifying the name of this device.
Definition: dvbdevice.c:1932
int Frontend(void) const
Definition: dvbdevice.c:1890
virtual bool GetTSPacket(uchar *&Data)
Gets exactly one TS packet from the DVR of this device and returns a pointer to it in Data.
Definition: dvbdevice.c:2341
virtual const cChannel * GetCurrentlyTunedTransponder(void) const
Returns a pointer to the currently tuned transponder.
Definition: dvbdevice.c:2288
bool Bond(cDvbDevice *Device)
Bonds this device with the given Device, making both of them use the same satellite cable and LNB.
Definition: dvbdevice.c:2051
virtual bool ProvidesDeliverySystem(int DeliverySystem) const
Definition: dvbdevice.c:2180
virtual bool ProvidesChannel(const cChannel *Channel, int Priority=IDLEPRIORITY, bool *NeedsDetachReceivers=NULL) const
Returns true if this device can provide the given channel.
Definition: dvbdevice.c:2208
virtual bool ProvidesSource(int Source) const
Returns true if this device can provide the given source.
Definition: dvbdevice.c:2185
virtual bool HasCi(void)
Returns true if this device has a Common Interface.
Definition: dvbdevice.c:2100
cDvbDevice * bondedDevice
Definition: dvbdevice.h:187
int frontend
Definition: dvbdevice.h:181
int fd_dvr
Definition: dvbdevice.h:184
virtual int SignalQuality(void) const
Returns the "quality" of the currently received signal.
Definition: dvbdevice.c:2283
static bool Probe(int Adapter, int Frontend)
Probes for existing DVB devices.
Definition: dvbdevice.c:1912
int Adapter(void) const
Definition: dvbdevice.h:192
virtual const cPositioner * Positioner(void) const
Returns a pointer to the positioner (if any) this device has used to move the satellite dish to the r...
Definition: dvbdevice.c:2268
int Open(void)
Definition: dvbdevice.c:386
uint32_t subsystemId
Definition: dvbdevice.c:349
int fd_frontend
Definition: dvbdevice.c:348
bool ProvidesModulation(int System, int StreamId, int Modulation) const
Definition: dvbdevice.c:411
uint32_t SubsystemId(void) const
Definition: dvbdevice.c:364
int NumDeliverySystems(void) const
Definition: dvbdevice.c:362
const char * FrontendName(void)
Definition: dvbdevice.c:359
cDvbFrontend(int Adapter, int Frontend)
Definition: dvbdevice.c:367
int NumModulations(void) const
Definition: dvbdevice.c:363
bool ProvidesDeliverySystem(int DeliverySystem) const
Definition: dvbdevice.c:402
int numModulations
Definition: dvbdevice.c:352
void Close(void)
Definition: dvbdevice.c:393
cVector< int > deliverySystems
Definition: dvbdevice.c:351
bool QueryDeliverySystems(void)
Definition: dvbdevice.c:429
dvb_frontend_info frontendInfo
Definition: dvbdevice.c:350
cDvbSourceParam(char Source, const char *Description)
Definition: dvbdevice.c:1785
cDvbTransponderParameters dtp
Definition: dvbdevice.c:1777
virtual void SetData(cChannel *Channel)
Sets all source specific parameters to those of the given Channel.
Definition: dvbdevice.c:1792
virtual cOsdItem * GetOsdItem(void)
Returns all the OSD items necessary for editing the source specific parameters of the channel that wa...
Definition: dvbdevice.c:1804
virtual void GetData(cChannel *Channel)
Copies all source specific parameters to the given Channel.
Definition: dvbdevice.c:1799
int StreamId(void) const
Definition: dvbdevice.h:138
cString ToString(char Type) const
Definition: dvbdevice.c:217
const char * ParseParameter(const char *s, int &Value, const tDvbParameterMap *Map=NULL)
Definition: dvbdevice.c:241
char Polarization(void) const
Definition: dvbdevice.h:127
int Guard(void) const
Definition: dvbdevice.h:135
bool Parse(const char *s)
Definition: dvbdevice.c:257
int Pilot(void) const
Definition: dvbdevice.h:141
int PrintParameter(char *p, char Name, int Value) const
Definition: dvbdevice.c:212
int RollOff(void) const
Definition: dvbdevice.h:137
int CoderateL(void) const
Definition: dvbdevice.h:131
int Transmission(void) const
Definition: dvbdevice.h:134
int Bandwidth(void) const
Definition: dvbdevice.h:129
int System(void) const
Definition: dvbdevice.h:133
int Inversion(void) const
Definition: dvbdevice.h:128
cDvbTransponderParameters(const char *Parameters=NULL)
Definition: dvbdevice.c:207
int Modulation(void) const
Definition: dvbdevice.h:132
int Hierarchy(void) const
Definition: dvbdevice.h:136
int CoderateH(void) const
Definition: dvbdevice.h:130
int diseqcOffset
Definition: dvbdevice.c:553
cPositioner * positioner
Definition: dvbdevice.c:555
void UnBond(void)
Definition: dvbdevice.c:725
int frontend
Definition: dvbdevice.c:540
void ExecuteDiseqc(const cDiseqc *Diseqc, int *Frequency)
Definition: dvbdevice.c:1480
time_t lastUncChange
Definition: dvbdevice.c:550
bool Bond(cDvbTuner *Tuner)
Definition: dvbdevice.c:709
cCondVar newSet
Definition: dvbdevice.c:561
int lockTimeout
Definition: dvbdevice.c:546
bool lnbPowerTurnedOn
Definition: dvbdevice.c:557
bool SetFrontend(void)
Definition: dvbdevice.c:1543
void ClearEventQueue(void) const
Definition: dvbdevice.c:850
cChannel channel
Definition: dvbdevice.c:551
eTunerStatus tunerStatus
Definition: dvbdevice.c:558
int NumProvidedSystems(void) const
Definition: dvbdevice.c:583
void SetChannel(const cChannel *Channel)
Definition: dvbdevice.c:805
cDvbTuner(const cDvbDevice *Device, int Adapter, int Frontend)
Definition: dvbdevice.c:600
bool BondingOk(const cChannel *Channel, bool ConsiderOccupied=false) const
Definition: dvbdevice.c:758
void ResetToneAndVoltage(void)
Definition: dvbdevice.c:1537
cPositioner * GetPositioner(void)
Definition: dvbdevice.c:1471
const cScr * scr
Definition: dvbdevice.c:556
cString GetBondingParams(const cChannel *Channel=NULL) const
Definition: dvbdevice.c:741
virtual void Action(void)
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
Definition: dvbdevice.c:1683
int frontendType
Definition: dvbdevice.c:536
bool Locked(int TimeoutMs=0)
Definition: dvbdevice.c:838
int GetSignalQuality(void) const
Definition: dvbdevice.c:1292
uint32_t lastUncValue
Definition: dvbdevice.c:548
int adapter
Definition: dvbdevice.c:539
bool ProvidesModulation(int System, int StreamId, int Modulation) const
Definition: dvbdevice.c:677
const cPositioner * Positioner(void) const
Definition: dvbdevice.c:592
uint32_t lastUncDelta
Definition: dvbdevice.c:549
cVector< cDvbFrontend * > dvbFrontends
Definition: dvbdevice.c:541
bool IsBondedMaster(void) const
Definition: dvbdevice.c:566
cDvbTuner * GetBondedMaster(void)
Definition: dvbdevice.c:774
int tuneTimeout
Definition: dvbdevice.c:545
uint32_t SubsystemId(void) const
Definition: dvbdevice.c:588
int Frontend(void) const
Definition: dvbdevice.c:580
bool GetSignalStats(int &Valid, double *Strength=NULL, double *Cnr=NULL, double *BerPre=NULL, double *BerPost=NULL, double *Per=NULL, int *Status=NULL) const
Definition: dvbdevice.c:877
bool bondedMaster
Definition: dvbdevice.c:563
int fd_frontend
Definition: dvbdevice.c:538
bool ProvidesFrontend(const cChannel *Channel, bool Activate=false) const
Definition: dvbdevice.c:686
int FrontendType(void) const
Definition: dvbdevice.c:581
int lastSource
Definition: dvbdevice.c:554
bool IsTunedTo(const cChannel *Channel) const
Definition: dvbdevice.c:795
@ tsPositioning
Definition: dvbdevice.c:535
cDvbFrontend * dvbFrontend
Definition: dvbdevice.c:542
static cMutex bondMutex
Definition: dvbdevice.c:534
const cDiseqc * lastDiseqc
Definition: dvbdevice.c:552
time_t lastTimeoutReport
Definition: dvbdevice.c:547
cMutex mutex
Definition: dvbdevice.c:559
const cDvbDevice * device
Definition: dvbdevice.c:537
int GetSignalStrength(void) const
Definition: dvbdevice.c:1227
bool ProvidesDeliverySystem(int DeliverySystem) const
Definition: dvbdevice.c:668
cCondVar locked
Definition: dvbdevice.c:560
cDvbTuner * bondedTuner
Definition: dvbdevice.c:562
bool GetFrontendStatus(fe_status_t &Status) const
Definition: dvbdevice.c:860
const cChannel * GetTransponder(void) const
Definition: dvbdevice.c:587
int numModulations
Definition: dvbdevice.c:544
int numDeliverySystems
Definition: dvbdevice.c:543
const char * FrontendName(void)
Definition: dvbdevice.c:582
virtual ~cDvbTuner()
Definition: dvbdevice.c:651
Definition: tools.h:641
Definition: thread.h:67
void Lock(void)
Definition: thread.c:222
void Unlock(void)
Definition: thread.c:228
Definition: tools.h:431
bool Poll(int TimeoutMs=0)
Definition: tools.c:1539
A steerable satellite dish generally points to the south on the northern hemisphere,...
Definition: positioner.h:31
static cPositioner * GetPositioner(void)
Returns a previously created positioner.
Definition: positioner.c:133
void SetFrontend(int Frontend)
This function is called whenever the positioner is connected to a DVB frontend.
Definition: positioner.h:89
virtual bool IsMoving(void) const
Returns true if the dish is currently moving as a result of a call to GotoPosition() or GotoAngle().
Definition: positioner.c:127
virtual void GotoPosition(uint Number, int Longitude)
Move the dish to the satellite position stored under the given Number.
Definition: positioner.c:100
virtual void GotoAngle(int Longitude)
Move the dish to the given angular position.
Definition: positioner.c:107
struct dirent * Next(void)
Definition: tools.c:1562
bool Ok(void)
Definition: tools.h:456
char * Read(FILE *f)
Definition: tools.c:1481
int FirstDeviceIndex(int DeviceIndex) const
Returns the first device index (starting at 0) that uses the same sat cable number as the device with...
Definition: config.c:116
Definition: diseqc.h:34
bool TransponderWrong(void) const
Definition: sdt.h:35
void SetStatus(bool On)
Definition: sections.c:147
int LnbSLOF
Definition: config.h:277
int LnbFrequLo
Definition: config.h:278
int DiSEqC
Definition: config.h:280
int LnbFrequHi
Definition: config.h:279
char Source(void) const
Definition: sourceparams.h:31
int Position(void)
Returns the orbital position of the satellite in case this is a DVB-S source (zero otherwise).
Definition: sources.h:35
static bool IsSat(int Code)
Definition: sources.h:57
@ st_Mask
Definition: sources.h:23
@ stCable
Definition: sources.h:20
@ stSat
Definition: sources.h:21
@ stNone
Definition: sources.h:18
@ stAtsc
Definition: sources.h:19
@ stTerr
Definition: sources.h:22
void Sort(bool IgnoreCase=false)
Definition: tools.h:853
Definition: tools.h:178
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition: tools.c:1149
Derived cDevice classes that can receive channels will have to provide Transport Stream (TS) packets ...
Definition: device.h:876
uchar * Get(int *Available=NULL, bool CheckAvailable=false)
Returns a pointer to the first TS packet in the buffer.
Definition: device.c:1926
void Skip(int Count)
If after a call to Get() more or less than TS_SIZE of the available data has been processed,...
Definition: device.c:1956
Definition: thread.h:79
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition: thread.c:304
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition: thread.c:267
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition: thread.h:101
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
Definition: thread.c:354
Definition: tools.h:401
void Set(int Ms=0)
Sets the timer.
Definition: tools.c:792
bool TimedOut(void) const
Definition: tools.c:797
Definition: tools.h:711
int Size(void) const
Definition: tools.h:764
virtual void Clear(void)
Definition: tools.h:815
virtual void Append(T Data)
Definition: tools.h:784
cSetup Setup
Definition: config.c:372
#define IDLEPRIORITY
Definition: config.h:47
#define DTV_STAT_HAS_NONE
Definition: device.h:112
#define DTV_STAT_HAS_VITERBI
Definition: device.h:115
#define DTV_STAT_VALID_STRENGTH
Definition: device.h:105
#define DTV_STAT_VALID_BERPRE
Definition: device.h:107
#define DTV_STAT_VALID_NONE
The cDevice class is the base from which actual devices can be derived.
Definition: device.h:104
#define DTV_STAT_VALID_PER
Definition: device.h:109
#define MAXDEVICES
Definition: device.h:29
#define DTV_STAT_HAS_CARRIER
Definition: device.h:114
#define DTV_STAT_VALID_CNR
Definition: device.h:106
#define DTV_STAT_HAS_LOCK
Definition: device.h:117
#define DTV_STAT_VALID_BERPOST
Definition: device.h:108
#define DTV_STAT_HAS_SYNC
Definition: device.h:116
#define DTV_STAT_HAS_SIGNAL
Definition: device.h:113
#define DTV_STAT_VALID_STATUS
Definition: device.h:110
cDiseqcs Diseqcs
Definition: diseqc.c:439
int DvbOpen(const char *Name, int Adapter, int Frontend, int Mode, bool ReportError)
Definition: dvbdevice.c:1881
int SignalToSQI(const cChannel *Channel, int Signal, int Ber, int FeModulation, int FeCoderateH, int FeFec)
Definition: dvbdevice.c:1085
static const int DeliverySystemNamesMax
Definition: dvbdevice.c:327
const tDvbParameterMap CoderateValues[]
Definition: dvbdevice.c:68
int dB1000toPercent(int dB1000, int Low, int High)
Definition: dvbdevice.c:987
#define DVB_SYSTEM_1
Definition: dvbdevice.c:101
const tDvbParameterMap PilotValues[]
Definition: dvbdevice.c:43
int StrengthToSSI(const cChannel *Channel, int Strength, int FeModulation, int FeCoderateH, int FeFec)
Definition: dvbdevice.c:1008
#define REF_T2(q1, q2, q3, q4)
Definition: dvbdevice.c:1005
const tDvbParameterMap GuardValues[]
Definition: dvbdevice.c:127
#define DVBC_LOCK_TIMEOUT
Definition: dvbdevice.c:31
const tDvbParameterMap SystemValuesSat[]
Definition: dvbdevice.c:104
#define SETCMD(c, d)
Definition: dvbdevice.c:337
const tDvbParameterMap HierarchyValues[]
Definition: dvbdevice.c:139
#define DVBC_TUNE_TIMEOUT
Definition: dvbdevice.c:30
int DriverIndex(int Value, const tDvbParameterMap *Map)
Definition: dvbdevice.c:167
#define REF_C1(q1, q2, q3, q4, q5)
Definition: dvbdevice.c:1006
cString DvbName(const char *Name, int Adapter, int Frontend)
Definition: dvbdevice.c:1876
#define SCR_RANDOM_TIMEOUT
Definition: dvbdevice.c:37
#define RB_NUM_SECTIONS
Definition: dvbdevice.c:2145
#define ST(s)
#define BANDWIDTH_HZ_AUTO
Definition: dvbdevice.c:26
#define REF_S2(q1, q2, q3, q4)
Definition: dvbdevice.c:1003
#define TSBUFFERSIZE
Definition: dvbdevice.c:39
const tDvbParameterMap BandwidthValues[]
Definition: dvbdevice.c:57
#define DVB_SYSTEM_2
Definition: dvbdevice.c:102
static const char * GetDeliverySystemName(int Index)
Definition: dvbdevice.c:329
#define TUNER_POLL_TIMEOUT
Definition: dvbdevice.c:514
#define LOCK_THRESHOLD
Definition: dvbdevice.c:1290
cList< cDvbDeviceProbe > DvbDeviceProbes
Definition: dvbdevice.c:2375
#define MAXFRONTENDCMDS
Definition: dvbdevice.c:336
const tDvbParameterMap TransmissionValues[]
Definition: dvbdevice.c:116
const tDvbParameterMap InversionValues[]
Definition: dvbdevice.c:50
static unsigned int FrequencyToHz(unsigned int f)
Definition: dvbdevice.c:1464
#define ATSC_TUNE_TIMEOUT
Definition: dvbdevice.c:34
const tDvbParameterMap RollOffValues[]
Definition: dvbdevice.c:148
const tDvbParameterMap ModulationValues[]
Definition: dvbdevice.c:84
const tDvbParameterMap SystemValuesTerr[]
Definition: dvbdevice.c:110
#define REF_S1(q1)
Definition: dvbdevice.c:1002
#define DVBT_TUNE_TIMEOUT
Definition: dvbdevice.c:32
int UserIndex(int Value, const tDvbParameterMap *Map)
Definition: dvbdevice.c:156
#define REF_T1(q1, q2, q3)
Definition: dvbdevice.c:1004
static int GetRequiredDeliverySystem(const cChannel *Channel, const cDvbTransponderParameters *Dtp)
Definition: dvbdevice.c:516
static int DvbApiVersion
Definition: dvbdevice.c:24
int MapToUser(int Value, const tDvbParameterMap *Map, const char **String)
Definition: dvbdevice.c:178
const char * MapToUserString(int Value, const tDvbParameterMap *Map)
Definition: dvbdevice.c:189
#define ATSC_LOCK_TIMEOUT
Definition: dvbdevice.c:35
#define DVBS_LOCK_TIMEOUT
Definition: dvbdevice.c:29
const char * DeliverySystemNames[]
Definition: dvbdevice.c:303
int MapToDriver(int Value, const tDvbParameterMap *Map)
Definition: dvbdevice.c:197
#define BER_ERROR_FREE
Definition: dvbdevice.c:1083
#define DVBS_TUNE_TIMEOUT
Definition: dvbdevice.c:28
#define DVBT_LOCK_TIMEOUT
Definition: dvbdevice.c:33
@ SYS_DVBT2
Definition: dvbdevice.h:52
#define DEV_DVB_BASE
Definition: dvbdevice.h:71
#define DEV_DVB_DVR
Definition: dvbdevice.h:75
const tDvbParameterMap CoderateValues[]
Definition: dvbdevice.c:68
const tDvbParameterMap PilotValues[]
Definition: dvbdevice.c:43
const tDvbParameterMap GuardValues[]
Definition: dvbdevice.c:127
#define DEV_DVB_DEMUX
Definition: dvbdevice.h:76
const tDvbParameterMap SystemValuesSat[]
Definition: dvbdevice.c:104
int DvbOpen(const char *Name, int Adapter, int Frontend, int Mode, bool ReportError=false)
Definition: dvbdevice.c:1881
const tDvbParameterMap HierarchyValues[]
Definition: dvbdevice.c:139
@ TRANSMISSION_MODE_16K
Definition: dvbdevice.h:43
@ TRANSMISSION_MODE_1K
Definition: dvbdevice.h:42
@ TRANSMISSION_MODE_32K
Definition: dvbdevice.h:44
#define DTV_ENUM_DELSYS
Definition: dvbdevice.h:57
@ GUARD_INTERVAL_19_128
Definition: dvbdevice.h:48
@ GUARD_INTERVAL_19_256
Definition: dvbdevice.h:49
@ GUARD_INTERVAL_1_128
Definition: dvbdevice.h:47
#define DEV_DVB_ADAPTER
Definition: dvbdevice.h:72
cString DvbName(const char *Name, int Adapter, int Frontend)
Definition: dvbdevice.c:1876
#define DTV_STREAM_ID
Definition: dvbdevice.h:64
#define DEV_DVB_CA
Definition: dvbdevice.h:79
@ FE_CAN_2G_MODULATION
Definition: dvbdevice.h:27
const tDvbParameterMap BandwidthValues[]
Definition: dvbdevice.c:57
int MapToUser(int Value, const tDvbParameterMap *Map, const char **String=NULL)
Definition: dvbdevice.c:178
cList< cDvbDeviceProbe > DvbDeviceProbes
Definition: dvbdevice.c:2375
const tDvbParameterMap TransmissionValues[]
Definition: dvbdevice.c:116
const tDvbParameterMap InversionValues[]
Definition: dvbdevice.c:50
@ TRANSMISSION_MODE_4K
Definition: dvbdevice.h:30
@ FE_CAN_MULTISTREAM
Definition: dvbdevice.h:62
const tDvbParameterMap RollOffValues[]
Definition: dvbdevice.c:148
const tDvbParameterMap ModulationValues[]
Definition: dvbdevice.c:84
#define DVBAPIVERSION
Definition: dvbdevice.h:17
@ FE_CAN_TURBO_FEC
Definition: dvbdevice.h:36
#define DTV_DVBT2_PLP_ID_LEGACY
Definition: dvbdevice.h:65
#define DEV_DVB_FRONTEND
Definition: dvbdevice.h:74
int MapToDriver(int Value, const tDvbParameterMap *Map)
Definition: dvbdevice.c:197
#define tr(s)
Definition: i18n.h:85
#define trNOOP(s)
Definition: i18n.h:88
#define MAX_SECTION_SIZE
Definition: remux.h:295
#define EITPID
Definition: remux.h:54
const char * userString
Definition: dvbdevice.h:84
char * strn0cpy(char *dest, const char *src, size_t n)
Definition: tools.c:131
cString AddDirectory(const char *DirName, const char *FileName)
Definition: tools.c:402
T constrain(T v, T l, T h)
Definition: tools.h:70
#define LOG_ERROR_STR(s)
Definition: tools.h:40
unsigned char uchar
Definition: tools.h:31
#define CHECK(s)
Definition: tools.h:51
#define dsyslog(a...)
Definition: tools.h:37
T min(T a, T b)
Definition: tools.h:63
#define esyslog(a...)
Definition: tools.h:35
#define LOG_ERROR
Definition: tools.h:39
#define isyslog(a...)
Definition: tools.h:36