From faf44d4b26f6e098933e8e394ac04588211a4ef1 Mon Sep 17 00:00:00 2001 From: Florian Zirker Date: Mon, 26 Nov 2018 23:39:18 +0100 Subject: [PATCH] get value direct with linux/wireless.h --- src/wlanSignal.cpp | 80 +++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/src/wlanSignal.cpp b/src/wlanSignal.cpp index 1b949a0..d905ee7 100644 --- a/src/wlanSignal.cpp +++ b/src/wlanSignal.cpp @@ -3,77 +3,71 @@ #include #include #include +#include +#include +#include +#include +#include using namespace std; using namespace ros; using namespace wlan_pioneer; -string GetStdoutFromCommand(const string &cmd) -{ - string data; - FILE *stream; - const int max_buffer = 256; - char buffer[max_buffer]; - //cmd.append(" 2>&1"); - stream = popen(cmd.c_str(), "r"); - if (stream) - { - while (!feof(stream)) - { - if (fgets(buffer, max_buffer, stream) != NULL) - { - data.append(buffer); - } - } - pclose(stream); +int64_t getWlanSignalStrength(const string &interface) { + int sockfd; + struct iw_statistics stats; + struct iwreq req; + memset(&stats, 0, sizeof(stats)); + memset(&req, 0, sizeof(iwreq)); + strncpy(req.ifr_name, interface.c_str(), 16); + req.u.data.pointer = &stats; + req.u.data.length = sizeof(iw_statistics); +#ifdef CLEAR_UPDATED + req.u.data.flags = 1; +#endif + + if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + ROS_ERROR("Could not create simple datagram socket"); + exit(EXIT_FAILURE); } - return data; + if(ioctl(sockfd, SIOCGIWSTATS, &req) == -1) { + ROS_ERROR("Error performing SIOCGIWSTATS"); + close(sockfd); + exit(EXIT_FAILURE); + } + close(sockfd); + return (int8_t)stats.qual.level; } -int64_t getWlanSignalStrength(const string &interface) -{ - stringstream command; - command << "iwconfig " << interface << " | grep Signal " - << "| cut -d \"=\" -f3 " - << "| cut -d \" \" -f1"; - return stol(GetStdoutFromCommand(command.str())); -} -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { init(argc, argv, "wlanSignal"); NodeHandle node; string wlanInterface24G; string wlanInterface5G; - if (node.param("wlan_interface_24G", wlanInterface24G, "wlan0")) - { - ROS_INFO("Param wlan_interface_24: %s", wlanInterface24G.c_str()); + if (node.param("wlan_interface_24G", wlanInterface24G, "wlan0")) { + ROS_INFO("Param wlan_interface_24G: %s", wlanInterface24G.c_str()); } - else - { - ROS_INFO("Param 'wlan_interface_24' not set. Using 'wlan0'"); + else { + ROS_INFO("Param 'wlan_interface_24G' not set. Using 'wlan0'"); } - if (node.param("wlan_interface_5G", wlanInterface5G, "wlan0")) - { + if (node.param("wlan_interface_5G", wlanInterface5G, "wlan0")) { ROS_INFO("Param wlan_interface_5G: %s", wlanInterface5G.c_str()); } - else - { + else { ROS_INFO("Param 'wlan_interface_5G' not set. Using 'wlan0'"); } // topic: wlan_signal Publisher publisher = node.advertise("wlan_signal", 1000); - Rate loopRate(1); // every second - while (ros::ok()) - { + while (ok()) { WlanSignalMsg msg; - msg.timestamp = ros::Time::now(); + msg.timestamp = Time::now(); msg.level_24G = getWlanSignalStrength(wlanInterface24G); msg.level_5G = getWlanSignalStrength(wlanInterface5G);