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