2 #include <lib/dvb/db.h>
3 #include <lib/dvb/frontend.h>
4 #include <lib/base/eerror.h>
5 #include <lib/base/estring.h>
6 #include <dvbsi++/service_description_section.h>
7 #include <dvbsi++/descriptor_tag.h>
8 #include <dvbsi++/service_descriptor.h>
9 #include <dvbsi++/satellite_delivery_system_descriptor.h>
11 DEFINE_REF(eDVBService);
13 eDVBService::eDVBService()
17 eDVBService::~eDVBService()
21 eDVBService &eDVBService::operator=(const eDVBService &s)
23 m_service_name = s.m_service_name;
24 m_service_name_sort = s.m_service_name_sort;
25 m_provider_name = s.m_provider_name;
32 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
34 if ( ref.name.length() )
37 name = m_service_name;
41 int eDVBService::getLength(const eServiceReference &ref)
46 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
51 case eDVBChannelQuery::tName:
52 res = m_service_name_sort.find(query.m_string) != std::string::npos;
54 case eDVBChannelQuery::tProvider:
55 res = m_provider_name.find(query.m_string) != std::string::npos;
57 case eDVBChannelQuery::tType:
58 res = ref.getServiceType() == query.m_int;
60 case eDVBChannelQuery::tBouquet:
63 case eDVBChannelQuery::tSatellitePosition:
64 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
66 case eDVBChannelQuery::tChannelID:
69 ref.getChannelID(chid);
70 res = chid == query.m_channelid;
73 case eDVBChannelQuery::tAND:
74 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
76 case eDVBChannelQuery::tOR:
77 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
87 int eDVBService::getCachePID(cacheID id)
89 std::map<int, int>::iterator it = m_cache.find(id);
90 if ( it != m_cache.end() )
95 void eDVBService::setCachePID(cacheID id, int pid)
102 /* THIS CODE IS BAD. it should be replaced by somethine better. */
105 eDebug("---- opening lame channel db");
106 FILE *f=fopen("lamedb", "rt");
110 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
112 eDebug("not a servicefile");
116 eDebug("reading services");
117 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
119 eDebug("services invalid, no transponders");
124 // clear all transponders
128 if (!fgets(line, 256, f))
130 if (!strcmp(line, "end\n"))
132 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
133 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
134 if (original_network_id == -1)
136 eDVBChannelID channelid = eDVBChannelID(
137 eDVBNamespace(dvb_namespace),
138 eTransportStreamID(transport_stream_id),
139 eOriginalNetworkID(original_network_id));
141 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
145 if (!strcmp(line, "/\n"))
149 eDVBFrontendParametersSatellite sat;
150 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
151 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
152 sat.frequency = frequency;
153 sat.symbol_rate = symbol_rate;
154 sat.polarisation = polarisation;
156 sat.orbital_position = orbital_position;
157 sat.inversion = inversion;
159 // t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
160 feparm->setDVBS(sat);
161 } else if (line[1]=='t')
163 eDVBFrontendParametersTerrestrial ter;
164 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
165 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &bandwidth, &code_rate_HP, &code_rate_LP, &modulation, &transmission_mode, &guard_interval, &hierarchy, &inversion);
166 ter.frequency = frequency;
167 ter.bandwidth = bandwidth;
168 ter.code_rate_HP = code_rate_HP;
169 ter.code_rate_LP = code_rate_LP;
170 ter.modulation = modulation;
171 ter.transmission_mode = transmission_mode;
172 ter.guard_interval = guard_interval;
173 ter.hierarchy = hierarchy;
174 ter.inversion = inversion;
176 feparm->setDVBT(ter);
177 } else if (line[1]=='c')
179 int frequency, symbol_rate, inversion=0, modulation=3;
180 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
181 // t.setCable(frequency, symbol_rate, inversion, modulation);
184 addChannelToList(channelid, feparm);
187 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
189 eDebug("services invalid, no services");
193 // clear all services
199 if (!fgets(line, 256, f))
201 if (!strcmp(line, "end\n"))
204 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
205 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
206 if (service_number == -1)
208 ePtr<eDVBService> s = new eDVBService;
209 eServiceReferenceDVB ref =
210 eServiceReferenceDVB(
211 eDVBNamespace(dvb_namespace),
212 eTransportStreamID(transport_stream_id),
213 eOriginalNetworkID(original_network_id),
214 eServiceID(service_id),
219 line[strlen(line)-1]=0;
221 s->m_service_name = line;
222 s->m_service_name_sort = removeDVBChars(line);
223 makeUpper(s->m_service_name_sort);
227 line[strlen(line)-1]=0;
229 std::string str=line;
231 if (str[1]!=':') // old ... (only service_provider)
233 s->m_provider_name=line;
235 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
237 unsigned int c=str.find(',');
240 if (c == std::string::npos)
246 v=str.substr(2, c-2);
249 // eDebug("%c ... %s", p, v.c_str());
251 s->m_provider_name=v;
254 sscanf(v.c_str(), "%x", &s->m_flags);
258 sscanf(v.c_str(), "%02d%04x", &cid, &val);
263 sscanf(v.c_str(), "%04x", &val);
270 eDebug("loaded %d services", count);
277 eDebug("---- saving lame channel db");
278 FILE *f=fopen("lamedb", "wt");
279 int channels=0, services=0;
281 eFatal("couldn't save lame channel db!");
282 fprintf(f, "eDVB services /3/\n");
283 fprintf(f, "transponders\n");
284 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
285 i != m_channels.end(); ++i)
287 const eDVBChannelID &chid = i->first;
288 const channel &ch = i->second;
290 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
291 chid.transport_stream_id.get(), chid.original_network_id.get());
292 eDVBFrontendParametersSatellite sat;
293 eDVBFrontendParametersTerrestrial ter;
294 if (!ch.m_frontendParameters->getDVBS(sat))
296 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
297 sat.frequency, sat.symbol_rate,
298 sat.polarisation, sat.fec, sat.orbital_position,
301 if (!ch.m_frontendParameters->getDVBT(ter))
303 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
304 ter.frequency, ter.bandwidth, ter.code_rate_HP,
305 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
306 ter.guard_interval, ter.hierarchy, ter.inversion);
311 fprintf(f, "end\nservices\n");
313 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
314 i != m_services.end(); ++i)
316 const eServiceReferenceDVB &s = i->first;
317 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
318 s.getServiceID().get(), s.getDVBNamespace().get(),
319 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
323 fprintf(f, "%s\n", i->second->m_service_name.c_str());
324 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
327 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
328 ca != i->second->m_cache.end(); ++ca)
329 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
331 // write cached ca pids
332 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
333 ca != i->second->m_ca.end(); ++ca)
334 fprintf(f, ",C:%04x", *ca);
339 fprintf(f, "end\nHave a lot of bugs!\n");
340 eDebug("saved %d channels and %d services!", channels, services);
344 void eDVBDB::loadBouquet(const char *path)
346 std::string bouquet_name = path;
347 if (!bouquet_name.length())
349 eDebug("Bouquet load failed.. no path given..");
352 unsigned int pos = bouquet_name.rfind('/');
353 if ( pos != std::string::npos )
354 bouquet_name.erase(0, pos+1);
355 if (bouquet_name.empty())
357 eDebug("Bouquet load failed.. no filename given..");
360 eBouquet &bouquet = m_bouquets[bouquet_name];
361 bouquet.m_path = path;
362 std::list<eServiceReference> &list = bouquet.m_services;
365 eDebug("loading bouquet... %s", path);
366 FILE *fp=fopen(path, "rt");
370 eDebug("failed to open.");
374 bool read_descr=false;
375 eServiceReference *e = NULL;
378 if (!fgets(line, 256, fp))
380 line[strlen(line)-1]=0;
381 if (strlen(line) && line[strlen(line)-1]=='\r')
382 line[strlen(line)-1]=0;
387 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
389 int offs = line[8] == ':' ? 10 : 9;
390 eServiceReference tmp(line+offs);
391 if (tmp.type != eServiceReference::idDVB)
393 eDebug("only DVB Bouquets supported");
396 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
398 std::string str = tmp.path;
399 unsigned int pos = str.rfind('/');
400 if ( pos != std::string::npos )
404 eDebug("Bouquet load failed.. no filename given..");
407 loadBouquet(tmp.path.c_str());
409 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
411 eDebug("read bouquet %s", tmp.toString().c_str());
418 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
423 else if (!strncmp(line, "#NAME ", 6))
424 bouquet.m_bouquet_name=line+6;
429 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
432 void eDVBDB::saveBouquet(const char *path)
437 void eDVBDB::loadBouquets()
439 loadBouquet("bouquets.tv");
440 loadBouquet("bouquets.radio");
443 void eDVBDB::saveBouquets()
459 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
463 ch.m_frontendParameters = feparm;
464 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
468 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
470 m_channels.erase(id);
474 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
476 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
477 if (i == m_channels.end())
482 parm = i->second.m_frontendParameters;
486 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
488 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
492 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
494 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
495 i = m_services.find(reference);
496 if (i == m_services.end())
505 RESULT eDVBDB::getBouquet(const eServiceReference &ref, const eBouquet* &bouquet)
507 std::string str = ref.path;
510 eDebug("getBouquet failed.. no path given!");
513 unsigned int pos = str.find("FROM BOUQUET \"");
514 if ( pos != std::string::npos )
516 str.erase(0, pos+14);
517 eDebug("str now %s", str.c_str());
519 if ( pos != std::string::npos )
523 eDebug("str now %s", str.c_str());
527 eDebug("getBouquet failed.. couldn't parse bouquet name");
530 std::map<std::string, eBouquet>::iterator i =
531 m_bouquets.find(str);
532 if (i == m_bouquets.end())
537 bouquet = &i->second;
541 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
543 if ( q && q->m_bouquet_name.length() )
544 query = new eDVBDBBouquetQuery(this, source, q);
546 query = new eDVBDBQuery(this, source, q);
550 DEFINE_REF(eDVBDBQueryBase);
552 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
553 :m_db(db), m_query(query), m_source(source)
557 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
559 ePtr<eDVBService> a_service, b_service;
561 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
563 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
565 if (m_db->getService(a, a_service))
567 if (m_db->getService(b, b_service))
573 case eDVBChannelQuery::tName:
574 return a_service->m_service_name_sort < b_service->m_service_name_sort;
575 case eDVBChannelQuery::tProvider:
576 return a_service->m_provider_name < b_service->m_provider_name;
577 case eDVBChannelQuery::tType:
578 return a.getServiceType() < b.getServiceType();
579 case eDVBChannelQuery::tBouquet:
581 case eDVBChannelQuery::tSatellitePosition:
582 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
589 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
590 :eDVBDBQueryBase(db, source, query)
592 m_cursor = m_db->m_services.begin();
595 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
597 while (m_cursor != m_db->m_services.end())
599 ref = m_cursor->first;
601 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
609 ref = eServiceReferenceDVB();
613 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
614 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
618 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
620 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
621 std::list<eServiceReference> &list = bouquet.m_services;
622 while (m_cursor != list.end())
624 ref = *((eServiceReferenceDVB*)&(*m_cursor));
626 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
627 m_db->m_services.find(ref);
629 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
637 ref = eServiceReferenceDVB();
642 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
644 static int decodeType(const std::string &type)
647 return eDVBChannelQuery::tName;
648 else if (type == "provider")
649 return eDVBChannelQuery::tProvider;
650 else if (type == "type")
651 return eDVBChannelQuery::tType;
652 else if (type == "bouquet")
653 return eDVBChannelQuery::tBouquet;
654 else if (type == "satellitePosition")
655 return eDVBChannelQuery::tSatellitePosition;
656 else if (type == "channelID")
657 return eDVBChannelQuery::tChannelID;
662 /* never, NEVER write a parser in C++! */
663 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
665 std::list<std::string>::const_iterator end_of_exp;
669 while (end_of_exp != end)
670 if (*end_of_exp == ")")
675 if (end_of_exp == end)
677 eDebug("expression parse: end of expression while searching for closing brace");
683 int r = parseExpression(res, begin, end_of_exp);
688 /* we had only one sub expression */
689 if (end_of_exp == end)
691 eDebug("only one sub expression");
695 /* otherwise we have an operator here.. */
697 ePtr<eDVBChannelQuery> r2 = res;
698 res = new eDVBChannelQuery();
704 if (*end_of_exp == "||")
705 res->m_type = eDVBChannelQuery::tOR;
706 else if (*end_of_exp == "&&")
707 res->m_type = eDVBChannelQuery::tAND;
710 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
717 return parseExpression(res->m_p2, end_of_exp, end);
720 // "begin" <op> "end"
721 std::string type, op, val;
723 res = new eDVBChannelQuery();
741 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
750 eDebug("malformed query: missing stuff");
755 res->m_type = decodeType(type);
757 if (res->m_type == -1)
759 eDebug("malformed query: invalid type %s", type.c_str());
770 eDebug("invalid operator %s", op.c_str());
776 res->m_int = atoi(val.c_str());
777 // res->m_channelid = eDVBChannelID(val);
782 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
784 std::list<std::string> tokens;
786 std::string current_token;
787 std::string bouquet_name;
789 eDebug("splitting %s....", query.c_str());
791 const char *splitchars="()";
792 int quotemode = 0, lastsplit = 0, lastalnum = 0;
793 while (i <= query.size())
795 int c = (i < query.size()) ? query[i] : ' ';
798 int issplit = !!strchr(splitchars, c);
799 int isaln = isalnum(c);
800 int iswhite = c == ' ';
801 int isquot = c == '\"';
805 iswhite = issplit = 0;
809 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
811 if (current_token.size())
812 tokens.push_back(current_token);
813 current_token.clear();
816 if (!(iswhite || isquot))
820 quotemode = !quotemode;
825 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
827 // printf("%s\n", a->c_str());
830 int sort = eDVBChannelQuery::tName;
831 /* check for "ORDER BY ..." */
833 while (tokens.size() > 2)
835 std::list<std::string>::iterator it = tokens.end();
843 sort = decodeType(*it);
844 tokens.pop_back(); // ...
845 tokens.pop_back(); // BY
846 tokens.pop_back(); // ORDER
850 else if (*it == "FROM")
853 if (*it == "BOUQUET")
857 tokens.pop_back(); // ...
858 tokens.pop_back(); // FROM
859 tokens.pop_back(); // BOUQUET
868 eWarning("ORDER BY .. string invalid.");
873 eDebug("sort by %d", sort);
875 /* now we recursivly parse that. */
876 int r = parseExpression(res, tokens.begin(), tokens.end());
881 res->m_bouquet_name = bouquet_name;
884 eDebug("return: %d", r);
888 DEFINE_REF(eDVBChannelQuery);