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::canDescent )
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 int 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::canDescent )
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 ( 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 std::set<unsigned int> found;
1161 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1162 it != m_db->m_services.end(); ++it)
1164 int res = it->second->checkFilter(it->first, *query);
1167 unsigned int dvbnamespace = it->first.getDVBNamespace().get()&0xFFFF0000;
1168 if (found.find(dvbnamespace) == found.end())
1170 found.insert(dvbnamespace);
1171 eServiceReferenceDVB ref;
1172 ref.setDVBNamespace(dvbnamespace);
1173 ref.flags=eServiceReference::flagDirectory;
1175 snprintf(buf, 128, "(satellitePosition == %d) && ", dvbnamespace>>16);
1177 ref.path=buf+source.path;
1178 unsigned int pos=ref.path.find("FROM");
1179 ref.path.erase(pos);
1180 ref.path+="ORDER BY name";
1181 // eDebug("ref.path now %s", ref.path.c_str());
1182 m_list.push_back(ref);
1184 ref.path=buf+source.path;
1185 pos=ref.path.find("FROM");
1186 ref.path.erase(pos+5);
1187 ref.path+="PROVIDERS ORDER BY name";
1188 // eDebug("ref.path now %s", ref.path.c_str());
1189 m_list.push_back(ref);
1191 snprintf(buf, 128, "(satellitePosition == %d) && (flags == %d) && ", dvbnamespace>>16, eDVBService::dxNewFound);
1192 ref.path=buf+source.path;
1193 pos=ref.path.find("FROM");
1194 ref.path.erase(pos);
1195 ref.path+="ORDER BY name";
1196 // eDebug("ref.path now %s", ref.path.c_str());
1197 m_list.push_back(ref);
1201 m_cursor=m_list.begin();
1204 eDVBDBProvidersQuery::eDVBDBProvidersQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query)
1205 :eDVBDBListQuery(db, source, query)
1207 std::set<std::string> found;
1208 for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator it(m_db->m_services.begin());
1209 it != m_db->m_services.end(); ++it)
1211 int res = it->second->checkFilter(it->first, *query);
1214 const char *provider_name = it->second->m_provider_name.length() ?
1215 it->second->m_provider_name.c_str() :
1217 if (found.find(std::string(provider_name)) == found.end())
1219 found.insert(std::string(provider_name));
1220 eServiceReferenceDVB ref;
1222 ref.name=provider_name;
1223 snprintf(buf, 64, "(provider == \"%s\") && ", provider_name);
1224 ref.path=buf+source.path;
1225 unsigned int pos = ref.path.find("FROM");
1226 ref.flags=eServiceReference::flagDirectory;
1227 ref.path.erase(pos);
1228 ref.path+="ORDER BY name";
1229 // eDebug("ref.path now %s", ref.path.c_str());
1230 m_list.push_back(ref);
1234 m_cursor=m_list.begin();
1237 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[||,&& (..)] */
1239 static int decodeType(const std::string &type)
1242 return eDVBChannelQuery::tName;
1243 else if (type == "provider")
1244 return eDVBChannelQuery::tProvider;
1245 else if (type == "type")
1246 return eDVBChannelQuery::tType;
1247 else if (type == "bouquet")
1248 return eDVBChannelQuery::tBouquet;
1249 else if (type == "satellitePosition")
1250 return eDVBChannelQuery::tSatellitePosition;
1251 else if (type == "channelID")
1252 return eDVBChannelQuery::tChannelID;
1253 else if (type == "flags")
1254 return eDVBChannelQuery::tFlags;
1259 /* never, NEVER write a parser in C++! */
1260 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
1262 std::list<std::string>::const_iterator end_of_exp;
1266 eDebug("empty expression!");
1273 while (end_of_exp != end)
1274 if (*end_of_exp == ")")
1279 if (end_of_exp == end)
1281 eDebug("expression parse: end of expression while searching for closing brace");
1286 // begin..end_of_exp
1287 int r = parseExpression(res, begin, end_of_exp);
1292 /* we had only one sub expression */
1293 if (end_of_exp == end)
1295 // eDebug("only one sub expression");
1299 /* otherwise we have an operator here.. */
1301 ePtr<eDVBChannelQuery> r2 = res;
1302 res = new eDVBChannelQuery();
1308 if (*end_of_exp == "||")
1309 res->m_type = eDVBChannelQuery::tOR;
1310 else if (*end_of_exp == "&&")
1311 res->m_type = eDVBChannelQuery::tAND;
1314 eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
1321 return parseExpression(res->m_p2, end_of_exp, end);
1324 // "begin" <op> "end"
1325 std::string type, op, val;
1327 res = new eDVBChannelQuery();
1331 while (begin != end)
1345 eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
1354 eDebug("malformed query: missing stuff");
1359 res->m_type = decodeType(type);
1361 if (res->m_type == -1)
1363 eDebug("malformed query: invalid type %s", type.c_str());
1370 else if (op == "!=")
1374 eDebug("invalid operator %s", op.c_str());
1379 res->m_string = val;
1380 res->m_int = atoi(val.c_str());
1381 // res->m_channelid = eDVBChannelID(val);
1386 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
1388 std::list<std::string> tokens;
1390 std::string current_token;
1391 std::string bouquet_name;
1393 // eDebug("splitting %s....", query.c_str());
1395 const char *splitchars="()";
1396 int quotemode = 0, lastsplit = 0, lastalnum = 0;
1397 while (i <= query.size())
1399 int c = (i < query.size()) ? query[i] : ' ';
1402 int issplit = !!strchr(splitchars, c);
1403 int isaln = isalnum(c);
1404 int iswhite = c == ' ';
1405 int isquot = c == '\"';
1409 iswhite = issplit = 0;
1413 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
1415 if (current_token.size())
1416 tokens.push_back(current_token);
1417 current_token.clear();
1420 if (!(iswhite || isquot))
1424 quotemode = !quotemode;
1425 lastsplit = issplit;
1429 // for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
1431 // printf("%s\n", a->c_str());
1434 int sort = eDVBChannelQuery::tName;
1435 /* check for "ORDER BY ..." */
1437 std::list<std::string>::iterator it = tokens.begin();
1438 while (it != tokens.end())
1443 if (it != tokens.end() && *it == "BY")
1446 sort = decodeType(*it);
1451 else if (*it == "FROM")
1454 if (it != tokens.end() && *it == "BOUQUET")
1460 else if (it != tokens.end() && *it == "SATELLITES")
1462 else if (it != tokens.end() && *it == "PROVIDERS")
1466 eDebug("FROM unknown %s", (*it).c_str());
1476 eWarning("ORDER BY .. string invalid.");
1481 // eDebug("sort by %d", sort);
1483 /* now we recursivly parse that. */
1484 int r = parseExpression(res, tokens.begin(), tokens.end());
1486 /* we have an empty (but valid!) expression */
1489 res = new eDVBChannelQuery();
1491 res->m_type = eDVBChannelQuery::tAny;
1497 res->m_bouquet_name = bouquet_name;
1500 // eDebug("return: %d", r);
1504 DEFINE_REF(eDVBChannelQuery);