Mercurial > hg > toybox
annotate toys/other/rfkill.c @ 1776:7bf68329eb3b draft default tip
Repository switched to git at https://github.com/landley/toybox
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 09 Apr 2015 02:28:32 -0500 |
parents | 57f2a26fa92c |
children |
rev | line source |
---|---|
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
1 /* rfkill.c - Enable/disable wireless devices. |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
2 * |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
3 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com> |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com> |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
5 * |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
6 * No Standard |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
7 |
1775
57f2a26fa92c
To ensure that toybox can be installed alongside busybox without
Paul Barker <paul@paulbarker.me.uk>
parents:
1718
diff
changeset
|
8 USE_RFKILL(NEWTOY(rfkill, "<1>2", TOYFLAG_USR|TOYFLAG_SBIN)) |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
9 |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
10 config RFKILL |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
11 bool "rfkill" |
1346 | 12 default y |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
13 help |
1344 | 14 Usage: rfkill COMMAND [DEVICE] |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
15 |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
16 Enable/disable wireless devices. |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
17 |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
18 Commands: |
1344 | 19 list [DEVICE] List current state |
20 block DEVICE Disable device | |
21 unblock DEVICE Enable device | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
22 |
1344 | 23 DEVICE is an index number, or one of: |
24 all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm. | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
25 */ |
1344 | 26 |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
27 #define FOR_rfkill |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
28 #include "toys.h" |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
29 #include <linux/rfkill.h> |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
30 |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
31 void rfkill_main(void) |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
32 { |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
33 struct rfkill_event rfevent; |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
34 int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL; |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
35 char **optargs = toys.optargs; |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
36 |
1344 | 37 // Parse command line options |
38 for (tvar = 0; tvar < 3; tvar++) | |
39 if (!strcmp((char *[]){"list", "block", "unblock"}[tvar], *optargs)) break; | |
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 | |
1718
3ac823675413
Fix several printf_format warnings.
Rob Landley <rob@landley.net>
parents:
1346
diff
changeset
|
52 if (!*++optargs) error_exit("'%s' needs IDENTIFIER", optargs[-1]); |
1344 | 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; | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
57 } |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
58 |
1344 | 59 // Perform requested action |
60 fd = xopen("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK); | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
61 if (tvar) { |
1344 | 62 // block/unblock |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
63 memset(&rfevent, 0, sizeof(rfevent)); |
1344 | 64 rfevent.soft = tvar == 1; |
65 if (idx >= 0) { | |
66 rfevent.idx = idx; | |
67 rfevent.op = RFKILL_OP_CHANGE; | |
68 } else { | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
69 rfevent.type = tid; |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
70 rfevent.op = RFKILL_OP_CHANGE_ALL; |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
71 } |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
72 xwrite(fd, &rfevent, sizeof(rfevent)); |
1344 | 73 } else { |
74 // show list. | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
75 while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) { |
1344 | 76 char *line, *name = 0, *type = 0; |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
77 |
1344 | 78 // filter list items |
79 if ((tid > 0 && tid != rfevent.type) || (idx != -1 && idx != rfevent.idx)) | |
80 continue; | |
81 | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
82 sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
83 tvar = xopen(toybuf, O_RDONLY); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
84 while ((line = get_line(tvar))) { |
1344 | 85 char *s = line; |
86 | |
87 if (strstart(&s, "RFKILL_NAME=")) name = xstrdup(s); | |
88 else if (strstart(&s, "RFKILL_TYPE=")) type = xstrdup(s); | |
89 | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
90 free(line); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
91 } |
1344 | 92 xclose(tvar); |
93 | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
94 xprintf("%u: %s: %s\n", rfevent.idx, name, type); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
95 xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no"); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
96 xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no"); |
1344 | 97 free(name); |
98 free(type); | |
1307
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
99 } |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
100 } |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
101 xclose(fd); |
8868e482963e
rfkill - enable/disable the radio devices
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
102 } |