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