-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWifiManager.cpp
More file actions
217 lines (181 loc) · 5.57 KB
/
Copy pathWifiManager.cpp
File metadata and controls
217 lines (181 loc) · 5.57 KB
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
///////////////////////////////////////////////////////////
// Manages wifi connections
///////////////////////////////////////////////////////////
#pragma once
#include "WifiManager.h"
#include <wlanapi.h>
#include <objbase.h>
#include <wtypes.h>
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "ole32.lib")
///////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////
CWifiManager::CWifiManager()
{
m_lSignalIntensity = 0;
m_pInterfaceList = NULL;
m_pNetworkList = NULL;
m_hWifiApiHandle = NULL;
m_pNetworkInterface = NULL;
m_pConnectedNetwork = NULL;
m_pConnectedNetworkType = NULL;
m_bHasSecurity = FALSE;
}
///////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////
CWifiManager::~CWifiManager()
{
Clean();
}
///////////////////////////////////////////////////////////
// Load api and init
///////////////////////////////////////////////////////////
eWifiErrors CWifiManager::Init()
{
// Return value
eWifiErrors WifiResult = WIFI_SUCCESS;
DWORD dwCurVersion = 0;
PWLAN_INTERFACE_INFO pInterface = NULL;
PWLAN_AVAILABLE_NETWORK pNetwork = NULL;
// Open a handle to the Wlan (wifi) api
if(ERROR_SUCCESS != WlanOpenHandle(2, NULL, &dwCurVersion, &m_hWifiApiHandle))
{
return WIFI_ERR_NO_HANDLE;
}
// Enum all available interfaces
if(ERROR_SUCCESS != WlanEnumInterfaces(m_hWifiApiHandle, NULL, &m_pInterfaceList))
{
return WIFI_ERR_INTERFACES;
}
// Get the connected interface
for (int i = 0; i < (int) m_pInterfaceList->dwNumberOfItems; i++)
{
if(NULL != (pInterface = (WLAN_INTERFACE_INFO *) &m_pInterfaceList->InterfaceInfo[i]))
{
if(wlan_interface_state_connected == pInterface->isState)
{
if(NULL != m_pNetworkInterface)
{
// More than one connected interface found, take the first one only !
WifiResult = WIFI_WRN_MULTIINTERFACES;
}
else
{
// Get now a list of acces points
if(ERROR_SUCCESS != WlanGetAvailableNetworkList(m_hWifiApiHandle, &pInterface->InterfaceGuid, 0, NULL, &m_pNetworkList))
{
return WIFI_ERR_NETWORKS;
}
for(int j = 0; j < (int) m_pNetworkList->dwNumberOfItems; j++)
{
if(NULL != (pNetwork = (WLAN_AVAILABLE_NETWORK *) & m_pNetworkList->Network[j]))
{
if(pNetwork->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED)
{
// Save the connected network informations
if(NULL == m_pConnectedNetwork)
{
m_pConnectedNetwork = &(pNetwork->dot11Ssid);
m_pConnectedNetworkType = &(pNetwork->dot11BssType);
m_bHasSecurity = pNetwork->bSecurityEnabled;
// Save also the network interface
m_pNetworkInterface = &(pInterface->InterfaceGuid);
}
else
{
// Connected to more than one network
WifiResult = WIFI_WRN_MULTINETWORKS;
}
}
}
}
}
}
}
}
// Verify that we have an interface and a connected network
if( (NULL == m_pNetworkInterface) || (NULL == m_pConnectedNetwork) || (NULL == m_pConnectedNetworkType) )
{
WifiResult = WIFI_ERR_NO_CONNECTION;
Clean();
}
else
{
// Get the signal value
Update();
}
return WifiResult;
}
///////////////////////////////////////////////////////////
// Set ressources free
///////////////////////////////////////////////////////////
bool CWifiManager::Clean()
{
m_pNetworkInterface = NULL;
m_pConnectedNetwork = NULL;
m_pConnectedNetworkType = NULL;
// Clean the interface list
if (m_pInterfaceList != NULL)
{
WlanFreeMemory(m_pInterfaceList);
m_pInterfaceList = NULL;
}
// Clean the network list
if (m_pNetworkList != NULL)
{
WlanFreeMemory(m_pNetworkList);
m_pNetworkList = NULL;
}
// Close the handle
if(m_hWifiApiHandle != NULL)
{
WlanCloseHandle(m_hWifiApiHandle, NULL);
m_hWifiApiHandle = NULL;
}
return true;
}
///////////////////////////////////////////////////////////
// Update the signal strength for connected access point
///////////////////////////////////////////////////////////
bool CWifiManager::Update()
{
bool bSuccess = false;
m_lSignalIntensity = 0;
if(m_hWifiApiHandle != NULL)
{
// List should only contain one entry, as we are looking for one network, on a specific interface
PWLAN_BSS_LIST pBssList = NULL;
if(ERROR_SUCCESS == WlanGetNetworkBssList(m_hWifiApiHandle, m_pNetworkInterface, m_pConnectedNetwork, *m_pConnectedNetworkType, m_bHasSecurity, NULL, &pBssList))
{
if(pBssList->dwNumberOfItems > 0)
{
// Cast from ulong to long is Ok, as the value can only be between 0 and 100
m_lSignalIntensity = (long)pBssList->wlanBssEntries[0].uLinkQuality;
bSuccess = true;
}
}
// Free the memory
WlanFreeMemory(pBssList);
}
return bSuccess;
}
///////////////////////////////////////////////////////////
// Get the last received signal intensity
///////////////////////////////////////////////////////////
long CWifiManager::GetSignalIntensity()
{
return m_lSignalIntensity;
}
///////////////////////////////////////////////////////////
// Get the name of the network
///////////////////////////////////////////////////////////
UCHAR const* CWifiManager::GetNetworkName()
{
if(NULL == m_pConnectedNetwork)
{
return NULL;
}
return m_pConnectedNetwork->ucSSID;
}