- added iStaticServiceInformation
[enigma2.git] / lib / dvb / db.cpp
1 #include <errno.h>
2 #include <lib/dvb/db.h>
3 #include <lib/dvb/frontend.h>
4 #include <lib/base/eerror.h>
5 #include <lib/dvb_si/sdt.h>
6 #include <lib/dvb_si/descriptor_tag.h>
7 #include <lib/dvb_si/service_descriptor.h>
8 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
9
10 DEFINE_REF(eDVBService);
11
12 eDVBService::eDVBService()
13 {
14 }
15
16 eDVBService::~eDVBService()
17 {
18 }
19
20 eDVBService &eDVBService::operator=(const eDVBService &s)
21 {
22         m_service_name = s.m_service_name;
23         m_provider_name = s.m_provider_name;
24         m_flags = s.m_flags;
25         m_ca = s.m_ca;
26         m_cache = s.m_cache;
27         return *this;
28 }
29
30 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
31 {
32         name = m_service_name;
33 }
34
35 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
36 {
37         int res = 0;
38         switch (query.m_type)
39         {
40         case eDVBChannelQuery::tName:
41                 res = m_service_name.find(query.m_string) != std::string::npos;
42                 break;
43         case eDVBChannelQuery::tProvider:
44                 res = m_provider_name.find(query.m_string) != std::string::npos;
45                 break;
46         case eDVBChannelQuery::tType:
47                 res = ref.getServiceType() == query.m_int;
48                 break;
49         case eDVBChannelQuery::tBouquet:
50                 res = 0;
51                 break;
52         case eDVBChannelQuery::tSatellitePosition:
53                 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
54                 break;
55         case eDVBChannelQuery::tChannelID:
56         {
57                 eDVBChannelID chid;
58                 ref.getChannelID(chid);
59                 res = chid == query.m_channelid;
60                 break;
61         }
62         case eDVBChannelQuery::tAND:
63                 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
64                 break;
65         case eDVBChannelQuery::tOR:
66                 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
67                 break;
68         }
69
70         if (query.m_inverse)
71                 return !res;
72         else
73                 return res;
74 }
75
76 DEFINE_REF(eDVBDB);
77
78 eDVBDB::eDVBDB()
79 {
80         eDebug("---- opening lame channel db");
81         FILE *f=fopen("lamedb", "rt");
82         if (!f)
83                 return;
84         char line[256];
85         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
86         {
87                 eDebug("not a servicefile");
88                 fclose(f);
89                 return;
90         }
91         eDebug("reading services");
92         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
93         {
94                 eDebug("services invalid, no transponders");
95                 fclose(f);
96                 return;
97         }
98         
99         // clear all transponders
100
101         while (!feof(f))
102         {
103                 if (!fgets(line, 256, f))
104                         break;
105                 if (!strcmp(line, "end\n"))
106                         break;
107                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
108                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
109                 if (original_network_id == -1)
110                         continue;
111                 eDVBChannelID channelid = eDVBChannelID(
112                         eDVBNamespace(dvb_namespace), 
113                         eTransportStreamID(transport_stream_id), 
114                         eOriginalNetworkID(original_network_id));
115                 
116                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
117                 while (!feof(f))
118                 {
119                         fgets(line, 256, f);
120                         if (!strcmp(line, "/\n"))
121                                 break;
122                         if (line[1]=='s')
123                         {
124                                 eDVBFrontendParametersSatellite sat;
125                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
126                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &inversion, &orbital_position);
127                                 sat.frequency = frequency;
128                                 sat.symbol_rate = symbol_rate;
129                                 sat.polarisation = polarisation;
130                                 sat.fec = fec;
131                                 sat.orbital_position = orbital_position;
132                                 sat.inversion = inversion;
133                                 // ...
134 //                              t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
135                                 feparm->setDVBS(sat);
136                         }
137                         if (line[1]=='c')
138                         {
139                                 int frequency, symbol_rate, inversion=0, modulation=3;
140                                 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
141 //                              t.setCable(frequency, symbol_rate, inversion, modulation);
142                         }
143                 }
144                 addChannelToList(channelid, feparm);
145         }
146
147         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
148         {
149                 eDebug("services invalid, no services");
150                 return;
151         }
152         
153         // clear all services
154         
155         int count=0;
156
157         while (!feof(f))
158         {
159                 if (!fgets(line, 256, f))
160                         break;
161                 if (!strcmp(line, "end\n"))
162                         break;
163
164                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
165                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
166                 if (service_number == -1)
167                         continue;
168                 ePtr<eDVBService> s = new eDVBService;
169                 eServiceReferenceDVB ref = 
170                                                 eServiceReferenceDVB(
171                                                 eDVBNamespace(dvb_namespace),
172                                                 eTransportStreamID(transport_stream_id),
173                                                 eOriginalNetworkID(original_network_id),
174                                                 eServiceID(service_id),
175                                                 service_type);
176                 count++;
177                 fgets(line, 256, f);
178                 if (strlen(line))
179                         line[strlen(line)-1]=0;
180                 s->m_service_name=line;
181                 fgets(line, 256, f);
182                 if (strlen(line))
183                         line[strlen(line)-1]=0;
184
185                 std::string str=line;
186
187                 if (str[1]!=':')        // old ... (only service_provider)
188                 {
189                         s->m_provider_name=line;
190                 } else
191                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
192                         {
193                                 unsigned int c=str.find(',');
194                                 char p=str[0];
195                                 std::string v;
196                                 if (c == std::string::npos)
197                                 {
198                                         v=str.substr(2);
199                                         str="";
200                                 } else
201                                 {
202                                         v=str.substr(2, c-2);
203                                         str=str.substr(c+1);
204                                 }
205 //                              eDebug("%c ... %s", p, v.c_str());
206                                 if (p == 'p')
207                                         s->m_provider_name=v;
208                                 else if (p == 'f')
209                                 {
210                                         sscanf(v.c_str(), "%x", &s->m_flags);
211                                 } else if (p == 'c')
212                                 {
213                                         int cid, val;
214                                         sscanf(v.c_str(), "%02d%04x", &cid, &val);
215                                         s->m_cache[cid]=val;
216                                 } else if (p == 'C')
217                                 {
218                                         int val;
219                                         sscanf(v.c_str(), "%04x", &val);
220                                         s->m_ca.insert(val);
221                                 }
222                         }
223                 addService(ref, s);
224         }
225
226         eDebug("loaded %d services", count);
227         
228         fclose(f);
229         
230 }
231
232 eDVBDB::~eDVBDB()
233 {
234         eDebug("---- saving lame channel db");
235         FILE *f=fopen("lamedb", "wt");
236         int channels=0, services=0;
237         if (!f)
238                 eFatal("couldn't save lame channel db!"); 
239         fprintf(f, "eDVB services /3/\n");
240         fprintf(f, "transponders\n");
241         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
242                         i != m_channels.end(); ++i)
243         {
244                 const eDVBChannelID &chid = i->first;
245                 const channel &ch = i->second;
246                 
247                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
248                                 chid.transport_stream_id.get(), chid.original_network_id.get());
249                 eDVBFrontendParametersSatellite sat;
250                 if (!ch.m_frontendParameters->getDVBS(sat))
251                 {
252                         fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n", 
253                                 sat.frequency, sat.symbol_rate,
254                                 sat.polarisation, sat.fec, sat.inversion,
255                                 sat.orbital_position);
256                 }
257                 fprintf(f, "/\n");
258                 channels++;
259         }
260         fprintf(f, "end\nservices\n");
261         
262         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
263                 i != m_services.end(); ++i)
264         {
265                 const eServiceReferenceDVB &s = i->first;
266                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n", 
267                                 s.getServiceID().get(), s.getDVBNamespace().get(), 
268                                 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(), 
269                                 s.getServiceType(),
270                                 0);
271                 
272                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
273                 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
274                 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
275                         ca != i->second->m_ca.end(); ++ca)
276                         fprintf(f, ",C:%04x", *ca);
277                 fprintf(f, "\n");
278                 services++;
279         }
280         fprintf(f, "end\nHave a lot of bugs!\n");
281         eDebug("saved %d channels and %d services!", channels, services);
282         fclose(f);
283 }
284
285 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
286 {
287         channel ch;
288         assert(feparm);
289         ch.m_frontendParameters = feparm;
290         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
291         return 0;
292 }
293
294 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
295 {
296         m_channels.erase(id);
297         return 0;
298 }
299
300 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
301 {
302         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
303         if (i == m_channels.end())
304         {
305                 parm = 0;
306                 return -ENOENT;
307         }
308         parm = i->second.m_frontendParameters;
309         return 0;
310 }
311
312 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
313 {
314         m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
315         return 0;
316 }
317
318 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
319 {
320         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
321         i = m_services.find(reference);
322         if (i == m_services.end())
323         {
324                 service = 0;
325                 return -ENOENT;
326         }
327         service = i->second;
328         return 0;
329 }
330
331 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q)
332 {
333         query = new eDVBDBQuery(this, q);
334         return 0;
335 }
336
337 DEFINE_REF(eDVBDBQuery);
338
339 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, eDVBChannelQuery *query): m_db(db), m_query(query)
340 {
341         m_cursor = m_db->m_services.begin();
342 }
343
344 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
345 {
346         while (m_cursor != m_db->m_services.end())
347         {
348                 ref = m_cursor->first;
349                 
350                 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
351
352                 ++m_cursor;
353                 
354                 if (res)
355                         return 0;
356         }
357         return 1;
358 }
359
360 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
361 {
362         res = new eDVBChannelQuery();
363         res->m_type = eDVBChannelQuery::tName;
364         res->m_inverse = 0;
365         res->m_string = query;
366         return 0;
367 }
368
369 DEFINE_REF(eDVBChannelQuery);