rotor turning with measure input power is now working
[enigma2.git] / lib / dvb / frontend.cpp
index 1322ea2..54c545e 100644 (file)
@@ -9,6 +9,16 @@
 #if HAVE_DVB_API_VERSION < 3
 #include <ost/frontend.h>
 #include <ost/sec.h>
+#define QAM_AUTO                               (Modulation)6
+#define TRANSMISSION_MODE_AUTO (TransmitMode)2
+#define BANDWIDTH_AUTO                 (BandWidth)3
+#define GUARD_INTERVAL_AUTO            (GuardInterval)4
+#define HIERARCHY_AUTO                 (Hierarchy)4
+#define constellation Constellation
+#define guard_interval guardInterval
+#define hierarchy_information HierarchyInformation
+#define code_rate_HP HP_CodeRate
+#define code_rate_LP LP_CodeRate
 #else
 #include <linux/dvb/frontend.h>
 #endif
@@ -267,10 +277,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;
 }
 
@@ -348,6 +363,185 @@ void eDVBFrontend::timeout()
                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("sleep %dms", delay);
+                               break;
+                       case eSecCommand::GOTO:
+                               if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
+                                       ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_VOLTAGE:
+                               eDebug("setVoltage %d", m_sec_sequence.current()->voltage);
+                               setVoltage(m_sec_sequence.current()++->voltage);
+                               break;
+                       case eSecCommand::SET_TONE:
+                               eDebug("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("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("sendToneburst: %d", m_sec_sequence.current()->toneburst);
+                               sendToneburst(m_sec_sequence.current()++->toneburst);
+                               break;
+                       case eSecCommand::SET_FRONTEND:
+                               eDebug("setFrontend");
+                               setFrontend();
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::MEASURE_IDLE_INPUTPOWER:
+                               m_idleInputpower = readInputpower();
+                               eDebug("idleInputpower is %d", m_idleInputpower);
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
+                               m_runningInputpower = readInputpower();
+                               eDebug("runningInputpower is %d", m_runningInputpower);
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_TIMEOUT:
+                               m_timeoutCount = m_sec_sequence.current()++->val;
+                               eDebug("set timeout %d", m_timeoutCount);
+                               break;
+                       case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
+                               m_data[5] = m_data[3];
+                               m_data[6] = m_data[4];
+                               eDebug("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)
+                                       setSecSequencePos(m_sec_sequence.current()->steps);
+                               else
+                                       ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
+                       {
+                               eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
+                               const char *txt = cmd.direction ? "running" : "stopped";
+                               eDebug("waiting for rotor %s", txt);
+                               eDebug("%s %d, idle %d, delta %d",
+                                       txt,
+                                       m_runningInputpower,
+                                       m_idleInputpower,
+                                       cmd.deltaA);
+                               if ( (cmd.direction && abs(m_runningInputpower - m_idleInputpower) >= cmd.deltaA)
+                                       || (!cmd.direction && abs(m_runningInputpower - m_idleInputpower) <= cmd.deltaA) )
+                               {
+                                       ++cmd.okcount;
+                                       eDebug("rotor %s step %d ok", txt, cmd.okcount);
+                                       if ( cmd.okcount > 1 )
+                                       {
+                                               eDebug("rotor is %s", txt);
+                                               if (setSecSequencePos(cmd.steps))
+                                                       break;
+                                       }
+                               }
+                               else
+                               {
+                                       eDebug("rotor not %s... reset counter.. increase timeout", txt);
+                                       --m_timeoutCount;
+                                       cmd.okcount=0;
+                               }
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       default:
+                               ++m_sec_sequence.current();
+                               eDebug("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)
@@ -361,12 +555,12 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
        if (m_type == -1)
                return -ENODEV;
 
-       FRONTENDPARAMETERS parm;
-
        feEvent(-1);
-       
+
+       m_sec_sequence.clear();
+
        eDebug("eDVBFrontend::tune. type: %d", m_type);
-       
+
        switch (m_type)
        {
        case feSatellite:
@@ -409,21 +603,45 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                        eDebug("no -T data");
                        return -EINVAL;
                }
+#if HAVE_DVB_API_VERSION < 3
+               parm.Frequency = feparm.frequency;
+#else
                parm.frequency = feparm.frequency;
-               
+#endif
+
                switch (feparm.bandwidth)
                {
                case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
-                       parm.u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.bandWidth =
+#else
+                       parm.u.ofdm.bandwidth =
+#endif
+                               BANDWIDTH_8_MHZ;
                        break;
                case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
-                       parm.u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.bandWidth =
+#else
+                       parm.u.ofdm.bandwidth =
+#endif
+                               BANDWIDTH_7_MHZ;
                        break;
                case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
-                       parm.u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.bandWidth =
+#else
+                       parm.u.ofdm.bandwidth =
+#endif
+                               BANDWIDTH_6_MHZ;
                        break;
                case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
-                       parm.u.ofdm.bandwidth = BANDWIDTH_AUTO;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.bandWidth =
+#else
+                       parm.u.ofdm.bandwidth =
+#endif
+                               BANDWIDTH_AUTO;
                        break;
                default:
                        eWarning("invalid OFDM bandwith");
@@ -449,39 +667,46 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                switch (feparm.transmission_mode)
                {
                case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
-                       parm.u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.TransmissionMode =
+#else
+                       parm.u.ofdm.transmission_mode =
+#endif
+                               TRANSMISSION_MODE_2K;
                        break;
                case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
-                       parm.u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.TransmissionMode =
+#else
+                       parm.u.ofdm.transmission_mode =
+#endif
+                               TRANSMISSION_MODE_8K;
                        break;
                case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
-                       parm.u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
+#if HAVE_DVB_API_VERSION < 3
+                       parm.u.ofdm.TransmissionMode =
+#else
+                       parm.u.ofdm.transmission_mode =
+#endif
+                               TRANSMISSION_MODE_AUTO;
                        break;
                }
                
                parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
                parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
-               parm.inversion = INVERSION_AUTO;
+#if HAVE_DVB_API_VERSION < 3
+               parm.Inversion =
+#else
+               parm.inversion =
+#endif
+                       INVERSION_AUTO;
                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;
 }
@@ -553,53 +778,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 = SEC_TONE_OFF;
-       seq.voltage = SEC_VOLTAGE_13;
-       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;
 }
 
@@ -608,3 +825,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;
+}
+