tizen 2.4 release
[framework/location/geofence-dbus.git] / geofence / src / geofence_client.c
1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdio.h>
17 #include "geofence_client.h"
18 #include "geofence_client_priv.h"
19
20 #define GEOFENCE_SERVICE_NAME   "org.tizen.lbs.Providers.GeofenceServer"
21 #define GEOFENCE_SERVICE_PATH   "/org/tizen/lbs/Providers/GeofenceServer"
22 #define GEOFENCE_INTERFACE_NAME "org.tizen.lbs.Geofence"
23
24
25 typedef struct _geofence_client_dbus_s {
26         GDBusConnection *conn;
27         gchar *service_name;
28         gchar *service_path;
29         gchar *signal_path;
30         int geofence_evt_id;
31         int geofence_evt_status_id;
32         geofence_client_cb user_cb;
33         void *user_data;
34 } geofence_client_dbus_s;
35
36 static void __geofence_signal_callback(GDBusConnection *conn, const gchar *name, const gchar *path, const gchar *interface, const gchar *sig, GVariant *param, gpointer user_data)
37 {
38         GEOFENCE_CLIENT_SECLOG("name: %s, path: %s, interface: %s, sig: %s, handle[%p]", name, path, interface, sig, user_data);
39         geofence_client_dbus_s *handle = (geofence_client_dbus_s *) user_data;
40         if (handle == NULL) {
41                 GEOFENCE_CLIENT_LOGD("Invalid handle");
42                 return;
43         }
44         if (handle->user_cb)
45                 handle->user_cb(sig, param, handle->user_data);
46 }
47
48 EXPORT_API int geo_client_add_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id, gint geofence_type, gdouble latitude, gdouble longitude, gint radius, const gchar *address, const gchar *bssid, const gchar *ssid)
49 {
50         GEOFENCE_CLIENT_LOGD("ENTER >>>");
51         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
52
53         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
54         GVariant *reg = NULL;
55         GError *error = NULL;
56         GDBusProxy *proxy = NULL;
57         int fence_id = -1;
58
59         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
60         if (proxy) {
61                 GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);
62
63                 reg = g_dbus_proxy_call_sync(proxy, "AddGeofence", g_variant_new("(siiddisss)", app_id, place_id, geofence_type, latitude, longitude, radius, address, bssid, ssid), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
64                 if (reg) {
65                         g_variant_get(reg, "(i)", &fence_id);
66                         g_variant_unref(reg);
67                         reg = NULL;
68                 } else {
69                         if (error) {
70                                 GEOFENCE_CLIENT_LOGE("Fail to add geofence Error[%s]", error->message);
71                                 g_error_free(error);
72                         }
73                 }
74                 g_object_unref(proxy);
75         } else {
76                 if (error) {
77                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
78                         g_error_free(error);
79                 }
80
81         }
82         GEOFENCE_CLIENT_LOGD("fence_id: %d", fence_id);
83
84         return fence_id;
85 }
86
87 EXPORT_API int geo_client_delete_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint fence_id)
88 {
89         GEOFENCE_CLIENT_LOGD("ENTER >>>");
90         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
91
92         geofence_client_dbus_s *handle = (geofence_client_dbus_s *) geofence_client;
93         GError *error = NULL;
94         GDBusProxy *proxy = NULL;
95         int ret = GEOFENCE_CLIENT_ERROR_NONE;
96
97         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
98         if (proxy) {
99                 g_dbus_proxy_call(proxy, "DeleteGeofence", g_variant_new("(is)", fence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
100         } else {
101                 if (error) {
102                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
103                         g_error_free(error);
104                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
105                 }
106         }
107
108         return ret;
109 }
110
111 EXPORT_API int geo_client_get_geofences(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id, GVariantIter **iter, gint *fence_cnt, gint *error_code)
112 {
113         GEOFENCE_CLIENT_LOGD("ENTER >>>");
114         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
115
116         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
117
118         GVariant *reg = NULL;
119         GError *error = NULL;
120         GDBusProxy *proxy = NULL;
121         int ret = GEOFENCE_CLIENT_ERROR_NONE;
122         GVariantIter *iterator;
123         int new_error_code = 0;
124         int new_fence_cnt = 0;
125
126         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
127         if (proxy) {
128                 GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);
129
130                 reg = g_dbus_proxy_call_sync(proxy, "GetGeofences", g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
131                 if (reg) {
132                         g_variant_get(reg, "(iiaa{sv})", &new_fence_cnt, &new_error_code, &iterator);
133                         *error_code = new_error_code;
134                         *fence_cnt = new_fence_cnt;
135                         if (iterator == NULL)
136                                 GEOFENCE_CLIENT_LOGE("Iterator is null");
137                         *iter = iterator;
138                         g_variant_unref(reg);
139                 } else {
140                         if (error) {
141                                 GEOFENCE_CLIENT_LOGE("Fail to get the list Error[%s]", error->message);
142                                 g_error_free(error);
143                                 ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
144                         }
145                 }
146                 g_object_unref(proxy);
147         } else {
148                 if (error) {
149                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
150                         g_error_free(error);
151                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
152                 }
153         }
154
155         return ret;
156 }
157
158 EXPORT_API int geo_client_enable_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id, gboolean onoff)
159 {
160         GEOFENCE_CLIENT_LOGD("ENTER >>>");
161         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
162
163         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
164         GError *error = NULL;
165         GDBusProxy *proxy = NULL;
166         int ret = GEOFENCE_CLIENT_ERROR_NONE;
167
168         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
169         if (proxy) {
170                 g_dbus_proxy_call(proxy, "EnableGeofence", g_variant_new("(isb)", geofence_id, app_id, onoff),  G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
171         } else {
172                 if (error) {
173                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
174                         g_error_free(error);
175                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
176                 }
177         }
178
179         return ret;
180 }
181
182 EXPORT_API int geo_client_start_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id)
183 {
184         GEOFENCE_CLIENT_LOGD("ENTER >>>");
185         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
186
187         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
188         GError *error = NULL;
189         GDBusProxy *proxy = NULL;
190         int ret = GEOFENCE_CLIENT_ERROR_NONE;
191
192         GEOFENCE_CLIENT_LOGD("handle->conn: %p, geofence_id", handle->conn, geofence_id);
193
194         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
195         if (proxy) {
196                 g_dbus_proxy_call(proxy, "StartGeofence", g_variant_new("(is)", geofence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
197         } else {
198                 if (error) {
199                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
200                         g_error_free(error);
201                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
202                 }
203         }
204         return ret;
205 }
206
207 EXPORT_API int geo_client_stop_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id)
208 {
209         GEOFENCE_CLIENT_LOGD("ENTER >>>");
210         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
211
212         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
213         int ret = GEOFENCE_CLIENT_ERROR_NONE;
214         GError *error = NULL;
215         GDBusProxy *proxy = NULL;
216
217         GEOFENCE_CLIENT_LOGD("handle->conn: %p", handle->conn);
218
219         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
220         if (proxy) {
221                 g_dbus_proxy_call(proxy, "StopGeofence", g_variant_new("(is)", geofence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
222         } else {
223                 if (error) {
224                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
225                         g_error_free(error);
226                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
227                 }
228         }
229
230         return ret;
231 }
232
233 EXPORT_API int geo_client_add_place(geofence_client_dbus_h geofence_client, gchar *app_id, const gchar *place_name)
234 {
235         /* add fence interface between App & geofence-server */
236         GEOFENCE_CLIENT_LOGD("ENTER >>>");
237         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
238
239         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
240         GVariant *reg = NULL;
241         GError *error = NULL;
242         GDBusProxy *proxy = NULL;
243         int place_id = -1;
244
245         GEOFENCE_CLIENT_LOGI("APP ID: %s", app_id);
246
247         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
248         if (proxy) {
249                 GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);
250                 reg = g_dbus_proxy_call_sync(proxy, "AddPlace", g_variant_new("(ss)", app_id, place_name), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
251                 if (reg) {
252                         g_variant_get(reg, "(i)", &place_id);
253                         g_variant_unref(reg);
254                         reg = NULL;
255                 } else {
256                         if (error) {
257                                 GEOFENCE_CLIENT_LOGE("Fail to add place Error[%s]", error->message);
258                                 g_error_free(error);
259                         }
260                 }
261                 g_object_unref(proxy);
262         } else {
263                 if (error) {
264                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
265                         g_error_free(error);
266                 }
267         }
268
269         GEOFENCE_CLIENT_LOGD("place_id: %d", place_id);
270
271         return place_id;
272 }
273
274 EXPORT_API int geo_client_update_place(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id, const gchar *place_name)
275 {
276         GEOFENCE_CLIENT_LOGD("ENTER >>>");
277         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
278
279         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
280         GError *error = NULL;
281         GDBusProxy *proxy = NULL;
282         int ret = GEOFENCE_CLIENT_ERROR_NONE;
283
284         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
285         if (proxy) {
286                 g_dbus_proxy_call(proxy, "UpdatePlace", g_variant_new("(iss)", place_id, app_id, place_name), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
287         } else {
288                 if (error) {
289                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
290                         g_error_free(error);
291                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
292                 }
293         }
294
295         return ret;
296 }
297
298 EXPORT_API int geo_client_delete_place(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id)
299 {
300         GEOFENCE_CLIENT_LOGD("ENTER >>>");
301         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
302
303         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
304         GError *error = NULL;
305         GDBusProxy *proxy = NULL;
306         int ret = GEOFENCE_CLIENT_ERROR_NONE;
307
308         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
309         if (proxy) {
310                 g_dbus_proxy_call(proxy, "DeletePlace", g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
311         } else {
312                 if (error) {
313                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
314                         g_error_free(error);
315                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
316                 }
317         }
318
319         return ret;
320 }
321
322 EXPORT_API int geo_client_get_place_name(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id, gchar **place_name, gint *error_code)
323 {
324         GEOFENCE_CLIENT_LOGD("ENTER >>>");
325         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
326
327         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
328         GVariant *reg = NULL;
329         GError *error = NULL;
330         GDBusProxy *proxy = NULL;
331         int ret = GEOFENCE_CLIENT_ERROR_NONE;
332         char *new_place_name = NULL;
333         int new_error_code = 0;
334
335         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
336         if (proxy) {
337                 reg = g_dbus_proxy_call_sync(proxy, "GetPlaceName", g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
338                 if (reg) {
339                         g_variant_get(reg, "(is)", &new_error_code, &new_place_name);
340                         *error_code = new_error_code;
341                         *place_name = g_strdup(new_place_name);
342                         g_free(new_place_name);
343                         g_variant_unref(reg);
344                 } else {
345                         if (error) {
346                                 GEOFENCE_CLIENT_LOGE("Fail to get the place name Error[%s]", error->message);
347                                 g_error_free(error);
348                                 ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
349                         }
350                 }
351                 g_object_unref(proxy);
352         } else {
353                 if (error) {
354                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
355                         g_error_free(error);
356                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
357                 }
358         }
359
360         return ret;
361 }
362
363 EXPORT_API int geo_client_get_places(geofence_client_dbus_h geofence_client, gchar *app_id, GVariantIter **iter, gint *place_cnt, gint *error_code)
364 {
365         GEOFENCE_CLIENT_LOGD("ENTER >>>");
366         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
367
368         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
369         GVariant *reg = NULL;
370         GError *error = NULL;
371         GDBusProxy *proxy = NULL;
372
373         int ret = GEOFENCE_CLIENT_ERROR_NONE;
374         GVariantIter *iterator;
375         int new_error_code = 0;
376         int new_place_cnt = 0;
377
378         GEOFENCE_CLIENT_LOGD("handle->conn: %p", handle->conn);
379         GEOFENCE_CLIENT_LOGI("APP ID: %s", app_id);
380
381         proxy = g_dbus_proxy_new_sync(handle->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, handle->service_name, handle->signal_path, GEOFENCE_INTERFACE_NAME, NULL, &error);
382         if (proxy) {
383                 reg = g_dbus_proxy_call_sync(proxy, "GetPlaces", g_variant_new("(s)", app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
384                 if (reg) {
385                         g_variant_get(reg, "(iiaa{sv})", &new_place_cnt, &new_error_code, &iterator);
386                         *place_cnt = new_place_cnt;
387                         *error_code = new_error_code;
388                         if (iterator == NULL)
389                                 GEOFENCE_CLIENT_LOGE("Iterator is null");
390                         *iter = iterator;
391                         g_variant_unref(reg);
392                 } else {
393                         if (error) {
394                                 GEOFENCE_CLIENT_LOGE("Fail to get the place list Error[%s]", error->message);
395                                 g_error_free(error);
396                                 ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
397                         }
398                 }
399                 g_object_unref(proxy);
400         } else {
401                 if (error) {
402                         GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
403                         g_error_free(error);
404                         ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
405                 }
406         }
407
408         return ret;
409 }
410
411 EXPORT_API int geo_client_start(geofence_client_dbus_h geofence_client, geofence_client_cb callback, void *user_data)
412 {
413         GEOFENCE_CLIENT_LOGD("ENTER >>>");
414         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
415
416         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
417         gchar *signal_path = NULL;
418
419         handle->service_name = g_strdup(GEOFENCE_SERVICE_NAME);
420         handle->service_path = g_strdup(GEOFENCE_SERVICE_PATH);
421         handle->signal_path = g_strdup_printf("%s/%s", handle->service_path, "SAMSUNG");
422         GEOFENCE_CLIENT_LOGD("Object Path [%s]", handle->signal_path);
423
424         if (callback) {
425                 handle->user_cb = callback;
426                 handle->user_data = user_data;
427                 handle->geofence_evt_id = g_dbus_connection_signal_subscribe(handle->conn, handle->service_name, GEOFENCE_INTERFACE_NAME, "GeofenceInout", handle->signal_path, NULL, G_DBUS_SIGNAL_FLAGS_NONE, __geofence_signal_callback, handle, NULL);
428
429                 if (handle->geofence_evt_id) {
430                         GEOFENCE_CLIENT_LOGD("Listening GeofenceInout");
431                 } else {
432                         GEOFENCE_CLIENT_LOGD("Fail to listen GeofenceInout");
433                 }
434
435                 handle->geofence_evt_status_id = g_dbus_connection_signal_subscribe(handle->conn, handle->service_name, GEOFENCE_INTERFACE_NAME, "GeofenceEvent", handle->signal_path,  NULL, G_DBUS_SIGNAL_FLAGS_NONE, __geofence_signal_callback, handle, NULL);
436
437                 if (handle->geofence_evt_status_id) {
438                         GEOFENCE_CLIENT_LOGD("Listening Geofence event");
439                 } else {
440                         GEOFENCE_CLIENT_LOGD("Fail to listen Geofence event");
441                         return GEOFENCE_CLIENT_ERROR_DBUS_CALL;
442                 }
443         }
444         g_free(signal_path);
445
446
447 #if SUPPORT_MULTI_CLIENT
448         GVariant *param = NULL;
449         GVariantBuilder *builder = NULL;
450
451
452         GEOFENCE_CLIENT_LOGD("START: CMD-START");
453         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
454         g_variant_builder_add(builder, "{sv}", "CMD", g_variant_new_string("START"));
455
456         param = g_variant_ref_sink(g_variant_new("(@a{sv})", g_variant_builder_end(builder)));
457
458         g_variant_unref(param);
459 #endif
460
461         return GEOFENCE_CLIENT_ERROR_NONE;
462 }
463
464 static void __geo_client_signal_unsubcribe(geofence_client_dbus_h geofence_client)
465 {
466         GEOFENCE_CLIENT_LOGD("ENTER >>>");
467
468         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
469         if (handle == NULL) {
470                 GEOFENCE_CLIENT_LOGE("Invalid handle");
471                 return;
472         }
473         if (handle->conn == NULL) {
474                 GEOFENCE_CLIENT_LOGE("Invalid dbus_connection");
475                 return;
476         }
477         if (handle->geofence_evt_id) {
478                 g_dbus_connection_signal_unsubscribe(handle->conn, handle->geofence_evt_id);
479                 handle->geofence_evt_id = 0;
480         }
481         if (handle->geofence_evt_status_id) {
482                 g_dbus_connection_signal_unsubscribe(handle->conn, handle->geofence_evt_status_id);
483                 handle->geofence_evt_status_id = 0;
484         }
485 }
486
487 EXPORT_API int geo_client_stop(geofence_client_dbus_h geofence_client)
488 {
489         GEOFENCE_CLIENT_LOGD("ENTER >>>");
490
491         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;;
492         g_return_val_if_fail(handle, GEOFENCE_CLIENT_ERROR_PARAMETER);
493
494         __geo_client_signal_unsubcribe(handle);
495
496 #if SUPPORT_MULTI_CLIENT
497         GVariant *param = NULL;
498         GVariantBuilder *builder = NULL;
499
500         /* Stop*/
501         GEOFENCE_CLIENT_LOGD("STOP: CMD-STOP");
502         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
503         g_variant_builder_add(builder, "{sv}", "CMD", g_variant_new_string("STOP"));
504         param = g_variant_ref_sink(g_variant_new("(@a{sv})", g_variant_builder_end(builder)));
505
506         g_variant_unref(param);
507
508 #endif
509
510         return GEOFENCE_CLIENT_ERROR_NONE;
511 }
512
513 static int __geofence_client_create_connection(geofence_client_dbus_s *client)
514 {
515         GEOFENCE_CLIENT_LOGD("ENTER >>>");
516
517         g_return_val_if_fail(client, GEOFENCE_CLIENT_ERROR_PARAMETER);
518         GError *error = NULL;
519
520 #if 0
521         client->conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
522 #endif
523
524         char *bus_addr = NULL;
525         bus_addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
526         if (!bus_addr) {
527                 GEOFENCE_CLIENT_LOGD("Fail to get addr of bus.");
528                 return GEOFENCE_CLIENT_ERROR_CONNECTION;
529         }
530
531         GEOFENCE_CLIENT_LOGD("bus_addr: %s", bus_addr);
532
533         client->conn = g_dbus_connection_new_for_address_sync(bus_addr,
534                                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
535                                                               G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
536                                                               NULL, NULL, &error);
537         g_free(bus_addr);
538
539         if (!client->conn) {
540                 if (error && error->message) {
541                         GEOFENCE_CLIENT_LOGD("Fail to get GBus. ErrCode[%d], Msg[%s]", error->code, error->message);
542                         g_error_free(error);
543                         error = NULL;
544                 }
545                 return GEOFENCE_CLIENT_ERROR_CONNECTION;
546         }
547         GEOFENCE_CLIENT_LOGD("client->conn: %p", client->conn);
548
549         return GEOFENCE_CLIENT_ERROR_NONE;
550 }
551
552 static void __glib_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
553 {
554         geofence_client_dbus_s *client = (geofence_client_dbus_s *)user_data;
555         if (client != NULL) {
556                 GEOFENCE_CLIENT_LOGD("client->conn: %p", client->conn);
557         }
558         GEOFENCE_CLIENT_LOGE("GLIB[%d]: %s", log_level, msg);
559 }
560
561 /* The reason why we seperate this from start is to support IPC for db operation between a server and a client.*/
562 EXPORT_API int geo_client_create(geofence_client_dbus_h *geofence_client)
563 {
564         GEOFENCE_CLIENT_LOGD("ENTER >>>");
565
566         int ret = GEOFENCE_CLIENT_ERROR_NONE;
567         geofence_client_dbus_s *client = g_new0(geofence_client_dbus_s, 1);
568         g_return_val_if_fail(client, GEOFENCE_CLIENT_ERROR_MEMORY);
569         g_log_set_default_handler(__glib_log, client);
570
571         ret = __geofence_client_create_connection(client);
572         if (ret != GEOFENCE_CLIENT_ERROR_NONE) {
573                 g_free(client);
574                 return ret;
575         }
576         *geofence_client = (geofence_client_dbus_s *) client;
577
578         return GEOFENCE_CLIENT_ERROR_NONE;
579 }
580
581 EXPORT_API int geo_client_destroy(geofence_client_dbus_h geofence_client)
582 {
583         GEOFENCE_CLIENT_LOGD("ENTER >>>");
584         g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);
585
586         geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
587
588         if (handle->conn) {
589                 g_object_unref(handle->conn);
590                 handle->conn = NULL;
591         }
592         g_free(handle->service_path);
593         g_free(handle->service_name);
594         g_free(handle->signal_path);
595         g_free(handle);
596
597         return GEOFENCE_CLIENT_ERROR_NONE;
598 }