DVBFrontendHandle.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "DVBFrontendHandle.hh"
18 //#include "DVBFrontendHandle.ih"
19 
20 // Custom includes
21 #include <boost/format.hpp>
22 #include <sys/socket.h>
23 #include <senf/Utils/Exception.hh>
24 #include <sys/ioctl.h>
25 
26 
27 //#include "DVBFrontendHandle.mpp"
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32 // senf::DVBFrontendHandle
33 using namespace std;
34 
35 prefix_ void senf::DVBFrontendSocketProtocol::init_client(unsigned short adapter, unsigned short device, int flags)
36  const
37 {
38  string devFrontend = str( boost::format("/dev/dvb/adapter%d/frontend%d") % adapter % device);
39  int f = open(devFrontend.c_str(), flags);
40  if (f < 0)
41  SENF_THROW_SYSTEM_EXCEPTION("Could not open frontend device of DVB adapter ") << devFrontend << ".";
42  fd(f);
43 }
44 
46  const
47 {
48  return 0;
49 }
50 
52  const
53 {
54  return false;
55 }
56 
57 prefix_ void senf::DVBFrontendSocketProtocol::tune(const struct dvb_frontend_parameters & frontend)
58  const
59 {
60  // tuning
61  if (::ioctl(fd(), FE_SET_FRONTEND, &frontend) )
62  SENF_THROW_SYSTEM_EXCEPTION("ioctl FE_SET_FRONTEND failed. Socket should initialized with r/w permissions.");
63 }
64 
66  fe_spectral_inversion_t inversion,
67  fe_bandwidth_t bandwidth,
68  fe_code_rate_t code_rate_HP, /* high priority stream code rate */
69  fe_code_rate_t code_rate_LP, /* low priority stream code rate */
70  fe_modulation_t constellation, /* modulation type */
71  fe_transmit_mode_t transmission_mode,
72  fe_guard_interval_t guard_interval,
73  fe_hierarchy_t hierarchy_information
74  )
75  const
76 {
77  struct dvb_ofdm_parameters ofdm; /* DVB-T Parameters */
78  struct dvb_frontend_parameters frontend;
79 
80  ::memset(&frontend, 0, sizeof(struct dvb_frontend_parameters));
81  ::memset(&ofdm, 0, sizeof(struct dvb_ofdm_parameters));
82 
83  ofdm.bandwidth = bandwidth;
84  ofdm.code_rate_HP = code_rate_HP;
85  ofdm.code_rate_LP = code_rate_LP;
86  ofdm.constellation = constellation;
87  ofdm.guard_interval = guard_interval;
88  ofdm.hierarchy_information = hierarchy_information;
89 
90  frontend.frequency = frequency;
91  frontend.inversion = inversion;
92  frontend.u.ofdm = ofdm;
93 
94  tune(frontend);
95 
96 }
98  fe_spectral_inversion_t inversion,
99  unsigned int symbole_rate, /* symbol rate in Symbols per second */
100  fe_code_rate_t fec_inner /* forward error correction (see above) */
101  )
102  const
103 {
104  struct dvb_qpsk_parameters qpsk; /* DVB-S Parameters*/
105  struct dvb_frontend_parameters frontend;
106 
107  ::memset(&frontend, 0, sizeof(struct dvb_frontend_parameters));
108  ::memset(&qpsk, 0, sizeof(struct dvb_qpsk_parameters));
109 
110  qpsk.symbol_rate = symbole_rate;
111  qpsk.fec_inner = fec_inner;
112 
113  frontend.frequency = frequency;
114  frontend.inversion = inversion;
115  frontend.u.qpsk = qpsk;
116 
117  tune(frontend);
118 }
119 
121  fe_spectral_inversion_t inversion,
122  unsigned int symbol_rate,
123  fe_code_rate_t fec_inner,
124  fe_modulation_t modulation
125  )
126 const
127 {
128  struct dvb_qam_parameters qam; /* DVB-C Parameters*/
129  struct dvb_frontend_parameters frontend;
130 
131  ::memset(&frontend, 0, sizeof(struct dvb_frontend_parameters));
132  ::memset(&qam, 0, sizeof(struct dvb_qam_parameters));
133 
134  qam.symbol_rate = symbol_rate;
135  qam.fec_inner = fec_inner;
136  qam.modulation = modulation;
137 
138 
139  frontend.frequency = frequency;
140  frontend.inversion = inversion;
141  frontend.u.qam = qam;
142 
143  tune(frontend);
144 }
146  const
147 {
148  if (on)
149  ::fcntl(fd(), F_SETFL, ::fcntl(fd(), F_GETFL) | O_NONBLOCK);
150  else
151  ::fcntl(fd(), F_SETFL, ::fcntl(fd(), F_GETFL) & ~O_NONBLOCK);
152 
153 }
155  const
156 {
157  struct dvb_frontend_info info;
158  ::memset(&info, 0, sizeof(struct dvb_frontend_info));
159 
160  if (::ioctl(fd(), FE_GET_INFO, &info)) {
161  SENF_THROW_SYSTEM_EXCEPTION("Could not read on fildescriptor.");
162  }
163  return info;
164 }
165 
167  struct dvb_frontend_parameters frontend_;
168 
169  ::memset(&frontend_, 0, sizeof(struct dvb_frontend_parameters));
170 
171  if (::ioctl(fd(), FE_GET_FRONTEND, &frontend_)) {
172  switch(errno) {
173  case EBADF:
174  SENF_THROW_SYSTEM_EXCEPTION("fd is not a valid open file descriptor.");
175  break;
176  case EFAULT:
177  SENF_THROW_SYSTEM_EXCEPTION( "frontend_ points to invalid address." );
178  break;
179  case EINVAL:
180  SENF_THROW_SYSTEM_EXCEPTION( "Maximum supported symbol rate reached. This call may not be implemented by kernel driver!" );
181  break;
182  default:
183  SENF_THROW_SYSTEM_EXCEPTION("Errno: ") << errno;
184  }
185  }
186  return frontend_;
187 }
188 
190  struct dvb_frontend_event ev ;
191 
192  ::memset(&ev, 0, sizeof(struct dvb_frontend_event));
193  if (::ioctl(fd(), FE_GET_EVENT, &ev)) {
194  switch(errno) {
195  case EBADF:
196  SENF_THROW_SYSTEM_EXCEPTION("Could not read from frontend device, read-only access to the device is sufficient.");
197  break;
198  case EWOULDBLOCK:
199  SENF_THROW_SYSTEM_EXCEPTION( "No event pending and device is in nonblocking mode." );
200  break;
201  case EINTR:
202  // TODO: ignore EINTR. which might be caused by watchdog signals. This is possibly not the solution, but should work
203  break;
204  default:
205  SENF_THROW_SYSTEM_EXCEPTION("Errno: ") << errno;
206  }
207  }
208  return ev;
209 }
210 
212  const
213 {
214  int16_t strength;
215  if (::ioctl(fd(), FE_READ_SIGNAL_STRENGTH, &strength) < 0)
216  SENF_THROW_SYSTEM_EXCEPTION("Could not get signal strength of DVB adapter.");
217  return strength;
218 }
219 
221  const
222 {
223  int16_t snr;
224  if (::ioctl(fd(), FE_READ_SNR, &snr) < 0)
225  SENF_THROW_SYSTEM_EXCEPTION("Could not get signal-to-noise ratio of DVB adapter.");
226  return snr;
227 }
228 
230  const
231 {
232  uint32_t ber;
233  if (::ioctl(fd(), FE_READ_BER, &ber) < 0)
234  SENF_THROW_SYSTEM_EXCEPTION("Could not get bit error rate of DVB adapter.");
235  return ber;
236 }
237 
239  const
240 {
241  uint32_t uncorrected_blocks;
242  if (::ioctl(fd(), FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks) < 0)
243  SENF_THROW_SYSTEM_EXCEPTION("Could not get bit error rate of DVB adapter.");
244  return uncorrected_blocks;
245 }
246 
247 
249  const
250 {
251  fe_status_t status;
252  if (::ioctl(fd(), FE_READ_STATUS, &status) < 0)
253  SENF_THROW_SYSTEM_EXCEPTION("Could not get bit error rate of DVB adapter.");
254  return status;
255 }
256 
257 
258 //-/////////////////////////////////////////////////////////////////////////////////////////////////
259 #undef prefix_
260 //#include "DVBFrontendHandle.mpp"
261 
262 
263 // Local Variables:
264 // mode: c++
265 // fill-column: 100
266 // c-file-style: "senf"
267 // indent-tabs-mode: nil
268 // ispell-local-dictionary: "american"
269 // compile-command: "scons -u test"
270 // comment-column: 40
271 // End:
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
uint32_t bitErrorRate() const
Returns the current bit error rate for the signal.
struct dvb_frontend_parameters getFrontendParam() const
Returns dvb_frontend_parameters struct.
std::string str(T const &t)
STL namespace.
#define prefix_
dvb_frontend_event getEvent() const
DVBFrontendHandle public header.
fe_status_t status() const
This ioctl call returns status information about the front-end.
unsigned available() const
Returns always 0
void tuneDVB_T(unsigned int frequency, fe_spectral_inversion_t inversion, fe_bandwidth_t bandwidth, fe_code_rate_t code_rate_HP, fe_code_rate_t code_rate_LP, fe_modulation_t constellation, fe_transmit_mode_t transmission_mode, fe_guard_interval_t guard_interval, fe_hierarchy_t hierarchy_information) const
Tunes a DVB-T device.
void tuneDVB_S(unsigned int frequency, fe_spectral_inversion_t inversion, unsigned int symbole_rate, fe_code_rate_t code_rate) const
Tunes a DVB-S device.
void tuneDVB_C(unsigned int frequency, fe_spectral_inversion_t inversion, unsigned int symbol_rate, fe_code_rate_t fec_inner, fe_modulation_t modulation) const
Tunes a DVB-C device.
bool eof() const
Returns always false
void setNonBlock(bool on=true) const
int16_t signalNoiseRatio() const
Returns current signal-to-noise ratio.
void init_client(unsigned short adapter=0, unsigned short device=0, int flags=(O_RDWR|O_NONBLOCK)) const
Opens the specified frontend device in read-only mode.
dvb_frontend_info getInfo() const
Returns information struct.
uint32_t uncorrectedBlocks() const
Returns the number of uncorrected blocks.
int16_t signalStrength() const
Returns current signal strength.