2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "can-bus.hpp"
25 /* Public: Parse the signal's bitfield from the given data and return the raw
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.
32 * Returns the raw value of the signal parsed as a bitfield from the given byte
35 static float parseSignalBitfield(can_signal_t& signal, const can_message_t& message);
37 /* Public: Find and return the corresponding string state for a CAN signal's
40 * This is an implementation of the SignalDecoder type signature, and can be
41 * used directly in the can_signal_t.decoder field.
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.
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.
54 static openxc_DynamicField stateDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
55 float value, bool* send);
57 /* Public: Coerces a numerical value to a boolean.
59 * This is an implementation of the SignalDecoder type signature, and can be
60 * used directly in the can_signal_t.decoder field.
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.
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.
73 static openxc_DynamicField booleanDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
74 float value, bool* send);
76 /* Public: Update the metadata for a signal and the newly received value.
78 * This is an implementation of the SignalDecoder type signature, and can be
79 * used directly in the can_signal_t.decoder field.
81 * This function always flips 'send' to false.
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.
90 * The return value is undefined.
92 static openxc_DynamicField ignoreDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
93 float value, bool* send);
95 /* Public: Wrap a raw CAN signal value in a DynamicField without modification.
97 * This is an implementation of the SignalDecoder type signature, and can be
98 * used directly in the can_signal_t.decoder field.
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.
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
111 static openxc_DynamicField noopDecoder(can_signal_t& signal, const std::vector<can_signal_t>& signals,
112 float value, bool* send);
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.
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.
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.
125 * The decoder returns an openxc_DynamicField, which may contain a number,
128 static openxc_DynamicField translateSignal(can_signal_t& signal, can_message_t& message,
129 const std::vector<can_signal_t>& signals);
131 /* Public: Parse a signal from a CAN message and apply any required
132 * transforations to get a human readable value.
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.
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
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.
146 static openxc_DynamicField decodeSignal(can_signal_t& signal, const can_message_t& message,
147 const std::vector<can_signal_t>& signals, bool* send);
149 /* Public: Decode a transformed, human readable value from an raw CAN signal
150 * already parsed from a CAN message.
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
157 static openxc_DynamicField decodeSignal(can_signal_t& signal, float value,
158 const std::vector<can_signal_t>& signals, bool* send);