Skip to content

Commit

Permalink
Added functions for endian-agnostic memory copying (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanFeofanov authored and mikepurvis committed Oct 11, 2016
1 parent f8a46f3 commit f8f3e76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 20 additions & 2 deletions rosserial_client/src/ros_lib/ros/msg.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
Expand Down Expand Up @@ -36,6 +36,7 @@
#define _ROS_MSG_H_

#include <stdint.h>
#include <stddef.h>

namespace ros {

Expand Down Expand Up @@ -114,14 +115,31 @@ class Msg
if (exp != 0)
{
*val |= ((exp) - 1023 + 127) << 23;
}
}

// Copy negative sign.
*val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;

return 8;
}

// Copy data from variable into a byte array
template<typename A, typename V>
static void varToArr(A arr, const V var)
{
for(size_t i = 0; i < sizeof(V); i++)
arr[i] = (var >> (8 * i));
}

// Copy data from a byte array into variable
template<typename V, typename A>
static void arrToVar(V& var, const A arr)
{
var = 0;
for(size_t i = 0; i < sizeof(V); i++)
var |= (arr[i] << (8 * i));
}

};

} // namespace ros
Expand Down
4 changes: 2 additions & 2 deletions rosserial_client/src/rosserial_client/make_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ def make_declaration(self, f):
def serialize(self, f):
cn = self.name.replace("[","").replace("]","")
f.write(' uint32_t length_%s = strlen(this->%s);\n' % (cn,self.name))
f.write(' memcpy(outbuffer + offset, &length_%s, sizeof(uint32_t));\n' % cn)
f.write(' varToArr(outbuffer + offset, length_%s);\n' % cn)
f.write(' offset += 4;\n')
f.write(' memcpy(outbuffer + offset, this->%s, length_%s);\n' % (self.name,cn))
f.write(' offset += length_%s;\n' % cn)

def deserialize(self, f):
cn = self.name.replace("[","").replace("]","")
f.write(' uint32_t length_%s;\n' % cn)
f.write(' memcpy(&length_%s, (inbuffer + offset), sizeof(uint32_t));\n' % cn)
f.write(' arrToVar(length_%s, (inbuffer + offset));\n' % cn)
f.write(' offset += 4;\n')
f.write(' for(unsigned int k= offset; k< offset+length_%s; ++k){\n'%cn) #shift for null character
f.write(' inbuffer[k-1]=inbuffer[k];\n')
Expand Down

0 comments on commit f8f3e76

Please sign in to comment.