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