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