use orbital position in isValidONIDTSID
[enigma2.git] / lib / dvb / scan.cpp
index 68121c447dc6e3602ea1df75c53b1de44027b883..961812f697a063bcd1b8b973fee7cb6b04b53b52 100644 (file)
@@ -1,22 +1,28 @@
 #include <lib/dvb/idvb.h>
 #include <lib/dvb/idvb.h>
-#include <lib/dvb_si/sdt.h>
-#include <lib/dvb_si/nit.h>
-#include <lib/dvb_si/bat.h>
-#include <lib/dvb_si/descriptor_tag.h>
-#include <lib/dvb_si/service_descriptor.h>
-#include <lib/dvb_si/satellite_delivery_system_descriptor.h>
-#include <lib/dvb_si/ca_identifier_descriptor.h>
+#include <dvbsi++/service_description_section.h>
+#include <dvbsi++/network_information_section.h>
+#include <dvbsi++/bouquet_association_section.h>
+#include <dvbsi++/descriptor_tag.h>
+#include <dvbsi++/service_descriptor.h>
+#include <dvbsi++/satellite_delivery_system_descriptor.h>
+#include <dvbsi++/ca_identifier_descriptor.h>
 #include <lib/dvb/specs.h>
 #include <lib/dvb/esection.h>
 #include <lib/dvb/scan.h>
 #include <lib/dvb/frontend.h>
 #include <lib/base/eerror.h>
 #include <lib/dvb/specs.h>
 #include <lib/dvb/esection.h>
 #include <lib/dvb/scan.h>
 #include <lib/dvb/frontend.h>
 #include <lib/base/eerror.h>
+#include <lib/base/estring.h>
 #include <errno.h>
 
 #include <errno.h>
 
+#define SCAN_eDebug(x...) eDebug(x)
+#define SCAN_eDebugNoNewLine(x...) eDebugNoNewLine(x)
+
+DEFINE_REF(eDVBScan);
+
 eDVBScan::eDVBScan(iDVBChannel *channel): m_channel(channel)
 {
        if (m_channel->getDemux(m_demux))
 eDVBScan::eDVBScan(iDVBChannel *channel): m_channel(channel)
 {
        if (m_channel->getDemux(m_demux))
-               eDebug("scan: failed to allocate demux!");
+               SCAN_eDebug("scan: failed to allocate demux!");
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
 }
 
        m_channel->connectStateChange(slot(*this, &eDVBScan::stateChange), m_stateChanged_connection);
 }
 
@@ -24,29 +30,28 @@ eDVBScan::~eDVBScan()
 {
 }
 
 {
 }
 
-int eDVBScan::isValidONIDTSID(eOriginalNetworkID onid, eTransportStreamID tsid)
+int eDVBScan::isValidONIDTSID(int orbital_position, eOriginalNetworkID onid, eTransportStreamID tsid)
 {
        switch (onid.get())
        {
        case 0:
 {
        switch (onid.get())
        {
        case 0:
-       case 0xFFFF:
        case 0x1111:
                return 0;
        case 1:
        case 0x1111:
                return 0;
        case 1:
-               return tsid>1;
+               return orbital_position == 192;
        case 0x00B1:
                return tsid != 0x00B0;
        case 0x0002:
        case 0x00B1:
                return tsid != 0x00B0;
        case 0x0002:
-               return tsid != 0x07E8;
+               return abs(orbital_position-282) < 6;
        default:
        default:
-               return 1;
+               return onid.get() < 0xFF00;
        }
 }
 
 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
 {
                // on valid ONIDs, ignore frequency ("sub network") part
        }
 }
 
 eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
 {
                // on valid ONIDs, ignore frequency ("sub network") part
-       if (isValidONIDTSID(onid, tsid))
+       if (isValidONIDTSID((hash >> 16) & 0xFFFF, onid, tsid))
                hash &= ~0xFFFF;
        return eDVBNamespace(hash);
 }
                hash &= ~0xFFFF;
        return eDVBNamespace(hash);
 }
@@ -63,11 +68,12 @@ void eDVBScan::stateChange(iDVBChannel *ch)
        {
                startFilter();
                m_channel_state = state;
        {
                startFilter();
                m_channel_state = state;
-       } else if (state == iDVBChannel::state_unavailable)
+       } else if (state == iDVBChannel::state_failed)
        {
                m_ch_unavailable.push_back(m_ch_current);
                nextChannel();
        }
        {
                m_ch_unavailable.push_back(m_ch_current);
                nextChannel();
        }
+                       /* unavailable will timeout, anyway. */
 }
 
 RESULT eDVBScan::nextChannel()
 }
 
 RESULT eDVBScan::nextChannel()
@@ -76,7 +82,17 @@ RESULT eDVBScan::nextChannel()
 
        m_SDT = 0; m_BAT = 0; m_NIT = 0;
 
 
        m_SDT = 0; m_BAT = 0; m_NIT = 0;
 
-       m_ready = readyBAT;
+       m_ready = 0;
+       
+               /* check what we need */
+       m_ready_all = readySDT;
+       
+       if (m_flags & scanNetworkSearch)
+               m_ready_all |= readyNIT;
+       
+       if (m_flags & scanSearchBAT)
+               m_ready_all |= readyBAT;
+       
        if (m_ch_toScan.empty())
        {
                eDebug("no channels left to scan.");
        if (m_ch_toScan.empty())
        {
                eDebug("no channels left to scan.");
@@ -88,14 +104,23 @@ RESULT eDVBScan::nextChannel()
        }
        
        m_ch_current = m_ch_toScan.front();
        }
        
        m_ch_current = m_ch_toScan.front();
+       m_chid_current = eDVBChannelID();
+       
        m_ch_toScan.pop_front();
        
        if (m_channel->getFrontend(fe))
        m_ch_toScan.pop_front();
        
        if (m_channel->getFrontend(fe))
+       {
+               m_event(evtFail);
                return -ENOTSUP;
                return -ENOTSUP;
+       }
        
        m_channel_state = iDVBChannel::state_idle;
        if (fe->tune(*m_ch_current))
        
        m_channel_state = iDVBChannel::state_idle;
        if (fe->tune(*m_ch_current))
+       {
+               return nextChannel();
+               m_event(evtFail);
                return -EINVAL;
                return -EINVAL;
+       }
                
        m_event(evtUpdate);
        return 0;
                
        m_event(evtUpdate);
        return 0;
@@ -105,28 +130,42 @@ RESULT eDVBScan::startFilter()
 {
        assert(m_demux);
        
 {
        assert(m_demux);
        
-       m_SDT = new eTable<ServiceDescriptionTable>();
-       if (m_SDT->start(m_demux, eDVBSDTSpec()))
-               return -1;
-       CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
+                       /* only start required filters filter */
+       
+       m_SDT = 0;
+
+       if (m_ready_all & readySDT)
+       {
+               m_SDT = new eTable<ServiceDescriptionSection>();
+               if (m_SDT->start(m_demux, eDVBSDTSpec()))
+                       return -1;
+               CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
+       }
 
        m_NIT = 0;
 
        m_NIT = 0;
-       m_NIT = new eTable<NetworkInformationTable>();
-       if (m_NIT->start(m_demux, eDVBNITSpec()))
-               return -1;
-       CONNECT(m_NIT->tableReady, eDVBScan::NITready);
-       
-       m_BAT = new eTable<BouquetAssociationTable>();
-       if (m_BAT->start(m_demux, eDVBBATSpec()))
-               return -1;
-       CONNECT(m_BAT->tableReady, eDVBScan::BATready);
+       if (m_ready_all & readyNIT)
+       {
+               m_NIT = new eTable<NetworkInformationSection>();
+               if (m_NIT->start(m_demux, eDVBNITSpec()))
+                       return -1;
+               CONNECT(m_NIT->tableReady, eDVBScan::NITready);
+       }
+
+       m_BAT = 0;
+       if (m_ready_all & readyBAT)
+       {
+               m_BAT = new eTable<BouquetAssociationSection>();
+               if (m_BAT->start(m_demux, eDVBBATSpec()))
+                       return -1;
+               CONNECT(m_BAT->tableReady, eDVBScan::BATready);
+       }
        
        return 0;
 }
 
 void eDVBScan::SDTready(int err)
 {
        
        return 0;
 }
 
 void eDVBScan::SDTready(int err)
 {
-       eDebug("got sdt");
+       SCAN_eDebug("got sdt");
        m_ready |= readySDT;
        if (!err)
                m_ready |= validSDT;
        m_ready |= readySDT;
        if (!err)
                m_ready |= validSDT;
@@ -135,7 +174,7 @@ void eDVBScan::SDTready(int err)
 
 void eDVBScan::NITready(int err)
 {
 
 void eDVBScan::NITready(int err)
 {
-       eDebug("got nit, err %d", err);
+       SCAN_eDebug("got nit, err %d", err);
        m_ready |= readyNIT;
        if (!err)
                m_ready |= validNIT;
        m_ready |= readyNIT;
        if (!err)
                m_ready |= validNIT;
@@ -144,19 +183,22 @@ void eDVBScan::NITready(int err)
 
 void eDVBScan::BATready(int err)
 {
 
 void eDVBScan::BATready(int err)
 {
-       eDebug("got bat");
+       SCAN_eDebug("got bat");
        m_ready |= readyBAT;
        if (!err)
                m_ready |= validBAT;
        channelDone();
 }
 
        m_ready |= readyBAT;
        if (!err)
                m_ready |= validBAT;
        channelDone();
 }
 
-void eDVBScan::addChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
+void eDVBScan::addKnownGoodChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
 {
                /* add it to the list of known channels. */
        if (chid)
                m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
 {
                /* add it to the list of known channels. */
        if (chid)
                m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
-       
+}
+
+void eDVBScan::addChannelToScan(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
+{
                /* check if we don't already have that channel ... */
                
                /* ... in the list of channels to scan */
                /* check if we don't already have that channel ... */
                
                /* ... in the list of channels to scan */
@@ -204,8 +246,8 @@ void eDVBScan::channelDone()
                        (**m_SDT->getSections().begin()).getTransportStreamId(),
                        hash);
                
                        (**m_SDT->getSections().begin()).getTransportStreamId(),
                        hash);
                
-               eDebug("SDT: ");
-               ServiceDescriptionTableConstIterator i;
+               SCAN_eDebug("SDT: ");
+               std::vector<ServiceDescriptionSection*>::const_iterator i;
                for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
                        processSDT(dvbnamespace, **i);
                m_ready &= ~validSDT;
                for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
                        processSDT(dvbnamespace, **i);
                m_ready &= ~validSDT;
@@ -213,16 +255,16 @@ void eDVBScan::channelDone()
        
        if (m_ready & validNIT)
        {
        
        if (m_ready & validNIT)
        {
-               eDebug("dumping NIT");
-               NetworkInformationTableConstIterator i;
+               SCAN_eDebug("dumping NIT");
+               std::vector<NetworkInformationSection*>::const_iterator i;
                for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
                {
                for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
                {
-                       const TransportStreamInfoVector &tsinfovec = *(*i)->getTsInfo();
+                       const TransportStreamInfoList &tsinfovec = *(*i)->getTsInfo();
                        
                        for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin()); 
                                tsinfo != tsinfovec.end(); ++tsinfo)
                        {
                        
                        for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin()); 
                                tsinfo != tsinfovec.end(); ++tsinfo)
                        {
-                               eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
+                               SCAN_eDebug("TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
                                        (*tsinfo)->getOriginalNetworkId());
                                
                                eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
                                        (*tsinfo)->getOriginalNetworkId());
                                
                                eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
@@ -237,7 +279,7 @@ void eDVBScan::channelDone()
                                        case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
                                        {
                                                SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
                                        case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
                                        {
                                                SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
-                                               eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d", 
+                                               SCAN_eDebug("%d kHz, %d%d%d.%d%c %s MOD:%d %d symb/s, fec %d", 
                                                                d.getFrequency(), 
                                                                (d.getOrbitalPosition()>>12)&0xF,
                                                                (d.getOrbitalPosition()>>8)&0xF,
                                                                d.getFrequency(), 
                                                                (d.getOrbitalPosition()>>12)&0xF,
                                                                (d.getOrbitalPosition()>>8)&0xF,
@@ -258,14 +300,19 @@ void eDVBScan::channelDone()
                                                feparm->getHash(hash);
                                                
                                                eDVBNamespace ns = buildNamespace(onid, tsid, hash);
                                                feparm->getHash(hash);
                                                
                                                eDVBNamespace ns = buildNamespace(onid, tsid, hash);
-
-                                               addChannel(
-                                                               eDVBChannelID(ns, tsid, onid),
-                                                               feparm);
+                                               
+                                               if (m_chid_current && ((ns.get() ^ m_chid_current.dvbnamespace.get()) & 0xFFFF0000))
+                                                       eDebug("dropping this transponder, it's on another satellite.");
+                                               else
+                                               {
+                                                       addChannelToScan(
+                                                                       eDVBChannelID(ns, tsid, onid),
+                                                                       feparm);
+                                               }
                                                break;
                                        }
                                        default:
                                                break;
                                        }
                                        default:
-                                               eDebug("descr<%x>", (*desc)->getTag());
+                                               SCAN_eDebug("descr<%x>", (*desc)->getTag());
                                                break;
                                        }
                                }
                                                break;
                                        }
                                }
@@ -275,15 +322,43 @@ void eDVBScan::channelDone()
                m_ready &= ~validNIT;
        }
        
                m_ready &= ~validNIT;
        }
        
-       if ((m_ready  & readyAll) != readyAll)
+       if ((m_ready  & m_ready_all) != m_ready_all)
                return;
                return;
-       eDebug("channel done!");
+       SCAN_eDebug("channel done!");
+       
+               /* if we had services on this channel, we declare
+                  this channels as "known good". add it.
+                  
+                  (TODO: not yet implemented)
+                  a NIT entry could have possible overridden
+                  our frontend data with more exact data.
+                  
+                  (TODO: not yet implemented)
+                  the tuning process could have lead to more
+                  exact data than the user entered.
+                  
+                  The channel id was probably corrected
+                  by the data written in the SDT. this is
+                  important, as "initial transponder lists"
+                  usually don't have valid CHIDs (and that's
+                  good).
+                  
+                  These are the reasons for adding the transponder
+                  here, and not before.
+               */
+       
+       if (!m_chid_current)
+               eWarning("SCAN: the current channel's ID was not corrected - not adding channel.");
+       else
+               addKnownGoodChannel(m_chid_current, m_ch_current);
+       
        m_ch_scanned.push_back(m_ch_current);
        nextChannel();
 }
 
        m_ch_scanned.push_back(m_ch_current);
        nextChannel();
 }
 
-void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_transponders)
+void eDVBScan::start(const eSmartPtrList<iDVBFrontendParameters> &known_transponders, int flags)
 {
 {
+       m_flags = flags;
        m_ch_toScan.clear();
        m_ch_scanned.clear();
        m_ch_unavailable.clear();
        m_ch_toScan.clear();
        m_ch_scanned.clear();
        m_ch_unavailable.clear();
@@ -293,7 +368,7 @@ void eDVBScan::start(const std::list<ePtr<iDVBFrontendParameters> > &known_trans
        nextChannel();
 }
 
        nextChannel();
 }
 
-void eDVBScan::insertInto(eDVBDB *db)
+void eDVBScan::insertInto(iDVBChannelList *db)
 {
        for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
                        ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
 {
        for (std::map<eDVBChannelID, ePtr<iDVBFrontendParameters> >::const_iterator 
                        ch(m_new_channels.begin()); ch != m_new_channels.end(); ++ch)
@@ -309,15 +384,19 @@ void eDVBScan::insertInto(eDVBDB *db)
        }
 }
 
        }
 }
 
-RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionTable &sdt)
+RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescriptionSection &sdt)
 {
 {
-       const ServiceDescriptionVector &services = *sdt.getDescriptions();
-       eDebug("ONID: %04x", sdt.getOriginalNetworkId());
+       const ServiceDescriptionList &services = *sdt.getDescriptions();
+       SCAN_eDebug("ONID: %04x", sdt.getOriginalNetworkId());
        eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
        
        eDVBChannelID chid(dvbnamespace, sdt.getTransportStreamId(), sdt.getOriginalNetworkId());
        
+               /* save correct CHID for this channel if this is an ACTUAL_SDT */
+       if (sdt.getTableId() == TID_SDT_ACTUAL)
+               m_chid_current = chid;
+       
        for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
        {
        for (ServiceDescriptionConstIterator s(services.begin()); s != services.end(); ++s)
        {
-               eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
+               SCAN_eDebugNoNewLine("SID %04x: ", (*s)->getServiceId());
 
                eServiceReferenceDVB ref;
                ePtr<eDVBService> service = new eDVBService;
 
                eServiceReferenceDVB ref;
                ePtr<eDVBService> service = new eDVBService;
@@ -338,26 +417,28 @@ RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescription
                        case SERVICE_DESCRIPTOR:
                        {
                                ServiceDescriptor &d = (ServiceDescriptor&)**desc;
                        case SERVICE_DESCRIPTOR:
                        {
                                ServiceDescriptor &d = (ServiceDescriptor&)**desc;
-                               eDebug("name '%s', provider_name '%s'", d.getServiceName().c_str(), d.getServiceProviderName().c_str());
-                               service->m_service_name = d.getServiceName();
-                               service->m_provider_name = d.getServiceProviderName();
+                               service->m_service_name = convertDVBUTF8(d.getServiceName());
+                               service->genSortName();
+
+                               service->m_provider_name = convertDVBUTF8(d.getServiceProviderName());
+                               SCAN_eDebug("name '%s', provider_name '%s'", service->m_service_name.c_str(), service->m_provider_name.c_str());
                                break;
                        }
                        case CA_IDENTIFIER_DESCRIPTOR:
                        {
                                CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
                                break;
                        }
                        case CA_IDENTIFIER_DESCRIPTOR:
                        {
                                CaIdentifierDescriptor &d = (CaIdentifierDescriptor&)**desc;
-                               const CaSystemIdVector &caids = *d.getCaSystemIds();
-                               eDebugNoNewLine("CA ");
-                               for (CaSystemIdVector::const_iterator i(caids.begin()); i != caids.end(); ++i)
+                               const CaSystemIdList &caids = *d.getCaSystemIds();
+                               SCAN_eDebugNoNewLine("CA ");
+                               for (CaSystemIdList::const_iterator i(caids.begin()); i != caids.end(); ++i)
                                {
                                {
-                                       eDebugNoNewLine("%04x ", *i);
+                                       SCAN_eDebugNoNewLine("%04x ", *i);
                                        service->m_ca.insert(*i);
                                }
                                        service->m_ca.insert(*i);
                                }
-                               eDebug("");
+                               SCAN_eDebug("");
                                break;
                        }
                        default:
                                break;
                        }
                        default:
-                               eDebug("descr<%x>", (*desc)->getTag());
+                               SCAN_eDebug("descr<%x>", (*desc)->getTag());
                                break;
                        }
                }
                                break;
                        }
                }
@@ -369,6 +450,13 @@ RESULT eDVBScan::processSDT(eDVBNamespace dvbnamespace, const ServiceDescription
 
 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
 {
 
 RESULT eDVBScan::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
 {
-       connection = new eConnection(m_event.connect(event));
+       connection = new eConnection(this, m_event.connect(event));
        return 0;
 }
        return 0;
 }
+
+void eDVBScan::getStats(int &transponders_done, int &transponders_total, int &services)
+{
+       transponders_done = m_ch_scanned.size() + m_ch_unavailable.size();
+       transponders_total = m_ch_toScan.size() + transponders_done;
+       services = m_new_services.size();
+}