Modify the bt-service label from system to user
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <stdlib.h>
23 #include <sys/socket.h>
24
25
26 #include <gio/gunixfdlist.h>
27
28
29 #include "bluetooth-api.h"
30 #include "bluetooth-audio-api.h"
31 #include "bluetooth-hid-api.h"
32 #include "bluetooth-media-control.h"
33 #include "bt-internal-types.h"
34 #include "bluetooth-ipsp-api.h"
35
36 #include "bt-common.h"
37 #include "bt-request-sender.h"
38 #include "bt-event-handler.h"
39
40 #ifdef TIZEN_DPM_ENABLE
41 #include "bt-dpm.h"
42 #endif
43
44
45 static bt_user_info_t user_info[BT_MAX_USER_INFO];
46 static GDBusConnection *system_gdbus_conn = NULL;
47
48
49 static guint bus_id;
50
51 static GDBusConnection *system_gconn = NULL;
52 static GDBusConnection *session_gconn = NULL;
53
54
55 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
56
57 GDBusConnection *g_bus_get_private_conn(void)
58 {
59         GError *error = NULL;
60         char *address;
61         GDBusConnection *private_gconn = NULL;
62
63         address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
64         if (address == NULL) {
65                 if (error) {
66                         BT_ERR("Failed to get bus address: %s", error->message);
67                         g_clear_error(&error);
68                 }
69                 return NULL;
70         }
71
72         private_gconn = g_dbus_connection_new_for_address_sync(address,
73                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
74                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
75                                 NULL, /* GDBusAuthObserver */
76                                 NULL,
77                                 &error);
78         if (!private_gconn) {
79                 if (error) {
80                         BT_ERR("Unable to connect to dbus: %s", error->message);
81                         g_clear_error(&error);
82                 }
83                 return NULL;
84         }
85
86         return private_gconn;
87 }
88
89 GDBusConnection *g_bus_get_private_session_conn(void)
90 {
91         GError *error = NULL;
92         char *address;
93         GDBusConnection *private_gconn = NULL;
94
95         address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, NULL, &error);
96         if (address == NULL) {
97                 if (error) {
98                         BT_ERR("Failed to get bus address: %s", error->message);
99                         g_clear_error(&error);
100                 }
101                 return NULL;
102         }
103
104         private_gconn = g_dbus_connection_new_for_address_sync(address,
105                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
106                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
107                                 NULL, /* GDBusAuthObserver */
108                                 NULL,
109                                 &error);
110         if (!private_gconn) {
111                 if (error) {
112                         BT_ERR("Unable to connect to dbus: %s", error->message);
113                         g_clear_error(&error);
114                 }
115                 return NULL;
116         }
117
118         return private_gconn;
119 }
120
121 GDBusConnection *_bt_gdbus_init_system_gconn(void)
122 {
123         dbus_threads_init_default();
124
125         if (system_gconn != NULL)
126                 return system_gconn;
127
128         system_gconn = g_bus_get_private_conn();
129
130         return system_gconn;
131 }
132
133 GDBusConnection *_bt_gdbus_init_session_gconn(void)
134 {
135         dbus_threads_init_default();
136
137         if (session_gconn != NULL)
138                 return session_gconn;
139
140         session_gconn = g_bus_get_private_session_conn();
141
142         return session_gconn;
143 }
144
145 GDBusConnection *_bt_gdbus_get_system_gconn(void)
146 {
147
148
149         if (system_gconn == NULL) {
150                 system_gconn = _bt_gdbus_init_system_gconn();
151         } else if (g_dbus_connection_is_closed(system_gconn)) {
152                 system_gconn = g_bus_get_private_conn();
153         }
154
155         return system_gconn;
156 }
157
158 GDBusConnection *_bt_gdbus_get_session_gconn(void)
159 {
160
161
162         if (session_gconn == NULL) {
163                 session_gconn = _bt_gdbus_init_session_gconn();
164         } else if (g_dbus_connection_is_closed(session_gconn)) {
165                 session_gconn = g_bus_get_private_session_conn();
166         }
167
168         return session_gconn;
169 }
170
171
172 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
173 {
174         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
175                                 addr->addr[3], addr->addr[4], addr->addr[5]);
176 }
177
178 void _bt_set_user_data(int type, void *callback, void *user_data)
179 {
180         user_info[type].cb = callback;
181         user_info[type].user_data = user_data;
182 }
183
184 bt_user_info_t *_bt_get_user_data(int type)
185 {
186         return &user_info[type];
187 }
188
189 void _bt_common_event_cb(int event, int result, void *param,
190                                         void *callback, void *user_data)
191 {
192         bluetooth_event_param_t bt_event = { 0, };
193         bt_event.event = event;
194         bt_event.result = result;
195         bt_event.param_data = param;
196
197         if (callback)
198                 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
199                                         user_data);
200 }
201
202 void _bt_input_event_cb(int event, int result, void *param,
203                                         void *callback, void *user_data)
204 {
205         hid_event_param_t bt_event = { 0, };
206         bt_event.event = event;
207         bt_event.result = result;
208         bt_event.param_data = param;
209
210         if (callback)
211                 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
212                                         user_data);
213 }
214
215 void _bt_headset_event_cb(int event, int result, void *param,
216                                         void *callback, void *user_data)
217 {
218         bt_audio_event_param_t bt_event = { 0, };
219         bt_event.event = event;
220         bt_event.result = result;
221         bt_event.param_data = param;
222
223         if (callback)
224                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
225                                         user_data);
226 }
227
228 void _bt_a2dp_source_event_cb(int event, int result, void *param,
229                                         void *callback, void *user_data)
230 {
231         bt_audio_event_param_t bt_event = { 0, };
232         bt_event.event = event;
233         bt_event.result = result;
234         bt_event.param_data = param;
235         if (callback)
236                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
237                                         user_data);
238 }
239
240 void _bt_hf_event_cb(int event, int result, void *param,
241                                         void *callback, void *user_data)
242 {
243         bt_hf_event_param_t bt_event = { 0, };
244         bt_event.event = event;
245         bt_event.result = result;
246         bt_event.param_data = param;
247
248         if (callback)
249                 ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
250                                         user_data);
251 }
252
253
254 void _bt_avrcp_event_cb(int event, int result, void *param,
255                                         void *callback, void *user_data)
256 {
257         media_event_param_t bt_event = { 0, };
258         bt_event.event = event;
259         bt_event.result = result;
260         bt_event.param_data = param;
261
262         if (callback)
263                 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
264                                         user_data);
265 }
266
267 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
268                                 unsigned int cod)
269 {
270         ret_if(device_class == NULL);
271
272         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
273         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
274         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
275
276         if (cod & 0x002000) {
277                 device_class->service_class |=
278                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
279         }
280 }
281
282 void _bt_convert_addr_string_to_type(unsigned char *addr,
283                                         const char *address)
284 {
285         int i;
286         char *ptr = NULL;
287
288         ret_if(address == NULL);
289         ret_if(addr == NULL);
290
291         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
292                 addr[i] = strtol(address, &ptr, 16);
293                 if (ptr[0] != '\0') {
294                         if (ptr[0] != ':')
295                                 return;
296
297                         address = ptr + 1;
298                 }
299         }
300 }
301
302 void _bt_convert_addr_type_to_string(char *address,
303                                 unsigned char *addr)
304 {
305         ret_if(address == NULL);
306         ret_if(addr == NULL);
307
308         g_snprintf(address, BT_ADDRESS_STRING_SIZE,
309                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
310                         addr[0], addr[1], addr[2],
311                         addr[3], addr[4], addr[5]);
312 }
313
314 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
315 {
316         int i;
317         const char *p = src;
318         char *next;
319         int count;
320
321         if (dest == NULL || src == NULL)
322                 return BLUETOOTH_ERROR_INVALID_PARAM;
323
324         i = 0;
325         while (*p != '\0' && i < length) {
326                 next = g_utf8_next_char(p);
327                 count = next - p;
328
329                 while (count > 0 && ((i + count) < length)) {
330                         dest[i++] = *p;
331                         p++;
332                         count--;
333                 }
334                 p = next;
335         }
336         return BLUETOOTH_ERROR_NONE;
337 }
338
339 gboolean _bt_utf8_validate(char *name)
340 {
341         BT_DBG("+");
342         gunichar2 *u16;
343         glong items_written = 0;
344
345         if (FALSE == g_utf8_validate(name, -1, NULL))
346                 return FALSE;
347
348         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
349         if (u16 == NULL)
350                 return FALSE;
351
352         g_free(u16);
353
354         if (items_written != g_utf8_strlen(name, -1))
355                 return FALSE;
356
357         BT_DBG("-");
358         return TRUE;
359 }
360
361
362 static GDBusProxy *profile_gproxy;
363 static GDBusConnection *gconn;
364 static int latest_id = -1;
365 #define BT_RFCOMM_ID_MAX 245
366 static gboolean id_used[BT_RFCOMM_ID_MAX];
367 GDBusNodeInfo *new_conn_node;
368
369 static const gchar rfcomm_agent_xml[] =
370 "<node name='/'>"
371 " <interface name='org.bluez.Profile1'>"
372 "     <method name='NewConnection'>"
373 "          <arg type='o' name='object' direction='in'/>"
374 "          <arg type='h' name='fd' direction='in'/>"
375 "          <arg type='a{sv}' name='properties' direction='in'/>"
376 "     </method>"
377 "     <method name='RequestDisconnection'>"
378 "          <arg type='o' name='device' direction='in'/>"
379 "     </method>"
380 "  </interface>"
381 "</node>";
382
383 static void __new_connection_method(GDBusConnection *connection,
384                                             const gchar *sender,
385                                             const gchar *object_path,
386                                             const gchar *interface_name,
387                                             const gchar *method_name,
388                                             GVariant *parameters,
389                                             GDBusMethodInvocation *invocation,
390                                             gpointer user_data)
391 {
392         BT_DBG("method %s", method_name);
393         if (g_strcmp0(method_name, "NewConnection") == 0) {
394                 int index;
395                 GDBusMessage *msg;
396                 GUnixFDList *fd_list;
397                 GVariantBuilder *properties;
398                 char *obj_path;
399                 char addr[20];
400                 bluetooth_device_address_t  remote_addr1;
401                 bt_new_connection_cb cb = user_data;
402                 int fd;
403
404                 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
405                                                                 &properties);
406
407                 msg = g_dbus_method_invocation_get_message(invocation);
408                 fd_list = g_dbus_message_get_unix_fd_list(msg);
409                 if (fd_list == NULL) {
410                         GQuark quark = g_quark_from_string("rfcomm-app");
411                         GError *err = g_error_new(quark, 0, "No fd in message");
412                         g_dbus_method_invocation_return_gerror(invocation, err);
413                         g_error_free(err);
414                         return;
415                 }
416
417
418                 fd = g_unix_fd_list_get(fd_list, index, NULL);
419                 if (fd == -1) {
420                         BT_ERR("Invalid fd return");
421                         GQuark quark = g_quark_from_string("rfcomm-app");
422                         GError *err = g_error_new(quark, 0, "Invalid FD return");
423                         g_dbus_method_invocation_return_gerror(invocation, err);
424                         g_error_free(err);
425                         return;
426                 }
427                 BT_INFO("Object Path %s", obj_path);
428
429                 _bt_device_path_to_address(obj_path, addr);
430                 _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
431                 BT_INFO("fd: %d, address %s", fd, addr);
432
433                 g_dbus_method_invocation_return_value(invocation, NULL);
434
435                 if (cb)
436                         cb(object_path, fd, &remote_addr1);
437         } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
438                 g_dbus_method_invocation_return_value(invocation, NULL);
439         }
440 }
441
442
443 static const GDBusInterfaceVTable method_table = {
444         __new_connection_method,
445         NULL,
446         NULL,
447 };
448
449 void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
450 {
451         int i;
452
453         for (i = 0; i < 6; i++)
454                 dst[i] = src[5-i];
455 }
456
457 int __rfcomm_assign_id(void)
458 {
459         int index;
460
461         BT_DBG("latest_id: %d", latest_id);
462
463         index = latest_id + 1;
464
465         if (index >= BT_RFCOMM_ID_MAX)
466                 index = 0;
467
468         BT_DBG("index: %d", index);
469
470         while (id_used[index] == TRUE) {
471                 if (index == latest_id) {
472                         /* No available ID */
473                         BT_ERR("All request ID is used");
474                         return -1;
475                 }
476
477                 index++;
478
479                 if (index >= BT_RFCOMM_ID_MAX)
480                         index = 0;
481         }
482
483         latest_id = index;
484         id_used[index] = TRUE;
485
486         BT_DBG("Assigned Id: %d", latest_id);
487
488         return latest_id;
489 }
490
491 void __rfcomm_delete_id(int id)
492 {
493         ret_if(id >= BT_RFCOMM_ID_MAX);
494         ret_if(id < 0);
495
496         id_used[id] = FALSE;
497
498         /* Next server will use this ID */
499         latest_id = id - 1;
500 }
501
502 static GDBusConnection *__get_gdbus_connection()
503 {
504         if (gconn == NULL)
505                 gconn = g_bus_get_private_conn();
506
507         return gconn;
508 }
509
510 static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
511 {
512         GDBusConnection *gconn;
513         GError *err = NULL;
514
515         if (profile_gproxy)
516                 return profile_gproxy;
517
518         gconn = __get_gdbus_connection();
519         if (gconn == NULL)
520                 return NULL;
521
522         profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
523                                                 NULL, BT_BLUEZ_NAME,
524                                                 "/org/bluez",
525                                                 "org.bluez.ProfileManager1",
526                                                 NULL, &err);
527         if (err) {
528                 BT_ERR("Unable to create proxy: %s", err->message);
529                 g_clear_error(&err);
530                 return NULL;
531         }
532
533         return profile_gproxy;
534 }
535
536 static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
537 {
538         GDBusConnection *gconn;
539         GError *err = NULL;
540         GDBusProxy *device_gproxy;
541
542         gconn = __get_gdbus_connection();
543         if (gconn == NULL)
544                 return NULL;
545
546         device_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
547                                                 NULL, BT_BLUEZ_NAME,
548                                                 object_path,
549                                                 BT_DEVICE_INTERFACE,
550                                                 NULL, &err);
551
552         if (err) {
553                 BT_ERR("Unable to create proxy: %s", err->message);
554                 g_clear_error(&err);
555                 return NULL;
556         }
557
558         return device_gproxy;
559 }
560
561 void _bt_unregister_gdbus(int object_id)
562 {
563         GDBusConnection *gconn;
564
565         gconn = __get_gdbus_connection();
566         if (gconn == NULL)
567                 return;
568
569         g_dbus_connection_unregister_object(gconn, object_id);
570 }
571
572 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
573 {
574         GDBusConnection *gconn;
575         int id;
576         GError *error = NULL;
577
578         gconn = __get_gdbus_connection();
579         if (gconn == NULL)
580                 return -1;
581
582         if (new_conn_node == NULL)
583                 new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
584
585         if (new_conn_node == NULL)
586                 return -1;
587
588         id = g_dbus_connection_register_object(gconn, path,
589                                                 new_conn_node->interfaces[0],
590                                                 &method_table,
591                                                 cb, NULL, &error);
592         if (id == 0) {
593                 BT_ERR("Failed to register: %s", error->message);
594                 g_error_free(error);
595                 return -1;
596         }
597
598         BT_DBG("NEW CONNECTION ID %d", id);
599
600         return id;
601 }
602
603 static GDBusProxy * __bt_gdbus_get_adapter_proxy()
604 {
605         GError *err = NULL;
606         GDBusProxy *manager_proxy = NULL;
607         GDBusProxy *adapter_proxy = NULL;
608         GDBusConnection *conn;
609         GVariant *result = NULL;
610         char *adapter_path = NULL;
611
612         conn = __get_gdbus_connection();
613         retv_if(conn == NULL, NULL);
614
615         manager_proxy =  g_dbus_proxy_new_sync(conn,
616                         G_DBUS_PROXY_FLAGS_NONE, NULL,
617                         BT_BLUEZ_NAME,
618                         BT_MANAGER_PATH,
619                         BT_MANAGER_INTERFACE,
620                         NULL, &err);
621
622         if (!manager_proxy) {
623                 BT_ERR("Unable to create proxy: %s", err->message);
624                 goto fail;
625         }
626
627         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
628                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
629         if (!result) {
630                 if (err != NULL)
631                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
632                 else
633                         BT_ERR("Fail to get DefaultAdapter");
634
635                 goto fail;
636         }
637
638         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
639                 BT_ERR("Incorrect result\n");
640                 goto fail;
641         }
642
643         g_variant_get(result, "(&o)", &adapter_path);
644
645         if (adapter_path == NULL ||
646                 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
647                 BT_ERR("Adapter path is inproper\n");
648                 goto fail;
649         }
650
651         BT_INFO("Adapter Path %s", adapter_path);
652
653         adapter_proxy = g_dbus_proxy_new_sync(conn,
654                                         G_DBUS_PROXY_FLAGS_NONE, NULL,
655                                         BT_BLUEZ_NAME,
656                                         adapter_path,
657                                         BT_ADAPTER_INTERFACE,
658                                         NULL, &err);
659         if (err) {
660                 BT_ERR("DBus Error message: [%s]", err->message);
661                 g_clear_error(&err);
662         }
663
664 fail:
665         if (manager_proxy)
666                 g_object_unref(manager_proxy);
667         if (result)
668                 g_variant_unref(result);
669         return adapter_proxy;
670 }
671
672 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb)
673 {
674         GDBusConnection *gconn;
675         int id;
676         GError *error = NULL;
677
678         gconn = __get_gdbus_connection();
679         if (gconn == NULL)
680                 return -1;
681
682         if (new_conn_node == NULL)
683                 new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
684
685         if (new_conn_node == NULL)
686                 return -1;
687
688         id = g_dbus_connection_register_object(gconn, path,
689                                                 new_conn_node->interfaces[0],
690                                                 &method_table,
691                                                 cb, NULL, &error);
692         if (id == 0) {
693                 BT_ERR("Failed to register: %s", error->message);
694                 g_error_free(error);
695                 return -1;
696         }
697
698         BT_DBG("NEW CONNECTION ID %d", id);
699
700         return id;
701 }
702
703 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
704 {
705         GVariantBuilder *option_builder;
706         GVariant *ret;
707         GDBusProxy *proxy;
708         GError *err = NULL;
709         int result = BLUETOOTH_ERROR_NONE;
710
711         proxy = __bt_gdbus_get_profile_proxy();
712         if (proxy == NULL) {
713                 BT_ERR("Getting profile proxy failed");
714                 return BLUETOOTH_ERROR_INTERNAL;
715         }
716
717         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
718         if (info->authentication)
719                 g_variant_builder_add(option_builder, "{sv}",
720                                                 "RequireAuthentication",
721                                                 g_variant_new_boolean(TRUE));
722         if (info->authorization)
723                 g_variant_builder_add(option_builder, "{sv}",
724                                                 "RequireAuthorization",
725                                                 g_variant_new_boolean(TRUE));
726         if (info->role)
727                 g_variant_builder_add(option_builder, "{sv}",
728                                                 "Role",
729                                                 g_variant_new_string(info->role));
730
731         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
732          * RFCOMM channels based on the availability when two services want
733          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
734          * two services use the same SPP RFCOMM channel. */
735         if (use_default_rfcomm)
736                 g_variant_builder_add(option_builder, "{sv}",
737                                                 "Channel",
738                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
739         if (info->service)
740                 g_variant_builder_add(option_builder, "{sv}",
741                                                 "Service",
742                                                 g_variant_new_string(info->service));
743
744         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
745                                         g_variant_new("(osa{sv})", info->obj_path,
746                                                                 info->uuid,
747                                                                 option_builder),
748                                         G_DBUS_CALL_FLAGS_NONE, -1,
749                                         NULL, &err);
750         if (err) {
751                 g_dbus_error_strip_remote_error(err);
752                 BT_ERR("RegisterProfile failed: %s", err->message);
753
754                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
755                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
756                 else
757                         result = BLUETOOTH_ERROR_INTERNAL;
758
759                 g_clear_error(&err);
760         }
761
762         g_variant_builder_unref(option_builder);
763
764         if (ret)
765                 g_variant_unref(ret);
766
767         return result;
768 }
769
770 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path)
771 {
772         GVariantBuilder *option_builder;
773         GVariant *ret;
774         GDBusProxy *proxy;
775         GError *err = NULL;
776         int result = BLUETOOTH_ERROR_NONE;
777
778         proxy = __bt_gdbus_get_profile_proxy();
779         if (proxy == NULL) {
780                 BT_ERR("Getting profile proxy failed");
781                 return BLUETOOTH_ERROR_INTERNAL;
782         }
783
784         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
785         if (info->authentication)
786                 g_variant_builder_add(option_builder, "{sv}",
787                                                 "RequireAuthentication",
788                                                 g_variant_new_boolean(TRUE));
789         if (info->authorization)
790                 g_variant_builder_add(option_builder, "{sv}",
791                                                 "RequireAuthorization",
792                                                 g_variant_new_boolean(TRUE));
793         if (info->role)
794                 g_variant_builder_add(option_builder, "{sv}",
795                                                 "Role",
796                                                 g_variant_new_string(info->role));
797
798         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
799          * RFCOMM channels based on the availability when two services want
800          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
801          * two services use the same SPP RFCOMM channel. */
802         if (use_default_rfcomm)
803                 g_variant_builder_add(option_builder, "{sv}",
804                                                 "Channel",
805                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
806         if (info->service)
807                 g_variant_builder_add(option_builder, "{sv}",
808                                                 "Service",
809                                                 g_variant_new_string(info->service));
810
811         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile2",
812                                         g_variant_new("(osssa{sv})", info->obj_path,
813                                                                 info->uuid,
814                                                                 name,
815                                                                 path,
816                                                                 option_builder),
817                                         G_DBUS_CALL_FLAGS_NONE, -1,
818                                         NULL, &err);
819         if (err) {
820                 g_dbus_error_strip_remote_error(err);
821                 BT_ERR("RegisterProfile failed: %s", err->message);
822
823                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
824                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
825                 else
826                         result = BLUETOOTH_ERROR_INTERNAL;
827
828                 g_clear_error(&err);
829         }
830
831         g_variant_builder_unref(option_builder);
832
833         if (ret)
834                 g_variant_unref(ret);
835
836         return result;
837 }
838
839 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
840 {
841         GVariantBuilder *option_builder;
842         GVariant *ret;
843         GDBusProxy *proxy;
844         GError *err = NULL;
845         int result = BLUETOOTH_ERROR_NONE;
846
847         proxy = __bt_gdbus_get_profile_proxy();
848         if (proxy == NULL) {
849                 BT_ERR("Getting profile proxy failed");
850                 return BLUETOOTH_ERROR_INTERNAL;
851         }
852
853         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
854         if (info->authentication)
855                 g_variant_builder_add(option_builder, "{sv}",
856                                                 "RequireAuthentication",
857                                                 g_variant_new_boolean(TRUE));
858         if (info->authorization)
859                 g_variant_builder_add(option_builder, "{sv}",
860                                                 "RequireAuthorization",
861                                                 g_variant_new_boolean(TRUE));
862         if (info->role)
863                 g_variant_builder_add(option_builder, "{sv}",
864                                                 "Role",
865                                                 g_variant_new_string(info->role));
866
867         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
868          * RFCOMM channels based on the availability when two services want
869          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
870          * two services use the same SPP RFCOMM channel. */
871         if (use_default_rfcomm)
872                 g_variant_builder_add(option_builder, "{sv}",
873                                                 "Channel",
874                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
875         if (info->service)
876                 g_variant_builder_add(option_builder, "{sv}",
877                                                 "Service",
878                                                 g_variant_new_string(info->service));
879
880         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
881                                         g_variant_new("(osa{sv})", info->obj_path,
882                                                                 info->uuid,
883                                                                 option_builder),
884                                         G_DBUS_CALL_FLAGS_NONE, -1,
885                                         NULL, &err);
886
887         if (err) {
888                 g_dbus_error_strip_remote_error(err);
889                 BT_ERR("RegisterProfile failed: %s", err->message);
890
891                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
892                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
893                 else
894                         result = BLUETOOTH_ERROR_INTERNAL;
895
896                 g_clear_error(&err);
897         }
898
899         g_variant_builder_unref(option_builder);
900
901         if (ret)
902                 g_variant_unref(ret);
903
904         return result;
905 }
906
907
908 void _bt_unregister_profile(char *path)
909 {
910         GVariant *ret;
911         GDBusProxy *proxy;
912         GError *err = NULL;
913
914         proxy = __bt_gdbus_get_profile_proxy();
915         if (proxy == NULL) {
916                 BT_ERR("Getting profile proxy failed");
917                 return;
918         }
919
920         ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
921                         g_variant_new("(o)", path),
922                         G_DBUS_CALL_FLAGS_NONE, -1,
923                         NULL, &err);
924         if (err) {
925                 BT_ERR("UnregisterProfile failed : %s", err->message);
926                 g_clear_error(&err);
927         }
928
929         if (ret)
930                 g_variant_unref(ret);
931
932         return;
933 }
934
935 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
936 {
937         if (bus_id == 0) {
938                 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
939
940                 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
941                                                 name,
942                                                 G_BUS_NAME_OWNER_FLAGS_NONE,
943                                                 NULL,
944                                                 NULL,
945                                                 NULL,
946                                                 NULL,
947                                                 NULL);
948                 BT_DBG("Got bus id %d", bus_id);
949                 g_free(name);
950         }
951
952         return g_dbus_node_info_new_for_xml(xml_data, NULL);
953 }
954
955 GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name)
956 {
957         if (bus_id == 0) {
958                 char *name = g_strdup(bus_name);
959                 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
960                                                 name,
961                                                 G_BUS_NAME_OWNER_FLAGS_NONE,
962                                                 NULL,
963                                                 NULL,
964                                                 NULL,
965                                                 NULL,
966                                                 NULL);
967                 BT_DBG("Got bus id %d", bus_id);
968                 g_free(name);
969         }
970
971         return g_dbus_node_info_new_for_xml(xml_data, NULL);
972 }
973
974 int _bt_connect_profile(char *address, char *uuid, void *cb,
975                                                         gpointer func_data)
976 {
977         GDBusProxy *proxy;
978         GDBusProxy *adapter_proxy;
979         char *object_path;
980         GError *err = NULL;
981
982         object_path = _bt_get_device_object_path(address);
983
984         if (object_path == NULL) {
985                 GVariant *ret = NULL;
986                 BT_ERR("No searched device");
987                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
988
989                 if (adapter_proxy == NULL) {
990                         BT_ERR("adapter proxy is NULL");
991                         return BLUETOOTH_ERROR_INTERNAL;
992                 }
993
994                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
995                                 g_variant_new("(s)", address),
996                                 G_DBUS_CALL_FLAGS_NONE,
997                                 DBUS_TIMEOUT, NULL,
998                                 &err);
999
1000                 if (err != NULL) {
1001                         BT_ERR("CreateDevice Failed: %s", err->message);
1002                         g_clear_error(&err);
1003                 }
1004                 if (ret)
1005                         g_variant_unref(ret);
1006                 g_object_unref(adapter_proxy);
1007                 object_path = _bt_get_device_object_path(address);
1008
1009                 if (object_path == NULL)
1010                         return BLUETOOTH_ERROR_INTERNAL;
1011         }
1012
1013         proxy = __bt_gdbus_get_device_proxy(object_path);
1014         g_free(object_path);
1015
1016         if (proxy == NULL) {
1017                 BT_ERR("Error while getting proxy");
1018                 return BLUETOOTH_ERROR_INTERNAL;
1019         }
1020
1021         g_dbus_proxy_call(proxy, "ConnectProfile",
1022                         g_variant_new("(s)", uuid),
1023                         G_DBUS_CALL_FLAGS_NONE,
1024                         DBUS_TIMEOUT, NULL,
1025                         (GAsyncReadyCallback)cb,
1026                         func_data);
1027         BT_DBG("-");
1028         return BLUETOOTH_ERROR_NONE;
1029 }
1030
1031 int _bt_discover_services(char *address, char *uuid, void *cb,
1032                 gpointer func_data)
1033 {
1034         char *object_path;
1035         GDBusProxy *proxy;
1036         GDBusProxy *adapter_proxy;
1037         GError *err = NULL;
1038         object_path = _bt_get_device_object_path(address);
1039         if (object_path == NULL) {
1040                 GVariant *ret = NULL;
1041                 BT_ERR("No searched device");
1042                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1043                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1044                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1045                                 g_variant_new("(s)", address),
1046                                 G_DBUS_CALL_FLAGS_NONE,
1047                                 DBUS_TIMEOUT, NULL,
1048                                 &err);
1049                 if (err != NULL) {
1050                         BT_ERR("CreateDevice Failed: %s", err->message);
1051                         g_clear_error(&err);
1052                 }
1053                 if (ret)
1054                         g_variant_unref(ret);
1055
1056                 g_object_unref(adapter_proxy);
1057
1058                 object_path = _bt_get_device_object_path(address);
1059                 if (object_path == NULL)
1060                         return BLUETOOTH_ERROR_INTERNAL;
1061         }
1062         proxy = __bt_gdbus_get_device_proxy(object_path);
1063         g_free(object_path);
1064         if (proxy == NULL) {
1065                 BT_ERR("Error while getting proxy");
1066                 return BLUETOOTH_ERROR_INTERNAL;
1067         }
1068         g_dbus_proxy_call(proxy, "DiscoverServices",
1069                         g_variant_new("(s)", uuid),
1070                         G_DBUS_CALL_FLAGS_NONE,
1071                         DBUS_TIMEOUT, NULL,
1072                         (GAsyncReadyCallback)cb,
1073                         func_data);
1074         BT_DBG("-");
1075         return BLUETOOTH_ERROR_NONE;
1076 }
1077
1078 int _bt_cancel_discovers(char *address)
1079 {
1080         char *object_path;
1081         GDBusProxy *proxy;
1082         GDBusProxy *adapter_proxy;
1083         GError *err = NULL;
1084         object_path = _bt_get_device_object_path(address);
1085         if (object_path == NULL) {
1086                 GVariant *ret = NULL;
1087                 BT_ERR("No searched device");
1088                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1089                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1090                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1091                                 g_variant_new("(s)", address),
1092                                 G_DBUS_CALL_FLAGS_NONE,
1093                                 DBUS_TIMEOUT, NULL,
1094                                 &err);
1095                 if (err != NULL) {
1096                         BT_ERR("CreateDevice Failed: %s", err->message);
1097                         g_clear_error(&err);
1098                 }
1099                 if (ret)
1100                         g_variant_unref(ret);
1101
1102                 g_object_unref(adapter_proxy);
1103
1104                 object_path = _bt_get_device_object_path(address);
1105                 if (object_path == NULL)
1106                         return BLUETOOTH_ERROR_INTERNAL;
1107         }
1108         proxy = __bt_gdbus_get_device_proxy(object_path);
1109         g_free(object_path);
1110         g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1111                 NULL,
1112                 G_DBUS_CALL_FLAGS_NONE,
1113                 DBUS_TIMEOUT, NULL,
1114                 &err);
1115         if (err) {
1116                 BT_ERR("DBus Error message: [%s]", err->message);
1117                 g_clear_error(&err);
1118                 return BLUETOOTH_ERROR_INTERNAL;
1119         }
1120         if (proxy)
1121                 g_object_unref(proxy);
1122         return BLUETOOTH_ERROR_NONE;
1123 }
1124
1125 int _bt_discover_service_uuids(char *address, char *remote_uuid)
1126 {
1127         char *object_path;
1128         GDBusProxy *proxy;
1129         GDBusConnection *gconn;
1130         GError *err = NULL;
1131         char **uuid_value = NULL;
1132         gsize size = 0;
1133         int i = 0;
1134         GVariant *value = NULL;
1135         GVariant *ret = NULL;
1136         int result = BLUETOOTH_ERROR_INTERNAL;
1137         BT_INFO("+");
1138         retv_if(remote_uuid == NULL, BLUETOOTH_ERROR_INTERNAL);
1139         gconn = __get_gdbus_connection();
1140         retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1141         object_path = _bt_get_device_object_path(address);
1142         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1143
1144         proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1145                                 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1146                                 &err);
1147         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1148         if (err) {
1149                 BT_ERR("DBus Error: [%s]", err->message);
1150                 g_clear_error(&err);
1151         }
1152         ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1153                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1154                         G_DBUS_CALL_FLAGS_NONE,
1155                         DBUS_TIMEOUT, NULL,
1156                         &err);
1157         if (err) {
1158                 result = BLUETOOTH_ERROR_INTERNAL;
1159                 BT_ERR("DBus Error : %s", err->message);
1160                 g_clear_error(&err);
1161                 goto done;
1162         }
1163         if (ret == NULL) {
1164                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1165                 result = BLUETOOTH_ERROR_INTERNAL;
1166                 goto done;
1167         }
1168
1169         g_variant_get(ret, "(@a{sv})", &value);
1170         g_variant_unref(ret);
1171         if (value) {
1172                 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1173                         G_VARIANT_TYPE_STRING_ARRAY);
1174
1175                 if (temp_value) {
1176                         size = g_variant_get_size(temp_value);
1177                         if (size > 0) {
1178                                 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1179                                 BT_DBG("Size items %d", size);
1180
1181                                 if (uuid_value) {
1182                                         for (i = 0; uuid_value[i] != NULL; i++) {
1183                                                 BT_DBG("Remote uuids %s", uuid_value[i]);
1184                                                 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
1185                                                         result = BLUETOOTH_ERROR_NONE;
1186                                                         g_variant_unref(temp_value);
1187                                                         goto done;
1188                                                 }
1189                                         }
1190                                 }
1191                         }
1192                         g_variant_unref(temp_value);
1193                 }
1194         }
1195 done:
1196         if (proxy)
1197                 g_object_unref(proxy);
1198         if (value)
1199                 g_variant_unref(value);
1200         if (uuid_value)
1201                 g_free(uuid_value);
1202
1203         BT_DBG("-");
1204         return result;
1205 }
1206
1207 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
1208 {
1209         char *object_path;
1210         GDBusProxy *proxy;
1211         GDBusConnection *gconn;
1212         GError *err = NULL;
1213         GVariant *value = NULL;
1214         GVariant *result = NULL;
1215         unsigned int  class = 0x00;
1216         int ret = BLUETOOTH_ERROR_INTERNAL;
1217
1218         gconn = __get_gdbus_connection();
1219         retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1220                 object_path = _bt_get_device_object_path(address);
1221
1222         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1223
1224         proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1225                                 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1226                                 &err);
1227         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1228         if (err) {
1229                 BT_ERR("DBus Error: [%s]", err->message);
1230                 g_clear_error(&err);
1231         }
1232
1233         result = g_dbus_proxy_call_sync(proxy, "GetAll",
1234                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1235                         G_DBUS_CALL_FLAGS_NONE,
1236                         DBUS_TIMEOUT, NULL,
1237                         &err);
1238         if (err) {
1239                 ret = BLUETOOTH_ERROR_INTERNAL;
1240                 BT_ERR("DBus Error : %s", err->message);
1241                 g_clear_error(&err);
1242                 goto done;
1243         }
1244         if (result == NULL) {
1245                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1246                 ret = BLUETOOTH_ERROR_INTERNAL;
1247                 goto done;
1248         }
1249         g_variant_get(result, "(@a{sv})", &value);
1250         g_variant_unref(result);
1251         if (value) {
1252                 GVariant *temp_value = g_variant_lookup_value(value, "Class",
1253                         G_VARIANT_TYPE_UINT32);
1254                 class = g_variant_get_uint32(temp_value);
1255                 _bt_divide_device_class(dev_class, class);
1256                 if (temp_value)
1257                         g_variant_unref(temp_value);
1258         }
1259
1260 done:
1261         if (proxy)
1262                 g_object_unref(proxy);
1263         if (value)
1264                 g_variant_unref(value);
1265
1266         BT_DBG("-");
1267         return ret;
1268 }
1269
1270 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
1271                                                         gpointer func_data)
1272 {
1273         GDBusProxy *proxy;
1274         char *object_path;
1275         GError *err = NULL;
1276         GDBusProxy *adapter_proxy;
1277         object_path = _bt_get_device_object_path(address);
1278         if (object_path == NULL) {
1279                 GVariant *ret = NULL;
1280                 BT_ERR("No searched device");
1281                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1282                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1283                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1284                                 g_variant_new("(s)", address),
1285                                 G_DBUS_CALL_FLAGS_NONE,
1286                                 DBUS_TIMEOUT, NULL,
1287                                 &err);
1288                 if (err != NULL) {
1289                         BT_ERR("CreateDevice Failed: %s", err->message);
1290                         g_error_free(err);
1291                 }
1292                 if (ret)
1293                         g_variant_unref(ret);
1294                 g_object_unref(adapter_proxy);
1295                 object_path = _bt_get_device_object_path(address);
1296                 if (object_path == NULL)
1297                         return BLUETOOTH_ERROR_INTERNAL;
1298         }
1299         proxy = __bt_gdbus_get_device_proxy(object_path);
1300         g_free(object_path);
1301         if (proxy == NULL) {
1302                 BT_ERR("Error while getting proxy");
1303                 return BLUETOOTH_ERROR_INTERNAL;
1304         }
1305         g_dbus_proxy_call(proxy, "DisconnectProfile",
1306                         g_variant_new("(s)", uuid),
1307                         G_DBUS_CALL_FLAGS_NONE,
1308                         DBUS_TIMEOUT, NULL,
1309                         (GAsyncReadyCallback)cb,
1310                         func_data);
1311         BT_DBG("-");
1312         return BLUETOOTH_ERROR_NONE;
1313 }
1314
1315 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
1316 {
1317         GError *err = NULL;
1318         GDBusProxy *manager_proxy = NULL;
1319         GVariant *result = NULL;
1320         char *adapter_path = NULL;
1321
1322         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1323
1324         manager_proxy =  g_dbus_proxy_new_sync(conn,
1325                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1326                         BT_BLUEZ_NAME,
1327                         BT_MANAGER_PATH,
1328                         BT_MANAGER_INTERFACE,
1329                         NULL, &err);
1330
1331         if (!manager_proxy) {
1332                 BT_ERR("Unable to create proxy: %s", err->message);
1333                 goto fail;
1334         }
1335
1336         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
1337                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1338         if (!result) {
1339                 if (err != NULL)
1340                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
1341                 else
1342                         BT_ERR("Fail to get DefaultAdapter");
1343
1344                 goto fail;
1345         }
1346
1347         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
1348                 BT_ERR("Incorrect result\n");
1349                 goto fail;
1350         }
1351
1352         g_variant_get(result, "(&o)", &adapter_path);
1353
1354         if (adapter_path == NULL ||
1355                 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
1356                 BT_ERR("Adapter path is inproper\n");
1357                 goto fail;
1358         }
1359
1360         if (path)
1361                 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
1362
1363         g_variant_unref(result);
1364         g_object_unref(manager_proxy);
1365
1366         return BLUETOOTH_ERROR_NONE;
1367
1368 fail:
1369         g_clear_error(&err);
1370
1371         if (result)
1372                 g_variant_unref(result);
1373
1374         if (manager_proxy)
1375                 g_object_unref(manager_proxy);
1376
1377         return BLUETOOTH_ERROR_INTERNAL;
1378
1379 }
1380
1381 void _bt_convert_device_path_to_address(const char *device_path,
1382                                                 char *device_address)
1383 {
1384         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1385         char *dev_addr;
1386
1387         ret_if(device_path == NULL);
1388         ret_if(device_address == NULL);
1389
1390         dev_addr = strstr(device_path, "dev_");
1391         if (dev_addr != NULL) {
1392                 char *pos = NULL;
1393                 dev_addr += 4;
1394                 g_strlcpy(address, dev_addr, sizeof(address));
1395
1396                 while ((pos = strchr(address, '_')) != NULL) {
1397                         *pos = ':';
1398                 }
1399
1400                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1401         }
1402 }
1403
1404 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
1405 {
1406         char *object_path = NULL;
1407         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1408         /* Parse the signature:  oa{sa{sv}}} */
1409         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
1410                         NULL)) {
1411                 retv_if(object_path == NULL, NULL);
1412                 _bt_convert_device_path_to_address(object_path, device_address);
1413
1414                 if (g_strcmp0(address, device_address) == 0) {
1415                         return g_strdup(object_path);
1416                 }
1417         }
1418         return NULL;
1419 }
1420
1421 char *_bt_get_device_object_path(char *address)
1422 {
1423         GError *err = NULL;
1424         GDBusProxy *proxy = NULL;
1425         GVariant *result = NULL;
1426         GVariantIter *iter = NULL;
1427         GDBusConnection *conn = NULL;
1428         char *object_path = NULL;
1429
1430         conn = _bt_gdbus_get_system_gconn();
1431         retv_if(conn == NULL, NULL);
1432
1433         proxy =  g_dbus_proxy_new_sync(conn,
1434                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1435                         BT_BLUEZ_NAME,
1436                         BT_MANAGER_PATH,
1437                         BT_MANAGER_INTERFACE,
1438                         NULL, &err);
1439
1440         if (!proxy) {
1441                 BT_ERR("Unable to create proxy: %s", err->message);
1442                 goto fail;
1443         }
1444
1445         result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
1446                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1447         if (!result) {
1448                 if (err != NULL)
1449                         BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
1450                 else
1451                         BT_ERR("Fail to get GetManagedObjects");
1452
1453                 goto fail;
1454         }
1455
1456         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1457         object_path = __bt_extract_device_path(iter, address);
1458
1459         g_variant_unref(result);
1460         g_object_unref(proxy);
1461         g_variant_iter_free(iter);
1462         return object_path;
1463
1464 fail:
1465         g_clear_error(&err);
1466
1467         if (proxy)
1468                 g_object_unref(proxy);
1469
1470         return object_path;
1471 }
1472
1473 void _bt_device_path_to_address(const char *device_path, char *device_address)
1474 {
1475         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1476         char *dev_addr = NULL;
1477
1478         if (!device_path || !device_address)
1479                 return;
1480
1481         dev_addr = strstr(device_path, "dev_");
1482         if (dev_addr != NULL) {
1483                 char *pos = NULL;
1484                 dev_addr += 4;
1485                 g_strlcpy(address, dev_addr, sizeof(address));
1486
1487                 while ((pos = strchr(address, '_')) != NULL) {
1488                         *pos = ':';
1489                 }
1490
1491                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1492         }
1493 }
1494
1495 GDBusConnection *_bt_init_system_gdbus_conn(void)
1496 {
1497         GError *error = NULL;
1498         if (system_gdbus_conn == NULL) {
1499                 system_gdbus_conn =
1500                 g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1501                 if (error) {
1502                         BT_ERR("GDBus connection Error : %s \n",
1503                                 error->message);
1504                         g_clear_error(&error);
1505                         return NULL;
1506                 }
1507         }
1508         return system_gdbus_conn;
1509 }
1510
1511
1512 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
1513 {
1514         int ret;
1515         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1516         char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1517
1518         BT_DBG("+");
1519         BT_INIT_PARAMS();
1520         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1521
1522         g_array_append_vals(in_param1, &type, sizeof(int));
1523         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1524         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1525         g_strlcpy(path_str, path, sizeof(path_str));
1526         g_array_append_vals(in_param3, &path_str, BLUETOOTH_PATH_STRING);
1527         g_array_append_vals(in_param4, &fd, sizeof(int));
1528
1529         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
1530                 in_param1, in_param2, in_param3, in_param4, &out_param);
1531
1532         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1533         BT_DBG("-");
1534         return ret;
1535 }
1536
1537 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
1538 {
1539         int ret;
1540         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1541
1542         BT_DBG("+");
1543         BT_INIT_PARAMS();
1544         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1545
1546         g_array_append_vals(in_param1, &type, sizeof(int));
1547         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1548         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1549
1550         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
1551                 in_param1, in_param2, in_param3, in_param4, &out_param);
1552
1553         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1554         BT_DBG("-");
1555         return ret;
1556 }
1557
1558 int _bt_check_privilege(int service_type, int service_function)
1559 {
1560         int result;
1561
1562         BT_INIT_PARAMS();
1563         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1564
1565         result = _bt_send_request(service_type, service_function,
1566                 in_param1, in_param2, in_param3, in_param4, &out_param);
1567
1568         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1569
1570         return result;
1571 }
1572
1573 GVariant *_bt_get_managed_objects(void)
1574 {
1575         GDBusConnection *g_conn;
1576         GDBusProxy *manager_proxy = NULL;
1577         GVariant *result = NULL;
1578         GError *error = NULL;
1579
1580         BT_DBG("+");
1581
1582         g_conn = _bt_gdbus_get_system_gconn();
1583         retv_if(g_conn == NULL, NULL);
1584
1585         manager_proxy = g_dbus_proxy_new_sync(g_conn,
1586                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1587                         BT_BLUEZ_NAME,
1588                         BT_MANAGER_PATH,
1589                         BT_MANAGER_INTERFACE,
1590                         NULL, &error);
1591
1592         if (error) {
1593                 BT_ERR("Unable to create proxy: %s", error->message);
1594                 g_clear_error(&error);
1595                 return NULL;
1596         }
1597
1598         result = g_dbus_proxy_call_sync(manager_proxy,
1599                         "GetManagedObjects", NULL,
1600                         G_DBUS_CALL_FLAGS_NONE, -1,
1601                         NULL, &error);
1602
1603         if (error) {
1604                 BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1605                 g_clear_error(&error);
1606         }
1607
1608         g_object_unref(manager_proxy);
1609
1610         BT_DBG("-");
1611         return result;
1612 }
1613
1614 BT_EXPORT_API int bluetooth_is_supported(void)
1615 {
1616         int is_supported = 0;
1617         int len = 0;
1618         int fd = -1;
1619         rfkill_event event;
1620
1621         fd = open(RFKILL_NODE, O_RDONLY);
1622         if (fd < 0) {
1623                 BT_ERR("Fail to open RFKILL node");
1624                 return BLUETOOTH_ERROR_INTERNAL;
1625         }
1626
1627         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
1628                 BT_ERR("Fail to set RFKILL node to non-blocking");
1629                 close(fd);
1630                 return BLUETOOTH_ERROR_INTERNAL;
1631         }
1632
1633         while (1) {
1634                 len = read(fd, &event, sizeof(event));
1635                 if (len < 0) {
1636                         BT_ERR("Fail to read events");
1637                         break;
1638                 }
1639
1640                 if (len != RFKILL_EVENT_SIZE) {
1641                         BT_ERR("The size is wrong\n");
1642                         continue;
1643                 }
1644
1645                 if (event.type == RFKILL_TYPE_BLUETOOTH) {
1646                         is_supported = 1;
1647                         break;
1648                 }
1649         }
1650
1651         close(fd);
1652
1653         BT_DBG("supported: %d", is_supported);
1654
1655         return is_supported;
1656 }
1657
1658 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
1659 {
1660         int ret;
1661
1662         _bt_gdbus_init_system_gconn();
1663
1664         ret = _bt_init_event_handler();
1665         if (ret != BLUETOOTH_ERROR_NONE &&
1666              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
1667                 BT_ERR("Fail to init the event handler");
1668                 return ret;
1669         }
1670
1671
1672         _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
1673
1674         /* Register All events */
1675         ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
1676         if (ret != BLUETOOTH_ERROR_NONE)
1677                 goto fail;
1678         ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
1679         if (ret != BLUETOOTH_ERROR_NONE)
1680                 goto fail;
1681         ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
1682         if (ret != BLUETOOTH_ERROR_NONE)
1683                 goto fail;
1684         ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
1685         if (ret != BLUETOOTH_ERROR_NONE)
1686                 goto fail;
1687         ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
1688         if (ret != BLUETOOTH_ERROR_NONE)
1689                 goto fail;
1690 #ifdef GATT_NO_RELAY
1691         ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
1692         if (ret != BLUETOOTH_ERROR_NONE)
1693                 goto fail;
1694 #endif
1695
1696         _bt_register_name_owner_changed();
1697
1698         return BLUETOOTH_ERROR_NONE;
1699 fail:
1700         BT_ERR("Fail to do _bt_register_event()");
1701         bluetooth_unregister_callback();
1702         return ret;
1703 }
1704
1705 BT_EXPORT_API int bluetooth_unregister_callback(void)
1706 {
1707         int ret;
1708
1709
1710         ret = _bt_deinit_event_handler();
1711         if (ret != BLUETOOTH_ERROR_NONE) {
1712                 BT_ERR("Fail to deinit the event handler");
1713         }
1714
1715         _bt_unregister_name_owner_changed();
1716
1717         _bt_set_user_data(BT_COMMON, NULL, NULL);
1718
1719         if (system_gconn) {
1720                 g_object_unref(system_gconn);
1721                 system_gconn = NULL;
1722         }
1723
1724         if (session_gconn) {
1725                 g_object_unref(session_gconn);
1726                 session_gconn = NULL;
1727         }
1728         return BLUETOOTH_ERROR_NONE;
1729 }
1730