MythTV  master
dtvconfparser.cpp
Go to the documentation of this file.
1 /*
2  * vim: set expandtab tabstop=4 shiftwidth=4:
3  *
4  * Original Project
5  * MythTV http://www.mythtv.org
6  *
7  * Author(s):
8  * John Pullan (john@pullan.org)
9  *
10  * Description:
11  * Collection of classes to provide dvb channel scanning
12  * functionallity
13  *
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
29  *
30  */
31 
32 // Qt headers
33 #include <QTextStream>
34 #include <QFile>
35 
36 // MythTV headers
37 #include "libmyth/mythcontext.h"
38 #include "libmythbase/mythdbcon.h"
40 #include "dtvconfparser.h"
41 #include "channelutil.h"
42 
43 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
44 #define PARSE_SKIP(VAR) do { \
45  if (it == tokens.end()) return false; \
46  ++it; } while(false)
47 
48 #define PARSE_CONF(VAR) do { \
49  if (it == tokens.end() || !(VAR).ParseConf(*it++)) \
50  return false; } while(false)
51 
52 #define PARSE_STR(VAR) do { \
53  if (it != tokens.end()) (VAR) = *it++; else return false; } while(false)
54 
55 #define PARSE_UINT(VAR) do { \
56  if (it != tokens.end()) \
57  (VAR) = (*it++).toUInt(); else return false; } while(false)
58 
59 #define PARSE_UINT_1000(VAR) do { \
60  if (it != tokens.end()) \
61  (VAR) = (*it++).toUInt() * 1000ULL; else return false; } while(false)
62 // NOLINTEND(cppcoreguidelines-macro-usage)
63 
64 
65 QString DTVChannelInfo::toString() const
66 {
67  return QString("%1 %2 %3 ").arg(m_name).arg(m_serviceid).arg(m_lcn);
68 }
69 
71 {
72  m_channels.clear();
73 
74  QFile file(m_filename);
75  if (!file.open(QIODevice::ReadOnly))
76  return ERROR_OPEN;
77 
78  bool ok = true;
79  QTextStream stream(&file);
80  QString line;
81  while (!stream.atEnd())
82  {
83  line = stream.readLine(); // line of text excluding '\n'
84  line = line.trimmed();
85  if (line.startsWith("#"))
86  continue;
87 
88 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
89  QStringList list = line.split(":", QString::SkipEmptyParts);
90 #else
91  QStringList list = line.split(":", Qt::SkipEmptyParts);
92 #endif
93 
94  if (list.empty())
95  continue;
96 
97  QString str = list[0];
98  int channelNo = -1;
99 
100  if ((str.length() >= 1) && (str.at(0) == '@'))
101  {
102  channelNo = str.mid(1).toInt();
103  line = stream.readLine();
104 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
105  list = line.split(":", QString::SkipEmptyParts);
106 #else
107  list = line.split(":", Qt::SkipEmptyParts);
108 #endif
109  }
110 
111  if (list.size() < 4)
112  continue;
113 
114  str = list[3];
115 
116  if ((str == "T") || (str == "C") || (str == "S"))
117  {
118  if (((m_type == OFDM) && (str == "T")) ||
119  ((m_type == QPSK || m_type == DVBS2) && (str == "S")) ||
120  ((m_type == QAM) && (str == "C")))
121  ok &= ParseVDR(list, channelNo);
122  }
123  else if (m_type == OFDM)
124  ok &= ParseConfOFDM(list);
125  else if (m_type == ATSC)
126  ok &= ParseConfATSC(list);
127  else if (m_type == QPSK || m_type == DVBS2)
128  ok &= ParseConfQPSK(list);
129  else if (m_type == QAM)
130  ok &= ParseConfQAM(list);
131  }
132  file.close();
133 
134  return (ok) ? OK : ERROR_PARSE;
135 }
136 
137 bool DTVConfParser::ParseConfOFDM(const QStringList &tokens)
138 {
139  DTVChannelInfo chan;
140  DTVMultiplex mux;
141 
142  QStringList::const_iterator it = tokens.begin();
143 
144  PARSE_SKIP(unknown);
145  PARSE_UINT(mux.m_frequency);
146  PARSE_CONF(mux.m_inversion);
147  PARSE_CONF(mux.m_bandwidth);
151  PARSE_CONF(mux.m_transMode);
153  PARSE_CONF(mux.m_hierarchy);
154  PARSE_SKIP(unknown);
155  PARSE_SKIP(unknown);
156  PARSE_UINT(chan.m_serviceid);
157 
158  AddChannel(mux, chan);
159 
160  return true;
161 }
162 
163 bool DTVConfParser::ParseConfATSC(const QStringList &tokens)
164 {
165  DTVChannelInfo chan;
166  DTVMultiplex mux;
167 
168  QStringList::const_iterator it = tokens.begin();
169 
170  PARSE_STR(chan.m_name);
171  PARSE_UINT(mux.m_frequency);
173  PARSE_SKIP(Ignore_Video_PID);
174  PARSE_SKIP(Ignore_Audio_PID);
175  PARSE_UINT(chan.m_serviceid);
176 
177  AddChannel(mux, chan);
178 
179  return true;
180 }
181 
182 bool DTVConfParser::ParseConfQAM(const QStringList &tokens)
183 {
184  DTVChannelInfo chan;
185  DTVMultiplex mux;
186 
187  QStringList::const_iterator it = tokens.begin();
188 
189  PARSE_SKIP(unknown);
190  PARSE_UINT(mux.m_frequency);
191  PARSE_CONF(mux.m_inversion);
193  PARSE_CONF(mux.m_fec);
195  PARSE_SKIP(unknown);
196  PARSE_SKIP(unknown);
197  PARSE_UINT(chan.m_serviceid);
198 
199  AddChannel(mux, chan);
200 
201  return true;
202 }
203 
204 bool DTVConfParser::ParseConfQPSK(const QStringList &tokens)
205 {
206  DTVChannelInfo chan;
207  DTVMultiplex mux;
208 
209  QStringList::const_iterator it = tokens.begin();
210 
211  PARSE_STR(chan.m_name);
213  PARSE_CONF(mux.m_polarity);
214  PARSE_SKIP(Satelite_Number);
216  PARSE_SKIP(unknown);
217  PARSE_SKIP(unknown);
218  PARSE_UINT(chan.m_serviceid);
219 
220  AddChannel(mux, chan);
221 
222  return true;
223 }
224 
225 bool DTVConfParser::ParseVDR(const QStringList &tokens, int channelNo)
226 {
227  DTVChannelInfo chan;
228  DTVMultiplex mux;
229 
230  QStringList::const_iterator it = tokens.begin();
231 
232  chan.m_lcn = channelNo;
233 
234 // BBC ONE:754166:I999B8C34D34M16T2G32Y0:T:27500:600:601, 602:0:0:4168:0:0:0
235 
236  PARSE_SKIP(unknown);
237 
239 
240  if (it == tokens.end())
241  return false;
242 
243  QString params = (*it++);
244  while (!params.isEmpty())
245  {
246  QString ori = params;
247  int s = (int) (params.toLatin1().constData()[0]);
248  params = params.mid(1);
249  switch (s)
250  {
251  case 'I':
252  mux.m_inversion.ParseVDR(params);
253  break;
254  case 'B':
255  mux.m_bandwidth.ParseVDR(params);
256  break;
257  case 'C':
258  mux.m_hpCodeRate.ParseVDR(params);
259  break;
260  case 'D':
261  mux.m_lpCodeRate.ParseVDR(params);
262  break;
263  case 'M':
264  mux.m_modulation.ParseVDR(params);
265  break;
266  case 'T':
267  mux.m_transMode.ParseVDR(params);
268  break;
269  case 'G':
270  mux.m_guardInterval.ParseVDR(params);
271  break;
272  case 'Y':
273  mux.m_hierarchy.ParseVDR(params);
274  break;
275  case 'V':
276  case 'H':
277  case 'R':
278  case 'L':
279  mux.m_polarity.ParseVDR(ori);
280  break;
281  case 'S':
282  mux.m_modSys.ParseVDR(params);
283  break;
284  case 'O':
285  mux.m_rolloff.ParseVDR(params);
286  break;
287  default:
288  return false;
289  }
290  }
291 
292  for (uint i = 0; i < 6; i++)
293  PARSE_SKIP(unknown);
294 
295  PARSE_UINT(chan.m_serviceid);
296 
297  AddChannel(mux, chan);
298 
299  return true;
300 }
301 
303 {
304  for (auto & channel : m_channels)
305  {
306  if (channel == mux)
307  {
308  channel.channels.push_back(chan);
309 
310  LOG(VB_GENERAL, LOG_INFO, "Imported channel: " + chan.toString() +
311  " on " + mux.toString());
312  return;
313  }
314  }
315 
316  m_channels.emplace_back(mux);
317  m_channels.back().channels.push_back(chan);
318 
319  LOG(VB_GENERAL, LOG_INFO, "Imported channel: " + chan.toString() +
320  " on " + mux.toString());
321 }
DTVInversion::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:200
DTVMultiplex::m_frequency
uint64_t m_frequency
Definition: dtvmultiplex.h:94
DTVMultiplex
Definition: dtvmultiplex.h:24
DTVRollOff::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:763
DTVMultiplex::m_rolloff
DTVRollOff m_rolloff
Definition: dtvmultiplex.h:107
DTVModulationSystem::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:714
DTVConfParser::ParseConfATSC
bool ParseConfATSC(const QStringList &tokens)
Definition: dtvconfparser.cpp:163
DTVConfParser::ERROR_OPEN
@ ERROR_OPEN
Definition: dtvconfparser.h:76
mythdbcon.h
DTVChannelInfo::toString
QString toString() const
Definition: dtvconfparser.cpp:65
DTVConfParser::ATSC
@ ATSC
Definition: dtvconfparser.h:77
DTVMultiplex::m_hierarchy
DTVHierarchy m_hierarchy
Definition: dtvmultiplex.h:103
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
DTVPolarity::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:627
DTVMultiplex::m_bandwidth
DTVBandwidth m_bandwidth
Definition: dtvmultiplex.h:97
build_compdb.file
file
Definition: build_compdb.py:55
DTVConfParser::QAM
@ QAM
Definition: dtvconfparser.h:77
DTVMultiplex::m_inversion
DTVInversion m_inversion
Definition: dtvmultiplex.h:96
DTVConfParser::DVBS2
@ DVBS2
Definition: dtvconfparser.h:77
DTVConfParser::ParseConfQPSK
bool ParseConfQPSK(const QStringList &tokens)
Definition: dtvconfparser.cpp:204
mythlogging.h
DTVMultiplex::m_guardInterval
DTVGuardInterval m_guardInterval
Definition: dtvmultiplex.h:102
DTVGuardInterval::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:535
DTVChannelInfo::m_name
QString m_name
Definition: dtvconfparser.h:54
DTVChannelInfo::m_serviceid
uint m_serviceid
Definition: dtvconfparser.h:55
DTVConfParser::OK
@ OK
Definition: dtvconfparser.h:76
DTVConfParser::m_filename
QString m_filename
Definition: dtvconfparser.h:98
DTVMultiplex::m_hpCodeRate
DTVCodeRate m_hpCodeRate
Definition: dtvmultiplex.h:98
DTVBandwidth::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:263
PARSE_STR
#define PARSE_STR(VAR)
Definition: dtvconfparser.cpp:52
DTVConfParser::ERROR_PARSE
@ ERROR_PARSE
Definition: dtvconfparser.h:76
DTVModulation::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:407
DTVMultiplex::m_modSys
DTVModulationSystem m_modSys
Definition: dtvmultiplex.h:106
DTVConfParser::m_type
cardtype_t m_type
Definition: dtvconfparser.h:96
DTVMultiplex::m_fec
DTVCodeRate m_fec
Definition: dtvmultiplex.h:105
DTVMultiplex::toString
QString toString() const
Definition: dtvmultiplex.cpp:35
uint
unsigned int uint
Definition: compat.h:81
DTVConfParser::OFDM
@ OFDM
Definition: dtvconfparser.h:77
channelutil.h
DTVConfParser::QPSK
@ QPSK
Definition: dtvconfparser.h:77
DTVTransmitMode::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:470
DTVMultiplex::m_symbolRate
uint64_t m_symbolRate
Definition: dtvmultiplex.h:95
PARSE_UINT_1000
#define PARSE_UINT_1000(VAR)
Definition: dtvconfparser.cpp:59
DTVConfParser::AddChannel
void AddChannel(const DTVMultiplex &mux, DTVChannelInfo &chan)
Definition: dtvconfparser.cpp:302
DTVHierarchy::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:588
DTVConfParser::Parse
return_t Parse(void)
Definition: dtvconfparser.cpp:70
DTVCodeRate::ParseVDR
bool ParseVDR(const QString &_value)
Definition: dtvconfparserhelpers.h:336
DTVConfParser::m_channels
DTVChannelList m_channels
Definition: dtvconfparser.h:102
mythcontext.h
DTVMultiplex::m_modulation
DTVModulation m_modulation
Definition: dtvmultiplex.h:100
PARSE_CONF
#define PARSE_CONF(VAR)
Definition: dtvconfparser.cpp:48
DTVMultiplex::m_polarity
DTVPolarity m_polarity
Definition: dtvmultiplex.h:104
DTVConfParser::return_t
return_t
Definition: dtvconfparser.h:76
DTVChannelInfo::m_lcn
int m_lcn
Definition: dtvconfparser.h:56
dtvconfparser.h
PARSE_UINT
#define PARSE_UINT(VAR)
Definition: dtvconfparser.cpp:55
DTVConfParser::ParseConfQAM
bool ParseConfQAM(const QStringList &tokens)
Definition: dtvconfparser.cpp:182
DTVMultiplex::m_transMode
DTVTransmitMode m_transMode
Definition: dtvmultiplex.h:101
DTVChannelInfo
Definition: dtvconfparser.h:47
PARSE_SKIP
#define PARSE_SKIP(VAR)
Definition: dtvconfparser.cpp:44
DTVConfParser::ParseVDR
bool ParseVDR(const QStringList &tokens, int channelNo=-1)
Definition: dtvconfparser.cpp:225
DTVConfParser::ParseConfOFDM
bool ParseConfOFDM(const QStringList &tokens)
Definition: dtvconfparser.cpp:137
DTVMultiplex::m_lpCodeRate
DTVCodeRate m_lpCodeRate
Definition: dtvmultiplex.h:99