40745a0676cb645ccd69de5305f30df4da07bfbc
[apps/agl-service-can-low-level.git] / src / utils / 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 #include "openxc-utils.hpp"
20
21 openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type, const openxc_SimpleMessage& message)
22 {
23         struct timeb t_msec;
24         long long int timestamp_msec;
25         
26         openxc_VehicleMessage v;
27         
28         if(!::ftime(&t_msec))
29         {
30                 timestamp_msec = ((long long int) t_msec.time) * 1000ll + 
31                                                         (long long int) t_msec.millitm;
32
33                 v.has_type = true;
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;
39
40                 return v;
41         }
42
43         v.has_type = true,
44         v.type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE;
45         v.has_simple_message = true;
46         v.simple_message =  message;
47
48         return v;
49 }
50
51 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value)
52 {
53         openxc_SimpleMessage s;
54
55         s.has_name = true;
56         ::strncpy(s.name, name.c_str(), 100);
57         s.has_value = true;
58         s.value = value;
59
60         return s;
61 }
62
63 openxc_DynamicField build_DynamicField(const std::string& value)
64 {
65         openxc_DynamicField d;
66         d.has_type = true;
67         d.type = openxc_DynamicField_Type_STRING;
68         
69         d.has_string_value = true;
70         d.has_numeric_value = false;
71         d.has_boolean_value = false;
72         ::strncpy(d.string_value, value.c_str(), 100);
73         
74         return d;
75 }
76
77 openxc_DynamicField build_DynamicField(double value)
78 {
79         openxc_DynamicField d;
80         d.has_type = true;
81         d.type = openxc_DynamicField_Type_NUM;
82         
83         d.has_string_value = false;
84         d.has_numeric_value = true;
85         d.has_boolean_value = false;
86         d.numeric_value = value;
87         
88         return d;
89 }
90
91 openxc_DynamicField build_DynamicField(bool value)
92 {
93         openxc_DynamicField d;
94         d.has_type = true;
95         d.type = openxc_DynamicField_Type_BOOL;
96         
97         d.has_string_value = false;
98         d.has_numeric_value = false;
99         d.has_boolean_value = true;
100         d.boolean_value = value;
101         
102         return d;
103 }
104
105 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg)
106 {
107         if (v_msg.has_simple_message)
108                 return v_msg.simple_message;
109         
110         openxc_SimpleMessage s_msg = { false, "", false, build_DynamicField(false), false, build_DynamicField(false)};
111         return s_msg;
112 }
113
114 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value)
115 {
116         if(field.has_numeric_value)
117                 json_object_object_add(value, "value", json_object_new_double(field.numeric_value));
118         else if(field.has_boolean_value)
119                 json_object_object_add(value, "value", json_object_new_boolean(field.boolean_value));
120         else if(field.has_string_value)
121                 json_object_object_add(value, "value", json_object_new_string(field.string_value));
122 }
123
124 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json)
125 {
126         if(s_msg.has_name)
127         {
128                 json_object_object_add(json, "name", json_object_new_string(s_msg.name));
129                 jsonify_DynamicField(s_msg.value, json);
130                 return true;
131         }
132         json_object_object_add(json, "error", json_object_new_string("openxc_SimpleMessage doesn't have name'"));
133         return false;
134 }