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