wifi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * libwebsockets - lws_netdev_wifi generic state handling
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * The generic wifi netdevs follow a
  25. */
  26. #include "private-lib-core.h"
  27. int
  28. lws_netdev_wifi_rssi_sort_compare(const lws_dll2_t *d, const lws_dll2_t *i)
  29. {
  30. const lws_wifi_sta_t *wsd = (const lws_wifi_sta_t *)d,
  31. *wsi = (const lws_wifi_sta_t *)i;
  32. return rssi_averaged(wsd) > rssi_averaged(wsi);
  33. }
  34. void
  35. lws_netdev_wifi_scan_empty(lws_netdev_instance_wifi_t *wnd)
  36. {
  37. lws_start_foreach_dll_safe(struct lws_dll2 *, p, p1, lws_dll2_get_head(
  38. &wnd->scan)) {
  39. lws_wifi_sta_t *s = lws_container_of(p, lws_wifi_sta_t, list);
  40. lws_dll2_remove(p);
  41. lws_free(s);
  42. } lws_end_foreach_dll_safe(p, p1);
  43. }
  44. void
  45. lws_netdev_wifi_scan(lws_sorted_usec_list_t *sul)
  46. {
  47. lws_netdev_instance_wifi_t *wnd = lws_container_of(sul,
  48. lws_netdev_instance_wifi_t, sul_scan);
  49. wnd->inst.ops->scan(&wnd->inst);
  50. }
  51. lws_wifi_sta_t *
  52. lws_netdev_wifi_scan_find(lws_netdev_instance_wifi_t *wnd, const char *ssid,
  53. const uint8_t *bssid)
  54. {
  55. lws_start_foreach_dll(struct lws_dll2 *, p, lws_dll2_get_head(
  56. &wnd->scan)) {
  57. lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
  58. if (!strcmp(ssid, (const char *)&w[1]) &&
  59. !memcmp(bssid, w->bssid, 6))
  60. return w;
  61. } lws_end_foreach_dll(p);
  62. return NULL;
  63. }
  64. int
  65. lws_netdev_wifi_scan_select(lws_netdev_instance_wifi_t *wnd)
  66. {
  67. lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
  68. struct lws_context *cx = lws_context_from_netdevs(netdevs);
  69. uint32_t least_recent = 0xffffffff;
  70. lws_wifi_creds_t *pc = NULL;
  71. lws_wifi_sta_t *pw = NULL;
  72. /*
  73. * Trim enough of the lowest RSSI guys in order to get us below the
  74. * limit we are allowed to keep track of...
  75. */
  76. while (wnd->scan.count > LWS_WIFI_MAX_SCAN_TRACK) {
  77. struct lws_dll2 *p = lws_dll2_get_tail(&wnd->scan);
  78. lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
  79. lws_dll2_remove(p);
  80. lws_free(w);
  81. }
  82. /*
  83. * ... let's dump what's left
  84. */
  85. lws_start_foreach_dll(struct lws_dll2 *, p, lws_dll2_get_head(
  86. &wnd->scan)) {
  87. lws_wifi_sta_t *w = lws_container_of(p, lws_wifi_sta_t, list);
  88. lwsl_notice("%s: %s, %02X:%02X:%02X:%02X:%02X:%02X, ch %d, rssi %d\n",
  89. __func__, (const char *)&w[1], w->bssid[0],
  90. w->bssid[1], w->bssid[2], w->bssid[3], w->bssid[4],
  91. w->bssid[5], w->ch, rssi_averaged(w));
  92. } lws_end_foreach_dll(p);
  93. /*
  94. * make sure we have our device's connection credentials at hand
  95. */
  96. if (!netdevs->ac_creds &&
  97. lws_netdev_credentials_settings_get(netdevs))
  98. return 0;
  99. netdevs->refcount_creds++;
  100. /*
  101. * Let's go through each starting from the best RSSI seeing if we
  102. * have credentials... if we do, pick the one we least-recently tried
  103. */
  104. lws_start_foreach_dll(struct lws_dll2 *, p1, wnd->scan.head) {
  105. lws_wifi_sta_t *w = lws_container_of(p1, lws_wifi_sta_t, list);
  106. lws_start_foreach_dll(struct lws_dll2 *, q,
  107. netdevs->owner_creds.head) {
  108. lws_wifi_creds_t *c = lws_container_of(q,
  109. lws_wifi_creds_t,
  110. list);
  111. if (!strcmp((const char *)&w[1], c->ssid) &&
  112. w->last_seen < least_recent) {
  113. /*
  114. * Not <= so we stick with higher RSSI when
  115. * all 0
  116. */
  117. pc = c;
  118. pw = w;
  119. least_recent = w->last_seen;
  120. }
  121. } lws_end_foreach_dll(q);
  122. } lws_end_foreach_dll(p1);
  123. if (least_recent != 0xffffffff) {
  124. /*
  125. * We picked one to try... note what we're trying so we can
  126. * record it in settings as last successful
  127. */
  128. lws_strncpy(wnd->current_attempt_ssid, (const char *)&pw[1],
  129. sizeof(wnd->current_attempt_ssid));
  130. memcpy(wnd->current_attempt_bssid, pw->bssid, LWS_ETH_ALEN);
  131. wnd->inst.ops->connect(&wnd->inst, pc->ssid, pc->passphrase,
  132. pw->bssid);
  133. } else {
  134. /*
  135. * We couldn't see anyone we recognized on this scan, let's
  136. * rescan in a bit
  137. */
  138. lwsl_notice("%s: nothing usable in scan, redoing in 3s\n", __func__);
  139. lws_sul_schedule(cx, 0, &wnd->sul_scan, lws_netdev_wifi_scan,
  140. 3 * LWS_US_PER_SEC);
  141. }
  142. if (!--netdevs->refcount_creds) {
  143. lws_dll2_owner_clear(&netdevs->owner_creds);
  144. lwsac_free(&netdevs->ac_creds);
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Initially our best bet is just try to reconnect to whatever we last
  150. * succeeded to connect to
  151. */
  152. int
  153. lws_netdev_wifi_redo_last(lws_netdev_instance_wifi_t *wnd)
  154. {
  155. lws_netdevs_t *netdevs = lws_netdevs_from_ndi(&wnd->inst);
  156. uint8_t buf[256], bssid[LWS_ETH_ALEN];
  157. const char *ssid, *pp = "", *pb;
  158. char setname[16], ssid_copy[33];
  159. size_t l = sizeof(buf), al;
  160. lws_wifi_creds_t *cred;
  161. /*
  162. * Let's try to retreive the last successful connect info for this
  163. * netdev
  164. */
  165. lws_snprintf(setname, sizeof(setname), "netdev.last.%s", wnd->inst.name);
  166. if (lws_settings_plat_get(netdevs->si, setname, buf, &l))
  167. return 1;
  168. lwsl_notice("%s: last successful %s\n", __func__, buf);
  169. ssid = lws_json_simple_find((const char *)buf, l, "\"ssid\":", &al);
  170. if (!ssid || al > 32)
  171. return 1;
  172. memcpy(ssid_copy, ssid, al);
  173. ssid_copy[al + 1] = '\0';
  174. pb = lws_json_simple_find((const char *)buf, l, "\"bssid\":", &al);
  175. if (!pb)
  176. return 1;
  177. lws_hex_to_byte_array(pb, bssid, sizeof(bssid));
  178. /*
  179. * make sure we have our device's connection credentials at hand
  180. */
  181. if (!netdevs->ac_creds &&
  182. lws_netdev_credentials_settings_get(netdevs))
  183. return 1;
  184. netdevs->refcount_creds++;
  185. cred = lws_netdev_credentials_find(netdevs, ssid_copy, bssid);
  186. if (cred)
  187. pp = cred->passphrase;
  188. lws_strncpy(wnd->current_attempt_ssid, ssid_copy,
  189. sizeof(wnd->current_attempt_ssid));
  190. memcpy(wnd->current_attempt_bssid, bssid, LWS_ETH_ALEN);
  191. wnd->inst.ops->connect(&wnd->inst, ssid_copy, pp, bssid);
  192. if (!--netdevs->refcount_creds) {
  193. lws_dll2_owner_clear(&netdevs->owner_creds);
  194. lwsac_free(&netdevs->ac_creds);
  195. }
  196. return 0;
  197. }