-
Notifications
You must be signed in to change notification settings - Fork 4
/
dvb_resource.c
425 lines (353 loc) · 11.9 KB
/
dvb_resource.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* ISDB-T Capture. A DVB v5 API TS capture for Linux, for ISDB-TB 6MHz Latin American ISDB-T International.
* Copyright (C) 2014 Rafael Diniz <rafael@riseup.net>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <linux/dvb/version.h>
#include <linux/dvb/frontend.h>
#include <linux/dvb/dmx.h>
#include "dvb_resource.h"
// Saves error parameters and returns -1
int _dvbres_error(struct dvb_resource* res, char* msg, int code)
{
strncpy(res->error_msg, msg, sizeof(res->error_msg));
res->error_msg[sizeof(res->error_msg) - 1] = 0;
res->error_code = code;
return -1;
}
// All OK with return value - zeroes error_msg and error_code and returns retval
int _dvbres_ok_retval(struct dvb_resource* res, int retval)
{
res->error_msg[0] = 0;
res->error_code = 0;
return retval;
}
// All OK - zeroes error_msg and error_code and return zero
int _dvbres_ok(struct dvb_resource* res)
{
return _dvbres_ok_retval(res, 0);
}
int dvbres_init(struct dvb_resource* res)
{
memset(res, 0, sizeof(struct dvb_resource));
return _dvbres_ok(res);
}
int dvbres_listdevices(struct dvb_resource* res, char* buffer, int max_length)
{
memset(buffer, 0, max_length);
buffer[--max_length] = 0;
buffer[0] = 0;
char frontname[128];
char adaptername[128];
int rc;
int pos = 0;
int ifnum = 0;
while (pos < max_length)
{
// generate the root device name to devprefix
sprintf(adaptername, "/dev/dvb/adapter%d", ifnum);
// generate the next device name and open it
sprintf(frontname, "%s/frontend0", adaptername);
// opening device
int front = open(frontname, O_RDWR);
if (front == -1 && ifnum == 0)
return _dvbres_error(res, "Error opening frontend.", errno);
if (front == -1 && ifnum > 0)
return _dvbres_ok(res);
// reading status with the purpose of identifying DVB-S
struct dvb_frontend_info finfo;
rc = ioctl(front, FE_GET_INFO, &finfo);
if (rc)
{
close(front);
return _dvbres_error(res, "Rading frontend info", errno);
}
// closing device
rc = close(front);
if (rc)
return _dvbres_error(res, "Closing frontend", errno);
// replace tabs with spaces (tab is used as separator)
unsigned int i;
for (i=0; i<sizeof(finfo.name); i++)
if (finfo.name[i] == '\t')
finfo.name[i] = ' ';
int slen;
// tab (if not the first device)
if (ifnum > 0) {
if (pos >= max_length)
return _dvbres_error(res, "Device enum buffer too small", -1);
buffer[pos++] = '\t';
}
// space check
slen = strlen(finfo.name);
if (pos + slen >= max_length)
return _dvbres_error(res, "Device enum buffer too small", -1);
// copy device name
buffer[pos++] = '0' + ifnum;
buffer[pos++] = ':';
buffer[pos++] = ' ';
strncpy(&buffer[pos], finfo.name, max_length - pos);
pos += slen;
// tab
if (pos >= max_length)
return _dvbres_error(res, "Device enum buffer too small", -1);
buffer[pos++] = '\t';
// space check
slen = strlen(adaptername);
if (pos + slen >= max_length)
return _dvbres_error(res, "Device enum buffer too small", -1);
// copy device name
strncpy(&buffer[pos], adaptername, max_length - pos);
pos += slen;
// next device
ifnum++;
}
return _dvbres_error(res, "Device enum buffer to small", -1);
}
int dvbres_open(struct dvb_resource* res, uint64_t freq, char* device, int layer_info) {
// return value (code) of calls
int rc;
// the index of adaper actually used
int adapternum = 0;
// temporaray field to hold the root path (/dev/dvb/adapterN)
char devprefix[64];
// temporaray field to hold device names (/dev/dvb/adapterN/{something}M)
char devname[64];
// information about the actual frontend
struct dvb_frontend_info finfo;
// if no device is given
if (device == NULL) {
// opening device
do {
// generate the root device name to devprefix
sprintf(devprefix, "/dev/dvb/adapter%d", adapternum);
// generate the next device name and open it
sprintf(devname, "%s/frontend0", devprefix);
res->frontend = open(devname, O_RDWR);
if (res->frontend == -1)
return _dvbres_error(res, "Error opening frontend device.", errno);
// reading status with the purpose of identifying tuner type
rc = ioctl(res->frontend, FE_GET_INFO, &finfo);
if (rc) {
close(res->frontend);
return _dvbres_error(res, "Error reading frontend information.", errno);
}
// if not DVB-T then we skip to the next adapter
if (finfo.type != 2) {
close(res->frontend);
res->frontend = 0;
adapternum++;
}
} while (!res->frontend);
} else { // if device
// copy the device parameter to the devprefix
strncpy(devprefix, device, sizeof(devprefix));
// opening device
sprintf(devname, "%s/frontend0", devprefix);
res->frontend = open(devname, O_RDWR);
if (!res->frontend)
return _dvbres_error(res, "Error opening frontend.", errno);
// reading status with the purpose of identifying tuner type
rc = ioctl(res->frontend, FE_GET_INFO, &finfo);
if (rc) {
close(res->frontend);
return _dvbres_error(res, "Reading frontend info", errno);
}
// if not DVB-T then we close and return an error
if (finfo.type != 2) {
close(res->frontend);
res->frontend = 0;
return _dvbres_error(res, "Device is not a DVB-T frontend", -1);
}
} // if device
int inversion = (finfo.caps & FE_CAN_INVERSION_AUTO) ? INVERSION_AUTO : INVERSION_OFF;
int partial_reception = 0;
int layers = 0;
int segment_count = 0;
switch (layer_info)
{
case LAYER_FULL:
layers = 7; // 0x1 | 0x2 | 0x4
partial_reception = 1;
segment_count = -1; // -1 means AUTO
break;
case LAYER_A:
layers = 1; // 0x1
partial_reception = 1;
segment_count = 1;
break;
case LAYER_B:
layers = 2; // 0x2
partial_reception = 0;
segment_count = -1;
break;
case LAYER_C:
layers = 4; // 0x4
partial_reception = 0;
segment_count = -1;
break;
}
if (partial_reception)
{
struct dtv_property myproperties[] = {
{ .cmd = DTV_CLEAR }, /* Clear the driver before everything */
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_ISDBT },
{ .cmd = DTV_FREQUENCY, .u.data = freq }, /* Set frequency */
{ .cmd = DTV_ISDBT_PARTIAL_RECEPTION, .u.data = partial_reception},
{ .cmd = DTV_ISDBT_LAYERA_SEGMENT_COUNT, .u.data = segment_count}, // we only need to make sure one-seg has the correct settings
// { .cmd = DTV_ISDBT_LAYERB_SEGMENT_COUNT, .u.data = 12},
// { .cmd = DTV_ISDBT_LAYERC_SEGMENT_COUNT, .u.data = 0},
{ .cmd = DTV_ISDBT_LAYER_ENABLED, .u.data = layers },
{ .cmd = DTV_BANDWIDTH_HZ, .u.data = 6000000},
{ .cmd = DTV_INVERSION, .u.data = inversion},
{ .cmd = DTV_TUNE }, /* now actually tune to that frequency (no parameters needed) */
};
struct dtv_properties mydtvproperties = {
.num = 9, /* The number of commands in the array */
.props = myproperties /* Pointer to the array */
};
rc = ioctl(res->frontend, FE_SET_PROPERTY, &mydtvproperties);
if (rc) {
close(res->frontend);
return _dvbres_error(res, "Setting properties", errno);
}
}
else
{
struct dtv_property myproperties[] = {
{ .cmd = DTV_CLEAR }, /* now actually tune to that frequency (no parameters needed) */
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_ISDBT }, /* Select DVB-S2 */
{ .cmd = DTV_FREQUENCY, .u.data = freq }, /* Set frequency */
{ .cmd = DTV_ISDBT_LAYER_ENABLED, .u.data = layers }, /* Set layers */
{ .cmd = DTV_ISDBT_PARTIAL_RECEPTION, .u.data = partial_reception},
{ .cmd = DTV_BANDWIDTH_HZ, .u.data = 6000000},
{ .cmd = DTV_INVERSION, .u.data = inversion},
{ .cmd = DTV_TUNE }, /* now actually tune to that frequency (no parameters needed) */
};
struct dtv_properties mydtvproperties = {
.num = 8, /* The number of commands in the array */
.props = myproperties /* Pointer to the array */
};
rc = ioctl(res->frontend, FE_SET_PROPERTY, &mydtvproperties);
if (rc) {
close(res->frontend);
return _dvbres_error(res, "Setting properties", errno);
}
}
// setting up demux to forward ALL pids to the DVR device
sprintf(devname, "%s/demux0", devprefix);
res->demux = open(devname, O_RDWR);
if (!res->demux) {
close(res->frontend);
return _dvbres_error(res, "Opening demux", errno);
}
struct dmx_pes_filter_params filter;
filter.pid = 8192;
filter.input = DMX_IN_FRONTEND;
filter.output = DMX_OUT_TS_TAP;
filter.pes_type = DMX_PES_OTHER;
filter.flags = DMX_IMMEDIATE_START;
rc = ioctl(res->demux, DMX_SET_PES_FILTER, &filter);
if (rc) {
close(res->frontend);
close(res->demux);
return _dvbres_error(res, "Setting up pes filter", errno);
}
// opening DVR device (non-blocking mode)
sprintf(devname, "%s/dvr0", devprefix);
res->dvr = open(devname, O_RDONLY | O_NONBLOCK);
if (!res->dvr) {
close(res->frontend);
close(res->demux);
return _dvbres_error(res, "Opening dvr", errno);
}
// all ok
return _dvbres_ok(res);
}
int dvbres_close(struct dvb_resource* res) {
int rc;
struct dtv_property myproperties[] = {
{ .cmd = DTV_CLEAR }, /* Clear the driver before everything */
};
struct dtv_properties mydtvproperties = {
.num = 1, /* The number of commands in the array */
.props = myproperties /* Pointer to the array */
};
rc = ioctl(res->frontend, FE_SET_PROPERTY, &mydtvproperties);
if (rc)
return _dvbres_error(res, "Reseting the tuner.", errno);
// closing
if (res->frontend) {
close(res->frontend);
res->frontend = 0;
}
if (res->demux) {
close(res->demux);
res->demux = 0;
}
if (res->dvr) {
close(res->dvr);
res->dvr = 0;
}
// all ok
return _dvbres_ok(res);
}
// get if signal is present
int dvbres_signalpresent(struct dvb_resource* res) {
int rc;
int status;
rc = ioctl(res->frontend, FE_READ_STATUS, &status);
if (rc)
return _dvbres_error(res, "Reading status.", errno);
return (status & FE_HAS_SIGNAL) != 0;
}
// get if signal is locked
int dvbres_signallocked(struct dvb_resource* res) {
int rc;
int status;
rc = ioctl(res->frontend, FE_READ_STATUS, &status);
if (rc)
return _dvbres_error(res, "Reading status.", errno);
return (status & FE_HAS_LOCK) != 0;
}
// get signal level 0: bad, 100: good
int dvbres_getsignalstrength(struct dvb_resource* res) {
int rc;
int strength = 0;
rc = ioctl(res->frontend, FE_READ_SIGNAL_STRENGTH, &strength);
if (rc)
return _dvbres_error(res, "Reading signal strength.", errno);
return strength * 100 / 65535;
}
// get signal quality 0: bad, 100: good
int dvbres_getsignalquality(struct dvb_resource* res) {
int rc;
int snr = 0;
rc = ioctl(res->frontend, FE_READ_SNR, &snr);
if (rc)
return _dvbres_error(res, "Reading signal strength.", errno);
return snr * 100 / 65535;
}