Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <ros/ros.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <unistd.h>
00041 #include <vector>
00042 #include <string>
00043 #include <list>
00044 #include <numeric>
00045 #include <iostream>
00046 #include <sstream>
00047 #include <wifi_comm/WiFiNeighboursList.h>
00048
00049
00050 #define NODE_VERSION 0.01
00051 #define BUFFSIZE 1024
00052 #define LINK_HEADER "Table: Links"
00053
00054
00055 std::string char2string(char *in)
00056 {
00057 std::string str(in);
00058 return str;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 void getNeighboursInfo(wifi_comm::WiFiNeighboursList * neighbours)
00079 {
00080 FILE *ptr;
00081 char buf[BUFFSIZE];
00082 char link_cmd[] = "wget -q -O - localhost:8080/link";
00083 std::vector<std::string> neighboursList;
00084 std::string myIP;
00085 std::vector<int> signalList;
00086
00087
00088 if((ptr = popen(link_cmd, "r")) != NULL)
00089 {
00090
00091 fgets(buf, BUFFSIZE, ptr);
00092
00093 if(strcmp(buf, LINK_HEADER) == 0)
00094 {
00095 ROS_INFO("buffer:%s",buf);
00096 ROS_WARN("Received Header is not a valid Header (%s)", LINK_HEADER);
00097 exit(1);
00098 }
00099
00100
00101 fgets(buf, BUFFSIZE, ptr);
00102
00103
00104 char my_ip[16], other_ip[16];
00105 float hyst, lq, nlq, cost;
00106 std::string ip_str;
00107
00108 while(fgets(buf, BUFFSIZE, ptr) != NULL && strlen(buf) > 1)
00109 {
00110
00111 sscanf (buf,"%s %s %f %f %f %f", my_ip, other_ip, &hyst, &lq, &nlq, &cost);
00112
00113
00114
00115 myIP = char2string(my_ip);
00116
00117 ip_str = char2string(other_ip);
00118
00119
00120
00121 if(lq > 0.0)
00122 {
00123 neighboursList.push_back(ip_str);
00124
00125 int ilq = (int)(lq*100);
00126
00127 signalList.push_back(ilq);
00128 }
00129 }
00130
00131 neighbours->self_ip = myIP;
00132
00133 neighbours->neighbours.clear();
00134
00135
00136 std::vector<std::string>::iterator it;
00137
00138 int index;
00139
00140 for(it = neighboursList.begin(); it != neighboursList.end(); it++)
00141 {
00142
00143
00144 index = int(it - neighboursList.begin());
00145
00146
00147 wifi_comm::WiFiNeighbour * nod = new wifi_comm::WiFiNeighbour();
00148
00149 nod->ip = (*it);
00150 nod->quality = signalList[index];
00151 neighbours->neighbours.push_back(*nod);
00152 }
00153 }
00154 pclose(ptr);
00155 }
00156
00157
00158
00159
00160 int main( int argc, char** argv )
00161 {
00162 ros::init(argc, argv, "wifi_discovery");
00163 ros::NodeHandle n;
00164
00165 wifi_comm::WiFiNeighboursList neighbours;
00166
00167 ros::Publisher list = n.advertise<wifi_comm::WiFiNeighboursList>("wifi_neighbours_list", 10);
00168
00169 ROS_INFO("Wifi Discovery Node for ROS %.2f", NODE_VERSION);
00170
00171 int freq;
00172 n.param("wifi_discovery_node/frequency", freq, 1);
00173 ros::Rate r(freq);
00174
00175 while(ros::ok())
00176 {
00177
00178 getNeighboursInfo(&neighbours);
00179
00180
00181 list.publish(neighbours);
00182
00183
00184 r.sleep();
00185 }
00186
00187 return 0;
00188 }
00189
00190
00191