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