Mercurial > hg > toybox
comparison toys/pending/rfkill.c @ 1344:788c6c097fa2 draft
Cleanup pass on rfkill.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 09 Jun 2014 07:07:33 -0500 |
parents | 8868e482963e |
children |
comparison
equal
deleted
inserted
replaced
1343:2538fa09b1b1 | 1344:788c6c097fa2 |
---|---|
9 | 9 |
10 config RFKILL | 10 config RFKILL |
11 bool "rfkill" | 11 bool "rfkill" |
12 default n | 12 default n |
13 help | 13 help |
14 Usage: rfkill COMMAND | 14 Usage: rfkill COMMAND [DEVICE] |
15 | 15 |
16 Enable/disable wireless devices. | 16 Enable/disable wireless devices. |
17 | 17 |
18 Commands: | 18 Commands: |
19 list [IDENTIFIER] List current state | 19 list [DEVICE] List current state |
20 block IDENTIFIER Disable device | 20 block DEVICE Disable device |
21 unblock IDENTIFIER Enable device | 21 unblock DEVICE Enable device |
22 | 22 |
23 where IDENTIFIER is the index no. of an rfkill switch or one of: | 23 DEVICE is an index number, or one of: |
24 <idx> all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm. | 24 all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm. |
25 */ | 25 */ |
26 | |
26 #define FOR_rfkill | 27 #define FOR_rfkill |
27 #include "toys.h" | 28 #include "toys.h" |
28 #include <linux/rfkill.h> | 29 #include <linux/rfkill.h> |
29 | |
30 struct arglist { | |
31 char *name; | |
32 int idx; | |
33 }; | |
34 | |
35 static int getidx(char ***argv, struct arglist *list) | |
36 { | |
37 struct arglist *alist; | |
38 | |
39 if (!**argv) return -1; | |
40 for (alist = list; alist->name; alist++) | |
41 if (!strcmp(**argv, alist->name)) { | |
42 *argv += 1; | |
43 return alist->idx; | |
44 } | |
45 return -1; | |
46 } | |
47 | 30 |
48 void rfkill_main(void) | 31 void rfkill_main(void) |
49 { | 32 { |
50 struct rfkill_event rfevent; | 33 struct rfkill_event rfevent; |
51 int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL; | 34 int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL; |
52 char **optargs = toys.optargs; | 35 char **optargs = toys.optargs; |
53 struct arglist cmds[] = {{"list", 0}, {"block", 1}, | |
54 {"unblock", 2}, {NULL, -1}}; | |
55 | 36 |
56 if (((tvar = getidx(&optargs, cmds)) < 0) || ((tvar) && !*optargs)) { | 37 // Parse command line options |
57 toys.exithelp = 1; | 38 for (tvar = 0; tvar < 3; tvar++) |
58 (tvar < 0) ? error_exit("cmd missmatch") | 39 if (!strcmp((char *[]){"list", "block", "unblock"}[tvar], *optargs)) break; |
59 : error_exit("%s idx missing", *toys.optargs); | 40 if (tvar == 3) error_exit("unknown cmd '%s'", *optargs); |
41 if (tvar) { | |
42 int i; | |
43 struct arglist { | |
44 char *name; | |
45 int idx; | |
46 } rftypes[] = {{"all", RFKILL_TYPE_ALL}, {"wifi", RFKILL_TYPE_WLAN}, | |
47 {"wlan", RFKILL_TYPE_WLAN}, {"bluetooth", RFKILL_TYPE_BLUETOOTH}, | |
48 {"uwb", RFKILL_TYPE_UWB}, {"ultrawideband", RFKILL_TYPE_UWB}, | |
49 {"wimax", RFKILL_TYPE_WIMAX}, {"wwan", RFKILL_TYPE_WWAN}, | |
50 {"gps", RFKILL_TYPE_GPS}, {"fm", 7}}; // RFKILL_TYPE_FM = 7 | |
51 | |
52 if (!*++optargs) error_exit("'%s' needs IDENTIFIER"); | |
53 for (i = 0; i < ARRAY_LEN(rftypes); i++) | |
54 if (!strcmp(rftypes[i].name, *optargs)) break; | |
55 if (i == ARRAY_LEN(rftypes)) idx = atolx_range(*optargs, 0, INT_MAX); | |
56 else tid = rftypes[i].idx; | |
60 } | 57 } |
61 if (*optargs) { | |
62 struct arglist rftypes[] = {{"all", RFKILL_TYPE_ALL}, | |
63 {"wifi", RFKILL_TYPE_WLAN}, {"wlan", RFKILL_TYPE_WLAN}, | |
64 {"bluetooth", RFKILL_TYPE_BLUETOOTH}, {"uwb", RFKILL_TYPE_UWB}, | |
65 {"ultrawideband", RFKILL_TYPE_UWB}, {"wimax", RFKILL_TYPE_WIMAX}, | |
66 {"wwan", RFKILL_TYPE_WWAN}, {"gps", RFKILL_TYPE_GPS}, | |
67 {"fm", 7}, {NULL, -1}}; // RFKILL_TYPE_FM = 7 | |
68 | 58 |
69 if ((tid = getidx(&optargs, rftypes)) == -1) | 59 // Perform requested action |
70 idx = atolx_range(*optargs, 0, INT_MAX); | 60 fd = xopen("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK); |
71 } | |
72 fd = xcreate("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK, 0600); | |
73 if (tvar) { | 61 if (tvar) { |
62 // block/unblock | |
74 memset(&rfevent, 0, sizeof(rfevent)); | 63 memset(&rfevent, 0, sizeof(rfevent)); |
75 rfevent.soft = (tvar & 1); | 64 rfevent.soft = tvar == 1; |
76 if (tid != -1) { | 65 if (idx >= 0) { |
66 rfevent.idx = idx; | |
67 rfevent.op = RFKILL_OP_CHANGE; | |
68 } else { | |
77 rfevent.type = tid; | 69 rfevent.type = tid; |
78 rfevent.op = RFKILL_OP_CHANGE_ALL; | 70 rfevent.op = RFKILL_OP_CHANGE_ALL; |
79 } else if (idx >= 0) { | |
80 rfevent.idx = idx; | |
81 rfevent.op = RFKILL_OP_CHANGE; | |
82 } | 71 } |
83 xwrite(fd, &rfevent, sizeof(rfevent)); | 72 xwrite(fd, &rfevent, sizeof(rfevent)); |
84 } else { // show list. | 73 } else { |
74 // show list. | |
85 while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) { | 75 while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) { |
86 char *line = NULL, *name = NULL, *type = NULL; | 76 char *line, *name = 0, *type = 0; |
87 | 77 |
88 // filter of list items. | 78 // filter list items |
89 if (((tid > 0) && (tid != rfevent.type)) | 79 if ((tid > 0 && tid != rfevent.type) || (idx != -1 && idx != rfevent.idx)) |
90 || ((idx != -1) && (idx != rfevent.idx))) continue; | 80 continue; |
81 | |
91 sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx); | 82 sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx); |
92 tvar = xopen(toybuf, O_RDONLY); | 83 tvar = xopen(toybuf, O_RDONLY); |
93 while ((line = get_line(tvar))) { | 84 while ((line = get_line(tvar))) { |
94 if (!strncmp(line, "RFKILL_NAME", strlen("RFKILL_NAME"))) | 85 char *s = line; |
95 name = xstrdup(strchr(line, '=')+1); | 86 |
96 else if (!strncmp(line, "RFKILL_TYPE", strlen("RFKILL_TYPE"))) | 87 if (strstart(&s, "RFKILL_NAME=")) name = xstrdup(s); |
97 type = xstrdup(strchr(line, '=')+1); | 88 else if (strstart(&s, "RFKILL_TYPE=")) type = xstrdup(s); |
89 | |
98 free(line); | 90 free(line); |
99 } | 91 } |
92 xclose(tvar); | |
93 | |
100 xprintf("%u: %s: %s\n", rfevent.idx, name, type); | 94 xprintf("%u: %s: %s\n", rfevent.idx, name, type); |
101 xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no"); | 95 xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no"); |
102 xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no"); | 96 xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no"); |
103 xclose(tvar); | 97 free(name); |
104 free(name), free(type); | 98 free(type); |
105 } | 99 } |
106 } | 100 } |
107 xclose(fd); | 101 xclose(fd); |
108 } | 102 } |