radioctl.c revision 1.3 1 /* $NetBSD: radioctl.c,v 1.3 2002/01/02 20:23:01 briggs Exp $ */
2 /* $OpenBSD: radioctl.c,v 1.5 2001/12/18 18:42:19 mickey Exp $ */
3 /* $RuOBSD: radioctl.c,v 1.4 2001/10/20 18:09:10 pva Exp $ */
4
5 /*
6 * Copyright (c) 2001 Vladimir Popov <jumbo (at) narod.ru>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/ioctl.h>
31 #include <sys/radioio.h>
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #define RADIO_ENV "RADIODEVICE"
41 #define RADIODEVICE "/dev/radio"
42
43 const char *varname[] = {
44 "search",
45 #define OPTION_SEARCH 0x00
46 "volume",
47 #define OPTION_VOLUME 0x01
48 "frequency",
49 #define OPTION_FREQUENCY 0x02
50 "mute",
51 #define OPTION_MUTE 0x03
52 "reference",
53 #define OPTION_REFERENCE 0x04
54 "mono",
55 #define OPTION_MONO 0x05
56 "stereo",
57 #define OPTION_STEREO 0x06
58 "sensitivity"
59 #define OPTION_SENSITIVITY 0x07
60 };
61
62 #define OPTION_NONE ~0u
63 #define VALUE_NONE ~0u
64
65 struct opt_t {
66 char *string;
67 int option;
68 int sign;
69 #define SIGN_NONE 0
70 #define SIGN_PLUS 1
71 #define SIGN_MINUS -1
72 u_int32_t value;
73 };
74
75 const char *onchar = "on";
76 #define ONCHAR_LEN 2
77 const char *offchar = "off";
78 #define OFFCHAR_LEN 3
79
80 static struct radio_info ri;
81
82 static int parse_opt(char *, struct opt_t *);
83
84 static void print_vars(int);
85 static void do_ioctls(int, struct opt_t *, int);
86
87 static void print_value(int);
88 static void change_value(const struct opt_t);
89 static void update_value(int, u_long *, u_long);
90
91 static void warn_unsupported(int);
92 static void usage(void);
93
94 static void show_verbose(const char *, int);
95 static void show_int_val(u_long, const char *, char *, int);
96 static void show_float_val(float, const char *, char *, int);
97 static void show_char_val(const char *, const char *, int);
98 static int str_to_opt(const char *);
99 static u_long str_to_long(char *, int);
100
101 /*
102 * Control behavior of a FM tuner - set frequency, volume etc
103 */
104 int
105 main(int argc, char **argv)
106 {
107 struct opt_t opt;
108
109 char *radiodev = NULL;
110 int rd = -1;
111
112 int optchar;
113 char *param = NULL;
114
115 int show_vars = 0;
116 int set_param = 0;
117 int silent = 0;
118
119 int optv = 0;
120
121 if (argc < 2) {
122 usage();
123 exit(1);
124 }
125
126 radiodev = getenv(RADIO_ENV);
127 if (radiodev == NULL)
128 radiodev = RADIODEVICE;
129
130 while ((optchar = getopt(argc, argv, "af:nw:")) != -1) {
131 switch (optchar) {
132 case 'a':
133 show_vars = 1;
134 optv = 1;
135 break;
136 case 'f':
137 radiodev = optarg;
138 optv = 2;
139 break;
140 case 'n':
141 silent = 1;
142 optv = 1;
143 break;
144 case 'w':
145 set_param = 1;
146 param = optarg;
147 optv = 2;
148 break;
149 default:
150 usage();
151 /* NOTREACHED */
152 }
153
154 argc -= optv;
155 argv += optv;
156 }
157
158 rd = open(radiodev, O_RDONLY);
159 if (rd < 0)
160 err(1, "%s open error", radiodev);
161
162 if (ioctl(rd, RIOCGINFO, &ri) < 0)
163 err(1, "RIOCGINFO");
164
165 if (argc > 1)
166 if (parse_opt(*(argv + 1), &opt)) {
167 show_verbose(varname[opt.option], silent);
168 print_value(opt.option);
169 free(opt.string);
170 putchar('\n');
171 }
172
173 if (set_param)
174 if (parse_opt(param, &opt))
175 do_ioctls(rd, &opt, silent);
176
177 if (show_vars)
178 print_vars(silent);
179
180 if (close(rd) < 0)
181 warn("%s close error", radiodev);
182
183 return 0;
184 }
185
186 static void
187 usage(void)
188 {
189 const char *progname = getprogname();
190
191 fprintf(stderr, "Usage:\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n",
192 progname, "[-n] variable ...",
193 progname, "[-n] -w name=value ...",
194 progname, "[-n] -a",
195 progname, "[-n] -f file");
196 exit(1);
197 }
198
199 static void
200 show_verbose(const char *nick, int silent)
201 {
202 if (!silent)
203 printf("%s=", nick);
204 }
205
206 static void
207 warn_unsupported(int optval)
208 {
209 warnx("driver does not support `%s'", varname[optval]);
210 }
211
212 static void
213 do_ioctls(int fd, struct opt_t *o, int silent)
214 {
215 int oval;
216
217 if (fd < 0 || o == NULL)
218 return;
219
220 if (o->option == OPTION_SEARCH && !(ri.caps & RADIO_CAPS_HW_SEARCH)) {
221 warn_unsupported(o->option);
222 return;
223 }
224
225 oval = o->option == OPTION_SEARCH ? OPTION_FREQUENCY : o->option;
226 if (!silent)
227 printf("%s: ", varname[oval]);
228
229 print_value(o->option);
230 printf(" -> ");
231
232 if (o->option == OPTION_SEARCH) {
233
234 if (ioctl(fd, RIOCSSRCH, &o->value) < 0) {
235 warn("RIOCSSRCH");
236 return;
237 }
238
239 } else {
240
241 change_value(*o);
242 if (ioctl(fd, RIOCSINFO, &ri) < 0) {
243 warn("RIOCSINFO");
244 return;
245 }
246
247 }
248
249 if (ioctl(fd, RIOCGINFO, &ri) < 0) {
250 warn("RIOCGINFO");
251 return;
252 }
253
254 print_value(o->option);
255 putchar('\n');
256 }
257
258 static void
259 change_value(const struct opt_t o)
260 {
261 int unsupported = 0;
262
263 if (o.value == VALUE_NONE)
264 return;
265
266 switch (o.option) {
267 case OPTION_VOLUME:
268 update_value(o.sign, (u_long *)&ri.volume, o.value);
269 break;
270 case OPTION_FREQUENCY:
271 update_value(o.sign, (u_long *)&ri.freq, o.value);
272 break;
273 case OPTION_REFERENCE:
274 if (ri.caps & RADIO_CAPS_REFERENCE_FREQ)
275 update_value(o.sign, (u_long *)&ri.rfreq, o.value);
276 else
277 unsupported++;
278 break;
279 case OPTION_MONO:
280 /* FALLTHROUGH */
281 case OPTION_STEREO:
282 if (ri.caps & RADIO_CAPS_SET_MONO)
283 ri.stereo = o.option == OPTION_MONO ? !o.value : o.value;
284 else
285 unsupported++;
286 break;
287 case OPTION_SENSITIVITY:
288 if (ri.caps & RADIO_CAPS_LOCK_SENSITIVITY)
289 update_value(o.sign, (u_long *)&ri.lock, o.value);
290 else
291 unsupported++;
292 break;
293 case OPTION_MUTE:
294 ri.mute = o.value;
295 break;
296 }
297
298 if ( unsupported )
299 warn_unsupported(o.option);
300 }
301
302 /*
303 * Convert string to integer representation of a parameter
304 */
305 static int
306 str_to_opt(const char *topt)
307 {
308 int res, toptlen, varlen, len, varsize;
309
310 if (topt == NULL || *topt == '\0')
311 return OPTION_NONE;
312
313 varsize = sizeof(varname) / sizeof(varname[0]);
314 toptlen = strlen(topt);
315
316 for (res = 0; res < varsize; res++) {
317 varlen = strlen(varname[res]);
318 len = toptlen > varlen ? toptlen : varlen;
319 if (strncmp(topt, varname[res], len) == 0)
320 return res;
321 }
322
323 warnx("bad name `%s'", topt);
324 return OPTION_NONE;
325 }
326
327 static void
328 update_value(int sign, u_long *value, u_long update)
329 {
330 switch (sign) {
331 case SIGN_NONE:
332 *value = update;
333 break;
334 case SIGN_PLUS:
335 *value += update;
336 break;
337 case SIGN_MINUS:
338 *value -= update;
339 break;
340 }
341 }
342
343 /*
344 * Convert string to unsigned integer
345 */
346 static u_long
347 str_to_long(char *str, int optval)
348 {
349 u_long val;
350
351 if (str == NULL || *str == '\0')
352 return VALUE_NONE;
353
354 if (optval == OPTION_FREQUENCY)
355 val = (u_long)1000 * atof(str);
356 else
357 val = (u_long)strtol(str, (char **)NULL, 10);
358
359 return val;
360 }
361
362 /*
363 * parse string s into struct opt_t
364 * return true on success, false on failure
365 */
366 static int
367 parse_opt(char *s, struct opt_t *o) {
368 const char *badvalue = "bad value `%s'";
369 char *topt = NULL;
370 int slen, optlen;
371
372 if (s == NULL || *s == '\0' || o == NULL)
373 return 0;
374
375 o->string = NULL;
376 o->option = OPTION_NONE;
377 o->value = VALUE_NONE;
378 o->sign = SIGN_NONE;
379
380 slen = strlen(s);
381 optlen = strcspn(s, "=");
382
383 /* Set only o->optval, the rest is missing */
384 if (slen == optlen) {
385 o->option = str_to_opt(s);
386 return o->option == OPTION_NONE ? 0 : 1;
387 }
388
389 if (optlen > slen - 2) {
390 warnx(badvalue, s);
391 return 0;
392 }
393
394 slen -= ++optlen;
395
396 if ((topt = (char *)malloc(optlen)) == NULL) {
397 warn("memory allocation error");
398 return 0;
399 }
400 strlcpy(topt, s, optlen);
401
402 if ((o->option = str_to_opt(topt)) == OPTION_NONE) {
403 free(topt);
404 return 0;
405 }
406 o->string = topt;
407
408 topt = &s[optlen];
409 switch (*topt) {
410 case '+':
411 case '-':
412 o->sign = (*topt == '+') ? SIGN_PLUS : SIGN_MINUS;
413 o->value = str_to_long(&topt[1], o->option);
414 break;
415 case 'o':
416 if (strncmp(topt, offchar,
417 slen > OFFCHAR_LEN ? slen : OFFCHAR_LEN) == 0)
418 o->value = 0;
419 else if (strncmp(topt, onchar,
420 slen > ONCHAR_LEN ? slen : ONCHAR_LEN) == 0)
421 o->value = 1;
422 break;
423 case 'u':
424 if (strncmp(topt, "up", slen > 2 ? slen : 2) == 0)
425 o->value = 1;
426 break;
427 case 'd':
428 if (strncmp(topt, "down", slen > 4 ? slen : 4) == 0)
429 o->value = 0;
430 break;
431 default:
432 if (*topt > 47 && *topt < 58)
433 o->value = str_to_long(topt, o->option);
434 break;
435 }
436
437 if (o->value == VALUE_NONE) {
438 warnx(badvalue, topt);
439 return 0;
440 }
441
442 return 1;
443 }
444
445 /*
446 * Print current value of the parameter.
447 */
448 static void
449 print_value(int optval)
450 {
451 if (optval == OPTION_NONE)
452 return;
453
454 switch (optval) {
455 case OPTION_SEARCH:
456 /* FALLTHROUGH */
457 case OPTION_FREQUENCY:
458 printf("%.2fMHz", (float)ri.freq / 1000.);
459 break;
460 case OPTION_REFERENCE:
461 printf("%ukHz", ri.rfreq);
462 break;
463 case OPTION_SENSITIVITY:
464 printf("%umkV", ri.lock);
465 break;
466 case OPTION_MUTE:
467 printf(ri.mute ? onchar : offchar);
468 break;
469 case OPTION_MONO:
470 printf(ri.stereo ? offchar : onchar);
471 break;
472 case OPTION_STEREO:
473 printf(ri.stereo ? onchar : offchar);
474 break;
475 case OPTION_VOLUME:
476 default:
477 printf("%u", ri.volume);
478 break;
479 }
480 }
481
482 static void
483 show_int_val(u_long val, const char *nick, char *append, int silent)
484 {
485 show_verbose(nick, silent);
486 printf("%lu%s\n", val, append);
487 }
488
489 static void
490 show_float_val(float val, const char *nick, char *append, int silent)
491 {
492 show_verbose(nick, silent);
493 printf("%.2f%s\n", val, append);
494 }
495
496 static void
497 show_char_val(const char *val, const char *nick, int silent)
498 {
499 show_verbose(nick, silent);
500 printf("%s\n", val);
501 }
502
503 /*
504 * Print all available parameters
505 */
506 static void
507 print_vars(int silent)
508 {
509 show_int_val(ri.volume, varname[OPTION_VOLUME], "", silent);
510 show_float_val((float)ri.freq / 1000., varname[OPTION_FREQUENCY],
511 "MHz", silent);
512 show_char_val(ri.mute ? onchar : offchar, varname[OPTION_MUTE], silent);
513
514 if (ri.caps & RADIO_CAPS_REFERENCE_FREQ)
515 show_int_val(ri.rfreq, varname[OPTION_REFERENCE], "kHz", silent);
516 if (ri.caps & RADIO_CAPS_LOCK_SENSITIVITY)
517 show_int_val(ri.lock, varname[OPTION_SENSITIVITY], "mkV", silent);
518
519 if (ri.caps & RADIO_CAPS_DETECT_SIGNAL) {
520 show_verbose("signal", silent);
521 printf("%s\n", ri.info & RADIO_INFO_SIGNAL ? onchar : offchar);
522 }
523 if (ri.caps & RADIO_CAPS_DETECT_STEREO) {
524 show_verbose(varname[OPTION_STEREO], silent);
525 printf("%s\n", ri.info & RADIO_INFO_STEREO ? onchar : offchar);
526 }
527
528 if (!silent)
529 puts("card capabilities:");
530 if (ri.caps & RADIO_CAPS_SET_MONO)
531 puts("\tmanageable mono/stereo");
532 if (ri.caps & RADIO_CAPS_HW_SEARCH)
533 puts("\thardware search");
534 if (ri.caps & RADIO_CAPS_HW_AFC)
535 puts("\thardware AFC");
536 }
537