don't use internals of other classes
[enigma2.git] / lib / dvb / frontend.cpp
index 00886ee4839dc6355361ffee52c64e0aa3b6a951..b8debd81c21754a4caec1b6459a63ceeab047f4c 100644 (file)
@@ -6,6 +6,10 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 
+#ifndef I2C_SLAVE_FORCE
+#define I2C_SLAVE_FORCE        0x0706
+#endif
+
 #if HAVE_DVB_API_VERSION < 3
 #include <ost/frontend.h>
 #include <ost/sec.h>
@@ -23,9 +27,9 @@
 #include <linux/dvb/frontend.h>
 #endif
 
-#include <lib/dvb_si/satellite_delivery_system_descriptor.h>
-#include <lib/dvb_si/cable_delivery_system_descriptor.h>
-#include <lib/dvb_si/terrestrial_delivery_system_descriptor.h>
+#include <dvbsi++/satellite_delivery_system_descriptor.h>
+#include <dvbsi++/cable_delivery_system_descriptor.h>
+#include <dvbsi++/terrestrial_delivery_system_descriptor.h>
 
 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
 {
@@ -204,7 +208,7 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
 
 DEFINE_REF(eDVBFrontend);
 
-eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1)
+eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1), m_curVoltage(-1)
 {
 #if HAVE_DVB_API_VERSION < 3
        char sec_filename[128];
@@ -277,10 +281,15 @@ eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1)
        m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
        CONNECT(m_sn->activated, eDVBFrontend::feEvent);
        m_sn->start();
-       
+
        m_timeout = new eTimer(eApp);
        CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
-       
+
+       m_tuneTimer = new eTimer(eApp);
+       CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
+
+       memset(m_data, 0xFFFF, sizeof(m_data));
+
        return;
 }
 
@@ -332,7 +341,12 @@ void eDVBFrontend::feEvent(int w)
                        if (m_tuning)
                                state = stateTuning;
                        else
-                               state = stateFailed;
+                       {
+                               state = stateLostLock;
+
+                               if (m_state != stateLostLock)
+                                       eDebug("FIXME: we lost lock, so we might have to retune.");
+                       }
                }
                if (m_state != state)
                {
@@ -354,10 +368,237 @@ void eDVBFrontend::timeout()
                        m_state = state;
                        m_stateChanged(this);
                }
+               m_tuning = 0;
        } else
                m_tuning = 0;
 }
 
+#ifndef FP_IOCTL_GET_ID
+#define FP_IOCTL_GET_ID 0
+#endif
+int eDVBFrontend::readInputpower()
+{
+       int power=0;
+//     if ( eSystemInfo::getInstance()->canMeasureLNBCurrent() )
+       {
+//             switch ( eSystemInfo::getInstance()->getHwType() )
+               {
+//                     case eSystemInfo::DM7000:
+//                     case eSystemInfo::DM7020:
+                       {
+                               // open front prozessor
+                               int fp=::open("/dev/dbox/fp0", O_RDWR);
+                               if (fp < 0)
+                               {
+                                       eDebug("couldn't open fp");
+                                       return -1;
+                               }
+                               static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
+                               if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
+                               {
+                                       eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
+                                       return -1;
+                               }
+                               ::close(fp);
+//                             break;
+                       }
+//                     default:
+//                             eDebug("Inputpower read for platform %d not yet implemented", eSystemInfo::getInstance()->getHwType());
+               }
+       }
+       return power;
+}
+
+bool eDVBFrontend::setSecSequencePos(int steps)
+{
+       eDebug("set sequence pos %d", steps);
+       if (!steps)
+               return false;
+       while( steps > 0 )
+       {
+               if (m_sec_sequence.current() != m_sec_sequence.end())
+                       ++m_sec_sequence.current();
+               --steps;
+       }
+       while( steps < 0 )
+       {
+               if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
+                       --m_sec_sequence.current();
+               ++steps;
+       }
+       return true;
+}
+
+void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
+{
+       int delay=0;
+       if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
+       {
+               switch (m_sec_sequence.current()->cmd)
+               {
+                       case eSecCommand::SLEEP:
+                               delay = m_sec_sequence.current()++->msec;
+                               eDebug("[SEC] sleep %dms", delay);
+                               break;
+                       case eSecCommand::GOTO:
+                               if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
+                                       ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_VOLTAGE:
+                               int voltage = m_sec_sequence.current()++->voltage;
+                               eDebug("[SEC] setVoltage %d", voltage);
+                               setVoltage(voltage);
+                               break;
+                       case eSecCommand::SET_TONE:
+                               eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
+                               setTone(m_sec_sequence.current()++->tone);
+                               break;
+                       case eSecCommand::SEND_DISEQC:
+                               sendDiseqc(m_sec_sequence.current()->diseqc);
+                               eDebugNoNewLine("[SEC] sendDiseqc: ");
+                               for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
+                                   eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
+                               eDebug("");
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SEND_TONEBURST:
+                               eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
+                               sendToneburst(m_sec_sequence.current()++->toneburst);
+                               break;
+                       case eSecCommand::SET_FRONTEND:
+                               eDebug("[SEC] setFrontend");
+                               setFrontend();
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::MEASURE_IDLE_INPUTPOWER:
+                       {
+                               int idx = m_sec_sequence.current()++->val;
+                               if ( idx == 0 || idx == 1 )
+                               {
+                                       m_idleInputpower[idx] = readInputpower();
+                                       eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
+                               }
+                               else
+                                       eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
+                               break;
+                       }
+                       case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
+                               m_runningInputpower = readInputpower();
+                               eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_TIMEOUT:
+                               m_timeoutCount = m_sec_sequence.current()++->val;
+                               eDebug("[SEC] set timeout %d", m_timeoutCount);
+                               break;
+                       case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
+                               m_data[5] = m_data[3];
+                               m_data[6] = m_data[4];
+                               eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::IF_TIMEOUT_GOTO:
+                               if (!m_timeoutCount)
+                               {
+                                       eDebug("[SEC] rotor timout");
+                                       setSecSequencePos(m_sec_sequence.current()->steps);
+                               }
+                               else
+                                       ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_POWER_LIMITING_MODE:
+                       {
+                               int fd=::open("/dev/i2c/0", O_RDWR);
+                               unsigned char data[2];
+                               ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
+                               if(::read(fd, data, 1) != 1)
+                                       eDebug("[SEC] error read lnbp (%m)");
+                               if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
+                               {
+                                       data[0] |= 0x90;  // enable static current limiting
+                                       eDebug("[SEC] set static current limiting");
+                               }
+                               else
+                               {
+                                       data[0] &= ~0x90;  // enable dynamic current limiting
+                                       eDebug("[SEC] set dynamic current limiting");
+                               }
+                               if(::write(fd, data, 1) != 1)
+                                       eDebug("[SEC] error write lnbp (%m)");
+                               ::close(fd);
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       case eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO:
+                               if (m_idleInputpower[0] && m_idleInputpower[1] && setSecSequencePos(m_sec_sequence.current()->steps))
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
+                       {
+                               int idleInputpower = m_idleInputpower[m_curVoltage == iDVBFrontend::voltage13 ? 0 : 1];
+                               eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
+                               const char *txt = cmd.direction ? "running" : "stopped";
+                               eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
+                                       txt,
+                                       m_runningInputpower,
+                                       idleInputpower,
+                                       cmd.deltaA);
+                               if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
+                                       || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
+                               {
+                                       ++cmd.okcount;
+                                       eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
+                                       if ( cmd.okcount > 6 )
+                                       {
+                                               eDebug("[SEC] rotor is %s", txt);
+                                               if (setSecSequencePos(cmd.steps))
+                                                       break;
+                                       }
+                               }
+                               else
+                               {
+                                       eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
+                                       --m_timeoutCount;
+                                       cmd.okcount=0;
+                               }
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       case eSecCommand::IF_VOLTAGE_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       default:
+                               ++m_sec_sequence.current();
+                               eDebug("[SEC] unhandled sec command");
+               }
+               m_tuneTimer->start(delay,true);
+       }
+}
+
+void eDVBFrontend::setFrontend()
+{
+       eDebug("setting frontend..\n");
+       if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
+       {
+               perror("FE_SET_FRONTEND failed");
+               return;
+       }
+
+       if (m_state != stateTuning)
+       {
+               m_tuning = 1;
+               m_state = stateTuning;
+               m_stateChanged(this);
+       }
+       m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
+}
+
 RESULT eDVBFrontend::getFrontendType(int &t)
 {
        if (m_type == -1)
@@ -371,12 +612,10 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
        if (m_type == -1)
                return -ENODEV;
 
-       FRONTENDPARAMETERS parm;
-
        feEvent(-1);
-       
-       eDebug("eDVBFrontend::tune. type: %d", m_type);
-       
+
+       m_sec_sequence.clear();
+
        switch (m_type)
        {
        case feSatellite:
@@ -519,23 +758,10 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                break;
        }
        }
-       
-       eDebug("setting frontend..\n");
-       
-       if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
-       {
-               perror("FE_SET_FRONTEND failed");
-               return errno;
-       }
-       
-       if (m_state != stateTuning)
-       {
-               m_tuning = 1;
-               m_state = stateTuning;
-               m_stateChanged(this);
-       }
-       
-       m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
+
+       m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
+       m_tuneTimer->start(0,true);
+       m_sec_sequence.current() = m_sec_sequence.begin();
 
        return 0;
 }
@@ -554,6 +780,7 @@ RESULT eDVBFrontend::setVoltage(int voltage)
        fe_sec_voltage_t vlt;
 #endif
 
+       m_curVoltage=voltage;
        switch (voltage)
        {
        case voltageOff:
@@ -607,63 +834,45 @@ RESULT eDVBFrontend::setTone(int t)
 #endif
 }
 
+#if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
+       #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
+#endif
+
 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
 {
 #if HAVE_DVB_API_VERSION < 3
-       secCmdSequence seq;
-       secCommand cmd;
-
+       struct secCommand cmd;
        cmd.type = SEC_CMDTYPE_DISEQC_RAW;
        cmd.u.diseqc.cmdtype = diseqc.data[0];
-       eDebug("cmdtype is %02x", diseqc.data[0]);
        cmd.u.diseqc.addr = diseqc.data[1];
-       eDebug("cmdaddr is %02x", diseqc.data[1]);
        cmd.u.diseqc.cmd = diseqc.data[2];
-       eDebug("cmd is %02x", diseqc.data[2]);
        cmd.u.diseqc.numParams = diseqc.len-3;
-       eDebug("numparams %d", diseqc.len-3);
-
        memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
-       for (int i=0; i < diseqc.len-3; ++i )
-               eDebugNoNewLine("%02x ", diseqc.data[3+i]);
-       eDebug("");
-
-       seq.continuousTone = diseqc.tone == toneOn ? SEC_TONE_ON : SEC_TONE_OFF;
-       switch ( diseqc.voltage )
-       {
-               case voltageOff:
-                       seq.voltage = SEC_VOLTAGE_OFF;
-                       break;
-               case voltage13:
-                       seq.voltage = SEC_VOLTAGE_13;
-                       break;
-               case voltage18:
-                       seq.voltage = SEC_VOLTAGE_18;
-                       break;
-       }
-       seq.miniCommand = SEC_MINI_NONE;
-       seq.commands=&cmd;
-       seq.numCommands=1;
-
-       if ( ioctl(m_secfd, SEC_SEND_SEQUENCE, &seq) < 0 )
-       {
-               eDebug("SEC_SEND_SEQUENCE failed ( %m )");
-               return -EINVAL;
-       }
-       return 0;
+       if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
 #else
        struct dvb_diseqc_master_cmd cmd;
-       if (::ioctl(m_fd, FE_SET_TONE, SEC_TONE_OFF))
-               return -EINVAL;
-       usleep(15 * 1000);
        memcpy(cmd.msg, diseqc.data, diseqc.len);
        cmd.msg_len = diseqc.len;
-       
        if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
+#endif
+               return -EINVAL;
+       return 0;
+}
+
+#if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
+       #define SEC_DISEQC_SEND_BURST _IO('o', 96)
+#endif
+RESULT eDVBFrontend::sendToneburst(int burst)
+{
+#if HAVE_DVB_API_VERSION < 3
+       secMiniCmd cmd = SEC_MINI_NONE;
+       if ( burst == eDVBSatelliteDiseqcParameters::A )
+               cmd = SEC_MINI_A;
+       else if ( burst == eDVBSatelliteDiseqcParameters::B )
+               cmd = SEC_MINI_B;
+       if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
                return -EINVAL;
-       usleep(15 * 1000);
 #endif
-       eDebug("diseqc ok");
        return 0;
 }
 
@@ -672,3 +881,30 @@ RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
        m_sec = sec;
        return 0;
 }
+
+RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
+{
+       m_sec_sequence = list;
+       return 0;
+}
+
+RESULT eDVBFrontend::getData(int num, int &data)
+{
+       if ( num < 7 )
+       {
+               data = m_data[num];
+               return 0;
+       }
+       return -EINVAL;
+}
+
+RESULT eDVBFrontend::setData(int num, int val)
+{
+       if ( num < 7 )
+       {
+               m_data[num] = val;
+               return 0;
+       }
+       return -EINVAL;
+}
+