autoscan.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * WPA Supplicant - auto scan
  3. * Copyright (c) 2012, Intel Corporation. All rights reserved.
  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 "config.h"
  11. #include "wpa_supplicant_i.h"
  12. #include "bss.h"
  13. #include "scan.h"
  14. #include "autoscan.h"
  15. #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
  16. extern const struct autoscan_ops autoscan_exponential_ops;
  17. #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
  18. #ifdef CONFIG_AUTOSCAN_PERIODIC
  19. extern const struct autoscan_ops autoscan_periodic_ops;
  20. #endif /* CONFIG_AUTOSCAN_PERIODIC */
  21. static const struct autoscan_ops * autoscan_modules[] = {
  22. #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
  23. &autoscan_exponential_ops,
  24. #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
  25. #ifdef CONFIG_AUTOSCAN_PERIODIC
  26. &autoscan_periodic_ops,
  27. #endif /* CONFIG_AUTOSCAN_PERIODIC */
  28. NULL
  29. };
  30. static void request_scan(struct wpa_supplicant *wpa_s)
  31. {
  32. wpa_s->scan_req = MANUAL_SCAN_REQ;
  33. if (wpa_supplicant_req_sched_scan(wpa_s))
  34. wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
  35. }
  36. int autoscan_init(struct wpa_supplicant *wpa_s, int req_scan)
  37. {
  38. const char *name = wpa_s->conf->autoscan;
  39. const char *params;
  40. size_t nlen;
  41. int i;
  42. const struct autoscan_ops *ops = NULL;
  43. if (wpa_s->autoscan && wpa_s->autoscan_priv)
  44. return 0;
  45. if (name == NULL)
  46. return 0;
  47. params = os_strchr(name, ':');
  48. if (params == NULL) {
  49. params = "";
  50. nlen = os_strlen(name);
  51. } else {
  52. nlen = params - name;
  53. params++;
  54. }
  55. for (i = 0; autoscan_modules[i]; i++) {
  56. if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
  57. ops = autoscan_modules[i];
  58. break;
  59. }
  60. }
  61. if (ops == NULL) {
  62. wpa_printf(MSG_ERROR, "autoscan: Could not find module "
  63. "matching the parameter '%s'", name);
  64. return -1;
  65. }
  66. wpa_s->autoscan_params = NULL;
  67. wpa_s->autoscan_priv = ops->init(wpa_s, params);
  68. if (wpa_s->autoscan_priv == NULL)
  69. return -1;
  70. wpa_s->autoscan = ops;
  71. wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
  72. "parameters '%s'", ops->name, params);
  73. if (!req_scan)
  74. return 0;
  75. /*
  76. * Cancelling existing scan requests, if any.
  77. */
  78. wpa_supplicant_cancel_sched_scan(wpa_s);
  79. wpa_supplicant_cancel_scan(wpa_s);
  80. /*
  81. * Firing first scan, which will lead to call autoscan_notify_scan.
  82. */
  83. request_scan(wpa_s);
  84. return 0;
  85. }
  86. void autoscan_deinit(struct wpa_supplicant *wpa_s)
  87. {
  88. if (wpa_s->autoscan && wpa_s->autoscan_priv) {
  89. wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
  90. wpa_s->autoscan->name);
  91. wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
  92. wpa_s->autoscan = NULL;
  93. wpa_s->autoscan_priv = NULL;
  94. wpa_s->scan_interval = 5;
  95. wpa_s->sched_scan_interval = 0;
  96. }
  97. }
  98. int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
  99. struct wpa_scan_results *scan_res)
  100. {
  101. int interval;
  102. if (wpa_s->autoscan && wpa_s->autoscan_priv) {
  103. interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
  104. scan_res);
  105. if (interval <= 0)
  106. return -1;
  107. wpa_s->scan_interval = interval;
  108. wpa_s->sched_scan_interval = interval;
  109. request_scan(wpa_s);
  110. }
  111. return 0;
  112. }