summaryrefslogtreecommitdiff
path: root/p2pvr/devices/frontends/ofdm.cpp
blob: e6c397975140c7a493cdc64c707672366b1e8ce5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "../frontend.h"
#include "../tuner.h"
#include <sys/ioctl.h>
#include <linux/dvb/frontend.h>

#define FREQ_OFFSET_MIN 0
#define FREQ_OFFSET_MAX 4

namespace P2PVR {
namespace DVB {
namespace Frontends {
class OFDM : public Frontend {
	public:
		OFDM(TunerI * t, const struct dvb_frontend_info & i) :
			Frontend(t, i, LOGMANAGER()->getLogger<OFDM>())
		{
		}

		void TuneTo(const DVBSI::DeliveryPtr & mp) const
		{
			auto td = DVBSI::TerrestrialDeliveryPtr::dynamicCast(mp);
			if (!td) {
				throw P2PVR::IncorrectDeliveryType();
			}
			dvb_frontend_parameters feparams;
			memset(&feparams, 0, sizeof(dvb_frontend_parameters));
			feparams.frequency = td->Frequency;
			feparams.inversion = INVERSION_OFF;
			feparams.u.ofdm.bandwidth = (fe_bandwidth)td->Bandwidth;
			feparams.u.ofdm.code_rate_HP = (fe_code_rate_t)td->CodeRateHP;
			feparams.u.ofdm.code_rate_LP = (fe_code_rate_t)td->CodeRateLP;
			feparams.u.ofdm.constellation = (fe_modulation_t)td->Constellation;
			feparams.u.ofdm.transmission_mode = (fe_transmit_mode)td->TransmissionMode;
			feparams.u.ofdm.guard_interval = (fe_guard_interval_t)td->GuardInterval;
			feparams.u.ofdm.hierarchy_information = (fe_hierarchy_t)td->Hierarchy;
			SetParameters(feparams);
			WaitForLock();
		}

		dvb_frontend_parameters GetParameters() const
		{
			dvb_frontend_parameters feparams;
			memset(&feparams, 0, sizeof(dvb_frontend_parameters));
			if (ioctl(tuner->frontendFD, FE_GET_FRONTEND, &feparams) < 0) {
				logger->messagebf(LOG::ERR, "Reading frontend parameters failed (%s:%d)", tuner->GetDevice(), strerror(errno), errno);
				throw P2PVR::DeviceError(tuner->GetDevice(), strerror(errno), errno);
			}
			return feparams;
		}

		void WaitForLock() const
		{
			fe_status_t status = (fe_status_t)0;
			// Wait for something
			for (int x = tuner->options->TuningTimeout / 10; x > 0 && (status = GetStatus()) == 0; x -= 1) {
				usleep(10000);
			}
			// Was it useful?
			if (!(status & (FE_HAS_SIGNAL | FE_HAS_CARRIER))) {
				logger->messagebf(LOG::ERR, "Tuning of device %s failed (No signal or carrier: 0x%02x)", tuner->GetDevice(), status);
				throw P2PVR::DeviceError(tuner->GetDevice(), "No carrier", 0);
			}
			// Wait for lock
			for (int x = tuner->options->LockTimeout / 10; x > 0 && ((status = GetStatus()) & FE_HAS_LOCK) == 0; x -= 1) {
				usleep(10000);
			}
			if (!(status & FE_HAS_LOCK)) {
				logger->messagebf(LOG::ERR, "Tuning of device %s failed (%s)", tuner->GetDevice(), "No lock");
				throw P2PVR::DeviceError(tuner->GetDevice(), "No lock", 0);
			}
		}

		void SetParameters(const dvb_frontend_parameters & feparams) const
		{
			if (ioctl(tuner->frontendFD, FE_SET_FRONTEND, &feparams) < 0) {
				logger->messagebf(LOG::ERR, "Tuning of device %s failed (%s:%d)", tuner->GetDevice(), strerror(errno), errno);
				throw P2PVR::DeviceError(tuner->GetDevice(), strerror(errno), errno);
			}
		}

		std::string Type() const
		{
			return "OFDM (DVB-T)";
		}

		enum Country { DVBT_AU, DVBT_DE, DVBT_FR, DVBT_GB };

		static uint32_t FrequencyForCountry(Country country, int channel)
		{
			switch (country) {
				case DVBT_AU:  //AUSTRALIA, 7MHz step list
					switch (channel) {
						case  5 ... 12: return  142500000;
						case 21 ... 69: return  333500000;
					}
				case DVBT_DE:  //GERMANY
				case DVBT_FR:  //FRANCE, +/- offset 166kHz & +offset 332kHz & +offset 498kHz
				case DVBT_GB:  //UNITED KINGDOM, +/- offset
					switch (channel) {
						case  5 ... 12: return  142500000; // VHF unused in FRANCE, skip those in offset loop
						case 21 ... 69: return  306000000;
					}
			}
			return  0;
		}
		static uint32_t FrequencyStepForCountry(Country country, int channel)
		{
			switch (country) {
				case DVBT_AU:
					return  7000000; // dvb-t australia, 7MHz step
				case DVBT_DE:
				case DVBT_FR:
				case DVBT_GB:
					switch (channel) { // dvb-t europe, 7MHz VHF ch5..12, all other 8MHz
						case  5 ... 12:    return 7000000;
						case 21 ... 69:    return 8000000;
					}
			}
			return 0;
		}
		static uint32_t ChannelFrequencyForCountry(Country country, int channel, int)
		{
			return FrequencyForCountry(country, channel) + (channel * FrequencyStepForCountry(country, channel));
		}

		void FrequencyScan(const OnFrequencyFound & onFrequencyFound) const
		{
			struct dvb_frontend_parameters feparams;
			memset(&feparams, 0, sizeof(dvb_frontend_parameters));
			feparams.inversion = (fe_info.caps & FE_CAN_INVERSION_AUTO ? INVERSION_AUTO : INVERSION_OFF);
			feparams.u.ofdm.constellation = (fe_info.caps & FE_CAN_QAM_AUTO ? QAM_AUTO : QAM_64);
			feparams.u.ofdm.hierarchy_information = HIERARCHY_NONE;

			for (int channel = 0; channel < 134; channel += 1) {
				for (uint32_t offset = FREQ_OFFSET_MIN; offset <= 0/*FREQ_OFFSET_MAX*/; offset += 1) {
					feparams.frequency = ChannelFrequencyForCountry(DVBT_GB, channel, offset);
					if (feparams.frequency == 0) {
						continue;
					}
					if (fe_info.frequency_min > feparams.frequency || fe_info.frequency_max < feparams.frequency) {
						logger->messagebf(LOG::WARNING, "Channel %d, freq (%d Hz) outside card range", channel, feparams.frequency);
						continue;
					}
					try {
						logger->messagebf(LOG::DEBUG, "Channel %d, Frequency %d Hz", channel, feparams.frequency);
						SetParameters(feparams);
						WaitForLock();
						logger->messagebf(LOG::INFO, "Found multiplex at %d Hz", feparams.frequency);
						logger->messagebf(LOG::DEBUG, "frequency %d", feparams.frequency);
						if (onFrequencyFound(feparams.frequency)) {
							return;
						}
					}
					catch (const P2PVR::DeviceError &) {
						// Moving on...
					}
				}
			}
		}
};

NAMEDFACTORY(Frontend::FactoryKey(FE_OFDM), OFDM, FrontendFactory);
}
}
}