- split out meta parser
[enigma2.git] / lib / dvb / scan.cpp
1 #include <lib/dvb/idvb.h>
2 #include <lib/dvb_si/sdt.h>
3 #include <lib/dvb_si/nit.h>
4 #include <lib/dvb_si/bat.h>
5 #include <lib/dvb_si/descriptor_tag.h>
6 #include <lib/dvb_si/service_descriptor.h>
7 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
8 #include <lib/dvb_si/ca_identifier_descriptor.h>
9 #include <lib/dvb/specs.h>
10 #include <lib/dvb/esection.h>
11 #include <lib/dvb/scan.h>
12 #include <lib/dvb/frontend.h>
13 #include <lib/base/eerror.h>
14 #include <errno.h>
15
16 #define SCAN_eDebug(x...) eDebug(x)
17 #define SCAN_eDebugNoNewLine(x...) eDebugNoNewLine(x)
18
19 DEFINE_REF(eDVBScan);
20
21 eDVBScan::eDVBScan(iDVBChannel *channel): m_channel(channel)
22 {
23         if (m_channel->getDemux(m_demux))
24                 SCAN_eDebug("scan: failed to allocate demux!");
25         m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
26 }
27
28 eDVBScan::~eDVBScan()
29 {
30 }
31
32 int eDVBScan::isValidONIDTSID(eOriginalNetworkID onid, eTransportStreamID tsid)
33 {
34         switch (onid.get())
35         {
36         case 0:
37         case 0xFFFF:
38         case 0x1111:
39                 return 0;
40         case 1:
41                 return tsid>1;
42         case 0x00B1:
43                 return tsid != 0x00B0;
44         case 0x0002:
45                 return tsid != 0x07E8;
46         default:
47                 return 1;
48         }
49 }
50
51 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
52 {
53                 // on valid ONIDs, ignore frequency ("sub network") part
54         if (isValidONIDTSID(onid, tsid))
55                 hash &= ~0xFFFF;
56         return eDVBNamespace(hash);
57 }
58
59 void eDVBScan::stateChange(iDVBChannel *ch)
60 {
61         int state;
62         if (ch->getState(state))
63                 return;
64         if (m_channel_state == state)
65                 return;
66         
67         if (state == iDVBChannel::state_ok)
68         {
69                 startFilter();
70                 m_channel_state = state;
71         } else if (state == iDVBChannel::state_unavailable)
72         {
73                 m_ch_unavailable.push_back(m_ch_current);
74                 nextChannel();
75         }
76 }
77
78 RESULT eDVBScan::nextChannel()
79 {
80         ePtr<iDVBFrontend> fe;
81
82         m_SDT = 0; m_BAT = 0; m_NIT = 0;
83
84         m_ready = readyBAT;
85         if (m_ch_toScan.empty())
86         {
87                 eDebug("no channels left to scan.");
88                 eDebug("%d channels scanned, %d were unavailable.", 
89                                 m_ch_scanned.size(), m_ch_unavailable.size());
90                 eDebug("%d channels in database.", m_new_channels.size());
91                 m_event(evtFinish);
92                 return -ENOENT;
93         }
94         
95         m_ch_current = m_ch_toScan.front();
96         m_ch_toScan.pop_front();
97         
98         if (m_channel->getFrontend(fe))
99         {
100                 m_event(evtFail);
101                 return -ENOTSUP;
102         }
103         
104         m_channel_state = iDVBChannel::state_idle;
105         if (fe->tune(*m_ch_current))
106         {
107                 m_event(evtFail);
108                 return -EINVAL;
109         }
110                 
111         m_event(evtUpdate);
112         return 0;
113 }
114
115 RESULT eDVBScan::startFilter()
116 {
117         assert(m_demux);
118         
119         m_SDT = new eTable<ServiceDescriptionTable>();
120         if (m_SDT->start(m_demux, eDVBSDTSpec()))
121                 return -1;
122         CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
123
124         m_NIT = 0;
125         m_NIT = new eTable<NetworkInformationTable>();
126         if (m_NIT->start(m_demux, eDVBNITSpec()))
127                 return -1;
128         CONNECT(m_NIT->tableReady, eDVBScan::NITready);
129         
130         m_BAT = new eTable<BouquetAssociationTable>();
131         if (m_BAT->start(m_demux, eDVBBATSpec()))
132                 return -1;
133         CONNECT(m_BAT->tableReady, eDVBScan::BATready);
134         
135         return 0;
136 }
137
138 void eDVBScan::SDTready(int err)
139 {
140         SCAN_eDebug("got sdt");
141         m_ready |= readySDT;
142         if (!err)
143                 m_ready |= validSDT;
144         channelDone();
145 }
146
147 void eDVBScan::NITready(int err)
148 {
149         SCAN_eDebug("got nit, err %d", err);
150         m_ready |= readyNIT;
151         if (!err)
152                 m_ready |= validNIT;
153         channelDone();
154 }
155
156 void eDVBScan::BATready(int err)
157 {
158         SCAN_eDebug("got bat");
159         m_ready |= readyBAT;
160         if (!err)
161                 m_ready |= validBAT;
162         channelDone();
163 }
164
165 void eDVBScan::addChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
166 {
167                 /* add it to the list of known channels. */
168         if (chid)
169                 m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
170         
171                 /* check if we don't already have that channel ... */
172                 
173                 /* ... in the list of channels to scan */
174         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end(); ++i)
175                 if (sameChannel(*i, feparm))
176                         return;
177
178                 /* ... in the list of successfully scanned channels */
179         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
180                 if (sameChannel(*i, feparm))
181                         return;
182                 
183                 /* ... in the list of unavailable channels */
184         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
185                 if (sameChannel(*i, feparm))
186                         return;
187
188                 /* ... on the current channel */
189         if (sameChannel(m_ch_current, feparm))
190                 return;
191
192                 /* otherwise, add it to the todo list. */
193         m_ch_toScan.push_back(feparm);
194 }
195
196 int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *ch2) const
197 {
198         int diff;
199         if (ch1->calculateDifference(ch2, diff))
200                 return 0;
201         if (diff < 4000) // more than 4mhz difference?
202                 return 1;
203         return 0;
204 }
205
206 void eDVBScan::channelDone()
207 {
208         if (m_ready & validSDT)
209         {
210                 unsigned long hash = 0;
211                 m_ch_current->getHash(hash);
212                 
213                 eDVBNamespace dvbnamespace = buildNamespace(
214                         (**m_SDT->getSections().begin()).getOriginalNetworkId(),
215                         (**m_SDT->getSections().begin()).getTransportStreamId(),
216                         hash);
217                 
218                 SCAN_eDebug("SDT: ");
219                 ServiceDescriptionTableConstIterator i;
220                 for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
221                         processSDT(dvbnamespace, **i);
222                 m_ready &= ~validSDT;
223         }
224         
225         if (m_ready & validNIT)
226         {
227                 SCAN_eDebug("dumping NIT");
228                 NetworkInformationTableConstIterator i;
229                 for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
230                 {
231                         const TransportStreamInfoVector &tsinfovec = *(*i)->getTsInfo();
232                         
233                         for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin()); 
234                                 tsinfo != tsinfovec.end(); ++tsinfo)
235                         {
236                                 SCAN_eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
237                                         (*tsinfo)->getOriginalNetworkId());
238                                 
239                                 eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
240                                 eTransportStreamID tsid = (*tsinfo)->getTransportStreamId();
241                                 
242                                 for (DescriptorConstIterator desc = (*tsinfo)->getDescriptors()->begin();
243                                                 desc != (*tsinfo)->getDescriptors()->end(); ++desc)
244                                 {
245                                         switch ((*desc)->getTag())
246                                         {
247 //                                      case SERVICE_LIST_DESCRIPTOR:
248                                         case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
249                                         {
250                                                 SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
251                                                 SCAN_eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d", 
252                                                                 d.getFrequency(), 
253                                                                 (d.getOrbitalPosition()>>12)&0xF,
254                                                                 (d.getOrbitalPosition()>>8)&0xF,
255                                                                 (d.getOrbitalPosition()>>4)&0xF,
256                                                                 d.getOrbitalPosition()&0xF, d.getWestEastFlag()?'E':'W',
257                                                                 d.getPolarization() ? "hor" : "vert",
258                                                                 d.getModulation(), d.getSymbolRate(), d.getFecInner());
259                                                 
260                                                         /* some sanity checking: below 100MHz is invalid */
261                                                 if (d.getFrequency() < 10000)
262                                                         break;
263                                                 
264                                                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
265                                                 eDVBFrontendParametersSatellite sat;
266                                                 sat.set(d);
267                                                 feparm->setDVBS(sat);
268                                                 unsigned long hash=0;
269                                                 feparm->getHash(hash);
270                                                 
271                                                 eDVBNamespace ns = buildNamespace(onid, tsid, hash);
272
273                                                 addChannel(
274                                                                 eDVBChannelID(ns, tsid, onid),
275                                                                 feparm);
276                                                 break;
277                                         }
278                                         default:
279                                                 SCAN_eDebug("descr<%x>", (*desc)->getTag());
280                                                 break;
281                                         }
282                                 }
283                                 
284                         }
285                 }
286                 m_ready &= ~validNIT;
287         }
288         
289         if ((m_ready  & readyAll) != readyAll)
290                 return;
291         SCAN_eDebug("channel done!");
292         m_ch_scanned.push_back(m_ch_current);
293         nextChannel();
294 }
295
296 void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_transponders)
297 {
298         m_ch_toScan.clear();
299         m_ch_scanned.clear();
300         m_ch_unavailable.clear();
301         m_new_channels.clear();
302         m_new_services.clear();
303         m_ch_toScan.insert(m_ch_toScan.end(), known_transponders.begin(), known_transponders.end());
304         nextChannel();
305 }
306
307 void eDVBScan::insertInto(iDVBChannelList *db)
308 {
309         for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
310                         ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
311                 db->addChannelToList(ch->first, ch->second);
312         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
313                 service(m_new_services.begin()); service != m_new_services.end(); ++service)
314         {
315                 ePtr<eDVBService> dvb_service;
316                 if (!db->getService(service->first, dvb_service))
317                         *dvb_service = *service->second;
318                 else
319                         db->addService(service->first, service->second);
320         }
321 }
322
323 RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionTable &sdt)
324 {
325         const ServiceDescriptionVector &services = *sdt.getDescriptions();
326         SCAN_eDebug("ONID: %04x", sdt.getOriginalNetworkId());
327         eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
328         
329         for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
330         {
331                 SCAN_eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
332
333                 eServiceReferenceDVB ref;
334                 ePtr<eDVBService> service = new eDVBService;
335                 
336                 ref.set(chid);
337                 ref.setServiceID((*s)->getServiceId());
338
339                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
340                                 desc != (*s)->getDescriptors()->end(); ++desc)
341                         if ((*desc)->getTag() == SERVICE_DESCRIPTOR)
342                                 ref.setServiceType(((ServiceDescriptor&)**desc).getServiceType());
343                 
344                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
345                                 desc != (*s)->getDescriptors()->end(); ++desc)
346                 {
347                         switch ((*desc)->getTag())
348                         {
349                         case SERVICE_DESCRIPTOR:
350                         {
351                                 ServiceDescriptor &d = (ServiceDescriptor&)**desc;
352                                 SCAN_eDebug("name '%s', provider_name '%s'", d.getServiceName().c_str(), d.getServiceProviderName().c_str());
353                                 service->m_service_name = d.getServiceName();
354                                 service->m_provider_name = d.getServiceProviderName();
355                                 break;
356                         }
357                         case CA_IDENTIFIER_DESCRIPTOR:
358                         {
359                                 CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
360                                 const CaSystemIdVector &caids = *d.getCaSystemIds();
361                                 SCAN_eDebugNoNewLine("CA ");
362                                 for (CaSystemIdVector::const_iterator i(caids.begin()); i != caids.end(); ++i)
363                                 {
364                                         SCAN_eDebugNoNewLine("%04x ", *i);
365                                         service->m_ca.insert(*i);
366                                 }
367                                 SCAN_eDebug("");
368                                 break;
369                         }
370                         default:
371                                 SCAN_eDebug("descr<%x>", (*desc)->getTag());
372                                 break;
373                         }
374                 }
375                 
376                 m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));               
377         }
378         return 0;
379 }
380
381 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
382 {
383         connection = new eConnection(this, m_event.connect(event));
384         return 0;
385 }
386
387 void eDVBScan::getStats(int &transponders_done, int &transponders_total, int &services)
388 {
389         transponders_done = m_ch_scanned.size() + m_ch_unavailable.size();
390         transponders_total = m_ch_toScan.size() + transponders_done;
391         services = m_new_services.size();
392 }