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 openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type, const openxc_SimpleMessage& message)
24 long long int timestamp_msec;
26 openxc_VehicleMessage v;
30 timestamp_msec = ((long long int) t_msec.time) * 1000ll +
31 (long long int) t_msec.millitm;
34 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
35 v.has_simple_message = true;
36 v.simple_message = message;
37 v.has_timestamp = true;
38 v.timestamp = timestamp_msec;
44 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
45 v.has_simple_message = true;
46 v.simple_message = message;
51 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
53 openxc_SimpleMessage s;
56 ::strncpy(s.name, name.c_str(), 100);
63 openxc_DynamicField build_DynamicField(const std::string& value)
65 openxc_DynamicField d;
67 d.type = openxc_DynamicField_Type_STRING;
69 d.has_string_value = true;
70 ::strncpy(d.string_value, value.c_str(), 100);
75 openxc_DynamicField build_DynamicField(double value)
77 openxc_DynamicField d;
79 d.type = openxc_DynamicField_Type_NUM;
81 d.has_numeric_value = true;
82 d.numeric_value = value;
87 openxc_DynamicField build_DynamicField(bool value)
89 openxc_DynamicField d;
91 d.type = openxc_DynamicField_Type_BOOL;
93 d.has_boolean_value = true;
94 d.boolean_value = value;
99 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
101 if(field.has_numeric_value)
102 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
103 else if(field.has_boolean_value)
104 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
105 else if(field.has_string_value)
106 json_object_object_add(value, "value", json_object_new_string(field.string_value));
109 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
111 if (v_msg.has_simple_message)
112 return v_msg.simple_message;
114 openxc_SimpleMessage s_msg = { true, "", false, build_DynamicField(false), false, build_DynamicField(false)};
118 json_object* jsonify_simple(const openxc_SimpleMessage& s_msg)
125 json = json_object_new_object();
126 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
127 jsonify_DynamicField(s_msg.value, json);