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);
77 int eDVBService::getCachePID(cacheID id)
79 std::map<int, int>::iterator it = m_cache.find(id);
80 if ( it != m_cache.end() )
85 void eDVBService::setCachePID(cacheID id, int pid)
94 eDebug("---- opening lame channel db");
95 FILE *f=fopen("lamedb", "rt");
99 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
101 eDebug("not a servicefile");
105 eDebug("reading services");
106 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
108 eDebug("services invalid, no transponders");
113 // clear all transponders
117 if (!fgets(line, 256, f))
119 if (!strcmp(line, "end\n"))
121 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
122 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
123 if (original_network_id == -1)
125 eDVBChannelID channelid = eDVBChannelID(
126 eDVBNamespace(dvb_namespace),
127 eTransportStreamID(transport_stream_id),
128 eOriginalNetworkID(original_network_id));
130 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
134 if (!strcmp(line, "/\n"))
138 eDVBFrontendParametersSatellite sat;
139 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
140 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &inversion, &orbital_position);
141 sat.frequency = frequency;
142 sat.symbol_rate = symbol_rate;
143 sat.polarisation = polarisation;
145 sat.orbital_position = orbital_position;
146 sat.inversion = inversion;
148 // t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
149 feparm->setDVBS(sat);
153 int frequency, symbol_rate, inversion=0, modulation=3;
154 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
155 // t.setCable(frequency, symbol_rate, inversion, modulation);
158 addChannelToList(channelid, feparm);
161 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
163 eDebug("services invalid, no services");
167 // clear all services
173 if (!fgets(line, 256, f))
175 if (!strcmp(line, "end\n"))
178 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
179 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
180 if (service_number == -1)
182 ePtr<eDVBService> s = new eDVBService;
183 eServiceReferenceDVB ref =
184 eServiceReferenceDVB(
185 eDVBNamespace(dvb_namespace),
186 eTransportStreamID(transport_stream_id),
187 eOriginalNetworkID(original_network_id),
188 eServiceID(service_id),
193 line[strlen(line)-1]=0;
194 s->m_service_name=line;
197 line[strlen(line)-1]=0;
199 std::string str=line;
201 if (str[1]!=':') // old ... (only service_provider)
203 s->m_provider_name=line;
205 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
207 unsigned int c=str.find(',');
210 if (c == std::string::npos)
216 v=str.substr(2, c-2);
219 // eDebug("%c ... %s", p, v.c_str());
221 s->m_provider_name=v;
224 sscanf(v.c_str(), "%x", &s->m_flags);
228 sscanf(v.c_str(), "%02d%04x", &cid, &val);
233 sscanf(v.c_str(), "%04x", &val);
240 eDebug("loaded %d services", count);
248 eDebug("---- saving lame channel db");
249 FILE *f=fopen("lamedb", "wt");
250 int channels=0, services=0;
252 eFatal("couldn't save lame channel db!");
253 fprintf(f, "eDVB services /3/\n");
254 fprintf(f, "transponders\n");
255 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
256 i != m_channels.end(); ++i)
258 const eDVBChannelID &chid = i->first;
259 const channel &ch = i->second;
261 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
262 chid.transport_stream_id.get(), chid.original_network_id.get());
263 eDVBFrontendParametersSatellite sat;
264 if (!ch.m_frontendParameters->getDVBS(sat))
266 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
267 sat.frequency, sat.symbol_rate,
268 sat.polarisation, sat.fec, sat.inversion,
269 sat.orbital_position);
274 fprintf(f, "end\nservices\n");
276 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
277 i != m_services.end(); ++i)
279 const eServiceReferenceDVB &s = i->first;
280 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
281 s.getServiceID().get(), s.getDVBNamespace().get(),
282 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
286 fprintf(f, "%s\n", i->second->m_service_name.c_str());
287 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
288 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
289 ca != i->second->m_ca.end(); ++ca)
290 fprintf(f, ",C:%04x", *ca);
294 fprintf(f, "end\nHave a lot of bugs!\n");
295 eDebug("saved %d channels and %d services!", channels, services);
299 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
303 ch.m_frontendParameters = feparm;
304 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
308 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
310 m_channels.erase(id);
314 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
316 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
317 if (i == m_channels.end())
322 parm = i->second.m_frontendParameters;
326 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
328 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
332 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
334 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
335 i = m_services.find(reference);
336 if (i == m_services.end())
345 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q)
347 query = new eDVBDBQuery(this, eServiceReference(), q);
351 DEFINE_REF(eDVBDBQuery);
353 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query): m_db(db), m_query(query)
355 // TODO: use SOURCE ...
356 m_cursor = m_db->m_services.begin();
359 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
361 while (m_cursor != m_db->m_services.end())
363 ref = m_cursor->first;
365 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
375 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[AND (..)] */
377 /* never, NEVER write a parser in C++! */
378 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
380 std::list<std::string>::const_iterator end_of_exp;
384 while (end_of_exp != end)
385 if (*end_of_exp == ")")
390 if (end_of_exp == end)
392 eDebug("expression parse: end of expression while searching for closing brace");
398 int r = parseExpression(res, begin, end_of_exp);
403 /* we had only one sub expression */
404 if (end_of_exp == end)
407 /* otherwise we have an operator here.. */
409 ePtr<eDVBChannelQuery> r2 = res;
410 res = new eDVBChannelQuery();
415 if (*end_of_exp == "||")
416 res->m_type = eDVBChannelQuery::tOR;
417 else if (*end_of_exp == "&&")
418 res->m_type = eDVBChannelQuery::tAND;
421 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
428 return parseExpression(res->m_p2, end_of_exp, end);
431 // "begin" <op> "end"
432 std::string type, op, val;
434 res = new eDVBChannelQuery();
451 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
460 eDebug("malformed query: missing stuff");
466 res->m_type = eDVBChannelQuery::tName;
467 else if (type == "provider")
468 res->m_type = eDVBChannelQuery::tProvider;
469 else if (type == "type")
470 res->m_type = eDVBChannelQuery::tType;
471 else if (type == "bouquet")
472 res->m_type = eDVBChannelQuery::tBouquet;
473 else if (type == "satellitePosition")
474 res->m_type = eDVBChannelQuery::tSatellitePosition;
475 else if (type == "channelID")
476 res->m_type = eDVBChannelQuery::tChannelID;
479 eDebug("malformed query: invalid type %s", type.c_str());
484 eDebug("type is %d, nice!", res->m_type);
492 eDebug("invalid operator %s", op.c_str());
498 res->m_int = atoi(val.c_str());
499 // res->m_channelid = eDVBChannelID(val);
504 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
506 std::list<std::string> tokens;
508 std::string current_token;
510 // eDebug("splitting %s....", query.c_str());
512 const char *splitchars="()";
513 int quotemode = 0, lastsplit = 0, lastalnum = 0;
514 while (i <= query.size())
516 int c = (i < query.size()) ? query[i] : ' ';
519 int issplit = !!strchr(splitchars, c);
520 int isaln = isalnum(c);
521 int iswhite = c == ' ';
522 int isquot = c == '\"';
526 iswhite = issplit = 0;
530 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
532 if (current_token.size())
533 tokens.push_back(current_token);
534 current_token.clear();
537 if (!(iswhite || isquot))
541 quotemode = !quotemode;
546 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
548 // printf("%s\n", a->c_str());
551 /* now we recursivly parse that. */
552 return parseExpression(res, tokens.begin(), tokens.end());
554 res = new eDVBChannelQuery();
555 res->m_type = eDVBChannelQuery::tName;
557 res->m_string = query;
561 DEFINE_REF(eDVBChannelQuery);