1 /* rfkill.c - Enable/disable wireless devices.
3 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
8 USE_RFKILL(NEWTOY(rfkill, "<1>2", TOYFLAG_USR|TOYFLAG_SBIN))
14 Usage: rfkill COMMAND [DEVICE]
16 Enable/disable wireless devices.
19 list [DEVICE] List current state
20 block DEVICE Disable device
21 unblock DEVICE Enable device
23 DEVICE is an index number, or one of:
24 all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm.
29 #include <linux/rfkill.h>
31 void rfkill_main(void)
33 struct rfkill_event rfevent;
34 int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL;
35 char **optargs = toys.optargs;
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);
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
52 if (!*++optargs) error_exit("'%s' needs IDENTIFIER", optargs[-1]);
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;
59 // Perform requested action
60 fd = xopen("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK);
63 memset(&rfevent, 0, sizeof(rfevent));
64 rfevent.soft = tvar == 1;
67 rfevent.op = RFKILL_OP_CHANGE;
70 rfevent.op = RFKILL_OP_CHANGE_ALL;
72 xwrite(fd, &rfevent, sizeof(rfevent));
75 while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) {
76 char *line, *name = 0, *type = 0;
79 if ((tid > 0 && tid != rfevent.type) || (idx != -1 && idx != rfevent.idx))
82 sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx);
83 tvar = xopen(toybuf, O_RDONLY);
84 while ((line = get_line(tvar))) {
87 if (strstart(&s, "RFKILL_NAME=")) name = xstrdup(s);
88 else if (strstart(&s, "RFKILL_TYPE=")) type = xstrdup(s);
94 xprintf("%u: %s: %s\n", rfevent.idx, name, type);
95 xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no");
96 xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no");