WifiThread.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef WIFITHREAD_H_
29 #define WIFITHREAD_H_
30 
31 #ifdef _WIN32
32  #ifndef UNICODE
33  #define UNICODE
34  #endif
35 
36  #include <windows.h>
37  #include <wlanapi.h>
38  #include <Windot11.h> // for DOT11_SSID struct
39  #include <objbase.h>
40  #include <wtypes.h>
41 
42  #include <stdio.h>
43  #include <stdlib.h>
44 
45  // Need to link with Wlanapi.lib and Ole32.lib
46  #pragma comment(lib, "wlanapi.lib")
47  #pragma comment(lib, "ole32.lib")
48 #elif __APPLE__
49 #include "WifiOSX.h"
50 #else
51  #include <sys/socket.h>
52  #include <linux/wireless.h>
53  #include <sys/ioctl.h>
54 #endif
56 #include <rtabmap/utilite/UTimer.h>
57 
58 // A percentage value that represents the signal quality
59 // of the network. WLAN_SIGNAL_QUALITY is of type ULONG.
60 // This member contains a value between 0 and 100. A value
61 // of 0 implies an actual RSSI signal strength of -100 dbm.
62 // A value of 100 implies an actual RSSI signal strength of -50 dbm.
63 // You can calculate the RSSI signal strength value for wlanSignalQuality
64 // values between 1 and 99 using linear interpolation.
65 inline int quality2dBm(int quality)
66 {
67  // Quality to dBm:
68  if(quality <= 0)
69  return -100;
70  else if(quality >= 100)
71  return -50;
72  else
73  return (quality / 2) - 100;
74 }
75 
76 
77 class WifiThread : public UThread, public UEventsSender
78 {
79 public:
80  WifiThread(const std::string & interfaceName, float rate = 0.5) :
81  interfaceName_(interfaceName),
82  rate_(rate)
83  {}
84  virtual ~WifiThread() {}
85 
86 private:
87  virtual void mainLoop()
88  {
89  uSleep(1000/rate_);
90  if(!this->isKilled())
91  {
92  int dBm = 0;
93 #ifdef _WIN32
94  //From https://msdn.microsoft.com/en-us/library/windows/desktop/ms706765(v=vs.85).aspx
95  // Declare and initialize variables.
96  HANDLE hClient = NULL;
97  DWORD dwMaxClient = 2; //
98  DWORD dwCurVersion = 0;
99  DWORD dwResult = 0;
100 
101  // variables used for WlanEnumInterfaces
102  PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
103  PWLAN_INTERFACE_INFO pIfInfo = NULL;
104 
105  // variables used for WlanQueryInterfaces for opcode = wlan_intf_opcode_current_connection
106  PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
107  DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
108  WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
109 
110  dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
111  if (dwResult != ERROR_SUCCESS)
112  {
113  UERROR("WlanOpenHandle failed with error: %u\n", dwResult);
114  }
115  else
116  {
117  dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
118  if (dwResult != ERROR_SUCCESS)
119  {
120  UERROR("WlanEnumInterfaces failed with error: %u\n", dwResult);
121  }
122  else
123  {
124  // take the first interface found
125  int i = 0;
126  pIfInfo = (WLAN_INTERFACE_INFO *) & pIfList->InterfaceInfo[i];
127  if(pIfInfo->isState == wlan_interface_state_connected)
128  {
129  dwResult = WlanQueryInterface(hClient,
130  &pIfInfo->InterfaceGuid,
131  wlan_intf_opcode_current_connection,
132  NULL,
133  &connectInfoSize,
134  (PVOID *) &pConnectInfo,
135  &opCode);
136 
137  if (dwResult != ERROR_SUCCESS)
138  {
139  UERROR("WlanQueryInterface failed with error: %u\n", dwResult);
140  }
141  else
142  {
143  int quality = pConnectInfo->wlanAssociationAttributes.wlanSignalQuality;
144  dBm = quality2dBm(quality);
145  }
146  }
147  else
148  {
149  UERROR("Interface not connected!");
150  }
151  }
152  }
153  if (pConnectInfo != NULL)
154  {
155  WlanFreeMemory(pConnectInfo);
156  pConnectInfo = NULL;
157  }
158 
159  if (pIfList != NULL)
160  {
161  WlanFreeMemory(pIfList);
162  pIfList = NULL;
163  }
164 #elif __APPLE__
165  dBm = getRssi(interfaceName_);
166 #else
167  // Code inspired from http://blog.ajhodges.com/2011/10/using-ioctl-to-gather-wifi-information.html
168 
169  //have to use a socket for ioctl
170  int sockfd;
171  /* Any old socket will do, and a datagram socket is pretty cheap */
172  if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
173  UERROR("Could not create simple datagram socket");
174  return;
175  }
176 
177  struct iwreq req;
178  struct iw_statistics stats;
179 
180  strncpy(req.ifr_name, interfaceName_.c_str(), IFNAMSIZ);
181 
182  //make room for the iw_statistics object
183  req.u.data.pointer = (caddr_t) &stats;
184  req.u.data.length = sizeof(stats);
185  // clear updated flag
186  req.u.data.flags = 1;
187 
188  //this will gather the signal strength
189  if(ioctl(sockfd, SIOCGIWSTATS, &req) == -1)
190  {
191  //die with error, invalid interface
192  UERROR("Invalid interface (\"%s\"). Tip: Try with sudo!", interfaceName_.c_str());
193  }
194  else if(((iw_statistics *)req.u.data.pointer)->qual.updated & IW_QUAL_DBM)
195  {
196  //signal is measured in dBm and is valid for us to use
197  dBm = ((iw_statistics *)req.u.data.pointer)->qual.level - 256;
198  }
199  else
200  {
201  UERROR("Could not get signal level.");
202  }
203 
204  close(sockfd);
205 #endif
206  if(dBm != 0)
207  {
208  double stamp = UTimer::now();
209 
210  // Create user data [level, stamp] with the value and a timestamp
211  cv::Mat data(1, 2, CV_64FC1);
212  data.at<double>(0) = double(dBm);
213  data.at<double>(1) = stamp;
214  this->post(new UserDataEvent(data));
215  //UWARN("posting level %d dBm", dBm);
216  }
217  }
218  }
219 
220 private:
221  std::string interfaceName_;
222  float rate_;
223 };
224 
225 #endif /* WIFITHREAD_H_ */
#define NULL
WifiThread(const std::string &interfaceName, float rate=0.5)
Definition: WifiThread.h:80
int getRssi(const std::string &interfaceName)
int quality2dBm(int quality)
Definition: WifiThread.h:65
float rate_
Definition: WifiThread.h:222
virtual ~WifiThread()
Definition: WifiThread.h:84
virtual void mainLoop()
Definition: WifiThread.h:87
std::string interfaceName_
Definition: WifiThread.h:221
void post(UEvent *event, bool async=true) const
bool isKilled() const
Definition: UThread.cpp:255
void uSleep(unsigned int ms)
#define UERROR(...)
static double now()
Definition: UTimer.cpp:73


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:41