Fix decoder_t methods to get it compile
[apps/agl-service-can-low-level.git] / src / can-decoder.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 "can-utils.hpp"
21
22 class decoder_t
23 {
24         private:
25
26         public:
27                 /* Public: Parse the signal's bitfield from the given data and return the raw
28                 * value.
29                 *
30                 * signal - The signal to parse from the data.
31                 * data - The data to parse the signal from.
32                 * length - The length of the data array.
33                 *
34                 * Returns the raw value of the signal parsed as a bitfield from the given byte
35                 * array.
36                 */
37                 float parseSignalBitfield(CanSignal& signal, const can_message_t& message);
38
39                 /* Public: Find and return the corresponding string state for a CAN signal's
40                 * raw integer value.
41                 *
42                 * This is an implementation of the SignalDecoder type signature, and can be
43                 * used directly in the CanSignal.decoder field.
44                 *
45                 * signal  - The details of the signal that contains the state mapping.
46                 * signals - The list of all signals.
47                 * signalCount - the length of the signals array.
48                 * value - The numerical value that should map to a state.
49                 * send - An output argument that will be set to false if the value should
50                 *     not be sent for any reason.
51                 *
52                 * Returns a DynamicField with a string value if a matching state is found in
53                 * the signal. If an equivalent isn't found, send is sent to false and the
54                 * return value is undefined.
55                 */
56                 static openxc_DynamicField stateDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
57                                 float value, bool* send);
58
59                 /* Public: Coerces a numerical value to a boolean.
60                 *
61                 * This is an implementation of the SignalDecoder type signature, and can be
62                 * used directly in the CanSignal.decoder field.
63                 *
64                 * signal  - The details of the signal that contains the state mapping.
65                 * signals - The list of all signals
66                 * signalCount - The length of the signals array
67                 * value - The numerical value that will be converted to a boolean.
68                 * send - An output argument that will be set to false if the value should
69                 *     not be sent for any reason.
70                 *
71                 * Returns a DynamicField with a boolean value of false if the raw signal value
72                 * is 0.0, otherwise true. The 'send' argument will not be modified as this
73                 * decoder always succeeds.
74                 */
75                 static openxc_DynamicField booleanDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
76                                 float value, bool* send);
77
78                 /* Public: Update the metadata for a signal and the newly received value.
79                 *
80                 * This is an implementation of the SignalDecoder type signature, and can be
81                 * used directly in the CanSignal.decoder field.
82                 *
83                 * This function always flips 'send' to false.
84                 *
85                 * signal  - The details of the signal that contains the state mapping.
86                 * signals - The list of all signals.
87                 * signalCount - The length of the signals array.
88                 * value - The numerical value that will be converted to a boolean.
89                 * send - This output argument will always be set to false, so the caller will
90                 *      know not to publish this value to the pipeline.
91                 *
92                 * The return value is undefined.
93                 */
94                  openxc_DynamicField ignoreDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
95                                 float value, bool* send);
96
97                 /* Public: Wrap a raw CAN signal value in a DynamicField without modification.
98                 *
99                 * This is an implementation of the SignalDecoder type signature, and can be
100                 * used directly in the CanSignal.decoder field.
101                 *
102                 * signal  - The details of the signal that contains the state mapping.
103                 * signals - The list of all signals
104                 * signalCount - The length of the signals array
105                 * value - The numerical value that will be wrapped in a DynamicField.
106                 * send - An output argument that will be set to false if the value should
107                 *     not be sent for any reason.
108                 *
109                 * Returns a DynamicField with the original, unmodified raw CAN signal value as
110                 * its numeric value. The 'send' argument will not be modified as this decoder
111                 * always succeeds.
112                 */
113                 static openxc_DynamicField noopDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
114                                 float value, bool* send);
115
116
117                 /* Public: Parse a signal from a CAN message, apply any required transforations
118                  *      to get a human readable value and public the result to the pipeline.
119                  *
120                 * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value
121                 * will be passed to the decoder before publishing.
122                 *
123                 * signal - The details of the signal to decode and forward.
124                 * message   - The received CAN message that should contain this signal.
125                 * signals - an array of all active signals.
126                 *
127                 * The decoder returns an openxc_DynamicField, which may contain a number,
128                 * string or boolean.
129                 */
130                 openxc_DynamicField translateSignal(CanSignal& signal, can_message_t& message,
131                         const std::vector<CanSignal>& signals);
132                 
133                 /* Public: Parse a signal from a CAN message and apply any required
134                 * transforations to get a human readable value.
135                 *
136                 * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value
137                 * will be passed to the decoder before returning.
138                 *
139                 * signal - The details of the signal to decode and forward.
140                 * message   - The CAN message that contains the signal.
141                 * signals - an array of all active signals.
142                 * send - An output parameter that will be flipped to false if the value could
143                 *      not be decoded.
144                 *
145                 * The decoder returns an openxc_DynamicField, which may contain a number,
146                 * string or boolean. If 'send' is false, the return value is undefined.
147                 */
148                 openxc_DynamicField decodeSignal(CanSignal& signal, const can_message_t& message,
149                                 const std::vector<CanSignal>& signals, bool* send);
150
151                 /* Public: Decode a transformed, human readable value from an raw CAN signal
152                 * already parsed from a CAN message.
153                 *
154                 * This is the same as decodeSignal(const CanSignal&, CanMessage*, const CanSignal&, int,
155                 * bool*) but you must parse the bitfield value of the signal from the CAN
156                 * message yourself. This is useful if you need that raw value for something
157                 * else.
158                 */
159                 openxc_DynamicField decodeSignal(CanSignal& signal, float value,
160                         const std::vector<CanSignal>& signals, bool* send);
161 };