0.4.1
Loading...
Searching...
No Matches
ArubaSensor.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "ArubaSensor.hpp"
10
12#include "util/Logger.hpp"
13#include <libssh/libssh.h>
14#include <regex>
15#include <sstream>
16
18namespace nm = NAV::NodeManager;
20
22
24 : Node(typeStatic())
25{
26 LOG_TRACE("{}: called", name);
27
28 _onlyRealTime = true;
29 _hasConfig = true;
30 _guiConfigDefaultWindowSize = { 710, 220 };
31
33}
34
36{
37 LOG_TRACE("{}: called", nameId());
38}
39
41{
42 return "ArubaSensor";
43}
44
45std::string NAV::ArubaSensor::type() const
46{
47 return typeStatic();
48}
49
51{
52 return "Data Provider";
53}
54
56{
57 if (ImGui::InputTextWithHint("SSH Host", "192.168.0.0", &_sshHost))
58 {
59 LOG_DEBUG("{}: ssh host changed to {}", nameId(), _sshHost);
62 }
63 if (ImGui::InputTextWithHint("SSH User", "admin", &_sshUser))
64 {
65 LOG_DEBUG("{}: ssh admin changed to {}", nameId(), _sshUser);
68 }
69 if (ImGui::InputText("SSH Password", &_sshPassword))
70 {
71 LOG_DEBUG("{}: ssh password changed to {}", nameId(), _sshPassword);
74 }
75 if (ImGui::InputText("SSH Host Keys", &_sshHostkeys))
76 {
77 LOG_DEBUG("{}: ssh host keys changed to {}", nameId(), _sshHostkeys);
80 }
81 if (ImGui::InputText("SSH Key Exchange", &_sshKeyExchange))
82 {
83 LOG_DEBUG("{}: ssh key exchange changed to {}", nameId(), _sshKeyExchange);
86 }
87 if (ImGui::InputText("SSH Publickey Accepted Types", &_sshPublickeyAcceptedTypes))
88 {
89 LOG_DEBUG("{}: ssh publickey accepted types changed to {}", nameId(), _sshPublickeyAcceptedTypes);
92 }
93 ImGui::Spacing(); // Add spacing here
94 ImGui::Separator(); // Add a horizontal line
95 if (ImGui::InputInt("Output interval [ms]", &_outputInterval))
96 {
97 LOG_DEBUG("{}: output interval changed to {}", nameId(), _outputInterval);
99 }
100}
101
102[[nodiscard]] json NAV::ArubaSensor::save() const
103{
104 LOG_TRACE("{}: called", nameId());
105
106 json j;
107
108 j["sshHost"] = _sshHost;
109 j["sshUser"] = _sshUser;
110 j["sshPassword"] = _sshPassword;
111 j["sshHostkeys"] = _sshHostkeys;
112 j["sshKeyExchange"] = _sshKeyExchange;
113 j["sshPublickeyAcceptedTypes"] = _sshPublickeyAcceptedTypes;
114 j["outputInterval"] = _outputInterval;
115
116 return j;
117}
118
120{
121 LOG_TRACE("{}: called", nameId());
122
123 if (j.contains("sshHost"))
124 {
125 j.at("sshHost").get_to(_sshHost);
126 }
127 if (j.contains("sshUser"))
128 {
129 j.at("sshUser").get_to(_sshUser);
130 }
131 if (j.contains("sshPassword"))
132 {
133 j.at("sshPassword").get_to(_sshPassword);
134 }
135 if (j.contains("sshHostkeys"))
136 {
137 j.at("sshHostkeys").get_to(_sshHostkeys);
138 }
139 if (j.contains("sshKeyExchange"))
140 {
141 j.at("sshKeyExchange").get_to(_sshKeyExchange);
142 }
143 if (j.contains("sshPublickeyAcceptedTypes"))
144 {
145 j.at("sshPublickeyAcceptedTypes").get_to(_sshPublickeyAcceptedTypes);
146 }
147 if (j.contains("outputInterval"))
148 {
149 j.at("outputInterval").get_to(_outputInterval);
150 }
151}
152
154{
155 return true;
156}
157
159{
160 LOG_TRACE("{}: called", nameId());
161
162 // SSH session
163 _session = ssh_new();
164 if (_session == nullptr)
165 {
166 LOG_INFO("{}: Failed to create SSH _session", nameId());
167 return false;
168 }
169 LOG_DEBUG("{}: Successfully created SSH _session", nameId());
170
171 ssh_options_set(_session, SSH_OPTIONS_HOST, "192.168.178.45");
172 ssh_options_set(_session, SSH_OPTIONS_USER, "admin");
173 ssh_options_set(_session, SSH_OPTIONS_HOSTKEYS, "ssh-rsa");
174 ssh_options_set(_session, SSH_OPTIONS_KEY_EXCHANGE, "ecdh-sha2-nistp256");
175 ssh_options_set(_session, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, "ssh-rsa");
176
177 // connect
178 if (ssh_connect(_session) != SSH_OK)
179 {
180 LOG_INFO("{}: Failed to connect to the router", nameId());
181 ssh_free(_session);
182 return false;
183 }
184 LOG_DEBUG("{}: Successfully connected to the router", nameId());
185
186 // authenticate
187 if (ssh_userauth_password(_session, nullptr, _sshPassword.c_str()) != SSH_AUTH_SUCCESS)
188 {
189 LOG_INFO("{}: Authentication failed", nameId());
190 ssh_disconnect(_session);
191 ssh_free(_session);
192 return false;
193 }
194 LOG_DEBUG("{}: Authentication succeeded", nameId());
195
196 // channel
197 _channel = ssh_channel_new(_session);
198 if (_channel == nullptr)
199 {
200 LOG_INFO("{}: Failed to create SSH channel", nameId());
201 ssh_disconnect(_session);
202 ssh_free(_session);
203 return false;
204 }
205 LOG_DEBUG("{}: Successfully created SSH channel", nameId());
206
207 // open _session
208 if (ssh_channel_open_session(_channel) != SSH_OK)
209 {
210 LOG_INFO("{}: Failed to open an SSH _session", nameId());
211 ssh_channel_free(_channel);
212 ssh_disconnect(_session);
213 ssh_free(_session);
214 return false;
215 }
216 LOG_DEBUG("{}: Successfully opened an SSH _session", nameId());
217
218 // pty
219 if (ssh_channel_request_pty(_channel) != SSH_OK)
220 {
221 LOG_INFO("{}: Failed to open pty", nameId());
222 ssh_channel_free(_channel);
223 ssh_disconnect(_session);
224 ssh_free(_session);
225 return false;
226 }
227 LOG_DEBUG("{}: Successfully opened pty", nameId());
228
229 // shell
230 if (ssh_channel_request_shell(_channel) != SSH_OK)
231 {
232 LOG_INFO("{}: Failed to open shell", nameId());
233 ssh_channel_free(_channel);
234 ssh_disconnect(_session);
235 ssh_free(_session);
236 return false;
237 }
238 LOG_DEBUG("{}: Successfully opened shell", nameId());
239
241
242 return true;
243}
244
246{
247 LOG_TRACE("{}: called", nameId());
248
249 if (!isInitialized())
250 {
251 return;
252 }
253
254 ssh_channel_write(_channel, "exit\n", strlen("exit\n"));
255 ssh_channel_free(_channel);
256 ssh_disconnect(_session);
257 ssh_free(_session);
258
259 if (_timer.is_running())
260 {
261 _timer.stop();
262 }
263}
264
266{
267 auto* node = static_cast<ArubaSensor*>(userData);
268 auto obs = std::make_shared<WiFiObs>();
269
270 std::array<char, 1024> buffer{};
271 std::string receivedData;
272 size_t bytesRead = 0;
273 // Send command to access point
274 ssh_channel_write(node->_channel, "show ap range scanning-results\n", strlen("show ap range scanning-results\n"));
275 // Read output
276 do {
277 bytesRead = static_cast<size_t>(ssh_channel_read_timeout(node->_channel, buffer.data(), sizeof(buffer), 0, 10)); // timeout in ms
278 if (bytesRead > 0)
279 {
280 receivedData.append(buffer.data(), bytesRead);
281 }
282 } while (bytesRead > 0);
283 // Send command to clear output
284 ssh_channel_write(node->_channel, "clear range-scanning-result\n", strlen("clear range-scanning-result\n"));
285
286 // Parse the received data
287 std::istringstream iss(receivedData);
288 std::string line;
289
290 while (std::getline(iss, line) && line.find("Peer-bssid") == std::string::npos) {} // Skip lines until the header "Peer-bssid" is found
291
292 // Skip the header lines
293 std::getline(iss, line);
294 std::getline(iss, line);
295
296 // MAC address validation
297 std::regex macRegex("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
298
299 while (std::getline(iss, line) && !line.empty())
300 {
301 std::istringstream lineStream(line);
302 std::string value;
303 std::string macAddress;
304 lineStream >> macAddress;
305
306 int rtt = 0;
307 int rssi = 0;
308 int stdValue = 0;
309
310 lineStream >> rtt >> rssi >> stdValue;
311
312 std::regex timeRegex(R"(\d{4}-\d{2}-\d{2})"); // Time format: YYYY-MM-DD
313 std::string timeStamp1;
314 std::string timeStamp2;
315 lineStream >> timeStamp1;
316 lineStream >> timeStamp2;
317 while (lineStream >> value)
318 {
319 if (std::regex_match(value, timeRegex)) // Check if the value is a time stamp
320 {
321 timeStamp1 = value;
322 break;
323 }
324 }
325 lineStream >> value;
326 timeStamp2 = value;
327 double measuredDistance = static_cast<double>(rtt) * 1e-9 / 2 * InsConst::C_AIR;
328 double measuredDistanceStd = static_cast<double>(stdValue) * 1e-9 / 2 * InsConst::C_AIR;
329 if (std::regex_match(macAddress, macRegex)) // Check if the MAC address is valid
330 {
331 InsTime_YMDHMS yearMonthDayHMS(std::stoi(timeStamp1.substr(0, 4)), std::stoi(timeStamp1.substr(5, 2)), std::stoi(timeStamp1.substr(8, 2)), std::stoi(timeStamp2.substr(0, 2)), std::stoi(timeStamp2.substr(3, 2)), std::stoi(timeStamp2.substr(6, 2)));
332 InsTime timeOfMeasurement(yearMonthDayHMS, UTC);
333 std::ranges::transform(macAddress, macAddress.begin(), ::toupper); // Convert to uppercase
334 obs->distance = measuredDistance;
335 obs->distanceStd = measuredDistanceStd;
336 obs->macAddress = macAddress;
337 obs->insTime = timeOfMeasurement;
338 // obs->insTime = util::time::GetCurrentInsTime(); // if you want to use the instinct time instead
339 node->invokeCallbacks(OUTPUT_PORT_INDEX_WIFI_OBS, obs);
340 }
341 }
342}
Aruba Sensor Class.
Save/Load the Nodes.
nlohmann::json json
json namespace
Utility class for logging to console and file.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_INFO
Info to the user on the state of the program.
Definition Logger.hpp:69
#define LOG_TRACE
Detailled info to trace the execution of the program. Should not be called on functions which receive...
Definition Logger.hpp:65
Manages all Nodes.
Keeps track of the current real/simulation time.
Espressif Observation Class.
std::string type() const override
String representation of the Class Type.
std::string _sshHost
Ssh options.
ssh_session _session
SSH session.
static constexpr size_t OUTPUT_PORT_INDEX_WIFI_OBS
Flow (WiFiObs)
CallbackTimer _timer
Timer object to handle async data requests.
~ArubaSensor() override
Destructor.
void guiConfig() override
ImGui config window which is shown on double click.
std::string _sshPublickeyAcceptedTypes
Public key type.
static void readSensorDataThread(void *userData)
Function which performs the async data reading.
void restore(const json &j) override
Restores the node from a json object.
void deinitialize() override
Deinitialize the node.
static std::string category()
String representation of the Class Category.
static std::string typeStatic()
String representation of the Class Type.
json save() const override
Saves the node into a json object.
std::string _sshHostkeys
SSH encryption.
std::string _sshPassword
User credentials.
ArubaSensor()
Default constructor.
std::string _sshKeyExchange
Key exchange.
ssh_channel _channel
SSH channel.
bool resetNode() override
Resets the node. It is guaranteed that the node is initialized when this is called.
int _outputInterval
Output interval in ms.
std::string _sshUser
User name.
bool initialize() override
Initialize the node.
static constexpr double C_AIR
Speed of light in air: CGPM defined speed of light divided by approximate refractory index of dry air...
Definition Constants.hpp:37
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
bool isInitialized() const
Checks if the node is initialized.
Definition Node.cpp:504
bool doDeinitialize(bool wait=false)
Asks the node worker to deinitialize the node.
Definition Node.cpp:395
ImVec2 _guiConfigDefaultWindowSize
Definition Node.hpp:410
Node(std::string name)
Constructor.
Definition Node.cpp:30
std::string nameId() const
Node name and id.
Definition Node.cpp:253
std::string name
Name of the Node.
Definition Node.hpp:395
bool _onlyRealTime
Whether the node can run in post-processing or only real-time.
Definition Node.hpp:419
bool _hasConfig
Flag if the config window should be shown.
Definition Node.hpp:413
static std::string type()
Returns the type of the data class.
Definition WiFiObs.hpp:27
OutputPin * CreateOutputPin(Node *node, const char *name, Pin::Type pinType, const std::vector< std::string > &dataIdentifier, OutputPin::PinData data=static_cast< void * >(nullptr), int idx=-1)
Create an Output Pin object.
void ApplyChanges()
Signals that there have been changes to the flow.
@ UTC
Coordinated Universal Time.
Universal Time Coordinated [UTC].
Definition InsTime.hpp:465
@ Flow
NodeData Trigger.
Definition Pin.hpp:52