add methods for save and load lamedb
[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/base/estring.h>
6 #include <lib/dvb_si/sdt.h>
7 #include <lib/dvb_si/descriptor_tag.h>
8 #include <lib/dvb_si/service_descriptor.h>
9 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
10
11 DEFINE_REF(eDVBService);
12
13 eDVBService::eDVBService()
14 {
15 }
16
17 eDVBService::~eDVBService()
18 {
19 }
20
21 eDVBService &eDVBService::operator=(const eDVBService &s)
22 {
23         m_service_name = s.m_service_name;
24         m_provider_name = s.m_provider_name;
25         m_flags = s.m_flags;
26         m_ca = s.m_ca;
27         m_cache = s.m_cache;
28         return *this;
29 }
30
31 RESULT eDVBService::getName(const eServiceReference &ref, std::string &name)
32 {
33         name = convertDVBUTF8(m_service_name);
34         return 0;
35 }
36
37 int eDVBService::getLength(const eServiceReference &ref)
38 {
39         return -1;
40 }
41
42 int eDVBService::checkFilter(const eServiceReferenceDVB &ref, const eDVBChannelQuery &query)
43 {
44         int res = 0;
45         switch (query.m_type)
46         {
47         case eDVBChannelQuery::tName:
48                 res = m_service_name.find(query.m_string) != std::string::npos;
49                 break;
50         case eDVBChannelQuery::tProvider:
51                 res = m_provider_name.find(query.m_string) != std::string::npos;
52                 break;
53         case eDVBChannelQuery::tType:
54                 res = ref.getServiceType() == query.m_int;
55                 break;
56         case eDVBChannelQuery::tBouquet:
57                 res = 0;
58                 break;
59         case eDVBChannelQuery::tSatellitePosition:
60                 res = (ref.getDVBNamespace().get() >> 16) == query.m_int;
61                 break;
62         case eDVBChannelQuery::tChannelID:
63         {
64                 eDVBChannelID chid;
65                 ref.getChannelID(chid);
66                 res = chid == query.m_channelid;
67                 break;
68         }
69         case eDVBChannelQuery::tAND:
70                 res = checkFilter(ref, *query.m_p1) && checkFilter(ref, *query.m_p2);
71                 break;
72         case eDVBChannelQuery::tOR:
73                 res = checkFilter(ref, *query.m_p1) || checkFilter(ref, *query.m_p2);
74                 break;
75         }
76
77         if (query.m_inverse)
78                 return !res;
79         else
80                 return res;
81 }
82
83 int eDVBService::getCachePID(cacheID id)
84 {
85         std::map<int, int>::iterator it = m_cache.find(id);
86         if ( it != m_cache.end() )
87                 return it->second;
88         return -1;
89 }
90
91 void eDVBService::setCachePID(cacheID id, int pid)
92 {
93         m_cache[id] = pid;
94 }
95
96 DEFINE_REF(eDVBDB);
97
98 void eDVBDB::load()
99 {
100         eDebug("---- opening lame channel db");
101         FILE *f=fopen("lamedb", "rt");
102         if (!f)
103                 return;
104         char line[256];
105         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
106         {
107                 eDebug("not a servicefile");
108                 fclose(f);
109                 return;
110         }
111         eDebug("reading services");
112         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
113         {
114                 eDebug("services invalid, no transponders");
115                 fclose(f);
116                 return;
117         }
118
119         // clear all transponders
120
121         while (!feof(f))
122         {
123                 if (!fgets(line, 256, f))
124                         break;
125                 if (!strcmp(line, "end\n"))
126                         break;
127                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
128                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
129                 if (original_network_id == -1)
130                         continue;
131                 eDVBChannelID channelid = eDVBChannelID(
132                         eDVBNamespace(dvb_namespace),
133                         eTransportStreamID(transport_stream_id),
134                         eOriginalNetworkID(original_network_id));
135
136                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
137                 while (!feof(f))
138                 {
139                         fgets(line, 256, f);
140                         if (!strcmp(line, "/\n"))
141                                 break;
142                         if (line[1]=='s')
143                         {
144                                 eDVBFrontendParametersSatellite sat;
145                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
146                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &inversion, &orbital_position);
147                                 sat.frequency = frequency;
148                                 sat.symbol_rate = symbol_rate;
149                                 sat.polarisation = polarisation;
150                                 sat.fec = fec;
151                                 sat.orbital_position = orbital_position;
152                                 sat.inversion = inversion;
153                                 // ...
154 //                              t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
155                                 feparm->setDVBS(sat);
156                         }
157                         if (line[1]=='c')
158                         {
159                                 int frequency, symbol_rate, inversion=0, modulation=3;
160                                 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
161 //                              t.setCable(frequency, symbol_rate, inversion, modulation);
162                         }
163                 }
164                 addChannelToList(channelid, feparm);
165         }
166
167         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
168         {
169                 eDebug("services invalid, no services");
170                 return;
171         }
172
173         // clear all services
174
175         int count=0;
176
177         while (!feof(f))
178         {
179                 if (!fgets(line, 256, f))
180                         break;
181                 if (!strcmp(line, "end\n"))
182                         break;
183
184                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
185                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
186                 if (service_number == -1)
187                         continue;
188                 ePtr<eDVBService> s = new eDVBService;
189                 eServiceReferenceDVB ref =
190                                                 eServiceReferenceDVB(
191                                                 eDVBNamespace(dvb_namespace),
192                                                 eTransportStreamID(transport_stream_id),
193                                                 eOriginalNetworkID(original_network_id),
194                                                 eServiceID(service_id),
195                                                 service_type);
196                 count++;
197                 fgets(line, 256, f);
198                 if (strlen(line))
199                         line[strlen(line)-1]=0;
200                 s->m_service_name=line;
201                 fgets(line, 256, f);
202                 if (strlen(line))
203                         line[strlen(line)-1]=0;
204
205                 std::string str=line;
206
207                 if (str[1]!=':')        // old ... (only service_provider)
208                 {
209                         s->m_provider_name=line;
210                 } else
211                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
212                         {
213                                 unsigned int c=str.find(',');
214                                 char p=str[0];
215                                 std::string v;
216                                 if (c == std::string::npos)
217                                 {
218                                         v=str.substr(2);
219                                         str="";
220                                 } else
221                                 {
222                                         v=str.substr(2, c-2);
223                                         str=str.substr(c+1);
224                                 }
225 //                              eDebug("%c ... %s", p, v.c_str());
226                                 if (p == 'p')
227                                         s->m_provider_name=v;
228                                 else if (p == 'f')
229                                 {
230                                         sscanf(v.c_str(), "%x", &s->m_flags);
231                                 } else if (p == 'c')
232                                 {
233                                         int cid, val;
234                                         sscanf(v.c_str(), "%02d%04x", &cid, &val);
235                                         s->m_cache[cid]=val;
236                                 } else if (p == 'C')
237                                 {
238                                         int val;
239                                         sscanf(v.c_str(), "%04x", &val);
240                                         s->m_ca.insert(val);
241                                 }
242                         }
243                 addService(ref, s);
244         }
245
246         eDebug("loaded %d services", count);
247
248         fclose(f);
249 }
250
251 void eDVBDB::save()
252 {
253         eDebug("---- saving lame channel db");
254         FILE *f=fopen("lamedb", "wt");
255         int channels=0, services=0;
256         if (!f)
257                 eFatal("couldn't save lame channel db!");
258         fprintf(f, "eDVB services /3/\n");
259         fprintf(f, "transponders\n");
260         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
261                         i != m_channels.end(); ++i)
262         {
263                 const eDVBChannelID &chid = i->first;
264                 const channel &ch = i->second;
265
266                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
267                                 chid.transport_stream_id.get(), chid.original_network_id.get());
268                 eDVBFrontendParametersSatellite sat;
269                 if (!ch.m_frontendParameters->getDVBS(sat))
270                 {
271                         fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n",
272                                 sat.frequency, sat.symbol_rate,
273                                 sat.polarisation, sat.fec, sat.inversion,
274                                 sat.orbital_position);
275                 }
276                 fprintf(f, "/\n");
277                 channels++;
278         }
279         fprintf(f, "end\nservices\n");
280
281         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
282                 i != m_services.end(); ++i)
283         {
284                 const eServiceReferenceDVB &s = i->first;
285                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n",
286                                 s.getServiceID().get(), s.getDVBNamespace().get(),
287                                 s.getTransportStreamID().get(),s.getOriginalNetworkID().get(),
288                                 s.getServiceType(),
289                                 0);
290
291                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
292                 fprintf(f, "p:%s", i->second->m_provider_name.c_str());
293
294                 // write cached pids
295                 for (std::map<int,int>::const_iterator ca(i->second->m_cache.begin());
296                         ca != i->second->m_cache.end(); ++ca)
297                         fprintf(f, ",c:%02d%04x", ca->first, ca->second);
298
299                 // write cached ca pids
300                 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
301                         ca != i->second->m_ca.end(); ++ca)
302                         fprintf(f, ",C:%04x", *ca);
303
304                 fprintf(f, "\n");
305                 services++;
306         }
307         fprintf(f, "end\nHave a lot of bugs!\n");
308         eDebug("saved %d channels and %d services!", channels, services);
309         fclose(f);
310 }
311
312 eDVBDB::eDVBDB()
313 {
314         load();
315 }
316
317 eDVBDB::~eDVBDB()
318 {
319 //      save();
320 }
321
322 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
323 {
324         channel ch;
325         assert(feparm);
326         ch.m_frontendParameters = feparm;
327         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
328         return 0;
329 }
330
331 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
332 {
333         m_channels.erase(id);
334         return 0;
335 }
336
337 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
338 {
339         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
340         if (i == m_channels.end())
341         {
342                 parm = 0;
343                 return -ENOENT;
344         }
345         parm = i->second.m_frontendParameters;
346         return 0;
347 }
348
349 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
350 {
351         m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
352         return 0;
353 }
354
355 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
356 {
357         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
358         i = m_services.find(reference);
359         if (i == m_services.end())
360         {
361                 service = 0;
362                 return -ENOENT;
363         }
364         service = i->second;
365         return 0;
366 }
367
368 RESULT eDVBDB::startQuery(ePtr<iDVBChannelListQuery> &query, eDVBChannelQuery *q)
369 {
370         query = new eDVBDBQuery(this, eServiceReference(), q);
371         return 0;
372 }
373
374 DEFINE_REF(eDVBDBQuery);
375
376 eDVBDBQuery::eDVBDBQuery(eDVBDB *db, const eServiceReference &source, eDVBChannelQuery *query): m_db(db), m_query(query)
377 {
378                 // TODO: use SOURCE ...
379         m_cursor = m_db->m_services.begin();
380 }
381
382 RESULT eDVBDBQuery::getNextResult(eServiceReferenceDVB &ref)
383 {
384         while (m_cursor != m_db->m_services.end())
385         {
386                 ref = m_cursor->first;
387                 
388                 int res = (!m_query) || m_cursor->second->checkFilter(ref, *m_query);
389
390                 ++m_cursor;
391                 
392                 if (res)
393                         return 0;
394         }
395         return 1;
396 }
397
398 /* (<name|provider|type|bouquet|satpos|chid> <==|...> <"string"|int>)[AND (..)] */
399
400         /* never, NEVER write a parser in C++! */
401 RESULT parseExpression(ePtr<eDVBChannelQuery> &res, std::list<std::string>::const_iterator begin, std::list<std::string>::const_iterator end)
402 {
403         std::list<std::string>::const_iterator end_of_exp;
404         if (*begin == "(")
405         {
406                 end_of_exp = begin;
407                 while (end_of_exp != end)
408                         if (*end_of_exp == ")")
409                                 break;
410                         else
411                                 ++end_of_exp;
412         
413                 if (end_of_exp == end)
414                 {
415                         eDebug("expression parse: end of expression while searching for closing brace");
416                         return -1;
417                 }
418                 
419                 ++begin;
420                 // begin..end_of_exp
421                 int r = parseExpression(res, begin, end_of_exp);
422                 if (r)
423                         return r;
424                 ++end_of_exp;
425                 
426                         /* we had only one sub expression */
427                 if (end_of_exp == end)
428                         return 1;
429                 
430                         /* otherwise we have an operator here.. */
431                 
432                 ePtr<eDVBChannelQuery> r2 = res;
433                 res = new eDVBChannelQuery();
434                 res->m_p1 = r2;
435                 res->m_inverse = 0;
436                 r2 = 0;
437                 
438                 if (*end_of_exp == "||")
439                         res->m_type = eDVBChannelQuery::tOR;
440                 else if (*end_of_exp == "&&")
441                         res->m_type = eDVBChannelQuery::tAND;
442                 else
443                 {
444                         eDebug("found operator %s, but only && and || are allowed!", end_of_exp->c_str());
445                         res = 0;
446                         return 1;
447                 }
448                 
449                 ++end_of_exp;
450                 
451                 return parseExpression(res->m_p2, end_of_exp, end);
452         }
453         
454         // "begin" <op> "end"
455         std::string type, op, val;
456         
457         res = new eDVBChannelQuery();
458         
459         int cnt = 0;
460         while (begin != end)
461         {
462                 switch (cnt)
463                 {
464                 case 0:
465                         type = *begin;
466                         break;
467                 case 1:
468                         op = *begin;
469                         break;
470                 case 2:
471                         val = *begin;
472                         break;
473                 case 3:
474                         eDebug("malformed query: got '%s', but expected only <type> <op> <val>", begin->c_str());
475                         return 1;
476                 }
477                 ++begin;
478                 ++cnt;
479         }
480         
481         if (cnt != 3)
482         {
483                 eDebug("malformed query: missing stuff");
484                 res = 0;
485                 return 1;
486         }
487         
488         if (type == "name")
489                 res->m_type = eDVBChannelQuery::tName;
490         else if (type == "provider")
491                 res->m_type = eDVBChannelQuery::tProvider;
492         else if (type == "type")
493                 res->m_type = eDVBChannelQuery::tType;
494         else if (type == "bouquet")
495                 res->m_type = eDVBChannelQuery::tBouquet;
496         else if (type == "satellitePosition")
497                 res->m_type = eDVBChannelQuery::tSatellitePosition;
498         else if (type == "channelID")
499                 res->m_type = eDVBChannelQuery::tChannelID;
500         else
501         {
502                 eDebug("malformed query: invalid type %s", type.c_str());
503                 res = 0;
504                 return 1;
505         }
506         
507         eDebug("type is %d, nice!", res->m_type);
508         
509         if (op == "==")
510                 res->m_inverse = 0;
511         else if (op == "!=")
512                 res->m_inverse = 1;
513         else
514         {
515                 eDebug("invalid operator %s", op.c_str());
516                 res = 0;
517                 return 1;
518         }
519         
520         res->m_string = val;
521         res->m_int = atoi(val.c_str());
522 //      res->m_channelid = eDVBChannelID(val);
523         
524         return 0;
525 }
526
527 RESULT eDVBChannelQuery::compile(ePtr<eDVBChannelQuery> &res, std::string query)
528 {
529         std::list<std::string> tokens;
530         
531         std::string current_token;
532         
533 //      eDebug("splitting %s....", query.c_str());
534         unsigned int i = 0;
535         const char *splitchars="()";
536         int quotemode = 0, lastsplit = 0, lastalnum = 0;
537         while (i <= query.size())
538         {
539                 int c = (i < query.size()) ? query[i] : ' ';
540                 ++i;
541                 
542                 int issplit = !!strchr(splitchars, c);
543                 int isaln = isalnum(c);
544                 int iswhite = c == ' ';
545                 int isquot = c == '\"';
546                 
547                 if (quotemode)
548                 {
549                         iswhite = issplit = 0;
550                         isaln = lastalnum;
551                 }
552                 
553                 if (issplit || iswhite || isquot || lastsplit || (lastalnum != isaln))
554                 {
555                         if (current_token.size())
556                                 tokens.push_back(current_token);
557                         current_token.clear();
558                 }
559                 
560                 if (!(iswhite || isquot))
561                         current_token += c;
562                 
563                 if (isquot)
564                         quotemode = !quotemode;
565                 lastsplit = issplit;
566                 lastalnum = isaln;
567         }
568         
569 //      for (std::list<std::string>::const_iterator a(tokens.begin()); a != tokens.end(); ++a)
570 //      {
571 //              printf("%s\n", a->c_str());
572 //      }
573         
574                 /* now we recursivly parse that. */
575         return  parseExpression(res, tokens.begin(), tokens.end());
576 /*      
577         res = new eDVBChannelQuery();
578         res->m_type = eDVBChannelQuery::tName;
579         res->m_inverse = 0;
580         res->m_string = query;
581         return 0; */
582 }
583
584 DEFINE_REF(eDVBChannelQuery);