- fixed console input mode restore
[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...)
17 #define SCAN_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                 return -ENOTSUP;
100         
101         m_channel_state = iDVBChannel::state_idle;
102         if (fe->tune(*m_ch_current))
103                 return -EINVAL;
104                 
105         m_event(evtUpdate);
106         return 0;
107 }
108
109 RESULT eDVBScan::startFilter()
110 {
111         assert(m_demux);
112         
113         m_SDT = new eTable<ServiceDescriptionTable>();
114         if (m_SDT->start(m_demux, eDVBSDTSpec()))
115                 return -1;
116         CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
117
118         m_NIT = 0;
119         m_NIT = new eTable<NetworkInformationTable>();
120         if (m_NIT->start(m_demux, eDVBNITSpec()))
121                 return -1;
122         CONNECT(m_NIT->tableReady, eDVBScan::NITready);
123         
124         m_BAT = new eTable<BouquetAssociationTable>();
125         if (m_BAT->start(m_demux, eDVBBATSpec()))
126                 return -1;
127         CONNECT(m_BAT->tableReady, eDVBScan::BATready);
128         
129         return 0;
130 }
131
132 void eDVBScan::SDTready(int err)
133 {
134         SCAN_eDebug("got sdt");
135         m_ready |= readySDT;
136         if (!err)
137                 m_ready |= validSDT;
138         channelDone();
139 }
140
141 void eDVBScan::NITready(int err)
142 {
143         SCAN_eDebug("got nit, err %d", err);
144         m_ready |= readyNIT;
145         if (!err)
146                 m_ready |= validNIT;
147         channelDone();
148 }
149
150 void eDVBScan::BATready(int err)
151 {
152         SCAN_eDebug("got bat");
153         m_ready |= readyBAT;
154         if (!err)
155                 m_ready |= validBAT;
156         channelDone();
157 }
158
159 void eDVBScan::addChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
160 {
161                 /* add it to the list of known channels. */
162         if (chid)
163                 m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
164         
165                 /* check if we don't already have that channel ... */
166                 
167                 /* ... in the list of channels to scan */
168         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end(); ++i)
169                 if (sameChannel(*i, feparm))
170                         return;
171
172                 /* ... in the list of successfully scanned channels */
173         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
174                 if (sameChannel(*i, feparm))
175                         return;
176                 
177                 /* ... in the list of unavailable channels */
178         for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
179                 if (sameChannel(*i, feparm))
180                         return;
181
182                 /* ... on the current channel */
183         if (sameChannel(m_ch_current, feparm))
184                 return;
185
186                 /* otherwise, add it to the todo list. */
187         m_ch_toScan.push_back(feparm);
188 }
189
190 int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *ch2) const
191 {
192         int diff;
193         if (ch1->calculateDifference(ch2, diff))
194                 return 0;
195         if (diff < 4000) // more than 4mhz difference?
196                 return 1;
197         return 0;
198 }
199
200 void eDVBScan::channelDone()
201 {
202         if (m_ready & validSDT)
203         {
204                 unsigned long hash = 0;
205                 m_ch_current->getHash(hash);
206                 
207                 eDVBNamespace dvbnamespace = buildNamespace(
208                         (**m_SDT->getSections().begin()).getOriginalNetworkId(),
209                         (**m_SDT->getSections().begin()).getTransportStreamId(),
210                         hash);
211                 
212                 SCAN_eDebug("SDT: ");
213                 ServiceDescriptionTableConstIterator i;
214                 for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
215                         processSDT(dvbnamespace, **i);
216                 m_ready &= ~validSDT;
217         }
218         
219         if (m_ready & validNIT)
220         {
221                 SCAN_eDebug("dumping NIT");
222                 NetworkInformationTableConstIterator i;
223                 for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
224                 {
225                         const TransportStreamInfoVector &tsinfovec = *(*i)->getTsInfo();
226                         
227                         for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin()); 
228                                 tsinfo != tsinfovec.end(); ++tsinfo)
229                         {
230                                 SCAN_eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
231                                         (*tsinfo)->getOriginalNetworkId());
232                                 
233                                 eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
234                                 eTransportStreamID tsid = (*tsinfo)->getTransportStreamId();
235                                 
236                                 for (DescriptorConstIterator desc = (*tsinfo)->getDescriptors()->begin();
237                                                 desc != (*tsinfo)->getDescriptors()->end(); ++desc)
238                                 {
239                                         switch ((*desc)->getTag())
240                                         {
241 //                                      case SERVICE_LIST_DESCRIPTOR:
242                                         case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
243                                         {
244                                                 SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
245                                                 SCAN_eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d", 
246                                                                 d.getFrequency(), 
247                                                                 (d.getOrbitalPosition()>>12)&0xF,
248                                                                 (d.getOrbitalPosition()>>8)&0xF,
249                                                                 (d.getOrbitalPosition()>>4)&0xF,
250                                                                 d.getOrbitalPosition()&0xF, d.getWestEastFlag()?'E':'W',
251                                                                 d.getPolarization() ? "hor" : "vert",
252                                                                 d.getModulation(), d.getSymbolRate(), d.getFecInner());
253                                                 
254                                                         /* some sanity checking: below 100MHz is invalid */
255                                                 if (d.getFrequency() < 10000)
256                                                         break;
257                                                 
258                                                 ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
259                                                 eDVBFrontendParametersSatellite sat;
260                                                 sat.set(d);
261                                                 feparm->setDVBS(sat);
262                                                 unsigned long hash=0;
263                                                 feparm->getHash(hash);
264                                                 
265                                                 eDVBNamespace ns = buildNamespace(onid, tsid, hash);
266
267                                                 addChannel(
268                                                                 eDVBChannelID(ns, tsid, onid),
269                                                                 feparm);
270                                                 break;
271                                         }
272                                         default:
273                                                 SCAN_eDebug("descr<%x>", (*desc)->getTag());
274                                                 break;
275                                         }
276                                 }
277                                 
278                         }
279                 }
280                 m_ready &= ~validNIT;
281         }
282         
283         if ((m_ready  & readyAll) != readyAll)
284                 return;
285         SCAN_eDebug("channel done!");
286         m_ch_scanned.push_back(m_ch_current);
287         nextChannel();
288 }
289
290 void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_transponders)
291 {
292         m_ch_toScan.clear();
293         m_ch_scanned.clear();
294         m_ch_unavailable.clear();
295         m_new_channels.clear();
296         m_new_services.clear();
297         m_ch_toScan.insert(m_ch_toScan.end(), known_transponders.begin(), known_transponders.end());
298         nextChannel();
299 }
300
301 void eDVBScan::insertInto(iDVBChannelList *db)
302 {
303         for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
304                         ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
305                 db->addChannelToList(ch->first, ch->second);
306         for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
307                 service(m_new_services.begin()); service != m_new_services.end(); ++service)
308         {
309                 ePtr<eDVBService> dvb_service;
310                 if (!db->getService(service->first, dvb_service))
311                         *dvb_service = *service->second;
312                 else
313                         db->addService(service->first, service->second);
314         }
315 }
316
317 RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionTable &sdt)
318 {
319         const ServiceDescriptionVector &services = *sdt.getDescriptions();
320         SCAN_eDebug("ONID: %04x", sdt.getOriginalNetworkId());
321         eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
322         
323         for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
324         {
325                 SCAN_eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
326
327                 eServiceReferenceDVB ref;
328                 ePtr<eDVBService> service = new eDVBService;
329                 
330                 ref.set(chid);
331                 ref.setServiceID((*s)->getServiceId());
332
333                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
334                                 desc != (*s)->getDescriptors()->end(); ++desc)
335                         if ((*desc)->getTag() == SERVICE_DESCRIPTOR)
336                                 ref.setServiceType(((ServiceDescriptor&)**desc).getServiceType());
337                 
338                 for (DescriptorConstIterator desc = (*s)->getDescriptors()->begin();
339                                 desc != (*s)->getDescriptors()->end(); ++desc)
340                 {
341                         switch ((*desc)->getTag())
342                         {
343                         case SERVICE_DESCRIPTOR:
344                         {
345                                 ServiceDescriptor &d = (ServiceDescriptor&)**desc;
346                                 SCAN_eDebug("name '%s', provider_name '%s'", d.getServiceName().c_str(), d.getServiceProviderName().c_str());
347                                 service->m_service_name = d.getServiceName();
348                                 service->m_provider_name = d.getServiceProviderName();
349                                 break;
350                         }
351                         case CA_IDENTIFIER_DESCRIPTOR:
352                         {
353                                 CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
354                                 const CaSystemIdVector &caids = *d.getCaSystemIds();
355                                 SCAN_eDebugNoNewLine("CA ");
356                                 for (CaSystemIdVector::const_iterator i(caids.begin()); i != caids.end(); ++i)
357                                 {
358                                         SCAN_eDebugNoNewLine("%04x ", *i);
359                                         service->m_ca.insert(*i);
360                                 }
361                                 SCAN_eDebug("");
362                                 break;
363                         }
364                         default:
365                                 SCAN_eDebug("descr<%x>", (*desc)->getTag());
366                                 break;
367                         }
368                 }
369                 
370                 m_new_services.insert(std::pair<eServiceReferenceDVB, ePtr<eDVBService> >(ref, service));               
371         }
372         return 0;
373 }
374
375 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
376 {
377         connection = new eConnection(this, m_event.connect(event));
378         return 0;
379 }
380
381 void eDVBScan::getStats(int &transponders_done, int &transponders_total, int &services)
382 {
383         transponders_done = m_ch_scanned.size() + m_ch_unavailable.size();
384         transponders_total = m_ch_toScan.size() + transponders_done;
385         services = m_new_services.size();
386 }