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