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