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