autoscan.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * WPA Supplicant - auto scan
  3. * Copyright (c) 2012, Intel Corporation. All rights reserved.
  4. * Copyright 2015 Intel Deutschland GmbH
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "config.h"
  12. #include "wpa_supplicant_i.h"
  13. #include "bss.h"
  14. #include "scan.h"
  15. #include "autoscan.h"
  16. static const struct autoscan_ops * autoscan_modules[] = {
  17. #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
  18. &autoscan_exponential_ops,
  19. #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
  20. #ifdef CONFIG_AUTOSCAN_PERIODIC
  21. &autoscan_periodic_ops,
  22. #endif /* CONFIG_AUTOSCAN_PERIODIC */
  23. NULL
  24. };
  25. static void request_scan(struct wpa_supplicant *wpa_s)
  26. {
  27. wpa_s->scan_req = MANUAL_SCAN_REQ;
  28. if (wpa_supplicant_req_sched_scan(wpa_s))
  29. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
  30. }
  31. int autoscan_init(struct wpa_supplicant *wpa_s, int req_scan)
  32. {
  33. const char *name = wpa_s->conf->autoscan;
  34. const char *params;
  35. size_t nlen;
  36. int i;
  37. const struct autoscan_ops *ops = NULL;
  38. struct sched_scan_plan *scan_plans;
  39. /* Give preference to scheduled scan plans if supported/configured */
  40. if (wpa_s->sched_scan_plans) {
  41. wpa_printf(MSG_DEBUG,
  42. "autoscan: sched_scan_plans set - use it instead");
  43. return 0;
  44. }
  45. if (wpa_s->autoscan && wpa_s->autoscan_priv) {
  46. wpa_printf(MSG_DEBUG, "autoscan: Already initialized");
  47. return 0;
  48. }
  49. if (name == NULL)
  50. return 0;
  51. params = os_strchr(name, ':');
  52. if (params == NULL) {
  53. params = "";
  54. nlen = os_strlen(name);
  55. } else {
  56. nlen = params - name;
  57. params++;
  58. }
  59. for (i = 0; autoscan_modules[i]; i++) {
  60. if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
  61. ops = autoscan_modules[i];
  62. break;
  63. }
  64. }
  65. if (ops == NULL) {
  66. wpa_printf(MSG_ERROR, "autoscan: Could not find module "
  67. "matching the parameter '%s'", name);
  68. return -1;
  69. }
  70. scan_plans = os_malloc(sizeof(*wpa_s->sched_scan_plans));
  71. if (!scan_plans)
  72. return -1;
  73. wpa_s->autoscan_params = NULL;
  74. wpa_s->autoscan_priv = ops->init(wpa_s, params);
  75. if (!wpa_s->autoscan_priv) {
  76. os_free(scan_plans);
  77. return -1;
  78. }
  79. scan_plans[0].interval = 5;
  80. scan_plans[0].iterations = 0;
  81. os_free(wpa_s->sched_scan_plans);
  82. wpa_s->sched_scan_plans = scan_plans;
  83. wpa_s->sched_scan_plans_num = 1;
  84. wpa_s->autoscan = ops;
  85. wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
  86. "parameters '%s'", ops->name, params);
  87. if (!req_scan)
  88. return 0;
  89. /*
  90. * Cancelling existing scan requests, if any.
  91. */
  92. wpa_supplicant_cancel_sched_scan(wpa_s);
  93. wpa_supplicant_cancel_scan(wpa_s);
  94. /*
  95. * Firing first scan, which will lead to call autoscan_notify_scan.
  96. */
  97. request_scan(wpa_s);
  98. return 0;
  99. }
  100. void autoscan_deinit(struct wpa_supplicant *wpa_s)
  101. {
  102. if (wpa_s->autoscan && wpa_s->autoscan_priv) {
  103. wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
  104. wpa_s->autoscan->name);
  105. wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
  106. wpa_s->autoscan = NULL;
  107. wpa_s->autoscan_priv = NULL;
  108. wpa_s->scan_interval = 5;
  109. os_free(wpa_s->sched_scan_plans);
  110. wpa_s->sched_scan_plans = NULL;
  111. wpa_s->sched_scan_plans_num = 0;
  112. }
  113. }
  114. int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
  115. struct wpa_scan_results *scan_res)
  116. {
  117. int interval;
  118. if (wpa_s->autoscan && wpa_s->autoscan_priv) {
  119. interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
  120. scan_res);
  121. if (interval <= 0)
  122. return -1;
  123. wpa_s->scan_interval = interval;
  124. wpa_s->sched_scan_plans[0].interval = interval;
  125. request_scan(wpa_s);
  126. }
  127. return 0;
  128. }