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