c29b61d96c7f343c31963efe2d7bbe5d7ba5cad4
[platform/core/system/sensord.git] / src / server / dbus_util.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <sensor_log.h>
21 #include <dbus_util.h>
22 #include <gio/gio.h>
23
24 static int wrist_up_total_cnt;
25 static int wrist_up_lcdon_cnt;
26
27 static GDBusNodeInfo *introspection_data = NULL;
28 static guint owner_id;
29
30 static const gchar introspection_xml[] =
31 "<node>"
32 "  <interface name='org.tizen.system.sensord'>"
33 "        <method name='check_privilege'>"
34 "          <arg type='i' name='response' direction='out'/>"
35 "        </method>"
36 "        <method name='wristup_lcdon_cnt'>"
37 "          <arg type='i' name='response' direction='out'/>"
38 "        </method>"
39 "        <method name='wristup_total_cnt'>"
40 "          <arg type='i' name='response' direction='out'/>"
41 "        </method>"
42 "  </interface>"
43 "</node>";
44
45 static void method_call_handler(GDBusConnection *conn,
46                                 const gchar *sender, const gchar *object_path,
47                                 const gchar *iface_name, const gchar *method_name,
48                                 GVariant *parameters, GDBusMethodInvocation *invocation,
49                                 gpointer user_data)
50 {
51         int ret = DBUS_INIT;
52
53         if (g_strcmp0(method_name, "check_privilege") == 0) {
54                 _D("check_privilege called");
55                 ret = DBUS_SUCCESS;
56         } else if (g_strcmp0(method_name, "wristup_lcdon_cnt") == 0) {
57                 _D("wristup_lcdon_cnt called, %d", wrist_up_lcdon_cnt);
58                 ret = wrist_up_lcdon_cnt;
59         } else if (g_strcmp0(method_name, "wristup_total_cnt") == 0) {
60                 _D("wristup_total_cnt called, %d", wrist_up_total_cnt);
61                 ret = wrist_up_total_cnt;
62         } else {
63                 _D("No matched method call");
64                 ret = DBUS_FAILED;
65         }
66
67         g_dbus_method_invocation_return_value(invocation,
68                                         g_variant_new("(i)", ret));
69 }
70
71 static const GDBusInterfaceVTable interface_vtable =
72 {
73         method_call_handler,
74         NULL,
75         NULL
76 };
77
78 static void on_bus_acquired(GDBusConnection *connection,
79                 const gchar *name,
80                 gpointer user_data)
81 {
82         guint registration_id;
83
84         if (!connection) {
85                 _E("connection is null");
86                 return;
87         }
88
89         registration_id = g_dbus_connection_register_object(connection,
90                 SENSORD_OBJECT_PATH,
91                 introspection_data->interfaces[0],
92                 &interface_vtable,
93                 NULL,  /* user_data */
94                 NULL,  /* user_data_free_func */
95                 NULL); /* GError** */
96
97         if (registration_id == 0)
98                 _E("Failed to g_dbus_connection_register_object");
99
100         _I("Gdbus method call registrated");
101 }
102
103 static void on_name_acquired(GDBusConnection *conn,
104                                 const gchar *name, gpointer user_data)
105 {
106 }
107
108 static void on_name_lost(GDBusConnection *conn,
109                                 const gchar *name, gpointer user_data)
110 {
111         _E("Dbus name is lost!");
112 }
113
114 int get_lcdon_count(void)
115 {
116         return wrist_up_lcdon_cnt;
117 }
118
119 void increase_lcdon_count(void)
120 {
121         wrist_up_lcdon_cnt++;
122 }
123
124 void reset_lcdon_count(void)
125 {
126         wrist_up_lcdon_cnt = 0;
127 }
128
129 int get_total_count(void)
130 {
131         return wrist_up_total_cnt;
132 }
133
134 void increase_total_count(void)
135 {
136         wrist_up_total_cnt++;
137 }
138
139 void reset_total_count(void)
140 {
141         wrist_up_total_cnt = 0;
142 }
143
144 void init_dbus(void)
145 {
146 #ifndef GLIB_VERSION_2_36
147         g_type_init();
148 #endif
149
150         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
151         if (introspection_data == NULL) {
152                 _E("Failed to init g_dbus_node_info_new_for_xml");
153                 return;
154         }
155
156         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
157                                                            SENSORD_BUS_NAME,
158                                                            (GBusNameOwnerFlags) (G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT
159                                                            | G_BUS_NAME_OWNER_FLAGS_REPLACE),
160                                                            on_bus_acquired,
161                                                            on_name_acquired,
162                                                            on_name_lost,
163                                                            NULL,
164                                                            NULL);
165         wrist_up_total_cnt = 0;
166         wrist_up_lcdon_cnt = 0;
167 }
168
169 void fini_dbus(void)
170 {
171         if (owner_id != 0)
172                 g_bus_unown_name(owner_id);
173
174         if (introspection_data)
175                 g_dbus_node_info_unref(introspection_data);
176 }