S Morita / mbed-mros2

Dependents:   mbed-os-example-mros2 example-mbed-mros2-sub-pose example-mbed-mros2-pub-twist example-mbed-mros2-mturtle-teleop

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers common.c Source File

common.c

00001 // Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <ucdr/common.h>
00016 
00017 #include <string.h>
00018 
00019 #if __BIG_ENDIAN__
00020     const ucdrEndianness UCDR_MACHINE_ENDIANNESS = UCDR_BIG_ENDIANNESS;
00021 #else
00022     const ucdrEndianness UCDR_MACHINE_ENDIANNESS = UCDR_LITTLE_ENDIANNESS;
00023 #endif
00024 
00025 // -------------------------------------------------------------------
00026 //                   INTERNAL UTIL IMPLEMENTATIONS
00027 // -------------------------------------------------------------------
00028 bool ucdr_check_buffer(ucdrBuffer* mb, const uint32_t bytes)
00029 {
00030     if(!mb->error)
00031     {
00032         bool fit = mb->iterator + bytes <= mb->final;
00033         if(!fit)
00034         {
00035             mb->error = true;
00036         }
00037     }
00038 
00039     return !mb->error;
00040 }
00041 
00042 // -------------------------------------------------------------------
00043 //                       PUBLIC IMPLEMENTATION
00044 // -------------------------------------------------------------------
00045 void ucdr_init_buffer(ucdrBuffer* mb, const uint8_t* data, const uint32_t size)
00046 {
00047     ucdr_init_buffer_offset(mb, data, size, 0U);
00048 }
00049 
00050 void ucdr_init_buffer_offset(ucdrBuffer* mb, const uint8_t* data, const uint32_t size, uint32_t offset)
00051 {
00052     ucdr_init_buffer_offset_endian(mb, data, size, offset, UCDR_MACHINE_ENDIANNESS);
00053 }
00054 
00055 void ucdr_init_buffer_offset_endian(ucdrBuffer* mb, const uint8_t* data, const uint32_t size, uint32_t offset, ucdrEndianness endianness)
00056 {
00057     mb->init = data;
00058     mb->final = mb->init + size;
00059     mb->iterator = (uint8_t*)mb->init + offset;
00060     mb->last_data_size = 0U;
00061     mb->endianness = endianness;
00062     mb->error = false;
00063 }
00064 
00065 
00066 void ucdr_copy_buffer(ucdrBuffer* mb_dest, const ucdrBuffer* mb_source)
00067 {
00068     memcpy(mb_dest, mb_source, sizeof(ucdrBuffer));
00069 }
00070 
00071 void ucdr_reset_buffer(ucdrBuffer* mb)
00072 {
00073     ucdr_reset_buffer_offset(mb, 0U);
00074 }
00075 
00076 void ucdr_reset_buffer_offset(ucdrBuffer* mb, const uint32_t offset)
00077 {
00078     mb->iterator = (uint8_t*) mb->init + offset;
00079     mb->last_data_size = 0U;
00080     mb->error = false;
00081 }
00082 
00083 void ucdr_align_to(ucdrBuffer* mb, const uint32_t size)
00084 {
00085     uint32_t offset = ucdr_buffer_alignment(mb, size);
00086     mb->iterator += offset;
00087     if(mb->iterator > mb->final)
00088     {
00089         mb->iterator = (uint8_t*)mb->final;
00090     }
00091 
00092     mb->last_data_size = size;
00093 }
00094 
00095 uint32_t ucdr_alignment(uint32_t current_alignment, const uint32_t data_size)
00096 {
00097     return ((data_size - (current_alignment % data_size)) & (data_size - 1));
00098 }
00099 
00100 uint32_t ucdr_buffer_alignment(const ucdrBuffer* mb, const uint32_t data_size)
00101 {
00102     if(data_size > mb->last_data_size)
00103     {
00104         return (data_size - ((uint32_t)(mb->iterator - mb->init) % data_size)) & (data_size - 1);
00105     }
00106 
00107     return 0;
00108 }
00109 
00110 size_t ucdr_buffer_size(const ucdrBuffer* mb)
00111 {
00112     return (size_t)(mb->final - mb->init);
00113 }
00114 
00115 size_t ucdr_buffer_length(const ucdrBuffer* mb)
00116 {
00117     return (size_t)(mb->iterator - mb->init);
00118 }
00119 
00120 size_t ucdr_buffer_remaining(const ucdrBuffer* mb)
00121 {
00122     return (size_t)(mb->final - mb->iterator);
00123 }
00124 
00125 ucdrEndianness ucdr_buffer_endianness(const ucdrBuffer* mb)
00126 {
00127     return mb->endianness;
00128 }
00129 
00130 bool ucdr_buffer_has_error(const ucdrBuffer* mb)
00131 {
00132     return mb->error;
00133 }