forked from rdkcentral/networkmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFiSignalStrengthMonitor.cpp
140 lines (127 loc) · 5.6 KB
/
WiFiSignalStrengthMonitor.cpp
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
#include <cstdio>
#include <thread>
#include <chrono>
#include <atomic>
#include "NetworkManagerLogger.h"
#include "NetworkManagerImplementation.h"
#include "WiFiSignalStrengthMonitor.h"
#define BUFFER_SIZE 512
#define rssid_command "wpa_cli signal_poll"
#define ssid_command "wpa_cli status"
namespace WPEFramework
{
namespace Plugin
{
static const float signalStrengthThresholdExcellent = -50.0f;
static const float signalStrengthThresholdGood = -60.0f;
static const float signalStrengthThresholdFair = -67.0f;
extern NetworkManagerImplementation* _instance;
std::string WiFiSignalStrengthMonitor::retrieveValues(const char *command, const char* keyword, char *output_buffer, size_t output_buffer_size)
{
std::string key, value;
std::string keystr = "";
FILE *fp = popen(command, "r");
if (!fp)
{
NMLOG_ERROR("Failed in getting output from command %s \n",command);
return keystr;
}
while ((!feof(fp)) && (fgets(output_buffer, output_buffer_size, fp) != NULL))
{
std::istringstream mystream(output_buffer);
if(std::getline(std::getline(mystream, key, '=') >> std::ws, value))
if (key == keyword) {
keystr = value;
break;
}
}
pclose(fp);
return keystr;
}
void WiFiSignalStrengthMonitor::getSignalData(std::string &ssid, Exchange::INetworkManager::WiFiSignalQuality &quality, std::string &strengthOut)
{
float signalStrengthOut = 0.0f;
char buff[BUFFER_SIZE] = {'\0'};
ssid = retrieveValues(ssid_command, "ssid", buff, sizeof (buff));
if (ssid.empty())
{
NMLOG_ERROR("ssid is empty");
quality = Exchange::INetworkManager::WIFI_SIGNAL_DISCONNECTED;
return;
}
string signalStrength = retrieveValues(rssid_command, "RSSI", buff, sizeof (buff));
if (!signalStrength.empty()) {
signalStrengthOut = std::stof(signalStrength.c_str());
strengthOut = signalStrength;
}
else
NMLOG_ERROR("signalStrength is empty");
NMLOG_TRACE("SSID = %s Signal Strength %f db", ssid.c_str(), signalStrengthOut);
if (signalStrengthOut == 0.0f)
{
quality = Exchange::INetworkManager::WIFI_SIGNAL_DISCONNECTED;
}
else if (signalStrengthOut >= signalStrengthThresholdExcellent && signalStrengthOut < 0)
{
quality = Exchange::INetworkManager::WIFI_SIGNAL_EXCELLENT;
}
else if (signalStrengthOut >= signalStrengthThresholdGood && signalStrengthOut < signalStrengthThresholdExcellent)
{
quality = Exchange::INetworkManager::WIFI_SIGNAL_GOOD;
}
else if (signalStrengthOut >= signalStrengthThresholdFair && signalStrengthOut < signalStrengthThresholdGood)
{
quality = Exchange::INetworkManager::WIFI_SIGNAL_FAIR;
}
else
{
quality = Exchange::INetworkManager::WIFI_SIGNAL_WEAK;
};
}
void WiFiSignalStrengthMonitor::startWiFiSignalStrengthMonitor(int interval)
{
stopThread = false;
if (isRunning) {
NMLOG_INFO("WiFiSignalStrengthMonitor Thread is already running.");
return;
}
isRunning = true;
monitorThread = std::thread(&WiFiSignalStrengthMonitor::monitorThreadFunction, this, interval);
monitorThread.detach();
NMLOG_INFO("Thread started with interval: %d seconds", interval);
}
void WiFiSignalStrengthMonitor::monitorThreadFunction(int interval)
{
static Exchange::INetworkManager::WiFiSignalQuality oldSignalQuality = Exchange::INetworkManager::WIFI_SIGNAL_DISCONNECTED;
NMLOG_INFO("WiFiSignalStrengthMonitor thread started !");
while (!stopThread)
{
string ssid = "";
string signalStrength;
Exchange::INetworkManager::WiFiSignalQuality newSignalQuality;
if (_instance != nullptr)
{
NMLOG_TRACE("checking WiFi signal strength");
getSignalData(ssid, newSignalQuality, signalStrength);
if(oldSignalQuality != newSignalQuality)
{
NMLOG_INFO("Notifying WiFiSignalStrengthChangedEvent %s", signalStrength.c_str());
oldSignalQuality = newSignalQuality;
_instance->ReportWiFiSignalStrengthChangedEvent(ssid, signalStrength, newSignalQuality);
}
if(newSignalQuality == Exchange::INetworkManager::WIFI_SIGNAL_DISCONNECTED)
{
NMLOG_WARNING("WiFiSignalStrengthChanged to disconnect - WiFiSignalStrengthMonitor exiting");
stopThread= false;
break; // Let the thread exit naturally
}
}
else
NMLOG_FATAL("NetworkManagerImplementation pointer error !");
// Wait for the specified interval or until notified to stop
std::this_thread::sleep_for(std::chrono::seconds(interval));
}
isRunning = false;
}
}
}