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