client: Add AMB objects for Vehicle Status
[profile/ivi/automotive-message-broker.git] / lib / client / libamb-util.h
1 /*
2  * Automotive Message Broker Client Library
3  *
4  * Copyright (C) 2016 Samsung Electronics Co., Ltd.
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 #ifndef __LIBAMB_UTIL_H__
20 #define __LIBAMB_UTIL_H__
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #include <glib.h>
27 #include <gio/gio.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include "libamb-common.h"
32 #include "libamb-client.h"
33
34 #define g_variant_get_gboolean g_variant_get_boolean
35 #define g_variant_get_guchar g_variant_get_byte
36 #define g_variant_get_gint16 g_variant_get_int16
37 #define g_variant_get_guint16 g_variant_get_uint16
38 #define g_variant_get_gint32 g_variant_get_int32
39 #define g_variant_get_guint32 g_variant_get_uint32
40 #define g_variant_get_gint64 g_variant_get_int64
41 #define g_variant_get_guint64 g_variant_get_uint64
42 #define g_variant_get_gdouble g_variant_get_double
43
44 /**
45  * Generate Custom CAN Object data structure and its related utility function.
46  *
47  * @param[in] CAN Object name
48  * @param[in] data type for CAN object
49  * @param[in] Alias name that is used in AMB. If it is NULL, then this value
50  * is ignored.
51  *
52  * @see amb_release_data()
53  */
54 #define CAN_OBJECT(obj_name, value_type, alias_name) \
55         struct obj_name ## Type { \
56                 gint32  Zone; \
57                 gint32  ValueSequence; \
58                 gdouble Time; \
59                 value_type Value; \
60         }; \
61         int amb_get_ ## obj_name ## _with_zone(struct obj_name ## Type **retobj, int zone) \
62         { \
63                 GVariant *variant; \
64                 GVariantIter *iter; \
65                 gchar *key; \
66                 GVariant *value; \
67                 GVariant *tmp; \
68                 int ret; \
69                 struct obj_name ## Type *retdata = NULL; \
70                 \
71                 retdata = g_malloc0(sizeof(*retdata)); \
72                 if (!retdata) { \
73                         return -ENOMEM; \
74                 } \
75                 ret = amb_get_property_all_with_zone(&variant, #obj_name, zone); \
76                 if (ret < 0) { \
77                         return ret; \
78                 } \
79                 \
80                 g_variant_get(variant, "(a{sv})", &iter); \
81                 while(g_variant_iter_loop(iter, "{sv}", &key, &value)) { \
82                         if (!g_strcmp0(key, "Zone")) { \
83                                 g_variant_get(value, "v", &tmp); \
84                                 retdata->Zone = g_variant_get_int32(tmp); \
85                                 g_variant_unref(tmp); \
86                         } else if (!g_strcmp0(key, "ValueSequence") || !g_strcmp0(key, #alias_name "Sequence")) { \
87                                 g_variant_get(value, "v", &tmp); \
88                                 retdata->ValueSequence = g_variant_get_int32(tmp); \
89                                 g_variant_unref(tmp); \
90                         } else if (!g_strcmp0(key, "Time")) { \
91                                 g_variant_get(value, "v", &tmp); \
92                                 retdata->Time = g_variant_get_double(tmp); \
93                                 g_variant_unref(tmp); \
94                         } else if (!g_strcmp0(key, "Value") || !g_strcmp0(key, #alias_name)) { \
95                                 g_variant_get(value, "v", &tmp); \
96                                 retdata->Value = g_variant_get_ ## value_type(tmp); \
97                                 g_variant_unref(tmp); \
98                         } \
99                 } \
100                 \
101                 g_variant_iter_free(iter); \
102                 amb_release_property_all_with_zone(variant); \
103                 *retobj = retdata; \
104                 return 0; \
105         } \
106         \
107         int amb_convert_ ## obj_name ## Type (gpointer data, struct obj_name ## Type *retdata) \
108         { \
109                 GVariantIter *iter; \
110                 gchar *key; \
111                 GVariant *value; \
112                 GVariant *gdata; \
113                 \
114                 gdata = (GVariant *)data; \
115                 g_variant_get(gdata, "a{sv}", &iter); \
116                 while(g_variant_iter_loop(iter, "{sv}", &key, &value)) { \
117                         if (!g_strcmp0(key, "Zone")) { \
118                                 g_variant_get(value, "i", &retdata->Zone); \
119                         } else if (!g_strcmp0(key, "ValueSequence") || !g_strcmp0(key, #alias_name "Sequence")) { \
120                                 g_variant_get(value, "i", &retdata->ValueSequence); \
121                         } else if (!g_strcmp0(key, "Time")) { \
122                                 g_variant_get(value, "d", &retdata->Time); \
123                         } else if (!g_strcmp0(key, "Value") || !g_strcmp0(key, #alias_name)) { \
124                                 retdata->Value = g_variant_get_ ## value_type(value); \
125                         } \
126                 } \
127                 g_variant_iter_free(iter); \
128                 return 0; \
129         }
130
131 #ifdef __cplusplus
132 }
133 #endif
134
135 #endif /* __LIBAMB_UTIL_H__ */