util: fix potential buffer overflow
[dvblast.git] / dvb.c
blob106a942809d116a4c2572364f50259083358c0fe
1 /*****************************************************************************
2 * dvb.c: linux-dvb input for DVBlast
3 *****************************************************************************
4 * Copyright (C) 2008-2010, 2015 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
22 #include "config.h"
24 #ifdef HAVE_DVB_SUPPORT
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <poll.h>
42 #include <errno.h>
44 /* DVB Card Drivers */
45 #include <linux/dvb/version.h>
46 #include <linux/dvb/dmx.h>
47 #include <linux/dvb/frontend.h>
48 #include <linux/dvb/ca.h>
50 #include <ev.h>
52 #if DVBAPI_VERSION < 508
53 #define DTV_STREAM_ID 42
54 #define FE_CAN_MULTISTREAM 0x4000000
55 #define FE_CAN_TURBO_FEC 0x8000000
56 #endif
58 #define MAX_DELIVERY_SYSTEMS 20
60 #include "dvblast.h"
61 #include "en50221.h"
62 #include "comm.h"
64 #include <bitstream/common.h>
66 /*****************************************************************************
67 * Local declarations
68 *****************************************************************************/
69 #define DVR_READ_TIMEOUT 30000000 /* 30 s */
70 #define MAX_READ_ONCE 50
71 #define DVR_BUFFER_SIZE 40*188*1024 /* bytes */
73 int i_dvr_buffer_size = DVR_BUFFER_SIZE;
75 static int i_frontend, i_dvr;
76 static struct ev_io frontend_watcher, dvr_watcher;
77 static struct ev_timer lock_watcher, mute_watcher, print_watcher;
78 static fe_status_t i_last_status;
79 static block_t *p_freelist = NULL;
81 /*****************************************************************************
82 * Local prototypes
83 *****************************************************************************/
84 static void DVRRead(struct ev_loop *loop, struct ev_io *w, int revents);
85 static void DVRMuteCb(struct ev_loop *loop, struct ev_timer *w, int revents);
86 static void FrontendRead(struct ev_loop *loop, struct ev_io *w, int revents);
87 static void FrontendLockCb(struct ev_loop *loop, struct ev_timer *w, int revents);
88 static void FrontendSet( bool b_reset );
90 /*****************************************************************************
91 * dvb_Open
92 *****************************************************************************/
93 void dvb_Open( void )
95 char psz_tmp[128];
97 msg_Dbg( NULL, "compiled with DVB API version %d.%d", DVB_API_VERSION, DVB_API_VERSION_MINOR );
99 if ( i_frequency )
101 sprintf( psz_tmp, "/dev/dvb/adapter%d/frontend%d", i_adapter, i_fenum );
102 if( (i_frontend = open(psz_tmp, O_RDWR | O_NONBLOCK)) < 0 )
104 msg_Err( NULL, "opening device %s failed (%s)", psz_tmp,
105 strerror(errno) );
106 exit(1);
109 FrontendSet(true);
111 else
113 i_frontend = -1;
116 sprintf( psz_tmp, "/dev/dvb/adapter%d/dvr%d", i_adapter, i_fenum );
118 if( (i_dvr = open(psz_tmp, O_RDONLY | O_NONBLOCK)) < 0 )
120 msg_Err( NULL, "opening device %s failed (%s)", psz_tmp,
121 strerror(errno) );
122 exit(1);
125 if ( ioctl( i_dvr, DMX_SET_BUFFER_SIZE, i_dvr_buffer_size ) < 0 )
127 msg_Warn( NULL, "couldn't set %s buffer size (%s)", psz_tmp,
128 strerror(errno) );
131 ev_io_init(&dvr_watcher, DVRRead, i_dvr, EV_READ);
132 ev_io_start(event_loop, &dvr_watcher);
134 if ( i_frontend != -1 )
136 ev_io_init(&frontend_watcher, FrontendRead, i_frontend, EV_READ);
137 ev_io_start(event_loop, &frontend_watcher);
140 ev_timer_init(&lock_watcher, FrontendLockCb,
141 i_frontend_timeout_duration / 1000000.,
142 i_frontend_timeout_duration / 1000000.);
143 ev_timer_init(&mute_watcher, DVRMuteCb,
144 DVR_READ_TIMEOUT / 1000000.,
145 DVR_READ_TIMEOUT / 1000000.);
147 en50221_Init();
150 /*****************************************************************************
151 * dvb_Reset
152 *****************************************************************************/
153 void dvb_Reset( void )
155 if ( i_frequency )
156 FrontendSet(true);
159 /*****************************************************************************
160 * DVR events
161 *****************************************************************************/
162 static void DVRRead(struct ev_loop *loop, struct ev_io *w, int revents)
164 int i, i_len;
165 block_t *p_ts = p_freelist, **pp_current = &p_ts;
166 struct iovec p_iov[MAX_READ_ONCE];
168 for ( i = 0; i < MAX_READ_ONCE; i++ )
170 if ( (*pp_current) == NULL ) *pp_current = block_New();
171 p_iov[i].iov_base = (*pp_current)->p_ts;
172 p_iov[i].iov_len = TS_SIZE;
173 pp_current = &(*pp_current)->p_next;
176 if ( (i_len = readv(i_dvr, p_iov, MAX_READ_ONCE)) < 0 )
178 msg_Err( NULL, "couldn't read from DVR device (%s)",
179 strerror(errno) );
180 i_len = 0;
182 i_len /= TS_SIZE;
184 if ( i_len )
185 ev_timer_again(loop, &mute_watcher);
187 pp_current = &p_ts;
188 while ( i_len && *pp_current )
190 pp_current = &(*pp_current)->p_next;
191 i_len--;
194 p_freelist = *pp_current;
195 *pp_current = NULL;
197 demux_Run( p_ts );
200 static void DVRMuteCb(struct ev_loop *loop, struct ev_timer *w, int revents)
202 msg_Warn( NULL, "no DVR output, resetting" );
203 ev_timer_stop(loop, w);
205 switch (i_print_type) {
206 case PRINT_XML:
207 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"dvr\" />\n");
208 break;
209 case PRINT_TEXT:
210 fprintf(print_fh, "reset cause: dvr\n");
211 break;
212 default:
213 break;
215 if ( i_frequency )
216 FrontendSet(false);
217 en50221_Reset();
222 * Demux
225 /*****************************************************************************
226 * dvb_SetFilter : controls the demux to add a filter
227 *****************************************************************************/
228 int dvb_SetFilter( uint16_t i_pid )
230 struct dmx_pes_filter_params s_filter_params;
231 char psz_tmp[128];
232 int i_fd;
234 sprintf( psz_tmp, "/dev/dvb/adapter%d/demux%d", i_adapter, i_fenum );
235 if( (i_fd = open(psz_tmp, O_RDWR)) < 0 )
237 msg_Err( NULL, "DMXSetFilter: opening device failed (%s)",
238 strerror(errno) );
239 return -1;
242 s_filter_params.pid = i_pid;
243 s_filter_params.input = DMX_IN_FRONTEND;
244 s_filter_params.output = DMX_OUT_TS_TAP;
245 s_filter_params.flags = DMX_IMMEDIATE_START;
246 s_filter_params.pes_type = DMX_PES_OTHER;
248 if ( ioctl( i_fd, DMX_SET_PES_FILTER, &s_filter_params ) < 0 )
250 msg_Err( NULL, "failed setting filter on %d (%s)", i_pid,
251 strerror(errno) );
252 close( i_fd );
253 return -1;
256 msg_Dbg( NULL, "setting filter on PID %d", i_pid );
258 return i_fd;
261 /*****************************************************************************
262 * dvb_UnsetFilter : removes a filter
263 *****************************************************************************/
264 void dvb_UnsetFilter( int i_fd, uint16_t i_pid )
266 if ( ioctl( i_fd, DMX_STOP ) < 0 )
267 msg_Err( NULL, "DMX_STOP failed (%s)", strerror(errno) );
268 else
269 msg_Dbg( NULL, "unsetting filter on PID %d", i_pid );
271 close( i_fd );
276 * Frontend
279 /*****************************************************************************
280 * Print info
281 *****************************************************************************/
282 static void PrintCb( struct ev_loop *loop, struct ev_timer *w, int revents )
284 uint32_t i_ber = 0;
285 uint16_t i_strength = 0, i_snr = 0;
286 uint32_t i_uncorrected = 0;
288 ioctl(i_frontend, FE_READ_BER, &i_ber);
289 ioctl(i_frontend, FE_READ_SIGNAL_STRENGTH, &i_strength);
290 ioctl(i_frontend, FE_READ_SNR, &i_snr);
291 ioctl(i_frontend, FE_READ_UNCORRECTED_BLOCKS, &i_uncorrected);
293 switch (i_print_type)
295 case PRINT_XML:
296 fprintf(print_fh,
297 "<STATUS type=\"frontend\" ber=\"%"PRIu32"\" strength=\"%"PRIu16"\" snr=\"%"PRIu16"\" uncorrected=\"%"PRIu32"\" />\n",
298 i_ber, i_strength, i_snr, i_uncorrected);
299 break;
300 case PRINT_TEXT:
301 fprintf(print_fh, "frontend ber: %"PRIu32" strength: %"PRIu16" snr: %"PRIu16" uncorrected: %"PRIu32"\n",
302 i_ber, i_strength, i_snr, i_uncorrected);
303 break;
304 default:
305 break;
309 /*****************************************************************************
310 * Frontend events
311 *****************************************************************************/
312 static void FrontendRead(struct ev_loop *loop, struct ev_io *w, int revents)
314 struct dvb_frontend_event event;
315 fe_status_t i_status, i_diff;
317 for( ;; )
319 int i_ret = ioctl( i_frontend, FE_GET_EVENT, &event );
321 if( i_ret < 0 )
323 if( errno == EWOULDBLOCK )
324 return; /* no more events */
326 msg_Err( NULL, "reading frontend event failed (%d) %s",
327 i_ret, strerror(errno) );
328 return;
331 i_status = event.status;
332 i_diff = i_status ^ i_last_status;
333 i_last_status = i_status;
336 #define IF_UP( x ) \
338 if ( i_diff & (x) ) \
340 if ( i_status & (x) )
342 IF_UP( FE_HAS_SIGNAL )
343 msg_Dbg( NULL, "frontend has acquired signal" );
344 else
345 msg_Dbg( NULL, "frontend has lost signal" );
347 IF_UP( FE_HAS_CARRIER )
348 msg_Dbg( NULL, "frontend has acquired carrier" );
349 else
350 msg_Dbg( NULL, "frontend has lost carrier" );
352 IF_UP( FE_HAS_VITERBI )
353 msg_Dbg( NULL, "frontend has acquired stable FEC" );
354 else
355 msg_Dbg( NULL, "frontend has lost FEC" );
357 IF_UP( FE_HAS_SYNC )
358 msg_Dbg( NULL, "frontend has acquired sync" );
359 else
360 msg_Dbg( NULL, "frontend has lost sync" );
362 IF_UP( FE_HAS_LOCK )
364 int32_t i_value = 0;
365 msg_Info( NULL, "frontend has acquired lock" );
366 switch (i_print_type) {
367 case PRINT_XML:
368 fprintf(print_fh, "<STATUS type=\"lock\" status=\"1\" />\n");
369 break;
370 case PRINT_TEXT:
371 fprintf(print_fh, "lock status: 1\n");
372 break;
373 default:
374 break;
377 ev_timer_stop(loop, &lock_watcher);
378 ev_timer_again(loop, &mute_watcher);
380 /* Read some statistics */
381 if( ioctl( i_frontend, FE_READ_BER, &i_value ) >= 0 )
382 msg_Dbg( NULL, "- Bit error rate: %d", i_value );
383 if( ioctl( i_frontend, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
384 msg_Dbg( NULL, "- Signal strength: %d", i_value );
385 if( ioctl( i_frontend, FE_READ_SNR, &i_value ) >= 0 )
386 msg_Dbg( NULL, "- SNR: %d", i_value );
388 if (i_print_period)
390 ev_timer_init( &print_watcher, PrintCb,
391 i_print_period / 1000000.,
392 i_print_period / 1000000. );
393 ev_timer_start( event_loop, &print_watcher );
396 else
398 msg_Dbg( NULL, "frontend has lost lock" );
399 switch (i_print_type) {
400 case PRINT_XML:
401 fprintf(print_fh, "<STATUS type=\"lock\" status=\"0\"/>\n");
402 break;
403 case PRINT_TEXT:
404 fprintf(print_fh, "lock status: 0\n");
405 break;
406 default:
407 break;
410 if (i_frontend_timeout_duration)
412 ev_timer_stop(event_loop, &lock_watcher);
413 ev_timer_again(loop, &mute_watcher);
416 if (i_print_period)
417 ev_timer_stop(event_loop, &print_watcher);
420 IF_UP( FE_REINIT )
422 /* The frontend was reinited. */
423 msg_Warn( NULL, "reiniting frontend");
424 if ( i_frequency )
425 FrontendSet(true);
428 #undef IF_UP
432 static void FrontendLockCb(struct ev_loop *loop, struct ev_timer *w, int revents)
434 if ( i_quit_timeout_duration )
436 msg_Err( NULL, "no lock" );
437 ev_break(loop, EVBREAK_ALL);
438 return;
441 msg_Warn( NULL, "no lock, tuning again" );
442 ev_timer_stop(loop, w);
444 switch (i_print_type) {
445 case PRINT_XML:
446 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"nolock\" />\n");
447 break;
448 case PRINT_TEXT:
449 fprintf(print_fh, "reset cause: nolock\n");
450 break;
451 default:
452 break;
454 if ( i_frequency )
455 FrontendSet(false);
458 static int FrontendDoDiseqc(void)
460 fe_sec_voltage_t fe_voltage;
461 fe_sec_tone_mode_t fe_tone;
462 int bis_frequency;
464 switch ( i_voltage )
466 case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
467 default:
468 case 13: fe_voltage = SEC_VOLTAGE_13; break;
469 case 18: fe_voltage = SEC_VOLTAGE_18; break;
472 fe_tone = b_tone ? SEC_TONE_ON : SEC_TONE_OFF;
474 if ( strcmp( psz_lnb_type, "universal" ) == 0 )
476 /* Automatic mode. */
477 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
479 msg_Dbg( NULL, "frequency %d is in IF-band", i_frequency );
480 bis_frequency = i_frequency;
482 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
484 msg_Dbg( NULL, "frequency %d is in S-band", i_frequency );
485 bis_frequency = 3650000 - i_frequency;
487 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
489 msg_Dbg( NULL, "frequency %d is in C-band (lower)", i_frequency );
490 bis_frequency = 5150000 - i_frequency;
492 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
494 msg_Dbg( NULL, "frequency %d is in C-band (higher)", i_frequency );
495 bis_frequency = 5950000 - i_frequency;
497 else if ( i_frequency >= 10700000 && i_frequency < 11700000 )
499 msg_Dbg( NULL, "frequency %d is in Ku-band (lower)",
500 i_frequency );
501 bis_frequency = i_frequency - 9750000;
503 else if ( i_frequency >= 11700000 && i_frequency <= 13250000 )
505 msg_Dbg( NULL, "frequency %d is in Ku-band (higher)",
506 i_frequency );
507 bis_frequency = i_frequency - 10600000;
508 fe_tone = SEC_TONE_ON;
510 else
512 msg_Err( NULL, "frequency %d is out of any known band",
513 i_frequency );
514 exit(1);
517 else if ( strcmp( psz_lnb_type, "old-sky" ) == 0 )
519 if ( i_frequency >= 11700000 && i_frequency <= 13250000 )
521 msg_Dbg( NULL, "frequency %d is in Ku-band (higher)",
522 i_frequency );
523 bis_frequency = i_frequency - 11300000;
524 fe_tone = SEC_TONE_ON;
526 else
528 msg_Err( NULL, "frequency %d is out of any known band",
529 i_frequency );
530 exit(1);
533 else
535 msg_Err( NULL, "lnb-type '%s' is not known. Valid type: universal old-sky",
536 psz_lnb_type );
537 exit(1);
540 /* Switch off continuous tone. */
541 if ( ioctl( i_frontend, FE_SET_TONE, SEC_TONE_OFF ) < 0 )
543 msg_Err( NULL, "FE_SET_TONE failed (%s)", strerror(errno) );
544 exit(1);
547 /* Configure LNB voltage. */
548 if ( ioctl( i_frontend, FE_SET_VOLTAGE, fe_voltage ) < 0 )
550 msg_Err( NULL, "FE_SET_VOLTAGE failed (%s)", strerror(errno) );
551 exit(1);
554 /* Wait for at least 15 ms. Currently 100 ms because of broken drivers. */
555 msleep(100000);
557 /* Diseqc */
558 if ( i_satnum > 0 && i_satnum < 5 )
560 /* digital satellite equipment control,
561 * specification is available from http://www.eutelsat.com/
564 /* DiSEqC 1.1 */
565 struct dvb_diseqc_master_cmd uncmd =
566 { {0xe0, 0x10, 0x39, 0xf0, 0x00, 0x00}, 4};
568 /* DiSEqC 1.0 */
569 struct dvb_diseqc_master_cmd cmd =
570 { {0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4};
572 cmd.msg[3] = 0xf0 /* reset bits */
573 | ((i_satnum - 1) << 2)
574 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
575 | (fe_tone == SEC_TONE_ON ? 1 : 0);
577 if ( i_uncommitted > 0 && i_uncommitted < 17 )
579 uncmd.msg[3] = 0xf0 /* reset bits */
580 | (i_uncommitted - 1);
581 if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &uncmd ) < 0 )
583 msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
584 strerror(errno) );
585 exit(1);
587 /* Repeat uncommitted command */
588 uncmd.msg[0] = 0xe1; /* framing: master, no reply, repeated TX */
589 if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &uncmd ) < 0 )
591 msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
592 strerror(errno) );
593 exit(1);
595 /* Pause 125 ms between uncommitted & committed diseqc commands. */
596 msleep(125000);
599 if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd ) < 0 )
601 msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
602 strerror(errno) );
603 exit(1);
605 msleep(100000); /* Should be 15 ms. */
607 /* Do it again just to be sure. */
608 cmd.msg[0] = 0xe1; /* framing: master, no reply, repeated TX */
609 if( ioctl( i_frontend, FE_DISEQC_SEND_MASTER_CMD, &cmd ) < 0 )
611 msg_Err( NULL, "ioctl FE_SEND_MASTER_CMD failed (%s)",
612 strerror(errno) );
613 exit(1);
615 msleep(100000); /* Again, should be 15 ms */
617 else if ( i_satnum == 0xA || i_satnum == 0xB )
619 /* A or B simple diseqc ("diseqc-compatible") */
620 if( ioctl( i_frontend, FE_DISEQC_SEND_BURST,
621 i_satnum == 0xB ? SEC_MINI_B : SEC_MINI_A ) < 0 )
623 msg_Err( NULL, "ioctl FE_SEND_BURST failed (%s)", strerror(errno) );
624 exit(1);
626 msleep(100000); /* ... */
629 if ( ioctl( i_frontend, FE_SET_TONE, fe_tone ) < 0 )
631 msg_Err( NULL, "FE_SET_TONE failed (%s)", strerror(errno) );
632 exit(1);
635 msleep(100000); /* ... */
637 msg_Dbg( NULL, "configuring LNB to v=%d p=%d satnum=%x uncommitted=%x lnb-type=%s bis_frequency=%d",
638 i_voltage, b_tone, i_satnum, i_uncommitted, psz_lnb_type, bis_frequency );
639 return bis_frequency;
642 #if DVB_API_VERSION >= 5
644 #if DVBAPI_VERSION < 505
645 #warning Your linux-dvb headers are old, you should consider upgrading your kernel and/or compiling against different kernel headers
646 #endif
648 /*****************************************************************************
649 * Helper functions for S2API
650 *****************************************************************************/
651 static fe_spectral_inversion_t GetInversion(void)
653 switch ( i_inversion )
655 case 0: return INVERSION_OFF;
656 case 1: return INVERSION_ON;
657 default:
658 msg_Warn( NULL, "invalid inversion %d", i_inversion );
659 case -1: return INVERSION_AUTO;
663 static fe_code_rate_t GetFEC(fe_caps_t fe_caps, int i_fec_value)
665 #define GET_FEC_INNER(fec, val) \
666 if ( (fe_caps & FE_CAN_##fec) && (i_fec_value == val) ) \
667 return fec;
669 GET_FEC_INNER(FEC_AUTO, 999);
670 GET_FEC_INNER(FEC_AUTO, -1);
671 if (i_fec_value == 0)
672 return FEC_NONE;
673 GET_FEC_INNER(FEC_1_2, 12);
674 GET_FEC_INNER(FEC_2_3, 23);
675 GET_FEC_INNER(FEC_3_4, 34);
676 if (i_fec_value == 35)
677 return FEC_3_5;
678 GET_FEC_INNER(FEC_4_5, 45);
679 GET_FEC_INNER(FEC_5_6, 56);
680 GET_FEC_INNER(FEC_6_7, 67);
681 GET_FEC_INNER(FEC_7_8, 78);
682 GET_FEC_INNER(FEC_8_9, 89);
683 if (i_fec_value == 910)
684 return FEC_9_10;
686 #undef GET_FEC_INNER
687 msg_Warn(NULL, "invalid FEC %d", i_fec_value );
688 return FEC_AUTO;
691 #define GetFECInner(caps) GetFEC(caps, i_fec)
692 #define GetFECLP(caps) GetFEC(caps, i_fec_lp)
694 static fe_modulation_t GetModulation(void)
696 #define GET_MODULATION( mod ) \
697 if ( !strcasecmp( psz_modulation, #mod ) ) \
698 return mod;
700 GET_MODULATION(QPSK);
701 GET_MODULATION(QAM_16);
702 GET_MODULATION(QAM_32);
703 GET_MODULATION(QAM_64);
704 GET_MODULATION(QAM_128);
705 GET_MODULATION(QAM_256);
706 GET_MODULATION(QAM_AUTO);
707 GET_MODULATION(VSB_8);
708 GET_MODULATION(VSB_16);
709 GET_MODULATION(PSK_8);
710 GET_MODULATION(APSK_16);
711 GET_MODULATION(APSK_32);
712 GET_MODULATION(DQPSK);
714 #undef GET_MODULATION
715 msg_Err( NULL, "invalid modulation %s", psz_modulation );
716 exit(1);
719 static fe_pilot_t GetPilot(void)
721 switch ( i_pilot )
723 case 0: return PILOT_OFF;
724 case 1: return PILOT_ON;
725 default:
726 msg_Warn( NULL, "invalid pilot %d", i_pilot );
727 case -1: return PILOT_AUTO;
731 static fe_rolloff_t GetRollOff(void)
733 switch ( i_rolloff )
735 case -1:
736 case 0: return ROLLOFF_AUTO;
737 case 20: return ROLLOFF_20;
738 case 25: return ROLLOFF_25;
739 default:
740 msg_Warn( NULL, "invalid rolloff %d", i_rolloff );
741 case 35: return ROLLOFF_35;
745 static fe_guard_interval_t GetGuard(void)
747 switch ( i_guard )
749 case 32: return GUARD_INTERVAL_1_32;
750 case 16: return GUARD_INTERVAL_1_16;
751 case 8: return GUARD_INTERVAL_1_8;
752 case 4: return GUARD_INTERVAL_1_4;
753 default:
754 msg_Warn( NULL, "invalid guard interval %d", i_guard );
755 case -1:
756 case 0: return GUARD_INTERVAL_AUTO;
760 static fe_transmit_mode_t GetTransmission(void)
762 switch ( i_transmission )
764 case 2: return TRANSMISSION_MODE_2K;
765 case 8: return TRANSMISSION_MODE_8K;
766 #ifdef TRANSMISSION_MODE_4K
767 case 4: return TRANSMISSION_MODE_4K;
768 #endif
769 default:
770 msg_Warn( NULL, "invalid tranmission mode %d", i_transmission );
771 case -1:
772 case 0: return TRANSMISSION_MODE_AUTO;
776 static fe_hierarchy_t GetHierarchy(void)
778 switch ( i_hierarchy )
780 case 0: return HIERARCHY_NONE;
781 case 1: return HIERARCHY_1;
782 case 2: return HIERARCHY_2;
783 case 4: return HIERARCHY_4;
784 default:
785 msg_Warn( NULL, "invalid intramission mode %d", i_transmission );
786 case -1: return HIERARCHY_AUTO;
790 /*****************************************************************************
791 * FrontendInfo : Print frontend info
792 *****************************************************************************/
793 static void FrontendInfo( struct dvb_frontend_info *info, uint32_t version,
794 fe_delivery_system_t *p_systems, int i_systems )
796 msg_Dbg( NULL, "using DVB API version %d.%d", version / 256, version % 256 );
797 msg_Dbg( NULL, "Frontend \"%s\" supports:", info->name );
798 msg_Dbg( NULL, " frequency min: %d, max: %d, stepsize: %d, tolerance: %d",
799 info->frequency_min, info->frequency_max,
800 info->frequency_stepsize, info->frequency_tolerance );
801 msg_Dbg( NULL, " symbolrate min: %d, max: %d, tolerance: %d",
802 info->symbol_rate_min, info->symbol_rate_max, info->symbol_rate_tolerance);
803 msg_Dbg( NULL, " capabilities:" );
805 #define FRONTEND_INFO(caps,val,msg) \
806 if ( caps & val ) \
807 msg_Dbg( NULL, " %s", msg );
809 FRONTEND_INFO( info->caps, FE_IS_STUPID, "FE_IS_STUPID" )
810 FRONTEND_INFO( info->caps, FE_CAN_INVERSION_AUTO, "INVERSION_AUTO" )
811 FRONTEND_INFO( info->caps, FE_CAN_FEC_1_2, "FEC_1_2" )
812 FRONTEND_INFO( info->caps, FE_CAN_FEC_2_3, "FEC_2_3" )
813 FRONTEND_INFO( info->caps, FE_CAN_FEC_3_4, "FEC_3_4" )
814 FRONTEND_INFO( info->caps, FE_CAN_FEC_4_5, "FEC_4_5" )
815 FRONTEND_INFO( info->caps, FE_CAN_FEC_5_6, "FEC_5_6" )
816 FRONTEND_INFO( info->caps, FE_CAN_FEC_6_7, "FEC_6_7" )
817 FRONTEND_INFO( info->caps, FE_CAN_FEC_7_8, "FEC_7_8" )
818 FRONTEND_INFO( info->caps, FE_CAN_FEC_8_9, "FEC_8_9" )
819 FRONTEND_INFO( info->caps, FE_CAN_FEC_AUTO,"FEC_AUTO")
820 FRONTEND_INFO( info->caps, FE_CAN_QPSK, "QPSK" )
821 FRONTEND_INFO( info->caps, FE_CAN_QAM_16, "QAM_16" )
822 FRONTEND_INFO( info->caps, FE_CAN_QAM_32, "QAM_32" )
823 FRONTEND_INFO( info->caps, FE_CAN_QAM_64, "QAM_64" )
824 FRONTEND_INFO( info->caps, FE_CAN_QAM_128,"QAM_128")
825 FRONTEND_INFO( info->caps, FE_CAN_QAM_256,"QAM_256")
826 FRONTEND_INFO( info->caps, FE_CAN_QAM_AUTO,"QAM_AUTO" )
827 FRONTEND_INFO( info->caps, FE_CAN_TRANSMISSION_MODE_AUTO, "TRANSMISSION_MODE_AUTO" )
828 FRONTEND_INFO( info->caps, FE_CAN_BANDWIDTH_AUTO, "BANDWIDTH_AUTO" )
829 FRONTEND_INFO( info->caps, FE_CAN_GUARD_INTERVAL_AUTO, "GUARD_INTERVAL_AUTO" )
830 FRONTEND_INFO( info->caps, FE_CAN_HIERARCHY_AUTO, "HIERARCHY_AUTO" )
831 FRONTEND_INFO( info->caps, FE_CAN_8VSB, "8VSB" )
832 FRONTEND_INFO( info->caps, FE_CAN_16VSB,"16VSB" )
833 FRONTEND_INFO( info->caps, FE_HAS_EXTENDED_CAPS, "EXTENDED_CAPS" )
834 #if DVBAPI_VERSION >= 501
835 FRONTEND_INFO( info->caps, FE_CAN_2G_MODULATION, "2G_MODULATION" )
836 #endif
837 FRONTEND_INFO( info->caps, FE_CAN_MULTISTREAM, "MULTISTREAM" )
838 FRONTEND_INFO( info->caps, FE_CAN_TURBO_FEC, "TURBO_FEC" )
839 FRONTEND_INFO( info->caps, FE_NEEDS_BENDING, "NEEDS_BENDING" )
840 FRONTEND_INFO( info->caps, FE_CAN_RECOVER, "FE_CAN_RECOVER" )
841 FRONTEND_INFO( info->caps, FE_CAN_MUTE_TS, "FE_CAN_MUTE_TS" )
842 #undef FRONTEND_INFO
844 msg_Dbg( NULL, " delivery systems:" );
845 int i;
846 for ( i = 0; i < i_systems; i++ )
848 switch ( p_systems[i] )
850 #define DELSYS_INFO(delsys, msg) \
851 case delsys: msg_Dbg( NULL, " %s", msg); break;
852 DELSYS_INFO( SYS_ATSC, "ATSC" )
853 DELSYS_INFO( SYS_ATSCMH, "ATSCMH" )
854 DELSYS_INFO( SYS_CMMB, "CMBB" )
855 DELSYS_INFO( SYS_DAB, "DAB" )
856 DELSYS_INFO( SYS_DSS, "DSS" )
857 DELSYS_INFO( SYS_DVBC_ANNEX_B, "DVBC_ANNEX_B" )
858 DELSYS_INFO( SYS_DVBH, "DVBH" )
859 DELSYS_INFO( SYS_DVBS, "DVBS" )
860 DELSYS_INFO( SYS_DVBS2, "DVBS2" )
861 DELSYS_INFO( SYS_DVBT, "DVBT" )
862 DELSYS_INFO( SYS_ISDBC, "ISDBC" )
863 DELSYS_INFO( SYS_ISDBS, "ISDBS" )
864 DELSYS_INFO( SYS_ISDBT, "ISDBT" )
865 DELSYS_INFO( SYS_UNDEFINED, "UNDEFINED" )
866 #if DVBAPI_VERSION >= 505
867 DELSYS_INFO( SYS_DVBC_ANNEX_A, "DVBC_ANNEX_A" )
868 DELSYS_INFO( SYS_DVBC_ANNEX_C, "DVBC_ANNEX_C" )
869 DELSYS_INFO( SYS_DVBT2, "DVBT2" )
870 DELSYS_INFO( SYS_TURBO, "TURBO" )
871 #else
872 DELSYS_INFO( SYS_DVBC_ANNEX_AC, "DVBC_ANNEX_AC" )
873 #endif
874 #if DVBAPI_VERSION >= 507
875 DELSYS_INFO( SYS_DTMB, "DTMB" )
876 #else
877 DELSYS_INFO( SYS_DMBTH, "DMBTH" )
878 #endif
879 default: msg_Dbg( NULL, " Unknown delivery system %u", p_systems[i]);
880 break;
885 /*****************************************************************************
886 * FrontendSet
887 *****************************************************************************/
888 /* S2API */
889 #if DVBAPI_VERSION >= 505
890 static struct dtv_property info_cmdargs[] = {
891 { .cmd = DTV_API_VERSION, .u.data = 0 },
893 static struct dtv_properties info_cmdseq = {
894 .num = sizeof(info_cmdargs)/sizeof(struct dtv_property),
895 .props = info_cmdargs
898 static struct dtv_property enum_cmdargs[] = {
899 { .cmd = DTV_ENUM_DELSYS, .u.data = 0 },
901 static struct dtv_properties enum_cmdseq = {
902 .num = sizeof(enum_cmdargs)/sizeof(struct dtv_property),
903 .props = enum_cmdargs
905 #endif
907 static struct dtv_property dvbs_cmdargs[] = {
908 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBS },
909 { .cmd = DTV_FREQUENCY, .u.data = 0 },
910 { .cmd = DTV_MODULATION, .u.data = QPSK },
911 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
912 { .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
913 { .cmd = DTV_INNER_FEC, .u.data = FEC_AUTO },
914 { .cmd = DTV_TUNE },
916 static struct dtv_properties dvbs_cmdseq = {
917 .num = sizeof(dvbs_cmdargs)/sizeof(struct dtv_property),
918 .props = dvbs_cmdargs
921 #define IDX_DVBS2_PILOT 6
922 #define IDX_DVBS2_ROLLOFF 7
923 #define IDX_DVBS2_STREAM_ID 8
925 /* Commands 0..5 are the same as dvbs_cmdargs */
926 /* Commands 6..8 are special for DVB-S2 */
927 static struct dtv_property dvbs2_cmdargs[] = {
928 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBS2 },
929 { .cmd = DTV_FREQUENCY, .u.data = 0 },
930 { .cmd = DTV_MODULATION, .u.data = PSK_8 },
931 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
932 { .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
933 { .cmd = DTV_INNER_FEC, .u.data = FEC_AUTO },
934 { .cmd = DTV_PILOT, .u.data = PILOT_AUTO }, /* idx: 6 */
935 { .cmd = DTV_ROLLOFF, .u.data = ROLLOFF_AUTO }, /* idx: 7 */
936 { .cmd = DTV_STREAM_ID, .u.data = 0 }, /* idx: 8 */
937 { .cmd = DTV_TUNE },
939 static struct dtv_properties dvbs2_cmdseq = {
940 .num = sizeof(dvbs2_cmdargs)/sizeof(struct dtv_property),
941 .props = dvbs2_cmdargs
944 static struct dtv_property dvbc_cmdargs[] = {
945 #if DVBAPI_VERSION >= 505
946 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBC_ANNEX_A },
947 #else
948 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBC_ANNEX_AC },
949 #endif
950 { .cmd = DTV_FREQUENCY, .u.data = 0 },
951 { .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
952 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
953 { .cmd = DTV_SYMBOL_RATE, .u.data = 27500000 },
954 { .cmd = DTV_TUNE },
956 static struct dtv_properties dvbc_cmdseq = {
957 .num = sizeof(dvbc_cmdargs)/sizeof(struct dtv_property),
958 .props = dvbc_cmdargs
961 static struct dtv_property dvbt_cmdargs[] = {
962 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBT },
963 { .cmd = DTV_FREQUENCY, .u.data = 0 },
964 { .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
965 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
966 { .cmd = DTV_BANDWIDTH_HZ, .u.data = 8000000 },
967 { .cmd = DTV_CODE_RATE_HP, .u.data = FEC_AUTO },
968 { .cmd = DTV_CODE_RATE_LP, .u.data = FEC_AUTO },
969 { .cmd = DTV_GUARD_INTERVAL, .u.data = GUARD_INTERVAL_AUTO },
970 { .cmd = DTV_TRANSMISSION_MODE,.u.data = TRANSMISSION_MODE_AUTO },
971 { .cmd = DTV_HIERARCHY, .u.data = HIERARCHY_AUTO },
972 { .cmd = DTV_TUNE },
975 static struct dtv_property dvbt2_cmdargs[] = {
976 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_DVBT2 },
977 { .cmd = DTV_FREQUENCY, .u.data = 0 },
978 { .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
979 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
980 { .cmd = DTV_BANDWIDTH_HZ, .u.data = 8000000 },
981 { .cmd = DTV_CODE_RATE_HP, .u.data = FEC_AUTO },
982 { .cmd = DTV_CODE_RATE_LP, .u.data = FEC_AUTO },
983 { .cmd = DTV_GUARD_INTERVAL, .u.data = GUARD_INTERVAL_AUTO },
984 { .cmd = DTV_TRANSMISSION_MODE,.u.data = TRANSMISSION_MODE_AUTO },
985 { .cmd = DTV_HIERARCHY, .u.data = HIERARCHY_AUTO },
986 { .cmd = DTV_STREAM_ID, .u.data = 0 },
987 { .cmd = DTV_TUNE },
990 static struct dtv_properties dvbt2_cmdseq = {
991 .num = sizeof(dvbt2_cmdargs)/sizeof(struct dtv_property),
992 .props = dvbt2_cmdargs
995 static struct dtv_properties dvbt_cmdseq = {
996 .num = sizeof(dvbt_cmdargs)/sizeof(struct dtv_property),
997 .props = dvbt_cmdargs
1000 /* ATSC + DVB-C annex B */
1001 static struct dtv_property atsc_cmdargs[] = {
1002 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_ATSC },
1003 { .cmd = DTV_FREQUENCY, .u.data = 0 },
1004 { .cmd = DTV_MODULATION, .u.data = QAM_AUTO },
1005 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
1006 { .cmd = DTV_TUNE },
1008 static struct dtv_properties atsc_cmdseq = {
1009 .num = sizeof(atsc_cmdargs)/sizeof(struct dtv_property),
1010 .props = atsc_cmdargs
1013 static struct dtv_property isdbt_cmdargs[] = {
1014 { .cmd = DTV_DELIVERY_SYSTEM, .u.data = SYS_ISDBT },
1015 { .cmd = DTV_FREQUENCY, .u.data = 0 },
1016 { .cmd = DTV_BANDWIDTH_HZ, .u.data = 6000000 },
1017 { .cmd = DTV_INVERSION, .u.data = INVERSION_AUTO },
1018 { .cmd = DTV_ISDBT_LAYERA_FEC, .u.data = FEC_AUTO },
1019 { .cmd = DTV_ISDBT_LAYERA_MODULATION, .u.data = QAM_AUTO },
1020 { .cmd = DTV_ISDBT_LAYERA_SEGMENT_COUNT, .u.data = 0 },
1021 { .cmd = DTV_ISDBT_LAYERA_TIME_INTERLEAVING,.u.data = 0 },
1022 { .cmd = DTV_ISDBT_LAYERB_FEC, .u.data = FEC_AUTO },
1023 { .cmd = DTV_ISDBT_LAYERB_MODULATION, .u.data = QAM_AUTO },
1024 { .cmd = DTV_ISDBT_LAYERB_SEGMENT_COUNT, .u.data = 0 },
1025 { .cmd = DTV_ISDBT_LAYERB_TIME_INTERLEAVING,.u.data = 0 },
1026 { .cmd = DTV_ISDBT_LAYERC_FEC, .u.data = FEC_AUTO },
1027 { .cmd = DTV_ISDBT_LAYERC_MODULATION, .u.data = QAM_AUTO },
1028 { .cmd = DTV_ISDBT_LAYERC_SEGMENT_COUNT, .u.data = 0 },
1029 { .cmd = DTV_ISDBT_LAYERC_TIME_INTERLEAVING,.u.data = 0 },
1030 { .cmd = DTV_TUNE },
1033 static struct dtv_properties isdbt_cmdseq = {
1034 .num = sizeof(isdbt_cmdargs)/sizeof(struct dtv_property),
1035 .props = isdbt_cmdargs
1038 #define DELSYS 0
1039 #define FREQUENCY 1
1040 #define MODULATION 2
1041 #define INVERSION 3
1042 #define SYMBOL_RATE 4
1043 #define BANDWIDTH 4
1044 #define FEC_INNER 5
1045 #define FEC_LP 6
1046 #define GUARD 7
1047 #define TRANSMISSION 8
1049 #define HIERARCHY 9
1050 #define PLP_ID 10
1052 //ISDBT
1053 #define ISDBT_BANDWIDTH 2
1054 #define ISDBT_LAYERA_FEC 4
1055 #define ISDBT_LAYERA_MODULATION 5
1056 #define ISDBT_LAYERA_SEGMENT_COUNT 6
1057 #define ISDBT_LAYERA_TIME_INTERLEAVING 7
1058 #define ISDBT_LAYERB_FEC 8
1059 #define ISDBT_LAYERB_MODULATION 9
1060 #define ISDBT_LAYERB_SEGMENT_COUNT 10
1061 #define ISDBT_LAYERB_TIME_INTERLEAVING 11
1062 #define ISDBT_LAYERC_FEC 12
1063 #define ISDBT_LAYERC_MODULATION 13
1064 #define ISDBT_LAYERC_SEGMENT_COUNT 14
1065 #define ISDBT_LAYERC_TIME_INTERLEAVING 15
1067 struct dtv_property pclear[] = {
1068 { .cmd = DTV_CLEAR },
1071 struct dtv_properties cmdclear = {
1072 .num = 1,
1073 .props = pclear
1076 static fe_delivery_system_t
1077 FrontendGuessSystem( fe_delivery_system_t *p_systems, int i_systems )
1079 if ( psz_delsys != NULL )
1081 if ( !strcasecmp( psz_delsys, "DVBS" ) )
1082 return SYS_DVBS;
1083 if ( !strcasecmp( psz_delsys, "DVBS2" ) )
1084 return SYS_DVBS2;
1085 if ( !strcasecmp( psz_delsys, "DVBC_ANNEX_A" ) )
1086 #if DVBAPI_VERSION >= 505
1087 return SYS_DVBC_ANNEX_A;
1088 #else
1089 return SYS_DVBC_ANNEX_AC;
1090 #endif
1091 if ( !strcasecmp( psz_delsys, "DVBC_ANNEX_B" ) )
1092 return SYS_DVBC_ANNEX_B;
1093 if ( !strcasecmp( psz_delsys, "DVBT" ) )
1094 return SYS_DVBT;
1095 if ( !strcasecmp( psz_delsys, "DVBT2" ) )
1096 return SYS_DVBT2;
1097 if ( !strcasecmp( psz_delsys, "ATSC" ) )
1098 return SYS_ATSC;
1099 if ( !strcasecmp( psz_delsys, "ISDBT" ) )
1100 return SYS_ISDBT;
1101 msg_Err( NULL, "unknown delivery system %s", psz_delsys );
1102 exit(1);
1105 if ( i_systems == 1 )
1106 return p_systems[0];
1108 int i;
1109 for ( i = 0; i < i_systems; i++ )
1111 switch ( p_systems[i] )
1113 case SYS_DVBS:
1114 if ( i_frequency < 50000000 )
1115 return SYS_DVBS;
1116 break;
1117 #if DVBAPI_VERSION >= 505
1118 case SYS_DVBC_ANNEX_A:
1119 if ( i_frequency > 50000000 || i_srate != 27500000 ||
1120 psz_modulation != NULL )
1121 return SYS_DVBC_ANNEX_A;
1122 break;
1123 #else
1124 case SYS_DVBC_ANNEX_AC:
1125 if ( i_frequency > 50000000 || i_srate != 27500000 ||
1126 psz_modulation != NULL )
1127 return SYS_DVBC_ANNEX_AC;
1128 break;
1129 #endif
1130 case SYS_DVBT:
1131 if ( i_frequency > 50000000 )
1132 return SYS_DVBT;
1133 break;
1134 case SYS_DVBT2:
1135 if ( i_frequency > 50000000 && (dvb_plp_id) )
1136 return SYS_DVBT2;
1137 break;
1138 default:
1139 break;
1143 msg_Warn( NULL, "couldn't guess delivery system, use --delsys" );
1144 return p_systems[0];
1147 static void FrontendSet( bool b_init )
1149 struct dvb_frontend_info info;
1150 struct dtv_properties *p;
1151 fe_delivery_system_t p_systems[MAX_DELIVERY_SYSTEMS] = { 0 };
1152 int i_systems = 0;
1154 if ( ioctl( i_frontend, FE_GET_INFO, &info ) < 0 )
1156 msg_Err( NULL, "FE_GET_INFO failed (%s)", strerror(errno) );
1157 exit(1);
1160 uint32_t version = 0x300;
1161 #if DVBAPI_VERSION >= 505
1162 if ( ioctl( i_frontend, FE_GET_PROPERTY, &info_cmdseq ) < 0 )
1164 #endif
1165 /* DVBv3 device */
1166 switch ( info.type )
1168 case FE_OFDM:
1169 p_systems[i_systems++] = SYS_DVBT;
1170 #if DVBAPI_VERSION >= 505
1171 if ( info.caps & FE_CAN_2G_MODULATION )
1172 p_systems[i_systems++] = SYS_DVBT2;
1173 #endif
1174 break;
1175 case FE_QAM:
1176 #if DVBAPI_VERSION >= 505
1177 p_systems[i_systems++] = SYS_DVBC_ANNEX_A;
1178 #else
1179 p_systems[i_systems++] = SYS_DVBC_ANNEX_AC;
1180 #endif
1181 break;
1182 case FE_QPSK:
1183 p_systems[i_systems++] = SYS_DVBS;
1184 if ( info.caps & FE_CAN_2G_MODULATION )
1185 p_systems[i_systems++] = SYS_DVBS2;
1186 break;
1187 case FE_ATSC:
1188 if ( info.caps & (FE_CAN_8VSB | FE_CAN_16VSB) )
1189 p_systems[i_systems++] = SYS_ATSC;
1190 if ( info.caps & (FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO) )
1191 p_systems[i_systems++] = SYS_DVBC_ANNEX_B;
1192 break;
1193 default:
1194 msg_Err( NULL, "unknown frontend type %d", info.type );
1195 exit(1);
1197 #if DVBAPI_VERSION >= 505
1199 else
1201 version = info_cmdargs[0].u.data;
1202 if ( ioctl( i_frontend, FE_GET_PROPERTY, &enum_cmdseq ) < 0 )
1204 msg_Err( NULL, "unable to query frontend" );
1205 exit(1);
1207 i_systems = enum_cmdargs[0].u.buffer.len;
1208 if ( i_systems < 1 )
1210 msg_Err( NULL, "no available delivery system" );
1211 exit(1);
1214 int i;
1215 for ( i = 0; i < i_systems; i++ )
1216 p_systems[i] = enum_cmdargs[0].u.buffer.data[i];
1218 #endif
1220 if ( b_init )
1221 FrontendInfo( &info, version, p_systems, i_systems );
1223 /* Clear frontend commands */
1224 if ( ioctl( i_frontend, FE_SET_PROPERTY, &cmdclear ) < 0 )
1226 msg_Err( NULL, "Unable to clear frontend" );
1227 exit(1);
1230 fe_delivery_system_t system = FrontendGuessSystem( p_systems, i_systems );
1231 switch ( system )
1233 case SYS_DVBT:
1234 p = &dvbt_cmdseq;
1235 p->props[DELSYS].u.data = system;
1236 p->props[FREQUENCY].u.data = i_frequency;
1237 p->props[INVERSION].u.data = GetInversion();
1238 if ( psz_modulation != NULL )
1239 p->props[MODULATION].u.data = GetModulation();
1240 p->props[BANDWIDTH].u.data = i_bandwidth * 1000000;
1241 p->props[FEC_INNER].u.data = GetFECInner(info.caps);
1242 p->props[FEC_LP].u.data = GetFECLP(info.caps);
1243 p->props[GUARD].u.data = GetGuard();
1244 p->props[TRANSMISSION].u.data = GetTransmission();
1245 p->props[HIERARCHY].u.data = GetHierarchy();
1247 msg_Dbg( NULL, "tuning DVB-T frontend to f=%d bandwidth=%d inversion=%d fec_hp=%d fec_lp=%d hierarchy=%d modulation=%s guard=%d transmission=%d",
1248 i_frequency, i_bandwidth, i_inversion, i_fec, i_fec_lp,
1249 i_hierarchy,
1250 psz_modulation == NULL ? "qam_auto" : psz_modulation,
1251 i_guard, i_transmission );
1252 break;
1253 case SYS_DVBT2:
1254 p = &dvbt2_cmdseq;
1255 p->props[DELSYS].u.data = system;
1256 p->props[FREQUENCY].u.data = i_frequency;
1257 p->props[INVERSION].u.data = GetInversion();
1258 if ( psz_modulation != NULL )
1259 p->props[MODULATION].u.data = GetModulation();
1260 p->props[BANDWIDTH].u.data = i_bandwidth * 1000000;
1261 p->props[FEC_INNER].u.data = GetFECInner(info.caps);
1262 p->props[FEC_LP].u.data = GetFECLP(info.caps);
1263 p->props[GUARD].u.data = GetGuard();
1264 p->props[TRANSMISSION].u.data = GetTransmission();
1265 p->props[HIERARCHY].u.data = GetHierarchy();
1266 p->props[PLP_ID].u.data = dvb_plp_id;
1268 msg_Dbg( NULL, "tuning DVB-T2 frontend to f=%d bandwidth=%d inversion=%d fec_hp=%d fec_lp=%d hierarchy=%d modulation=%s guard=%d transmission=%d PLP_ID=%d ",
1269 i_frequency, i_bandwidth, i_inversion, i_fec, i_fec_lp,
1270 i_hierarchy,
1271 psz_modulation == NULL ? "qam_auto" : psz_modulation,
1272 i_guard, i_transmission, p->props[PLP_ID].u.data );
1273 break;
1274 #if DVBAPI_VERSION >= 505
1275 case SYS_DVBC_ANNEX_A:
1276 #else
1277 case SYS_DVBC_ANNEX_AC:
1278 #endif
1279 p = &dvbc_cmdseq;
1280 p->props[FREQUENCY].u.data = i_frequency;
1281 p->props[INVERSION].u.data = GetInversion();
1282 if ( psz_modulation != NULL )
1283 p->props[MODULATION].u.data = GetModulation();
1284 p->props[SYMBOL_RATE].u.data = i_srate;
1286 msg_Dbg( NULL, "tuning DVB-C frontend to f=%d srate=%d inversion=%d modulation=%s",
1287 i_frequency, i_srate, i_inversion,
1288 psz_modulation == NULL ? "qam_auto" : psz_modulation );
1289 break;
1291 case SYS_DVBC_ANNEX_B:
1292 p = &atsc_cmdseq;
1293 p->props[DELSYS].u.data = system;
1294 p->props[FREQUENCY].u.data = i_frequency;
1295 p->props[INVERSION].u.data = GetInversion();
1296 if ( psz_modulation != NULL )
1297 p->props[MODULATION].u.data = GetModulation();
1299 msg_Dbg( NULL, "tuning ATSC cable frontend to f=%d inversion=%d modulation=%s",
1300 i_frequency, i_inversion,
1301 psz_modulation == NULL ? "qam_auto" : psz_modulation );
1302 break;
1304 case SYS_DVBS:
1305 case SYS_DVBS2:
1306 if ( psz_modulation != NULL )
1308 p = &dvbs2_cmdseq;
1309 p->props[MODULATION].u.data = GetModulation();
1310 p->props[IDX_DVBS2_PILOT].u.data = GetPilot();
1311 p->props[IDX_DVBS2_ROLLOFF].u.data = GetRollOff();
1312 p->props[IDX_DVBS2_STREAM_ID].u.data = i_mis;
1314 else
1315 p = &dvbs_cmdseq;
1317 p->props[INVERSION].u.data = GetInversion();
1318 p->props[SYMBOL_RATE].u.data = i_srate;
1319 p->props[FEC_INNER].u.data = GetFECInner(info.caps);
1320 p->props[FREQUENCY].u.data = FrontendDoDiseqc();
1322 msg_Dbg( NULL, "tuning DVB-S frontend to f=%d srate=%d inversion=%d fec=%d rolloff=%d modulation=%s pilot=%d mis=%d /pls-mode: %s (%d) pls-code: %d is-id: %d /",
1323 i_frequency, i_srate, i_inversion, i_fec, i_rolloff,
1324 psz_modulation == NULL ? "legacy" : psz_modulation, i_pilot,
1325 i_mis, psz_mis_pls_mode, i_mis_pls_mode, i_mis_pls_code, i_mis_is_id );
1326 break;
1328 case SYS_ATSC:
1329 p = &atsc_cmdseq;
1330 p->props[FREQUENCY].u.data = i_frequency;
1331 p->props[INVERSION].u.data = GetInversion();
1332 if ( psz_modulation != NULL )
1333 p->props[MODULATION].u.data = GetModulation();
1335 msg_Dbg( NULL, "tuning ATSC frontend to f=%d inversion=%d modulation=%s",
1336 i_frequency, i_inversion,
1337 psz_modulation == NULL ? "qam_auto" : psz_modulation );
1338 break;
1339 case SYS_ISDBT:
1340 p = &isdbt_cmdseq;
1341 p->props[DELSYS].u.data = system;
1342 p->props[FREQUENCY].u.data = i_frequency;
1343 p->props[ISDBT_BANDWIDTH].u.data = i_bandwidth * 1000000;
1344 p->props[INVERSION].u.data = GetInversion();
1345 p->props[ISDBT_LAYERA_FEC].u.data = FEC_AUTO;
1346 p->props[ISDBT_LAYERA_MODULATION].u.data = QAM_AUTO;
1347 p->props[ISDBT_LAYERA_SEGMENT_COUNT].u.data = 0;
1348 p->props[ISDBT_LAYERA_TIME_INTERLEAVING].u.data = 0;
1349 p->props[ISDBT_LAYERB_FEC].u.data = FEC_AUTO;
1350 p->props[ISDBT_LAYERB_MODULATION].u.data = QAM_AUTO;
1351 p->props[ISDBT_LAYERB_SEGMENT_COUNT].u.data = 0;
1352 p->props[ISDBT_LAYERB_TIME_INTERLEAVING].u.data = 0;
1353 p->props[ISDBT_LAYERC_FEC].u.data = FEC_AUTO;
1354 p->props[ISDBT_LAYERC_MODULATION].u.data = QAM_AUTO;
1355 p->props[ISDBT_LAYERC_SEGMENT_COUNT].u.data = 0;
1356 p->props[ISDBT_LAYERC_TIME_INTERLEAVING].u.data = 0;
1358 msg_Dbg( NULL, "tuning ISDB-T frontend to f=%d bandwidth=%d ",
1359 i_frequency, i_bandwidth);
1360 break;
1362 default:
1363 msg_Err( NULL, "unknown frontend type %d", info.type );
1364 exit(1);
1367 /* Empty the event queue */
1368 for ( ; ; )
1370 struct dvb_frontend_event event;
1371 if ( ioctl( i_frontend, FE_GET_EVENT, &event ) < 0
1372 && errno == EWOULDBLOCK )
1373 break;
1376 /* Now send it all to the frontend device */
1377 if ( ioctl( i_frontend, FE_SET_PROPERTY, p ) < 0 )
1379 msg_Err( NULL, "setting frontend failed (%s)", strerror(errno) );
1380 exit(1);
1383 i_last_status = 0;
1385 if (i_frontend_timeout_duration)
1386 ev_timer_again(event_loop, &lock_watcher);
1389 #else /* !S2API */
1391 #warning "You are trying to compile DVBlast with an outdated linux-dvb interface."
1392 #warning "DVBlast will be very limited and some options will have no effect."
1394 static void FrontendSet( bool b_init )
1396 struct dvb_frontend_info info;
1397 struct dvb_frontend_parameters fep;
1399 if ( ioctl( i_frontend, FE_GET_INFO, &info ) < 0 )
1401 msg_Err( NULL, "FE_GET_INFO failed (%s)", strerror(errno) );
1402 exit(1);
1405 switch ( info.type )
1407 case FE_OFDM:
1408 fep.frequency = i_frequency;
1409 fep.inversion = INVERSION_AUTO;
1411 switch ( i_bandwidth )
1413 case 6: fep.u.ofdm.bandwidth = BANDWIDTH_6_MHZ; break;
1414 case 7: fep.u.ofdm.bandwidth = BANDWIDTH_7_MHZ; break;
1415 default:
1416 case 8: fep.u.ofdm.bandwidth = BANDWIDTH_8_MHZ; break;
1419 fep.u.ofdm.code_rate_HP = FEC_AUTO;
1420 fep.u.ofdm.code_rate_LP = FEC_AUTO;
1421 fep.u.ofdm.constellation = QAM_AUTO;
1422 fep.u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1423 fep.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1424 fep.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1426 msg_Dbg( NULL, "tuning OFDM frontend to f=%d, bandwidth=%d",
1427 i_frequency, i_bandwidth );
1428 break;
1430 case FE_QAM:
1431 fep.frequency = i_frequency;
1432 fep.inversion = INVERSION_AUTO;
1433 fep.u.qam.symbol_rate = i_srate;
1434 fep.u.qam.fec_inner = FEC_AUTO;
1435 fep.u.qam.modulation = QAM_AUTO;
1437 msg_Dbg( NULL, "tuning QAM frontend to f=%d, srate=%d",
1438 i_frequency, i_srate );
1439 break;
1441 case FE_QPSK:
1442 fep.inversion = INVERSION_AUTO;
1443 fep.u.qpsk.symbol_rate = i_srate;
1444 fep.u.qpsk.fec_inner = FEC_AUTO;
1445 fep.frequency = FrontendDoDiseqc();
1447 msg_Dbg( NULL, "tuning QPSK frontend to f=%d, srate=%d",
1448 i_frequency, i_srate );
1449 break;
1451 #if DVBAPI_VERSION >= 301
1452 case FE_ATSC:
1453 fep.frequency = i_frequency;
1455 fep.u.vsb.modulation = QAM_AUTO;
1457 msg_Dbg( NULL, "tuning ATSC frontend to f=%d", i_frequency );
1458 break;
1459 #endif
1461 default:
1462 msg_Err( NULL, "unknown frontend type %d", info.type );
1463 exit(1);
1466 /* Empty the event queue */
1467 for ( ; ; )
1469 struct dvb_frontend_event event;
1470 if ( ioctl( i_frontend, FE_GET_EVENT, &event ) < 0
1471 && errno == EWOULDBLOCK )
1472 break;
1475 /* Now send it all to the frontend device */
1476 if ( ioctl( i_frontend, FE_SET_FRONTEND, &fep ) < 0 )
1478 msg_Err( NULL, "setting frontend failed (%s)", strerror(errno) );
1479 exit(1);
1482 i_last_status = 0;
1484 if (i_frontend_timeout_duration)
1485 ev_timer_again(event_loop, &lock_watcher);
1488 #endif /* S2API */
1490 /*****************************************************************************
1491 * dvb_FrontendStatus
1492 *****************************************************************************/
1493 uint8_t dvb_FrontendStatus( uint8_t *p_answer, ssize_t *pi_size )
1495 struct ret_frontend_status *p_ret = (struct ret_frontend_status *)p_answer;
1497 if ( ioctl( i_frontend, FE_GET_INFO, &p_ret->info ) < 0 )
1499 msg_Err( NULL, "ioctl FE_GET_INFO failed (%s)", strerror(errno) );
1500 return RET_ERR;
1503 if ( ioctl( i_frontend, FE_READ_STATUS, &p_ret->i_status ) < 0 )
1505 msg_Err( NULL, "ioctl FE_READ_STATUS failed (%s)", strerror(errno) );
1506 return RET_ERR;
1509 if ( p_ret->i_status & FE_HAS_LOCK )
1511 if ( ioctl( i_frontend, FE_READ_BER, &p_ret->i_ber ) < 0 )
1512 msg_Err( NULL, "ioctl FE_READ_BER failed (%s)", strerror(errno) );
1514 if ( ioctl( i_frontend, FE_READ_SIGNAL_STRENGTH, &p_ret->i_strength )
1515 < 0 )
1516 msg_Err( NULL, "ioctl FE_READ_SIGNAL_STRENGTH failed (%s)",
1517 strerror(errno) );
1519 if ( ioctl( i_frontend, FE_READ_SNR, &p_ret->i_snr ) < 0 )
1520 msg_Err( NULL, "ioctl FE_READ_SNR failed (%s)", strerror(errno) );
1523 *pi_size = sizeof(struct ret_frontend_status);
1524 return RET_FRONTEND_STATUS;
1527 #endif