summaryrefslogtreecommitdiff
path: root/geofence/src/geofence_client.c
blob: 0e25521f7917835a06a63e5d748396a451b7805b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "geofence_client.h"
#include "geofence_client_priv.h"

#define GEOFENCE_SERVICE_NAME	"org.tizen.lbs.Providers.GeofenceServer"
#define GEOFENCE_SERVICE_PATH	"/org/tizen/lbs/Providers/GeofenceServer"
#define GEOFENCE_INTERFACE_NAME "org.tizen.lbs.Geofence"


typedef struct _geofence_client_dbus_s {
	GDBusConnection *conn;
	gchar *service_name;
	gchar *service_path;
	gchar *signal_path;
	int geofence_evt_id;
	int geofence_evt_status_id;
	geofence_client_cb user_cb;
	void *user_data;
} geofence_client_dbus_s;

static void __geofence_signal_callback(GDBusConnection *conn, const gchar *name, const gchar *path, const gchar *interface, const gchar *sig, GVariant *param, gpointer user_data)
{
	GEOFENCE_CLIENT_SECLOG("name: %s, path: %s, interface: %s, sig: %s, handle[%p]", name, path, interface, sig, user_data);
	geofence_client_dbus_s *handle = (geofence_client_dbus_s *) user_data;
	if (handle == NULL) {
		GEOFENCE_CLIENT_LOGD("Invalid handle");
		return;
	}
	if (handle->user_cb)
		handle->user_cb(sig, param, handle->user_data);
}

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)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GVariant *reg = NULL;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int fence_id = -1;

	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);
	if (proxy) {
		GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);

		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);
		if (reg) {
			g_variant_get(reg, "(i)", &fence_id);
			g_variant_unref(reg);
			reg = NULL;
		} else {
			if (error) {
				GEOFENCE_CLIENT_LOGE("Fail to add geofence Error[%s]", error->message);
				g_error_free(error);
			}
		}
		g_object_unref(proxy);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
		}

	}
	GEOFENCE_CLIENT_LOGD("fence_id: %d", fence_id);

	return fence_id;
}

EXPORT_API int geo_client_delete_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint fence_id)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *) geofence_client;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;

	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);
	if (proxy) {
		g_dbus_proxy_call(proxy, "DeleteGeofence", g_variant_new("(is)", fence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

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)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;

	GVariant *reg = NULL;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;
	GVariantIter *iterator;
	int new_error_code = 0;
	int new_fence_cnt = 0;

	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);
	if (proxy) {
		GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);

		reg = g_dbus_proxy_call_sync(proxy, "GetGeofences", g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
		if (reg) {
			g_variant_get(reg, "(iiaa{sv})", &new_fence_cnt, &new_error_code, &iterator);
			*error_code = new_error_code;
			*fence_cnt = new_fence_cnt;
			if (iterator == NULL)
				GEOFENCE_CLIENT_LOGE("Iterator is null");
			*iter = iterator;
			g_variant_unref(reg);
		} else {
			if (error) {
				GEOFENCE_CLIENT_LOGE("Fail to get the list Error[%s]", error->message);
				g_error_free(error);
				ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
			}
		}
		g_object_unref(proxy);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_enable_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id, gboolean onoff)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;

	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);
	if (proxy) {
		g_dbus_proxy_call(proxy, "EnableGeofence", g_variant_new("(isb)", geofence_id, app_id, onoff),	G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_start_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;

	GEOFENCE_CLIENT_LOGD("handle->conn: %p, geofence_id", handle->conn, geofence_id);

	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);
	if (proxy) {
		g_dbus_proxy_call(proxy, "StartGeofence", g_variant_new("(is)", geofence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}
	return ret;
}

EXPORT_API int geo_client_stop_geofence(geofence_client_dbus_h geofence_client, gchar *app_id, gint geofence_id)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;

	GEOFENCE_CLIENT_LOGD("handle->conn: %p", handle->conn);

	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);
	if (proxy) {
		g_dbus_proxy_call(proxy, "StopGeofence", g_variant_new("(is)", geofence_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_add_place(geofence_client_dbus_h geofence_client, gchar *app_id, const gchar *place_name)
{
	/* add fence interface between App & geofence-server */
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GVariant *reg = NULL;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int place_id = -1;

	GEOFENCE_CLIENT_LOGI("APP ID: %s", app_id);

	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);
	if (proxy) {
		GEOFENCE_CLIENT_LOGD("proxy: %p", proxy);
		reg = g_dbus_proxy_call_sync(proxy, "AddPlace", g_variant_new("(ss)", app_id, place_name), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
		if (reg) {
			g_variant_get(reg, "(i)", &place_id);
			g_variant_unref(reg);
			reg = NULL;
		} else {
			if (error) {
				GEOFENCE_CLIENT_LOGE("Fail to add place Error[%s]", error->message);
				g_error_free(error);
			}
		}
		g_object_unref(proxy);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
		}
	}

	GEOFENCE_CLIENT_LOGD("place_id: %d", place_id);

	return place_id;
}

EXPORT_API int geo_client_update_place(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id, const gchar *place_name)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;

	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);
	if (proxy) {
		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);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_delete_place(geofence_client_dbus_h geofence_client, gchar *app_id, gint place_id)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;

	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);
	if (proxy) {
		g_dbus_proxy_call(proxy, "DeletePlace",	g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

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)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GVariant *reg = NULL;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;
	int ret = GEOFENCE_CLIENT_ERROR_NONE;
	char *new_place_name = NULL;
	int new_error_code = 0;

	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);
	if (proxy) {
		reg = g_dbus_proxy_call_sync(proxy, "GetPlaceName", g_variant_new("(is)", place_id, app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
		if (reg) {
			g_variant_get(reg, "(is)", &new_error_code, &new_place_name);
			*error_code = new_error_code;
			*place_name = g_strdup(new_place_name);
			g_free(new_place_name);
			g_variant_unref(reg);
		} else {
			if (error) {
				GEOFENCE_CLIENT_LOGE("Fail to get the place name Error[%s]", error->message);
				g_error_free(error);
				ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
			}
		}
		g_object_unref(proxy);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_get_places(geofence_client_dbus_h geofence_client, gchar *app_id, GVariantIter **iter, gint *place_cnt, gint *error_code)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	GVariant *reg = NULL;
	GError *error = NULL;
	GDBusProxy *proxy = NULL;

	int ret = GEOFENCE_CLIENT_ERROR_NONE;
	GVariantIter *iterator;
	int new_error_code = 0;
	int new_place_cnt = 0;

	GEOFENCE_CLIENT_LOGD("handle->conn: %p", handle->conn);
	GEOFENCE_CLIENT_LOGI("APP ID: %s", app_id);

	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);
	if (proxy) {
		reg = g_dbus_proxy_call_sync(proxy, "GetPlaces", g_variant_new("(s)", app_id), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
		if (reg) {
			g_variant_get(reg, "(iiaa{sv})", &new_place_cnt, &new_error_code, &iterator);
			*place_cnt = new_place_cnt;
			*error_code = new_error_code;
			if (iterator == NULL)
				GEOFENCE_CLIENT_LOGE("Iterator is null");
			*iter = iterator;
			g_variant_unref(reg);
		} else {
			if (error) {
				GEOFENCE_CLIENT_LOGE("Fail to get the place list Error[%s]", error->message);
				g_error_free(error);
				ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
			}
		}
		g_object_unref(proxy);
	} else {
		if (error) {
			GEOFENCE_CLIENT_LOGE("Fail to get proxy Error[%s]", error->message);
			g_error_free(error);
			ret = GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}

	return ret;
}

EXPORT_API int geo_client_start(geofence_client_dbus_h geofence_client, geofence_client_cb callback, void *user_data)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	gchar *signal_path = NULL;

	handle->service_name = g_strdup(GEOFENCE_SERVICE_NAME);
	handle->service_path = g_strdup(GEOFENCE_SERVICE_PATH);
	handle->signal_path = g_strdup_printf("%s/%s", handle->service_path, "SAMSUNG");
	GEOFENCE_CLIENT_LOGD("Object Path [%s]", handle->signal_path);

	if (callback) {
		handle->user_cb = callback;
		handle->user_data = user_data;
		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);

		if (handle->geofence_evt_id) {
			GEOFENCE_CLIENT_LOGD("Listening GeofenceInout");
		} else {
			GEOFENCE_CLIENT_LOGD("Fail to listen GeofenceInout");
		}

		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);

		if (handle->geofence_evt_status_id) {
			GEOFENCE_CLIENT_LOGD("Listening Geofence event");
		} else {
			GEOFENCE_CLIENT_LOGD("Fail to listen Geofence event");
			return GEOFENCE_CLIENT_ERROR_DBUS_CALL;
		}
	}
	g_free(signal_path);


#if SUPPORT_MULTI_CLIENT
	GVariant *param = NULL;
	GVariantBuilder *builder = NULL;


	GEOFENCE_CLIENT_LOGD("START: CMD-START");
	builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
	g_variant_builder_add(builder, "{sv}", "CMD", g_variant_new_string("START"));

	param = g_variant_ref_sink(g_variant_new("(@a{sv})", g_variant_builder_end(builder)));

	g_variant_unref(param);
#endif

	return GEOFENCE_CLIENT_ERROR_NONE;
}

static void __geo_client_signal_unsubcribe(geofence_client_dbus_h geofence_client)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;
	if (handle == NULL) {
		GEOFENCE_CLIENT_LOGE("Invalid handle");
		return;
	}
	if (handle->conn == NULL) {
		GEOFENCE_CLIENT_LOGE("Invalid dbus_connection");
		return;
	}
	if (handle->geofence_evt_id) {
		g_dbus_connection_signal_unsubscribe(handle->conn, handle->geofence_evt_id);
		handle->geofence_evt_id = 0;
	}
	if (handle->geofence_evt_status_id) {
		g_dbus_connection_signal_unsubscribe(handle->conn, handle->geofence_evt_status_id);
		handle->geofence_evt_status_id = 0;
	}
}

EXPORT_API int geo_client_stop(geofence_client_dbus_h geofence_client)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;;
	g_return_val_if_fail(handle, GEOFENCE_CLIENT_ERROR_PARAMETER);

	__geo_client_signal_unsubcribe(handle);

#if SUPPORT_MULTI_CLIENT
	GVariant *param = NULL;
	GVariantBuilder *builder = NULL;

	/* Stop*/
	GEOFENCE_CLIENT_LOGD("STOP: CMD-STOP");
	builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
	g_variant_builder_add(builder, "{sv}", "CMD", g_variant_new_string("STOP"));
	param = g_variant_ref_sink(g_variant_new("(@a{sv})", g_variant_builder_end(builder)));

	g_variant_unref(param);

#endif

	return GEOFENCE_CLIENT_ERROR_NONE;
}

static int __geofence_client_create_connection(geofence_client_dbus_s *client)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");

	g_return_val_if_fail(client, GEOFENCE_CLIENT_ERROR_PARAMETER);
	GError *error = NULL;

	char *bus_addr = NULL;
	bus_addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, NULL, &error);
	if (!bus_addr) {
		GEOFENCE_CLIENT_LOGD("Fail to get addr of bus.");
		return GEOFENCE_CLIENT_ERROR_CONNECTION;
	}

	GEOFENCE_CLIENT_LOGD("bus_addr: %s", bus_addr);

	client->conn = g_dbus_connection_new_for_address_sync(bus_addr,
	                                                      G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
	                                                      G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
	                                                      NULL, NULL, &error);
	g_free(bus_addr);

	if (!client->conn) {
		if (error && error->message) {
			GEOFENCE_CLIENT_LOGD("Fail to get GBus. ErrCode[%d], Msg[%s]", error->code, error->message);
			g_error_free(error);
			error = NULL;
		}
		return GEOFENCE_CLIENT_ERROR_CONNECTION;
	}
	GEOFENCE_CLIENT_LOGD("client->conn: %p", client->conn);

	return GEOFENCE_CLIENT_ERROR_NONE;
}

static void __glib_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
{
	geofence_client_dbus_s *client = (geofence_client_dbus_s *)user_data;
	if (client != NULL) {
		GEOFENCE_CLIENT_LOGD("client->conn: %p", client->conn);
	}
	GEOFENCE_CLIENT_LOGE("GLIB[%d]: %s", log_level, msg);
}

/* The reason why we seperate this from start is to support IPC for db operation between a server and a client.*/
EXPORT_API int geo_client_create(geofence_client_dbus_h *geofence_client)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");

	int ret = GEOFENCE_CLIENT_ERROR_NONE;
	geofence_client_dbus_s *client = g_new0(geofence_client_dbus_s, 1);
	g_return_val_if_fail(client, GEOFENCE_CLIENT_ERROR_MEMORY);
	g_log_set_default_handler(__glib_log, client);

	ret = __geofence_client_create_connection(client);
	if (ret != GEOFENCE_CLIENT_ERROR_NONE) {
		g_free(client);
		return ret;
	}
	*geofence_client = (geofence_client_dbus_s *) client;

	return GEOFENCE_CLIENT_ERROR_NONE;
}

EXPORT_API int geo_client_destroy(geofence_client_dbus_h geofence_client)
{
	GEOFENCE_CLIENT_LOGD("ENTER >>>");
	g_return_val_if_fail(geofence_client, GEOFENCE_CLIENT_ERROR_PARAMETER);

	geofence_client_dbus_s *handle = (geofence_client_dbus_s *)geofence_client;

	if (handle->conn) {
		g_object_unref(handle->conn);
		handle->conn = NULL;
	}
	g_free(handle->service_path);
	g_free(handle->service_name);
	g_free(handle->signal_path);
	g_free(handle);

	return GEOFENCE_CLIENT_ERROR_NONE;
}