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