diff src/recpt1.c @ 168:061ef2cd98f0

Code refactoring.
author Naoya OYAMA <naoya.oyama@gmail.com>
date Sun, 07 Oct 2012 00:37:10 +0900
parents 726fe10d9e4a
children 4f3640bf350d
line wrap: on
line diff
--- a/src/recpt1.c	Sat Oct 06 21:57:42 2012 +0900
+++ b/src/recpt1.c	Sun Oct 07 00:37:10 2012 +0900
@@ -41,22 +41,14 @@
 /* maximum write length at once */
 #define SIZE_CHANK 1316
 
-#define ISDB_T_NODE_LIMIT 24        // 32:ARIB limit 24:program maximum
-#define ISDB_T_SLOT_LIMIT 8
-
 /* globals */
 boolean f_exit = FALSE;
 struct channel_info_list *channel_list = NULL;
 extern struct ushare_t *ut;
-char  bs_channel_buf[8];
-ISDB_T_FREQ_CONV_TABLE isdb_t_conv_set = { 0, CHTYPE_SATELLITE, 0, bs_channel_buf };
+thread_data *gp_tdata;
 
 /* prototypes */
-int tune(char *channel, thread_data *tdata, char *device);
-int close_tuner(thread_data *tdata);
 static int get_device_id_by_name ( const char *name );
-void calc_cn(int fd, int type);
-ISDB_T_FREQ_CONV_TABLE *searchrecoff(char *channel);
 
 static struct channel_info_list *open_list_file(
        char *type,
@@ -262,7 +254,7 @@
                     tdata->ch[0] = '\0';
                     goto CHECK_TIME_TO_ADD;
                 }
-                calc_cn(tdata->tfd, tdata->table->type);
+                calc_cn(tdata->tfd, tdata->table->type, false);
             }
             /* restart recording */
             if(ioctl(tdata->tfd, START_REC, 0) < 0) {
@@ -294,50 +286,6 @@
     }
 }
 
-
-/* lookup frequency conversion table*/
-ISDB_T_FREQ_CONV_TABLE *
-searchrecoff(char *channel)
-{
-    int lp;
-
-    if((channel[0] == 'B' || channel[0] == 'b') &&
-       (channel[1] == 'S' || channel[1] == 's')) {
-        int node = 0;
-        int slot = 0;
-        char *bs_ch;
-
-        bs_ch = channel + 2;
-        while(isdigit(*bs_ch)) {
-            node *= 10;
-            node += *bs_ch++ - '0';
-        }
-        if(*bs_ch == '_' && (node&0x01) && node < ISDB_T_NODE_LIMIT) {
-            if(isdigit(*++bs_ch)) {
-                slot = *bs_ch - '0';
-                if(*++bs_ch == '\0' && slot < ISDB_T_SLOT_LIMIT) {
-                    isdb_t_conv_set.set_freq = node / 2;
-                    isdb_t_conv_set.add_freq = slot;
-                    sprintf(bs_channel_buf, "BS%d_%d", node, slot);
-                    return &isdb_t_conv_set;
-                }
-            }
-        }
-        return NULL;
-    }
-
-    for(lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) {
-        /* return entry number in the table when strings match and
-         * lengths are same. */
-        if((memcmp(isdb_t_conv_table[lp].parm_freq, channel,
-                   strlen(channel)) == 0) &&
-           (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))) {
-            return &isdb_t_conv_table[lp];
-        }
-    }
-    return NULL;
-}
-
 QUEUE_T *
 create_queue(size_t size)
 {
@@ -953,113 +901,6 @@
 }
 
 void
-show_channels(void)
-{
-    FILE *f;
-    char *home;
-    char buf[255], filename[255];
-
-    fprintf(stderr, "Available Channels:\n");
-
-    home = getenv("HOME");
-    sprintf(filename, "%s/.recpt1-channels", home);
-    f = fopen(filename, "r");
-    if(f) {
-        while(fgets(buf, 255, f))
-            fprintf(stderr, "%s", buf);
-        fclose(f);
-    }
-    else {
-        fprintf(stderr, "13-62: Terrestrial Channels\n");
-        fprintf(stderr, "101ch: NHK BS1\n");
-        fprintf(stderr, "102ch: NHK BS2\n");
-        fprintf(stderr, "103ch: NHK BShi\n");
-        fprintf(stderr, "141ch: BS Nittele\n");
-        fprintf(stderr, "151ch: BS Asahi\n");
-        fprintf(stderr, "161ch: BS-TBS\n");
-        fprintf(stderr, "171ch: BS Japan\n");
-        fprintf(stderr, "181ch: BS Fuji\n");
-        fprintf(stderr, "191ch: WOWOW\n");
-        fprintf(stderr, "192ch: WOWOW2\n");
-        fprintf(stderr, "193ch: WOWOW3\n");
-        fprintf(stderr, "200ch: Star Channel\n");
-        fprintf(stderr, "211ch: BS11 Digital\n");
-        fprintf(stderr, "222ch: TwellV\n");
-        fprintf(stderr, "C13-C63: CATV Channels\n");
-        fprintf(stderr, "CS2-CS24: CS Channels\n");
-    }
-}
-
-float
-getsignal_isdb_s(int signal)
-{
-    /* apply linear interpolation */
-    static const float afLevelTable[] = {
-        24.07f,    // 00    00    0        24.07dB
-        24.07f,    // 10    00    4096     24.07dB
-        18.61f,    // 20    00    8192     18.61dB
-        15.21f,    // 30    00    12288    15.21dB
-        12.50f,    // 40    00    16384    12.50dB
-        10.19f,    // 50    00    20480    10.19dB
-        8.140f,    // 60    00    24576    8.140dB
-        6.270f,    // 70    00    28672    6.270dB
-        4.550f,    // 80    00    32768    4.550dB
-        3.730f,    // 88    00    34816    3.730dB
-        3.630f,    // 88    FF    35071    3.630dB
-        2.940f,    // 90    00    36864    2.940dB
-        1.420f,    // A0    00    40960    1.420dB
-        0.000f     // B0    00    45056    -0.01dB
-    };
-
-    unsigned char sigbuf[4];
-    memset(sigbuf, '\0', sizeof(sigbuf));
-    sigbuf[0] =  (((signal & 0xFF00) >> 8) & 0XFF);
-    sigbuf[1] =  (signal & 0xFF);
-
-    /* calculate signal level */
-    if(sigbuf[0] <= 0x10U) {
-        /* clipped maximum */
-        return 24.07f;
-    }
-    else if (sigbuf[0] >= 0xB0U) {
-        /* clipped minimum */
-        return 0.0f;
-    }
-    else {
-        /* linear interpolation */
-        const float fMixRate =
-            (float)(((unsigned short)(sigbuf[0] & 0x0FU) << 8) |
-                    (unsigned short)sigbuf[0]) / 4096.0f;
-        return afLevelTable[sigbuf[0] >> 4] * (1.0f - fMixRate) +
-            afLevelTable[(sigbuf[0] >> 4) + 0x01U] * fMixRate;
-    }
-}
-
-void
-calc_cn(int fd, int type)
-{
-    int     rc ;
-    double  P ;
-    double  CNR;
-
-    if(ioctl(fd, GET_SIGNAL_STRENGTH, &rc) < 0) {
-        fprintf(stderr, "Tuner Select Error\n");
-        return ;
-    }
-
-    if(type == CHTYPE_GROUND) {
-        P = log10(5505024/(double)rc) * 10;
-        CNR = (0.000024 * P * P * P * P) - (0.0016 * P * P * P) +
-                    (0.0398 * P * P) + (0.5491 * P)+3.0965;
-        fprintf(stderr, "C/N = %fdB\n", CNR);
-    }
-    else {
-        CNR = getsignal_isdb_s(rc);
-        fprintf(stderr, "C/N = %fdB\n", CNR);
-    }
-}
-
-void
 cleanup(thread_data *tdata)
 {
     boolean use_dlna = tdata->streamer ? TRUE : FALSE;
@@ -1137,100 +978,6 @@
 }
 
 int
-tune(char *channel, thread_data *tdata, char *device)
-{
-    char **tuner;
-    int num_devs;
-    int lp;
-    FREQUENCY freq;
-
-    /* get channel */
-    tdata->table = searchrecoff(channel);
-    if(tdata->table == NULL) {
-        fprintf(stderr, "Invalid Channel: %s\n", channel);
-        return 1;
-    }
-
-    freq.frequencyno = tdata->table->set_freq;
-    freq.slot = tdata->table->add_freq;
-
-    /* open tuner */
-    /* case 1: specified tuner device */
-    if(device) {
-        tdata->tfd = open(device, O_RDONLY);
-	tdata->device_id = get_device_id_by_name(device);
-        if(tdata->tfd < 0) {
-            fprintf(stderr, "Cannot open tuner device: %s\n", device);
-            return 1;
-        }
-
-        /* power on LNB */
-        if(tdata->table->type == CHTYPE_SATELLITE) {
-            if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
-                fprintf(stderr, "Power on LNB failed: %s\n", device);
-            }
-        }
-
-        /* tune to specified channel */
-        if(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) {
-            close(tdata->tfd);
-            fprintf(stderr, "Cannot tune to the specified channel: %s\n", device);
-            return 1;
-        }
-        else {
-            strncpy(tdata->ch, channel, sizeof(tdata->ch));
-        }
-    }
-    else {
-        /* case 2: loop around available devices */
-        if(tdata->table->type == CHTYPE_SATELLITE) {
-            tuner = bsdev;
-            num_devs = NUM_BSDEV;
-        }
-        else {
-            tuner = isdb_t_dev;
-            num_devs = NUM_ISDB_T_DEV;
-        }
-
-        for(lp = 0; lp < num_devs; lp++) {
-            tdata->tfd = open(tuner[lp], O_RDONLY);
-            if(tdata->tfd >= 0) {
-                /* power on LNB */
-                if(tdata->table->type == CHTYPE_SATELLITE) {
-                    if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
-                        fprintf(stderr, "Warning: Power on LNB failed: %s\n", tuner[lp]);
-                    }
-                }
-
-                /* tune to specified channel */
-                if(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) {
-                    close(tdata->tfd);
-                    tdata->tfd = -1;
-                    continue;
-                }
-
-                tdata->device_id = get_device_id_by_name(tuner[lp]);
-                break; /* found suitable tuner */
-            }
-        }
-
-        /* all tuners cannot be used */
-        if(tdata->tfd < 0) {
-            fprintf(stderr, "Cannot tune to the specified channel\n");
-            return 1;
-        }
-        else {
-            strncpy(tdata->ch, channel, sizeof(tdata->ch));
-        }
-    }
-
-    /* show signal strength */
-    calc_cn(tdata->tfd, tdata->table->type);
-
-    return 0; /* success */
-}
-
-int
 parse_time(char *rectimestr, thread_data *tdata)
 {
     /* indefinite */
@@ -1283,22 +1030,6 @@
     return 0; /* success */
 }
 
-int
-close_tuner(thread_data *tdata)
-{
-    int rv = 0;
-
-    if(tdata->table->type == CHTYPE_SATELLITE) {
-        if(ioctl(tdata->tfd, LNB_DISABLE, 0) < 0) {
-            rv = 1;
-        }
-    }
-    close(tdata->tfd);
-
-    return rv;
-}
-
-thread_data *gp_tdata;
 
 int
 main(int argc, char **argv)