1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include "libbb.h"
38#include <linux/rfkill.h>
39
40enum {
41 OPT_b = (1 << 0),
42 OPT_u = (1 << 1),
43 OPT_l = (1 << 2),
44};
45
46int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47int rfkill_main(int argc UNUSED_PARAM, char **argv)
48{
49 struct rfkill_event event;
50 const char *rf_name;
51 int rf_fd;
52 int mode;
53 int rf_type;
54 int rf_idx;
55 unsigned rf_opt = 0;
56
57 argv++;
58
59 if (!argv[0] || (argv[1] && argv[2]))
60 bb_show_usage();
61
62 mode = O_RDWR | O_NONBLOCK;
63 rf_name = argv[1];
64 if (strcmp(argv[0], "list") == 0) {
65 rf_opt |= OPT_l;
66 mode = O_RDONLY | O_NONBLOCK;
67 } else if (strcmp(argv[0], "block") == 0 && rf_name) {
68 rf_opt |= OPT_b;
69 } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
70 rf_opt |= OPT_u;
71 } else
72 bb_show_usage();
73
74 rf_type = RFKILL_TYPE_ALL;
75 rf_idx = -1;
76 if (rf_name) {
77 static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
78 if (strcmp(rf_name, "wifi") == 0)
79 rf_name = "wlan";
80 if (strcmp(rf_name, "ultrawideband") == 0)
81 rf_name = "uwb";
82 rf_type = index_in_strings(rfkill_types, rf_name);
83 if (rf_type < 0) {
84 rf_idx = xatoi_positive(rf_name);
85 }
86 }
87
88 rf_fd = device_open("/dev/rfkill", mode);
89 if (rf_fd < 0)
90 bb_simple_perror_msg_and_die("/dev/rfkill");
91
92 if (rf_opt & OPT_l) {
93 while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
94 parser_t *parser;
95 char *tokens[2];
96 char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
97 char *name, *type;
98
99 if (rf_type && rf_type != event.type && rf_idx < 0) {
100 continue;
101 }
102
103 if (rf_idx >= 0 && event.idx != rf_idx) {
104 continue;
105 }
106
107 name = NULL;
108 type = NULL;
109 sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
110 parser = config_open2(rf_sysfs, fopen_for_read);
111 while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
112 if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
113 name = xstrdup(tokens[1]);
114 continue;
115 }
116 if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
117 type = xstrdup(tokens[1]);
118 continue;
119 }
120 }
121 config_close(parser);
122
123 printf("%u: %s: %s\n", event.idx, name, type);
124 printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
125 printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
126 free(name);
127 free(type);
128 }
129 } else {
130 memset(&event, 0, sizeof(event));
131 if (rf_type >= 0) {
132 event.type = rf_type;
133 event.op = RFKILL_OP_CHANGE_ALL;
134 }
135
136 if (rf_idx >= 0) {
137 event.idx = rf_idx;
138 event.op = RFKILL_OP_CHANGE;
139 }
140
141
142 event.soft = (rf_opt & OPT_b);
143
144 xwrite(rf_fd, &event, sizeof(event));
145 }
146
147 return EXIT_SUCCESS;
148}
149