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.
27 #include "openxc.pb.h"
28 #include "can-bus.hpp"
29 #include "can-message.hpp"
33 #include <afb/afb-binding.h>
34 #include <afb/afb-event-itf.h>
37 #define MESSAGE_SET_ID 0
40 * @brief Can signal event map making access to afb_event
41 * externaly to an openxc existing structure.
43 * @desc Event map is making relation between CanSignal generic name
44 * and the afb_event struct used by application framework to pushed
47 static std::map<std::string, struct afb_event> subscribed_signals;
49 std::mutex& get_subscribed_signals_mutex();
52 * @brief The type signature for a CAN signal decoder.
54 * @desc A SignalDecoder transforms a raw floating point CAN signal into a number,
57 * @param[in] CanSignal signal - The CAN signal that we are decoding.
58 * @param[in] CanSignal signals - The list of all signals.
59 * @param[in] int signalCount - The length of the signals array.
60 * @param[in] float value - The CAN signal parsed from the message as a raw floating point
62 * @param[out] bool send - An output parameter. If the decoding failed or the CAN signal should
63 * not send for some other reason, this should be flipped to false.
65 * @return a decoded value in an openxc_DynamicField struct.
67 typedef openxc_DynamicField (*SignalDecoder)(struct CanSignal& signal,
68 const std::vector<CanSignal>& signals, float value, bool* send);
71 * @brief: The type signature for a CAN signal encoder.
73 * @desc A SignalEncoder transforms a number, string or boolean into a raw floating
74 * point value that fits in the CAN signal.
76 * @params[signal] - The CAN signal to encode.
77 * @params[value] - The dynamic field to encode.
78 * @params[send] - An output parameter. If the encoding failed or the CAN signal should
79 * not be encoded for some other reason, this will be flipped to false.
81 typedef uint64_t (*SignalEncoder)(struct CanSignal* signal,
82 openxc_DynamicField* value, bool* send);
85 * @struct CanSignalState
87 * @brief A state encoded (SED) signal's mapping from numerical values to
90 struct CanSignalState {
91 const int value; /*!< int value - The integer value of the state on the CAN bus.*/
92 const char* name; /*!< char* name - The corresponding string name for the state in OpenXC. */
94 typedef struct CanSignalState CanSignalState;
99 * @brief A CAN signal to decode from the bus and output over USB.
102 struct CanMessageDefinition* message; /*!< message - The message this signal is a part of. */
103 const char* genericName; /*!< genericName - The name of the signal to be output over USB.*/
104 uint8_t bitPosition; /*!< bitPosition - The starting bit of the signal in its CAN message (assuming
105 * non-inverted bit numbering, i.e. the most significant bit of
107 uint8_t bitSize; /*!< bitSize - The width of the bit field in the CAN message. */
108 float factor; /*!< factor - The final value will be multiplied by this factor. Use 1 if you
109 * don't need a factor. */
110 float offset; /*!< offset - The final value will be added to this offset. Use 0 if you
111 * don't need an offset. */
112 float minValue; /*!< minValue - The minimum value for the processed signal.*/
113 float maxValue; /*!< maxValue - The maximum value for the processed signal. */
114 FrequencyClock frequencyClock; /*!< frequencyClock - A FrequencyClock struct to control the maximum frequency to
115 * process and send this signal. To process every value, set the
116 * clock's frequency to 0. */
117 bool sendSame; /*!< sendSame - If true, will re-send even if the value hasn't changed.*/
118 bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send the
119 * value if it has changed. */
120 const CanSignalState* states; /*!< states - An array of CanSignalState describing the mapping
121 * between numerical and string values for valid states. */
122 uint8_t stateCount; /*!< stateCount - The length of the states array. */
123 bool writable; /*!< writable - True if the signal is allowed to be written from the USB host
124 * back to CAN. Defaults to false.*/
125 SignalDecoder decoder; /*!< decoder - An optional function to decode a signal from the bus to a human
126 * readable value. If NULL, the default numerical decoder is used. */
127 SignalEncoder encoder; /*!< encoder - An optional function to encode a signal value to be written to
128 * CAN into a byte array. If NULL, the default numerical encoder
130 bool received; /*!< received - True if this signal has ever been received.*/
131 float lastValue; /*!< lastValue - The last received value of the signal. If 'received' is false,
132 * this value is undefined. */
134 typedef struct CanSignal CanSignal;
136 /* Public: Return signals from an signals array filtered on name.
138 const std::vector<CanSignal> getSignals();
140 /* Public: Return the length of the array returned by getSignals(). */
141 size_t getSignalCount();
144 * @brief Find one or many signals based on its name or id
145 * passed through openxc_DynamicField.
147 * @param[in] openxc_DynamicField& - a const reference with the key to search into signal.
148 * Key is either a signal name or its CAN arbitration id.
150 * @return std::vector<std::string> return found CanSignal generic name vector.
152 std::vector<CanSignal> find_can_signals(const openxc_DynamicField &key);
155 * @brief Retrieve can arbitration id of a given CanSignal
157 * @param[in] CanSignal& - a const reference to a CanSignal
159 * @return uint32_t - unsigned integer representing the arbitration id.
161 inline uint32_t get_CanSignal_id(const CanSignal& sig);
164 * @brief return the subscribed_signals map.
166 * return std::map<std::string, struct afb_event> - map of subscribed signals.
168 const std::map<std::string, struct afb_event> get_subscribed_signals();