249d5b00e351d42cdf5dcab07ffd051ac1758669
[apps/low-level-can-service.git] / can-decoder.h
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
21 class Decoder_c
22 {
23         private:
24                 openxc_DynamicField decoded_value;
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 CanMessage* 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                 openxc_DynamicField stateDecoder(CanSignal* signal, CanSignal* signals,
57                                 int signalCount, 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                 openxc_DynamicField booleanDecoder(CanSignal* signal, CanSignal* signals,
76                                 int signalCount, 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, CanSignal* signals,
95                                 int signalCount, 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                 openxc_DynamicField noopDecoder(CanSignal* signal, CanSignal* signals,
114                                 int signalCount, float value, bool* send);
115
116                 /* Public: Parse a signal from a CAN message and apply any required
117                 * transforations to get a human readable value.
118                 *
119                 * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value
120                 * will be passed to the decoder before returning.
121                 *
122                 * signal - The details of the signal to decode and forward.
123                 * message   - The CAN message that contains the signal.
124                 * signals - an array of all active signals.
125                 * signalCount - The length of the signals array.
126                 * send - An output parameter that will be flipped to false if the value could
127                 *      not be decoded.
128                 *
129                 * The decoder returns an openxc_DynamicField, which may contain a number,
130                 * string or boolean. If 'send' is false, the return value is undefined.
131                 */
132                 openxc_DynamicField decodeSignal(CanSignal* signal,
133                                 const CanMessage* message, CanSignal* signals, int signalCount,
134                                 bool* send);
135
136                 /* Public: Decode a transformed, human readable value from an raw CAN signal
137                 * already parsed from a CAN message.
138                 *
139                 * This is the same as decodeSignal(CanSignal*, CanMessage*, CanSignal*, int,
140                 * bool*) but you must parse the bitfield value of the signal from the CAN
141                 * message yourself. This is useful if you need that raw value for something
142                 * else.
143                 */
144                 openxc_DynamicField decodeSignal(CanSignal* signal, float value,
145                 CanSignal* signals, int signalCount, bool* send);
146 }