uc_scalar_codec.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
6 
7 namespace uavcan
8 {
9 
10 void ScalarCodec::swapByteOrder(uint8_t* const bytes, const unsigned len)
11 {
12  UAVCAN_ASSERT(bytes);
13  for (unsigned i = 0, j = len - 1; i < j; i++, j--)
14  {
15  const uint8_t c = bytes[i];
16  bytes[i] = bytes[j];
17  bytes[j] = c;
18  }
19 }
20 
21 int ScalarCodec::encodeBytesImpl(uint8_t* const bytes, const unsigned bitlen)
22 {
23  UAVCAN_ASSERT(bytes);
24  // Underlying stream class assumes that more significant bits have lower index, so we need to shift some.
25  if (bitlen % 8)
26  {
27  bytes[bitlen / 8] = uint8_t(bytes[bitlen / 8] << ((8 - (bitlen % 8)) & 7));
28  }
29  return stream_.write(bytes, bitlen);
30 }
31 
32 int ScalarCodec::decodeBytesImpl(uint8_t* const bytes, const unsigned bitlen)
33 {
34  UAVCAN_ASSERT(bytes);
35  const int read_res = stream_.read(bytes, bitlen);
36  if (read_res > 0)
37  {
38  if (bitlen % 8)
39  {
40  bytes[bitlen / 8] = uint8_t(bytes[bitlen / 8] >> ((8 - (bitlen % 8)) & 7)); // As in encode(), vice versa
41  }
42  }
43  return read_res;
44 }
45 
46 }
std::uint8_t uint8_t
Definition: std.hpp:24
static void swapByteOrder(uint8_t *bytes, unsigned len)
int read(uint8_t *bytes, const unsigned bitlen)
int decodeBytesImpl(uint8_t *bytes, unsigned bitlen)
int write(const uint8_t *bytes, const unsigned bitlen)
int encodeBytesImpl(uint8_t *bytes, unsigned bitlen)


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:40