functions.h
Go to the documentation of this file.
00001 /*
00002  * functions.h
00003  *
00004  *  Created on: Apr 14, 2014
00005  *      Author: cwioro
00006  */
00007 
00008 #ifndef FUNCTIONS_H_
00009 #define FUNCTIONS_H_
00010 
00011 struct mac {
00012     unsigned char mac_adr[6];
00013 
00014     mac()
00015     {
00016     }
00017 
00018     mac(unsigned char* m)
00019     {
00020         memcpy((void*) mac_adr, m, 6);
00021     }
00022 };
00023 
00024 template <class t>
00025 void desializeObject(unsigned char* serialized_pose_stamp, uint32_t length, t* obj);
00026 void initMacFromString(unsigned char* mac, const char* mac_str);
00027 const char* getMacAsCStr(unsigned char* mac);
00028 void sleepMS(int* milli_sec);
00029 void sleepMS(int milli_sec);
00030 std::string getBoolAsString(bool value);
00031 
00032 const char* getPathAsCStr(std::list<mac> p);
00033 bool isBufferInList(unsigned char* buffer, std::vector<std::string>* old_buffers, uint8_t last_inserted);
00034 unsigned long getMillisecondsTime();
00035 
00036 unsigned long getMillisecondsTime()
00037 {
00038     struct timeval tv;
00039     if (gettimeofday(&tv, NULL) != 0) return 0;
00040     return (unsigned long) ((tv.tv_sec * 1000ul) + (tv.tv_usec / 1000ul));
00041 }
00042 
00043 bool isBufferInList(unsigned char* buffer, std::vector<std::string>* old_buffers, uint8_t last_inserted)
00044 {
00045     uint8_t iterations = 0;
00046     while (iterations++ < old_buffers->size())
00047     {
00048 
00049         std::string t = old_buffers->at(last_inserted--);
00050         if (t.compare(std::string((const char*) buffer, t.size())) == 0)
00051             return true;
00052 
00053 
00054         if (last_inserted >= old_buffers->size())
00055             last_inserted = old_buffers->size() - 1;
00056     }
00057 
00058     return false;
00059 }
00060 
00061 std::string getPathAsStr(std::list<mac> p)
00062 {
00063     /* Description:
00064      * returns a C-String that displayed the a path of macs in hex.
00065      */
00066     std::string path = "";
00067     mac m;
00068     for (std::list<mac>::iterator it = p.begin(); it != p.end(); ++it)
00069     {
00070         if (it != p.begin())
00071             path.append("|");
00072 
00073         m = *it;
00074         boost::format fmt("%02X-%02X-%02X-%02X-%02X-%02X");
00075         for (int i = 0; i != 6; ++i)
00076             fmt % static_cast<unsigned int> (m.mac_adr[i]);
00077 
00078         path.append(fmt.str());
00079     }
00080 
00081     return path;
00082 }
00083 
00084 bool compareMac(char* mac1, char* mac2)
00085 {
00086 
00087 
00088     for (int i = 0; i < 6; i++)
00089     {
00090         if (mac1[i] != mac2[i])
00091         {
00092 
00093             return false;
00094         }
00095     }
00096     return true;
00097 }
00098 
00099 bool compareMac(const unsigned char mac1[6], const unsigned char mac2[6])
00100 {
00101     //  return true;
00102 
00103     for (int i = 0; i < 6; i++)
00104     {
00105         if (mac1[i] != mac2[i])
00106         {
00107 
00108             return false;
00109         }
00110     }
00111     return true;
00112 
00113     return compareMac((char*) mac1, (char*) mac2);
00114 }
00115 
00116 std::list<uint32_t> getRandomNumbers(uint32_t return_list_size, uint32_t max)
00117 {
00118     uint32_t max_numbers = max > return_list_size ? return_list_size : max;
00119     std::list<uint32_t> rand_n;
00120     while (rand_n.size() <= max_numbers)
00121     {
00122         uint32_t r = rand() % max + 1;
00123         bool number_exsists = false;
00124         for (std::list<uint32_t>::iterator it = rand_n.begin(); it != rand_n.end(); ++it)
00125         {
00126             if (*it == r)
00127                 number_exsists = true;
00128         }
00129         if (!number_exsists)
00130             rand_n.push_front(r);
00131     }
00132 
00133     return rand_n;
00134 
00135 }
00136 
00137 template <class t>
00138 std::string getSerializedMessage(t message)
00139 {
00140     /* Description:
00141      * returns a serialized ROS message as string to send it over network.
00142      */
00143     uint32_t serial_size = ros::serialization::serializationLength(message);
00144     boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00145     ros::serialization::OStream streamOut(buffer.get(), serial_size);
00146     ros::serialization::serialize(streamOut, message);
00147 
00148     std::string serializedMap = "";
00149     serializedMap.append((const char*) buffer.get(), serial_size);
00150 
00151     return serializedMap;
00152 }
00153 
00154 
00155 /*
00156 const char* getMacAsCStr(unsigned char* mac)
00157 {
00158 
00159     /* Description:
00160      * returns a C-String that displayed the mac in hex.
00161      */
00162 /*
00163     boost::format fmt("%02X-%02X-%02X-%02X-%02X-%02X");
00164     for (int i = 0; i != 6; ++i)
00165         fmt % static_cast<unsigned int> (mac[i]);
00166 
00167     return fmt.str().c_str();
00168 }*/
00169 
00170 std::string getMacAsStr(unsigned char* mac)
00171 {
00172 
00173     /* Description:
00174      * returns a C-String that displayed the mac in hex.
00175      */
00176     boost::format fmt("%02X-%02X-%02X-%02X-%02X-%02X");
00177     for (int i = 0; i != 6; ++i)
00178         fmt % static_cast<unsigned int> (mac[i]);
00179 
00180     return fmt.str();
00181 }
00182 
00183 void sleepMS(int* milli_sec)
00184 {
00185 
00186     //boost::this_thread::sleep(boost::posix_time::milliseconds(*milli_sec));//todo
00187     //
00188 
00189     int sec = *milli_sec / 1000;
00190     int m_sec = *milli_sec % 1000; //*milli_sec - ( sec * 1000); 
00191     ros::Duration(sec, m_sec * 1000000).sleep();
00192 }
00193 
00194 void sleepMS(int milli_sec)
00195 {
00196     sleepMS(&milli_sec);
00197 }
00198 
00199 std::string getBoolAsString(bool value)
00200 {
00201     if (value)
00202         return std::string("true");
00203     else
00204         return std::string("false");
00205 }
00206 
00207 bool containsString(std::vector<std::string>* l ,std::string* s)
00208 {
00209     for(std::vector<std::string>::iterator i = l->begin(); i != l->end() ; i++)
00210     {
00211         if((*i).compare(*s)==0)
00212             return true;
00213     }
00214     return false;
00215 }
00216 
00217 std::string getIntAsString(unsigned long number)
00218 {
00219     std::stringstream ss; //create a stringstream
00220     ss << number; //add number to the stream
00221     return ss.str(); //return a string with the contents of the stream
00222 }
00223 
00224 void initMacFromString(unsigned char* mac, const char* mac_str)
00225 {
00226     const char* hex_str = mac_str;
00227 
00228     std::string mac_as_string;
00229     unsigned int ch;
00230 
00231     for (; std::sscanf(hex_str, "%2x", &ch) == 1; hex_str += 3)
00232         mac_as_string += ch;
00233 
00234     memcpy(mac, mac_as_string.data(), 6);
00235 }
00236 
00237 #endif /* FUNCTIONS_H_ */


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:42