net_backend_rfkill.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @file net_backend_rfkill.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Rfkill monitoring module.
  32. *
  33. * Synopsis: net.backend.rfkill(string type, string name)
  34. * Arguments:
  35. * type - method of determining the index of the rfkill device. "index" for
  36. * rfkill device index, "wlan" for wireless device. Be aware that, for
  37. * the wireless device method, the index is resloved at initialization,
  38. * and no attempt is made to refresh it if the device goes away. In other
  39. * words, you should probably put a "net.backend.waitdevice" statement
  40. * in front of the rfkill statement.
  41. * name - rfkill index or wireless device name
  42. */
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <inttypes.h>
  46. #include <stdio.h>
  47. #include <sys/types.h>
  48. #include <dirent.h>
  49. #include <misc/string_begins_with.h>
  50. #include <ncd/extra/NCDRfkillMonitor.h>
  51. #include <ncd/module_common.h>
  52. #include <generated/blog_channel_ncd_net_backend_rfkill.h>
  53. struct instance {
  54. NCDModuleInst *i;
  55. uint32_t index;
  56. NCDRfkillMonitor monitor;
  57. int up;
  58. };
  59. static int find_wlan_rfill (const char *ifname, uint32_t *out_index)
  60. {
  61. char ieee_path[100];
  62. snprintf(ieee_path, sizeof(ieee_path), "/sys/class/net/%s/../../ieee80211", ifname);
  63. int res = 0;
  64. DIR *d = opendir(ieee_path);
  65. if (!d) {
  66. goto fail0;
  67. }
  68. struct dirent *e;
  69. while (e = readdir(d)) {
  70. if (!string_begins_with(e->d_name, "phy")) {
  71. continue;
  72. }
  73. char phy_path[150];
  74. snprintf(phy_path, sizeof(phy_path), "%s/%s", ieee_path, e->d_name);
  75. DIR *d2 = opendir(phy_path);
  76. if (!d2) {
  77. continue;
  78. }
  79. struct dirent *e2;
  80. while (e2 = readdir(d2)) {
  81. int index_pos;
  82. if (!(index_pos = string_begins_with(e2->d_name, "rfkill"))) {
  83. continue;
  84. }
  85. uint32_t index;
  86. if (sscanf(e2->d_name + index_pos, "%"SCNu32, &index) != 1) {
  87. continue;
  88. }
  89. res = 1;
  90. *out_index = index;
  91. }
  92. closedir(d2);
  93. }
  94. closedir(d);
  95. fail0:
  96. return res;
  97. }
  98. static void monitor_handler (struct instance *o, struct rfkill_event event)
  99. {
  100. if (event.idx != o->index) {
  101. return;
  102. }
  103. int was_up = o->up;
  104. o->up = (event.op != RFKILL_OP_DEL && !event.soft && !event.hard);
  105. if (o->up && !was_up) {
  106. NCDModuleInst_Backend_Up(o->i);
  107. }
  108. else if (!o->up && was_up) {
  109. NCDModuleInst_Backend_Down(o->i);
  110. }
  111. }
  112. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  113. {
  114. struct instance *o = vo;
  115. o->i = i;
  116. // check arguments
  117. NCDValRef type_arg;
  118. NCDValRef name_arg;
  119. if (!NCDVal_ListRead(params->args, 2, &type_arg, &name_arg)) {
  120. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  121. goto fail0;
  122. }
  123. if (!NCDVal_IsString(type_arg) || !NCDVal_IsStringNoNulls(name_arg)) {
  124. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  125. goto fail0;
  126. }
  127. // null terminate name
  128. NCDValNullTermString name_nts;
  129. if (!NCDVal_StringNullTerminate(name_arg, &name_nts)) {
  130. ModuleLog(o->i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  131. goto fail0;
  132. }
  133. if (NCDVal_StringEquals(type_arg, "index")) {
  134. if (sscanf(name_nts.data, "%"SCNu32, &o->index) != 1) {
  135. ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
  136. goto fail1;
  137. }
  138. }
  139. else if (NCDVal_StringEquals(type_arg, "wlan")) {
  140. if (!find_wlan_rfill(name_nts.data, &o->index)) {
  141. ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
  142. goto fail1;
  143. }
  144. }
  145. else {
  146. ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
  147. goto fail1;
  148. }
  149. // init monitor
  150. if (!NCDRfkillMonitor_Init(&o->monitor, o->i->params->iparams->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
  151. ModuleLog(o->i, BLOG_ERROR, "monitor failed");
  152. goto fail1;
  153. }
  154. // set not up
  155. o->up = 0;
  156. // free name nts
  157. NCDValNullTermString_Free(&name_nts);
  158. return;
  159. fail1:
  160. NCDValNullTermString_Free(&name_nts);
  161. fail0:
  162. NCDModuleInst_Backend_DeadError(i);
  163. }
  164. static void func_die (void *vo)
  165. {
  166. struct instance *o = vo;
  167. // free monitor
  168. NCDRfkillMonitor_Free(&o->monitor);
  169. NCDModuleInst_Backend_Dead(o->i);
  170. }
  171. static struct NCDModule modules[] = {
  172. {
  173. .type = "net.backend.rfkill",
  174. .func_new2 = func_new,
  175. .func_die = func_die,
  176. .alloc_size = sizeof(struct instance)
  177. }, {
  178. .type = NULL
  179. }
  180. };
  181. const struct NCDModuleGroup ncdmodule_net_backend_rfkill = {
  182. .modules = modules
  183. };