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