rfkill.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //usage:#define rfkill_trivial_usage
  10. //usage: "COMMAND [INDEX|TYPE]"
  11. //usage:#define rfkill_full_usage "\n\n"
  12. //usage: "Enable/disable wireless devices\n"
  13. //usage: "\nCommands:"
  14. //usage: "\n list [INDEX|TYPE] List current state"
  15. //usage: "\n block INDEX|TYPE Disable device"
  16. //usage: "\n unblock INDEX|TYPE Enable device"
  17. //usage: "\n"
  18. //usage: "\n TYPE: all, wlan(wifi), bluetooth, uwb(ultrawideband),"
  19. //usage: "\n wimax, wwan, gps, fm"
  20. #include "libbb.h"
  21. #include <linux/rfkill.h>
  22. enum {
  23. OPT_b = (1 << 0), /* must be = 1 */
  24. OPT_u = (1 << 1),
  25. OPT_l = (1 << 2),
  26. };
  27. int rfkill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  28. int rfkill_main(int argc UNUSED_PARAM, char **argv)
  29. {
  30. struct rfkill_event event;
  31. const char *rf_name;
  32. int rf_fd;
  33. int mode;
  34. int rf_type;
  35. int rf_idx;
  36. unsigned rf_opt = 0;
  37. argv++;
  38. /* Must have one or two params */
  39. if (!argv[0] || (argv[1] && argv[2]))
  40. bb_show_usage();
  41. mode = O_RDWR | O_NONBLOCK;
  42. rf_name = argv[1];
  43. if (strcmp(argv[0], "list") == 0) {
  44. rf_opt |= OPT_l;
  45. mode = O_RDONLY | O_NONBLOCK;
  46. } else if (strcmp(argv[0], "block") == 0 && rf_name) {
  47. rf_opt |= OPT_b;
  48. } else if (strcmp(argv[0], "unblock") == 0 && rf_name) {
  49. rf_opt |= OPT_u;
  50. } else
  51. bb_show_usage();
  52. rf_type = RFKILL_TYPE_ALL;
  53. rf_idx = -1;
  54. if (rf_name) {
  55. static const char rfkill_types[] ALIGN1 = "all\0wlan\0bluetooth\0uwb\0wimax\0wwan\0gps\0fm\0";
  56. if (strcmp(rf_name, "wifi") == 0)
  57. rf_name = "wlan";
  58. if (strcmp(rf_name, "ultrawideband") == 0)
  59. rf_name = "uwb";
  60. rf_type = index_in_strings(rfkill_types, rf_name);
  61. if (rf_type < 0) {
  62. rf_idx = xatoi_positive(rf_name);
  63. }
  64. }
  65. rf_fd = device_open("/dev/rfkill", mode);
  66. if (rf_fd < 0)
  67. bb_perror_msg_and_die("/dev/rfkill");
  68. if (rf_opt & OPT_l) {
  69. while (full_read(rf_fd, &event, sizeof(event)) == RFKILL_EVENT_SIZE_V1) {
  70. parser_t *parser;
  71. char *tokens[2];
  72. char rf_sysfs[sizeof("/sys/class/rfkill/rfkill%u/uevent") + sizeof(int)*3];
  73. char *name, *type;
  74. if (rf_type && rf_type != event.type && rf_idx < 0) {
  75. continue;
  76. }
  77. if (rf_idx >= 0 && event.idx != rf_idx) {
  78. continue;
  79. }
  80. name = NULL;
  81. type = NULL;
  82. sprintf(rf_sysfs, "/sys/class/rfkill/rfkill%u/uevent", event.idx);
  83. parser = config_open2(rf_sysfs, fopen_for_read);
  84. while (config_read(parser, tokens, 2, 2, "\n=", PARSE_NORMAL)) {
  85. if (strcmp(tokens[0], "RFKILL_NAME") == 0) {
  86. name = xstrdup(tokens[1]);
  87. continue;
  88. }
  89. if (strcmp(tokens[0], "RFKILL_TYPE") == 0) {
  90. type = xstrdup(tokens[1]);
  91. continue;
  92. }
  93. }
  94. config_close(parser);
  95. printf("%u: %s: %s\n", event.idx, name, type);
  96. printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
  97. printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
  98. free(name);
  99. free(type);
  100. }
  101. } else {
  102. memset(&event, 0, sizeof(event));
  103. if (rf_type >= 0) {
  104. event.type = rf_type;
  105. event.op = RFKILL_OP_CHANGE_ALL;
  106. }
  107. if (rf_idx >= 0) {
  108. event.idx = rf_idx;
  109. event.op = RFKILL_OP_CHANGE;
  110. }
  111. /* Note: OPT_b == 1 */
  112. event.soft = (rf_opt & OPT_b);
  113. xwrite(rf_fd, &event, sizeof(event));
  114. }
  115. return EXIT_SUCCESS;
  116. }