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