5f14375307e3004c7e9c0642eea0db1f7572bb34
[apps/agl-service-can-low-level.git] / low-can-binding / can / signals.hpp
1 ///
2 /// Copyright (C) 2015, 2016 "IoT.bzh"
3 /// Author "Romain Forlot" <romain.forlot@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 #pragma once
19
20 #include <map>
21 #include <mutex>
22 #include <vector>
23 #include <string>
24 #include <memory>
25
26 #include "openxc.pb.h"
27 #include "message-definition.hpp"
28 #include "../utils/timer.hpp"
29 #include "../diagnostic/diagnostic-message.hpp"
30
31 #define MESSAGE_SET_ID 0
32
33 class signal_t;
34 ///
35 /// @brief The type signature for a CAN signal decoder.
36 ///
37 /// A signal_decoder transforms a raw floating point CAN signal into a number,
38 /// string or boolean.
39 ///
40 /// @param[in] signal - The CAN signal that we are decoding.
41 /// @param[in] message - The message with data to decode.
42 /// @param[out] send - An output parameter. If decoding fails or CAN signal is
43 ///     not sending, this should be flipped to false.
44 ///
45 /// @return a decoded value in an openxc_DynamicField struct.
46 ///
47 typedef openxc_DynamicField (*signal_decoder)(signal_t& signal, std::shared_ptr<message_t> message, bool* send);
48
49 ///
50 /// @brief: The type signature for a CAN signal encoder.
51 ///
52 /// A signal_encoder transforms a number, string or boolean into a raw floating
53 /// point value that fits in the CAN signal.
54 ///
55 /// @param[in] signal - The CAN signal to encode.
56 /// @param[in] value - The dynamic field to encode.
57 /// @param[out] send - An output parameter. If the encoding failed or the CAN signal should
58 /// not be encoded for some other reason, this will be flipped to false.
59 ///
60 typedef uint64_t (*signal_encoder)(signal_t& signal,
61                  const openxc_DynamicField& field, bool* send);
62
63 enum sign_t
64 {
65                 UNSIGNED = 0,
66                 SIGN_BIT = 1,
67                 ONES_COMPLEMENT = 2,
68                 TWOS_COMPLEMENT = 3,
69                 SIGN_BIT_EXTERN = 4
70 };
71
72 class signal_t
73 {
74 private:
75         std::shared_ptr<message_definition_t> parent_; /*!< parent_ - pointer to the parent message definition holding this signal*/
76         std::string generic_name_; /*!< generic_name_ - The name of the signal to be output.*/
77         static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
78                                          * which make easier to sort message when the come in.*/
79         uint32_t bit_position_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
80                                 *       non-inverted bit numbering, i.e. the most significant bit of
81                                 *       each byte is 0) */
82         uint32_t bit_size_; /*!< bit_size_ - The width of the bit field in the CAN message. */
83         float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
84                         *       don't need a factor. */
85         float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
86                         *       don't need an offset. */
87         float min_value_; /*!< min_value_ - The minimum value for the processed signal.*/
88         float max_value_; /*!< max_value_ - The maximum value for the processed signal. */
89         frequency_clock_t frequency_; /*!< frequency_ - A frequency_clock_t struct to control the maximum frequency to
90                                            * process and send this signal. To process every value, set the
91                                            * clock's frequency to 0. */
92         bool send_same_; /*!< send_same_ - If true, will re-send even if the value hasn't changed.*/
93         bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send the
94                                    * value if it has changed. */
95         std::map<uint8_t, std::string> states_; /*!< states_ - A map of CAN signal state describing the mapping
96                                                  * between numerical and string values for valid states. */
97         bool writable_; /*!< writable - True if the signal is allowed to be written from the USB host
98                          * back to CAN. Defaults to false.*/
99         signal_decoder decoder_; /*!< decoder_ - An optional function to decode a signal from the bus to a human
100                                   * readable value. If NULL, the default numerical decoder is used. */
101         signal_encoder encoder_; /*!< encoder_ - An optional function to encode a signal value to be written to
102                                   * CAN into a byte array. If NULL, the default numerical encoder
103                                   * is used. */
104         bool received_; /*!< received_ - True if this signal has ever been received.*/
105         float last_value_; /*!< lastValue_ - The last received value of the signal. If 'received' is false,
106                                 * this value is undefined. */
107         std::pair<bool, int> multiplex_; /*!< multiplex_ - If bool is false and int is 0 is not a multiplex signal
108                                                                                 If bool is true, that indicate that is a multiplexor
109                                                                                 If int is different of 0, that indicate the link with a multiplexor */
110         bool is_big_endian_; /*!< is_big_endian - True if the signal's data are meant to be read as a big_endian */
111         sign_t sign_; /* !< sign_ - if the data is signed it indicates the encode */
112         int32_t bit_sign_position_; /*!< bit_sign_position_ - The bit that indicates the sign of the signal in its CAN message*/
113         std::string unit_; /* !< unit_ - The unit of the data */
114         bool bit_position_is_swapped_; /* !<bit_position_is_swapped_- True if the signal's bit position has been
115                                                                                 swapped (refer to converter_t::bit_position_swap()). Default is false.*/
116
117 public:
118
119         signal_t(
120                 std::string generic_name,
121                 uint32_t bit_position,
122                 uint32_t bit_size,
123                 float factor,
124                 float offset,
125                 float min_value,
126                 float max_value,
127                 frequency_clock_t frequency,
128                 bool send_same,
129                 bool force_send_changed,
130                 std::map<uint8_t, std::string> states,
131                 bool writable,
132                 signal_decoder decoder,
133                 signal_encoder encoder,
134                 bool received,
135                 std::pair<bool, int> multiplex,
136                 bool is_big_endian,
137                 sign_t sign,
138                 int32_t bit_sign_position,
139                 std::string unit);
140
141
142         signal_t(
143                 std::string generic_name,
144                 uint32_t bit_position,
145                 uint32_t bit_size,
146                 float factor,
147                 float offset,
148                 float min_value,
149                 float max_value,
150                 frequency_clock_t frequency,
151                 bool send_same,
152                 bool force_send_changed,
153                 std::map<uint8_t, std::string> states,
154                 bool writable,
155                 signal_decoder decoder,
156                 signal_encoder encoder,
157                 bool received);
158
159
160         std::shared_ptr<message_definition_t> get_message() const;
161         const std::string get_generic_name() const;
162         const std::string get_name() const;
163         uint32_t get_bit_position() const;
164         uint32_t get_bit_size() const;
165         float get_factor() const;
166         float get_offset() const;
167         frequency_clock_t& get_frequency();
168         bool get_send_same() const;
169         const std::string get_states(uint8_t value);
170         uint64_t get_states(const std::string& value) const;
171         bool get_writable() const;
172         signal_decoder& get_decoder();
173         signal_encoder& get_encoder();
174         bool get_received() const;
175         float get_last_value() const;
176         std::pair<float, uint64_t> get_last_value_with_timestamp() const;
177         std::pair<bool, int> get_multiplex() const;
178         bool get_is_big_endian() const;
179         sign_t get_sign() const;
180         int32_t get_bit_sign_position() const;
181         const std::string get_unit() const;
182         bool bit_position_is_swapped() const;
183
184         void set_parent(std::shared_ptr<message_definition_t> parent);
185         void set_received(bool r);
186         void set_last_value(float val);
187         void set_timestamp(uint64_t timestamp);
188         void set_bit_position(uint32_t bit_position);
189         void bit_position_is_swapped_reverse();
190 };