bgscan_simple.c 8.0 KB

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