LCOV - code coverage report
Current view: top level - AP_PiccoloCAN/piccolo_protocol - fieldencode.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 147 0.0 %
Date: 2026-09-09 11:02:08 Functions: 0 18 0.0 %

          Line data    Source code
       1             : // fieldencode.c was generated by ProtoGen version 3.5.c
       2             : 
       3             : /*
       4             :  * This file is free software: you can redistribute it and/or modify it
       5             :  * under the terms of the GNU General Public License as published by the
       6             :  * Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This file is distributed in the hope that it will be useful, but
      10             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      12             :  * See the GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License along
      15             :  * with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  *
      17             :  * Author: Oliver Walters / Currawong Engineering Pty Ltd
      18             :  */
      19             : 
      20             : #include "fieldencode.h"
      21             : 
      22             : /*!
      23             :  * Copy a null terminated string to a destination whose maximum length (with
      24             :  * null terminator) is `maxLength`. The destination string is guaranteed to
      25             :  * have a null terminator when this operation is complete. This is a
      26             :  * replacement for strncpy().
      27             :  * \param dst receives the string, and is guaranteed to be null terminated.
      28             :  * \param src is the null terminated source string to copy.
      29             :  * \param maxLength is the size of the `dst` buffer.
      30             :  */
      31           0 : void pgstrncpy(char* dst, const char* src, int maxLength)
      32             : {
      33           0 :     int index = 0;
      34           0 :     stringToBytes(src, (uint8_t*)dst, &index, maxLength, 0);
      35           0 : }
      36             : 
      37             : 
      38             : /*!
      39             :  * Encode a null terminated string on a byte stream
      40             :  * \param string is the null termianted string to encode
      41             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
      42             :  * \param index gives the location of the first byte in the byte stream, and
      43             :  *        will be incremented by the number of bytes encoded when this function
      44             :  *        is complete.
      45             :  * \param maxLength is the maximum number of bytes that can be encoded. A null
      46             :  *        terminator is always included in the encoding.
      47             :  * \param fixedLength should be 1 to force the number of bytes encoded to be
      48             :  *        exactly equal to maxLength.
      49             :  */
      50           0 : void stringToBytes(const char* string, uint8_t* bytes, int* index, int maxLength, int fixedLength)
      51             : {
      52             :     int i;
      53             : 
      54             :     // increment byte pointer for starting point
      55           0 :     bytes += (*index);
      56             : 
      57             :     // Reserve the last byte for null termination
      58           0 :     for(i = 0; i < maxLength - 1; i++)
      59             :     {
      60           0 :         if(string[i] == 0)
      61           0 :             break;
      62             :         else
      63           0 :             bytes[i] = (uint8_t)string[i];
      64             :     }
      65             : 
      66             :     // Make sure last byte has null termination
      67           0 :     bytes[i++] = 0;
      68             : 
      69           0 :     if(fixedLength)
      70             :     {
      71             :         // Finish with null bytes
      72           0 :         for(; i < maxLength; i++)
      73           0 :             bytes[i] = 0;
      74             :     }
      75             : 
      76             :     // Return for the number of bytes we encoded
      77           0 :     (*index) += i;
      78             : 
      79           0 : }// stringToBytes
      80             : 
      81             : 
      82             : /*!
      83             :  * Copy an array of bytes to a byte stream without changing the order.
      84             :  * \param data is the array of bytes to copy.
      85             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
      86             :  * \param index gives the location of the first byte in the byte stream, and
      87             :  *        will be incremented by num when this function is complete.
      88             :  * \param num is the number of bytes to copy
      89             :  */
      90           0 : void bytesToBeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num)
      91             : {
      92             :     // increment byte pointer for starting point
      93           0 :     bytes += (*index);
      94             : 
      95             :     // Increment byte index to indicate number of bytes copied
      96           0 :     (*index) += num;
      97             : 
      98             :     // Copy the bytes without changing the order
      99           0 :     while(num > 0)
     100             :     {
     101           0 :         *(bytes++) = *(data++);
     102           0 :         num--;
     103             :     }
     104             : 
     105           0 : }// bytesToBeBytes
     106             : 
     107             : 
     108             : /*!
     109             :  * Copy an array of bytes to a byte stream while reversing the order.
     110             :  * \param data is the array of bytes to copy.
     111             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     112             :  * \param index gives the location of the first byte in the byte stream, and
     113             :  *        will be incremented by num when this function is complete.
     114             :  * \param num is the number of bytes to copy
     115             :  */
     116           0 : void bytesToLeBytes(const uint8_t* data, uint8_t* bytes, int* index, int num)
     117             : {
     118             :     // increment byte pointer for starting point
     119           0 :     bytes += (*index);
     120             : 
     121             :     // Increment byte index to indicate number of bytes copied
     122           0 :     (*index) += num;
     123             : 
     124             :     // To encode as "little endian bytes", (a nonsensical statement), reverse the byte order
     125           0 :     bytes += (num - 1);
     126             : 
     127             :     // Copy the bytes, reversing the order
     128           0 :     while(num > 0)
     129             :     {
     130           0 :         *(bytes--) = *(data++);
     131           0 :         num--;
     132             :     }
     133             : 
     134           0 : }// bytesToLeBytes
     135             : 
     136             : /*!
     137             :  * Encode a 4 byte float on a big endian byte stream.
     138             :  * \param number is the value to encode.
     139             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     140             :  * \param index gives the location of the first byte in the byte stream, and
     141             :  *        will be incremented by 4 when this function is complete.
     142             :  */
     143           0 : void float32ToBeBytes(float number, uint8_t* bytes, int* index)
     144             : {
     145             :     union
     146             :     {
     147             :         float floatValue;
     148             :         uint32_t integerValue;
     149             :     }field;
     150             : 
     151           0 :     field.floatValue = number;
     152             : 
     153           0 :     uint32ToBeBytes(field.integerValue, bytes, index);
     154           0 : }
     155             : 
     156             : /*!
     157             :  * Encode a 4 byte float on a little endian byte stream.
     158             :  * \param number is the value to encode.
     159             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     160             :  * \param index gives the location of the first byte in the byte stream, and
     161             :  *        will be incremented by 4 when this function is complete.
     162             :  */
     163           0 : void float32ToLeBytes(float number, uint8_t* bytes, int* index)
     164             : {
     165             :     union
     166             :     {
     167             :         float floatValue;
     168             :         uint32_t integerValue;
     169             :     }field;
     170             : 
     171           0 :     field.floatValue = number;
     172             : 
     173           0 :     uint32ToLeBytes(field.integerValue, bytes, index);
     174           0 : }
     175             : 
     176             : /*!
     177             :  * Encode a unsigned 4 byte integer on a big endian byte stream.
     178             :  * \param number is the value to encode.
     179             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     180             :  * \param index gives the location of the first byte in the byte stream, and
     181             :  *        will be incremented by 4 when this function is complete.
     182             :  */
     183           0 : void uint32ToBeBytes(uint32_t number, uint8_t* bytes, int* index)
     184             : {
     185             :     // increment byte pointer for starting point
     186           0 :     bytes += (*index) + 3;
     187             : 
     188           0 :     *(bytes--) = (uint8_t)(number);
     189           0 :     number = number >> 8;
     190           0 :     *(bytes--) = (uint8_t)(number);
     191           0 :     number = number >> 8;
     192           0 :     *(bytes--) = (uint8_t)(number);
     193           0 :     number = number >> 8;
     194           0 :     *bytes = (uint8_t)(number);
     195             : 
     196           0 :     (*index) += 4;
     197           0 : }
     198             : 
     199             : /*!
     200             :  * Encode a unsigned 4 byte integer on a little endian byte stream.
     201             :  * \param number is the value to encode.
     202             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     203             :  * \param index gives the location of the first byte in the byte stream, and
     204             :  *        will be incremented by 4 when this function is complete.
     205             :  */
     206           0 : void uint32ToLeBytes(uint32_t number, uint8_t* bytes, int* index)
     207             : {
     208             :     // increment byte pointer for starting point
     209           0 :     bytes += (*index);
     210             : 
     211           0 :     *(bytes++) = (uint8_t)(number);
     212           0 :     number = number >> 8;
     213           0 :     *(bytes++) = (uint8_t)(number);
     214           0 :     number = number >> 8;
     215           0 :     *(bytes++) = (uint8_t)(number);
     216           0 :     number = number >> 8;
     217           0 :     *bytes = (uint8_t)(number);
     218             : 
     219           0 :     (*index) += 4;
     220           0 : }
     221             : 
     222             : /*!
     223             :  * Encode a signed 4 byte integer on a big endian byte stream.
     224             :  * \param number is the value to encode.
     225             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     226             :  * \param index gives the location of the first byte in the byte stream, and
     227             :  *        will be incremented by 4 when this function is complete.
     228             :  */
     229           0 : void int32ToBeBytes(int32_t number, uint8_t* bytes, int* index)
     230             : {
     231             :     // increment byte pointer for starting point
     232           0 :     bytes += (*index) + 3;
     233             : 
     234           0 :     *(bytes--) = (uint8_t)(number);
     235           0 :     number = number >> 8;
     236           0 :     *(bytes--) = (uint8_t)(number);
     237           0 :     number = number >> 8;
     238           0 :     *(bytes--) = (uint8_t)(number);
     239           0 :     number = number >> 8;
     240           0 :     *bytes = (uint8_t)(number);
     241             : 
     242           0 :     (*index) += 4;
     243           0 : }
     244             : 
     245             : /*!
     246             :  * Encode a signed 4 byte integer on a little endian byte stream.
     247             :  * \param number is the value to encode.
     248             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     249             :  * \param index gives the location of the first byte in the byte stream, and
     250             :  *        will be incremented by 4 when this function is complete.
     251             :  */
     252           0 : void int32ToLeBytes(int32_t number, uint8_t* bytes, int* index)
     253             : {
     254             :     // increment byte pointer for starting point
     255           0 :     bytes += (*index);
     256             : 
     257           0 :     *(bytes++) = (uint8_t)(number);
     258           0 :     number = number >> 8;
     259           0 :     *(bytes++) = (uint8_t)(number);
     260           0 :     number = number >> 8;
     261           0 :     *(bytes++) = (uint8_t)(number);
     262           0 :     number = number >> 8;
     263           0 :     *bytes = (uint8_t)(number);
     264             : 
     265           0 :     (*index) += 4;
     266           0 : }
     267             : 
     268             : /*!
     269             :  * Encode a unsigned 3 byte integer on a big endian byte stream.
     270             :  * \param number is the value to encode.
     271             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     272             :  * \param index gives the location of the first byte in the byte stream, and
     273             :  *        will be incremented by 3 when this function is complete.
     274             :  */
     275           0 : void uint24ToBeBytes(uint32_t number, uint8_t* bytes, int* index)
     276             : {
     277             :     // increment byte pointer for starting point
     278           0 :     bytes += (*index) + 2;
     279             : 
     280           0 :     *(bytes--) = (uint8_t)(number);
     281           0 :     number = number >> 8;
     282           0 :     *(bytes--) = (uint8_t)(number);
     283           0 :     number = number >> 8;
     284           0 :     *bytes = (uint8_t)(number);
     285             : 
     286           0 :     (*index) += 3;
     287           0 : }
     288             : 
     289             : /*!
     290             :  * Encode a unsigned 3 byte integer on a little endian byte stream.
     291             :  * \param number is the value to encode.
     292             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     293             :  * \param index gives the location of the first byte in the byte stream, and
     294             :  *        will be incremented by 3 when this function is complete.
     295             :  */
     296           0 : void uint24ToLeBytes(uint32_t number, uint8_t* bytes, int* index)
     297             : {
     298             :     // increment byte pointer for starting point
     299           0 :     bytes += (*index);
     300             : 
     301           0 :     *(bytes++) = (uint8_t)(number);
     302           0 :     number = number >> 8;
     303           0 :     *(bytes++) = (uint8_t)(number);
     304           0 :     number = number >> 8;
     305           0 :     *bytes = (uint8_t)(number);
     306             : 
     307           0 :     (*index) += 3;
     308           0 : }
     309             : 
     310             : /*!
     311             :  * Encode a signed 3 byte integer on a big endian byte stream.
     312             :  * \param number is the value to encode.
     313             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     314             :  * \param index gives the location of the first byte in the byte stream, and
     315             :  *        will be incremented by 3 when this function is complete.
     316             :  */
     317           0 : void int24ToBeBytes(int32_t number, uint8_t* bytes, int* index)
     318             : {
     319             :     // increment byte pointer for starting point
     320           0 :     bytes += (*index) + 2;
     321             : 
     322           0 :     *(bytes--) = (uint8_t)(number);
     323           0 :     number = number >> 8;
     324           0 :     *(bytes--) = (uint8_t)(number);
     325           0 :     number = number >> 8;
     326           0 :     *bytes = (uint8_t)(number);
     327             : 
     328           0 :     (*index) += 3;
     329           0 : }
     330             : 
     331             : /*!
     332             :  * Encode a signed 3 byte integer on a little endian byte stream.
     333             :  * \param number is the value to encode.
     334             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     335             :  * \param index gives the location of the first byte in the byte stream, and
     336             :  *        will be incremented by 3 when this function is complete.
     337             :  */
     338           0 : void int24ToLeBytes(int32_t number, uint8_t* bytes, int* index)
     339             : {
     340             :     // increment byte pointer for starting point
     341           0 :     bytes += (*index);
     342             : 
     343           0 :     *(bytes++) = (uint8_t)(number);
     344           0 :     number = number >> 8;
     345           0 :     *(bytes++) = (uint8_t)(number);
     346           0 :     number = number >> 8;
     347           0 :     *bytes = (uint8_t)(number);
     348             : 
     349           0 :     (*index) += 3;
     350           0 : }
     351             : 
     352             : /*!
     353             :  * Encode a unsigned 2 byte integer on a big endian byte stream.
     354             :  * \param number is the value to encode.
     355             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     356             :  * \param index gives the location of the first byte in the byte stream, and
     357             :  *        will be incremented by 2 when this function is complete.
     358             :  */
     359           0 : void uint16ToBeBytes(uint16_t number, uint8_t* bytes, int* index)
     360             : {
     361             :     // increment byte pointer for starting point
     362           0 :     bytes += (*index) + 1;
     363             : 
     364           0 :     *(bytes--) = (uint8_t)(number);
     365           0 :     number = number >> 8;
     366           0 :     *bytes = (uint8_t)(number);
     367             : 
     368           0 :     (*index) += 2;
     369           0 : }
     370             : 
     371             : /*!
     372             :  * Encode a unsigned 2 byte integer on a little endian byte stream.
     373             :  * \param number is the value to encode.
     374             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     375             :  * \param index gives the location of the first byte in the byte stream, and
     376             :  *        will be incremented by 2 when this function is complete.
     377             :  */
     378           0 : void uint16ToLeBytes(uint16_t number, uint8_t* bytes, int* index)
     379             : {
     380             :     // increment byte pointer for starting point
     381           0 :     bytes += (*index);
     382             : 
     383           0 :     *(bytes++) = (uint8_t)(number);
     384           0 :     number = number >> 8;
     385           0 :     *bytes = (uint8_t)(number);
     386             : 
     387           0 :     (*index) += 2;
     388           0 : }
     389             : 
     390             : /*!
     391             :  * Encode a signed 2 byte integer on a big endian byte stream.
     392             :  * \param number is the value to encode.
     393             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     394             :  * \param index gives the location of the first byte in the byte stream, and
     395             :  *        will be incremented by 2 when this function is complete.
     396             :  */
     397           0 : void int16ToBeBytes(int16_t number, uint8_t* bytes, int* index)
     398             : {
     399             :     // increment byte pointer for starting point
     400           0 :     bytes += (*index) + 1;
     401             : 
     402           0 :     *(bytes--) = (uint8_t)(number);
     403           0 :     number = number >> 8;
     404           0 :     *bytes = (uint8_t)(number);
     405             : 
     406           0 :     (*index) += 2;
     407           0 : }
     408             : 
     409             : /*!
     410             :  * Encode a signed 2 byte integer on a little endian byte stream.
     411             :  * \param number is the value to encode.
     412             :  * \param bytes is a pointer to the byte stream which receives the encoded data.
     413             :  * \param index gives the location of the first byte in the byte stream, and
     414             :  *        will be incremented by 2 when this function is complete.
     415             :  */
     416           0 : void int16ToLeBytes(int16_t number, uint8_t* bytes, int* index)
     417             : {
     418             :     // increment byte pointer for starting point
     419           0 :     bytes += (*index);
     420             : 
     421           0 :     *(bytes++) = (uint8_t)(number);
     422           0 :     number = number >> 8;
     423           0 :     *bytes = (uint8_t)(number);
     424             : 
     425           0 :     (*index) += 2;
     426           0 : }
     427             : 
     428             : // end of fieldencode.c

Generated by: LCOV version 1.14