2 * Copyright (C) 2019, 2020 "IoT.bzh"
3 * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "converter.hpp"
21 #include <afb/afb-binding>
24 * @brief Convert hex data to string
26 * @param data An array of data
27 * @param length The length of the data
28 * @return std::string The string data
30 std::string converter_t::to_hex(const uint8_t data[], const size_t length)
32 std::stringstream stream;
33 stream << std::hex << std::setfill('0');
34 for(int i = 0; i < length; i++)
35 stream << std::hex << ((int) data[i]);
41 * @brief Translate bit_position and bit_size
44 * @param bit_position The position in the frame
45 * @param bit_size The size of the signal
46 * @param new_start_byte The first bytes of the signal in the frame
47 * @param new_end_byte The last byte of the signal in the frame
48 * @param new_start_bit The first bit of the signal in the frame
49 * @param new_end_bit The last bit of the signal in the frame
51 void converter_t::signal_to_bits_bytes(uint32_t bit_position, uint32_t bit_size, int &new_start_byte, int &new_end_byte, uint8_t &new_start_bit, uint8_t &new_end_bit)
53 new_start_byte = bit_position >> 3;
54 new_start_bit = bit_position % 8;
55 new_end_byte = (bit_position + bit_size - 1) >> 3;
56 new_end_bit = (bit_position + bit_size - 1) % 8;
61 * @brief This is to use when you have a big endian CAN frame layout.
62 * It converts the bit position so it matches with little endiant CAN frame layout.
64 * @param bit_position Original bit position.
65 * @param bit_size Size of the data.
66 * @return uint32_t New little endian bit position.
68 uint32_t converter_t::bit_position_swap(uint32_t bit_position,uint32_t bit_size)
70 uint32_t start_byte_position = (uint32_t)(bit_position/8);
71 uint32_t bit_size_rest = bit_size;
73 if((int)(bit_size-(8 + start_byte_position*8-bit_position%8))>0)
75 AFB_ERROR("Error: bit_position and bit_size getting out of range");
79 if(bit_size<=8 && ((bit_position+bit_size)%8==bit_size || (bit_position+bit_size)%8==0))
81 return (uint32_t)(start_byte_position*8 + (8-bit_size));
87 bit_size_rest = bit_size_rest - ((start_byte_position+1)*8-bit_position);
88 start_byte_position--;
89 bit_position = start_byte_position*8;
90 } while (bit_size_rest>8);
91 return (uint32_t)(start_byte_position*8 + (8-bit_size_rest));