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