Basic implementation of Xbus message parsing and generation for embedded processors. The code has no dependencies and should also work for other MCU architectures than ARM provided a C99 compiler is available.

Dependents:   MTi-1_example_LPC1768 MTi-1_rikbeun MTi-1_example MTi-1_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xbusutility.c Source File

xbusutility.c

Go to the documentation of this file.
00001 /*!
00002  * \file
00003  * \copyright Copyright (C) Xsens Technologies B.V., 2015.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00006  * use this file except in compliance with the License. You may obtain a copy
00007  * of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00014  * License for the specific language governing permissions and limitations
00015  * under the License.
00016  */
00017 
00018 #include "xbusutility.h "
00019 
00020 /*!
00021  * \name Xbus message reading utility functions.
00022  * Xbus messages use big-endian representations for multibyte data.
00023  * These functions help in reading data from an Xbus message and converting
00024  * it to a native type.
00025  *
00026  * The functions are intended to scan over a message as follows:
00027  * \code
00028  * void readValues(XbusMessage const* message)
00029  * {
00030  *     uint16_t v1;
00031  *     uint8_t v2;
00032  *     uint32_t v3;
00033  *     uint8_t* dptr = message->data;
00034  *     dptr = XbusUtility_readU16(&v1, dptr);
00035  *     dptr = XbusUtility_readU8(&v2, dptr);
00036  *     dptr = XbusUtility_readU32(&v3, dptr);
00037  * }
00038  * \endcode
00039  * \{
00040  */
00041 /*!
00042  * \brief Read a uint8_t value from an Xbus message.
00043  */
00044 uint8_t const* XbusUtility_readU8(uint8_t* out, uint8_t const* in)
00045 {
00046     *out = *in;
00047     return ++in;
00048 }
00049 
00050 /*! \brief Read a uint16_t value from an Xbus message. */
00051 uint8_t const* XbusUtility_readU16(uint16_t* out, uint8_t const* in)
00052 {
00053     *out = (in[0] << 8) | in[1];
00054     return in + sizeof(uint16_t);
00055 }
00056 
00057 /*! \brief Read a uint32_t value from an Xbus message. */
00058 uint8_t const* XbusUtility_readU32(uint32_t* out, uint8_t const* in)
00059 {
00060     *out = (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3];
00061     return in + sizeof(uint32_t);
00062 }
00063 /*! \} */
00064 
00065 /*!
00066  * \name Xbus message writing utility functions.
00067  * These functions aid in writing native values to big-endian xbus message
00068  * payloads. See corresponding reading functions for further details.
00069  * \{
00070  */
00071 /*! \brief Write a uint8_t value to an Xbus message. */
00072 uint8_t* XbusUtility_writeU8(uint8_t* out, uint8_t in)
00073 {
00074     *out++ = in;
00075     return out;
00076 }
00077 
00078 /*! \brief Write a uint16_t value to an Xbus message. */
00079 uint8_t* XbusUtility_writeU16(uint8_t* out, uint16_t in)
00080 {
00081     *out++ = (in >> 8) & 0xFF;
00082     *out++ = in & 0xFF;
00083     return out;
00084 }
00085 
00086 /*! \brief Write a uint32_t value to an Xbus message. */
00087 uint8_t* XbusUtility_writeU32(uint8_t* out, uint32_t in)
00088 {
00089     *out++ = (in >> 24) & 0xFF;
00090     *out++ = (in >> 16) & 0xFF;
00091     *out++ = (in >> 8) & 0xFF;
00092     *out++ = in & 0xFF;
00093     return out;
00094 }
00095 /*! \}  */