decoder: Add new decoders: ascii, date, time
[apps/agl-service-can-low-level.git] / low-can-binding / utils / converter.cpp
1 /*
2  * Copyright (C) 2019, 2020 "IoT.bzh"
3  * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "converter.hpp"
19 #include <sstream>
20 #include <net/if.h>
21 #include <afb/afb-binding>
22 #include <climits>
23
24 /**
25  * @brief Convert data to hex string
26  *
27  * @param data An array of data
28  * @param length The length of the data
29  * @return std::string The hex string
30  */
31 std::string converter_t::to_hex(const uint8_t data[], const size_t length)
32 {
33         std::stringstream stream;
34         stream << std::hex << std::setfill('0');
35         for(int i = 0; i < length; i++)
36                 stream << std::setfill('0') << std::setw(2) << std::hex << ((int) data[i]);
37
38         return stream.str();
39 }
40
41 /**
42  * @brief Convert data to ascii string
43  *
44  * @param data An array of data
45  * @param length The length of the data
46  * @return std::string The ascii string
47  */
48 std::string converter_t::to_ascii(const uint8_t data[], const size_t length)
49 {
50         std::stringstream stream;
51         for(int i = 0; i < length; i++)
52                 stream << ((char) data[i]);
53         return stream.str();
54 }
55
56 /**
57  * @brief Translate bit_position and bit_size
58  *
59  *
60  * @param bit_position The position in the frame
61  * @param bit_size The size of the signal
62  * @param new_start_byte The first bytes of the signal in the frame
63  * @param new_end_byte The last byte of the signal in the frame
64  * @param new_start_bit The first bit of the signal in the frame
65  * @param new_end_bit The last bit of the signal in the frame
66  */
67 void converter_t::signal_to_bits_bytes(unsigned int bit_position, unsigned int bit_size, int &new_start_byte, int &new_end_byte, uint8_t &new_start_bit, uint8_t &new_end_bit)
68 {
69         new_start_byte = bit_position >> 3;
70         new_start_bit = bit_position % CHAR_BIT;
71         new_end_byte = (bit_position + bit_size - 1) >> 3;
72         new_end_bit = (bit_position + bit_size - 1) % CHAR_BIT;
73 }
74
75
76 /**
77  * @brief       This is to use when you have a big endian CAN frame layout.
78  *              It converts the bit position so it matches with little endiant CAN frame layout.
79  *
80  * @param msg_length    Message length in bytes.
81  * @param bit_position  Original bit position.
82  * @param bit_size      Size of the data.
83  * @return uint32_t     New bit position.
84  */
85 uint32_t converter_t::bit_position_swap(unsigned int msg_length, unsigned int bit_position, unsigned int bit_size)
86 {
87         return (msg_length * CHAR_BIT) - bit_position - bit_size;
88 }
89
90 /**
91  * @brief       This allow to get the correct bit_position using the weird Continental.
92  *              bit numbering method where the Frame is read using little endianness
93  *              and bit count using a big endianness
94  *
95  * @param msg_length    Message length in bytes.
96  * @param bit_position  Original bit position.
97  * @param bit_size      Size of the data.
98  * @return uint32_t     New bit position.
99  */
100 uint32_t converter_t::continental_bit_position_mess(unsigned int msg_length, unsigned int bit_position, unsigned int bit_size)
101 {
102         return bit_position + (CHAR_BIT - bit_position % CHAR_BIT) - bit_size;
103 }