2 #include <lib/dvb/db.h>
3 #include <lib/dvb/frontend.h>
4 #include <lib/base/eerror.h>
5 #include <lib/dvb_si/sdt.h>
6 #include <lib/dvb_si/descriptor_tag.h>
7 #include <lib/dvb_si/service_descriptor.h>
8 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
10 DEFINE_REF(eDVBService);
12 eDVBService::eDVBService()
16 eDVBService::~eDVBService()
20 eDVBService &eDVBService::operator=(const eDVBService &s)
22 m_service_name = s.m_service_name;
23 m_provider_name = s.m_provider_name;
30 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
32 name = m_service_name;
36 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
41 case eDVBChannelQuery::tName:
42 res = m_service_name.find(query.m_string) != std::string::npos;
44 case eDVBChannelQuery::tProvider:
45 res = m_provider_name.find(query.m_string) != std::string::npos;
47 case eDVBChannelQuery::tType:
48 res = ref.getServiceType() == query.m_int;
50 case eDVBChannelQuery::tBouquet:
53 case eDVBChannelQuery::tSatellitePosition:
54 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
56 case eDVBChannelQuery::tChannelID:
59 ref.getChannelID(chid);
60 res = chid == query.m_channelid;
63 case eDVBChannelQuery::tAND:
64 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
66 case eDVBChannelQuery::tOR:
67 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
81 eDebug("---- opening lame channel db");
82 FILE *f=fopen("lamedb", "rt");
86 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
88 eDebug("not a servicefile");
92 eDebug("reading services");
93 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
95 eDebug("services invalid, no transponders");
100 // clear all transponders
104 if (!fgets(line, 256, f))
106 if (!strcmp(line, "end\n"))
108 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
109 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
110 if (original_network_id == -1)
112 eDVBChannelID channelid = eDVBChannelID(
113 eDVBNamespace(dvb_namespace),
114 eTransportStreamID(transport_stream_id),
115 eOriginalNetworkID(original_network_id));
117 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
121 if (!strcmp(line, "/\n"))
125 eDVBFrontendParametersSatellite sat;
126 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
127 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &inversion, &orbital_position);
128 sat.frequency = frequency;
129 sat.symbol_rate = symbol_rate;
130 sat.polarisation = polarisation;
132 sat.orbital_position = orbital_position;
133 sat.inversion = inversion;
135 // t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
136 feparm->setDVBS(sat);
140 int frequency, symbol_rate, inversion=0, modulation=3;
141 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
142 // t.setCable(frequency, symbol_rate, inversion, modulation);
145 addChannelToList(channelid, feparm);
148 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
150 eDebug("services invalid, no services");
154 // clear all services
160 if (!fgets(line, 256, f))
162 if (!strcmp(line, "end\n"))
165 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
166 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
167 if (service_number == -1)
169 ePtr<eDVBService> s = new eDVBService;
170 eServiceReferenceDVB ref =
171 eServiceReferenceDVB(
172 eDVBNamespace(dvb_namespace),
173 eTransportStreamID(transport_stream_id),
174 eOriginalNetworkID(original_network_id),
175 eServiceID(service_id),
180 line[strlen(line)-1]=0;
181 s->m_service_name=line;
184 line[strlen(line)-1]=0;
186 std::string str=line;
188 if (str[1]!=':') // old ... (only service_provider)
190 s->m_provider_name=line;
192 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
194 unsigned int c=str.find(',');
197 if (c == std::string::npos)
203 v=str.substr(2, c-2);
206 // eDebug("%c ... %s", p, v.c_str());
208 s->m_provider_name=v;
211 sscanf(v.c_str(), "%x", &s->m_flags);
215 sscanf(v.c_str(), "%02d%04x", &cid, &val);
220 sscanf(v.c_str(), "%04x", &val);
227 eDebug("loaded %d services", count);
235 eDebug("---- saving lame channel db");
236 FILE *f=fopen("lamedb", "wt");
237 int channels=0, services=0;
239 eFatal("couldn't save lame channel db!");
240 fprintf(f, "eDVB services /3/\n");
241 fprintf(f, "transponders\n");
242 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
243 i != m_channels.end(); ++i)
245 const eDVBChannelID &chid = i->first;
246 const channel &ch = i->second;
248 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
249 chid.transport_stream_id.get(), chid.original_network_id.get());
250 eDVBFrontendParametersSatellite sat;
251 if (!ch.m_frontendParameters->getDVBS(sat))
253 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
254 sat.frequency, sat.symbol_rate,
255 sat.polarisation, sat.fec, sat.inversion,
256 sat.orbital_position);
261 fprintf(f, "end\nservices\n");
263 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
264 i != m_services.end(); ++i)
266 const eServiceReferenceDVB &s = i->first;
267 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
268 s.getServiceID().get(), s.getDVBNamespace().get(),
269 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
273 fprintf(f, "%s\n", i->second->m_service_name.c_str());
274 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
275 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
276 ca != i->second->m_ca.end(); ++ca)
277 fprintf(f, ",C:%04x", *ca);
281 fprintf(f, "end\nHave a lot of bugs!\n");
282 eDebug("saved %d channels and %d services!", channels, services);
286 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
290 ch.m_frontendParameters = feparm;
291 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
295 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
297 m_channels.erase(id);
301 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
303 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
304 if (i == m_channels.end())
309 parm = i->second.m_frontendParameters;
313 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
315 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
319 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
321 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
322 i = m_services.find(reference);
323 if (i == m_services.end())
332 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q)
334 query = new eDVBDBQuery(this, eServiceReference(), q);
338 DEFINE_REF(eDVBDBQuery);
340 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query): m_db(db), m_query(query)
342 // TODO: use SOURCE ...
343 m_cursor = m_db->m_services.begin();
346 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
348 while (m_cursor != m_db->m_services.end())
350 ref = m_cursor->first;
352 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
362 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[AND (..)] */
364 /* never, NEVER write a parser in C++! */
365 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
367 std::list<std::string>::const_iterator end_of_exp;
371 while (end_of_exp != end)
372 if (*end_of_exp == ")")
377 if (end_of_exp == end)
379 eDebug("expression parse: end of expression while searching for closing brace");
385 int r = parseExpression(res, begin, end_of_exp);
390 /* we had only one sub expression */
391 if (end_of_exp == end)
394 /* otherwise we have an operator here.. */
396 ePtr<eDVBChannelQuery> r2 = res;
397 res = new eDVBChannelQuery();
402 if (*end_of_exp == "||")
403 res->m_type = eDVBChannelQuery::tOR;
404 else if (*end_of_exp == "&&")
405 res->m_type = eDVBChannelQuery::tAND;
408 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
415 return parseExpression(res->m_p2, end_of_exp, end);
418 // "begin" <op> "end"
419 std::string type, op, val;
421 res = new eDVBChannelQuery();
438 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
447 eDebug("malformed query: missing stuff");
453 res->m_type = eDVBChannelQuery::tName;
454 else if (type == "provider")
455 res->m_type = eDVBChannelQuery::tProvider;
456 else if (type == "type")
457 res->m_type = eDVBChannelQuery::tType;
458 else if (type == "bouquet")
459 res->m_type = eDVBChannelQuery::tBouquet;
460 else if (type == "satellitePosition")
461 res->m_type = eDVBChannelQuery::tSatellitePosition;
462 else if (type == "channelID")
463 res->m_type = eDVBChannelQuery::tChannelID;
466 eDebug("malformed query: invalid type %s", type.c_str());
471 eDebug("type is %d, nice!", res->m_type);
479 eDebug("invalid operator %s", op.c_str());
485 res->m_int = atoi(val.c_str());
486 // res->m_channelid = eDVBChannelID(val);
491 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
493 std::list<std::string> tokens;
495 std::string current_token;
497 // eDebug("splitting %s....", query.c_str());
499 const char *splitchars="()";
500 int quotemode = 0, lastsplit = 0, lastalnum = 0;
501 while (i <= query.size())
503 int c = (i < query.size()) ? query[i] : ' ';
506 int issplit = !!strchr(splitchars, c);
507 int isaln = isalnum(c);
508 int iswhite = c == ' ';
509 int isquot = c == '\"';
513 iswhite = issplit = 0;
517 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
519 if (current_token.size())
520 tokens.push_back(current_token);
521 current_token.clear();
524 if (!(iswhite || isquot))
528 quotemode = !quotemode;
533 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
535 // printf("%s\n", a->c_str());
538 /* now we recursivly parse that. */
539 return parseExpression(res, tokens.begin(), tokens.end());
541 res = new eDVBChannelQuery();
542 res->m_type = eDVBChannelQuery::tName;
544 res->m_string = query;
548 DEFINE_REF(eDVBChannelQuery);