Project

General

Profile

Bug #3927 » snr.c

Oliver O, 2016-08-18 18:47

 
1
#include <stdio.h>
2
#include <errno.h>
3
#include <signal.h>
4
#include <string.h>
5
#include <fcntl.h>
6
#include <unistd.h>
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <time.h>
10
#include <assert.h>
11
#include <sys/ioctl.h>
12
#include <linux/dvb/frontend.h>
13

    
14

    
15
static int fd = 0;
16
static char* program_name = "?";
17
static char* device = "?";
18

    
19

    
20
void cleanup(int signal_number) {
21
    if (fd != 0) {
22
        if (close(fd) != 0) {
23
            fprintf(stderr, "%s: error closing %s: %s\n", program_name, device, strerror(errno));
24
            exit(2);
25
        }
26
    }
27
    
28
    exit(0);
29
}
30

    
31

    
32
int main(int argc, char* argv[]) {
33
    program_name = argv[0];
34

    
35
    if (argc != 2) {
36
        fprintf(stderr, "usage: %s device\n", program_name);
37
        return 1;
38
    }
39
    
40
    device = argv[1];
41

    
42
    {
43
        static struct dtv_property properties[] = {
44
            { .cmd = DTV_STAT_CNR }
45
        };
46
        static struct dtv_properties properties_request = {
47
            .num = sizeof(properties) / sizeof(properties[0]),
48
            .props = properties
49
        };
50
        
51
        (void) signal(SIGINT, cleanup);
52
        (void) signal(SIGTERM, cleanup);
53
        (void) signal(SIGHUP, cleanup);
54

    
55
        fd = open(device, O_RDONLY);
56

    
57
        if (fd < 1) {
58
            fprintf(stderr, "%s: cannot open %s: %s\n", program_name, device, strerror(errno));
59
            return 2;
60
        }
61

    
62
        while (ioctl(fd, FE_GET_PROPERTY, &properties_request) == 0) {
63
            unsigned scale = properties[0].u.st.stat[0].scale;
64
            time_t current_time = time(NULL);
65
            char current_time_string[1024];
66
            
67
            assert(strftime(current_time_string, sizeof(current_time_string), "%H:%M:%S", localtime(&current_time)) > 0);
68

    
69
            printf("%s SNR: new=", current_time_string);
70

    
71
            switch (scale) {
72
            case FE_SCALE_NOT_AVAILABLE:
73
                printf("N/A");
74
                break;
75
            case FE_SCALE_DECIBEL:
76
                printf("%.3f dB", properties[0].u.st.stat[0].svalue / 1000.0);
77
                break;
78
            case FE_SCALE_RELATIVE:
79
                printf("%.1f %%", properties[0].u.st.stat[0].uvalue / 65535.0 * 100);
80
                break;
81
            default:
82
                printf("(determination failed, unexpected scale %d)\n", scale);
83
            }
84
            
85
            {
86
                uint16_t snr; 
87

    
88
                if (ioctl(fd, FE_READ_SNR, &snr) == 0) {
89
                    printf(", old=%d\n", snr);
90
                }
91
                else {
92
                    printf(", old=(failed, error on ioctl FE_READ_SNR: %s)\n", strerror(errno));
93
                }
94
            }
95
            
96
            sleep(1);
97
        }
98

    
99
        fprintf(stderr, "%s: error on ioctl FE_GET_PROPERTY: %s\n", program_name, strerror(errno));
100
        
101
        cleanup(0);
102
    }
103

    
104
    return 0;
105
}
(15-15/17)