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 RESULT eBouquet::addService(const eServiceReference &ref)
16 std::find(m_services.begin(), m_services.end(), ref);
17 if ( it != m_services.end() )
19 m_services.push_back(ref);
23 RESULT eBouquet::removeService(const eServiceReference &ref)
26 std::find(m_services.begin(), m_services.end(), ref);
27 if ( it == m_services.end() )
33 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
35 if ( pos < 0 || pos >= m_services.size() )
37 list::iterator source=m_services.end();
38 list::iterator dest=m_services.end();
39 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
41 if (dest == m_services.end() && !pos--)
45 if (dest != m_services.end() && source != m_services.end())
48 if (dest == m_services.end() || source == m_services.end() || source == dest)
50 std::iter_swap(source,dest);
54 RESULT eBouquet::flushChanges()
56 FILE *f=fopen(m_path.c_str(), "wt");
59 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
61 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
63 eServiceReference tmp = *i;
64 std::string str = tmp.path;
65 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
67 unsigned int p1 = str.find("FROM BOUQUET \"");
68 if (p1 == std::string::npos)
70 eDebug("doof... kaputt");
75 if (p1 == std::string::npos)
77 eDebug("doof2... kaputt");
83 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
85 if ( i->name.length() )
86 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
93 eDebug("couldn't write file %s", m_path.c_str());
97 eDVBService::eDVBService()
101 eDVBService::~eDVBService()
105 eDVBService &eDVBService::operator=(const eDVBService &s)
107 m_service_name = s.m_service_name;
108 m_service_name_sort = s.m_service_name_sort;
109 m_provider_name = s.m_provider_name;
116 void eDVBService::genSortName()
118 m_service_name_sort = removeDVBChars(m_service_name);
119 makeUpper(m_service_name_sort);
120 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
121 m_service_name_sort.erase(0, 1);
123 /* put unnamed services at the end, not at the beginning. */
124 if (m_service_name_sort.empty())
125 m_service_name_sort = "\xFF";
128 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
130 if (!ref.name.empty())
132 else if (!m_service_name.empty())
133 name = m_service_name;
139 int eDVBService::getLength(const eServiceReference &ref)
144 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
147 switch (query.m_type)
149 case eDVBChannelQuery::tName:
150 res = m_service_name_sort.find(query.m_string) != std::string::npos;
152 case eDVBChannelQuery::tProvider:
153 res = m_provider_name.find(query.m_string) != std::string::npos;
155 case eDVBChannelQuery::tType:
156 res = ref.getServiceType() == query.m_int;
158 case eDVBChannelQuery::tBouquet:
161 case eDVBChannelQuery::tSatellitePosition:
162 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
164 case eDVBChannelQuery::tChannelID:
167 ref.getChannelID(chid);
168 res = chid == query.m_channelid;
171 case eDVBChannelQuery::tAND:
172 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
174 case eDVBChannelQuery::tOR:
175 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
185 int eDVBService::getCachePID(cacheID id)
187 std::map<int, int>::iterator it = m_cache.find(id);
188 if ( it != m_cache.end() )
193 void eDVBService::setCachePID(cacheID id, int pid)
200 /* THIS CODE IS BAD. it should be replaced by somethine better. */
203 eDebug("---- opening lame channel db");
204 FILE *f=fopen("lamedb", "rt");
208 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
210 eDebug("not a servicefile");
214 eDebug("reading services");
215 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
217 eDebug("services invalid, no transponders");
222 // clear all transponders
226 if (!fgets(line, 256, f))
228 if (!strcmp(line, "end\n"))
230 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
231 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
232 if (original_network_id == -1)
234 eDVBChannelID channelid = eDVBChannelID(
235 eDVBNamespace(dvb_namespace),
236 eTransportStreamID(transport_stream_id),
237 eOriginalNetworkID(original_network_id));
239 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
243 if (!strcmp(line, "/\n"))
247 eDVBFrontendParametersSatellite sat;
248 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
249 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
250 sat.frequency = frequency;
251 sat.symbol_rate = symbol_rate;
252 sat.polarisation = polarisation;
254 sat.orbital_position = orbital_position;
255 sat.inversion = inversion;
256 feparm->setDVBS(sat);
257 } else if (line[1]=='t')
259 eDVBFrontendParametersTerrestrial ter;
260 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
261 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);
262 ter.frequency = frequency;
263 ter.bandwidth = bandwidth;
264 ter.code_rate_HP = code_rate_HP;
265 ter.code_rate_LP = code_rate_LP;
266 ter.modulation = modulation;
267 ter.transmission_mode = transmission_mode;
268 ter.guard_interval = guard_interval;
269 ter.hierarchy = hierarchy;
270 ter.inversion = inversion;
272 feparm->setDVBT(ter);
273 } else if (line[1]=='c')
275 int frequency, symbol_rate, inversion=0, modulation=3;
276 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
277 // t.setCable(frequency, symbol_rate, inversion, modulation);
280 addChannelToList(channelid, feparm);
283 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
285 eDebug("services invalid, no services");
289 // clear all services
295 if (!fgets(line, 256, f))
297 if (!strcmp(line, "end\n"))
300 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
301 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
302 if (service_number == -1)
304 ePtr<eDVBService> s = new eDVBService;
305 eServiceReferenceDVB ref =
306 eServiceReferenceDVB(
307 eDVBNamespace(dvb_namespace),
308 eTransportStreamID(transport_stream_id),
309 eOriginalNetworkID(original_network_id),
310 eServiceID(service_id),
315 line[strlen(line)-1]=0;
317 s->m_service_name = line;
322 line[strlen(line)-1]=0;
324 std::string str=line;
326 if (str[1]!=':') // old ... (only service_provider)
328 s->m_provider_name=line;
330 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
332 unsigned int c=str.find(',');
335 if (c == std::string::npos)
341 v=str.substr(2, c-2);
344 // eDebug("%c ... %s", p, v.c_str());
346 s->m_provider_name=v;
349 sscanf(v.c_str(), "%x", &s->m_flags);
353 sscanf(v.c_str(), "%02d%04x", &cid, &val);
358 sscanf(v.c_str(), "%04x", &val);
365 eDebug("loaded %d services", count);
372 eDebug("---- saving lame channel db");
373 FILE *f=fopen("lamedb", "wt");
374 int channels=0, services=0;
376 eFatal("couldn't save lame channel db!");
377 fprintf(f, "eDVB services /3/\n");
378 fprintf(f, "transponders\n");
379 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
380 i != m_channels.end(); ++i)
382 const eDVBChannelID &chid = i->first;
383 const channel &ch = i->second;
385 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
386 chid.transport_stream_id.get(), chid.original_network_id.get());
387 eDVBFrontendParametersSatellite sat;
388 eDVBFrontendParametersTerrestrial ter;
389 if (!ch.m_frontendParameters->getDVBS(sat))
391 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
392 sat.frequency, sat.symbol_rate,
393 sat.polarisation, sat.fec, sat.orbital_position,
396 if (!ch.m_frontendParameters->getDVBT(ter))
398 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
399 ter.frequency, ter.bandwidth, ter.code_rate_HP,
400 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
401 ter.guard_interval, ter.hierarchy, ter.inversion);
406 fprintf(f, "end\nservices\n");
408 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
409 i != m_services.end(); ++i)
411 const eServiceReferenceDVB &s = i->first;
412 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
413 s.getServiceID().get(), s.getDVBNamespace().get(),
414 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
418 fprintf(f, "%s\n", i->second->m_service_name.c_str());
419 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
422 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
423 ca != i->second->m_cache.end(); ++ca)
424 fprintf(f, ",c:%02d%04x", ca->first, ca->second);
426 // write cached ca pids
427 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
428 ca != i->second->m_ca.end(); ++ca)
429 fprintf(f, ",C:%04x", *ca);
434 fprintf(f, "end\nHave a lot of bugs!\n");
435 eDebug("saved %d channels and %d services!", channels, services);
439 void eDVBDB::loadBouquet(const char *path)
441 std::string bouquet_name = path;
442 if (!bouquet_name.length())
444 eDebug("Bouquet load failed.. no path given..");
447 unsigned int pos = bouquet_name.rfind('/');
448 if ( pos != std::string::npos )
449 bouquet_name.erase(0, pos+1);
450 if (bouquet_name.empty())
452 eDebug("Bouquet load failed.. no filename given..");
455 eBouquet &bouquet = m_bouquets[bouquet_name];
456 bouquet.m_path = path;
457 std::list<eServiceReference> &list = bouquet.m_services;
460 eDebug("loading bouquet... %s", path);
461 FILE *fp=fopen(path, "rt");
465 eDebug("failed to open.");
466 if ( strstr(path, "bouquets.tv") )
468 eDebug("recreate bouquets.tv");
469 bouquet.m_bouquet_name="Bouquets (TV)";
470 bouquet.flushChanges();
472 else if ( strstr(path, "bouquets.radio") )
474 eDebug("recreate bouquets.radio");
475 bouquet.m_bouquet_name="Bouquets (Radio)";
476 bouquet.flushChanges();
481 bool read_descr=false;
482 eServiceReference *e = NULL;
485 if (!fgets(line, 256, fp))
487 line[strlen(line)-1]=0;
488 if (strlen(line) && line[strlen(line)-1]=='\r')
489 line[strlen(line)-1]=0;
494 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
496 int offs = line[8] == ':' ? 10 : 9;
497 eServiceReference tmp(line+offs);
498 if (tmp.type != eServiceReference::idDVB)
500 eDebug("only DVB Bouquets supported");
503 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
505 std::string str = tmp.path;
506 unsigned int pos = str.rfind('/');
507 if ( pos != std::string::npos )
511 eDebug("Bouquet load failed.. no filename given..");
514 loadBouquet(tmp.path.c_str());
516 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], str.c_str());
524 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
529 else if (!strncmp(line, "#NAME ", 6))
530 bouquet.m_bouquet_name=line+6;
535 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
538 void eDVBDB::saveBouquet(const char *path)
543 void eDVBDB::loadBouquets()
545 loadBouquet("bouquets.tv");
546 loadBouquet("bouquets.radio");
547 // create default bouquets when missing
548 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
550 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
551 b.m_path = "userbouquet.favourites.tv";
552 b.m_bouquet_name = "Favourites (TV)";
554 eServiceReference ref;
555 memset(ref.data, 0, sizeof(ref.data));
559 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
560 eBouquet &parent = m_bouquets["bouquets.tv"];
561 parent.m_services.push_back(ref);
562 parent.flushChanges();
564 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
566 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
567 b.m_path = "userbouquet.favourites.radio";
568 b.m_bouquet_name = "Favourites (Radio)";
570 eServiceReference ref;
571 memset(ref.data, 0, sizeof(ref.data));
575 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
576 eBouquet &parent = m_bouquets["bouquets.radio"];
577 parent.m_services.push_back(ref);
578 parent.flushChanges();
582 void eDVBDB::saveBouquets()
598 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
602 ch.m_frontendParameters = feparm;
603 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
607 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
609 m_channels.erase(id);
613 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
615 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
616 if (i == m_channels.end())
621 parm = i->second.m_frontendParameters;
625 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
627 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
631 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
633 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
634 i = m_services.find(reference);
635 if (i == m_services.end())
644 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
646 std::string str = ref.path;
649 eDebug("getBouquet failed.. no path given!");
652 unsigned int pos = str.find("FROM BOUQUET \"");
653 if ( pos != std::string::npos )
655 str.erase(0, pos+14);
657 if ( pos != std::string::npos )
664 eDebug("getBouquet failed.. couldn't parse bouquet name");
667 std::map<std::string, eBouquet>::iterator i =
668 m_bouquets.find(str);
669 if (i == m_bouquets.end())
674 bouquet = &i->second;
678 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
680 if ( q && q->m_bouquet_name.length() )
681 query = new eDVBDBBouquetQuery(this, source, q);
683 query = new eDVBDBQuery(this, source, q);
687 DEFINE_REF(eDVBDBQueryBase);
689 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
690 :m_db(db), m_query(query), m_source(source)
694 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
696 ePtr<eDVBService> a_service, b_service;
698 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
700 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
702 if (m_db->getService(a, a_service))
704 if (m_db->getService(b, b_service))
710 case eDVBChannelQuery::tName:
711 return a_service->m_service_name_sort < b_service->m_service_name_sort;
712 case eDVBChannelQuery::tProvider:
713 return a_service->m_provider_name < b_service->m_provider_name;
714 case eDVBChannelQuery::tType:
715 return a.getServiceType() < b.getServiceType();
716 case eDVBChannelQuery::tBouquet:
718 case eDVBChannelQuery::tSatellitePosition:
719 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
726 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
727 :eDVBDBQueryBase(db, source, query)
729 m_cursor = m_db->m_services.begin();
732 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
734 while (m_cursor != m_db->m_services.end())
736 ref = m_cursor->first;
738 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
746 ref.type = eServiceReference::idInvalid;
751 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
752 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
756 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
758 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
759 std::list<eServiceReference> &list = bouquet.m_services;
760 while (m_cursor != list.end())
762 ref = *((eServiceReferenceDVB*)&(*m_cursor));
764 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
765 m_db->m_services.find(ref);
767 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
775 ref.type = eServiceReference::idInvalid;
780 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
782 static int decodeType(const std::string &type)
785 return eDVBChannelQuery::tName;
786 else if (type == "provider")
787 return eDVBChannelQuery::tProvider;
788 else if (type == "type")
789 return eDVBChannelQuery::tType;
790 else if (type == "bouquet")
791 return eDVBChannelQuery::tBouquet;
792 else if (type == "satellitePosition")
793 return eDVBChannelQuery::tSatellitePosition;
794 else if (type == "channelID")
795 return eDVBChannelQuery::tChannelID;
800 /* never, NEVER write a parser in C++! */
801 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
803 std::list<std::string>::const_iterator end_of_exp;
807 while (end_of_exp != end)
808 if (*end_of_exp == ")")
813 if (end_of_exp == end)
815 eDebug("expression parse: end of expression while searching for closing brace");
821 int r = parseExpression(res, begin, end_of_exp);
826 /* we had only one sub expression */
827 if (end_of_exp == end)
829 // eDebug("only one sub expression");
833 /* otherwise we have an operator here.. */
835 ePtr<eDVBChannelQuery> r2 = res;
836 res = new eDVBChannelQuery();
842 if (*end_of_exp == "||")
843 res->m_type = eDVBChannelQuery::tOR;
844 else if (*end_of_exp == "&&")
845 res->m_type = eDVBChannelQuery::tAND;
848 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
855 return parseExpression(res->m_p2, end_of_exp, end);
858 // "begin" <op> "end"
859 std::string type, op, val;
861 res = new eDVBChannelQuery();
879 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
888 eDebug("malformed query: missing stuff");
893 res->m_type = decodeType(type);
895 if (res->m_type == -1)
897 eDebug("malformed query: invalid type %s", type.c_str());
908 eDebug("invalid operator %s", op.c_str());
914 res->m_int = atoi(val.c_str());
915 // res->m_channelid = eDVBChannelID(val);
920 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
922 std::list<std::string> tokens;
924 std::string current_token;
925 std::string bouquet_name;
927 // eDebug("splitting %s....", query.c_str());
929 const char *splitchars="()";
930 int quotemode = 0, lastsplit = 0, lastalnum = 0;
931 while (i <= query.size())
933 int c = (i < query.size()) ? query[i] : ' ';
936 int issplit = !!strchr(splitchars, c);
937 int isaln = isalnum(c);
938 int iswhite = c == ' ';
939 int isquot = c == '\"';
943 iswhite = issplit = 0;
947 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
949 if (current_token.size())
950 tokens.push_back(current_token);
951 current_token.clear();
954 if (!(iswhite || isquot))
958 quotemode = !quotemode;
963 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
965 // printf("%s\n", a->c_str());
968 int sort = eDVBChannelQuery::tName;
969 /* check for "ORDER BY ..." */
971 while (tokens.size() > 2)
973 std::list<std::string>::iterator it = tokens.end();
981 sort = decodeType(*it);
982 tokens.pop_back(); // ...
983 tokens.pop_back(); // BY
984 tokens.pop_back(); // ORDER
988 else if (*it == "FROM")
991 if (*it == "BOUQUET")
995 tokens.pop_back(); // ...
996 tokens.pop_back(); // FROM
997 tokens.pop_back(); // BOUQUET
1006 eWarning("ORDER BY .. string invalid.");
1011 // eDebug("sort by %d", sort);
1013 /* now we recursivly parse that. */
1014 int r = parseExpression(res, tokens.begin(), tokens.end());
1019 res->m_bouquet_name = bouquet_name;
1022 // eDebug("return: %d", r);
1026 DEFINE_REF(eDVBChannelQuery);