Beginning of work on obd2 object and
[apps/low-level-can-service.git] / src / utils / openxc-utils.hpp
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 #pragma once
20
21 #include <string>
22 #include <json-c/json.h>
23 #include <sys/timeb.h>
24
25 #include "openxc.pb.h"
26
27 /**
28  * @fn openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type, const openxc_SimpleMessage& message);
29  *
30  * @brief Build a specific VehicleMessage containing a SimpleMessage.
31  *
32  * @param[in] openxc_DynamicField_Type type - The type of message to build
33  * @param[in] openxc_SimpleMessage& message - simple message to include into openxc_VehicleMessage
34  *
35  * @return openxc_VehicleMessage - a vehicle message including simple message that will be convert into 
36  * a JSON object before being pushed to the subscribers
37  */
38 openxc_VehicleMessage build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type type, const openxc_SimpleMessage& message);
39
40 /**
41  * @fn openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value);
42  *
43  * @brief Build an openxc_SimpleMessage associating a name to an openxc_DynamicField
44  *
45  * @param[in] std::string name - const string reference name to assign to the created SimpleMessage
46  *  this will set has_name member to true and assign name to the name member. Maximum size for name is 
47  *  set to 100 char.
48  * @param[in] openxc_DynamicField value - const reference with DynamicField to assign to SimpleMessage
49  *  value.
50  *
51  * @return an openxc_SimpleMessage struct initialized with name and value provided.
52  */
53 openxc_SimpleMessage build_SimpleMessage(const std::string& name, const openxc_DynamicField& value);
54
55 /**
56  * @fn openxc_DynamicField build_DynamicField(const std::string& value);
57  *
58  * @brief Build an openxc_DynamicField with a string value
59  *
60  * @param std::string value - const string reference value to assign to builded
61  *  openxc_DynamicField.
62  *
63  * @return openxc_DynamicField initialized with a string value.
64  */
65 openxc_DynamicField build_DynamicField(const std::string& value);
66
67 /**
68  * @fn openxc_DynamicField build_DynamicField(double value);
69  *
70  * @brief Build an openxc_DynamicField with a double value
71  *
72  * @param double value - double value to assign to builded openxc_DynamicField.
73  *
74  * @return openxc_DynamicField initialized with a double value.
75  */
76 openxc_DynamicField build_DynamicField(double value);
77
78 /**
79  * @fn openxc_DynamicField build_DynamicField(bool value);
80  *
81  * @brief Build an openxc_DynamicField with a boolean value
82  *
83  * @param bool value - boolean value to assign to builded openxc_DynamicField.
84  *
85  * @return openxc_DynamicField initialized with a boolean value.
86  */
87 openxc_DynamicField build_DynamicField(bool value);
88
89 /**
90  * @fn void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value);
91  *
92  * @brief Make a JSON object from a DynamicField
93  *
94  * @param[in] openxc_DynamicField field - openxc_DynamicField struct to convert into
95  *  a json object.
96  * @param[out] json_object pointer with the DynamicField converted
97  */
98 void jsonify_DynamicField(const openxc_DynamicField& field, json_object* value);
99
100 /**
101  * @fn openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg);
102  *
103  * @brief Extract the simple message value from an openxc_VehicleMessage
104  *  and return it. If there isn't SimpleMessage in the VehicleMessage then
105  *  returned value will be a SimpleMessage with all field set at false.
106  *  DynamicField from SimpleMessage will be boolean DynamicField set to false too.
107  *
108  * @param[in] openxc_VehicleMessage - const reference to openxc_VehicleMessage
109  *
110  * @return openxc_SimpleMessage - SimpleMessage from the provided VehicleMessage.
111  */
112 openxc_SimpleMessage get_simple_message(const openxc_VehicleMessage& v_msg);
113
114 /**
115  * @fn json_object* jsonify_simple(const openxc_SimpleMessage& s_msg);
116  *
117  * @brief Make a JSON object from a SimpleMessage
118  *
119  * @param[in] openxc_SimpleMessage s_msg - const reference to an openxc_SimpleMessage 
120  * struct to convert into a json object.
121  * @param[out] json_object pointer with the DynamicField converted
122  * 
123  * @return boolean value - true if SimpleMessage has been transformed into json object
124  *  and false if not. In such case, a json object is returned { "error": "error msg"}
125  */
126 bool jsonify_simple(const openxc_SimpleMessage& s_msg, json_object* json);