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 }
uavcan::ScalarCodec::swapByteOrder
static void swapByteOrder(uint8_t *bytes, unsigned len)
Definition: uc_scalar_codec.cpp:10
uavcan::ScalarCodec::encodeBytesImpl
int encodeBytesImpl(uint8_t *bytes, unsigned bitlen)
Definition: uc_scalar_codec.cpp:21
uavcan::ScalarCodec::stream_
BitStream & stream_
Definition: scalar_codec.hpp:22
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::ScalarCodec::decodeBytesImpl
int decodeBytesImpl(uint8_t *bytes, unsigned bitlen)
Definition: uc_scalar_codec.cpp:32
scalar_codec.hpp
uavcan::BitStream::read
int read(uint8_t *bytes, const unsigned bitlen)
Definition: uc_bit_stream.cpp:55
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::BitStream::write
int write(const uint8_t *bytes, const unsigned bitlen)
Definition: uc_bit_stream.cpp:15
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03