Fix: OBD2_PIDS array initialization
[apps/agl-service-can-low-level.git] / src / openxc-utils.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type,
20         const openxc_SimpleMessage& message)
21 {
22         struct timeb t_msec;
23         long long int timestamp_msec;
24         
25         openxc_VehicleMessage v = {0};
26         
27         if(!ftime(&t_msec))
28         {
29                 timestamp_msec = ((long long int) t_msec.time) * 1000ll + 
30                                                         (long long int) t_msec.millitm;
31
32                 v.has_type = true:
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;
38
39                 return v;
40         }
41
42         v.has_type = true,
43         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
44         v.has_simple_message = true;
45         v.simple_message =  message;
46
47         return v;
48 }
49
50 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
51 {
52         openxc_SimpleMessage s = {0};
53
54         s.has_name = true;
55         ::strncpy(s.name, name.c_str(), 100);
56         s.has_value = true;
57         s.value = value;
58
59         return s;
60 }
61
62 openxc_DynamicField build_DynamicField(const std::string& value)
63 {
64         openxc_DynamicField d = {0}
65         d.has_type = true;
66         d.type = openxc_DynamicField_Type_STRING;
67         
68         d.has_string_value = true;
69         ::strncpy(d.string_value, value.c_tr(), 100);
70         
71         return d;
72 }
73
74 openxc_DynamicField build_DynamicField(double value)
75 {
76         openxc_DynamicField d = {0}
77         d.has_type = true;
78         d.type = openxc_DynamicField_Type_NUM;
79         
80         d.has_numeric_value = true;
81         d.numeric_value = field;
82         
83         return d;
84 }
85
86 openxc_DynamicField build_DynamicField(bool value)
87 {
88         openxc_DynamicField d = {0}
89         d.has_type = true;
90         d.type = openxc_DynamicField_Type_BOOL;
91         
92         d.has_boolean_value = true;
93         d.boolean_value = field;
94         
95         return d;
96 }
97
98 void jsonify_DynamicField(const openxc_DynamicField& field, const json_object& value)
99 {
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));
106
107         return value;
108 }
109
110 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
111 {
112         return v_msg.has_simple_message ? v_msg.simple_message : {0};
113 }
114
115 json_object jsonify_simple(const openxc_SimpleMessage& s_msg)
116 {
117         json_object *json;
118         json = nullptr;
119
120         if(s_msg->has_name)
121         {
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);
125         }
126         return json;
127 }