Welcome to mirror list, hosted at ThFree Co, Russian Federation.

wifi_info_windows.cpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 027d95a1361427debb41bc06f0876cfa0f7ced5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "std/target_os.hpp"

#ifdef OMIM_OS_WINDOWS

#include "platform/wifi_info.hpp"

#include "base/logging.hpp"

#include "std/windows.hpp"
#include "std/cstdio.hpp"

#include <winsock2.h>
#include <iphlpapi.h>

bool GatewaysInfo(vector<WiFiInfo::AccessPoint> & out)
{
  vector<string> ips;

  ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
  PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
  if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
  {
    free(pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
  }

  if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR)
  {
    PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
    while (pAdapter)
    {
      if (strcmp(pAdapter->GatewayList.IpAddress.String, "0.0.0.0"))
        ips.push_back(pAdapter->GatewayList.IpAddress.String);
      pAdapter = pAdapter->Next;
    }
  }
  if (pAdapterInfo)
    free(pAdapterInfo);

  if (!ips.empty())
  {
    DWORD dwSize = 0;
    GetIpNetTable(NULL, &dwSize, 0);
    PMIB_IPNETTABLE pIpNetTable = (MIB_IPNETTABLE *)malloc(dwSize);
    if (GetIpNetTable(pIpNetTable, &dwSize, 0) == NO_ERROR)
    {
      for (DWORD i = 0; i < pIpNetTable->dwNumEntries; ++i)
      {
        for (size_t j = 0; j < ips.size(); ++j)
        {
          if (ips[j] == inet_ntoa(*(struct in_addr *)&pIpNetTable->table[i].dwAddr)
              && pIpNetTable->table[i].dwPhysAddrLen == 6)
          {
            char buff[20] = {};
            sprintf(buff, "%02X-%02X-%02X-%02X-%02X-%02X",
                pIpNetTable->table[i].bPhysAddr[0],
                pIpNetTable->table[i].bPhysAddr[1],
                pIpNetTable->table[i].bPhysAddr[2],
                pIpNetTable->table[i].bPhysAddr[3],
                pIpNetTable->table[i].bPhysAddr[4],
                pIpNetTable->table[i].bPhysAddr[5]);
            WiFiInfo::AccessPoint apn;
            apn.m_bssid = buff;
            apn.m_signalStrength = "-34";
            apn.m_ssid = "dlink";
            out.push_back(apn);
          }
        }
      }
    }
    free(pIpNetTable);
  }
  return !out.empty();
}

#if defined(OMIM_OS_WINDOWS_NATIVE)

#include <wlanapi.h>
#include <objbase.h>
#include <wtypes.h>

/////////////////////////////////////////////////////////////////
class WiFiInfo::Impl
{
  WiFiInfo::WifiRequestCallbackT m_callback;
  HANDLE m_hClient;
  bool m_isNotificationSupported;

  /// pointers to potential Vista-and-above functions
  typedef DWORD (WINAPI *PWLANGETNETWORKBSSLIST)(HANDLE, const GUID *, const PDOT11_SSID, DOT11_BSS_TYPE,
                  BOOL, PVOID, PWLAN_BSS_LIST *);
  PWLANGETNETWORKBSSLIST pWlanGetNetworkBssList;

  /// used only on XP
  HANDLE m_timer;

public:
  Impl();
  ~Impl();
  HANDLE Handle() const { return m_hClient; }
  void RequestWiFiBSSIDs(WifiRequestCallbackT callback);
  void GetApnsAndNotifyClient();
  void StopNotifications();
};
/////////////////////////////////////////////////////////////////

/// Called on Vista and above
VOID WINAPI OnWlanScanCompleted(PWLAN_NOTIFICATION_DATA data, PVOID context)
{
  switch (data->NotificationCode)
  {
  case wlan_notification_acm_scan_fail:
    LOG(LDEBUG, ("WiFi scan failed with code", *((DWORD *)data->pData)));
    // no break here! try to collect bssids anyway
  case wlan_notification_acm_scan_complete:
    {
      WiFiInfo::Impl * impl = reinterpret_cast<WiFiInfo::Impl *>(context);
      impl->GetApnsAndNotifyClient();
    }
    break;
  default:
    LOG(LDEBUG, ("Unknown WiFi notification", data->NotificationCode));
  }
}

/// Called only on XP by timer
VOID CALLBACK WaitOrTimerCallback(PVOID lpParameter, BOOLEAN)
{
  WiFiInfo::Impl * impl = reinterpret_cast<WiFiInfo::Impl *>(lpParameter);
  impl->GetApnsAndNotifyClient();
}

WiFiInfo::Impl::Impl() : m_hClient(NULL), m_timer(NULL)
{
  DWORD dwMaxClient = 2;  // 1 for WinXP, 2 for Vista and above
  DWORD dwCurVersion = 0;
  DWORD dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &m_hClient);
  if (dwResult != ERROR_SUCCESS)
  {
    LOG(LWARNING, ("Error while opening WLAN handle", dwResult));
  }
  m_isNotificationSupported = (dwCurVersion > 1);
  pWlanGetNetworkBssList = (PWLANGETNETWORKBSSLIST)GetProcAddress(GetModuleHandleA("wlanapi.dll"),
                "WlanGetNetworkBssList");
}

WiFiInfo::Impl::~Impl()
{
  WlanCloseHandle(m_hClient, NULL);
}

void WiFiInfo::Impl::RequestWiFiBSSIDs(WifiRequestCallbackT callback)
{
  m_callback = callback;
  // if it's XP without necessary api... use gateway instead
  if (!pWlanGetNetworkBssList)
  {
    vector<WiFiInfo::AccessPoint> apns;
    GatewaysInfo(apns);
    callback(apns);
    return;
  }

  if (!m_isNotificationSupported)
  { // request timer after 4 seconds, when scanning completes
    CreateTimerQueueTimer(&m_timer, NULL, &WaitOrTimerCallback, this,
                          4100, 0, WT_EXECUTEONLYONCE);
  }
  else
  { // subscribe to notification when scanning is completed
    WlanRegisterNotification(m_hClient,
                             WLAN_NOTIFICATION_SOURCE_ACM,
                             TRUE,
                             &OnWlanScanCompleted,
                             this,
                             NULL,
                             NULL);
  }
  // start scanning
  PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
  DWORD dwResult = WlanEnumInterfaces(m_hClient, NULL, &pIfList);
  if (dwResult == ERROR_SUCCESS)
  {
    for (int ifIndex = 0; ifIndex < static_cast<int>(pIfList->dwNumberOfItems); ++ifIndex)
    {
      PWLAN_INTERFACE_INFO pIfInfo = (PWLAN_INTERFACE_INFO)&pIfList->InterfaceInfo[ifIndex];
      WlanScan(m_hClient, &pIfInfo->InterfaceGuid, NULL, NULL, NULL);
    }
    WlanFreeMemory(pIfList);
  }
}

void WiFiInfo::Impl::GetApnsAndNotifyClient()
{
  vector<WiFiInfo::AccessPoint> apns;
  PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
  DWORD dwResult = WlanEnumInterfaces(m_hClient, NULL, &pIfList);
  if (dwResult == ERROR_SUCCESS)
  {
    for (int ifIndex = 0; ifIndex < static_cast<int>(pIfList->dwNumberOfItems); ++ifIndex)
    {
      PWLAN_INTERFACE_INFO pIfInfo = (PWLAN_INTERFACE_INFO)&pIfList->InterfaceInfo[ifIndex];
      PWLAN_BSS_LIST pWlanBssList = NULL;
      if (pWlanGetNetworkBssList)
      { // on WinXP we don't have this function :(
        dwResult = pWlanGetNetworkBssList(m_hClient,
                                         &pIfInfo->InterfaceGuid,
                                         0,
                                         dot11_BSS_type_any,
                                         FALSE,
                                         NULL,
                                         &pWlanBssList);
        if (dwResult == ERROR_SUCCESS)
        {
          for (int wlanIndex = 0; wlanIndex < static_cast<int>(pWlanBssList->dwNumberOfItems); ++wlanIndex)
          {
            PWLAN_BSS_ENTRY pBssEntry = &pWlanBssList->wlanBssEntries[wlanIndex];
            WiFiInfo::AccessPoint apn;
            apn.m_ssid.assign(&pBssEntry->dot11Ssid.ucSSID[0],
                              &pBssEntry->dot11Ssid.ucSSID[pBssEntry->dot11Ssid.uSSIDLength]);
            char buff[20] = {};
            sprintf(buff, "%02X-%02X-%02X-%02X-%02X-%02X",
                    pBssEntry->dot11Bssid[0], pBssEntry->dot11Bssid[1],
                    pBssEntry->dot11Bssid[2], pBssEntry->dot11Bssid[3],
                    pBssEntry->dot11Bssid[4], pBssEntry->dot11Bssid[5]);
            apn.m_bssid = buff;
            sprintf(buff, "%ld", pBssEntry->lRssi);
            apn.m_signalStrength = buff;
            apns.push_back(apn);
          }
          WlanFreeMemory(pWlanBssList);
        }
      }
    }
    WlanFreeMemory(pIfList);
  }

  m_callback(apns);

  // on WinXP, clean up timer if it was used
  if (m_timer)
  {
    DeleteTimerQueueTimer(NULL, m_timer, NULL);
    m_timer = NULL;
  }
}

void WiFiInfo::Impl::StopNotifications()
{
  WlanRegisterNotification(m_hClient, WLAN_NOTIFICATION_SOURCE_NONE, TRUE, NULL, NULL, NULL, NULL);
}
#else
///////////////////////////////////////////////////////////////////
// MinGW doesn't support wlan api, so simply return mac address of the router

class WiFiInfo::Impl
{
public:
  Impl() {}
  ~Impl() {}
  void RequestWiFiBSSIDs(WifiRequestCallbackT callback)
  {
    vector<WiFiInfo::AccessPoint> apns;
    GatewaysInfo(apns);
    callback(apns);
  }
  void StopNotifications() {}
};

#endif
///////////////////////////////////////////////////////////////////


WiFiInfo::WiFiInfo() : m_pImpl(new WiFiInfo::Impl)
{
}

WiFiInfo::~WiFiInfo()
{
  delete m_pImpl;
}

void WiFiInfo::RequestWiFiBSSIDs(WifiRequestCallbackT callback)
{
  m_pImpl->RequestWiFiBSSIDs(callback);
}

void WiFiInfo::Stop()
{
  m_pImpl->StopNotifications();
}

#endif