2 #include <lib/dvb/db.h>
3 #include <lib/dvb/dvb.h>
4 #include <lib/dvb/frontend.h>
5 #include <lib/dvb/epgcache.h>
6 #include <lib/base/eerror.h>
7 #include <lib/base/estring.h>
8 #include <dvbsi++/service_description_section.h>
9 #include <dvbsi++/descriptor_tag.h>
10 #include <dvbsi++/service_descriptor.h>
11 #include <dvbsi++/satellite_delivery_system_descriptor.h>
13 DEFINE_REF(eDVBService);
15 RESULT eBouquet::addService(const eServiceReference &ref, eServiceReference before)
18 std::find(m_services.begin(), m_services.end(), ref);
19 if ( it != m_services.end() )
23 it = std::find(m_services.begin(), m_services.end(), before);
24 m_services.insert(it, ref);
27 m_services.push_back(ref);
31 RESULT eBouquet::removeService(const eServiceReference &ref)
34 std::find(m_services.begin(), m_services.end(), ref);
35 if ( it == m_services.end() )
41 RESULT eBouquet::moveService(const eServiceReference &ref, unsigned int pos)
43 if ( pos < 0 || pos >= m_services.size() )
46 list::iterator source=m_services.end();
47 list::iterator dest=m_services.end();
49 for (list::iterator it(m_services.begin()); it != m_services.end(); ++it)
51 if (dest == m_services.end() && !--pos)
58 if (dest != m_services.end() && source != m_services.end())
61 if (dest == m_services.end() || source == m_services.end() || source == dest)
63 while (source != dest)
66 std::iter_swap(source++, source);
68 std::iter_swap(source--, source);
73 RESULT eBouquet::flushChanges()
75 FILE *f=fopen((CONFIGDIR"/enigma2/"+m_filename).c_str(), "w");
78 if ( fprintf(f, "#NAME %s\r\n", m_bouquet_name.c_str()) < 0 )
80 for (list::iterator i(m_services.begin()); i != m_services.end(); ++i)
82 eServiceReference tmp = *i;
83 std::string str = tmp.path;
84 if ( (i->flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
86 unsigned int p1 = str.find("FROM BOUQUET \"");
87 if (p1 == std::string::npos)
89 eDebug("doof... kaputt");
94 if (p1 == std::string::npos)
96 eDebug("doof2... kaputt");
102 if ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
104 if ( i->name.length() )
105 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
112 eDebug("couldn't write file %s", m_filename.c_str());
116 RESULT eBouquet::setListName(const std::string &name)
118 m_bouquet_name = name;
122 eDVBService::eDVBService()
123 :m_cache(0), m_flags(0)
127 eDVBService::~eDVBService()
132 eDVBService &eDVBService::operator=(const eDVBService &s)
134 m_service_name = s.m_service_name;
135 m_service_name_sort = s.m_service_name_sort;
136 m_provider_name = s.m_provider_name;
139 copyCache(s.m_cache);
143 void eDVBService::genSortName()
145 m_service_name_sort = removeDVBChars(m_service_name);
146 makeUpper(m_service_name_sort);
147 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
148 m_service_name_sort.erase(0, 1);
150 /* put unnamed services at the end, not at the beginning. */
151 if (m_service_name_sort.empty())
152 m_service_name_sort = "\xFF";
155 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
157 if (!ref.name.empty())
158 name = ref.name; // use renamed service name..
159 else if (!m_service_name.empty())
160 name = m_service_name;
166 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
168 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
171 bool eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
173 ePtr<eDVBResourceManager> res_mgr;
174 if ( eDVBResourceManager::getInstance( res_mgr ) )
175 eDebug("isPlayble... no res manager!!");
178 eDVBChannelID chid, chid_ignore;
179 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
180 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
181 return res_mgr->canAllocateChannel(chid, chid_ignore);
186 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
189 switch (query.m_type)
191 case eDVBChannelQuery::tName:
192 res = m_service_name_sort.find(query.m_string) != std::string::npos;
194 case eDVBChannelQuery::tProvider:
195 res = m_provider_name.find(query.m_string) != std::string::npos;
197 case eDVBChannelQuery::tType:
198 res = ref.getServiceType() == query.m_int;
200 case eDVBChannelQuery::tBouquet:
203 case eDVBChannelQuery::tSatellitePosition:
204 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
206 case eDVBChannelQuery::tFlags:
207 res = (m_flags & query.m_int) == query.m_int;
209 case eDVBChannelQuery::tChannelID:
212 ref.getChannelID(chid);
213 res = chid == query.m_channelid;
216 case eDVBChannelQuery::tAND:
217 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
219 case eDVBChannelQuery::tOR:
220 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
222 case eDVBChannelQuery::tAny:
233 bool eDVBService::cacheEmpty()
236 for (int i=0; i < cacheMax; ++i)
237 if (m_cache[i] != -1)
242 void eDVBService::initCache()
244 m_cache = new int[cacheMax];
245 memset(m_cache, -1, sizeof(int) * cacheMax);
248 void eDVBService::copyCache(int *source)
253 m_cache = new int[cacheMax];
254 memcpy(m_cache, source, cacheMax * sizeof(int));
263 int eDVBService::getCacheEntry(cacheID id)
265 if (id >= cacheMax || !m_cache)
270 void eDVBService::setCacheEntry(cacheID id, int pid)
280 /* THIS CODE IS BAD. it should be replaced by somethine better. */
281 void eDVBDB::reloadServicelist()
283 eDebug("---- opening lame channel db");
284 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
288 if ( !stat("lamedb", &s) )
290 if ( !stat(CONFIGDIR"/enigma2", &s) )
292 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
299 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
301 eDebug("not a servicefile");
305 eDebug("reading services");
306 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
308 eDebug("services invalid, no transponders");
313 // clear all transponders
317 if (!fgets(line, 256, f))
319 if (!strcmp(line, "end\n"))
321 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
322 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
323 if (original_network_id == -1)
325 eDVBChannelID channelid = eDVBChannelID(
326 eDVBNamespace(dvb_namespace),
327 eTransportStreamID(transport_stream_id),
328 eOriginalNetworkID(original_network_id));
330 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
334 if (!strcmp(line, "/\n"))
338 eDVBFrontendParametersSatellite sat;
339 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
340 system=eDVBFrontendParametersSatellite::System::DVB_S,
341 modulation=eDVBFrontendParametersSatellite::Modulation::QPSK,
342 rolloff=eDVBFrontendParametersSatellite::RollOff::alpha_auto;
343 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &system, &modulation, &rolloff);
344 sat.frequency = frequency;
345 sat.symbol_rate = symbol_rate;
346 sat.polarisation = polarisation;
348 sat.orbital_position =
349 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
350 sat.inversion = inversion;
352 sat.modulation = modulation;
353 sat.roll_off = rolloff;
354 feparm->setDVBS(sat);
355 } else if (line[1]=='t')
357 eDVBFrontendParametersTerrestrial ter;
358 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
359 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);
360 ter.frequency = frequency;
361 ter.bandwidth = bandwidth;
362 ter.code_rate_HP = code_rate_HP;
363 ter.code_rate_LP = code_rate_LP;
364 ter.modulation = modulation;
365 ter.transmission_mode = transmission_mode;
366 ter.guard_interval = guard_interval;
367 ter.hierarchy = hierarchy;
368 ter.inversion = inversion;
369 feparm->setDVBT(ter);
370 } else if (line[1]=='c')
372 eDVBFrontendParametersCable cab;
373 int frequency, symbol_rate,
374 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
375 modulation=eDVBFrontendParametersCable::Modulation::Auto,
376 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
377 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
378 cab.frequency = frequency;
379 cab.fec_inner = fec_inner;
380 cab.inversion = inversion;
381 cab.symbol_rate = symbol_rate;
382 cab.modulation = modulation;
383 feparm->setDVBC(cab);
386 addChannelToList(channelid, feparm);
389 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
391 eDebug("services invalid, no services");
395 // clear all services
401 if (!fgets(line, 256, f))
403 if (!strcmp(line, "end\n"))
406 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
407 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
408 if (service_number == -1)
410 ePtr<eDVBService> s = new eDVBService;
411 eServiceReferenceDVB ref =
412 eServiceReferenceDVB(
413 eDVBNamespace(dvb_namespace),
414 eTransportStreamID(transport_stream_id),
415 eOriginalNetworkID(original_network_id),
416 eServiceID(service_id),
421 line[strlen(line)-1]=0;
423 s->m_service_name = line;
428 line[strlen(line)-1]=0;
429 std::string str=line;
431 if (str[1]!=':') // old ... (only service_provider)
433 s->m_provider_name=line;
435 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
437 unsigned int c=str.find(',');
440 if (c == std::string::npos)
446 v=str.substr(2, c-2);
449 // eDebug("%c ... %s", p, v.c_str());
451 s->m_provider_name=v;
454 sscanf(v.c_str(), "%x", &s->m_flags);
458 sscanf(v.c_str(), "%02d%x", &cid, &val);
459 s->setCacheEntry((eDVBService::cacheID)cid,val);
463 sscanf(v.c_str(), "%04x", &val);
464 s->m_ca.push_front((uint16_t)val);
470 eDebug("loaded %d services", count);
475 void eDVBDB::saveServicelist()
477 eDebug("---- saving lame channel db");
478 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
479 int channels=0, services=0;
481 eFatal("couldn't save lame channel db!");
482 fprintf(f, "eDVB services /3/\n");
483 fprintf(f, "transponders\n");
484 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
485 i != m_channels.end(); ++i)
487 const eDVBChannelID &chid = i->first;
488 const channel &ch = i->second;
490 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
491 chid.transport_stream_id.get(), chid.original_network_id.get());
492 eDVBFrontendParametersSatellite sat;
493 eDVBFrontendParametersTerrestrial ter;
494 eDVBFrontendParametersCable cab;
495 if (!ch.m_frontendParameters->getDVBS(sat))
497 if (sat.system == eDVBFrontendParametersSatellite::System::DVB_S2)
499 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
500 sat.frequency, sat.symbol_rate,
501 sat.polarisation, sat.fec,
502 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
510 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
511 sat.frequency, sat.symbol_rate,
512 sat.polarisation, sat.fec,
513 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
517 if (!ch.m_frontendParameters->getDVBT(ter))
519 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
520 ter.frequency, ter.bandwidth, ter.code_rate_HP,
521 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
522 ter.guard_interval, ter.hierarchy, ter.inversion);
524 if (!ch.m_frontendParameters->getDVBC(cab))
526 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
527 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
532 fprintf(f, "end\nservices\n");
534 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
535 i != m_services.end(); ++i)
537 const eServiceReferenceDVB &s = i->first;
538 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
539 s.getServiceID().get(), s.getDVBNamespace().get(),
540 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
544 fprintf(f, "%s\n", i->second->m_service_name.c_str());
546 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
549 for (int x=0; x < eDVBService::cacheMax; ++x)
551 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
553 fprintf(f, ",c:%02d%04x", x, entry);
556 // write cached ca pids
557 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
558 ca != i->second->m_ca.end(); ++ca)
559 fprintf(f, ",C:%04x", *ca);
561 if (i->second->m_flags)
562 fprintf(f, ",f:%x", i->second->m_flags);
567 fprintf(f, "end\nHave a lot of bugs!\n");
568 eDebug("saved %d channels and %d services!", channels, services);
572 void eDVBDB::loadBouquet(const char *path)
574 std::string bouquet_name = path;
575 if (!bouquet_name.length())
577 eDebug("Bouquet load failed.. no path given..");
580 unsigned int pos = bouquet_name.rfind('/');
581 if ( pos != std::string::npos )
582 bouquet_name.erase(0, pos+1);
583 if (bouquet_name.empty())
585 eDebug("Bouquet load failed.. no filename given..");
588 eBouquet &bouquet = m_bouquets[bouquet_name];
589 bouquet.m_filename = bouquet_name;
590 std::list<eServiceReference> &list = bouquet.m_services;
593 std::string p = CONFIGDIR"/enigma2/";
595 eDebug("loading bouquet... %s", p.c_str());
596 FILE *fp=fopen(p.c_str(), "rt");
601 if ( !stat(path, &s) )
603 rename(path, p.c_str() );
607 eDebug("failed to open.");
608 if ( strstr(path, "bouquets.tv") )
610 eDebug("recreate bouquets.tv");
611 bouquet.m_bouquet_name="Bouquets (TV)";
612 bouquet.flushChanges();
614 else if ( strstr(path, "bouquets.radio") )
616 eDebug("recreate bouquets.radio");
617 bouquet.m_bouquet_name="Bouquets (Radio)";
618 bouquet.flushChanges();
623 bool read_descr=false;
624 eServiceReference *e = NULL;
627 if (!fgets(line, 256, fp))
629 line[strlen(line)-1]=0;
630 if (strlen(line) && line[strlen(line)-1]=='\r')
631 line[strlen(line)-1]=0;
636 if (!strncmp(line, "#SERVICE ", 9) || !strncmp(line, "#SERVICE: ", 10))
638 int offs = line[8] == ':' ? 10 : 9;
639 eServiceReference tmp(line+offs);
640 if (tmp.type != eServiceReference::idDVB)
642 eDebug("only DVB Bouquets supported");
645 if ( (tmp.flags&eServiceReference::flagDirectory) == eServiceReference::flagDirectory )
647 unsigned int pos = tmp.path.rfind('/');
648 if ( pos != std::string::npos )
649 tmp.path.erase(0, pos+1);
650 if (tmp.path.empty())
652 eDebug("Bouquet load failed.. no filename given..");
655 loadBouquet(tmp.path.c_str());
657 snprintf(buf, 256, "(type == %d) FROM BOUQUET \"%s\" ORDER BY bouquet", tmp.data[0], tmp.path.c_str());
665 else if (read_descr && !strncmp(line, "#DESCRIPTION ", 13))
670 else if (!strncmp(line, "#NAME ", 6))
671 bouquet.m_bouquet_name=line+6;
676 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
679 void eDVBDB::reloadBouquets()
682 loadBouquet("bouquets.tv");
683 loadBouquet("bouquets.radio");
684 // create default bouquets when missing
685 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
687 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
688 b.m_filename = "userbouquet.favourites.tv";
689 b.m_bouquet_name = "Favourites (TV)";
691 eServiceReference ref;
692 memset(ref.data, 0, sizeof(ref.data));
696 ref.path="(type == 1) FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
697 eBouquet &parent = m_bouquets["bouquets.tv"];
698 parent.m_services.push_back(ref);
699 parent.flushChanges();
701 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
703 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
704 b.m_filename = "userbouquet.favourites.radio";
705 b.m_bouquet_name = "Favourites (Radio)";
707 eServiceReference ref;
708 memset(ref.data, 0, sizeof(ref.data));
712 ref.path="(type == 2) FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
713 eBouquet &parent = m_bouquets["bouquets.radio"];
714 parent.m_services.push_back(ref);
715 parent.flushChanges();
719 eDVBDB *eDVBDB::instance;
732 RESULT eDVBDB::removeService(const eServiceReference &ref)
734 if (ref.type == eServiceReference::idDVB)
736 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
737 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
738 if (it != m_services.end())
740 m_services.erase(it);
747 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
749 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
752 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
756 eTransportStreamID eTsid;
757 eOriginalNetworkID eOnid;
758 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
759 std::set<eDVBChannelID> removed_chids;
760 while (it != m_channels.end())
762 const eDVBChannelID &ch = it->first;
765 it->second.m_frontendParameters->getSystem(system);
766 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
768 eDVBFrontendParametersSatellite sat;
769 it->second.m_frontendParameters->getDVBS(sat);
770 if ((unsigned int)sat.orbital_position != orbpos)
773 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
775 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
777 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
781 eDebug("remove %08x %04x %04x",
782 ch.dvbnamespace.get(),
783 ch.original_network_id.get(),
784 ch.transport_stream_id.get());
785 removed_chids.insert(it->first);
786 m_channels.erase(it++);
791 if (!removed_chids.empty())
793 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
794 while(service != m_services.end())
797 service->first.getChannelID(chid);
798 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
799 if (it != removed_chids.end())
800 m_services.erase(service++);
809 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
811 if (ref.type == eServiceReference::idDVB)
813 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
814 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
815 if (it != m_services.end())
816 it->second->m_flags |= ~flagmask;
822 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
824 if (ref.type == eServiceReference::idDVB)
826 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
827 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
828 if (it != m_services.end())
829 it->second->m_flags &= ~flagmask;
835 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
837 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
840 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
843 eTransportStreamID eTsid;
844 eOriginalNetworkID eOnid;
845 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
846 std::set<eDVBChannelID> removed_chids;
847 while (it != m_channels.end())
849 const eDVBChannelID &ch = it->first;
852 it->second.m_frontendParameters->getSystem(system);
853 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
855 eDVBFrontendParametersSatellite sat;
856 it->second.m_frontendParameters->getDVBS(sat);
857 if ((unsigned int)sat.orbital_position != orbpos)
860 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
862 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
864 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
867 removed_chids.insert(it->first);
870 if (!removed_chids.empty())
872 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
873 while(service != m_services.end())
876 service->first.getChannelID(chid);
877 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
878 if (it != removed_chids.end())
879 service->second->m_flags &= ~flagmask;
886 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
890 ch.m_frontendParameters = feparm;
891 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
895 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
897 m_channels.erase(id);
901 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
903 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
904 if (i == m_channels.end())
909 parm = i->second.m_frontendParameters;
913 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
915 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
916 if (it == m_services.end())
917 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
921 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
923 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
924 i = m_services.find(reference);
925 if (i == m_services.end())
934 RESULT eDVBDB::flush()
940 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
942 std::string str = ref.path;
945 eDebug("getBouquet failed.. no path given!");
948 unsigned int pos = str.find("FROM BOUQUET \"");
949 if ( pos != std::string::npos )
951 str.erase(0, pos+14);
953 if ( pos != std::string::npos )
960 eDebug("getBouquet failed.. couldn't parse bouquet name");
963 std::map<std::string, eBouquet>::iterator i =
964 m_bouquets.find(str);
965 if (i == m_bouquets.end())
970 bouquet = &i->second;
974 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
976 if ( source.path.find("FROM") != std::string::npos )
978 if ( source.path.find("BOUQUET") != std::string::npos )
979 query = new eDVBDBBouquetQuery(this, source, q);
980 else if ( source.path.find("SATELLITES") != std::string::npos )
981 query = new eDVBDBSatellitesQuery(this, source, q);
982 else if ( source.path.find("PROVIDERS") != std::string::npos )
983 query = new eDVBDBProvidersQuery(this, source, q);
985 eFatal("invalid query %s", source.toString().c_str());
988 query = new eDVBDBQuery(this, source, q);
992 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
995 eTransportStreamID Tsid(tsid);
996 eOriginalNetworkID Onid(onid);
997 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
998 sit != m_services.end(); ++sit)
1000 if (sit->first.getTransportStreamID() == Tsid &&
1001 sit->first.getOriginalNetworkID() == Onid &&
1002 sit->first.getServiceID() == Sid)
1005 return eServiceReference();
1008 DEFINE_REF(eDVBDBQueryBase);
1010 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1011 :m_db(db), m_query(query), m_source(source)
1015 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1017 ePtr<eDVBService> a_service, b_service;
1018 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1020 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1022 if (a.name.empty() && m_db->getService(a, a_service))
1024 if (b.name.empty() && m_db->getService(b, b_service))
1030 case eDVBChannelQuery::tName:
1034 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1037 std::string str = b.name;
1039 return a_service->m_service_name_sort < str;
1044 std::string str = a.name;
1046 return str < b_service->m_service_name_sort;
1050 std::string aa = a.name, bb = b.name;
1055 case eDVBChannelQuery::tProvider:
1056 return a_service->m_provider_name < b_service->m_provider_name;
1057 case eDVBChannelQuery::tType:
1058 return a.getServiceType() < b.getServiceType();
1059 case eDVBChannelQuery::tBouquet:
1061 case eDVBChannelQuery::tSatellitePosition:
1062 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1069 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1070 :eDVBDBQueryBase(db, source, query)
1072 m_cursor = m_db->m_services.begin();
1075 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1077 while (m_cursor != m_db->m_services.end())
1079 ref = m_cursor->first;
1081 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
1089 ref.type = eServiceReference::idInvalid;
1094 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1095 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1099 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1101 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1102 std::list<eServiceReference> &list = bouquet.m_services;
1103 while (m_cursor != list.end())
1105 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1107 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1108 m_db->m_services.find(ref);
1110 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
1118 ref.type = eServiceReference::idInvalid;
1123 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1124 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1128 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1130 if (m_cursor != m_list.end())
1135 ref.type = eServiceReference::idInvalid;
1139 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1141 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1143 int x = (a.getDVBNamespace().get() >> 16);
1144 int y = (b.getDVBNamespace().get() >> 16);
1151 std::string aa = a.name, bb = b.name;
1157 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1158 :eDVBDBListQuery(db, source, query)
1160 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1161 it != m_db->m_services.end(); ++it)
1163 int res = it->second->checkFilter(it->first, *query);
1166 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1168 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1169 if ( (i->getDVBNamespace().get()&0xFFFF0000) == dvbnamespace )
1176 eServiceReferenceDVB ref;
1177 ref.setDVBNamespace(dvbnamespace);
1178 ref.flags=eServiceReference::flagDirectory;
1180 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1182 ref.path=buf+source.path;
1183 unsigned int pos=ref.path.find("FROM");
1184 ref.path.erase(pos);
1185 ref.path+="ORDER BY name";
1186 // eDebug("ref.path now %s", ref.path.c_str());
1187 m_list.push_back(ref);
1189 ref.path=buf+source.path;
1190 pos=ref.path.find("FROM");
1191 ref.path.erase(pos+5);
1192 ref.path+="PROVIDERS ORDER BY name";
1193 // eDebug("ref.path now %s", ref.path.c_str());
1194 m_list.push_back(ref);
1196 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1197 ref.path=buf+source.path;
1198 pos=ref.path.find("FROM");
1199 ref.path.erase(pos);
1200 ref.path+="ORDER BY name";
1201 // eDebug("ref.path now %s", ref.path.c_str());
1202 m_list.push_back(ref);
1206 m_cursor=m_list.begin();
1209 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1210 :eDVBDBListQuery(db, source, query)
1212 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1213 it != m_db->m_services.end(); ++it)
1215 int res = it->second->checkFilter(it->first, *query);
1220 const char *provider_name = it->second->m_provider_name.length() ?
1221 it->second->m_provider_name.c_str() :
1224 for (std::list<eServiceReferenceDVB>::iterator i(m_list.begin()); i != m_list.end(); ++i)
1225 if (i->name == provider_name)
1232 eServiceReferenceDVB ref;
1234 ref.name=provider_name;
1235 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1236 ref.path=buf+source.path;
1237 unsigned int pos = ref.path.find("FROM");
1238 ref.flags=eServiceReference::flagDirectory;
1239 ref.path.erase(pos);
1240 ref.path+="ORDER BY name";
1241 // eDebug("ref.path now %s", ref.path.c_str());
1242 m_list.push_back(ref);
1246 m_cursor=m_list.begin();
1249 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1251 static int decodeType(const std::string &type)
1254 return eDVBChannelQuery::tName;
1255 else if (type == "provider")
1256 return eDVBChannelQuery::tProvider;
1257 else if (type == "type")
1258 return eDVBChannelQuery::tType;
1259 else if (type == "bouquet")
1260 return eDVBChannelQuery::tBouquet;
1261 else if (type == "satellitePosition")
1262 return eDVBChannelQuery::tSatellitePosition;
1263 else if (type == "channelID")
1264 return eDVBChannelQuery::tChannelID;
1265 else if (type == "flags")
1266 return eDVBChannelQuery::tFlags;
1271 /* never, NEVER write a parser in C++! */
1272 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1274 std::list<std::string>::const_iterator end_of_exp;
1278 eDebug("empty expression!");
1285 while (end_of_exp != end)
1286 if (*end_of_exp == ")")
1291 if (end_of_exp == end)
1293 eDebug("expression parse: end of expression while searching for closing brace");
1298 // begin..end_of_exp
1299 int r = parseExpression(res, begin, end_of_exp);
1304 /* we had only one sub expression */
1305 if (end_of_exp == end)
1307 // eDebug("only one sub expression");
1311 /* otherwise we have an operator here.. */
1313 ePtr<eDVBChannelQuery> r2 = res;
1314 res = new eDVBChannelQuery();
1320 if (*end_of_exp == "||")
1321 res->m_type = eDVBChannelQuery::tOR;
1322 else if (*end_of_exp == "&&")
1323 res->m_type = eDVBChannelQuery::tAND;
1326 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1333 return parseExpression(res->m_p2, end_of_exp, end);
1336 // "begin" <op> "end"
1337 std::string type, op, val;
1339 res = new eDVBChannelQuery();
1343 while (begin != end)
1357 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1366 eDebug("malformed query: missing stuff");
1371 res->m_type = decodeType(type);
1373 if (res->m_type == -1)
1375 eDebug("malformed query: invalid type %s", type.c_str());
1382 else if (op == "!=")
1386 eDebug("invalid operator %s", op.c_str());
1391 res->m_string = val;
1392 res->m_int = atoi(val.c_str());
1393 // res->m_channelid = eDVBChannelID(val);
1398 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1400 std::list<std::string> tokens;
1402 std::string current_token;
1403 std::string bouquet_name;
1405 // eDebug("splitting %s....", query.c_str());
1407 const char *splitchars="()";
1408 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1409 while (i <= query.size())
1411 int c = (i < query.size()) ? query[i] : ' ';
1414 int issplit = !!strchr(splitchars, c);
1415 int isaln = isalnum(c);
1416 int iswhite = c == ' ';
1417 int isquot = c == '\"';
1421 iswhite = issplit = 0;
1425 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1427 if (current_token.size())
1428 tokens.push_back(current_token);
1429 current_token.clear();
1432 if (!(iswhite || isquot))
1436 quotemode = !quotemode;
1437 lastsplit = issplit;
1441 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1443 // printf("%s\n", a->c_str());
1446 int sort = eDVBChannelQuery::tName;
1447 /* check for "ORDER BY ..." */
1449 std::list<std::string>::iterator it = tokens.begin();
1450 while (it != tokens.end())
1455 if (it != tokens.end() && *it == "BY")
1458 sort = decodeType(*it);
1463 else if (*it == "FROM")
1466 if (it != tokens.end() && *it == "BOUQUET")
1472 else if (it != tokens.end() && *it == "SATELLITES")
1474 else if (it != tokens.end() && *it == "PROVIDERS")
1478 eDebug("FROM unknown %s", (*it).c_str());
1488 eWarning("ORDER BY .. string invalid.");
1493 // eDebug("sort by %d", sort);
1495 /* now we recursivly parse that. */
1496 int r = parseExpression(res, tokens.begin(), tokens.end());
1498 /* we have an empty (but valid!) expression */
1501 res = new eDVBChannelQuery();
1503 res->m_type = eDVBChannelQuery::tAny;
1509 res->m_bouquet_name = bouquet_name;
1512 // eDebug("return: %d", r);
1516 DEFINE_REF(eDVBChannelQuery);