#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <argp.h>
#include <config.h>
#include <linux/dvb/dmx.h>
#define PROGRAM_NAME "dvbv5-scan"
#define DEFAULT_OUTPUT "dvb_channel.conf"
const char *argp_program_version = PROGRAM_NAME " version " V4L_UTILS_VERSION;
const char *argp_program_bug_address = "Mauro Carvalho Chehab <m.chehab@samsung.com>";
struct arguments {
char *confname, *lnb_name, *output, *demux_dev;
unsigned adapter, n_adapter, adapter_fe, adapter_dmx, frontend, demux, get_detected, get_nit;
int force_dvbv3, lna, lnb, sat_number, freq_bpf;
unsigned diseqc_wait, dont_add_new_freqs, timeout_multiply;
unsigned other_nit;
unsigned n_status_lines;
};
static const struct argp_option options[] = {
{"adapter", 'a', "adapter#", 0, "use given adapter (default 0)", 0},
{"frontend", 'f', "frontend#", 0, "use given frontend (default 0)", 0},
{"demux", 'd', "demux#", 0, "use given demux (default 0)", 0},
{"lnbf", 'l', "LNBf_type", 0, "type of LNBf to use. 'help' lists the available ones", 0},
{"lna", 'w', "LNA (0, 1, -1)", 0, "enable/disable/auto LNA power", 0},
{"sat_number", 'S', "satellite_number", 0, "satellite number. If not specified, disable DISEqC", 0},
{"freq_bpf", 'U', "frequency", 0, "SCR/Unicable band-pass filter frequency to use, in kHz", 0},
{"wait", 'W', "time", 0, "adds additional wait time for DISEqC command completion", 0},
{"nit", 'N', NULL, 0, "use data from NIT table on the output file", 0},
{"get_frontend",'G', NULL, 0, "use data from get_frontend on the output file", 0},
{"verbose", 'v', NULL, 0, "be (very) verbose", 0},
{"output", 'o', "file", 0, "output filename (default: " DEFAULT_OUTPUT ")", 0},
{"file-freqs-only", 'F', NULL, 0, "don't use the other frequencies discovered during scan", 0},
{"timeout-multiply", 'T', "factor", 0, "Multiply scan timeouts by this factor", 0},
{"parse-other-nit", 'p', NULL, 0, "Parse the other NIT/SDT tables", 0},
{"input-format", 'I', "format", 0, "Input format: CHANNEL, DVBV5 (default: DVBV5)", 0},
{"output-format", 'O', "format", 0, "Output format: VDR, CHANNEL, ZAP, DVBV5 (default: DVBV5)", 0},
{"dvbv3", '3', 0, 0, "Use DVBv3 only", 0},
{ 0, 0, 0, 0, 0, 0 }
};
static int verbose = 0;
#define CHANNEL_FILE "channels.conf"
#define ERROR(x...) \
do { \
fprintf(stderr, "ERROR: "); \
fprintf(stderr, x); \
fprintf(stderr, "\n"); \
} while (0)
#define PERROR(x...) \
do { \
fprintf(stderr, "ERROR: "); \
fprintf(stderr, x); \
fprintf(stderr, " (%s)\n", strerror(errno)); \
} while (0)
static int print_frontend_stats(struct arguments *args,
{
char buf[512], *p;
int rc, i, len, show;
uint32_t status = 0;
if (isatty(STDERR_FILENO) && args->n_status_lines)
fprintf(stderr, "\r\x1b[%dA\x1b[J", args->n_status_lines);
args->n_status_lines = 0;
if (isatty(STDERR_FILENO)) {
if (rc)
status = 0;
if (status & FE_HAS_LOCK)
fprintf(stderr, "\x1b[1;32m");
else
fprintf(stderr, "\x1b[33m");
}
p = buf;
len = sizeof(buf);
for (i = 0; i < MAX_DTV_STATS; i++) {
show = 1;
i, &p, &len, &show);
i, &p, &len, &show);
i, &p, &len, &show);
i, &p, &len, &show);
i, &p, &len, &show);
i, &p, &len, &show);
i, &p, &len, &show);
if (p != buf) {
if (args->n_status_lines)
fprintf(stderr, "\t%s\n", buf);
else
fprintf(stderr, "%s\n", buf);
args->n_status_lines++;
p = buf;
len = sizeof(buf);
}
}
fflush(stderr);
return 0;
}
static int check_frontend(void *__args,
{
struct arguments *args = __args;
int rc, i;
fe_status_t status;
args->n_status_lines = 0;
for (i = 0; i < args->timeout_multiply * 40; i++) {
return 0;
if (rc)
PERROR("dvb_fe_get_stats failed");
if (rc)
status = 0;
print_frontend_stats(args, parms);
if (status & FE_HAS_LOCK)
break;
usleep(100000);
};
if (isatty(STDERR_FILENO)) {
fprintf(stderr, "\x1b[37m");
}
return (status & FE_HAS_LOCK) ? 0 : -1;
}
static int run_scan(struct arguments *args,
{
int count = 0, dmx_fd, shift;
uint32_t freq, sys;
case SYS_DVBT:
case SYS_DVBS:
case SYS_DVBC_ANNEX_A:
case SYS_ATSC:
break;
case SYS_DVBC_ANNEX_C:
sys = SYS_DVBC_ANNEX_A;
break;
case SYS_DVBC_ANNEX_B:
sys = SYS_ATSC;
break;
case SYS_ISDBT:
sys = SYS_DVBT;
break;
default:
sys = SYS_UNDEFINED;
break;
}
args->input_format);
if (!dvb_file)
return -2;
dmx_fd = open(args->demux_dev, O_RDWR);
if (dmx_fd < 0) {
perror("openening pat demux failed");
return -3;
}
continue;
shift = dvb_estimate_freq_shift(parms);
if (!dvb_new_freq_is_needed(dvb_file->
first_entry, entry,
freq, pol, shift))
continue;
count++;
dvb_log("Scanning frequency #%d %d", count, freq);
&check_frontend, args,
args->other_nit,
args->timeout_multiply);
break;
}
if (!dvb_scan_handler)
continue;
args->get_detected, args->get_nit);
if (!args->dont_add_new_freqs)
}
if (dvb_file_new)
if (dvb_file_new)
close(dmx_fd);
return 0;
}
static error_t parse_opt(int k, char *optarg, struct argp_state *state)
{
struct arguments *args = state->input;
switch (k) {
case 'a':
args->adapter = strtoul(optarg, NULL, 0);
args->n_adapter++;
break;
case 'f':
args->frontend = strtoul(optarg, NULL, 0);
args->adapter_fe = args->adapter;
break;
case 'd':
args->demux = strtoul(optarg, NULL, 0);
args->adapter_dmx = args->adapter;
break;
case 'w':
if (!strcasecmp(optarg,"on")) {
args->lna = 1;
} else if (!strcasecmp(optarg,"off")) {
args->lna = 0;
} else if (!strcasecmp(optarg,"auto")) {
args->lna = LNA_AUTO;
} else {
int val = strtoul(optarg, NULL, 0);
if (!val)
args->lna = 0;
else if (val > 0)
args->lna = 1;
else
args->lna = LNA_AUTO;
}
break;
case 'l':
args->lnb_name = optarg;
break;
case 'S':
args->sat_number = strtoul(optarg, NULL, 0);
break;
case 'U':
args->freq_bpf = strtoul(optarg, NULL, 0);
break;
case 'W':
args->diseqc_wait = strtoul(optarg, NULL, 0);
break;
case 'N':
args->get_nit++;
break;
case 'G':
args->get_detected++;
break;
case 'F':
args->dont_add_new_freqs++;
break;
case 'p':
args->other_nit++;
break;
case 'v':
verbose++;
break;
case 'T':
args->timeout_multiply = strtoul(optarg, NULL, 0);
break;
case 'I':
break;
case 'O':
break;
case 'o':
args->output = optarg;
break;
case '3':
args->force_dvbv3 = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
};
return 0;
}
static int *timeout_flag;
static void do_timeout(int x)
{
(void)x;
if (*timeout_flag == 0) {
*timeout_flag = 1;
alarm(5);
signal(SIGALRM, do_timeout);
} else {
exit(1);
}
}
int main(int argc, char **argv)
{
struct arguments args;
int err, lnb = -1,idx = -1;
int r;
const struct argp argp = {
.options = options,
.parser = parse_opt,
.doc = "scan DVB services using the channel file",
.args_doc = "<initial file>",
};
memset(&args, 0, sizeof(args));
args.sat_number = -1;
args.output = DEFAULT_OUTPUT;
args.timeout_multiply = 1;
args.adapter = (unsigned)-1;
args.lna = LNA_AUTO;
argp_parse(&argp, argc, argv, 0, &idx, &args);
if (args.timeout_multiply == 0)
args.timeout_multiply = 1;
if (args.n_adapter == 1) {
args.adapter_fe = args.adapter;
args.adapter_dmx = args.adapter;
}
if (args.lnb_name) {
if (lnb < 0) {
printf("Please select one of the LNBf's below:\n");
exit(1);
} else {
printf("Using LNBf ");
}
}
if (idx < argc)
args.confname = argv[idx];
if (!args.confname || idx < 0) {
argp_help(&argp, stderr, ARGP_HELP_STD_HELP, PROGRAM_NAME);
return -1;
}
fprintf(stderr, "ERROR: Please specify a valid format\n");
argp_help(&argp, stderr, ARGP_HELP_STD_HELP, PROGRAM_NAME);
return -1;
}
r = asprintf(&args.demux_dev,
"/dev/dvb/adapter%i/demux%i", args.adapter_dmx, args.demux);
if (r < 0) {
fprintf(stderr, "asprintf error\n" );
return -1;
}
if (verbose)
fprintf(stderr, "using demux '%s'\n", args.demux_dev);
args.frontend,
if (!parms) {
free(args.demux_dev);
return -1;
}
if (lnb >= 0)
if (args.sat_number >= 0)
timeout_flag = &parms->
abort;
signal(SIGTERM, do_timeout);
signal(SIGINT, do_timeout);
err = run_scan(&args, parms);
free(args.demux_dev);
return err;
}