d4b22ccca4e8d0ce28e0b37bc859cdec10a81395
[apps/agl-service-can-low-level.git] / src / 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 "can/can-bus.hpp"
29 #include "can/can-message.hpp"
30 #include "obd2/obd2-signals.hpp"
31
32 extern "C"
33 {
34         #include <afb/afb-binding.h>
35         #include <afb/afb-event-itf.h>
36 }
37
38 #define MESSAGE_SET_ID 0
39
40 /**
41  * @brief The type signature for a CAN signal decoder.
42  *
43  * @desc A SignalDecoder transforms a raw floating point CAN signal into a number,
44  * string or boolean.
45  *
46  * @param[in] signal - The CAN signal that we are decoding.
47  * @param[in] signals - The list of all signals.
48  * @param[in] signalCount - The length of the signals array.
49  * @param[in] value - The CAN signal parsed from the message as a raw floating point
50  *      value.
51  * @param[out] send - An output parameter. If the decoding failed or the CAN signal should
52  *      not send for some other reason, this should be flipped to false.
53  *
54  * @return a decoded value in an openxc_DynamicField struct.
55  */
56 typedef openxc_DynamicField (*SignalDecoder)(struct CanSignal& signal,
57                 const std::vector<CanSignal>& signals, float value, bool* send);
58
59 /**
60  * @brief: The type signature for a CAN signal encoder.
61  *
62  * @desc A SignalEncoder transforms a number, string or boolean into a raw floating
63  * point value that fits in the CAN signal.
64  *
65  * @params[in] signal - The CAN signal to encode. 
66  * @params[in] value - The dynamic field to encode.
67  * @params send - An output parameter. If the encoding failed or the CAN signal should
68  * not be encoded for some other reason, this will be flipped to false.
69  */
70 typedef uint64_t (*SignalEncoder)(struct CanSignal* signal,
71                 openxc_DynamicField* value, bool* send);
72
73 /**
74  * @struct CanSignalState
75  *
76  * @brief A state encoded (SED) signal's mapping from numerical values to
77  * OpenXC state names.
78  */
79 struct CanSignalState {
80         const int value; /*!< value - The integer value of the state on the CAN bus.*/
81         const char* name; /*!< name - The corresponding string name for the state in OpenXC. */
82 };
83 typedef struct CanSignalState CanSignalState;
84
85 /**
86  * @struct CanSignal
87  *
88  * @brief A CAN signal to decode from the bus and output over USB.
89  */
90 struct CanSignal {
91         struct CanMessageDefinition* message; /*!< message - The message this signal is a part of. */
92         const char* generic_name; /*!< generic_name - The name of the signal to be output over USB.*/
93         uint8_t bitPosition; /*!< bitPosition - The starting bit of the signal in its CAN message (assuming
94                                                 *       non-inverted bit numbering, i.e. the most significant bit of
95                                                 *       each byte is 0) */
96         uint8_t bitSize; /*!< bitSize - The width of the bit field in the CAN message. */
97         float factor; /*!< factor - The final value will be multiplied by this factor. Use 1 if you
98                                 *       don't need a factor. */
99         float offset; /*!< offset          - The final value will be added to this offset. Use 0 if you
100                                 *       don't need an offset. */
101         float minValue; /*!< minValue    - The minimum value for the processed signal.*/
102         float maxValue; /*!< maxValue    - The maximum value for the processed signal. */
103         FrequencyClock frequencyClock; /*!< frequencyClock - A FrequencyClock struct to control the maximum frequency to
104                                                                 *       process and send this signal. To process every value, set the
105                                                                 *       clock's frequency to 0. */
106         bool sendSame; /*!< sendSame    - If true, will re-send even if the value hasn't changed.*/
107         bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send the
108                                                 *       value if it has changed. */
109         const CanSignalState* states; /*!< states          - An array of CanSignalState describing the mapping
110                                                                 *       between numerical and string values for valid states. */
111         uint8_t stateCount; /*!< stateCount  - The length of the states array. */
112         bool writable; /*!< writable    - True if the signal is allowed to be written from the USB host
113                                 *       back to CAN. Defaults to false.*/
114         SignalDecoder decoder; /*!< decoder        - An optional function to decode a signal from the bus to a human
115                                                 *       readable value. If NULL, the default numerical decoder is used. */
116         SignalEncoder encoder; /*!< encoder        - An optional function to encode a signal value to be written to
117                                                 *       CAN into a byte array. If NULL, the default numerical encoder
118                                                 *       is used. */
119         bool received; /*!< received    - True if this signal has ever been received.*/
120         float lastValue; /*!< lastValue   - The last received value of the signal. If 'received' is false,
121                                         *       this value is undefined. */
122 };
123 typedef struct CanSignal CanSignal;
124
125 class can_signal_t
126 {
127         private:
128                 struct can_message_definition_t* message_; /*!< message_ - The message this signal is a part of. */
129                 const std::string generic_name_; /*!< generic_name_ - The name of the signal to be output over USB.*/
130                 uint8_t bitPosition_; /*!< bitPosition_ - The starting bit of the signal in its CAN message (assuming
131                                                            *    non-inverted bit numbering, i.e. the most significant bit of
132                                                            *    each byte is 0) */
133                 uint8_t bitSize_; /*!< bitSize_ - The width of the bit field in the CAN message. */
134                 float factor_; /*!< factor_ - The final value will be multiplied by this factor. Use 1 if you
135                                                 *       don't need a factor. */
136                 float offset_; /*!< offset_ - The final value will be added to this offset. Use 0 if you
137                                                 *       don't need an offset. */
138                 float min_value_; /*!< min_value_ - The minimum value for the processed signal.*/
139                 float max_value_; /*!< max_value_ - The maximum value for the processed signal. */
140                 FrequencyClock frequency_; /*!< frequency_ - A FrequencyClock struct to control the maximum frequency to
141                                                                 *       process and send this signal. To process every value, set the
142                                                                 *       clock's frequency to 0. */
143                 bool send_same_; /*!< send_same_ - If true, will re-send even if the value hasn't changed.*/
144                 bool force_send_changed_; /*!< force_send_changed_ - If true, regardless of the frequency, it will send the
145                                                                  *      value if it has changed. */
146                 const std::map<const int, const std::string> states_; /*!< states_ - A map of CAN signal state describing the mapping
147                                                                                                                            * between numerical and string values for valid states. */
148                 uint8_t state_count_; /*!< state_count_ - The length of the states array. */
149                 bool writable_; /*!< writable - True if the signal is allowed to be written from the USB host
150                                                 *       back to CAN. Defaults to false.*/
151                 SignalDecoder decoder_; /*!< decoder_ - An optional function to decode a signal from the bus to a human
152                                                                  * readable value. If NULL, the default numerical decoder is used. */
153                 SignalEncoder encoder_; /*!< encoder_ - An optional function to encode a signal value to be written to
154                                                                  * CAN into a byte array. If NULL, the default numerical encoder
155                                                                  * is used. */
156                 bool received_; /*!< received_ - True if this signal has ever been received.*/
157                 float lastValue_; /*!< lastValue_ - The last received value of the signal. If 'received' is false,
158                                                    *    this value is undefined. */
159
160         public:
161                 can_message_definition_t* get_message()
162                 {
163                         return message_;
164                 }
165
166                 const std::string& get_generic_name()
167                 {
168                         return generic_name_;
169                 }
170 };
171
172 void find_can_signals(const openxc_DynamicField &key, std::vector<CanSignal*>& found_signals);