2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loic Collignon" <loic.collignon@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "openxc-utils.hpp"
21 #include "../configuration.hpp"
23 openxc_VehicleMessage build_VehicleMessage(active_diagnostic_request_t* request, const DiagnosticResponse& response, float parsed_value)
25 openxc_VehicleMessage message;
27 message.has_type = true;
28 message.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_DIAGNOSTIC;
29 message.has_diagnostic_response = true;
30 message.diagnostic_response.has_bus = true;
31 message.diagnostic_response.bus = configuration_t::instance().get_diagnostic_manager().get_can_bus_dev()->get_address();
32 message.diagnostic_response.has_message_id = true;
34 if(request->get_id() != OBD2_FUNCTIONAL_BROADCAST_ID)
36 message.diagnostic_response.message_id = response.arbitration_id
37 - DIAGNOSTIC_RESPONSE_ARBITRATION_ID_OFFSET;
41 // must preserve responding arb ID for responses to functional broadcast
42 // requests, as they are the actual module address and not just arb ID +
44 message.diagnostic_response.message_id = response.arbitration_id;
47 message.diagnostic_response.has_mode = true;
48 message.diagnostic_response.mode = response.mode;
49 message.diagnostic_response.has_pid = response.has_pid;
50 if(message.diagnostic_response.has_pid)
51 message.diagnostic_response.pid = response.pid;
52 message.diagnostic_response.has_success = true;
53 message.diagnostic_response.success = response.success;
54 message.diagnostic_response.has_negative_response_code = !response.success;
55 message.diagnostic_response.negative_response_code =
56 response.negative_response_code;
58 if(response.payload_length > 0)
60 if(request->get_decoder() != nullptr)
62 message.diagnostic_response.has_value = true;
63 message.diagnostic_response.value = parsed_value;
67 message.diagnostic_response.has_payload = true;
68 ::memcpy(message.diagnostic_response.payload.bytes, response.payload,
69 response.payload_length);
70 message.diagnostic_response.payload.size = response.payload_length;
77 openxc_VehicleMessage build_VehicleMessage(const openxc_SimpleMessage& message)
79 openxc_VehicleMessage v;
82 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
83 v.has_simple_message = true;
84 v.simple_message = message;
85 v.has_timestamp = true;
86 v.timestamp = system_time_ms();
91 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
93 openxc_SimpleMessage s;
96 ::strncpy(s.name, name.c_str(), 100);
103 openxc_DynamicField build_DynamicField(const std::string& value)
105 openxc_DynamicField d;
107 d.type = openxc_DynamicField_Type_STRING;
109 d.has_string_value = true;
110 d.has_numeric_value = false;
111 d.has_boolean_value = false;
112 ::strncpy(d.string_value, value.c_str(), 100);
117 openxc_DynamicField build_DynamicField(double value)
119 openxc_DynamicField d;
121 d.type = openxc_DynamicField_Type_NUM;
123 d.has_string_value = false;
124 d.has_numeric_value = true;
125 d.has_boolean_value = false;
126 d.numeric_value = value;
131 openxc_DynamicField build_DynamicField(bool value)
133 openxc_DynamicField d;
135 d.type = openxc_DynamicField_Type_BOOL;
137 d.has_string_value = false;
138 d.has_numeric_value = false;
139 d.has_boolean_value = true;
140 d.boolean_value = value;
145 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
147 if (v_msg.has_simple_message)
148 return v_msg.simple_message;
150 openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
154 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
156 if(field.has_numeric_value)
157 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
158 else if(field.has_boolean_value)
159 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
160 else if(field.has_string_value)
161 json_object_object_add(value, "value", json_object_new_string(field.string_value));
164 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
168 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
169 jsonify_DynamicField(s_msg.value, json);
172 json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));