From 2797a8e6f758c1f8a252f5cd4b3e3e90e7264a1c Mon Sep 17 00:00:00 2001 From: Florian Zirker Date: Tue, 27 Nov 2018 14:27:21 +0100 Subject: [PATCH] using int8_t, dont crash on error and better output --- msg/WlanSignalMsg.msg | 4 ++-- src/wlanSignal.cpp | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/msg/WlanSignalMsg.msg b/msg/WlanSignalMsg.msg index dda92f8..6d4a5da 100644 --- a/msg/WlanSignalMsg.msg +++ b/msg/WlanSignalMsg.msg @@ -1,3 +1,3 @@ time timestamp -int64 level_24G -int64 level_5G \ No newline at end of file +int8 level_24G +int8 level_5G \ No newline at end of file diff --git a/src/wlanSignal.cpp b/src/wlanSignal.cpp index d905ee7..0d9038b 100644 --- a/src/wlanSignal.cpp +++ b/src/wlanSignal.cpp @@ -8,14 +8,14 @@ #include #include #include +#include using namespace std; using namespace ros; using namespace wlan_pioneer; -int64_t getWlanSignalStrength(const string &interface) { - int sockfd; +int8_t getWlanSignalStrength(const string &interface) { struct iw_statistics stats; struct iwreq req; memset(&stats, 0, sizeof(stats)); @@ -27,17 +27,19 @@ int64_t getWlanSignalStrength(const string &interface) { req.u.data.flags = 1; #endif - if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if(sockfd == -1) { ROS_ERROR("Could not create simple datagram socket"); - exit(EXIT_FAILURE); + return numeric_limits::min() ; } if(ioctl(sockfd, SIOCGIWSTATS, &req) == -1) { - ROS_ERROR("Error performing SIOCGIWSTATS"); + ROS_ERROR("Error retrieving WLAN signal strength "); close(sockfd); - exit(EXIT_FAILURE); + return numeric_limits::min() ; + } close(sockfd); - return (int8_t)stats.qual.level; + return stats.qual.level; } @@ -71,7 +73,7 @@ int main(int argc, char **argv) { msg.level_24G = getWlanSignalStrength(wlanInterface24G); msg.level_5G = getWlanSignalStrength(wlanInterface5G); - ROS_INFO("Signal strength 2.4G: %li Signal strength 5G: %li", msg.level_24G, msg.level_5G); + ROS_INFO("Signal strength 2.4G: %i Signal strength 5G: %i", msg.level_24G, msg.level_5G); publisher.publish(msg); spinOnce();