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