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