Make an extern get_signal_id() function plug to get_pid()
[apps/agl-service-can-low-level.git] / src / diagnostic / diagnostic-message.cpp
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 #include "obd2/obd2-signals.hpp"
19
20 #include "utils/signals.hpp"
21
22 #define OBD2_FUNCTIONAL_BROADCAST_ID 0x7df
23
24 const char *UNIT_NAMES[10] = {
25         "POURCENT",
26         "DEGREES_CELSIUS",
27         "KPA",
28         "RPM",
29         "GRAMS_SEC",
30         "SECONDS",
31         "KM",
32         "KM_H",
33         "PA",
34         "NM"
35 };
36
37 /*
38 * Pre-defined OBD-II PIDs to query for if supported by the vehicle.
39 */
40  std::vector<obd2_signals_t> OBD2_PIDS = {
41         { 0x04, "obd2.engine.load", 0, 100, POURCENT, 5, false},
42         { 0x05, "obd2.engine.coolant.temperature", -40, 215, DEGREES_CELSIUS, 1, false},
43         { 0x0a, "obd2.fuel.pressure", 0, 765, KPA, 1, false},
44         { 0x0b, "obd2.intake.manifold.pressure", 0, 255, KPA, 1, false},
45         { 0x0c, "obd2.engine.speed", 0, 16383, RPM, 5, false},
46         { 0x0d, "obd2.vehicle.speed", 0, 255, KM_H, 5, false},
47         { 0x0f, "obd2.intake.air.temperature", -40, 215, DEGREES_CELSIUS, 1, false},
48         { 0x10, "obd2.mass.airflow", 0, 655, GRAMS_SEC, 5, false},
49         { 0x11, "obd2.throttle.position", 0, 100, POURCENT, 5, false},
50         { 0x1f, "obd2.running.time", 0, 65535, SECONDS, 1, false},
51         { 0x2d, "obd2.EGR.error", -100, 99, POURCENT, 0, false},
52         { 0x2f, "obd2.fuel.level", 0, 100, POURCENT, 1, false},
53         { 0x33, "obd2.barometric.pressure", 0, 255, KPA, 1, false},
54         { 0x4c, "obd2.commanded.throttle.position", 0, 100, POURCENT, 1, false},
55         { 0x52, "obd2.ethanol.fuel.percentage", 0, 100, POURCENT, 1, false},
56         { 0x5a, "obd2.accelerator.pedal.position", 0, 100, POURCENT, 5, false},
57         { 0x5b, "obd2.hybrid.battery-pack.remaining.life", 0, 100, POURCENT, 5, false},
58         { 0x5c, "obd2.engine.oil.temperature",-40, 210, DEGREES_CELSIUS, 1, false},
59         { 0x63, "obd2.engine.torque", 0, 65535, NM, 1, false}
60 };
61
62 uint32_t get_signal_id(obd2_signals_t& sig)
63 {
64         return sig.get_pid();
65 }
66
67 std::vector<obd2_signals_t>& get_obd2_signals()
68 {
69         return OBD2_PIDS;
70 }
71
72 obd2_signals_t::obd2_signals_t(uint8_t pid, const char* generic_name, const int min, const int max, enum UNIT unit, int frequency, bool supported)
73         :       pid_{pid}, generic_name_{generic_name}, min_{min}, max_{max}, unit_{unit}, frequency_{frequency}, supported_{supported}
74 {
75 }
76
77 uint32_t obd2_signals_t::get_pid()
78 {
79         return (uint32_t)pid_;
80 }
81
82 /**
83  * @fn std::vector<std::string> find_signals(const openxc_DynamicField &key)
84  * @brief return signals name found searching through CAN_signals and OBD2 pid
85  * 
86  * @param[in] const openxc_DynamicField : can contain numeric or string value in order to search against 
87  *   can signals or obd2 signals name.
88  *
89  * @return std::vector<std::string> Vector of signals name found. 
90  */
91 void obd2_signals_t::find_obd2_signals(const openxc_DynamicField &key, std::vector<obd2_signals_t*>& found_signals)
92 {
93         switch(key.type)
94         {
95                 case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
96                                 lookup_signals_by_name(key.string_value, get_obd2_signals(), found_signals);
97                         break;
98                 case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
99                                 lookup_signals_by_id(key.numeric_value, get_obd2_signals(), found_signals);
100                         break;
101                 default:
102                         ERROR(binder_interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
103                         break;
104         }
105         DEBUG(binder_interface, "Found %d signal(s)", (int)found_signals.size());
106 }
107
108 uint32_t get_signal_id(const obd2_signals_t& sig)
109 {
110         return (uint32_t)sig.pid;
111 }
112
113 void shims_logger(const char* m, const struct afb_binding_interface *interface)
114 {
115         DEBUG(interface, "%s", m);
116 }
117
118 void shims_timer()
119 {
120 }
121
122 bool obd2_signals_t::is_obd2_response(can_message_t can_message)
123 {
124         if(can_message.get_id() >= 0x7E8 && can_message.get_id() <= 0x7EF)
125         {
126                 openxc_VehicleMessage message = {0};
127                 message.has_type = true;
128                 message.type = openxc_VehicleMessage_Type_DIAGNOSTIC;
129                 message.has_diagnostic_response = true;
130                 message.diagnostic_response = {0};
131                 message.diagnostic_response.has_bus = true;
132                 message.diagnostic_response.bus = bus->address;
133                 message.diagnostic_response.has_message_id = true;
134                 //7DF should respond with a random message id between 7e8 and 7ef
135                 //7E0 through 7E7 should respond with a id that is 8 higher (7E0->7E8)
136                 if(can_message.get_id() == 0x7DF)
137                 {
138                         message.diagnostic_response.message_id = rand()%(0x7EF-0x7E8 + 1) + 0x7E8;
139                 }
140                 else if(commandRequest->message_id >= 0x7E0 && commandRequest->message_id <= 0x7E7)
141                 {
142                         message.diagnostic_response.message_id = commandRequest->message_id + 8;
143                 }
144                 message.diagnostic_response.has_mode = true;
145                 message.diagnostic_response.mode = commandRequest->mode;
146                 if(commandRequest->has_pid)
147                 {
148                         message.diagnostic_response.has_pid = true;
149                         message.diagnostic_response.pid = commandRequest->pid;
150                 }
151                 message.diagnostic_response.has_value = true;
152                 message.diagnostic_response.value = rand() % 100;
153                 pipeline::publish(&message, &getConfiguration()->pipeline);
154         }
155         else //If it's outside the range, the command_request will return false
156         {
157                 debug("Sent message ID is outside the valid range for emulator (7DF to 7E7)");
158                 status=false;
159         }
160         return false;
161 }
162
163 void obd2_signals_t::add_request(int pid)
164 {
165         DiagnosticRequest request = {
166         arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
167         mode: 0x1, has_true, pid};
168 }
169
170 /**
171 * @brief Check if a request is an OBD-II PID request.
172 *
173 * @return true if the request is a mode 1 request and it has a 1 byte PID.
174 */
175 bool obd2_signals_t::is_obd2_request(DiagnosticRequest* request)
176 {
177         return request->mode == 0x1 && request->has_pid && request->pid < 0xff;
178 }
179
180 /**
181 * @brief Check if requested signal name is an obd2 pid
182
183 * @return true if name began with obd2 else false.
184 */
185 bool obd2_signals_t::is_obd2_signal(const char *name)
186 {
187         if(fnmatch("obd2.*", name, FNM_CASEFOLD) == 0)
188                 return true;
189         return false;
190 }
191
192 /**
193 * @brief Decode the payload of an OBD-II PID.
194 *
195 * This function matches the type signature for a DiagnosticResponseDecoder, so
196 * it can be used as the decoder for a DiagnosticRequest. It returns the decoded
197 * value of the PID, using the standard formulas (see
198 * http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01).
199 *
200 * @param[in] DiagnosticResponse response - the received DiagnosticResponse (the data is in response.payload,
201 *  a byte array). This is most often used when the byte order is
202 *  signiticant, i.e. with many OBD-II PID formulas.
203 * @param[in] float parsed_payload - the entire payload of the response parsed as an int.
204 */
205 float obd2_signals_t::decode_obd2_response(const DiagnosticResponse* response, float parsedPayload)
206 {
207         return diagnostic_decode_obd2_pid(response);
208 }