wifi_discovery_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita and Pedro Sousa
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 // defines
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  * EXAMPLE OF THE INPUT
00064 Table: Neighbors
00065 IP address      SYM     MPR     MPRS    Will.   2 Hop Neighbors
00066 192.168.10.4    NO      NO      NO      6       1
00067 192.168.10.8    YES     NO      NO      3       1
00068 192.168.10.7    YES     YES     NO      3       2
00069 * 
00070 Table: Links
00071 Local IP        Remote IP       Hyst.   LQ      NLQ     Cost
00072 192.168.10.6    192.168.10.8    0.00    1.000   1.000   1.000   
00073 192.168.10.6    192.168.10.4    0.00    1.000   0.000   INFINITE        
00074 192.168.10.6    192.168.10.7    0.00    1.000   1.000   1.000   
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         // check if can be used
00088         if((ptr = popen(link_cmd, "r")) != NULL)
00089         {
00090                 //read the first line
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                 // read next line and ignore - column description
00101                 fgets(buf, BUFFSIZE, ptr);
00102                 
00103                 // parsing variables
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                         //ROS_INFO("rcv:%s", buf);
00111                         sscanf (buf,"%s %s %f %f %f %f", my_ip, other_ip, &hyst, &lq, &nlq, &cost);
00112                         //ROS_INFO("Parsed - my:%s   other:%s  H:%2.3f  LQ:%2.3f  NLQ:%2.3f  cost:%2.3f", my_ip, other_ip, hyst, lq, nlq, cost);
00113                         
00114                         // my IP                                        
00115                         myIP = char2string(my_ip);
00116                         // other IP
00117                         ip_str = char2string(other_ip);
00118                         
00119                         // add to neigbour vector if link quality (LQ) is not zero
00120                         // if LQ is zero the IP can be listed but is not connected
00121                         if(lq > 0.0)
00122                         {
00123                                 neighboursList.push_back(ip_str);
00124                         
00125                                 int ilq = (int)(lq*100);
00126                                 // add to link quality vector
00127                                 signalList.push_back(ilq);
00128                         }
00129                 }
00130                 
00131                 neighbours->self_ip = myIP;
00132                 // clear neigbours buffer from MSG
00133                 neighbours->neighbours.clear();
00134 
00135                 // create a vector<string>::iterator and set it to the beginning of the vector
00136                 std::vector<std::string>::iterator it;
00137                         
00138                 int index;
00139                 // Now, we iterate through the array until the iterator exceeds
00140                 for(it = neighboursList.begin(); it != neighboursList.end(); it++)
00141                 {
00142                         //ROS_INFO("index:%d ", index);
00143                         // list index
00144                         index = int(it - neighboursList.begin());
00145                         
00146                         // one node
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 // Main function for the multiRobotCom node
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         // main loop
00175         while(ros::ok())
00176         {
00177                 // Do stuff...
00178                 getNeighboursInfo(&neighbours);
00179 
00180                 // Publish data
00181                 list.publish(neighbours);
00182                 
00183                 //ros::spinOnce();
00184                 r.sleep();
00185         }
00186 
00187         return 0;
00188 }
00189 
00190 // EOF
00191 


wifi_comm
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:27:05