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