aboutsummaryrefslogtreecommitdiff
path: root/toys/net/rfkill.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-07-03 16:05:12 -0500
committerRob Landley <rob@landley.net>2016-07-03 16:05:12 -0500
commit746e565c097ed311ebb65842ac8a6527535993a7 (patch)
tree5ba0d6003059d4e0bb900af21071428f104e6c03 /toys/net/rfkill.c
parent0713e4b89f7c7339e060d54db1b877b709d985a1 (diff)
downloadtoybox-746e565c097ed311ebb65842ac8a6527535993a7.tar.gz
Promote netsat, and move ifconfig, netcat, and rfkill to new toys/net directory.
Diffstat (limited to 'toys/net/rfkill.c')
-rw-r--r--toys/net/rfkill.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/toys/net/rfkill.c b/toys/net/rfkill.c
new file mode 100644
index 00000000..36fe320d
--- /dev/null
+++ b/toys/net/rfkill.c
@@ -0,0 +1,102 @@
+/* rfkill.c - Enable/disable wireless devices.
+ *
+ * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
+ * Copyright 2014 Kyungwan Han <asura321@gmail.com>
+ *
+ * No Standard
+
+USE_RFKILL(NEWTOY(rfkill, "<1>2", TOYFLAG_USR|TOYFLAG_SBIN))
+
+config RFKILL
+ bool "rfkill"
+ default y
+ help
+ Usage: rfkill COMMAND [DEVICE]
+
+ Enable/disable wireless devices.
+
+ Commands:
+ list [DEVICE] List current state
+ block DEVICE Disable device
+ unblock DEVICE Enable device
+
+ DEVICE is an index number, or one of:
+ all, wlan(wifi), bluetooth, uwb(ultrawideband), wimax, wwan, gps, fm.
+*/
+
+#define FOR_rfkill
+#include "toys.h"
+#include <linux/rfkill.h>
+
+void rfkill_main(void)
+{
+ struct rfkill_event rfevent;
+ int fd, tvar, idx = -1, tid = RFKILL_TYPE_ALL;
+ char **optargs = toys.optargs;
+
+ // Parse command line options
+ for (tvar = 0; tvar < 3; tvar++)
+ if (!strcmp((char *[]){"list", "block", "unblock"}[tvar], *optargs)) break;
+ if (tvar == 3) error_exit("unknown cmd '%s'", *optargs);
+ if (tvar) {
+ int i;
+ struct arglist {
+ char *name;
+ int idx;
+ } rftypes[] = {{"all", RFKILL_TYPE_ALL}, {"wifi", RFKILL_TYPE_WLAN},
+ {"wlan", RFKILL_TYPE_WLAN}, {"bluetooth", RFKILL_TYPE_BLUETOOTH},
+ {"uwb", RFKILL_TYPE_UWB}, {"ultrawideband", RFKILL_TYPE_UWB},
+ {"wimax", RFKILL_TYPE_WIMAX}, {"wwan", RFKILL_TYPE_WWAN},
+ {"gps", RFKILL_TYPE_GPS}, {"fm", 7}}; // RFKILL_TYPE_FM = 7
+
+ if (!*++optargs) error_exit("'%s' needs IDENTIFIER", optargs[-1]);
+ for (i = 0; i < ARRAY_LEN(rftypes); i++)
+ if (!strcmp(rftypes[i].name, *optargs)) break;
+ if (i == ARRAY_LEN(rftypes)) idx = atolx_range(*optargs, 0, INT_MAX);
+ else tid = rftypes[i].idx;
+ }
+
+ // Perform requested action
+ fd = xopen("/dev/rfkill", (tvar ? O_RDWR : O_RDONLY)|O_NONBLOCK);
+ if (tvar) {
+ // block/unblock
+ memset(&rfevent, 0, sizeof(rfevent));
+ rfevent.soft = tvar == 1;
+ if (idx >= 0) {
+ rfevent.idx = idx;
+ rfevent.op = RFKILL_OP_CHANGE;
+ } else {
+ rfevent.type = tid;
+ rfevent.op = RFKILL_OP_CHANGE_ALL;
+ }
+ xwrite(fd, &rfevent, sizeof(rfevent));
+ } else {
+ // show list.
+ while (sizeof(rfevent) == readall(fd, &rfevent, sizeof(rfevent))) {
+ char *line, *name = 0, *type = 0;
+
+ // filter list items
+ if ((tid > 0 && tid != rfevent.type) || (idx != -1 && idx != rfevent.idx))
+ continue;
+
+ sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx);
+ tvar = xopen(toybuf, O_RDONLY);
+ while ((line = get_line(tvar))) {
+ char *s = line;
+
+ if (strstart(&s, "RFKILL_NAME=")) name = xstrdup(s);
+ else if (strstart(&s, "RFKILL_TYPE=")) type = xstrdup(s);
+
+ free(line);
+ }
+ xclose(tvar);
+
+ xprintf("%u: %s: %s\n", rfevent.idx, name, type);
+ xprintf("\tSoft blocked: %s\n", rfevent.soft ? "yes" : "no");
+ xprintf("\tHard blocked: %s\n", rfevent.hard ? "yes" : "no");
+ free(name);
+ free(type);
+ }
+ }
+ xclose(fd);
+}