From 2f28e926c35b5ffc607fa7ea6d0d01f588fe1348 Mon Sep 17 00:00:00 2001 From: Florian Zirker Date: Mon, 26 Nov 2018 19:50:11 +0100 Subject: [PATCH] configurable interfaces over ros params --- src/wlanSignal.cpp | 96 +++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/src/wlanSignal.cpp b/src/wlanSignal.cpp index e740fc5..1b949a0 100644 --- a/src/wlanSignal.cpp +++ b/src/wlanSignal.cpp @@ -10,52 +10,78 @@ 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"); + 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) + stream = popen(cmd.c_str(), "r"); + if (stream) + { + while (!feof(stream)) { - data.append(buffer); + if (fgets(buffer, max_buffer, stream) != NULL) + { + data.append(buffer); + } } - } - pclose(stream); - } - return data; + pclose(stream); + } + return data; +} + +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) { - // TODO: make interface configurable - string singalLevelCmd("iwconfig wlp3s0 | grep Signal | cut -d \"=\" -f3 | cut -d \" \" -f1"); + init(argc, argv, "wlanSignal"); + NodeHandle node; + string wlanInterface24G; + string wlanInterface5G; - init(argc, argv, "wlanSignal"); - NodeHandle node; + if (node.param("wlan_interface_24G", wlanInterface24G, "wlan0")) + { + ROS_INFO("Param wlan_interface_24: %s", wlanInterface24G.c_str()); + } + else + { + ROS_INFO("Param 'wlan_interface_24' not set. Using 'wlan0'"); + } - // topic: wlan_signal - Publisher publisher = node.advertise("wlan_signal", 1000); + if (node.param("wlan_interface_5G", wlanInterface5G, "wlan0")) + { + ROS_INFO("Param wlan_interface_5G: %s", wlanInterface5G.c_str()); + } + else + { + ROS_INFO("Param 'wlan_interface_5G' not set. Using 'wlan0'"); + } - Rate loopRate(1); // every second + // topic: wlan_signal + Publisher publisher = node.advertise("wlan_signal", 1000); - while (ros::ok()) - { - WlanSignalMsg msg; - msg.timestamp = ros::Time::now(); - msg.level_24G = stol(GetStdoutFromCommand(singalLevelCmd)); - msg.level_5G = 0; + Rate loopRate(1); // every second - ROS_INFO("Signal strength 2.4G: %li Signal strength 5G: %li", msg.level_24G, msg.level_5G); + while (ros::ok()) + { + WlanSignalMsg msg; + msg.timestamp = ros::Time::now(); + msg.level_24G = getWlanSignalStrength(wlanInterface24G); + msg.level_5G = getWlanSignalStrength(wlanInterface5G); - publisher.publish(msg); - spinOnce(); - loopRate.sleep(); - } - return 0; + ROS_INFO("Signal strength 2.4G: %li Signal strength 5G: %li", msg.level_24G, msg.level_5G); + + publisher.publish(msg); + spinOnce(); + loopRate.sleep(); + } + return 0; }