-
Notifications
You must be signed in to change notification settings - Fork 26
/
dvb.c
1548 lines (1367 loc) · 50.7 KB
/
dvb.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* dvb.c: linux-dvb input for DVBlast
*****************************************************************************
* Copyright (C) 2008-2010, 2015 VideoLAN
*
* Authors: Christophe Massiot <[email protected]>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* 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 "config.h"
#ifdef HAVE_DVB_SUPPORT
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <errno.h>
/* DVB Card Drivers */
#include <linux/dvb/version.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/frontend.h>
#include <linux/dvb/ca.h>
#include <ev.h>
#if DVBAPI_VERSION < 508
#define DTV_STREAM_ID 42
#define FE_CAN_MULTISTREAM 0x4000000
#define FE_CAN_TURBO_FEC 0x8000000
#endif
#define MAX_DELIVERY_SYSTEMS 20
#include "dvblast.h"
#include "en50221.h"
#include "comm.h"
#include <bitstream/common.h>
/*****************************************************************************
* Local declarations
*****************************************************************************/
#define DVR_READ_TIMEOUT 30000000 /* 30 s */
#define MAX_READ_ONCE 50
#define DVR_BUFFER_SIZE 40*188*1024 /* bytes */
int i_dvr_buffer_size = DVR_BUFFER_SIZE;
static int i_frontend, i_dvr;
static struct ev_io frontend_watcher, dvr_watcher;
static struct ev_timer lock_watcher, mute_watcher, print_watcher;
static fe_status_t i_last_status;
static block_t *p_freelist = NULL;
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void DVRRead(struct ev_loop *loop, struct ev_io *w, int revents);
static void DVRMuteCb(struct ev_loop *loop, struct ev_timer *w, int revents);
static void FrontendRead(struct ev_loop *loop, struct ev_io *w, int revents);
static void FrontendLockCb(struct ev_loop *loop, struct ev_timer *w, int revents);
static void FrontendSet( bool b_reset );
/*****************************************************************************
* dvb_Open
*****************************************************************************/
void dvb_Open( void )
{
char psz_tmp[128];
msg_Dbg( NULL, "compiled with DVB API version %d.%d", DVB_API_VERSION, DVB_API_VERSION_MINOR );
if ( i_frequency )
{
sprintf( psz_tmp, "/dev/dvb/adapter%d/frontend%d", i_adapter, i_fenum );
if( (i_frontend = open(psz_tmp, O_RDWR | O_NONBLOCK)) < 0 )
{
msg_Err( NULL, "opening device %s failed (%s)", psz_tmp,
strerror(errno) );
exit(1);
}
FrontendSet(true);
}
else
{
i_frontend = -1;
}
sprintf( psz_tmp, "/dev/dvb/adapter%d/dvr%d", i_adapter, i_fenum );
if( (i_dvr = open(psz_tmp, O_RDONLY | O_NONBLOCK)) < 0 )
{
msg_Err( NULL, "opening device %s failed (%s)", psz_tmp,
strerror(errno) );
exit(1);
}
if ( ioctl( i_dvr, DMX_SET_BUFFER_SIZE, i_dvr_buffer_size ) < 0 )
{
msg_Warn( NULL, "couldn't set %s buffer size (%s)", psz_tmp,
strerror(errno) );
}
ev_io_init(&dvr_watcher, DVRRead, i_dvr, EV_READ);
ev_io_start(event_loop, &dvr_watcher);
if ( i_frontend != -1 )
{
ev_io_init(&frontend_watcher, FrontendRead, i_frontend, EV_READ);
ev_io_start(event_loop, &frontend_watcher);
}
ev_timer_init(&lock_watcher, FrontendLockCb,
i_frontend_timeout_duration / 1000000.,
i_frontend_timeout_duration / 1000000.);
ev_timer_init(&mute_watcher, DVRMuteCb,
DVR_READ_TIMEOUT / 1000000.,
DVR_READ_TIMEOUT / 1000000.);
en50221_Init();
}
/*****************************************************************************
* dvb_Reset
*****************************************************************************/
void dvb_Reset( void )
{
if ( i_frequency )
FrontendSet(true);
}
/*****************************************************************************
* DVR events
*****************************************************************************/
static void DVRRead(struct ev_loop *loop, struct ev_io *w, int revents)
{
int i, i_len;
block_t *p_ts = p_freelist, **pp_current = &p_ts;
struct iovec p_iov[MAX_READ_ONCE];
for ( i = 0; i < MAX_READ_ONCE; i++ )
{
if ( (*pp_current) == NULL ) *pp_current = block_New();
p_iov[i].iov_base = (*pp_current)->p_ts;
p_iov[i].iov_len = TS_SIZE;
pp_current = &(*pp_current)->p_next;
}
if ( (i_len = readv(i_dvr, p_iov, MAX_READ_ONCE)) < 0 )
{
msg_Err( NULL, "couldn't read from DVR device (%s)",
strerror(errno) );
i_len = 0;
}
i_len /= TS_SIZE;
if ( i_len )
ev_timer_again(loop, &mute_watcher);
pp_current = &p_ts;
while ( i_len && *pp_current )
{
pp_current = &(*pp_current)->p_next;
i_len--;
}
p_freelist = *pp_current;
*pp_current = NULL;
demux_Run( p_ts );
}
static void DVRMuteCb(struct ev_loop *loop, struct ev_timer *w, int revents)
{
msg_Warn( NULL, "no DVR output, resetting" );
ev_timer_stop(loop, w);
switch (i_print_type) {
case PRINT_XML:
fprintf(print_fh, "<EVENT type=\"reset\" cause=\"dvr\" />\n");
break;
case PRINT_TEXT:
fprintf(print_fh, "reset cause: dvr\n");
break;
default:
break;
}
if ( i_frequency )
FrontendSet(false);
en50221_Reset();
}
/*
* Demux
*/
/*****************************************************************************
* dvb_SetFilter : controls the demux to add a filter
*****************************************************************************/
int dvb_SetFilter( uint16_t i_pid )
{
struct dmx_pes_filter_params s_filter_params;
char psz_tmp[128];
int i_fd;
sprintf( psz_tmp, "/dev/dvb/adapter%d/demux%d", i_adapter, i_fenum );
if( (i_fd = open(psz_tmp, O_RDWR)) < 0 )
{
msg_Err( NULL, "DMXSetFilter: opening device failed (%s)",
strerror(errno) );
return -1;
}
s_filter_params.pid = i_pid;
s_filter_params.input = DMX_IN_FRONTEND;
s_filter_params.output = DMX_OUT_TS_TAP;
s_filter_params.flags = DMX_IMMEDIATE_START;
s_filter_params.pes_type = DMX_PES_OTHER;
if ( ioctl( i_fd, DMX_SET_PES_FILTER, &s_filter_params ) < 0 )
{
msg_Err( NULL, "failed setting filter on %d (%s)", i_pid,
strerror(errno) );
close( i_fd );
return -1;
}
msg_Dbg( NULL, "setting filter on PID %d", i_pid );
return i_fd;
}
/*****************************************************************************
* dvb_UnsetFilter : removes a filter
*****************************************************************************/
void dvb_UnsetFilter( int i_fd, uint16_t i_pid )
{
if ( ioctl( i_fd, DMX_STOP ) < 0 )
msg_Err( NULL, "DMX_STOP failed (%s)", strerror(errno) );
else
msg_Dbg( NULL, "unsetting filter on PID %d", i_pid );
close( i_fd );
}
/*
* Frontend
*/
/*****************************************************************************
* Print info
*****************************************************************************/
static void PrintCb( struct ev_loop *loop, struct ev_timer *w, int revents )
{
uint32_t i_ber = 0;
uint16_t i_strength = 0, i_snr = 0;
uint32_t i_uncorrected = 0;
ioctl(i_frontend, FE_READ_BER, &i_ber);
ioctl(i_frontend, FE_READ_SIGNAL_STRENGTH, &i_strength);
ioctl(i_frontend, FE_READ_SNR, &i_snr);
ioctl(i_frontend, FE_READ_UNCORRECTED_BLOCKS, &i_uncorrected);
int64_t i_strength_dbm = i_strength;
int64_t i_snr_db = i_snr;
#ifdef DTV_STAT_SIGNAL_STRENGTH
struct dtv_property prop[] = {
{ .cmd = DTV_STAT_SIGNAL_STRENGTH },
{ .cmd = DTV_STAT_CNR },
};
struct dtv_properties props = { .num = 2, .props = prop };
if (ioctl(i_frontend, FE_GET_PROPERTY, &props) != -1)
{
if (prop[0].u.st.len > 0 &&
prop[0].u.st.stat[0].scale == FE_SCALE_DECIBEL)
i_strength_dbm = prop[0].u.st.stat[0].svalue;
if (prop[1].u.st.len > 0 &&
prop[1].u.st.stat[0].scale == FE_SCALE_DECIBEL)
i_snr_db = prop[1].u.st.stat[0].svalue;
}
#endif
switch (i_print_type)
{
case PRINT_XML:
fprintf(print_fh,
"<STATUS type=\"frontend\" ber=\"%"PRIu32"\" strength=\"%"PRId64"\" snr=\"%"PRId64"\" uncorrected=\"%"PRIu32"\" />\n",
i_ber, i_strength_dbm, i_snr_db, i_uncorrected);
break;
case PRINT_TEXT:
fprintf(print_fh, "frontend ber: %"PRIu32" strength: %"PRId64" snr: %"PRId64" uncorrected: %"PRIu32"\n",
i_ber, i_strength_dbm, i_snr_db, i_uncorrected);
break;
default:
break;
}
}
/*****************************************************************************
* Frontend events
*****************************************************************************/
static void FrontendRead(struct ev_loop *loop, struct ev_io *w, int revents)
{
struct dvb_frontend_event event;
fe_status_t i_status, i_diff;
for( ;; )
{
int i_ret = ioctl( i_frontend, FE_GET_EVENT, &event );
if( i_ret < 0 )
{
if( errno == EWOULDBLOCK )
return; /* no more events */
msg_Err( NULL, "reading frontend event failed (%d) %s",
i_ret, strerror(errno) );
return;
}
i_status = event.status;
i_diff = i_status ^ i_last_status;
i_last_status = i_status;
{
#define IF_UP( x ) \
} \
if ( i_diff & (x) ) \
{ \
if ( i_status & (x) )
IF_UP( FE_HAS_SIGNAL )
msg_Dbg( NULL, "frontend has acquired signal" );
else
msg_Dbg( NULL, "frontend has lost signal" );
IF_UP( FE_HAS_CARRIER )
msg_Dbg( NULL, "frontend has acquired carrier" );
else
msg_Dbg( NULL, "frontend has lost carrier" );
IF_UP( FE_HAS_VITERBI )
msg_Dbg( NULL, "frontend has acquired stable FEC" );
else
msg_Dbg( NULL, "frontend has lost FEC" );
IF_UP( FE_HAS_SYNC )
msg_Dbg( NULL, "frontend has acquired sync" );
else
msg_Dbg( NULL, "frontend has lost sync" );
IF_UP( FE_HAS_LOCK )
{
int32_t i_value = 0;
msg_Info( NULL, "frontend has acquired lock" );
switch (i_print_type) {
case PRINT_XML:
fprintf(print_fh, "<STATUS type=\"lock\" status=\"1\" />\n");
break;
case PRINT_TEXT:
fprintf(print_fh, "lock status: 1\n");
break;
default:
break;
}
ev_timer_stop(loop, &lock_watcher);
ev_timer_again(loop, &mute_watcher);
/* Read some statistics */
if( ioctl( i_frontend, FE_READ_BER, &i_value ) >= 0 )
msg_Dbg( NULL, "- Bit error rate: %d", i_value );
if( ioctl( i_frontend, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
msg_Dbg( NULL, "- Signal strength: %d", i_value );
if( ioctl( i_frontend, FE_READ_SNR, &i_value ) >= 0 )
msg_Dbg( NULL, "- SNR: %d", i_value );
if (i_print_period)
{
ev_timer_init( &print_watcher, PrintCb,
i_print_period / 1000000.,
i_print_period / 1000000. );
ev_timer_start( event_loop, &print_watcher );
}
}
else
{
msg_Dbg( NULL, "frontend has lost lock" );
switch (i_print_type) {
case PRINT_XML:
fprintf(print_fh, "<STATUS type=\"lock\" status=\"0\"/>\n");
break;
case PRINT_TEXT:
fprintf(print_fh, "lock status: 0\n");
break;
default:
break;
}
if (i_frontend_timeout_duration)
{
ev_timer_stop(event_loop, &lock_watcher);
ev_timer_again(loop, &mute_watcher);
}
if (i_print_period)
ev_timer_stop(event_loop, &print_watcher);
}
IF_UP( FE_REINIT )
{
/* The frontend was reinited. */
msg_Warn( NULL, "reiniting frontend");
if ( i_frequency )
FrontendSet(true);
}
}
#undef IF_UP
}
}
static void FrontendLockCb(struct ev_loop *loop, struct ev_timer *w, int revents)
{
if ( i_quit_timeout_duration )
{
msg_Err( NULL, "no lock" );
ev_break(loop, EVBREAK_ALL);
return;
}
msg_Warn( NULL, "no lock, tuning again" );
ev_timer_stop(loop, w);
switch (i_print_type) {
case PRINT_XML:
fprintf(print_fh, "<EVENT type=\"reset\" cause=\"nolock\" />\n");
break;
case PRINT_TEXT:
fprintf(print_fh, "reset cause: nolock\n");
break;
default:
break;
}
if ( i_frequency )
FrontendSet(false);
}
static int FrontendDoDiseqc(void)
{
fe_sec_voltage_t fe_voltage;
fe_sec_tone_mode_t fe_tone;
int bis_frequency;
switch ( i_voltage )
{
case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
default:
case 13: fe_voltage = SEC_VOLTAGE_13; break;
case 18: fe_voltage = SEC_VOLTAGE_18; break;
}
fe_tone = b_tone ? SEC_TONE_ON : SEC_TONE_OFF;
if ( strcmp( psz_lnb_type, "universal" ) == 0 )
{
/* Automatic mode. */
if ( i_frequency >= 950000 && i_frequency <= 2150000 )
{
msg_Dbg( NULL, "frequency %d is in IF-band", i_frequency );
bis_frequency = i_frequency;
}
else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
{
msg_Dbg( NULL, "frequency %d is in S-band", i_frequency );
bis_frequency = 3650000 - i_frequency;
}
else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
{
msg_Dbg( NULL, "frequency %d is in C-band (lower)", i_frequency );
bis_frequency = 5150000 - i_frequency;
}
else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
{
msg_Dbg( NULL, "frequency %d is in C-band (higher)", i_frequency );
bis_frequency = 5950000 - i_frequency;
}
else if ( i_frequency >= 10700000 && i_frequency < 11700000 )
{
msg_Dbg( NULL, "frequency %d is in Ku-band (lower)",
i_frequency );
bis_frequency = i_frequency - 9750000;
}
else if ( i_frequency >= 11700000 && i_frequency <= 13250000 )
{
msg_Dbg( NULL, "frequency %d is in Ku-band (higher)",
i_frequency );
bis_frequency = i_frequency - 10600000;
fe_tone = SEC_TONE_ON;
}
else
{
msg_Err( NULL, "frequency %d is out of any known band",
i_frequency );
exit(1);
}
}
else if ( strcmp( psz_lnb_type, "old-sky" ) == 0 )
{
if ( i_frequency >= 11700000 && i_frequency <= 13250000 )
{
msg_Dbg( NULL, "frequency %d is in Ku-band (higher)",
i_frequency );
bis_frequency = i_frequency - 11300000;
fe_tone = SEC_TONE_ON;
}
else
{
msg_Err( NULL, "frequency %d is out of any known band",
i_frequency );
exit(1);
}
}
else
{
msg_Err( NULL, "lnb-type '%s' is not known. Valid type: universal old-sky",
psz_lnb_type );
exit(1);
}
/* Switch off continuous tone. */
if ( ioctl( i_frontend, FE_SET_TONE, SEC_TONE_OFF ) < 0 )
{
msg_Err( NULL, "FE_SET_TONE failed (%s)", strerror(errno) );
exit(1);
}
/* Configure LNB voltage. */
if ( ioctl( i_frontend, FE_SET_VOLTAGE, fe_voltage ) < 0 )
{
msg_Err( NULL, "FE_SET_VOLTAGE failed (%s)", strerror(errno) );
exit(1);
}
/* Wait for at least 15 ms. Currently 100 ms because of broken drivers. */
msleep(100000);
/* Diseqc */
if ( i_satnum > 0 && i_satnum < 5 )
{
/* digital satellite equipment control,
* specification is available from http://www.eutelsat.com/
*/
/* DiSEqC 1.1 */
struct dvb_diseqc_master_cmd uncmd =
{ {0xe0, 0x10, 0x39, 0xf0, 0x00, 0x00}, 4};
/* DiSEqC 1.0 */
struct dvb_diseqc_master_cmd cmd =
{ {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4};
cmd.msg[3] = 0xf0 /* reset bits */
| ((i_satnum - 1) << 2)
| (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
| (fe_tone == SEC_TONE_ON ? 1 : 0);
if ( i_uncommitted > 0 && i_uncommitted < 17 )
{
uncmd.msg[3] = 0xf0 /* reset bits */
| (i_uncommitted - 1);
if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &uncmd ) < 0 )
{
msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
strerror(errno) );
exit(1);
}
/* Repeat uncommitted command */
uncmd.msg[0] = 0xe1; /* framing: master, no reply, repeated TX */
if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &uncmd ) < 0 )
{
msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
strerror(errno) );
exit(1);
}
/* Pause 125 ms between uncommitted & committed diseqc commands. */
msleep(125000);
}
if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd ) < 0 )
{
msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
strerror(errno) );
exit(1);
}
msleep(100000); /* Should be 15 ms. */
/* Do it again just to be sure. */
cmd.msg[0] = 0xe1; /* framing: master, no reply, repeated TX */
if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd ) < 0 )
{
msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
strerror(errno) );
exit(1);
}
msleep(100000); /* Again, should be 15 ms */
}
else if ( i_satnum == 0xA || i_satnum == 0xB )
{
/* A or B simple diseqc ("diseqc-compatible") */
if( ioctl( i_frontend, FE_DISEQC_SEND_BURST,
i_satnum == 0xB ? SEC_MINI_B : SEC_MINI_A ) < 0 )
{
msg_Err( NULL, "ioctl FE_SEND_BURST failed (%s)", strerror(errno) );
exit(1);
}
msleep(100000); /* ... */
}
if ( ioctl( i_frontend, FE_SET_TONE, fe_tone ) < 0 )
{
msg_Err( NULL, "FE_SET_TONE failed (%s)", strerror(errno) );
exit(1);
}
msleep(100000); /* ... */
msg_Dbg( NULL, "configuring LNB to v=%d p=%d satnum=%x uncommitted=%x lnb-type=%s bis_frequency=%d",
i_voltage, b_tone, i_satnum, i_uncommitted, psz_lnb_type, bis_frequency );
return bis_frequency;
}
#if DVB_API_VERSION >= 5
#if DVBAPI_VERSION < 505
#warning Your linux-dvb headers are old, you should consider upgrading your kernel and/or compiling against different kernel headers
#endif
/*****************************************************************************
* Helper functions for S2API
*****************************************************************************/
static fe_spectral_inversion_t GetInversion(void)
{
switch ( i_inversion )
{
case 0: return INVERSION_OFF;
case 1: return INVERSION_ON;
default:
msg_Warn( NULL, "invalid inversion %d", i_inversion );
case -1: return INVERSION_AUTO;
}
}
static fe_code_rate_t GetFEC(fe_caps_t fe_caps, int i_fec_value)
{
#define GET_FEC_INNER(fec, val) \
if ( (fe_caps & FE_CAN_##fec) && (i_fec_value == val) ) \
return fec;
GET_FEC_INNER(FEC_AUTO, 999);
GET_FEC_INNER(FEC_AUTO, -1);
if (i_fec_value == 0)
return FEC_NONE;
GET_FEC_INNER(FEC_1_2, 12);
GET_FEC_INNER(FEC_2_3, 23);
GET_FEC_INNER(FEC_3_4, 34);
if (i_fec_value == 35)
return FEC_3_5;
GET_FEC_INNER(FEC_4_5, 45);
GET_FEC_INNER(FEC_5_6, 56);
GET_FEC_INNER(FEC_6_7, 67);
GET_FEC_INNER(FEC_7_8, 78);
GET_FEC_INNER(FEC_8_9, 89);
if (i_fec_value == 910)
return FEC_9_10;
#undef GET_FEC_INNER
msg_Warn(NULL, "invalid FEC %d", i_fec_value );
return FEC_AUTO;
}
#define GetFECInner(caps) GetFEC(caps, i_fec)
#define GetFECLP(caps) GetFEC(caps, i_fec_lp)
static fe_modulation_t GetModulation(void)
{
#define GET_MODULATION( mod ) \
if ( !strcasecmp( psz_modulation, #mod ) ) \
return mod;
GET_MODULATION(QPSK);
GET_MODULATION(QAM_16);
GET_MODULATION(QAM_32);
GET_MODULATION(QAM_64);
GET_MODULATION(QAM_128);
GET_MODULATION(QAM_256);
GET_MODULATION(QAM_AUTO);
GET_MODULATION(VSB_8);
GET_MODULATION(VSB_16);
GET_MODULATION(PSK_8);
GET_MODULATION(APSK_16);
GET_MODULATION(APSK_32);
GET_MODULATION(DQPSK);
#undef GET_MODULATION
msg_Err( NULL, "invalid modulation %s", psz_modulation );
exit(1);
}
static fe_pilot_t GetPilot(void)
{
switch ( i_pilot )
{
case 0: return PILOT_OFF;
case 1: return PILOT_ON;
default:
msg_Warn( NULL, "invalid pilot %d", i_pilot );
case -1: return PILOT_AUTO;
}
}
static fe_rolloff_t GetRollOff(void)
{
switch ( i_rolloff )
{
case -1:
case 0: return ROLLOFF_AUTO;
case 20: return ROLLOFF_20;
case 25: return ROLLOFF_25;
default:
msg_Warn( NULL, "invalid rolloff %d", i_rolloff );
case 35: return ROLLOFF_35;
}
}
static fe_guard_interval_t GetGuard(void)
{
switch ( i_guard )
{
case 32: return GUARD_INTERVAL_1_32;
case 16: return GUARD_INTERVAL_1_16;
case 8: return GUARD_INTERVAL_1_8;
case 4: return GUARD_INTERVAL_1_4;
default:
msg_Warn( NULL, "invalid guard interval %d", i_guard );
case -1:
case 0: return GUARD_INTERVAL_AUTO;
}
}
static fe_transmit_mode_t GetTransmission(void)
{
switch ( i_transmission )
{
case 2: return TRANSMISSION_MODE_2K;
case 8: return TRANSMISSION_MODE_8K;
#ifdef TRANSMISSION_MODE_4K
case 4: return TRANSMISSION_MODE_4K;
#endif
default:
msg_Warn( NULL, "invalid tranmission mode %d", i_transmission );
case -1:
case 0: return TRANSMISSION_MODE_AUTO;
}
}
static fe_hierarchy_t GetHierarchy(void)
{
switch ( i_hierarchy )
{
case 0: return HIERARCHY_NONE;
case 1: return HIERARCHY_1;
case 2: return HIERARCHY_2;
case 4: return HIERARCHY_4;
default:
msg_Warn( NULL, "invalid intramission mode %d", i_transmission );
case -1: return HIERARCHY_AUTO;
}
}
/*****************************************************************************
* FrontendInfo : Print frontend info
*****************************************************************************/
static void FrontendInfo( struct dvb_frontend_info *info, uint32_t version,
fe_delivery_system_t *p_systems, int i_systems )
{
msg_Dbg( NULL, "using DVB API version %d.%d", version / 256, version % 256 );
msg_Dbg( NULL, "Frontend \"%s\" supports:", info->name );
msg_Dbg( NULL, " frequency min: %d, max: %d, stepsize: %d, tolerance: %d",
info->frequency_min, info->frequency_max,
info->frequency_stepsize, info->frequency_tolerance );
msg_Dbg( NULL, " symbolrate min: %d, max: %d, tolerance: %d",
info->symbol_rate_min, info->symbol_rate_max, info->symbol_rate_tolerance);
msg_Dbg( NULL, " capabilities:" );
#define FRONTEND_INFO(caps,val,msg) \
if ( caps & val ) \
msg_Dbg( NULL, " %s", msg );
FRONTEND_INFO( info->caps, FE_IS_STUPID, "FE_IS_STUPID" )
FRONTEND_INFO( info->caps, FE_CAN_INVERSION_AUTO, "INVERSION_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_1_2, "FEC_1_2" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_2_3, "FEC_2_3" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_3_4, "FEC_3_4" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_4_5, "FEC_4_5" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_5_6, "FEC_5_6" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_6_7, "FEC_6_7" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_7_8, "FEC_7_8" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_8_9, "FEC_8_9" )
FRONTEND_INFO( info->caps, FE_CAN_FEC_AUTO,"FEC_AUTO")
FRONTEND_INFO( info->caps, FE_CAN_QPSK, "QPSK" )
FRONTEND_INFO( info->caps, FE_CAN_QAM_16, "QAM_16" )
FRONTEND_INFO( info->caps, FE_CAN_QAM_32, "QAM_32" )
FRONTEND_INFO( info->caps, FE_CAN_QAM_64, "QAM_64" )
FRONTEND_INFO( info->caps, FE_CAN_QAM_128,"QAM_128")
FRONTEND_INFO( info->caps, FE_CAN_QAM_256,"QAM_256")
FRONTEND_INFO( info->caps, FE_CAN_QAM_AUTO,"QAM_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_TRANSMISSION_MODE_AUTO, "TRANSMISSION_MODE_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_BANDWIDTH_AUTO, "BANDWIDTH_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_GUARD_INTERVAL_AUTO, "GUARD_INTERVAL_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_HIERARCHY_AUTO, "HIERARCHY_AUTO" )
FRONTEND_INFO( info->caps, FE_CAN_8VSB, "8VSB" )
FRONTEND_INFO( info->caps, FE_CAN_16VSB,"16VSB" )
FRONTEND_INFO( info->caps, FE_HAS_EXTENDED_CAPS, "EXTENDED_CAPS" )
#if DVBAPI_VERSION >= 501
FRONTEND_INFO( info->caps, FE_CAN_2G_MODULATION, "2G_MODULATION" )
#endif
FRONTEND_INFO( info->caps, FE_CAN_MULTISTREAM, "MULTISTREAM" )
FRONTEND_INFO( info->caps, FE_CAN_TURBO_FEC, "TURBO_FEC" )
FRONTEND_INFO( info->caps, FE_NEEDS_BENDING, "NEEDS_BENDING" )
FRONTEND_INFO( info->caps, FE_CAN_RECOVER, "FE_CAN_RECOVER" )
FRONTEND_INFO( info->caps, FE_CAN_MUTE_TS, "FE_CAN_MUTE_TS" )
#undef FRONTEND_INFO
msg_Dbg( NULL, " delivery systems:" );
int i;
for ( i = 0; i < i_systems; i++ )
{
switch ( p_systems[i] )
{
#define DELSYS_INFO(delsys, msg) \
case delsys: msg_Dbg( NULL, " %s", msg); break;
DELSYS_INFO( SYS_ATSC, "ATSC" )
DELSYS_INFO( SYS_ATSCMH, "ATSCMH" )
DELSYS_INFO( SYS_CMMB, "CMBB" )
DELSYS_INFO( SYS_DAB, "DAB" )
DELSYS_INFO( SYS_DSS, "DSS" )
DELSYS_INFO( SYS_DVBC_ANNEX_B, "DVBC_ANNEX_B" )
DELSYS_INFO( SYS_DVBH, "DVBH" )
DELSYS_INFO( SYS_DVBS, "DVBS" )
DELSYS_INFO( SYS_DVBS2, "DVBS2" )
DELSYS_INFO( SYS_DVBT, "DVBT" )
DELSYS_INFO( SYS_ISDBC, "ISDBC" )
DELSYS_INFO( SYS_ISDBS, "ISDBS" )
DELSYS_INFO( SYS_ISDBT, "ISDBT" )
DELSYS_INFO( SYS_UNDEFINED, "UNDEFINED" )
#if DVBAPI_VERSION >= 505
DELSYS_INFO( SYS_DVBC_ANNEX_A, "DVBC_ANNEX_A" )
DELSYS_INFO( SYS_DVBC_ANNEX_C, "DVBC_ANNEX_C" )
DELSYS_INFO( SYS_DVBT2, "DVBT2" )
DELSYS_INFO( SYS_TURBO, "TURBO" )
#else
DELSYS_INFO( SYS_DVBC_ANNEX_AC, "DVBC_ANNEX_AC" )
#endif
#if DVBAPI_VERSION >= 507
DELSYS_INFO( SYS_DTMB, "DTMB" )
#else
DELSYS_INFO( SYS_DMBTH, "DMBTH" )
#endif
default: msg_Dbg( NULL, " Unknown delivery system %u", p_systems[i]);
break;
}
}
}
/*****************************************************************************
* FrontendSet
*****************************************************************************/
/* S2API */
#if DVBAPI_VERSION >= 505
static struct dtv_property info_cmdargs[] = {
{ .cmd = DTV_API_VERSION, .u.data = 0 },
};
static struct dtv_properties info_cmdseq = {
.num = sizeof(info_cmdargs)/sizeof(struct dtv_property),
.props = info_cmdargs
};
static struct dtv_property enum_cmdargs[] = {
{ .cmd = DTV_ENUM_DELSYS, .u.data = 0 },
};
static struct dtv_properties enum_cmdseq = {
.num = sizeof(enum_cmdargs)/sizeof(struct dtv_property),
.props = enum_cmdargs
};
#endif
static struct dtv_property dvbs_cmdargs[] = {
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBS },
{ .cmd = DTV_FREQUENCY, .u.data = 0 },
{ .cmd = DTV_MODULATION, .u.data = QPSK },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
{ .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
{ .cmd = DTV_INNER_FEC, .u.data = FEC_AUTO },
{ .cmd = DTV_TUNE },
};
static struct dtv_properties dvbs_cmdseq = {
.num = sizeof(dvbs_cmdargs)/sizeof(struct dtv_property),
.props = dvbs_cmdargs
};
#define IDX_DVBS2_PILOT 6
#define IDX_DVBS2_ROLLOFF 7
#define IDX_DVBS2_STREAM_ID 8
/* Commands 0..5 are the same as dvbs_cmdargs */
/* Commands 6..8 are special for DVB-S2 */
static struct dtv_property dvbs2_cmdargs[] = {
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBS2 },
{ .cmd = DTV_FREQUENCY, .u.data = 0 },
{ .cmd = DTV_MODULATION, .u.data = PSK_8 },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
{ .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
{ .cmd = DTV_INNER_FEC, .u.data = FEC_AUTO },
{ .cmd = DTV_PILOT, .u.data = PILOT_AUTO }, /* idx: 6 */
{ .cmd = DTV_ROLLOFF, .u.data = ROLLOFF_AUTO }, /* idx: 7 */
{ .cmd = DTV_STREAM_ID, .u.data = 0 }, /* idx: 8 */
{ .cmd = DTV_TUNE },
};
static struct dtv_properties dvbs2_cmdseq = {
.num = sizeof(dvbs2_cmdargs)/sizeof(struct dtv_property),
.props = dvbs2_cmdargs
};
static struct dtv_property dvbc_cmdargs[] = {
#if DVBAPI_VERSION >= 505
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBC_ANNEX_A },
#else
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBC_ANNEX_AC },
#endif
{ .cmd = DTV_FREQUENCY, .u.data = 0 },
{ .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
{ .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
{ .cmd = DTV_TUNE },
};
static struct dtv_properties dvbc_cmdseq = {
.num = sizeof(dvbc_cmdargs)/sizeof(struct dtv_property),
.props = dvbc_cmdargs
};
static struct dtv_property dvbt_cmdargs[] = {
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBT },
{ .cmd = DTV_FREQUENCY, .u.data = 0 },
{ .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
{ .cmd = DTV_BANDWIDTH_HZ, .u.data = 8000000 },
{ .cmd = DTV_CODE_RATE_HP, .u.data = FEC_AUTO },
{ .cmd = DTV_CODE_RATE_LP, .u.data = FEC_AUTO },
{ .cmd = DTV_GUARD_INTERVAL, .u.data = GUARD_INTERVAL_AUTO },
{ .cmd = DTV_TRANSMISSION_MODE,.u.data = TRANSMISSION_MODE_AUTO },
{ .cmd = DTV_HIERARCHY, .u.data = HIERARCHY_AUTO },
{ .cmd = DTV_TUNE },
};
static struct dtv_property dvbt2_cmdargs[] = {
{ .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBT2 },
{ .cmd = DTV_FREQUENCY, .u.data = 0 },
{ .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
{ .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },