non blocking diseqc and sec stuff
[enigma2.git] / lib / dvb / sec.cpp
1 #include <config.h>
2 #include <lib/dvb/sec.h>
3 #if HAVE_DVB_API_VERSION < 3
4 #define INVERSION Inversion
5 #define FREQUENCY Frequency
6 #define FEC_INNER FEC_inner
7 #define SYMBOLRATE SymbolRate
8 #else
9 #define INVERSION inversion
10 #define FREQUENCY frequency
11 #define FEC_INNER fec_inner
12 #define SYMBOLRATE symbol_rate
13 #endif
14 #include <lib/base/eerror.h>
15
16 DEFINE_REF(eDVBSatelliteEquipmentControl);
17
18 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl()
19 {
20         m_lnblist.push_back(eDVBSatelliteLNBParameters());
21         eDVBSatelliteLNBParameters &lnb_ref = m_lnblist.front();
22         eDVBSatelliteParameters &astra1 = lnb_ref.m_satellites[192];
23         eDVBSatelliteDiseqcParameters &diseqc_ref = astra1.m_diseqc_parameters;
24         eDVBSatelliteSwitchParameters &switch_ref = astra1.m_switch_parameters;
25
26         lnb_ref.m_lof_hi = 10607000;
27         lnb_ref.m_lof_lo = 9750000;
28         lnb_ref.m_lof_threshold = 11700000;
29
30         diseqc_ref.m_diseqc_mode = eDVBSatelliteDiseqcParameters::V1_0;
31         diseqc_ref.m_commited_cmd = eDVBSatelliteDiseqcParameters::BB;
32         diseqc_ref.m_repeats = 0;
33         diseqc_ref.m_seq_repeat = false;
34         diseqc_ref.m_swap_cmds = false;
35         diseqc_ref.m_toneburst_param = eDVBSatelliteDiseqcParameters::NO;
36         diseqc_ref.m_uncommitted_cmd = 0;
37         diseqc_ref.m_use_fast = 1;
38
39         switch_ref.m_22khz_signal = eDVBSatelliteSwitchParameters::HILO;
40         switch_ref.m_voltage_mode = eDVBSatelliteSwitchParameters::HV;
41 }
42
43 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat)
44 {
45         std::list<eDVBSatelliteLNBParameters>::iterator it = m_lnblist.begin();
46         for (;it != m_lnblist.end(); ++it )
47         {
48                 eDVBSatelliteLNBParameters &lnb_param = *it;
49                 std::map<int, eDVBSatelliteParameters>::iterator sit =
50                         lnb_param.m_satellites.find(sat.orbital_position);
51                 if ( sit != lnb_param.m_satellites.end())
52                 {
53                         int hi=0;
54                         int voltage = iDVBFrontend::voltageOff;
55                         int tone = iDVBFrontend::toneOff;
56
57                         eDVBSatelliteDiseqcParameters &di_param = sit->second.m_diseqc_parameters;
58                         eDVBSatelliteSwitchParameters &sw_param = sit->second.m_switch_parameters;
59
60                         if ( sat.frequency > lnb_param.m_lof_threshold )
61                                 hi = 1;
62
63                         if (hi)
64                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
65                         else
66                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
67
68                         parm.INVERSION = (!sat.inversion) ? INVERSION_ON : INVERSION_OFF;
69
70                         switch (sat.fec)
71                         {
72                 //              case 1:
73                 //              case ...:
74                         default:
75                                 parm.u.qpsk.FEC_INNER = FEC_AUTO;
76                                 break;
77                         }
78
79                         parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
80
81                         if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
82                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
83                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
84                                 voltage = iDVBFrontend::voltage13;
85                         else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
86                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
87                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
88                                 voltage = iDVBFrontend::voltage18;
89
90                         if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
91                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && hi ) )
92                                 tone = iDVBFrontend::toneOn;
93                         else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
94                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !hi ) )
95                                 tone = iDVBFrontend::toneOff;
96
97                         eSecCommandList sec_sequence;
98
99                         if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
100                         {
101                                 eDebug("rotor...");
102                         }
103                         else if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
104                         {
105                                 if ( di_param.m_commited_cmd < eDVBSatelliteDiseqcParameters::SENDNO ||
106                                         di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
107                                 {
108                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
109                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
110                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
111                                 }
112
113                                 if ( di_param.m_commited_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
114                                 {
115                                         eDVBDiseqcCommand diseqc;
116                                         diseqc.len = 4;
117                                         diseqc.data[0] = 0xe0;
118                                         diseqc.data[1] = 0x10;
119                                         diseqc.data[2] = 0x38;
120                                         diseqc.data[3] = 0xF0 | (di_param.m_commited_cmd << 2);
121
122                                         if (hi)
123                                                 diseqc.data[3] |= 1;
124
125                                         if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
126                                                 diseqc.data[3] |= 2;
127
128                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
129                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
130                                 }
131
132                                 if ( di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
133                                 {
134                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
135                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
136                                 }
137                         }
138                         else
139                         {
140                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
141                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
142                         }
143
144                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
145                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
146
147                         frontend.setSecSequence(sec_sequence);
148
149                         return 0;
150                 }
151         }
152
153         eDebug("not found satellite configuration for orbital position (%d)", sat.orbital_position );
154
155         return -1;
156 }
157