fmtools version 1.0.2.
[fmtools.git] / fmscan.c
blob814c5be3dbd4cfd302b7ab6e1a809f6baf736687
1 /* scan.c - v4l radio band scanner using signal strength
3 Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <math.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include "videodev.h"
30 #include "version.h"
32 #define TRIES 25 /* get 25 samples */
33 #define LOCKTIME 400000 /* wait 400ms for card to lock on */
34 #define SAMPLEDELAY 15000 /* wait 15ms between samples */
35 #define THRESHOLD .5 /* minimum acceptable signal % */
37 void help(char *prog)
39 printf("fmtools fmscan version %s\n\n", FMT_VERSION);
40 printf("usage: %s [-h] [-d <dev>] [-s <freq>] [-e <freq>] [-i <freq>] [-t <%%>]\n\n", prog);
42 printf("Auxiliary program to scan a frequency band for radio stations.\n\n");
44 printf(" -h - display this help\n");
45 printf(" -d <dev> - select device (default: /dev/radio0)\n");
46 printf(" -s <freq> - set start of scanning range to <freq>\n");
47 printf(" -e <freq> - set end of scanning range to <freq>\n");
48 printf(" -i <freq> - set increment value between channels to <freq>\n");
49 printf(" -t <%%> - set signal strength percentage to lock onto <%%>\n");
50 printf(" <freq> - a value in the format nnn.nn (MHz)\n");
52 exit(0);
55 int main(int argc, char **argv)
57 int fd, ret, i, tries = TRIES;
58 struct video_tuner vt;
59 float perc, begval, incval, endval, threshold;
60 long lowf, highf, freq, totsig, incr, fact;
61 char *progname, *dev = NULL;
63 progname = argv[0]; /* getopt munges argv[] later */
65 /* USA defaults */
66 begval = 87.9; /* start at 87.9 MHz */
67 incval = 0.20; /* increment 0.2 MHz */
68 endval = 107.9; /* stop at 107.9 MHz */
70 threshold = THRESHOLD;
72 while ((i = getopt(argc, argv, "+e:hi:s:d:t:")) != EOF) {
73 switch (i) {
74 case 'd':
75 dev = strdup(optarg);
76 break;
77 case 'e':
78 endval = atof(optarg);
79 break;
80 case 'i':
81 incval = atof(optarg);
82 break;
83 case 's':
84 begval = atof(optarg);
85 break;
86 case 't':
87 threshold = atof(optarg)/100.;
88 break;
89 case 'h':
90 default:
91 help(progname);
92 break;
96 if (!dev)
97 dev = strdup("/dev/radio0"); /* default */
99 fd = open(dev, O_RDONLY);
100 if (fd < 0) {
101 fprintf(stderr, "Unable to open %s: %s\n", dev, strerror(errno));
102 exit(1);
105 vt.tuner = 0;
106 ret = ioctl(fd, VIDIOCGTUNER, &vt); /* get initial info */
107 if (ret < 0) {
108 perror("ioctl VIDIOCGTUNER");
109 exit(1);
112 if ((vt.flags & VIDEO_TUNER_LOW) == 0)
113 fact = 16;
114 else
115 fact = 16000;
117 /* cope with bizarre things from atof() like 95.099998 */
118 lowf = fact * (ceil(rint(begval * 10)) / 10);
119 highf = fact * (ceil(rint(endval * 10)) / 10);
121 incr = fact * incval;
123 printf("Scanning range: %2.1f - %2.1f MHz (%2.1f MHz increments)...\n",
124 begval, endval, incval);
126 for (freq = lowf; freq <= highf; freq += incr) {
127 ret = ioctl(fd, VIDIOCSFREQ, &freq); /* tune */
129 if (ret < 0) {
130 perror("ioctl VIDIOCSFREQ");
131 exit(1);
134 printf("%2.1f:\r", (freq / (double) fact));
135 fflush(stdout);
136 usleep(LOCKTIME); /* let it lock on */
138 totsig = 0;
139 for (i = 1; i < tries+1; i++) {
140 vt.tuner = 0;
141 ret = ioctl(fd, VIDIOCGTUNER, &vt); /* get info */
142 if (ret < 0) {
143 perror("ioctl VIDIOCGTUNER");
144 exit(1);
147 totsig += vt.signal;
148 perc = (totsig / (65535.0 * i));
150 printf("%2.1f: checking: %3.1f%% (%d/%d) \r",
151 (freq / (double) fact), perc * 100.0, i, tries);
152 fflush(stdout);
153 usleep(SAMPLEDELAY);
156 /* clean up the display */
157 printf(" \r");
159 perc = (totsig / (65535.0 * tries));
161 if (perc > threshold)
162 printf("%2.1f: %3.1f%% \n",
163 (freq / (double) fact), perc * 100.0);
166 close(fd);
167 return 0;