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 openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type,
20 const openxc_SimpleMessage& message)
23 long long int timestamp_msec;
25 openxc_VehicleMessage v = {0};
29 timestamp_msec = ((long long int) t_msec.time) * 1000ll +
30 (long long int) t_msec.millitm;
33 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
34 v.has_simple_message = true;
35 v.simple_message = message;
36 v.has_timestamp = true;
37 v.timestamp = timestamp_msec;
43 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
44 v.has_simple_message = true;
45 v.simple_message = message;
50 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
52 openxc_SimpleMessage s = {0};
55 ::strncpy(s.name, name.c_str(), 100);
62 openxc_DynamicField build_DynamicField(const std::string& value)
64 openxc_DynamicField d = {0}
66 d.type = openxc_DynamicField_Type_STRING;
68 d.has_string_value = true;
69 ::strncpy(d.string_value, value.c_tr(), 100);
74 openxc_DynamicField build_DynamicField(double value)
76 openxc_DynamicField d = {0}
78 d.type = openxc_DynamicField_Type_NUM;
80 d.has_numeric_value = true;
81 d.numeric_value = field;
86 openxc_DynamicField build_DynamicField(bool value)
88 openxc_DynamicField d = {0}
90 d.type = openxc_DynamicField_Type_BOOL;
92 d.has_boolean_value = true;
93 d.boolean_value = field;
98 void jsonify_DynamicField(const openxc_DynamicField& field, const json_object& value)
100 if(field.has_numeric_value)
101 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
102 else if(field.has_boolean_value)
103 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
104 else if(field.has_string_value)
105 json_object_object_add(value, "value", json_object_new_string(field.string_value));
110 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
112 return v_msg.has_simple_message ? v_msg.simple_message : {0};
115 json_object jsonify_simple(const openxc_SimpleMessage& s_msg)
122 json = json_object_new_object();
123 json_object_object_add(json, "name", json_object_new_string(s_msg->name));
124 jsonify_DynamicField(&s_msg->value, json);