[Boiler Plate] Update Boiler plate to remove unwanted information
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <termios.h>
25 #include <net_connection.h>
26 #include <dbus/dbus.h>
27 #include <glib.h>
28 #include <dlog.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <termios.h>
32 #include <net_connection.h>
33 #include <bundle.h>
34 #include <eventsystem.h>
35
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
39
40 static GDBusConnection *system_conn;
41 static GDBusConnection *session_conn;
42 static GDBusProxy *manager_proxy;
43 static GDBusProxy *adapter_proxy;
44 static void *net_conn;
45
46 static GDBusProxy *adapter_properties_proxy;
47
48 static GDBusConnection *system_gconn = NULL;
49
50 GDBusConnection *_bt_gdbus_init_system_gconn(void)
51 {
52         GError *error = NULL;
53
54         dbus_threads_init_default();
55
56         g_type_init();
57
58         if (system_gconn != NULL)
59                 return system_gconn;
60
61         system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
62
63         if (!system_gconn) {
64                 BT_ERR("Unable to connect to dbus: %s", error->message);
65                 g_clear_error(&error);
66         }
67
68         return system_gconn;
69 }
70
71 GDBusConnection *_bt_gdbus_get_system_gconn(void)
72 {
73         GDBusConnection *local_system_gconn = NULL;
74         GError *error = NULL;
75
76         if (system_gconn == NULL) {
77                 system_gconn = _bt_gdbus_init_system_gconn();
78         } else if (g_dbus_connection_is_closed(system_gconn)){
79
80                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
81
82                 if (!local_system_gconn) {
83                         BT_ERR("Unable to connect to dbus: %s", error->message);
84                         g_clear_error(&error);
85                 }
86
87                 system_gconn = local_system_gconn;
88         }
89
90         return system_gconn;
91 }
92
93 static GDBusProxy *__bt_init_manager_proxy(void)
94 {
95         GDBusProxy *proxy;
96
97         g_type_init();
98
99         if (system_conn == NULL) {
100                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
101                 retv_if(system_conn == NULL, NULL);
102         }
103
104         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
105                                                                 NULL, BT_BLUEZ_NAME,
106                                                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE,  NULL, NULL);
107
108         retv_if(proxy == NULL, NULL);
109
110         manager_proxy = proxy;
111
112         return proxy;
113 }
114
115 static GDBusProxy *__bt_init_adapter_proxy(void)
116 {
117         GDBusProxy *manager_proxy;
118         GDBusProxy *proxy;
119         char *adapter_path = NULL;
120
121         g_type_init();
122
123         if (system_conn == NULL) {
124                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
125                 retv_if(system_conn == NULL, NULL);
126         }
127
128         manager_proxy = _bt_get_manager_proxy();
129         retv_if(manager_proxy == NULL, NULL);
130
131         adapter_path = _bt_get_adapter_path();
132         retv_if(adapter_path == NULL, NULL);
133
134         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
135                                                                 NULL, BT_BLUEZ_NAME,
136                                                                 adapter_path, BT_ADAPTER_INTERFACE,  NULL, NULL);
137
138         g_free(adapter_path);
139
140         retv_if(proxy == NULL, NULL);
141
142         adapter_proxy = proxy;
143
144         return proxy;
145 }
146
147 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
148 {
149         GDBusProxy *manager_proxy;
150         GDBusProxy *proxy;
151         char *adapter_path = NULL;
152
153         g_type_init();
154
155         if (system_conn == NULL) {
156                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
157                 retv_if(system_conn == NULL, NULL);
158         }
159
160         manager_proxy = _bt_get_manager_proxy();
161         retv_if(manager_proxy == NULL, NULL);
162
163         adapter_path = _bt_get_adapter_path();
164         retv_if(adapter_path == NULL, NULL);
165
166         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
167                                                                         NULL, BT_BLUEZ_NAME,
168                                                                         adapter_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
169
170         g_free(adapter_path);
171
172         retv_if(proxy == NULL, NULL);
173
174         adapter_properties_proxy = proxy;
175
176         return proxy;
177 }
178
179 GDBusConnection *__bt_init_system_gconn(void)
180 {
181         g_type_init();
182
183         if (system_conn == NULL)
184                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
185
186         return system_conn;
187 }
188
189 GDBusConnection *__bt_init_session_conn(void)
190 {
191         if (session_conn == NULL)
192                 session_conn =g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
193
194         return session_conn;
195 }
196
197 GDBusConnection *_bt_get_session_gconn(void)
198 {
199         return (session_conn) ? session_conn : __bt_init_session_conn();
200 }
201
202 GDBusConnection *_bt_get_system_gconn(void)
203 {
204         return (system_conn) ? system_conn : __bt_init_system_gconn();
205 }
206
207 GDBusConnection *_bt_get_system_conn(void)
208 {
209         GDBusConnection *g_conn;
210
211         if (system_conn == NULL) {
212                 g_conn = __bt_init_system_gconn();
213         } else {
214                 g_conn = system_conn;
215         }
216
217         retv_if(g_conn == NULL, NULL);
218
219         return g_conn;
220 }
221
222 GDBusProxy *_bt_get_manager_proxy(void)
223 {
224         if (manager_proxy) {
225                 const gchar *path =  g_dbus_proxy_get_object_path(manager_proxy);
226                 if (path == NULL) {
227                         BT_ERR("Already proxy released hence creating new proxy");
228                         return  __bt_init_manager_proxy();
229                 }
230                 return manager_proxy;
231         }
232         return  __bt_init_manager_proxy();
233 }
234
235 static void *__bt_init_net_conn(void)
236 {
237         int result;
238         connection_h connection = NULL;
239
240         if (net_conn == NULL) {
241                 result = connection_create(&connection);
242
243         if (result != CONNECTION_ERROR_NONE ||
244                                         connection == NULL) {
245                 BT_DBG("connection_create() failed: %d", result);
246                 net_conn = NULL;
247                 return NULL;
248         }
249                 net_conn = connection;
250         }
251         return net_conn;
252 }
253
254 void *_bt_get_net_conn(void)
255 {
256         return (net_conn) ? net_conn : __bt_init_net_conn();
257 }
258
259 GDBusProxy *_bt_get_adapter_proxy(void)
260 {
261         if (adapter_proxy) {
262                 const char *path =  g_dbus_proxy_get_object_path(adapter_proxy);
263                 if (path == NULL) {
264                         BT_ERR("Already proxy released hence creating new proxy");
265                         return  __bt_init_adapter_proxy();
266                 }
267
268                 return adapter_proxy;
269         }
270         return  __bt_init_adapter_proxy();
271
272 }
273
274 GDBusProxy *_bt_get_adapter_properties_proxy(void)
275 {
276         return (adapter_properties_proxy) ? adapter_properties_proxy :
277                                         __bt_init_adapter_properties_proxy();
278 }
279
280 static char *__bt_extract_adapter_path(GVariantIter *iter)
281 {
282         char *object_path = NULL;
283         GVariantIter *interface_iter;
284         GVariantIter *svc_iter;
285         char *interface_str = NULL;
286
287         /* Parse the signature: oa{sa{sv}}} */
288         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
289                         &interface_iter)) {
290
291                 if (object_path == NULL)
292                         continue;
293
294                 while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
295                                 &interface_str, &svc_iter)) {
296                         if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
297                                 continue;
298
299                         BT_DBG("Object Path: %s", object_path);
300                         g_variant_iter_free(svc_iter);
301                         g_variant_iter_free(interface_iter);
302                         return g_strdup(object_path);
303                 }
304         }
305         return NULL;
306 }
307
308 char *_bt_get_adapter_path(void)
309 {
310         GDBusConnection *conn;
311         GDBusProxy *manager_proxy;
312         GVariant *result = NULL;
313         GVariantIter *iter = NULL;
314         char *adapter_path = NULL;
315
316         conn = _bt_get_system_conn();
317         retv_if(conn == NULL, NULL);
318
319         manager_proxy = _bt_get_manager_proxy();
320         retv_if(manager_proxy == NULL, NULL);
321
322         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
323                                 NULL,
324                                 G_DBUS_CALL_FLAGS_NONE,
325                                 -1,
326                                 NULL,
327                                 NULL);
328         if (!result) {
329                 BT_ERR("Can't get managed objects");
330                 return NULL;
331         }
332
333         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
334         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
335
336         adapter_path = __bt_extract_adapter_path(iter);
337         g_variant_iter_free(iter);
338         g_variant_unref(result);
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                 g_object_unref(system_conn);
366                 system_conn = NULL;
367         }
368
369         if (session_conn) {
370                 g_object_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; i < dev_info->uuid_count && dev_info->uuids[i]; 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         BT_DBG("+");
540         if (!_bt_agent_register_osp_server( type, uuid, path, fd))
541                 return BLUETOOTH_ERROR_INTERNAL;
542
543         return BLUETOOTH_ERROR_NONE;
544 }
545
546 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
547 {
548         BT_DBG("+");
549         if (!_bt_agent_unregister_osp_server( type, uuid))
550                 return BLUETOOTH_ERROR_INTERNAL;
551
552         return BLUETOOTH_ERROR_NONE;
553 }
554
555 int _bt_set_socket_non_blocking(int socket_fd)
556 {
557         /* Set Nonblocking */
558         long arg;
559
560         arg = fcntl(socket_fd, F_GETFL);
561
562         if (arg < 0)
563                 return -errno;
564
565         if (arg & O_NONBLOCK) {
566                 BT_ERR("Already Non-blocking \n");
567         }
568
569         arg |= O_NONBLOCK;
570
571         if (fcntl(socket_fd, F_SETFL, arg) < 0)
572                 return -errno;
573
574         return BLUETOOTH_ERROR_NONE;
575 }
576
577 int _bt_set_non_blocking_tty(int sk)
578 {
579         struct termios ti = {0,};
580         int err;
581
582         err = _bt_set_socket_non_blocking(sk);
583
584         if (err < 0) {
585                 BT_ERR("Error in set non blocking!\n");
586                 return err;
587         }
588
589         tcflush(sk, TCIOFLUSH);
590
591         /* Switch tty to RAW mode */
592         cfmakeraw(&ti);
593         tcsetattr(sk, TCSANOW, &ti);
594
595         return BLUETOOTH_ERROR_NONE;
596 }
597
598 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
599 {
600         char *object_path = NULL;
601         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
602
603         /* Parse the signature: oa{sa{sv}}} */
604         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
605                         NULL)) {
606                 retv_if(object_path == NULL, NULL);
607                 _bt_convert_device_path_to_address(object_path, device_address);
608                 if (g_strcmp0(address, device_address) == 0) {
609                         return g_strdup(object_path);
610                 }
611         }
612         return NULL;
613 }
614
615 char *_bt_get_device_object_path(char *address)
616 {
617         char *object_path = NULL;
618         GDBusConnection *conn;
619         GDBusProxy *manager_proxy;
620         GVariant *result = NULL;
621         GVariantIter *iter = NULL;
622
623         conn = _bt_get_system_conn();
624         retv_if(conn == NULL, NULL);
625
626         manager_proxy = _bt_get_manager_proxy();
627         retv_if(manager_proxy == NULL, NULL);
628
629         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
630                                 NULL,
631                                 G_DBUS_CALL_FLAGS_NONE,
632                                 -1,
633                                 NULL,
634                                 NULL);
635         if (!result) {
636                 BT_ERR("Can't get managed objects");
637                 return NULL;
638         }
639
640         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
641         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
642         object_path = __bt_extract_device_path(iter, address);
643         g_variant_iter_free(iter);
644         g_variant_unref(result);
645         return object_path;
646 }
647
648 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
649 {
650         switch(profile_type) {
651         case BT_PROFILE_CONN_RFCOMM:
652                 return strdup(RFCOMM_UUID_STR);
653         case BT_PROFILE_CONN_A2DP:
654                 return strdup(A2DP_SINK_UUID);
655         case BT_PROFILE_CONN_A2DP_SINK:
656                 return strdup(A2DP_SOURCE_UUID);
657         case BT_PROFILE_CONN_HSP:
658                 return strdup(HFP_HS_UUID);
659         case BT_PROFILE_CONN_HID:
660                 return strdup(HID_UUID);
661         case BT_PROFILE_CONN_NAP:
662                 return strdup(NAP_UUID);
663         case BT_PROFILE_CONN_HFG:
664                 return strdup(HFP_AG_UUID);
665         case BT_PROFILE_CONN_GATT:
666         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
667         default:
668                 return NULL;
669         };
670 }
671
672 char *_bt_convert_error_to_string(int error)
673 {
674         switch (error) {
675         case BLUETOOTH_ERROR_CANCEL:
676                 return "CANCELLED";
677         case BLUETOOTH_ERROR_INVALID_PARAM:
678                 return "INVALID_PARAMETER";
679         case BLUETOOTH_ERROR_INVALID_DATA:
680                 return "INVALID DATA";
681         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
682         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
683                 return "OUT_OF_MEMORY";
684         case BLUETOOTH_ERROR_TIMEOUT:
685                 return "TIMEOUT";
686         case BLUETOOTH_ERROR_NO_RESOURCES:
687                 return "NO_RESOURCES";
688         case BLUETOOTH_ERROR_INTERNAL:
689                 return "INTERNAL";
690         case BLUETOOTH_ERROR_NOT_SUPPORT:
691                 return "NOT_SUPPORT";
692         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
693                 return "NOT_ENABLED";
694         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
695                 return "ALREADY_ENABLED";
696         case BLUETOOTH_ERROR_DEVICE_BUSY:
697                 return "DEVICE_BUSY";
698         case BLUETOOTH_ERROR_ACCESS_DENIED:
699                 return "ACCESS_DENIED";
700         case BLUETOOTH_ERROR_MAX_CLIENT:
701                 return "MAX_CLIENT";
702         case BLUETOOTH_ERROR_NOT_FOUND:
703                 return "NOT_FOUND";
704         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
705                 return "SERVICE_SEARCH_ERROR";
706         case BLUETOOTH_ERROR_PARING_FAILED:
707                 return "PARING_FAILED";
708         case BLUETOOTH_ERROR_NOT_PAIRED:
709                 return "NOT_PAIRED";
710         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
711                 return "SERVICE_NOT_FOUND";
712         case BLUETOOTH_ERROR_NOT_CONNECTED:
713                 return "NOT_CONNECTED";
714         case BLUETOOTH_ERROR_ALREADY_CONNECT:
715                 return "ALREADY_CONNECT";
716         case BLUETOOTH_ERROR_CONNECTION_BUSY:
717                 return "CONNECTION_BUSY";
718         case BLUETOOTH_ERROR_CONNECTION_ERROR:
719                 return "CONNECTION_ERROR";
720         case BLUETOOTH_ERROR_MAX_CONNECTION:
721                 return "MAX_CONNECTION";
722         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
723                 return "NOT_IN_OPERATION";
724         case BLUETOOTH_ERROR_CANCEL_BY_USER:
725                 return "CANCEL_BY_USER";
726         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
727                 return "REGISTRATION_FAILED";
728         case BLUETOOTH_ERROR_IN_PROGRESS:
729                 return "IN_PROGRESS";
730         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
731                 return "AUTHENTICATION_FAILED";
732         case BLUETOOTH_ERROR_HOST_DOWN:
733                 return "HOST_DOWN";
734         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
735                 return "END_OF_DEVICE_LIST";
736         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
737                 return "AGENT_ALREADY_EXIST";
738         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
739                 return "AGENT_DOES_NOT_EXIST";
740         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
741                 return "ALREADY_INITIALIZED";
742         case BLUETOOTH_ERROR_PERMISSION_DEINED:
743                 return "PERMISSION_DEINED";
744         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
745                 return "ALREADY_DEACTIVATED";
746         case BLUETOOTH_ERROR_NOT_INITIALIZED:
747                 return "NOT_INITIALIZED";
748         default:
749                 return "UNKNOWN";
750         }
751 }
752
753 char * _bt_convert_disc_reason_to_string(int reason)
754 {
755         switch(reason) {
756         case 1:
757                 return "Link loss";
758         case 2:
759                 return "Connection terminated by local host";
760         case 3:
761                 return "Remote user terminated connection";
762         case 0:
763         default:
764                 return "Unknown";
765         }
766 }
767
768 void _bt_logging_connection(gboolean connect, int addr_type)
769 {
770         static int le_conn = 0;
771         static int le_disc = 0;
772         static int edr_conn = 0;
773         static int edr_disc = 0;
774
775         if (connect) {
776                 if (addr_type)
777                         le_conn++;
778                 else
779                         edr_conn++;
780         } else {
781                 if (addr_type)
782                         le_disc++;
783                 else
784                         edr_disc++;
785         }
786
787         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
788                         le_conn, le_disc, edr_conn, edr_disc);
789 }
790
791 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
792 {
793         int ret;
794         bundle *b = NULL;
795
796         b = bundle_create();
797
798         bundle_add_str(b, key, value);
799
800         ret = eventsystem_send_system_event(event, b);
801
802         BT_DBG("eventsystem_send_system_event result: %d", ret);
803
804         bundle_free(b);
805
806         return ret;
807 }
808
809 void _bt_swap_byte_ordering(char *data, int data_len)
810 {
811         char temp;
812         int i, j;
813
814         ret_if(data == NULL);
815         /* Swap to opposite endian */
816         for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
817                 temp = data[i];
818                 data[i] = data[j];
819                 data[j] = temp;
820                 }
821 }
822
823 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
824 {
825         int i;
826
827         retv_if(data1 == NULL, -1);
828         retv_if(data2 == NULL, -1);
829         for (i = 0; i < data_len; i++) {
830                 if (data1[i] != data2[i])
831                         return data1[i] - data2[i];
832                 }
833         return 0;
834 }
835 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
836         const char *mask, int data_len)
837 {
838         int i;
839         char a, b;
840
841         retv_if(data1 == NULL, -1);
842         retv_if(data2 == NULL, -1);
843         retv_if(mask == NULL, -1);
844         for (i = 0; i < data_len; i++) {
845                 a = data1[i] & mask[i];
846                 b = data2[i] & mask[i];
847                 if (a != b)
848                         return (int)(a - b);
849                 }
850         return 0;
851 }