msg.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  *  * Neither the name of Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote prducts derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #ifndef _ROS_MSG_H_
00036 #define _ROS_MSG_H_
00037 
00038 #include <stdint.h>
00039 #include <stddef.h>
00040 
00041 namespace ros {
00042 
00043 /* Base Message Type */
00044 class Msg
00045 {
00046 public:
00047   virtual int serialize(unsigned char *outbuffer) const = 0;
00048   virtual int deserialize(unsigned char *data) = 0;
00049   virtual const char * getType() = 0;
00050   virtual const char * getMD5() = 0;
00051 
00063   static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
00064   {
00065     const int32_t* val = (int32_t*) &f;
00066     int32_t exp = ((*val >> 23) & 255);
00067     if (exp != 0)
00068     {
00069       exp += 1023 - 127;
00070     }
00071 
00072     int32_t sig = *val;
00073     *(outbuffer++) = 0;
00074     *(outbuffer++) = 0;
00075     *(outbuffer++) = 0;
00076     *(outbuffer++) = (sig << 5) & 0xff;
00077     *(outbuffer++) = (sig >> 3) & 0xff;
00078     *(outbuffer++) = (sig >> 11) & 0xff;
00079     *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
00080     *(outbuffer++) = (exp >> 4) & 0x7F;
00081 
00082     // Mark negative bit as necessary.
00083     if (f < 0)
00084     {
00085       *(outbuffer - 1) |= 0x80;
00086     }
00087 
00088     return 8;
00089   }
00090 
00101   static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
00102   {
00103     uint32_t* val = (uint32_t*)f;
00104     inbuffer += 3;
00105 
00106     // Copy truncated mantissa.
00107     *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
00108     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
00109     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
00110     *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
00111 
00112     // Copy truncated exponent.
00113     uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
00114     exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
00115     if (exp != 0)
00116     {
00117       *val |= ((exp) - 1023 + 127) << 23;
00118     }
00119 
00120     // Copy negative sign.
00121     *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
00122 
00123     return 8;
00124   }
00125 
00126   // Copy data from variable into a byte array
00127   template<typename A, typename V>
00128   static void varToArr(A arr, const V var)
00129   {
00130     for(size_t i = 0; i < sizeof(V); i++)
00131       arr[i] = (var >> (8 * i));
00132   }
00133 
00134   // Copy data from a byte array into variable
00135   template<typename V, typename A>
00136   static void arrToVar(V& var, const A arr)
00137   {
00138     var = 0;
00139     for(size_t i = 0; i < sizeof(V); i++)
00140       var |= (arr[i] << (8 * i));
00141   }
00142 
00143 };
00144 
00145 }  // namespace ros
00146 
00147 #endif


rosserial_client
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Sat Oct 7 2017 03:08:37