Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

bgscan_simple.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * WPA Supplicant - background scan and roaming module: simple
  3. * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "drivers/driver.h"
  18. #include "config_ssid.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "scan.h"
  22. #include "bgscan.h"
  23. struct bgscan_simple_data {
  24. struct wpa_supplicant *wpa_s;
  25. const struct wpa_ssid *ssid;
  26. int scan_interval;
  27. int signal_threshold;
  28. int short_scan_count; /* counter for scans using short scan interval */
  29. int short_interval; /* use if signal < threshold */
  30. int long_interval; /* use if signal > threshold */
  31. struct os_time last_bgscan;
  32. };
  33. static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx)
  34. {
  35. struct bgscan_simple_data *data = eloop_ctx;
  36. struct wpa_supplicant *wpa_s = data->wpa_s;
  37. struct wpa_driver_scan_params params;
  38. os_memset(&params, 0, sizeof(params));
  39. params.num_ssids = 1;
  40. params.ssids[0].ssid = data->ssid->ssid;
  41. params.ssids[0].ssid_len = data->ssid->ssid_len;
  42. params.freqs = data->ssid->scan_freq;
  43. /*
  44. * A more advanced bgscan module would learn about most like channels
  45. * over time and request scans only for some channels (probing others
  46. * every now and then) to reduce effect on the data connection.
  47. */
  48. wpa_printf(MSG_DEBUG, "bgscan simple: Request a background scan");
  49. if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
  50. wpa_printf(MSG_DEBUG, "bgscan simple: Failed to trigger scan");
  51. eloop_register_timeout(data->scan_interval, 0,
  52. bgscan_simple_timeout, data, NULL);
  53. } else {
  54. if (data->scan_interval == data->short_interval) {
  55. data->short_scan_count++;
  56. /*
  57. * Spend at most the duration of a long scan interval
  58. * scanning at the short scan interval. After that,
  59. * revert to the long scan interval.
  60. */
  61. if (data->short_scan_count >
  62. data->long_interval / data->short_interval + 1) {
  63. data->scan_interval = data->long_interval;
  64. wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
  65. "off to long scan interval");
  66. }
  67. }
  68. os_get_time(&data->last_bgscan);
  69. }
  70. }
  71. static int bgscan_simple_get_params(struct bgscan_simple_data *data,
  72. const char *params)
  73. {
  74. const char *pos;
  75. if (params == NULL)
  76. return 0;
  77. data->short_interval = atoi(params);
  78. pos = os_strchr(params, ':');
  79. if (pos == NULL)
  80. return 0;
  81. pos++;
  82. data->signal_threshold = atoi(pos);
  83. pos = os_strchr(pos, ':');
  84. if (pos == NULL) {
  85. wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
  86. "for high signal");
  87. return -1;
  88. }
  89. pos++;
  90. data->long_interval = atoi(pos);
  91. return 0;
  92. }
  93. static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
  94. const char *params,
  95. const struct wpa_ssid *ssid)
  96. {
  97. struct bgscan_simple_data *data;
  98. data = os_zalloc(sizeof(*data));
  99. if (data == NULL)
  100. return NULL;
  101. data->wpa_s = wpa_s;
  102. data->ssid = ssid;
  103. if (bgscan_simple_get_params(data, params) < 0) {
  104. os_free(data);
  105. return NULL;
  106. }
  107. if (data->short_interval <= 0)
  108. data->short_interval = 30;
  109. if (data->long_interval <= 0)
  110. data->long_interval = 30;
  111. wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d "
  112. "Short bgscan interval %d Long bgscan interval %d",
  113. data->signal_threshold, data->short_interval,
  114. data->long_interval);
  115. if (data->signal_threshold &&
  116. wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
  117. wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
  118. "signal strength monitoring");
  119. }
  120. data->scan_interval = data->short_interval;
  121. if (data->signal_threshold) {
  122. /* Poll for signal info to set initial scan interval */
  123. struct wpa_signal_info siginfo;
  124. if (wpa_drv_signal_poll(wpa_s, &siginfo) == 0 &&
  125. siginfo.current_signal >= data->signal_threshold)
  126. data->scan_interval = data->long_interval;
  127. }
  128. wpa_printf(MSG_DEBUG, "bgscan simple: Init scan interval: %d",
  129. data->scan_interval);
  130. eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
  131. data, NULL);
  132. /*
  133. * This function is called immediately after an association, so it is
  134. * reasonable to assume that a scan was completed recently. This makes
  135. * us skip an immediate new scan in cases where the current signal
  136. * level is below the bgscan threshold.
  137. */
  138. os_get_time(&data->last_bgscan);
  139. return data;
  140. }
  141. static void bgscan_simple_deinit(void *priv)
  142. {
  143. struct bgscan_simple_data *data = priv;
  144. eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
  145. if (data->signal_threshold)
  146. wpa_drv_signal_monitor(data->wpa_s, 0, 0);
  147. os_free(data);
  148. }
  149. static int bgscan_simple_notify_scan(void *priv,
  150. struct wpa_scan_results *scan_res)
  151. {
  152. struct bgscan_simple_data *data = priv;
  153. wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
  154. eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
  155. eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
  156. data, NULL);
  157. /*
  158. * A more advanced bgscan could process scan results internally, select
  159. * the BSS and request roam if needed. This sample uses the existing
  160. * BSS/ESS selection routine. Change this to return 1 if selection is
  161. * done inside the bgscan module.
  162. */
  163. return 0;
  164. }
  165. static void bgscan_simple_notify_beacon_loss(void *priv)
  166. {
  167. wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
  168. /* TODO: speed up background scanning */
  169. }
  170. static void bgscan_simple_notify_signal_change(void *priv, int above,
  171. int current_signal,
  172. int current_noise,
  173. int current_txrate)
  174. {
  175. struct bgscan_simple_data *data = priv;
  176. int scan = 0;
  177. struct os_time now;
  178. if (data->short_interval == data->long_interval ||
  179. data->signal_threshold == 0)
  180. return;
  181. wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
  182. "(above=%d current_signal=%d current_noise=%d "
  183. "current_txrate=%d))", above, current_signal,
  184. current_noise, current_txrate);
  185. if (data->scan_interval == data->long_interval && !above) {
  186. wpa_printf(MSG_DEBUG, "bgscan simple: Start using short "
  187. "bgscan interval");
  188. data->scan_interval = data->short_interval;
  189. data->short_scan_count = 0;
  190. os_get_time(&now);
  191. if (now.sec > data->last_bgscan.sec + 1)
  192. scan = 1;
  193. else if (data->last_bgscan.sec + data->long_interval >
  194. now.sec + data->scan_interval) {
  195. /*
  196. * Restart scan interval timer if currently scheduled
  197. * scan is too far in the future.
  198. */
  199. eloop_cancel_timeout(bgscan_simple_timeout, data,
  200. NULL);
  201. eloop_register_timeout(data->scan_interval, 0,
  202. bgscan_simple_timeout, data,
  203. NULL);
  204. }
  205. } else if (data->scan_interval == data->short_interval && above) {
  206. wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
  207. "interval");
  208. data->scan_interval = data->long_interval;
  209. eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
  210. eloop_register_timeout(data->scan_interval, 0,
  211. bgscan_simple_timeout, data, NULL);
  212. } else if (!above) {
  213. /*
  214. * Signal dropped further 4 dB. Request a new scan if we have
  215. * not yet scanned in a while.
  216. */
  217. os_get_time(&now);
  218. if (now.sec > data->last_bgscan.sec + 10)
  219. scan = 1;
  220. }
  221. if (scan) {
  222. wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan");
  223. eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
  224. eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
  225. NULL);
  226. }
  227. }
  228. const struct bgscan_ops bgscan_simple_ops = {
  229. .name = "simple",
  230. .init = bgscan_simple_init,
  231. .deinit = bgscan_simple_deinit,
  232. .notify_scan = bgscan_simple_notify_scan,
  233. .notify_beacon_loss = bgscan_simple_notify_beacon_loss,
  234. .notify_signal_change = bgscan_simple_notify_signal_change,
  235. };