rfkill.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * rfkill implementation for busybox
  4. *
  5. * Copyright (C) 2010 Malek Degachi <malek-degachi@laposte.net>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config RFKILL
  10. //config: bool "rfkill (4.4 kb)"
  11. //config: default n # doesn't build on Ubuntu 9.04
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: Enable/disable wireless devices.
  15. //config:
  16. //config: rfkill list : list all wireless devices
  17. //config: rfkill list bluetooth : list all bluetooth devices
  18. //config: rfkill list 1 : list device corresponding to the given index
  19. //config: rfkill block|unblock wlan : block/unblock all wlan(wifi) devices
  20. //config:
  21. //applet:IF_RFKILL(APPLET(rfkill, BB_DIR_USR_SBIN, BB_SUID_DROP))
  22. //kbuild:lib-$(CONFIG_RFKILL) += rfkill.o
  23. //usage:#define rfkill_trivial_usage
  24. //usage: "COMMAND [INDEX|TYPE]"
  25. //usage:#define rfkill_full_usage "\n\n"
  26. //usage: "Enable/disable wireless devices\n"
  27. //usage: "\nCommands:"
  28. //usage: "\n list [INDEX|TYPE] List current state"
  29. //usage: "\n block INDEX|TYPE Disable device"
  30. //usage: "\n unblock INDEX|TYPE Enable device"
  31. //usage: "\n"
  32. //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
  33. //usage: "\n wimax, wwan, gps, fm"
  34. #include "libbb.h"
  35. #include <linux/rfkill.h>
  36. enum {
  37. OPT_b = (1 << 0), /* must be = 1 */
  38. OPT_u = (1 << 1),
  39. OPT_l = (1 << 2),
  40. };
  41. int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int rfkill_main(int argc UNUSED_PARAM, char **argv)
  43. {
  44. struct rfkill_event event;
  45. const char *rf_name;
  46. int rf_fd;
  47. int mode;
  48. int rf_type;
  49. int rf_idx;
  50. unsigned rf_opt = 0;
  51. argv++;
  52. /* Must have one or two params */
  53. if (!argv[0] || (argv[1] && argv[2]))
  54. bb_show_usage();
  55. mode = O_RDWR | O_NONBLOCK;
  56. rf_name = argv[1];
  57. if (strcmp(argv[0], "list") == 0) {
  58. rf_opt |= OPT_l;
  59. mode = O_RDONLY | O_NONBLOCK;
  60. } else if (strcmp(argv[0], "block") == 0 && rf_name) {
  61. rf_opt |= OPT_b;
  62. } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
  63. rf_opt |= OPT_u;
  64. } else
  65. bb_show_usage();
  66. rf_type = RFKILL_TYPE_ALL;
  67. rf_idx = -1;
  68. if (rf_name) {
  69. static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
  70. if (strcmp(rf_name, "wifi") == 0)
  71. rf_name = "wlan";
  72. if (strcmp(rf_name, "ultrawideband") == 0)
  73. rf_name = "uwb";
  74. rf_type = index_in_strings(rfkill_types, rf_name);
  75. if (rf_type < 0) {
  76. rf_idx = xatoi_positive(rf_name);
  77. }
  78. }
  79. rf_fd = device_open("/dev/rfkill", mode);
  80. if (rf_fd < 0)
  81. bb_simple_perror_msg_and_die("/dev/rfkill");
  82. if (rf_opt & OPT_l) {
  83. while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
  84. parser_t *parser;
  85. char *tokens[2];
  86. char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
  87. char *name, *type;
  88. if (rf_type && rf_type != event.type && rf_idx < 0) {
  89. continue;
  90. }
  91. if (rf_idx >= 0 && event.idx != rf_idx) {
  92. continue;
  93. }
  94. name = NULL;
  95. type = NULL;
  96. sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
  97. parser = config_open2(rf_sysfs, fopen_for_read);
  98. while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
  99. if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
  100. name = xstrdup(tokens[1]);
  101. continue;
  102. }
  103. if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
  104. type = xstrdup(tokens[1]);
  105. continue;
  106. }
  107. }
  108. config_close(parser);
  109. printf("%u: %s: %s\n", event.idx, name, type);
  110. printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
  111. printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
  112. free(name);
  113. free(type);
  114. }
  115. } else {
  116. memset(&event, 0, sizeof(event));
  117. if (rf_type >= 0) {
  118. event.type = rf_type;
  119. event.op = RFKILL_OP_CHANGE_ALL;
  120. }
  121. if (rf_idx >= 0) {
  122. event.idx = rf_idx;
  123. event.op = RFKILL_OP_CHANGE;
  124. }
  125. /* Note: OPT_b == 1 */
  126. event.soft = (rf_opt & OPT_b);
  127. xwrite(rf_fd, &event, sizeof(event));
  128. }
  129. return EXIT_SUCCESS;
  130. }