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"
27 /* Public: Parse the signal's bitfield from the given data and return the raw
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.
34 * Returns the raw value of the signal parsed as a bitfield from the given byte
37 float parseSignalBitfield(CanSignal& signal, const can_message_t& message);
39 /* Public: Find and return the corresponding string state for a CAN signal's
42 * This is an implementation of the SignalDecoder type signature, and can be
43 * used directly in the CanSignal.decoder field.
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.
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.
56 static openxc_DynamicField stateDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
57 float value, bool* send);
59 /* Public: Coerces a numerical value to a boolean.
61 * This is an implementation of the SignalDecoder type signature, and can be
62 * used directly in the CanSignal.decoder field.
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.
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.
75 static openxc_DynamicField booleanDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
76 float value, bool* send);
78 /* Public: Update the metadata for a signal and the newly received value.
80 * This is an implementation of the SignalDecoder type signature, and can be
81 * used directly in the CanSignal.decoder field.
83 * This function always flips 'send' to false.
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.
92 * The return value is undefined.
94 openxc_DynamicField ignoreDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
95 float value, bool* send);
97 /* Public: Wrap a raw CAN signal value in a DynamicField without modification.
99 * This is an implementation of the SignalDecoder type signature, and can be
100 * used directly in the CanSignal.decoder field.
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.
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
113 static openxc_DynamicField noopDecoder(CanSignal& signal, const std::vector<CanSignal>& signals,
114 float value, bool* send);
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.
120 * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value
121 * will be passed to the decoder before publishing.
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.
127 * The decoder returns an openxc_DynamicField, which may contain a number,
130 openxc_DynamicField translateSignal(CanSignal& signal, can_message_t& message,
131 const std::vector<CanSignal>& signals);
133 /* Public: Parse a signal from a CAN message and apply any required
134 * transforations to get a human readable value.
136 * If the CanSignal has a non-NULL 'decoder' field, the raw CAN signal value
137 * will be passed to the decoder before returning.
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
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.
148 openxc_DynamicField decodeSignal(CanSignal& signal, const can_message_t& message,
149 const std::vector<CanSignal>& signals, bool* send);
151 /* Public: Decode a transformed, human readable value from an raw CAN signal
152 * already parsed from a CAN message.
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
159 openxc_DynamicField decodeSignal(CanSignal& signal, float value,
160 const std::vector<CanSignal>& signals, bool* send);