Sort and comment the code
[apps/agl-service-can-low-level.git] / src / can-signals.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 <map>
21 #include <mutex>
22 #include <queue>
23 #include <vector>
24 #include <string>
25
26 #include "obd2-signals.hpp"
27 #include "timer.hpp"
28 #include "openxc.pb.h"
29 #include "can-bus.hpp"
30 #include "can-message.hpp"
31
32 extern "C"
33 {
34         #include <afb/afb-binding.h>
35         #include <afb/afb-event-itf.h>
36 }
37
38 #define MESSAGE_SET_ID 0
39
40 extern std::mutex subscribed_signals_mutex;
41 std::mutex& get_subscribed_signals_mutex();
42
43 /**
44  * @brief return the subscribed_signals map.
45  * 
46  * return std::map<std::string, struct afb_event> - map of subscribed signals.
47  */
48 extern std::map<std::string, struct afb_event> subscribed_signals;
49 std::map<std::string, struct afb_event>& get_subscribed_signals();
50
51 /**
52  * @brief The type signature for a CAN signal decoder.
53  *
54  * @desc A SignalDecoder transforms a raw floating point CAN signal into a number,
55  * string or boolean.
56  *
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
61  *      value.
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.
64  *
65  * @return a decoded value in an openxc_DynamicField struct.
66  */
67 typedef openxc_DynamicField (*SignalDecoder)(struct CanSignal& signal,
68                 const std::vector<CanSignal>& signals, float value, bool* send);
69
70 /**
71  * @brief: The type signature for a CAN signal encoder.
72  *
73  * @desc A SignalEncoder transforms a number, string or boolean into a raw floating
74  * point value that fits in the CAN signal.
75  *
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.
80  */
81 typedef uint64_t (*SignalEncoder)(struct CanSignal* signal,
82                 openxc_DynamicField* value, bool* send);
83
84 /**
85  * @struct CanSignalState
86  *
87  * @brief A state encoded (SED) signal's mapping from numerical values to
88  * OpenXC state names.
89  */
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. */
93 };
94 typedef struct CanSignalState CanSignalState;
95
96 /**
97  * @struct CanSignal
98  *
99  * @brief A CAN signal to decode from the bus and output over USB.
100  */
101 struct CanSignal {
102         struct CanMessageDefinition* message; /*!< message         - The message this signal is a part of. */
103         const char* generic_name; /*!< generic_name - 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
106                                                 *       each byte is 0) */
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
129                                                 *       is used. */
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. */
133 };
134 typedef struct CanSignal CanSignal;
135
136 std::vector<CanSignal>& get_can_signals();
137
138 size_t getSignalCount();
139
140 void find_can_signals(const openxc_DynamicField &key, std::vector<CanSignal*>& found_signals);
141
142 uint32_t get_signal_id(const CanSignal& sig);