2 /// Copyright (C) 2015, 2016 "IoT.bzh"
3 /// Author "Romain Forlot" <romain.forlot@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.
26 #include "openxc.pb.h"
27 #include "can-message-definition.hpp"
28 #include "../utils/timer.hpp"
29 #include "../utils/socketcan-bcm.hpp"
30 #include "can-message.hpp"
31 #include "../diagnostic/diagnostic-message.hpp"
33 #define MESSAGE_SET_ID 0
37 /// @brief The type signature for a CAN signal decoder.
39 /// A signal_decoder transforms a raw floating point CAN signal into a number,
40 /// string or boolean.
42 /// @param[in] signal - The CAN signal that we are decoding.
43 /// @param[in] signalCount - The length of the signals array.
44 /// @param[in] value - The CAN signal parsed from the message as a raw floating point
46 /// @param[out] send - An output parameter. If decoding fails or CAN signal is
47 /// not sending, this should be flipped to false.
49 /// @return a decoded value in an openxc_DynamicField struct.
51 typedef openxc_DynamicField (*signal_decoder)(can_signal_t& signal, float value, bool* send);
54 /// @brief: The type signature for a CAN signal encoder.
56 /// A signal_encoder transforms a number, string or boolean into a raw floating
57 /// point value that fits in the CAN signal.
59 /// @param[in] signal - The CAN signal to encode.
60 /// @param[in] value - The dynamic field to encode.
61 /// @param[out] send - An output parameter. If the encoding failed or the CAN signal should
62 /// not be encoded for some other reason, this will be flipped to false.
64 typedef uint64_t (*signal_encoder)(can_signal_t& signal,
65 const openxc_DynamicField& field, bool* send);
70 can_message_definition_t* parent_; /*!< parent_ - pointer to the parent message definition holding this signal*/
71 std::string generic_name_; /*!< generic_name_ - The name of the signal to be output.*/
72 static std::string prefix_; /*!< prefix_ - generic_name_ will be prefixed with it. It has to reflect the used protocol.
73 * which make easier to sort message when the come in.*/
74 uint8_t bit_position_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
75 * non-inverted bit numbering, i.e. the most significant bit of
77 uint8_t bit_size_; /*!< bit_size_ - The width of the bit field in the CAN message. */
78 float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
79 * don't need a factor. */
80 float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
81 * don't need an offset. */
82 float min_value_; /*!< min_value_ - The minimum value for the processed signal.*/
83 float max_value_; /*!< max_value_ - The maximum value for the processed signal. */
84 frequency_clock_t frequency_; /*!< frequency_ - A frequency_clock_t struct to control the maximum frequency to
85 * process and send this signal. To process every value, set the
86 * clock's frequency to 0. */
87 bool send_same_; /*!< send_same_ - If true, will re-send even if the value hasn't changed.*/
88 bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send the
89 * value if it has changed. */
90 std::map<uint8_t, std::string> states_; /*!< states_ - A map of CAN signal state describing the mapping
91 * between numerical and string values for valid states. */
92 bool writable_; /*!< writable - True if the signal is allowed to be written from the USB host
93 * back to CAN. Defaults to false.*/
94 signal_decoder decoder_; /*!< decoder_ - An optional function to decode a signal from the bus to a human
95 * readable value. If NULL, the default numerical decoder is used. */
96 signal_encoder encoder_; /*!< encoder_ - An optional function to encode a signal value to be written to
97 * CAN into a byte array. If NULL, the default numerical encoder
99 bool received_; /*!< received_ - True if this signal has ever been received.*/
100 float last_value_; /*!< lastValue_ - The last received value of the signal. If 'received' is false,
101 * this value is undefined. */
105 std::string generic_name,
106 uint8_t bit_position,
112 frequency_clock_t frequency,
114 bool force_send_changed,
115 std::map<uint8_t, std::string> states,
117 signal_decoder decoder,
118 signal_encoder encoder,
121 can_message_definition_t* get_message() const;
122 const std::string get_generic_name() const;
123 const std::string get_name() const;
124 uint8_t get_bit_position() const;
125 uint8_t get_bit_size() const;
126 float get_factor() const;
127 float get_offset() const;
128 frequency_clock_t& get_frequency();
129 bool get_send_same() const;
130 const std::string get_states(uint8_t value);
131 uint64_t get_states(const std::string& value) const;
132 bool get_writable() const;
133 signal_decoder& get_decoder();
134 signal_encoder& get_encoder();
135 bool get_received() const;
136 float get_last_value() const;
137 std::pair<float, uint64_t> get_last_value_with_timestamp() const;
139 void set_parent(can_message_definition_t* parent);
140 void set_received(bool r);
141 void set_last_value(float val);
142 void set_timestamp(uint64_t timestamp);