]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
Added support for block/unblock wireless types.
authorTim Gardner <tim.gardner@canonical.com>
Thu, 23 Jul 2009 16:53:47 +0000 (10:53 -0600)
committerTim Gardner <tim.gardner@canonical.com>
Thu, 23 Jul 2009 16:53:47 +0000 (10:53 -0600)
block/unblock now accepts an index as well as a wireless type,
uncluding 'all'.

Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
rfkill.c

index 7d470be6a26f14ebc4f8956cbda3ec4073664544..c6d7a7296b38b4a6e7c2ffa750808936e5490350 100644 (file)
--- a/rfkill.c
+++ b/rfkill.c
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <sys/types.h>
 #include <sys/poll.h>
 
@@ -221,16 +222,42 @@ static void rfkill_block_all(enum rfkill_type type, __u8 block)
                return;
 
        for (i = 0; i < num_events; i++) {
-               if ((events[i].type == type) || (type == RFKILL_TYPE_ALL)) {
+               if ((events[i].type == type) || (type == RFKILL_TYPE_ALL))
                        rfkill_block(events[i].idx, block);
-               }
        }
 
        free(events);
 }
 
+struct rfkill_type_str {
+       enum rfkill_type type;
+       char *name;
+};
+static struct rfkill_type_str rfkill_type_strings[] = {
+       {       .type = RFKILL_TYPE_ALL,                .name = "all"   },
+       {       .type = RFKILL_TYPE_WLAN,               .name = "wifi"  },
+       {       .type = RFKILL_TYPE_BLUETOOTH,  .name = "bluetooth"     },
+       {       .type = RFKILL_TYPE_UWB,                .name = "uwb"   },
+       {       .type = RFKILL_TYPE_WIMAX,              .name = "wimax" },
+       {       .type = RFKILL_TYPE_WWAN,               .name = "wwan"  },
+       {       .name = NULL }
+};
+
+static enum rfkill_type rfkill_str_to_type(char *s)
+{
+       struct rfkill_type_str *p;
+
+       for (p = rfkill_type_strings; p->name != NULL; p++) {
+               if ((strlen(s) == strlen(p->name)) && (!strcmp(s,p->name)))
+                       return p->type;
+       }
+       return NUM_RFKILL_TYPES;
+}
+
 static const char *argv0;
 
+#define BLOCK_PARAMS "{<idx>,all,wifi,bluetooth,uwb,wimax,wwan}"
+
 static void usage(void)
 {
        fprintf(stderr, "Usage:\t%s [options] command\n", argv0);
@@ -240,8 +267,8 @@ static void usage(void)
        fprintf(stderr, "\thelp\n");
        fprintf(stderr, "\tevent\n");
        fprintf(stderr, "\tlist\n");
-       fprintf(stderr, "\tblock <idx>\n");
-       fprintf(stderr, "\tunblock <idx>\n");
+       fprintf(stderr, "\tblock "BLOCK_PARAMS"\n");
+       fprintf(stderr, "\tunblock "BLOCK_PARAMS"\n");
 }
 
 static void version(void)
@@ -249,6 +276,31 @@ static void version(void)
        printf("rfkill %s\n", rfkill_version);
 }
 
+static void do_block_unblock(__u8 block, char *param)
+{
+       enum rfkill_type t;
+       __u32 idx;
+
+       if (islower(*param)) {
+               /* assume alphabetic characters imply a wireless type name */
+               t = rfkill_str_to_type(param);
+               if ( t < NUM_RFKILL_TYPES)
+                       rfkill_block_all(t,block);
+               else
+                       goto err;
+       } else if (isdigit(*param)) {
+               /* assume a numeric character implies an index. */
+               idx = atoi(param);
+               rfkill_block(idx, block);
+       } else
+               goto err;
+
+       return;
+err:
+       fprintf(stderr,"Bogus %sblock argument '%s'.\n",block?"":"un",param);
+       exit(1);
+}
+
 int main(int argc, char **argv)
 {
        /* strip off self */
@@ -272,13 +324,11 @@ int main(int argc, char **argv)
        } else if (strcmp(*argv, "block") == 0 && argc > 1) {
                argc--;
                argv++;
-               __u32 idx = atoi(*argv);
-               rfkill_block(idx, 1);
+               do_block_unblock(1,*argv);
        } else if (strcmp(*argv, "unblock") == 0 && argc > 1) {
                argc--;
                argv++;
-               __u32 idx = atoi(*argv);
-               rfkill_block(idx, 0);
+               do_block_unblock(0,*argv);
        } else {
                usage();
                return 1;