int main( int argc, char *argv[] ){ pid_t pid, sid; pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } umask(0); openlog("EPGREAD_LOG:", LOG_PID|LOG_CONS, LOG_USER); sid = setsid(); if (sid < 0) { syslog(LOG_INFO, "SID fail"); exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { /* Log the failure */ syslog(LOG_INFO, "chdir fail"); exit(EXIT_FAILURE); } close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); int adapter = 0; int frontend = 0; int demux = 0; int opt; char channelsfile[128] = "/tmp/channels.conf"; int lnb = 0; char xmlPath[128] = "/tmp/XML/"; while ((opt = getopt(argc, argv, "a:f:d:c:l:t:")) != -1) { switch (opt) { case 'a': adapter = strtoul(optarg, NULL, 0); break; case 'f': frontend = strtoul(optarg, NULL, 0); break; case 'd': demux = strtoul(optarg, NULL, 0); break; case 'c': strncpy(channelsfile, optarg, sizeof(channelsfile)); break; case 'l': lnb = strtoul(optarg, NULL, 0); break; case 't': strncpy(xmlPath, optarg, sizeof(xmlPath)); break; } } if(lnb < 5){ lnb_type = lnbs[lnb]; } else { syslog(LOG_INFO, "Bad lnb number: %d", lnb ); return -1; } snprintf(dedev, sizeof(dedev), DEMUXDEVICE, adapter, demux); snprintf(frdev, sizeof(frdev), FRONTENDDEVICE, adapter, frontend); FILE *fchannels = fopen(channelsfile, "r"); if (fchannels == NULL) { syslog(LOG_INFO, "Unable to open %s\n", channelsfile); exit(1); } while (1) { dvbcfg_zapchannel_parse(fchannels, channels_cb, NULL); events.sort(SortCompare); ToXml(xmlPath, channels, events); sleep(10); /* wait 10 seconds */ fseek(fchannels, 0, SEEK_SET); } fclose(fchannels); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int adapter_id = 0; int frontend_id = 0; int demux_id = 0; int caslot_num = 0; char *chanfile = "/etc/channels.conf"; char *secfile = NULL; char *secid = NULL; char *channel_name = NULL; int output_type = OUTPUT_TYPE_DECODER; char *outfile = NULL; char *outhost = NULL; char *outport = NULL; char *outif = NULL; struct addrinfo *outaddrs = NULL; int timeout = -1; int moveca = 1; int cammenu = 0; int argpos = 1; struct gnutv_dvb_params gnutv_dvb_params; struct gnutv_ca_params gnutv_ca_params; int ffaudiofd = -1; int usertp = 0; int buffer_size = 0; while(argpos != argc) { if (!strcmp(argv[argpos], "-h")) { usage(); } else if (!strcmp(argv[argpos], "-adapter")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &adapter_id) != 1) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-frontend")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &frontend_id) != 1) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-demux")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &demux_id) != 1) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-caslotnum")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &caslot_num) != 1) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-channels")) { if ((argc - argpos) < 2) usage(); chanfile = argv[argpos+1]; argpos+=2; } else if (!strcmp(argv[argpos], "-secfile")) { if ((argc - argpos) < 2) usage(); secfile = argv[argpos+1]; argpos+=2; } else if (!strcmp(argv[argpos], "-secid")) { if ((argc - argpos) < 2) usage(); secid = argv[argpos+1]; argpos+=2; } else if (!strcmp(argv[argpos], "-buffer")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &buffer_size) != 1) usage(); if (buffer_size < 0) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-out")) { if ((argc - argpos) < 2) usage(); if (!strcmp(argv[argpos+1], "decoder")) { output_type = OUTPUT_TYPE_DECODER; } else if (!strcmp(argv[argpos+1], "decoderabypass")) { output_type = OUTPUT_TYPE_DECODER_ABYPASS; } else if (!strcmp(argv[argpos+1], "dvr")) { output_type = OUTPUT_TYPE_DVR; } else if (!strcmp(argv[argpos+1], "null")) { output_type = OUTPUT_TYPE_NULL; } else if (!strcmp(argv[argpos+1], "stdout")) { output_type = OUTPUT_TYPE_STDOUT; } else if (!strcmp(argv[argpos+1], "file")) { output_type = OUTPUT_TYPE_FILE; if ((argc - argpos) < 3) usage(); outfile = argv[argpos+2]; argpos++; } else if ((!strcmp(argv[argpos+1], "udp")) || (!strcmp(argv[argpos+1], "rtp"))) { output_type = OUTPUT_TYPE_UDP; if ((argc - argpos) < 4) usage(); if (!strcmp(argv[argpos+1], "rtp")) usertp = 1; outhost = argv[argpos+2]; outport = argv[argpos+3]; argpos+=2; } else if ((!strcmp(argv[argpos+1], "udpif")) || (!strcmp(argv[argpos+1], "rtpif"))) { output_type = OUTPUT_TYPE_UDP; if ((argc - argpos) < 5) usage(); if (!strcmp(argv[argpos+1], "rtpif")) usertp = 1; outhost = argv[argpos+2]; outport = argv[argpos+3]; outif = argv[argpos+4]; argpos+=3; } else { usage(); } argpos+=2; } else if (!strcmp(argv[argpos], "-timeout")) { if ((argc - argpos) < 2) usage(); if (sscanf(argv[argpos+1], "%i", &timeout) != 1) usage(); argpos+=2; } else if (!strcmp(argv[argpos], "-nomoveca")) { moveca = 0; argpos++; } else if (!strcmp(argv[argpos], "-cammenu")) { cammenu = 1; argpos++; } else { if ((argc - argpos) != 1) usage(); channel_name = argv[argpos]; argpos++; } } // the user didn't select anything! if ((channel_name == NULL) && (!cammenu)) usage(); // resolve host/port if ((outhost != NULL) && (outport != NULL)) { int res; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ((res = getaddrinfo(outhost, outport, &hints, &outaddrs)) != 0) { fprintf(stderr, "Unable to resolve requested address: %s\n", gai_strerror(res)); exit(1); } } // setup any signals signal(SIGINT, signal_handler); signal(SIGPIPE, SIG_IGN); // start the CA stuff gnutv_ca_params.adapter_id = adapter_id; gnutv_ca_params.caslot_num = caslot_num; gnutv_ca_params.cammenu = cammenu; gnutv_ca_params.moveca = moveca; gnutv_ca_start(&gnutv_ca_params); // frontend setup if a channel name was supplied if ((!cammenu) && (channel_name != NULL)) { // find the requested channel if (strlen(channel_name) >= sizeof(gnutv_dvb_params.channel.name)) { fprintf(stderr, "Channel name is too long %s\n", channel_name); exit(1); } FILE *channel_file = fopen(chanfile, "r"); if (channel_file == NULL) { fprintf(stderr, "Could open channel file %s\n", chanfile); exit(1); } memcpy(gnutv_dvb_params.channel.name, channel_name, strlen(channel_name) + 1); if (dvbcfg_zapchannel_parse(channel_file, find_channel, &gnutv_dvb_params.channel) != 1) { fprintf(stderr, "Unable to find requested channel %s\n", channel_name); exit(1); } fclose(channel_file); // default SEC with a DVBS card if ((secid == NULL) && (gnutv_dvb_params.channel.fe_type == DVBFE_TYPE_DVBS)) secid = "UNIVERSAL"; // look it up if one were supplied gnutv_dvb_params.valid_sec = 0; if (secid != NULL) { if (dvbsec_cfg_find(secfile, secid, &gnutv_dvb_params.sec)) { fprintf(stderr, "Unable to find suitable sec/lnb configuration for channel\n"); exit(1); } gnutv_dvb_params.valid_sec = 1; } // open the frontend gnutv_dvb_params.fe = dvbfe_open(adapter_id, frontend_id, 0); if (gnutv_dvb_params.fe == NULL) { fprintf(stderr, "Failed to open frontend\n"); exit(1); } // failover decoder to dvr output if decoder not available if ((output_type == OUTPUT_TYPE_DECODER) || (output_type == OUTPUT_TYPE_DECODER_ABYPASS)) { ffaudiofd = dvbaudio_open(adapter_id, 0); if (ffaudiofd < 0) { fprintf(stderr, "Cannot open decoder; defaulting to dvr output\n"); output_type = OUTPUT_TYPE_DVR; } } // start the DVB stuff gnutv_dvb_params.adapter_id = adapter_id; gnutv_dvb_params.frontend_id = frontend_id; gnutv_dvb_params.demux_id = demux_id; gnutv_dvb_params.output_type = output_type; gnutv_dvb_start(&gnutv_dvb_params); // start the data stuff gnutv_data_start(output_type, ffaudiofd, adapter_id, demux_id, buffer_size, outfile, outif, outaddrs, usertp); } // the UI time_t start = 0; while(!quit_app) { if (gnutv_dvb_locked() && (start == 0)) start = time(NULL); // the timeout if ((timeout != -1) && (start != 0)) { if ((time(NULL) - start) >= timeout) break; } if (cammenu) gnutv_ca_ui(); else usleep(1); } // stop data handling gnutv_data_stop(); // shutdown DVB stuff if (channel_name != NULL) gnutv_dvb_stop(); // shutdown CA stuff gnutv_ca_stop(); // done exit(0); }