win_wifiScanner.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "nsWifiAccessPoint.h"
  5. #include "win_wifiScanner.h"
  6. // Moz headers (alphabetical)
  7. #include "win_wlanLibrary.h"
  8. #define DOT11_BSS_TYPE_UNUSED static_cast<DOT11_BSS_TYPE>(0)
  9. class InterfaceScanCallbackData {
  10. public:
  11. InterfaceScanCallbackData(uint32_t numInterfaces)
  12. : mCurrentlyScanningInterfaces(numInterfaces)
  13. {
  14. mAllInterfacesDoneScanningEvent =
  15. ::CreateEvent(nullptr, // null security
  16. TRUE, // manual reset event
  17. FALSE, // initially nonsignaled
  18. nullptr); // not named
  19. MOZ_ASSERT(NULL != mAllInterfacesDoneScanningEvent);
  20. }
  21. ~InterfaceScanCallbackData()
  22. {
  23. ::CloseHandle(mAllInterfacesDoneScanningEvent);
  24. }
  25. void
  26. OnInterfaceScanComplete()
  27. {
  28. uint32_t val = ::InterlockedDecrement(&mCurrentlyScanningInterfaces);
  29. if (!val) {
  30. ::SetEvent(mAllInterfacesDoneScanningEvent);
  31. }
  32. }
  33. void
  34. WaitForAllInterfacesToFinishScanning(uint32_t msToWait)
  35. {
  36. ::WaitForSingleObject(mAllInterfacesDoneScanningEvent,
  37. msToWait);
  38. }
  39. private:
  40. volatile uint32_t mCurrentlyScanningInterfaces;
  41. HANDLE mAllInterfacesDoneScanningEvent;
  42. };
  43. static void
  44. OnScanComplete(PWLAN_NOTIFICATION_DATA data, PVOID context)
  45. {
  46. if (WLAN_NOTIFICATION_SOURCE_ACM != data->NotificationSource) {
  47. return;
  48. }
  49. if (wlan_notification_acm_scan_complete != data->NotificationCode &&
  50. wlan_notification_acm_scan_fail != data->NotificationCode) {
  51. return;
  52. }
  53. InterfaceScanCallbackData* cbData =
  54. reinterpret_cast<InterfaceScanCallbackData*>(context);
  55. cbData->OnInterfaceScanComplete();
  56. }
  57. WinWifiScanner::WinWifiScanner()
  58. {
  59. // NOTE: We assume that, if we were unable to load the WLAN library when
  60. // we initially tried, we will not be able to load it in the future.
  61. // Technically, on Windows XP SP2, a user could install the redistributable
  62. // and make our assumption incorrect. We opt to avoid making a bunch of
  63. // spurious LoadLibrary calls in the common case rather than load the
  64. // WLAN API in the edge case.
  65. mWlanLibrary = WinWLANLibrary::Load();
  66. if (!mWlanLibrary) {
  67. NS_WARNING("Could not initialize Windows Wi-Fi scanner");
  68. }
  69. }
  70. WinWifiScanner::~WinWifiScanner()
  71. {
  72. }
  73. nsresult
  74. WinWifiScanner::GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
  75. {
  76. accessPoints.Clear();
  77. // NOTE: We do not try to load the WLAN library if we previously failed
  78. // to load it. See the note in WinWifiScanner constructor
  79. if (!mWlanLibrary) {
  80. return NS_ERROR_NOT_AVAILABLE;
  81. }
  82. // Get the list of interfaces. WlanEnumInterfaces allocates interface_list.
  83. WLAN_INTERFACE_INFO_LIST *interface_list = nullptr;
  84. if (ERROR_SUCCESS !=
  85. (*mWlanLibrary->GetWlanEnumInterfacesPtr())(mWlanLibrary->GetWLANHandle(),
  86. nullptr,
  87. &interface_list)) {
  88. return NS_ERROR_FAILURE;
  89. }
  90. // This ensures we call WlanFreeMemory on interface_list
  91. ScopedWLANObject scopedInterfaceList(mWlanLibrary, interface_list);
  92. if (!interface_list->dwNumberOfItems) {
  93. return NS_OK;
  94. }
  95. InterfaceScanCallbackData cbData(interface_list->dwNumberOfItems);
  96. DWORD wlanNotifySource;
  97. if (ERROR_SUCCESS !=
  98. (*mWlanLibrary->GetWlanRegisterNotificationPtr())(
  99. mWlanLibrary->GetWLANHandle(),
  100. WLAN_NOTIFICATION_SOURCE_ACM,
  101. TRUE,
  102. (WLAN_NOTIFICATION_CALLBACK)OnScanComplete,
  103. &cbData,
  104. NULL,
  105. &wlanNotifySource)) {
  106. return NS_ERROR_FAILURE;
  107. }
  108. // Go through the list of interfaces and call `WlanScan` on each
  109. for (unsigned int i = 0; i < interface_list->dwNumberOfItems; ++i) {
  110. if (ERROR_SUCCESS !=
  111. (*mWlanLibrary->GetWlanScanPtr())(
  112. mWlanLibrary->GetWLANHandle(),
  113. &interface_list->InterfaceInfo[i].InterfaceGuid,
  114. NULL,
  115. NULL,
  116. NULL)) {
  117. cbData.OnInterfaceScanComplete();
  118. }
  119. }
  120. // From the MSDN documentation:
  121. // "Wireless network drivers that meet Windows logo requirements are
  122. // required to complete a WlanScan function request in 4 seconds"
  123. cbData.WaitForAllInterfacesToFinishScanning(5000);
  124. // Unregister for the notifications. The documentation mentions that,
  125. // if a callback is currently running, this will wait for the callback
  126. // to complete.
  127. (*mWlanLibrary->GetWlanRegisterNotificationPtr())(
  128. mWlanLibrary->GetWLANHandle(),
  129. WLAN_NOTIFICATION_SOURCE_NONE,
  130. TRUE,
  131. NULL,
  132. NULL,
  133. NULL,
  134. &wlanNotifySource);
  135. // Go through the list of interfaces and get the data for each.
  136. for (uint32_t i = 0; i < interface_list->dwNumberOfItems; ++i) {
  137. WLAN_BSS_LIST *bss_list;
  138. if (ERROR_SUCCESS !=
  139. (*mWlanLibrary->GetWlanGetNetworkBssListPtr())(
  140. mWlanLibrary->GetWLANHandle(),
  141. &interface_list->InterfaceInfo[i].InterfaceGuid,
  142. nullptr, // Use all SSIDs.
  143. DOT11_BSS_TYPE_UNUSED,
  144. false, // bSecurityEnabled - unused
  145. nullptr, // reserved
  146. &bss_list)) {
  147. continue;
  148. }
  149. // This ensures we call WlanFreeMemory on bss_list
  150. ScopedWLANObject scopedBssList(mWlanLibrary, bss_list);
  151. // Store each discovered access point in our outparam
  152. for (int j = 0; j < static_cast<int>(bss_list->dwNumberOfItems); ++j) {
  153. nsWifiAccessPoint* ap = new nsWifiAccessPoint();
  154. if (!ap) {
  155. continue;
  156. }
  157. const WLAN_BSS_ENTRY bss_entry = bss_list->wlanBssEntries[j];
  158. ap->setMac(bss_entry.dot11Bssid);
  159. ap->setSignal(bss_entry.lRssi);
  160. ap->setSSID(reinterpret_cast<char const*>(bss_entry.dot11Ssid.ucSSID),
  161. bss_entry.dot11Ssid.uSSIDLength);
  162. accessPoints.AppendObject(ap);
  163. }
  164. }
  165. return NS_OK;
  166. }