[Non-ACR][TFIVE-357]
[platform/core/system/sensord.git] / src / sensorctl / dbus_util.cpp
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2015 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
20 #include "dbus_util.h"
21
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25
26 #include "log.h"
27
28 static GDBusConnection *connection;
29
30 bool dbus_init(void)
31 {
32         GError *error = NULL;
33         gchar *gaddr;
34
35         if (connection)
36                 return true;
37
38 #ifndef GLIB_VERSION_2_36
39         g_type_init();
40 #endif
41
42         gaddr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
43
44         if (!gaddr) {
45                 _E("Failed to get dbus address : %s\n", error->message);
46                 g_error_free(error);
47                 error = NULL;
48                 return false;
49         }
50
51         connection = g_dbus_connection_new_for_address_sync(gaddr,
52                         (GDBusConnectionFlags)(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
53                         | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
54                         NULL, NULL, &error);
55         g_free(gaddr);
56
57         if (!connection) {
58                 _E("Failed to get dbus connection : %s\n", error->message);
59                 g_error_free(error);
60                 error = NULL;
61                 return false;
62         }
63
64         _I("G-DBUS connected[%s]\n",
65                         g_dbus_connection_get_unique_name(connection));
66         return true;
67 }
68
69 bool dbus_fini(void)
70 {
71         if (!connection)
72                 return true;
73
74         g_dbus_connection_close_sync(connection, NULL, NULL);
75         g_object_unref(connection);
76         connection = NULL;
77
78         return true;
79 }
80
81 bool dbus_emit_signal(gchar *dest_bus_name, gchar *object_path,
82                 gchar *interface_name, gchar *signal_name,
83                 GVariant *variant, GError **error)
84 {
85         g_dbus_connection_emit_signal(connection,
86                                 dest_bus_name,
87                                 object_path,
88                                 interface_name,
89                                 signal_name,
90                                 variant,
91                                 error);
92         return true;
93 }
94
95 GVariant *make_variant_int(int count, char *options[])
96 {
97         switch (count) {
98         case 1:
99                 return g_variant_new("(i)", atoi(options[0]));
100         case 2:
101                 return g_variant_new("(ii)", atoi(options[0]), atoi(options[1]));
102         case 3:
103                 return g_variant_new("(iii)", atoi(options[0]), atoi(options[1]), atoi(options[2]));
104         default:
105                 break;
106         }
107
108         return NULL;
109 }