- disabled gui for a moment
[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 DEFINE_REF(eDVBDB);
21
22 eDVBDB::eDVBDB()
23 {
24         eDebug("---- opening lame channel db");
25         FILE *f=fopen("lamedb", "rt");
26         if (!f)
27                 return;
28         char line[256];
29         if ((!fgets(line, 256, f)) || strncmp(line, "eDVB services", 13))
30         {
31                 eDebug("not a servicefile");
32                 fclose(f);
33                 return;
34         }
35         eDebug("reading services");
36         if ((!fgets(line, 256, f)) || strcmp(line, "transponders\n"))
37         {
38                 eDebug("services invalid, no transponders");
39                 fclose(f);
40                 return;
41         }
42         
43         // clear all transponders
44
45         while (!feof(f))
46         {
47                 if (!fgets(line, 256, f))
48                         break;
49                 if (!strcmp(line, "end\n"))
50                         break;
51                 int dvb_namespace=-1, transport_stream_id=-1, original_network_id=-1;
52                 sscanf(line, "%x:%x:%x", &dvb_namespace, &transport_stream_id, &original_network_id);
53                 if (original_network_id == -1)
54                         continue;
55                 eDVBChannelID channelid = eDVBChannelID(
56                         eDVBNamespace(dvb_namespace), 
57                         eTransportStreamID(transport_stream_id), 
58                         eOriginalNetworkID(original_network_id));
59                 
60                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
61                 while (!feof(f))
62                 {
63                         fgets(line, 256, f);
64                         if (!strcmp(line, "/\n"))
65                                 break;
66                         if (line[1]=='s')
67                         {
68                                 eDVBFrontendParametersSatellite sat;
69                                 int frequency, symbol_rate, polarisation, fec, orbital_position, inversion;
70                                 sscanf(line+2, "%d:%d:%d:%d:%d:%d", &frequency, &symbol_rate, &polarisation, &fec, &orbital_position, &inversion);
71                                 sat.frequency = frequency;
72                                 sat.symbol_rate = symbol_rate;
73                                 sat.polarisation = polarisation;
74                                 sat.fec = fec;
75                                 sat.orbital_position = orbital_position;
76                                 sat.inversion = inversion;
77                                 // ...
78 //                              t.setSatellite(frequency, symbol_rate, polarisation, fec, sat, inversion);
79                                 feparm->setDVBS(sat);
80                         }
81                         if (line[1]=='c')
82                         {
83                                 int frequency, symbol_rate, inversion=0, modulation=3;
84                                 sscanf(line+2, "%d:%d:%d:%d", &frequency, &symbol_rate, &inversion, &modulation);
85 //                              t.setCable(frequency, symbol_rate, inversion, modulation);
86                         }
87                 }
88                 addChannelToList(channelid, feparm);
89         }
90
91         if ((!fgets(line, 256, f)) || strcmp(line, "services\n"))
92         {
93                 eDebug("services invalid, no services");
94                 return;
95         }
96         
97         // clear all services
98         
99         int count=0;
100
101         while (!feof(f))
102         {
103                 if (!fgets(line, 256, f))
104                         break;
105                 if (!strcmp(line, "end\n"))
106                         break;
107
108                 int service_id=-1, dvb_namespace, transport_stream_id=-1, original_network_id=-1, service_type=-1, service_number=-1;
109                 sscanf(line, "%x:%x:%x:%x:%d:%d", &service_id, &dvb_namespace, &transport_stream_id, &original_network_id, &service_type, &service_number);
110                 if (service_number == -1)
111                         continue;
112                 ePtr<eDVBService> s = new eDVBService;
113                 eServiceReferenceDVB ref = 
114                                                 eServiceReferenceDVB(
115                                                 eDVBNamespace(dvb_namespace),
116                                                 eTransportStreamID(transport_stream_id),
117                                                 eOriginalNetworkID(original_network_id),
118                                                 eServiceID(service_id),
119                                                 service_type);
120                 count++;
121                 fgets(line, 256, f);
122                 if (strlen(line))
123                         line[strlen(line)-1]=0;
124                 s->m_service_name=line;
125                 fgets(line, 256, f);
126                 if (strlen(line))
127                         line[strlen(line)-1]=0;
128
129                 eString str=line;
130
131                 if (str[1]!=':')        // old ... (only service_provider)
132                 {
133                         s->m_provider_name=line;
134                 } else
135                         while ((!str.empty()) && str[1]==':') // new: p:, f:, c:%02d...
136                         {
137                                 unsigned int c=str.find(',');
138                                 char p=str[0];
139                                 eString v;
140                                 if (c == eString::npos)
141                                 {
142                                         v=str.mid(2);
143                                         str="";
144                                 } else
145                                 {
146                                         v=str.mid(2, c-2);
147                                         str=str.mid(c+1);
148                                 }
149 //                              eDebug("%c ... %s", p, v.c_str());
150                                 if (p == 'p')
151                                         s->m_provider_name=v;
152                                 else if (p == 'f')
153                                 {
154                                         sscanf(v.c_str(), "%x", &s->m_flags);
155                                 } else if (p == 'c')
156                                 {
157                                         int cid, val;
158                                         sscanf(v.c_str(), "%02d%04x", &cid, &val);
159                                         s->m_cache[cid]=val;
160                                 } else if (p == 'C')
161                                 {
162                                         int val;
163                                         sscanf(v.c_str(), "%04x", &val);
164                                         s->m_ca.insert(val);
165                                 }
166                         }
167                 addService(ref, s);
168         }
169
170         eDebug("loaded %d services", count);
171         
172         fclose(f);
173         
174 }
175
176 eDVBDB::~eDVBDB()
177 {
178         eDebug("---- saving lame channel db");
179         FILE *f=fopen("lamedb", "wt");
180         int channels=0, services=0;
181         if (!f)
182                 eFatal("couldn't save lame channel db!"); 
183         fprintf(f, "eDVB services /3/\n");
184         fprintf(f, "transponders\n");
185         for (std::map<eDVBChannelID, channel>::const_iterator i(m_channels.begin());
186                         i != m_channels.end(); ++i)
187         {
188                 const eDVBChannelID &chid = i->first;
189                 const channel &ch = i->second;
190                 
191                 fprintf(f, "%08x:%04x:%04x\n", chid.dvbnamespace.get(),
192                                 chid.transport_stream_id.get(), chid.original_network_id.get());
193                 eDVBFrontendParametersSatellite sat;
194                 if (!ch.m_frontendParameters->getDVBS(sat))
195                 {
196                         fprintf(f, "\ts %d:%d:%d:%d:%d:%d\n", 
197                                 sat.frequency, sat.symbol_rate,
198                                 sat.polarisation, sat.fec, sat.inversion,
199                                 sat.orbital_position);
200                 }
201                 fprintf(f, "/\n");
202                 channels++;
203         }
204         fprintf(f, "end\nservices\n");
205         
206         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i(m_services.begin());
207                 i != m_services.end(); ++i)
208         {
209                 const eServiceReferenceDVB &s = i->first;
210                 fprintf(f, "%04x:%08x:%04x:%04x:%d:%d\n", 
211                                 s.getServiceID().get(), s.getDVBNamespace().get(), 
212                                 s.getOriginalNetworkID().get(), s.getTransportStreamID().get(),
213                                 s.getServiceType(),
214                                 0);
215                 
216                 fprintf(f, "%s\n", i->second->m_service_name.c_str());
217                 fprintf(f, "p=%s", i->second->m_provider_name.c_str());
218                 for (std::set<int>::const_iterator ca(i->second->m_ca.begin());
219                         ca != i->second->m_ca.end(); ++ca)
220                         fprintf(f, ",C=%04x", *ca);
221                 fprintf(f, "\n");
222                 services++;
223         }
224         fprintf(f, "end\nHave a lot of bugs!\n");
225         eDebug("saved %d channels and %d services!", channels, services);
226         fclose(f);
227 }
228
229 RESULT eDVBDB::addChannelToList(const eDVBChannelID &id, iDVBFrontendParameters *feparm)
230 {
231         channel ch;
232         assert(feparm);
233         ch.m_frontendParameters = feparm;
234         m_channels.insert(std::pair<eDVBChannelID, channel>(id, ch));
235         return 0;
236 }
237
238 RESULT eDVBDB::removeChannel(const eDVBChannelID &id)
239 {
240         m_channels.erase(id);
241         return 0;
242 }
243
244 RESULT eDVBDB::getChannelFrontendData(const eDVBChannelID &id, ePtr<iDVBFrontendParameters> &parm)
245 {
246         std::map<eDVBChannelID, channel>::iterator i = m_channels.find(id);
247         if (i == m_channels.end())
248         {
249                 parm = 0;
250                 return -ENOENT;
251         }
252         parm = i->second.m_frontendParameters;
253         return 0;
254 }
255
256 RESULT eDVBDB::addService(const eServiceReferenceDVB &serviceref, eDVBService *service)
257 {
258         m_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(serviceref, service));
259         return 0;
260 }
261
262 RESULT eDVBDB::getService(const eServiceReferenceDVB &reference, ePtr<eDVBService> &service)
263 {
264         std::map<eServiceReferenceDVB, ePtr<eDVBService> >::iterator i;
265         i = m_services.find(reference);
266         if (i == m_services.end())
267         {
268                 service = 0;
269                 return -ENOENT;
270         }
271         service = i->second;
272         return 0;
273 }
274