-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvtuner.cpp
632 lines (539 loc) · 14.2 KB
/
vtuner.cpp
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* satip: vtuner to satip mapping
*
* Copyright (C) 2014 [email protected]
* [fragments from vtuner by Honza Petrous]
* DVB-C and DVB-T(2) support added by [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "config.h"
#include "vtuner.h"
#include "log.h"
#include "rtp.h"
const char* vtuner_path = "/dev/misc/vtuner";
static inline timespec operator-( const timespec &t1, const timespec &t2 )
{
timespec tmp;
tmp.tv_sec = t1.tv_sec - t2.tv_sec;
if ( (tmp.tv_nsec = t1.tv_nsec - t2.tv_nsec) < 0 )
{
tmp.tv_sec--;
tmp.tv_nsec += 1000000000;
}
return tmp;
}
static inline timespec &operator+=( timespec &t1, const long msek )
{
t1.tv_sec += msek / 1000;
if ( (t1.tv_nsec += (msek % 1000) * 1000000) >= 1000000000 )
{
t1.tv_sec++;
t1.tv_nsec -= 1000000000;
}
return t1;
}
static inline int64_t timeout_msec ( const timespec & orig, const timespec &now )
{
const timespec tv = orig - now;
return (int64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
}
static inline int64_t timeout_msec ( const timespec & orig )
{
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return timeout_msec(orig, now);
}
satipVtuner::satipVtuner(satipConfig* satip_cfg)
:m_fd(-1), m_openok(false), m_tone(SEC_TONE_OFF)
{
DEBUG(MSG_MAIN,"Create SATIP VTUNER.\n");
m_satip_cfg = satip_cfg;
m_satip_rtp = NULL;
m_openok = !openVtuner();
if (!m_openok)
ERROR(MSG_MAIN,"vtuner control failed\n");
}
satipVtuner::~satipVtuner()
{
if (m_fd >= 0)
close(m_fd);
DEBUG(MSG_MAIN,"Destruct SATIP VTUNER.\n");
}
int satipVtuner::AllocateVtuner()
{
int vtuner_fd = -1;
int vtuner_index = 0;
char filename[128];
while( vtuner_fd < 0)
{
sprintf(filename, "%s%d", vtuner_path, vtuner_index);
if (access (filename, 0) != 0) break;
vtuner_fd = open(filename, O_RDWR);
if (vtuner_fd < 0) vtuner_index++;
}
if (vtuner_fd < 0)
return -1;
DEBUG(MSG_MAIN, "Allocate vtuner %s%d\n", vtuner_path, vtuner_index);
return vtuner_fd;
}
int satipVtuner::openVtuner()
{
int fd;
struct dvb_frontend_info fe_info;
int fe_type = -1;
char fe_type_str[8];
fd = AllocateVtuner();
if (fd == -1)
{
DEBUG(MSG_MAIN, "Allocate vtuner failed!\n");
goto error;
}
m_fd = fd;
fe_type = m_satip_cfg->getFeType();
switch (fe_type)
{
case FE_TYPE_SAT:
strncpy(fe_info.name, "SAT>IP DVB-S2 VTUNER", sizeof(fe_info.name));
strcpy(fe_type_str, "DVB-S2");
fe_info.type = FE_QPSK;
fe_info.frequency_min=925000;
fe_info.frequency_max=2175000;
fe_info.frequency_stepsize=125000;
fe_info.frequency_tolerance=0;
fe_info.symbol_rate_min=1000000;
fe_info.symbol_rate_max=45000000;
fe_info.symbol_rate_tolerance=0;
fe_info.notifier_delay=0;
fe_info.caps=(fe_caps_t)(FE_CAN_INVERSION_AUTO |
FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | FE_CAN_FEC_4_5 |
FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 |
FE_CAN_QPSK | FE_CAN_RECOVER | FE_CAN_2G_MODULATION |
FE_CAN_MULTISTREAM);
break;
case FE_TYPE_CABLE:
strncpy(fe_info.name, "SAT>IP DVB-C VTUNER", sizeof(fe_info.name));
strcpy(fe_type_str,"DVB-C");
fe_info.type = FE_QAM;
fe_info.frequency_min=51000000;
fe_info.frequency_max=858000000;
fe_info.frequency_stepsize=62500;
fe_info.frequency_tolerance=0;
fe_info.symbol_rate_min=(57840000/2)/64; /* SACLK/64 == (XIN/2)/64 */
fe_info.symbol_rate_max=(57840000/2)/4; /* SACLK/4 */
fe_info.symbol_rate_tolerance=0;
fe_info.notifier_delay=0;
fe_info.caps=(fe_caps_t)(FE_CAN_INVERSION_AUTO |
FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 | FE_CAN_QAM_128 |
FE_CAN_QAM_256 | FE_CAN_RECOVER | FE_CAN_FEC_AUTO);
break;
case FE_TYPE_TERRESTRIAL:
strncpy(fe_info.name, "SAT>IP DVB-T VTUNER", sizeof(fe_info.name));
strcpy(fe_type_str,"DVB-T");
fe_info.type = FE_OFDM;
fe_info.frequency_min=0;
fe_info.frequency_max=863250000;
fe_info.frequency_stepsize=62500;
fe_info.frequency_tolerance=0;
fe_info.notifier_delay=0;
fe_info.caps=(fe_caps_t)(FE_CAN_INVERSION_AUTO |
FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QPSK |
FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_2G_MODULATION);
break;
default:
ERROR(MSG_MAIN,"Unknown frontend type: %d\n", fe_type);
goto error;
break;
}
ioctl(fd, VTUNER_SET_NAME, fe_info.name);
ioctl(fd, VTUNER_SET_TYPE, fe_type_str);
ioctl(fd, VTUNER_SET_FE_INFO, &fe_info);
#if DVB_VER_ATLEAST(5, 5)
/* set delsys */
{
u32 ncaps = 0;
struct dtv_property p[1];
p[0].cmd = DTV_ENUM_DELSYS;
memset(p[0].u.buffer.data, 0, sizeof(p[0].u.buffer.data));
switch(fe_type)
{
case FE_TYPE_SAT:
p[0].u.buffer.data[ncaps++] = SYS_DVBS;
if (fe_info.caps & FE_CAN_2G_MODULATION)
p[0].u.buffer.data[ncaps++] = SYS_DVBS2;
break;
case FE_TYPE_CABLE:
p[0].u.buffer.data[ncaps++] = SYS_DVBC_ANNEX_A;
break;
case FE_TYPE_TERRESTRIAL:
p[0].u.buffer.data[ncaps++] = SYS_DVBT;
if (fe_info.caps & FE_CAN_2G_MODULATION)
p[0].u.buffer.data[ncaps++] = SYS_DVBT2;
break;
}
// p[0].u.buffer.len = ncaps;
ioctl(fd, VTUNER_SET_DELSYS, p[0].u.buffer.data);
}
#endif
/* has_outputs */
if (ioctl(fd, VTUNER_SET_HAS_OUTPUTS, "no"))
{
ERROR(MSG_MAIN,"VTUNER_SET_HAS_OUTPUTS error\n");
}
DEBUG(MSG_MAIN, "Vtuner initialize OK!\n");
return 0;
error:
if (m_fd >= 0)
{
close(m_fd);
m_fd = -1;
}
return -1;
}
/*
DTV_FREQUENCY
DTV_DELIVERY_SYSTEM
DTV_MODULATION
DTV_SYMBOL_RATE
DTV_INNER_FEC
DTV_ROLLOFF
DTV_PILOT
DTV_TUNE
DTV_CLEAR
DTV_CODE_RATE_LP
DTV_CODE_RATE_HP
DTV_TRANSMISSION_MODE
DTV_GUARD_INTERVAL
DTV_HIERARCHY
DTV_BANDWIDTH_HZ
DTV_STREAM_ID or DTV_DVBT2_PLP_ID
DTV_INVERSION
DTV_SCRAMBLING_SEQUENCE_INDEX
*/
void satipVtuner::setProperty(struct vtuner_message* msg)
{
struct dtv_property prop = msg->body.prop;
__u32 cmd = prop.cmd;
__u32 data = prop.u.data;
switch(cmd)
{
case DTV_FREQUENCY:
{
DEBUG(MSG_MAIN,"DTV_FREQUENCY : %d\n",(unsigned int)data);
unsigned int freq = 0;
if ( m_satip_cfg->getFeType() == FE_TYPE_SAT) // Khz to Mhz
{
freq = (unsigned int)data/100;
if ( m_tone == SEC_TONE_ON ) /* high band */
{
freq += 106000;
}
else /* low band */
{
freq+=97500;
}
}
else // DVB-T/C, hz to Mhz
{
freq = (unsigned int)data/100000;
}
m_satip_cfg->setFrequency(freq);
break;
}
case DTV_DELIVERY_SYSTEM:
{
DEBUG(MSG_MAIN,"DTV_DELIVERY_SYSTEM : %d\n",(int)data);
m_satip_cfg->setModsys(data);
break;
}
case DTV_MODULATION:
{
DEBUG(MSG_MAIN,"DTV_MODULATION : %d\n", data);
m_satip_cfg->setModtype(data);
break;
}
case DTV_SYMBOL_RATE:
{
DEBUG(MSG_MAIN,"DTV_SYMBOL_RATE : %d\n",(int)data);
int symrate = (unsigned int)data/1000;
m_satip_cfg->setSymrate(symrate);
break;
}
case DTV_INNER_FEC:
{
DEBUG(MSG_MAIN,"DTV_INNER_FEC : %d\n",(int)data);
int fec = data & 31;
if (fec <= FEC_9_10)
{
m_satip_cfg->setFec(fec);
}
else
{
ERROR(MSG_MAIN,"invalid FEC configured\n");
}
break;
}
case DTV_ROLLOFF:
{
DEBUG(MSG_MAIN,"DTV_ROLLOFF : %d\n",(int)data);
m_satip_cfg->setRolloff(data);
break;
}
case DTV_PILOT:
{
DEBUG(MSG_MAIN,"DTV_PILOT : %d\n",(int)data);
m_satip_cfg->setPilots(data);
break;
}
case DTV_TUNE:
{
DEBUG(MSG_MAIN,"DTV_TUNE \n");
m_satip_cfg->setChannelChanged();
break;
}
case DTV_CLEAR:
{
DEBUG(MSG_MAIN,"DTV_CLEAR \n");
m_satip_cfg->clearProperty();
break;
}
case DTV_CODE_RATE_LP:
{
DEBUG(MSG_MAIN,"DTV_CODE_RATE_LP \n");
fe_code_rate_t fec_inner = FEC_AUTO;
m_satip_cfg->setFec(fec_inner);
break;
}
case DTV_CODE_RATE_HP:
{
DEBUG(MSG_MAIN,"DTV_CODE_RATE_HP \n");
fe_code_rate_t fec_inner = FEC_AUTO;
m_satip_cfg->setFec(fec_inner);
break;
}
case DTV_TRANSMISSION_MODE:
{
DEBUG(MSG_MAIN,"DTV_TRANSMISSION_MODE : %d\n",(int)data);
m_satip_cfg->setTransmode(data);
break;
}
case DTV_GUARD_INTERVAL:
{
DEBUG(MSG_MAIN,"DTV_GUARD_INTERVAL : %d\n",(int)data);
m_satip_cfg->setGuardInterval(data);
break;
}
case DTV_HIERARCHY:
{
DEBUG(MSG_MAIN,"DTV_HIERARCHY skip..\n");
break;
}
case DTV_BANDWIDTH_HZ:
{
DEBUG(MSG_MAIN,"DTV_BANDWIDTH_HZ : %d\n",(int)data);
m_satip_cfg->setBandwidth(data);
break;
}
case DTV_STREAM_ID:
DEBUG(MSG_MAIN,"DTV_STREAM_ID : %d\n",(int)data);
{
unsigned int plp = (unsigned int)data;
m_satip_cfg->setPLP(plp);
break;
}
case DTV_INVERSION:
{
DEBUG(MSG_MAIN,"DTV_INVERSION skip..\n");
break;
}
#if DVB_VER_ATLEAST(5, 11)
case DTV_SCRAMBLING_SEQUENCE_INDEX:
DEBUG(MSG_MAIN,"DTV_SCRAMBLING_SEQUENCE_INDEX : %d\n",(int)data);
{
unsigned int pls_code = (unsigned int)data;
m_satip_cfg->setPLScode(pls_code);
break;
}
#endif
default:
break;
}
}
void satipVtuner::setDiseqc(struct vtuner_message* msg)
{
struct dvb_diseqc_master_cmd* cmd=&msg->body.diseqc_master_cmd;
if ( cmd->msg[0] == 0xe0 && cmd->msg[1] == 0x10 && cmd->msg[2] == 0x38 && cmd->msg_len == 4 )
{
/* committed switch */
u8 data=cmd->msg[3];
int voltage;
if ( (data & 0x01) == 0x01 )
{
m_tone = SEC_TONE_ON; /* high band */
}
else if ( (data & 0x11) == 0x10 )
{
m_tone = SEC_TONE_OFF; /* low band */
}
if ( (data & 0x02) == 0x02 )
{
voltage = SEC_VOLTAGE_18;
m_satip_cfg->setVoltage(SEC_VOLTAGE_18);
}
else if ( (data & 0x22) == 0x20 )
{
voltage = SEC_VOLTAGE_13;
m_satip_cfg->setVoltage(SEC_VOLTAGE_13);
}
int position = ( (data & 0x0c) >> 2) + 1;
m_satip_cfg->setPosition(position);
DEBUG(MSG_MAIN,"SEC_TONE : %s, SEC_VOLTAGE : %s, pos : %d\n", m_tone == SEC_TONE_ON ? "ON" : "OFF",
voltage == SEC_VOLTAGE_13 ? "V" : "H", position);
}
else if ( cmd->msg[0] == 0xe0 && cmd->msg[1] == 0x10 && cmd->msg[2] == 0x39 && cmd->msg_len == 4 )
{
/* uncommitted switch */
u8 data=cmd->msg[3];
int voltage;
int position = (data & 0x0F) + 1;
m_satip_cfg->setPosition(position);
DEBUG(MSG_MAIN,"uncommitted switch pos : %d\n", position);
}
}
void satipVtuner::setPidList(struct vtuner_message* msg)
{
u16* pid_list = msg->body.pidlist;
m_satip_cfg->updatePidList(pid_list);
}
void satipVtuner::vtunerEvent()
{
struct vtuner_message msg;
if (ioctl(m_fd, VTUNER_GET_MESSAGE, &msg))
return;
switch(msg.type)
{
case MSG_SET_FRONTEND:
#if HAVE_NO_MSG16
DEBUG(MSG_MAIN,"MSG_SET_PROPERTY old Mode\n");
setProperty(&msg);
#else
DEBUG(MSG_MAIN,"MSG_SET_FRONTEND skip..\n");
#endif
++tune_tries;
break;
case MSG_GET_FRONTEND:
DEBUG(MSG_MAIN,"MSG_GET_FRONTEND: Not implemented!\n");
break;
case MSG_SET_TONE:
m_tone = msg.body.tone;
DEBUG(MSG_MAIN,"MSG_SET_TONE: %s\n", m_tone == SEC_TONE_ON ? "high" : "low");
break;
case MSG_SET_VOLTAGE:
DEBUG(MSG_MAIN,"MSG_SET_VOLTAGE: %d\n",msg.body.voltage);
m_satip_cfg->setVoltage(msg.body.voltage);
break;
case MSG_PIDLIST:
setPidList(&msg);
DEBUG(MSG_MAIN,"MSG_SET_PIDLIST: \n");
return;
break;
case MSG_READ_STATUS:
msg.body.status = 0;
//INFO(MSG_MAIN,"MSG_READ_STATUS\n");
if (m_satip_rtp && m_satip_rtp->getHasLock())
msg.body.status = FE_HAS_LOCK;
else {
if (timeout_msec(timeout) < 0)
msg.body.status |= FE_TIMEDOUT;
if (tune_tries & 4)
msg.body.status |= FE_HAS_SIGNAL;
}
break;
case MSG_READ_BER:
//INFO(MSG_MAIN,"MSG_READ_BER\n");
msg.body.ber = 0;
break;
case MSG_READ_SIGNAL_STRENGTH:
//INFO(MSG_MAIN,"MSG_READ_SIGNAL_STRENGTH %d -> %d\n", msg.body.ss, rtcp_data.signalStrength);
if (m_satip_rtp)
msg.body.ss = (u16)m_satip_rtp->getSignalStrength();
break;
case MSG_READ_SNR:
//INFO(MSG_MAIN,"MSG_READ_SNR %d -> %d\n", msg.body.snr, m_satip_cfg->satip_get_signal_quality());
if (m_satip_rtp)
msg.body.snr = (u16)m_satip_rtp->getSignalStrength() * (u16)m_satip_rtp->getSignalQuality() / 65535;
break;
case MSG_READ_UCBLOCKS:
DEBUG(MSG_MAIN,"MSG_READ_UCBLOCKS\n");
msg.body.ucb = 0;
break;
case MSG_SEND_DISEQC_BURST:
DEBUG(MSG_MAIN,"MSG_SEND_DISEQC_BURST\n");
if ( msg.body.burst == SEC_MINI_A )
m_satip_cfg->setPosition(1);
else if ( msg.body.burst == SEC_MINI_B )
m_satip_cfg->setPosition(2);
else
ERROR(MSG_MAIN,"invalid diseqc burst %d\n",msg.body.burst);
break;
case MSG_SEND_DISEQC_MSG:
DEBUG(MSG_MAIN,"MSG_SEND_DISEQC_MSG: %02x %02x %02x %02x %02x %02x len %d\n",
msg.body.diseqc_master_cmd.msg[0],
msg.body.diseqc_master_cmd.msg[1],
msg.body.diseqc_master_cmd.msg[2],
msg.body.diseqc_master_cmd.msg[3],
msg.body.diseqc_master_cmd.msg[4],
msg.body.diseqc_master_cmd.msg[5],
msg.body.diseqc_master_cmd.msg_len);
setDiseqc(&msg);
break;
case MSG_SET_PROPERTY:
DEBUG(MSG_MAIN,"MSG_SET_PROPERTY\n");
setProperty(&msg);
break;
case MSG_GET_PROPERTY:
DEBUG(MSG_MAIN,"MSG_GET_PROPERTY\n");
break;
case MSG_GET_TUNE_SETTINGS:
DEBUG(MSG_MAIN,"MSG_GET_TUNE_SETTINGS\n");
if (m_satip_rtp) {
m_satip_rtp->unset();
}
tune_tries = 0;
clock_gettime(CLOCK_MONOTONIC, &timeout);
timeout += 3000;
msg.body.tune_settings.min_delay_ms = 50; // let linux-tv api ask every 50ms for locked/unlocked state...
msg.body.tune_settings.step_size = 0;
msg.body.tune_settings.max_drift = 0;
break;
default:
DEBUG(MSG_MAIN,"MSG_UNKNOWN: %d\n", msg.type);
break;
}
msg.type=0;
if (ioctl(m_fd, VTUNER_SET_RESPONSE, &msg))
return;
}