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