Improve granularity of BCM socket using one by signal
[apps/low-level-can-service.git] / CAN-binder / low-can-binding / can / 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 <queue>
23 #include <vector>
24 #include <string>
25
26 #include "openxc.pb.h"
27 #include "../utils/timer.hpp"
28 #include "../utils/socketcan-bcm.hpp"
29 #include "can-message.hpp"
30 #include "can-message-definition.hpp"
31 #include "../diagnostic/diagnostic-message.hpp"
32
33 extern "C"
34 {
35         #include <afb/afb-binding.h>
36         #include <afb/afb-event-itf.h>
37 }
38
39 #define MESSAGE_SET_ID 0
40
41 class can_signal_t;
42
43 ///
44 /// @brief The type signature for a CAN signal decoder.
45 ///
46 /// A SignalDecoder transforms a raw floating point CAN signal into a number,
47 /// string or boolean.
48 ///
49 /// @param[in] signal - The CAN signal that we are decoding.
50 /// @param[in] signals - The list of all signals.
51 /// @param[in] signalCount - The length of the signals array.
52 /// @param[in] value - The CAN signal parsed from the message as a raw floating point
53 ///     value.
54 /// @param[out] send - An output parameter. If the decoding failed or the CAN signal should
55 ///     not send for some other reason, this should be flipped to false.
56 ///
57 /// @return a decoded value in an openxc_DynamicField struct.
58 ///
59 typedef openxc_DynamicField (*SignalDecoder)(can_signal_t& signal,
60                 const std::vector<can_signal_t>& signals, float value, bool* send);
61
62 ///
63 /// @brief: The type signature for a CAN signal encoder.
64 ///
65 /// A SignalEncoder transforms a number, string or boolean into a raw floating
66 /// point value that fits in the CAN signal.
67 ///
68 /// @param[in] signal - The CAN signal to encode. 
69 /// @param[in] value - The dynamic field to encode.
70 /// @param[out] send - An output parameter. If the encoding failed or the CAN signal should
71 /// not be encoded for some other reason, this will be flipped to false.
72 ///
73 typedef uint64_t (*SignalEncoder)(can_signal_t* signal,
74                 openxc_DynamicField* value, bool* send);
75
76 class can_signal_t
77 {
78 private:
79         utils::socketcan_bcm_t  socket_; /*!< socket_ - Specific BCM socket that filter the signal read from CAN device */
80         std::uint8_t message_set_id_; ///< message_set_id_ - Index number to the message_set_id container object
81         std::uint8_t message_id_; ///< message_id_ - Index number to the message_definition_t container object
82         std::string generic_name_; /*!< generic_name_ - The name of the signal to be output.*/
83         static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
84                                                   * which make easier to sort message when the come in.*/
85         uint8_t bit_position_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
86                                                                                 *       non-inverted bit numbering, i.e. the most significant bit of
87                                                                                 *       each byte is 0) */
88         uint8_t bit_size_; /*!< bit_size_ - The width of the bit field in the CAN message. */
89         float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
90                                                         *       don't need a factor. */
91         float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
92                                                         *       don't need an offset. */
93         float min_value_; /*!< min_value_ - The minimum value for the processed signal.*/
94         float max_value_; /*!< max_value_ - The maximum value for the processed signal. */
95         frequency_clock_t frequency_; /*!< frequency_ - A frequency_clock_t struct to control the maximum frequency to
96                                                                 *       process and send this signal. To process every value, set the
97                                                                 *       clock's frequency to 0. */
98         bool send_same_; /*!< send_same_ - If true, will re-send even if the value hasn't changed.*/
99         bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send the
100                                                            * value if it has changed. */
101         std::map<uint8_t, std::string> states_; /*!< states_ - A map of CAN signal state describing the mapping
102                                                                                  * between numerical and string values for valid states. */
103         bool writable_; /*!< writable - True if the signal is allowed to be written from the USB host
104                                          *      back to CAN. Defaults to false.*/
105         SignalDecoder decoder_; /*!< decoder_ - An optional function to decode a signal from the bus to a human
106                                                          * readable value. If NULL, the default numerical decoder is used. */
107         SignalEncoder encoder_; /*!< encoder_ - An optional function to encode a signal value to be written to
108                                                          * CAN into a byte array. If NULL, the default numerical encoder
109                                                          * is used. */
110         bool received_; /*!< received_ - True if this signal has ever been received.*/
111         float last_value_; /*!< lastValue_ - The last received value of the signal. If 'received' is false,
112                                                 *       this value is undefined. */
113
114 public:
115         can_signal_t(
116                 std::uint8_t message_set_id,
117                 std::uint8_t message_id,
118                 std::string generic_name,
119                 uint8_t bit_position,
120                 uint8_t bit_size,
121                 float factor,
122                 float offset,
123                 float min_value,
124                 float max_value,
125                 frequency_clock_t frequency,
126                 bool send_same,
127                 bool force_send_changed,
128                 std::map<uint8_t, std::string> states,
129                 bool writable,
130                 SignalDecoder decoder,
131                 SignalEncoder encoder,
132                 bool received);
133
134         utils::socketcan_bcm_t get_socket() const;
135         const can_message_definition_t& get_message() const;
136         const std::string& get_generic_name() const;
137         const std::string get_name() const;
138         const std::string& get_prefix() const;
139         uint8_t get_bit_position() const;
140         uint8_t get_bit_size() const;
141         float get_factor() const;
142         float get_offset() const;
143         float get_min_value() const;
144         float get_max_value() const;
145         frequency_clock_t& get_frequency();
146         bool get_send_same() const;
147         bool get_force_send_changed() const;
148         const std::map<uint8_t, std::string>& get_states() const;
149         const std::string get_states(uint8_t value);
150         size_t get_state_count() const;
151         bool get_writable() const;
152         SignalDecoder& get_decoder();
153         SignalEncoder& get_encoder();
154         bool get_received() const;
155         float get_last_value() const;
156
157         void set_prefix(std::string val);
158         void set_received(bool r);
159         void set_last_value(float val);
160         int create_rx_filter();
161 };