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 ( fprintf(f, "#SERVICE %s\r\n", tmp.toString().c_str()) < 0 )
86 if ( i->name.length() )
87 if ( fprintf(f, "#DESCRIPTION %s\r\n", i->name.c_str()) < 0 )
94 eDebug("couldn't write file %s", m_filename.c_str());
98 RESULT eBouquet::setListName(const std::string &name)
100 m_bouquet_name = name;
104 eDVBService::eDVBService()
105 :m_cache(0), m_flags(0)
109 eDVBService::~eDVBService()
114 eDVBService &eDVBService::operator=(const eDVBService &s)
116 m_service_name = s.m_service_name;
117 m_service_name_sort = s.m_service_name_sort;
118 m_provider_name = s.m_provider_name;
121 copyCache(s.m_cache);
125 void eDVBService::genSortName()
127 m_service_name_sort = removeDVBChars(m_service_name);
128 makeUpper(m_service_name_sort);
129 while ((!m_service_name_sort.empty()) && m_service_name_sort[0] == ' ')
130 m_service_name_sort.erase(0, 1);
132 /* put unnamed services at the end, not at the beginning. */
133 if (m_service_name_sort.empty())
134 m_service_name_sort = "\xFF";
137 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
139 if (!ref.name.empty())
140 name = ref.name; // use renamed service name..
141 else if (!m_service_name.empty())
142 name = m_service_name;
148 RESULT eDVBService::getEvent(const eServiceReference &ref, ePtr<eServiceEvent> &ptr, time_t start_time)
150 return eEPGCache::getInstance()->lookupEventTime(ref, start_time, ptr);
153 int eDVBService::isPlayable(const eServiceReference &ref, const eServiceReference &ignore)
155 ePtr<eDVBResourceManager> res_mgr;
156 if ( eDVBResourceManager::getInstance( res_mgr ) )
157 eDebug("isPlayble... no res manager!!");
160 eDVBChannelID chid, chid_ignore;
161 ((const eServiceReferenceDVB&)ref).getChannelID(chid);
162 ((const eServiceReferenceDVB&)ignore).getChannelID(chid_ignore);
163 return res_mgr->canAllocateChannel(chid, chid_ignore);
168 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
171 switch (query.m_type)
173 case eDVBChannelQuery::tName:
174 res = m_service_name_sort.find(query.m_string) != std::string::npos;
176 case eDVBChannelQuery::tProvider:
177 res = m_provider_name.find(query.m_string) != std::string::npos;
179 case eDVBChannelQuery::tType:
180 res = ref.getServiceType() == query.m_int;
182 case eDVBChannelQuery::tBouquet:
185 case eDVBChannelQuery::tSatellitePosition:
186 res = ((unsigned int)ref.getDVBNamespace().get())>>16 == (unsigned int)query.m_int;
188 case eDVBChannelQuery::tFlags:
189 res = (m_flags & query.m_int) == query.m_int;
191 case eDVBChannelQuery::tChannelID:
194 ref.getChannelID(chid);
195 res = chid == query.m_channelid;
198 case eDVBChannelQuery::tAND:
199 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
201 case eDVBChannelQuery::tOR:
202 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
204 case eDVBChannelQuery::tAny:
215 bool eDVBService::cacheEmpty()
218 for (int i=0; i < cacheMax; ++i)
219 if (m_cache[i] != -1)
224 void eDVBService::initCache()
226 m_cache = new int[cacheMax];
227 memset(m_cache, -1, sizeof(int) * cacheMax);
230 void eDVBService::copyCache(int *source)
235 m_cache = new int[cacheMax];
236 memcpy(m_cache, source, cacheMax * sizeof(int));
245 int eDVBService::getCacheEntry(cacheID id)
247 if (id >= cacheMax || !m_cache)
252 void eDVBService::setCacheEntry(cacheID id, int pid)
262 /* THIS CODE IS BAD. it should be replaced by somethine better. */
263 void eDVBDB::reloadServicelist()
265 eDebug("---- opening lame channel db");
266 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "rt");
270 if ( !stat("lamedb", &s) )
272 if ( !stat(CONFIGDIR"/enigma2", &s) )
274 rename("lamedb", CONFIGDIR"/enigma2/lamedb" );
281 if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
283 eDebug("not a servicefile");
287 eDebug("reading services");
288 if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
290 eDebug("services invalid, no transponders");
295 // clear all transponders
299 if (!fgets(line, 256, f))
301 if (!strcmp(line, "end\n"))
303 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
304 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
305 if (original_network_id == -1)
307 eDVBChannelID channelid = eDVBChannelID(
308 eDVBNamespace(dvb_namespace),
309 eTransportStreamID(transport_stream_id),
310 eOriginalNetworkID(original_network_id));
312 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
316 if (!strcmp(line, "/\n"))
320 eDVBFrontendParametersSatellite sat;
321 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion,
322 system=eDVBFrontendParametersSatellite::System::DVB_S,
323 modulation=eDVBFrontendParametersSatellite::Modulation::QPSK,
324 rolloff=eDVBFrontendParametersSatellite::RollOff::alpha_auto;
325 sscanf(line+2, "%d:%d:%d:%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion, &system, &modulation, &rolloff);
326 sat.frequency = frequency;
327 sat.symbol_rate = symbol_rate;
328 sat.polarisation = polarisation;
330 sat.orbital_position =
331 orbital_position < 0 ? orbital_position + 3600 : orbital_position;
332 sat.inversion = inversion;
334 sat.modulation = modulation;
335 sat.roll_off = rolloff;
336 feparm->setDVBS(sat);
337 } else if (line[1]=='t')
339 eDVBFrontendParametersTerrestrial ter;
340 int frequency, bandwidth, code_rate_HP, code_rate_LP, modulation, transmission_mode, guard_interval, hierarchy, inversion;
341 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);
342 ter.frequency = frequency;
343 ter.bandwidth = bandwidth;
344 ter.code_rate_HP = code_rate_HP;
345 ter.code_rate_LP = code_rate_LP;
346 ter.modulation = modulation;
347 ter.transmission_mode = transmission_mode;
348 ter.guard_interval = guard_interval;
349 ter.hierarchy = hierarchy;
350 ter.inversion = inversion;
351 feparm->setDVBT(ter);
352 } else if (line[1]=='c')
354 eDVBFrontendParametersCable cab;
355 int frequency, symbol_rate,
356 inversion=eDVBFrontendParametersCable::Inversion::Unknown,
357 modulation=eDVBFrontendParametersCable::Modulation::Auto,
358 fec_inner=eDVBFrontendParametersCable::FEC::fAuto;
359 sscanf(line+2, "%d:%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation, &fec_inner);
360 cab.frequency = frequency;
361 cab.fec_inner = fec_inner;
362 cab.inversion = inversion;
363 cab.symbol_rate = symbol_rate;
364 cab.modulation = modulation;
365 feparm->setDVBC(cab);
368 addChannelToList(channelid, feparm);
371 if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
373 eDebug("services invalid, no services");
377 // clear all services
383 if (!fgets(line, 256, f))
385 if (!strcmp(line, "end\n"))
388 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
389 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
390 if (service_number == -1)
392 ePtr<eDVBService> s = new eDVBService;
393 eServiceReferenceDVB ref =
394 eServiceReferenceDVB(
395 eDVBNamespace(dvb_namespace),
396 eTransportStreamID(transport_stream_id),
397 eOriginalNetworkID(original_network_id),
398 eServiceID(service_id),
403 line[strlen(line)-1]=0;
405 s->m_service_name = line;
410 line[strlen(line)-1]=0;
411 std::string str=line;
413 if (str[1]!=':') // old ... (only service_provider)
415 s->m_provider_name=line;
417 while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
419 unsigned int c=str.find(',');
422 if (c == std::string::npos)
428 v=str.substr(2, c-2);
431 // eDebug("%c ... %s", p, v.c_str());
433 s->m_provider_name=v;
436 sscanf(v.c_str(), "%x", &s->m_flags);
440 sscanf(v.c_str(), "%02d%x", &cid, &val);
441 s->setCacheEntry((eDVBService::cacheID)cid,val);
445 sscanf(v.c_str(), "%04x", &val);
446 s->m_ca.push_front((uint16_t)val);
452 eDebug("loaded %d services", count);
457 void eDVBDB::saveServicelist()
459 eDebug("---- saving lame channel db");
460 FILE *f=fopen(CONFIGDIR"/enigma2/lamedb", "w");
461 int channels=0, services=0;
463 eFatal("couldn't save lame channel db!");
464 fprintf(f, "eDVB services /3/\n");
465 fprintf(f, "transponders\n");
466 for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
467 i != m_channels.end(); ++i)
469 const eDVBChannelID &chid = i->first;
470 const channel &ch = i->second;
472 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
473 chid.transport_stream_id.get(), chid.original_network_id.get());
474 eDVBFrontendParametersSatellite sat;
475 eDVBFrontendParametersTerrestrial ter;
476 eDVBFrontendParametersCable cab;
477 if (!ch.m_frontendParameters->getDVBS(sat))
479 if (sat.system == eDVBFrontendParametersSatellite::System::DVB_S2)
481 fprintf(f, "\ts %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
482 sat.frequency, sat.symbol_rate,
483 sat.polarisation, sat.fec,
484 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
492 fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
493 sat.frequency, sat.symbol_rate,
494 sat.polarisation, sat.fec,
495 sat.orbital_position > 1800 ? sat.orbital_position - 3600 : sat.orbital_position,
499 if (!ch.m_frontendParameters->getDVBT(ter))
501 fprintf(f, "\tt %d:%d:%d:%d:%d:%d:%d:%d:%d\n",
502 ter.frequency, ter.bandwidth, ter.code_rate_HP,
503 ter.code_rate_LP, ter.modulation, ter.transmission_mode,
504 ter.guard_interval, ter.hierarchy, ter.inversion);
506 if (!ch.m_frontendParameters->getDVBC(cab))
508 fprintf(f, "\tc %d:%d:%d:%d:%d\n",
509 cab.frequency, cab.symbol_rate, cab.inversion, cab.modulation, cab.fec_inner);
514 fprintf(f, "end\nservices\n");
516 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
517 i != m_services.end(); ++i)
519 const eServiceReferenceDVB &s = i->first;
520 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
521 s.getServiceID().get(), s.getDVBNamespace().get(),
522 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
526 fprintf(f, "%s\n", i->second->m_service_name.c_str());
528 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
531 for (int x=0; x < eDVBService::cacheMax; ++x)
533 int entry = i->second->getCacheEntry((eDVBService::cacheID)x);
535 fprintf(f, ",c:%02d%04x", x, entry);
538 // write cached ca pids
539 for (CAID_LIST::const_iterator ca(i->second->m_ca.begin());
540 ca != i->second->m_ca.end(); ++ca)
541 fprintf(f, ",C:%04x", *ca);
543 if (i->second->m_flags)
544 fprintf(f, ",f:%x", i->second->m_flags);
549 fprintf(f, "end\nHave a lot of bugs!\n");
550 eDebug("saved %d channels and %d services!", channels, services);
554 void eDVBDB::loadBouquet(const char *path)
556 std::string bouquet_name = path;
557 if (!bouquet_name.length())
559 eDebug("Bouquet load failed.. no path given..");
562 unsigned int pos = bouquet_name.rfind('/');
563 if ( pos != std::string::npos )
564 bouquet_name.erase(0, pos+1);
565 if (bouquet_name.empty())
567 eDebug("Bouquet load failed.. no filename given..");
570 eBouquet &bouquet = m_bouquets[bouquet_name];
571 bouquet.m_filename = bouquet_name;
572 std::list<eServiceReference> &list = bouquet.m_services;
575 std::string p = CONFIGDIR"/enigma2/";
577 eDebug("loading bouquet... %s", p.c_str());
578 FILE *fp=fopen(p.c_str(), "rt");
583 if ( !stat(path, &s) )
585 rename(path, p.c_str() );
589 eDebug("failed to open.");
590 if ( strstr(path, "bouquets.tv") )
592 eDebug("recreate bouquets.tv");
593 bouquet.m_bouquet_name="Bouquets (TV)";
594 bouquet.flushChanges();
596 else if ( strstr(path, "bouquets.radio") )
598 eDebug("recreate bouquets.radio");
599 bouquet.m_bouquet_name="Bouquets (Radio)";
600 bouquet.flushChanges();
605 bool read_descr=false;
606 eServiceReference *e = NULL;
609 if (!fgets(line, 256, fp))
611 line[strlen(line)-1]=0;
612 if (strlen(line) && line[strlen(line)-1]=='\r')
613 line[strlen(line)-1]=0;
618 if (!strncmp(line, "#SERVICE", 8))
620 int offs = line[8] == ':' ? 10 : 9;
621 eServiceReference tmp(line+offs);
622 if (tmp.type != eServiceReference::idDVB)
624 eDebug("only DVB Bouquets supported");
627 if ( tmp.flags&eServiceReference::canDescent )
629 unsigned int pos = tmp.path.rfind('/');
631 std::string path = tmp.path;
632 if ( pos != std::string::npos )
633 path.erase(0, pos+1);
636 eDebug("Bouquet load failed.. no filename given..");
639 pos = path.find("FROM BOUQUET ");
640 if (pos != std::string::npos)
642 char endchr = path[pos+13];
645 eDebug("ignore invalid bouquet '%s' (only \" are allowed)",
646 tmp.toString().c_str());
649 char *beg = &path[pos+14];
650 char *end = strchr(beg, endchr);
651 path.assign(beg, end - beg);
655 snprintf(buf, 256, "FROM BOUQUET \"%s\" ORDER BY bouquet", path.c_str());
658 loadBouquet(path.c_str());
665 else if (read_descr && !strncmp(line, "#DESCRIPTION", 12))
667 int offs = line[12] == ':' ? 14 : 13;
671 else if (!strncmp(line, "#NAME ", 6))
672 bouquet.m_bouquet_name=line+6;
677 eDebug("%d entries in Bouquet %s", entries, bouquet_name.c_str());
680 void eDVBDB::reloadBouquets()
683 loadBouquet("bouquets.tv");
684 loadBouquet("bouquets.radio");
685 // create default bouquets when missing
686 if ( m_bouquets.find("userbouquet.favourites.tv") == m_bouquets.end() )
688 eBouquet &b = m_bouquets["userbouquet.favourites.tv"];
689 b.m_filename = "userbouquet.favourites.tv";
690 b.m_bouquet_name = "Favourites (TV)";
692 eServiceReference ref;
693 memset(ref.data, 0, sizeof(ref.data));
697 ref.path="FROM BOUQUET \"userbouquet.favourites.tv\" ORDER BY bouquet";
698 eBouquet &parent = m_bouquets["bouquets.tv"];
699 parent.m_services.push_back(ref);
700 parent.flushChanges();
702 if ( m_bouquets.find("userbouquet.favourites.radio") == m_bouquets.end() )
704 eBouquet &b = m_bouquets["userbouquet.favourites.radio"];
705 b.m_filename = "userbouquet.favourites.radio";
706 b.m_bouquet_name = "Favourites (Radio)";
708 eServiceReference ref;
709 memset(ref.data, 0, sizeof(ref.data));
713 ref.path="FROM BOUQUET \"userbouquet.favourites.radio\" ORDER BY bouquet";
714 eBouquet &parent = m_bouquets["bouquets.radio"];
715 parent.m_services.push_back(ref);
716 parent.flushChanges();
720 eDVBDB *eDVBDB::instance;
733 RESULT eDVBDB::removeService(const eServiceReference &ref)
735 if (ref.type == eServiceReference::idDVB)
737 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
738 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
739 if (it != m_services.end())
741 m_services.erase(it);
748 RESULT eDVBDB::removeServices(int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
750 return removeServices(eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
753 RESULT eDVBDB::removeServices(eDVBChannelID chid, unsigned int orbpos)
757 eTransportStreamID eTsid;
758 eOriginalNetworkID eOnid;
759 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
760 std::set<eDVBChannelID> removed_chids;
761 while (it != m_channels.end())
763 const eDVBChannelID &ch = it->first;
766 it->second.m_frontendParameters->getSystem(system);
767 if ( system == iDVBFrontend::feSatellite )
769 eDVBFrontendParametersSatellite sat;
770 it->second.m_frontendParameters->getDVBS(sat);
771 if ((unsigned int)sat.orbital_position != orbpos)
774 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
776 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
778 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
782 eDebug("remove %08x %04x %04x",
783 ch.dvbnamespace.get(),
784 ch.original_network_id.get(),
785 ch.transport_stream_id.get());
786 removed_chids.insert(it->first);
787 m_channels.erase(it++);
792 if (!removed_chids.empty())
794 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
795 while(service != m_services.end())
798 service->first.getChannelID(chid);
799 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
800 if (it != removed_chids.end())
801 m_services.erase(service++);
810 RESULT eDVBDB::addFlag(const eServiceReference &ref, unsigned int flagmask)
812 if (ref.type == eServiceReference::idDVB)
814 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
815 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
816 if (it != m_services.end())
817 it->second->m_flags |= ~flagmask;
823 RESULT eDVBDB::removeFlag(const eServiceReference &ref, unsigned int flagmask)
825 if (ref.type == eServiceReference::idDVB)
827 eServiceReferenceDVB &service = (eServiceReferenceDVB&)ref;
828 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(service));
829 if (it != m_services.end())
830 it->second->m_flags &= ~flagmask;
836 RESULT eDVBDB::removeFlags(unsigned int flagmask, int dvb_namespace, int tsid, int onid, unsigned int orb_pos)
838 return removeFlags(flagmask, eDVBChannelID(eDVBNamespace(dvb_namespace), eTransportStreamID(tsid), eOriginalNetworkID(onid)), orb_pos);
841 RESULT eDVBDB::removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orbpos)
844 eTransportStreamID eTsid;
845 eOriginalNetworkID eOnid;
846 std::map<eDVBChannelID, channel>::iterator it(m_channels.begin());
847 std::set<eDVBChannelID> removed_chids;
848 while (it != m_channels.end())
850 const eDVBChannelID &ch = it->first;
853 it->second.m_frontendParameters->getSystem(system);
854 if ( orbpos != 0xFFFFFFFF && system == iDVBFrontend::feSatellite )
856 eDVBFrontendParametersSatellite sat;
857 it->second.m_frontendParameters->getDVBS(sat);
858 if ((unsigned int)sat.orbital_position != orbpos)
861 if ( remove && chid.dvbnamespace != eNs && chid.dvbnamespace != ch.dvbnamespace )
863 if ( remove && chid.original_network_id != eOnid && chid.original_network_id != ch.original_network_id )
865 if ( remove && chid.transport_stream_id != eTsid && chid.transport_stream_id != ch.transport_stream_id )
868 removed_chids.insert(it->first);
871 if (!removed_chids.empty())
873 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator service(m_services.begin());
874 while(service != m_services.end())
877 service->first.getChannelID(chid);
878 std::set<eDVBChannelID>::iterator it(removed_chids.find(chid));
879 if (it != removed_chids.end())
880 service->second->m_flags &= ~flagmask;
887 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
891 ch.m_frontendParameters = feparm;
892 m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
896 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
898 m_channels.erase(id);
902 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
904 std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
905 if (i == m_channels.end())
910 parm = i->second.m_frontendParameters;
914 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
916 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_services.find(serviceref));
917 if (it == m_services.end())
918 m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
922 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
924 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
925 i = m_services.find(reference);
926 if (i == m_services.end())
935 RESULT eDVBDB::flush()
941 RESULT eDVBDB::getBouquet(const eServiceReference &ref, eBouquet* &bouquet)
943 std::string str = ref.path;
946 eDebug("getBouquet failed.. no path given!");
949 unsigned int pos = str.find("FROM BOUQUET \"");
950 if ( pos != std::string::npos )
952 str.erase(0, pos+14);
954 if ( pos != std::string::npos )
961 eDebug("getBouquet failed.. couldn't parse bouquet name");
964 std::map<std::string, eBouquet>::iterator i =
965 m_bouquets.find(str);
966 if (i == m_bouquets.end())
971 bouquet = &i->second;
975 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q, const eServiceReference &source)
977 if ( source.path.find("FROM") != std::string::npos )
979 if ( source.path.find("BOUQUET") != std::string::npos )
980 query = new eDVBDBBouquetQuery(this, source, q);
981 else if ( source.path.find("SATELLITES") != std::string::npos )
982 query = new eDVBDBSatellitesQuery(this, source, q);
983 else if ( source.path.find("PROVIDERS") != std::string::npos )
984 query = new eDVBDBProvidersQuery(this, source, q);
986 eFatal("invalid query %s", source.toString().c_str());
989 query = new eDVBDBQuery(this, source, q);
993 eServiceReference eDVBDB::searchReference(int tsid, int onid, int sid)
996 eTransportStreamID Tsid(tsid);
997 eOriginalNetworkID Onid(onid);
998 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator sit(m_services.begin());
999 sit != m_services.end(); ++sit)
1001 if (sit->first.getTransportStreamID() == Tsid &&
1002 sit->first.getOriginalNetworkID() == Onid &&
1003 sit->first.getServiceID() == Sid)
1006 return eServiceReference();
1009 DEFINE_REF(eDVBDBQueryBase);
1011 eDVBDBQueryBase::eDVBDBQueryBase(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1012 :m_db(db), m_query(query), m_source(source)
1016 int eDVBDBQueryBase::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1018 ePtr<eDVBService> a_service, b_service;
1019 int sortmode = m_query ? m_query->m_sort : eDVBChannelQuery::tName;
1021 if ((sortmode == eDVBChannelQuery::tName) || (sortmode == eDVBChannelQuery::tProvider))
1023 if (a.name.empty() && m_db->getService(a, a_service))
1025 if (b.name.empty() && m_db->getService(b, b_service))
1031 case eDVBChannelQuery::tName:
1035 return a_service->m_service_name_sort < b_service->m_service_name_sort;
1038 std::string str = b.name;
1040 return a_service->m_service_name_sort < str;
1045 std::string str = a.name;
1047 return str < b_service->m_service_name_sort;
1051 std::string aa = a.name, bb = b.name;
1056 case eDVBChannelQuery::tProvider:
1057 return a_service->m_provider_name < b_service->m_provider_name;
1058 case eDVBChannelQuery::tType:
1059 return a.getServiceType() < b.getServiceType();
1060 case eDVBChannelQuery::tBouquet:
1062 case eDVBChannelQuery::tSatellitePosition:
1063 return (a.getDVBNamespace().get() >> 16) < (b.getDVBNamespace().get() >> 16);
1070 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1071 :eDVBDBQueryBase(db, source, query)
1073 m_cursor = m_db->m_services.begin();
1076 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
1078 while (m_cursor != m_db->m_services.end())
1080 ref = m_cursor->first;
1082 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
1090 ref.type = eServiceReference::idInvalid;
1095 eDVBDBBouquetQuery::eDVBDBBouquetQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1096 :eDVBDBQueryBase(db, source, query), m_cursor(db->m_bouquets[query->m_bouquet_name].m_services.begin())
1100 RESULT eDVBDBBouquetQuery::getNextResult(eServiceReferenceDVB &ref)
1102 eBouquet &bouquet = m_db->m_bouquets[m_query->m_bouquet_name];
1103 std::list<eServiceReference> &list = bouquet.m_services;
1104 while (m_cursor != list.end())
1106 ref = *((eServiceReferenceDVB*)&(*m_cursor));
1108 std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it =
1109 m_db->m_services.find(ref);
1111 int res = (!m_query) || it == m_db->m_services.end() || it->second->checkFilter(ref, *m_query);
1119 ref.type = eServiceReference::idInvalid;
1124 eDVBDBListQuery::eDVBDBListQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1125 :eDVBDBQueryBase(db, source, query), m_cursor(m_list.end())
1129 RESULT eDVBDBListQuery::getNextResult(eServiceReferenceDVB &ref)
1131 if (m_cursor != m_list.end())
1136 ref.type = eServiceReference::idInvalid;
1140 int eDVBDBListQuery::compareLessEqual(const eServiceReferenceDVB &a, const eServiceReferenceDVB &b)
1142 if ( m_query->m_sort == eDVBChannelQuery::tSatellitePosition )
1144 int x = (a.getDVBNamespace().get() >> 16);
1145 int y = (b.getDVBNamespace().get() >> 16);
1152 std::string aa = a.name, bb = b.name;
1158 eDVBDBSatellitesQuery::eDVBDBSatellitesQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1159 :eDVBDBListQuery(db, source, query)
1161 std::set<unsigned int> found;
1162 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1163 it != m_db->m_services.end(); ++it)
1165 int res = it->second->checkFilter(it->first, *query);
1168 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1169 if (found.find(dvbnamespace) == found.end())
1171 found.insert(dvbnamespace);
1172 eServiceReferenceDVB ref;
1173 ref.setDVBNamespace(dvbnamespace);
1174 ref.flags=eServiceReference::flagDirectory;
1176 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1178 ref.path=buf+source.path;
1179 unsigned int pos=ref.path.find("FROM");
1180 ref.path.erase(pos);
1181 ref.path+="ORDER BY name";
1182 // eDebug("ref.path now %s", ref.path.c_str());
1183 m_list.push_back(ref);
1185 ref.path=buf+source.path;
1186 pos=ref.path.find("FROM");
1187 ref.path.erase(pos+5);
1188 ref.path+="PROVIDERS ORDER BY name";
1189 // eDebug("ref.path now %s", ref.path.c_str());
1190 m_list.push_back(ref);
1192 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1193 ref.path=buf+source.path;
1194 pos=ref.path.find("FROM");
1195 ref.path.erase(pos);
1196 ref.path+="ORDER BY name";
1197 // eDebug("ref.path now %s", ref.path.c_str());
1198 m_list.push_back(ref);
1202 m_cursor=m_list.begin();
1205 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1206 :eDVBDBListQuery(db, source, query)
1208 std::set<std::string> found;
1209 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1210 it != m_db->m_services.end(); ++it)
1212 int res = it->second->checkFilter(it->first, *query);
1215 const char *provider_name = it->second->m_provider_name.length() ?
1216 it->second->m_provider_name.c_str() :
1218 if (found.find(std::string(provider_name)) == found.end())
1220 found.insert(std::string(provider_name));
1221 eServiceReferenceDVB ref;
1223 ref.name=provider_name;
1224 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1225 ref.path=buf+source.path;
1226 unsigned int pos = ref.path.find("FROM");
1227 ref.flags=eServiceReference::flagDirectory;
1228 ref.path.erase(pos);
1229 ref.path+="ORDER BY name";
1230 // eDebug("ref.path now %s", ref.path.c_str());
1231 m_list.push_back(ref);
1235 m_cursor=m_list.begin();
1238 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1240 static int decodeType(const std::string &type)
1243 return eDVBChannelQuery::tName;
1244 else if (type == "provider")
1245 return eDVBChannelQuery::tProvider;
1246 else if (type == "type")
1247 return eDVBChannelQuery::tType;
1248 else if (type == "bouquet")
1249 return eDVBChannelQuery::tBouquet;
1250 else if (type == "satellitePosition")
1251 return eDVBChannelQuery::tSatellitePosition;
1252 else if (type == "channelID")
1253 return eDVBChannelQuery::tChannelID;
1254 else if (type == "flags")
1255 return eDVBChannelQuery::tFlags;
1260 /* never, NEVER write a parser in C++! */
1261 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1263 std::list<std::string>::const_iterator end_of_exp;
1271 while (end_of_exp != end)
1272 if (*end_of_exp == ")")
1277 if (end_of_exp == end)
1279 eDebug("expression parse: end of expression while searching for closing brace");
1284 // begin..end_of_exp
1285 int r = parseExpression(res, begin, end_of_exp);
1290 /* we had only one sub expression */
1291 if (end_of_exp == end)
1293 // eDebug("only one sub expression");
1297 /* otherwise we have an operator here.. */
1299 ePtr<eDVBChannelQuery> r2 = res;
1300 res = new eDVBChannelQuery();
1306 if (*end_of_exp == "||")
1307 res->m_type = eDVBChannelQuery::tOR;
1308 else if (*end_of_exp == "&&")
1309 res->m_type = eDVBChannelQuery::tAND;
1312 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1319 return parseExpression(res->m_p2, end_of_exp, end);
1322 // "begin" <op> "end"
1323 std::string type, op, val;
1325 res = new eDVBChannelQuery();
1329 while (begin != end)
1343 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1352 eDebug("malformed query: missing stuff");
1357 res->m_type = decodeType(type);
1359 if (res->m_type == -1)
1361 eDebug("malformed query: invalid type %s", type.c_str());
1368 else if (op == "!=")
1372 eDebug("invalid operator %s", op.c_str());
1377 res->m_string = val;
1378 res->m_int = atoi(val.c_str());
1379 // res->m_channelid = eDVBChannelID(val);
1384 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1386 std::list<std::string> tokens;
1388 std::string current_token;
1389 std::string bouquet_name;
1391 // eDebug("splitting %s....", query.c_str());
1393 const char *splitchars="()";
1394 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1395 while (i <= query.size())
1397 int c = (i < query.size()) ? query[i] : ' ';
1400 int issplit = !!strchr(splitchars, c);
1401 int isaln = isalnum(c);
1402 int iswhite = c == ' ';
1403 int isquot = c == '\"';
1407 iswhite = issplit = 0;
1411 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1413 if (current_token.size())
1414 tokens.push_back(current_token);
1415 current_token.clear();
1418 if (!(iswhite || isquot))
1422 quotemode = !quotemode;
1423 lastsplit = issplit;
1427 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1429 // printf("%s\n", a->c_str());
1432 int sort = eDVBChannelQuery::tName;
1433 /* check for "ORDER BY ..." */
1435 std::list<std::string>::iterator it = tokens.begin();
1436 while (it != tokens.end())
1441 if (it != tokens.end() && *it == "BY")
1444 sort = decodeType(*it);
1449 else if (*it == "FROM")
1452 if (it != tokens.end() && *it == "BOUQUET")
1458 else if (it != tokens.end() && *it == "SATELLITES")
1460 else if (it != tokens.end() && *it == "PROVIDERS")
1464 eDebug("FROM unknown %s", (*it).c_str());
1474 eWarning("ORDER BY .. string invalid.");
1479 // eDebug("sort by %d", sort);
1481 /* now we recursivly parse that. */
1482 int r = parseExpression(res, tokens.begin(), tokens.end());
1484 /* we have an empty (but valid!) expression */
1487 res = new eDVBChannelQuery();
1489 res->m_type = eDVBChannelQuery::tAny;
1495 res->m_bouquet_name = bouquet_name;
1498 // eDebug("return: %d", r);
1502 DEFINE_REF(eDVBChannelQuery);