Raw import openXC function to detect obd2 response.
[apps/low-level-can-service.git] / src / obd2-signals.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-signals.hpp"
19
20 #include "signals.hpp"
21
22 const char *UNIT_NAMES[10] = {
23         "POURCENT",
24         "DEGREES_CELSIUS",
25         "KPA",
26         "RPM",
27         "GRAMS_SEC",
28         "SECONDS",
29         "KM",
30         "KM_H",
31         "PA",
32         "NM"
33 };
34
35 /*
36 * Pre-defined OBD-II PIDs to query for if supported by the vehicle.
37 */
38  std::vector<Obd2Pid> OBD2_PIDS = {
39         { 0x04, "obd2.engine.load", 0, 100, POURCENT, 5,  false},
40         { 0x05, "obd2.engine.coolant.temperature", -40, 215, DEGREES_CELSIUS, 1,  false},
41         { 0x0a, "obd2.fuel.pressure", 0, 765, KPA, 1,  false},
42         { 0x0b, "obd2.intake.manifold.pressure", 0, 255, KPA, 1,  false},
43         { 0x0c, "obd2.engine.speed", 0, 16383, RPM, 5,  false},
44         { 0x0d, "obd2.vehicle.speed", 0, 255, KM_H, 5,  false},
45         { 0x0f, "obd2.intake.air.temperature", -40, 215, DEGREES_CELSIUS, 1,  false},
46         { 0x10, "obd2.mass.airflow", 0, 655, GRAMS_SEC, 5,  false},
47         { 0x11, "obd2.throttle.position", 0, 100, POURCENT, 5,  false},
48         { 0x1f, "obd2.running.time", 0, 65535, SECONDS, 1,  false},
49         { 0x2d, "obd2.EGR.error", -100, 99, POURCENT, 0,  false},
50         { 0x2f, "obd2.fuel.level", 0, 100, POURCENT, 1,  false},
51         { 0x33, "obd2.barometric.pressure", 0, 255, KPA, 1,  false},
52         { 0x4c, "obd2.commanded.throttle.position", 0, 100, POURCENT, 1,  false},
53         { 0x52, "obd2.ethanol.fuel.percentage", 0, 100, POURCENT, 1,  false},
54         { 0x5a, "obd2.accelerator.pedal.position", 0, 100, POURCENT, 5,  false},
55         { 0x5b, "obd2.hybrid.battery-pack.remaining.life", 0, 100, POURCENT, 5,  false},
56         { 0x5c, "obd2.engine.oil.temperature",-40, 210, DEGREES_CELSIUS, 1,  false},
57         { 0x63, "obd2.engine.torque", 0, 65535, NM, 1,  false}
58 };
59
60 /**
61  * @fn std::vector<std::string> find_signals(const openxc_DynamicField &key)
62  * @brief return signals name found searching through CAN_signals and OBD2 pid
63  * 
64  * @param[in] const openxc_DynamicField : can contain numeric or string value in order to search against 
65  *   can signals or obd2 signals name.
66  *
67  * @return std::vector<std::string> Vector of signals name found. 
68  */
69 void find_signals(const openxc_DynamicField &key, std::vector<Obd2Pid*>& found_signals)
70 {
71         switch(key.type)
72         {
73                 case openxc_DynamicField_Type::openxc_DynamicField_Type_STRING:
74                                 lookup_signals_by_name(key.string_value, get_obd2_signals(), found_signals);
75                         break;
76                 case openxc_DynamicField_Type::openxc_DynamicField_Type_NUM:
77                                 lookup_signals_by_id(key.numeric_value, get_obd2_signals(), found_signals);
78                         break;
79                 default:
80                         ERROR(binder_interface, "find_signals: wrong openxc_DynamicField specified. Use openxc_DynamicField_Type_NUM or openxc_DynamicField_Type_STRING type only.");
81                         break;
82         }
83         DEBUG(binder_interface, "Found %d signal(s)", (int)found_signals.size());
84 }
85
86 std::vector<Obd2Pid>& get_obd2_signals()
87 {
88         return OBD2_PIDS;
89 }
90
91 uint32_t get_signal_id(const Obd2Pid& sig)
92 {
93         return (uint32_t)sig.pid;
94 }
95
96 void shims_logger(const char* m, const struct afb_binding_interface *interface)
97 {
98         DEBUG(interface, "%s", m);
99 }
100
101 void shims_timer()
102 {
103 }
104
105 bool is_obd2_response(can_message_t can_message)
106 {
107         if(can_message.get_id() >= 0x7E8 && can_message.get_id() <= 0x7EF)
108         {
109                 openxc_VehicleMessage message = {0};
110                 message.has_type = true;
111                 message.type = openxc_VehicleMessage_Type_DIAGNOSTIC;
112                 message.has_diagnostic_response = true;
113                 message.diagnostic_response = {0};
114                 message.diagnostic_response.has_bus = true;
115                 message.diagnostic_response.bus = bus->address;
116                 message.diagnostic_response.has_message_id = true;
117                 //7DF should respond with a random message id between 7e8 and 7ef
118                 //7E0 through 7E7 should respond with a id that is 8 higher (7E0->7E8)
119                 if(commandRequest->message_id == 0x7DF)
120                 {
121                         message.diagnostic_response.message_id = rand()%(0x7EF-0x7E8 + 1) + 0x7E8;
122                 }
123                 else if(commandRequest->message_id >= 0x7E0 && commandRequest->message_id <= 0x7E7)
124                 {
125                         message.diagnostic_response.message_id = commandRequest->message_id + 8;
126                 }
127                 message.diagnostic_response.has_mode = true;
128                 message.diagnostic_response.mode = commandRequest->mode;
129                 if(commandRequest->has_pid)
130                 {
131                         message.diagnostic_response.has_pid = true;
132                         message.diagnostic_response.pid = commandRequest->pid;
133                 }
134                 message.diagnostic_response.has_value = true;
135                 message.diagnostic_response.value = rand() % 100;
136                 pipeline::publish(&message, &getConfiguration()->pipeline);
137         }
138         else //If it's outside the range, the command_request will return false
139         {
140                 debug("Sent message ID is outside the valid range for emulator (7DF to 7E7)");
141                 status=false;
142 };
143
144 /*
145  * Will scan for supported Obd2 pids
146  *
147 obd2_handler_t::obd2_handler_t()
148 {
149         DiagnosticShims shims_ = diagnostic_init_shims(shims_logger, can_bus.send_can_message, NULL);
150
151         int n_pids_, i_;
152
153         n_pids_ = size(Obd2Pid);
154         for(i_=0; i_<=n_pids_; i_++)
155         {
156         }
157 }
158
159 void obd2_handler_t::add_request(int pid)
160 {
161         DiagnosticRequest request = {
162         arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
163         mode: 0x1, has_true, pid};
164 }
165
166 bool obd2_handler_t::is_obd2_request(DiagnosticRequest* request)
167 {
168         return request->mode == 0x1 && request->has_pid && request->pid < 0xff;
169 }
170
171 bool obd2_handler_t::is_obd2_signal(const char *name)
172 {
173         if(fnmatch("obd2.*", name, FNM_CASEFOLD) == 0)
174                 return true;
175         return false;
176 }
177
178 bool obd2_handler_t::decode_obd2_response(DiagnosticResponse* responce)
179 {
180         return diagnostic_decode_obd2_pid(response);
181 }*/