Some cleaning and comments
[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, const openxc_SimpleMessage& message)
20 {
21         struct timeb t_msec;
22         long long int timestamp_msec;
23         
24         openxc_VehicleMessage v = {0};
25         
26         if(!ftime(&t_msec))
27         {
28                 timestamp_msec = ((long long int) t_msec.time) * 1000ll + 
29                                                         (long long int) t_msec.millitm;
30
31                 v.has_type = true:
32                 v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
33                 v.has_simple_message = true;
34                 v.simple_message =  message;
35                 v.has_timestamp = true;
36                 v.timestamp = timestamp_msec;
37
38                 return v;
39         }
40
41         v.has_type = true,
42         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
43         v.has_simple_message = true;
44         v.simple_message =  message;
45
46         return v;
47 }
48
49 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
50 {
51         openxc_SimpleMessage s = {0};
52
53         s.has_name = true;
54         ::strncpy(s.name, name.c_str(), 100);
55         s.has_value = true;
56         s.value = value;
57
58         return s;
59 }
60
61 openxc_DynamicField build_DynamicField(const std::string& value)
62 {
63         openxc_DynamicField d = {0}
64         d.has_type = true;
65         d.type = openxc_DynamicField_Type_STRING;
66         
67         d.has_string_value = true;
68         ::strncpy(d.string_value, value.c_tr(), 100);
69         
70         return d;
71 }
72
73 openxc_DynamicField build_DynamicField(double value)
74 {
75         openxc_DynamicField d = {0}
76         d.has_type = true;
77         d.type = openxc_DynamicField_Type_NUM;
78         
79         d.has_numeric_value = true;
80         d.numeric_value = field;
81         
82         return d;
83 }
84
85 openxc_DynamicField build_DynamicField(bool value)
86 {
87         openxc_DynamicField d = {0}
88         d.has_type = true;
89         d.type = openxc_DynamicField_Type_BOOL;
90         
91         d.has_boolean_value = true;
92         d.boolean_value = field;
93         
94         return d;
95 }
96
97 void jsonify_DynamicField(const openxc_DynamicField& field, const json_object& value)
98 {
99         if(field.has_numeric_value)
100                 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
101         else if(field.has_boolean_value)
102                 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
103         else if(field.has_string_value)
104                 json_object_object_add(value, "value", json_object_new_string(field.string_value));
105
106         return value;
107 }
108
109 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
110 {
111         return v_msg.has_simple_message ? v_msg.simple_message : {0};
112 }
113
114 json_object jsonify_simple(const openxc_SimpleMessage& s_msg)
115 {
116         json_object *json;
117         json = nullptr;
118
119         if(s_msg->has_name)
120         {
121                 json = json_object_new_object();
122                 json_object_object_add(json, "name", json_object_new_string(s_msg->name));
123                 jsonify_DynamicField(&s_msg->value, json);
124         }
125         return json;
126 }