Code Merge [Tizen3.0]: Applied tizen.org patches
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-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 <stdio.h>
25 #include <string.h>
26 #include <dbus/dbus-glib-lowlevel.h>
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus.h>
29 #include <glib.h>
30 #include <dlog.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <termios.h>
34 #include <net_connection.h>
35
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
39
40 static DBusGConnection *system_conn;
41 static DBusGConnection *session_conn;
42 static DBusGProxy *manager_proxy;
43 static DBusGProxy *adapter_proxy;
44 static void *net_conn;
45
46 static DBusGProxy *adapter_properties_proxy;
47
48 static DBusGProxy *__bt_init_manager_proxy(void)
49 {
50         DBusGProxy *proxy;
51
52         g_type_init();
53
54         if (system_conn == NULL) {
55                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
56                 retv_if(system_conn == NULL, NULL);
57         }
58
59         proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
60                         BT_MANAGER_PATH, BT_MANAGER_INTERFACE);
61
62         retv_if(proxy == NULL, NULL);
63
64         manager_proxy = proxy;
65
66         return proxy;
67 }
68
69 static DBusGProxy *__bt_init_adapter_proxy(void)
70 {
71         DBusGProxy *manager_proxy;
72         DBusGProxy *proxy;
73
74         if (system_conn == NULL) {
75                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
76                 retv_if(system_conn == NULL, NULL);
77         }
78
79         manager_proxy = _bt_get_manager_proxy();
80         retv_if(manager_proxy == NULL, NULL);
81
82         proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
83                                 BT_BLUEZ_HCI_PATH, BT_ADAPTER_INTERFACE);
84
85         retv_if(proxy == NULL, NULL);
86
87         adapter_proxy = proxy;
88
89         return proxy;
90 }
91
92 static DBusGProxy *__bt_init_adapter_properties_proxy(void)
93 {
94         DBusGProxy *manager_proxy;
95         DBusGProxy *proxy;
96
97         g_type_init();
98
99         if (system_conn == NULL) {
100                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
101                 retv_if(system_conn == NULL, NULL);
102         }
103
104         manager_proxy = _bt_get_manager_proxy();
105         retv_if(manager_proxy == NULL, NULL);
106
107         proxy = dbus_g_proxy_new_for_name(system_conn, BT_BLUEZ_NAME,
108                         BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
109
110         retv_if(proxy == NULL, NULL);
111
112         adapter_properties_proxy = proxy;
113
114         return proxy;
115 }
116
117 DBusGConnection *__bt_init_system_gconn(void)
118 {
119         g_type_init();
120
121         if (system_conn == NULL)
122                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
123
124         return system_conn;
125 }
126
127 DBusGConnection *__bt_init_session_conn(void)
128 {
129         if (session_conn == NULL)
130                 session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
131
132         return session_conn;
133 }
134
135 DBusGConnection *_bt_get_session_gconn(void)
136 {
137         return (session_conn) ? session_conn : __bt_init_session_conn();
138 }
139
140 DBusGConnection *_bt_get_system_gconn(void)
141 {
142         return (system_conn) ? system_conn : __bt_init_system_gconn();
143 }
144
145 DBusConnection *_bt_get_system_conn(void)
146 {
147         DBusGConnection *g_conn;
148
149         if (system_conn == NULL) {
150                 g_conn = __bt_init_system_gconn();
151         } else {
152                 g_conn = system_conn;
153         }
154
155         retv_if(g_conn == NULL, NULL);
156
157         return dbus_g_connection_get_connection(g_conn);
158 }
159
160 DBusGProxy *_bt_get_manager_proxy(void)
161 {
162         if (manager_proxy) {
163                 const char *path =  dbus_g_proxy_get_path(manager_proxy);
164                 if (path == NULL) {
165                         BT_ERR("Already proxy released hence creating new proxy");
166                         return  __bt_init_manager_proxy();
167                 }
168                 return manager_proxy;
169         }
170         return  __bt_init_manager_proxy();
171 }
172
173 static void *__bt_init_net_conn(void)
174 {
175         int result;
176         connection_h connection = NULL;
177
178         if (net_conn == NULL) {
179                 result = connection_create(&connection);
180
181         if (result != CONNECTION_ERROR_NONE ||
182                                         connection == NULL) {
183                 BT_DBG("connection_create() failed: %d", result);
184                 net_conn = NULL;
185                 return NULL;
186         }
187                 net_conn = connection;
188         }
189         return net_conn;
190 }
191
192 void *_bt_get_net_conn(void)
193 {
194         return (net_conn) ? net_conn : __bt_init_net_conn();
195 }
196
197 gboolean _bt_get_adapter_power(void)
198 {
199         DBusGProxy *proxy = NULL;
200         gboolean powered;
201         GValue powered_v = { 0 };
202         GError *err = NULL;
203
204         proxy = _bt_get_adapter_properties_proxy();
205         retv_if(proxy == NULL, FALSE);
206
207         if (!dbus_g_proxy_call(proxy, "Get", &err,
208                         G_TYPE_STRING, BT_ADAPTER_INTERFACE,
209                         G_TYPE_STRING, "Powered",
210                         G_TYPE_INVALID,
211                         G_TYPE_VALUE, &powered_v,
212                         G_TYPE_INVALID)) {
213                 if (err != NULL) {
214                         BT_ERR("Getting property failed: [%s]\n", err->message);
215                         g_error_free(err);
216                 }
217                 return FALSE;
218         }
219
220         powered = (gboolean)g_value_get_boolean(&powered_v);
221
222         BT_DBG("powered = %d", powered);
223
224         return powered;
225 }
226
227 DBusGProxy *_bt_get_adapter_proxy(void)
228 {
229         if (adapter_proxy) {
230                 const char *path =  dbus_g_proxy_get_path(adapter_proxy);
231                 if (path == NULL) {
232                         BT_ERR("Already proxy released hence creating new proxy");
233                         return  __bt_init_adapter_proxy();
234                 }
235
236                 return adapter_proxy;
237         }
238         return  __bt_init_adapter_proxy();
239
240 }
241
242 DBusGProxy *_bt_get_adapter_properties_proxy(void)
243 {
244         return (adapter_properties_proxy) ? adapter_properties_proxy :
245                                         __bt_init_adapter_properties_proxy();
246 }
247
248 static char *__bt_extract_adapter_path(DBusMessageIter *msg_iter)
249 {
250         char *object_path = NULL;
251         DBusMessageIter value_iter;
252
253         /* Parse the signature:  oa{sa{sv}}} */
254         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
255                                 DBUS_TYPE_OBJECT_PATH, NULL);
256
257         dbus_message_iter_get_basic(msg_iter, &object_path);
258         retv_if(object_path == NULL, NULL);
259
260         /* object array (oa) */
261         retv_if(dbus_message_iter_next(msg_iter) == FALSE, NULL);
262         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
263                                 DBUS_TYPE_ARRAY, NULL);
264
265         dbus_message_iter_recurse(msg_iter, &value_iter);
266
267         /* string array (sa) */
268         while (dbus_message_iter_get_arg_type(&value_iter) ==
269                                         DBUS_TYPE_DICT_ENTRY) {
270                 char *interface_name = NULL;
271                 DBusMessageIter interface_iter;
272
273                 dbus_message_iter_recurse(&value_iter, &interface_iter);
274
275                 retv_if(dbus_message_iter_get_arg_type(&interface_iter) !=
276                         DBUS_TYPE_STRING, NULL);
277
278                 dbus_message_iter_get_basic(&interface_iter, &interface_name);
279
280                 if (g_strcmp0(interface_name, "org.bluez.Adapter1") == 0) {
281                         /* Tizen don't allow the multi-adapter */
282                         BT_DBG("Found an adapter: %s", object_path);
283                         return g_strdup(object_path);
284                 }
285
286                 dbus_message_iter_next(&value_iter);
287         }
288
289         BT_DBG("There is no adapter");
290
291         return NULL;
292 }
293
294 char *_bt_get_adapter_path(void)
295 {
296         DBusMessage *msg;
297         DBusMessage *reply;
298         DBusMessageIter reply_iter;
299         DBusMessageIter value_iter;
300         DBusError err;
301         DBusConnection *conn;
302         char *adapter_path = NULL;
303
304         conn = _bt_get_system_conn();
305         retv_if(conn == NULL, NULL);
306
307         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
308                                                 BT_MANAGER_INTERFACE,
309                                                 "GetManagedObjects");
310
311         retv_if(msg == NULL, NULL);
312
313         /* Synchronous call */
314         dbus_error_init(&err);
315         reply = dbus_connection_send_with_reply_and_block(
316                                         conn, msg,
317                                         -1, &err);
318         dbus_message_unref(msg);
319
320         if (!reply) {
321                 BT_ERR("Can't get managed objects");
322
323                 if (dbus_error_is_set(&err)) {
324                         BT_ERR("%s", err.message);
325                         dbus_error_free(&err);
326                 }
327                 return NULL;
328         }
329
330         if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
331                 BT_ERR("Fail to iterate the reply");
332                 dbus_message_unref(reply);
333                 return NULL;
334         }
335
336         dbus_message_iter_recurse(&reply_iter, &value_iter);
337
338         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
339         while (dbus_message_iter_get_arg_type(&value_iter) ==
340                                                 DBUS_TYPE_DICT_ENTRY) {
341                 DBusMessageIter msg_iter;
342
343                 dbus_message_iter_recurse(&value_iter, &msg_iter);
344
345                 adapter_path = __bt_extract_adapter_path(&msg_iter);
346                 if (adapter_path != NULL) {
347                         BT_DBG("Found the adapter path");
348                         break;
349                 }
350
351                 dbus_message_iter_next(&value_iter);
352         }
353         dbus_message_unref(reply);
354
355         return adapter_path;
356 }
357
358 void _bt_deinit_bluez_proxy(void)
359 {
360         if (manager_proxy) {
361                 g_object_unref(manager_proxy);
362                 manager_proxy = NULL;
363         }
364
365         if (adapter_proxy) {
366                 g_object_unref(adapter_proxy);
367                 adapter_proxy = NULL;
368         }
369         if (adapter_properties_proxy) {
370                 g_object_unref(adapter_properties_proxy);
371                 adapter_properties_proxy = NULL;
372         }
373 }
374
375 void _bt_deinit_proxys(void)
376 {
377         int ret;
378         _bt_deinit_bluez_proxy();
379
380         if (system_conn) {
381                 dbus_g_connection_unref(system_conn);
382                 system_conn = NULL;
383         }
384
385         if (session_conn) {
386                 dbus_g_connection_unref(session_conn);
387                 session_conn = NULL;
388         }
389
390         if (net_conn) {
391                 ret = connection_destroy(net_conn);
392                 net_conn = NULL;
393                 if (ret != 0)
394                         BT_ERR("connection_destroy failed : %d", ret);
395         }
396 }
397
398 void _bt_convert_device_path_to_address(const char *device_path,
399                                                 char *device_address)
400 {
401         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
402         char *dev_addr;
403
404         ret_if(device_path == NULL);
405         ret_if(device_address == NULL);
406
407         dev_addr = strstr(device_path, "dev_");
408         if (dev_addr != NULL) {
409                 char *pos = NULL;
410                 dev_addr += 4;
411                 g_strlcpy(address, dev_addr, sizeof(address));
412
413                 while ((pos = strchr(address, '_')) != NULL) {
414                         *pos = ':';
415                 }
416
417                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
418         }
419 }
420
421
422 void _bt_convert_addr_string_to_type(unsigned char *addr,
423                                         const char *address)
424 {
425         int i;
426         char *ptr = NULL;
427
428         ret_if(address == NULL);
429         ret_if(addr == NULL);
430
431         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
432                 addr[i] = strtol(address, &ptr, 16);
433                 if (ptr[0] != '\0') {
434                         if (ptr[0] != ':')
435                                 return;
436
437                         address = ptr + 1;
438                 }
439         }
440 }
441
442 void _bt_convert_addr_type_to_string(char *address,
443                                 unsigned char *addr)
444 {
445         ret_if(address == NULL);
446         ret_if(addr == NULL);
447
448         snprintf(address, BT_ADDRESS_STRING_SIZE,
449                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
450                         addr[0], addr[1], addr[2],
451                         addr[3], addr[4], addr[5]);
452 }
453
454 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
455 {
456         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
457                                 addr->addr[3], addr->addr[4], addr->addr[5]);
458 }
459
460 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
461                                 unsigned int cod)
462 {
463         ret_if(device_class == NULL);
464
465         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
466         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
467         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
468
469         if (cod & 0x002000) {
470                 device_class->service_class |=
471                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
472         }
473 }
474
475 void _bt_free_device_info(bt_remote_dev_info_t *dev_info)
476 {
477         int i;
478
479         ret_if(dev_info == NULL);
480
481         g_free(dev_info->address);
482         g_free(dev_info->name);
483         g_free(dev_info->manufacturer_data);
484
485         if (dev_info->uuids) {
486                 for (i = 0; dev_info->uuids[i] != NULL; i++)
487                         g_free(dev_info->uuids[i]);
488
489                 g_free(dev_info->uuids);
490         }
491
492         g_free(dev_info);
493 }
494
495 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
496 {
497         ret_if(le_dev_info == NULL);
498
499         g_free(le_dev_info->adv_data);
500         g_free(le_dev_info);
501 }
502
503 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
504 {
505         int i;
506         const char *p = src;
507         char *next;
508         int count;
509
510         if (dest == NULL || src == NULL)
511                 return BLUETOOTH_ERROR_INVALID_PARAM;
512
513         BT_DBG("+src : %s", src);
514         BT_DBG("+dest : %s", dest);
515
516         i = 0;
517         while (*p != '\0' && i < length) {
518                 next = g_utf8_next_char(p);
519                 count = next - p;
520
521                 while (count > 0 && ((i + count) < length)) {
522                         dest[i++] = *p;
523                         p++;
524                         count --;
525                 }
526                 p = next;
527         }
528         return BLUETOOTH_ERROR_NONE;
529 }
530
531 gboolean _bt_utf8_validate(char *name)
532 {
533         BT_DBG("+");
534         gunichar2 *u16;
535         glong items_written = 0;
536
537         if (FALSE == g_utf8_validate(name, -1, NULL))
538                 return FALSE;
539
540         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
541         if (u16 == NULL)
542                 return FALSE;
543
544         g_free(u16);
545
546         if (items_written != g_utf8_strlen(name, -1))
547                 return FALSE;
548
549         BT_DBG("-");
550         return TRUE;
551 }
552
553 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
554 {
555         if (!_bt_agent_register_osp_server( type, uuid, path, fd))
556                 return BLUETOOTH_ERROR_INTERNAL;
557
558         return BLUETOOTH_ERROR_NONE;
559 }
560
561 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
562 {
563         if (!_bt_agent_unregister_osp_server( type, uuid))
564                 return BLUETOOTH_ERROR_INTERNAL;
565
566         return BLUETOOTH_ERROR_NONE;
567 }
568
569 int _bt_set_socket_non_blocking(int socket_fd)
570 {
571         /* Set Nonblocking */
572         long arg;
573
574         arg = fcntl(socket_fd, F_GETFL);
575
576         if (arg < 0)
577                 return -errno;
578
579         if (arg & O_NONBLOCK) {
580                 BT_ERR("Already Non-blocking \n");
581         }
582
583         arg |= O_NONBLOCK;
584
585         if (fcntl(socket_fd, F_SETFL, arg) < 0)
586                 return -errno;
587
588         return BLUETOOTH_ERROR_NONE;
589 }
590
591 int _bt_set_non_blocking_tty(int sk)
592 {
593         struct termios ti = {0,};
594         int err;
595
596         err = _bt_set_socket_non_blocking(sk);
597
598         if (err < 0) {
599                 BT_ERR("Error in set non blocking!\n");
600                 return err;
601         }
602
603         tcflush(sk, TCIOFLUSH);
604
605         /* Switch tty to RAW mode */
606         cfmakeraw(&ti);
607         tcsetattr(sk, TCSANOW, &ti);
608
609         return BLUETOOTH_ERROR_NONE;
610 }
611
612 static char *__bt_extract_device_path(DBusMessageIter *msg_iter, char *address)
613 {
614         char *object_path = NULL;
615         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
616
617         /* Parse the signature:  oa{sa{sv}}} */
618         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
619                                 DBUS_TYPE_OBJECT_PATH, NULL);
620
621         dbus_message_iter_get_basic(msg_iter, &object_path);
622         retv_if(object_path == NULL, NULL);
623
624         _bt_convert_device_path_to_address(object_path, device_address);
625
626         if (g_strcmp0(address, device_address) == 0) {
627                 return g_strdup(object_path);
628         }
629
630         return NULL;
631 }
632
633 char *_bt_get_device_object_path(char *address)
634 {
635         DBusMessage *msg;
636         DBusMessage *reply;
637         DBusMessageIter reply_iter;
638         DBusMessageIter value_iter;
639         DBusError err;
640         DBusConnection *conn;
641         char *object_path = NULL;
642
643         conn = _bt_get_system_conn();
644         retv_if(conn == NULL, NULL);
645
646         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
647                                                 BT_MANAGER_INTERFACE,
648                                                 "GetManagedObjects");
649
650         retv_if(msg == NULL, NULL);
651
652         /* Synchronous call */
653         dbus_error_init(&err);
654         reply = dbus_connection_send_with_reply_and_block(
655                                         conn, msg,
656                                         -1, &err);
657         dbus_message_unref(msg);
658
659         if (!reply) {
660                 BT_ERR("Can't get managed objects");
661
662                 if (dbus_error_is_set(&err)) {
663                         BT_ERR("%s", err.message);
664                         dbus_error_free(&err);
665                 }
666                 return NULL;
667         }
668
669         if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
670                 BT_ERR("Fail to iterate the reply");
671                 dbus_message_unref(reply);
672                 return NULL;
673         }
674
675         dbus_message_iter_recurse(&reply_iter, &value_iter);
676
677         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
678         while (dbus_message_iter_get_arg_type(&value_iter) ==
679                                                 DBUS_TYPE_DICT_ENTRY) {
680                 DBusMessageIter msg_iter;
681
682                 dbus_message_iter_recurse(&value_iter, &msg_iter);
683
684                 object_path = __bt_extract_device_path(&msg_iter, address);
685                 if (object_path != NULL) {
686                         BT_DBG("Found the device path");
687                         break;
688                 }
689
690                 dbus_message_iter_next(&value_iter);
691         }
692         dbus_message_unref(reply);
693
694         return object_path;
695 }
696
697 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
698 {
699         switch(profile_type) {
700         case BT_PROFILE_CONN_RFCOMM:
701                 return strdup(RFCOMM_UUID_STR);
702         case BT_PROFILE_CONN_A2DP:
703                 return strdup(A2DP_SINK_UUID);
704         case BT_PROFILE_CONN_HSP:
705                 return strdup(HFP_HS_UUID);
706         case BT_PROFILE_CONN_HID:
707                 return strdup(HID_UUID);
708         case BT_PROFILE_CONN_NAP:
709                 return strdup(NAP_UUID);
710         case BT_PROFILE_CONN_HFG:
711                 return strdup(HFP_AG_UUID);
712         case BT_PROFILE_CONN_GATT:
713         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
714         default:
715                 return NULL;
716         };
717 }
718
719 char *_bt_convert_error_to_string(int error)
720 {
721         switch (error) {
722         case BLUETOOTH_ERROR_CANCEL:
723                 return "CANCELLED";
724         case BLUETOOTH_ERROR_INVALID_PARAM:
725                 return "INVALID_PARAMETER";
726         case BLUETOOTH_ERROR_INVALID_DATA:
727                 return "INVALID DATA";
728         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
729         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
730                 return "OUT_OF_MEMORY";
731         case BLUETOOTH_ERROR_TIMEOUT:
732                 return "TIMEOUT";
733         case BLUETOOTH_ERROR_NO_RESOURCES:
734                 return "NO_RESOURCES";
735         case BLUETOOTH_ERROR_INTERNAL:
736                 return "INTERNAL";
737         case BLUETOOTH_ERROR_NOT_SUPPORT:
738                 return "NOT_SUPPORT";
739         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
740                 return "NOT_ENABLED";
741         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
742                 return "ALREADY_ENABLED";
743         case BLUETOOTH_ERROR_DEVICE_BUSY:
744                 return "DEVICE_BUSY";
745         case BLUETOOTH_ERROR_ACCESS_DENIED:
746                 return "ACCESS_DENIED";
747         case BLUETOOTH_ERROR_MAX_CLIENT:
748                 return "MAX_CLIENT";
749         case BLUETOOTH_ERROR_NOT_FOUND:
750                 return "NOT_FOUND";
751         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
752                 return "SERVICE_SEARCH_ERROR";
753         case BLUETOOTH_ERROR_PARING_FAILED:
754                 return "PARING_FAILED";
755         case BLUETOOTH_ERROR_NOT_PAIRED:
756                 return "NOT_PAIRED";
757         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
758                 return "SERVICE_NOT_FOUND";
759         case BLUETOOTH_ERROR_NOT_CONNECTED:
760                 return "NOT_CONNECTED";
761         case BLUETOOTH_ERROR_ALREADY_CONNECT:
762                 return "ALREADY_CONNECT";
763         case BLUETOOTH_ERROR_CONNECTION_BUSY:
764                 return "CONNECTION_BUSY";
765         case BLUETOOTH_ERROR_CONNECTION_ERROR:
766                 return "CONNECTION_ERROR";
767         case BLUETOOTH_ERROR_MAX_CONNECTION:
768                 return "MAX_CONNECTION";
769         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
770                 return "NOT_IN_OPERATION";
771         case BLUETOOTH_ERROR_CANCEL_BY_USER:
772                 return "CANCEL_BY_USER";
773         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
774                 return "REGISTRATION_FAILED";
775         case BLUETOOTH_ERROR_IN_PROGRESS:
776                 return "IN_PROGRESS";
777         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
778                 return "AUTHENTICATION_FAILED";
779         case BLUETOOTH_ERROR_HOST_DOWN:
780                 return "HOST_DOWN";
781         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
782                 return "END_OF_DEVICE_LIST";
783         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
784                 return "AGENT_ALREADY_EXIST";
785         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
786                 return "AGENT_DOES_NOT_EXIST";
787         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
788                 return "ALREADY_INITIALIZED";
789         case BLUETOOTH_ERROR_PERMISSION_DEINED:
790                 return "PERMISSION_DEINED";
791         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
792                 return "ALREADY_DEACTIVATED";
793         case BLUETOOTH_ERROR_NOT_INITIALIZED:
794                 return "NOT_INITIALIZED";
795         default:
796                 return "UNKNOWN";
797         }
798 }
799
800 char * _bt_convert_disc_reason_to_string(int reason)
801 {
802         switch(reason) {
803         case 1:
804                 return "Link loss";
805         case 2:
806                 return "Connection terminated by local host";
807         case 3:
808                 return "Remote user terminated connection";
809         case 0:
810         default:
811                 return "Unknown";
812         }
813 }
814
815 void _bt_logging_connection(gboolean connect, int addr_type)
816 {
817         static int le_conn = 0;
818         static int le_disc = 0;
819         static int edr_conn = 0;
820         static int edr_disc = 0;
821
822         if (connect) {
823                 if (addr_type)
824                         le_conn++;
825                 else
826                         edr_conn++;
827         } else {
828                 if (addr_type)
829                         le_disc++;
830                 else
831                         edr_disc++;
832         }
833
834         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
835                         le_conn, le_disc, edr_conn, edr_disc);
836 }
837