48710a0b05248a2577602725f4a5eab213b80f7a
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <termios.h>
25 #include <net_connection.h>
26 #include <dbus/dbus.h>
27 #include <glib.h>
28 #include <dlog.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <termios.h>
32 #include <net_connection.h>
33 #include <bundle.h>
34 #include <eventsystem.h>
35
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
39
40 static GDBusConnection *system_conn;
41 static GDBusConnection *session_conn;
42 static GDBusProxy *manager_proxy;
43 static GDBusProxy *adapter_proxy;
44 static void *net_conn;
45
46 static GDBusProxy *adapter_properties_proxy;
47
48 static GDBusConnection *system_gconn = NULL;
49
50 GDBusConnection *_bt_gdbus_init_system_gconn(void)
51 {
52         GError *error = NULL;
53
54         dbus_threads_init_default();
55
56         if (system_gconn != NULL)
57                 return system_gconn;
58
59         system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
60
61         if (!system_gconn) {
62                 BT_ERR("Unable to connect to dbus: %s", error->message);
63                 g_clear_error(&error);
64         }
65
66         return system_gconn;
67 }
68
69 GDBusConnection *_bt_gdbus_get_system_gconn(void)
70 {
71         GDBusConnection *local_system_gconn = NULL;
72         GError *error = NULL;
73
74         if (system_gconn == NULL) {
75                 system_gconn = _bt_gdbus_init_system_gconn();
76         } else if (g_dbus_connection_is_closed(system_gconn)) {
77
78                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
79
80                 if (!local_system_gconn) {
81                         BT_ERR("Unable to connect to dbus: %s", error->message);
82                         g_clear_error(&error);
83                 }
84
85                 system_gconn = local_system_gconn;
86         }
87
88         return system_gconn;
89 }
90
91 static GDBusProxy *__bt_init_manager_proxy(void)
92 {
93         GDBusProxy *proxy;
94
95         if (system_conn == NULL) {
96                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
97                 retv_if(system_conn == NULL, NULL);
98         }
99
100         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
101                                                                 NULL, BT_BLUEZ_NAME,
102                                                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE,  NULL, NULL);
103
104         if (!proxy) {
105                 BT_ERR("Unable to get proxy");
106                 return NULL;
107         }
108
109         manager_proxy = proxy;
110
111         return proxy;
112 }
113
114 static GDBusProxy *__bt_init_adapter_proxy(void)
115 {
116         GDBusProxy *manager_proxy;
117         GDBusProxy *proxy;
118         char *adapter_path = NULL;
119
120         if (system_conn == NULL) {
121                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
122                 retv_if(system_conn == NULL, NULL);
123         }
124
125         manager_proxy = _bt_get_manager_proxy();
126         retv_if(manager_proxy == NULL, NULL);
127
128         adapter_path = _bt_get_adapter_path();
129         retv_if(adapter_path == NULL, NULL);
130
131         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
132                                                                 NULL, BT_BLUEZ_NAME,
133                                                                 adapter_path, BT_ADAPTER_INTERFACE,  NULL, NULL);
134
135         g_free(adapter_path);
136
137         retv_if(proxy == NULL, NULL);
138
139         adapter_proxy = proxy;
140
141         return proxy;
142 }
143
144 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
145 {
146         GDBusProxy *manager_proxy;
147         GDBusProxy *proxy;
148         char *adapter_path = NULL;
149
150         if (system_conn == NULL) {
151                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
152                 retv_if(system_conn == NULL, NULL);
153         }
154
155         manager_proxy = _bt_get_manager_proxy();
156         retv_if(manager_proxy == NULL, NULL);
157
158         adapter_path = _bt_get_adapter_path();
159         retv_if(adapter_path == NULL, NULL);
160
161         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
162                                                                         NULL, BT_BLUEZ_NAME,
163                                                                         adapter_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
164
165         g_free(adapter_path);
166
167         retv_if(proxy == NULL, NULL);
168
169         adapter_properties_proxy = proxy;
170
171         return proxy;
172 }
173
174 GDBusConnection *__bt_init_system_gconn(void)
175 {
176         if (system_conn == NULL)
177                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
178
179         return system_conn;
180 }
181
182 GDBusConnection *__bt_init_session_conn(void)
183 {
184         if (session_conn == NULL)
185                 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
186
187         return session_conn;
188 }
189
190 GDBusConnection *_bt_get_session_gconn(void)
191 {
192         return (session_conn) ? session_conn : __bt_init_session_conn();
193 }
194
195 GDBusConnection *_bt_get_system_gconn(void)
196 {
197         return (system_conn) ? system_conn : __bt_init_system_gconn();
198 }
199
200 GDBusConnection *_bt_get_system_conn(void)
201 {
202         GDBusConnection *g_conn;
203
204         if (system_conn == NULL) {
205                 g_conn = __bt_init_system_gconn();
206         } else {
207                 g_conn = system_conn;
208         }
209
210         if (!g_conn) {
211                 BT_ERR("Unable to get dbus connection");
212                 return NULL;
213         }
214
215         return g_conn;
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_get_system_conn();
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_get_system_conn();
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_GATT:
686         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
687         default:
688                 return NULL;
689         };
690 }
691
692 char *_bt_convert_error_to_string(int error)
693 {
694         switch (error) {
695         case BLUETOOTH_ERROR_CANCEL:
696                 return "CANCELLED";
697         case BLUETOOTH_ERROR_INVALID_PARAM:
698                 return "INVALID_PARAMETER";
699         case BLUETOOTH_ERROR_INVALID_DATA:
700                 return "INVALID DATA";
701         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
702         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
703                 return "OUT_OF_MEMORY";
704         case BLUETOOTH_ERROR_TIMEOUT:
705                 return "TIMEOUT";
706         case BLUETOOTH_ERROR_NO_RESOURCES:
707                 return "NO_RESOURCES";
708         case BLUETOOTH_ERROR_INTERNAL:
709                 return "INTERNAL";
710         case BLUETOOTH_ERROR_NOT_SUPPORT:
711                 return "NOT_SUPPORT";
712         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
713                 return "NOT_ENABLED";
714         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
715                 return "ALREADY_ENABLED";
716         case BLUETOOTH_ERROR_DEVICE_BUSY:
717                 return "DEVICE_BUSY";
718         case BLUETOOTH_ERROR_ACCESS_DENIED:
719                 return "ACCESS_DENIED";
720         case BLUETOOTH_ERROR_MAX_CLIENT:
721                 return "MAX_CLIENT";
722         case BLUETOOTH_ERROR_NOT_FOUND:
723                 return "NOT_FOUND";
724         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
725                 return "SERVICE_SEARCH_ERROR";
726         case BLUETOOTH_ERROR_PARING_FAILED:
727                 return "PARING_FAILED";
728         case BLUETOOTH_ERROR_NOT_PAIRED:
729                 return "NOT_PAIRED";
730         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
731                 return "SERVICE_NOT_FOUND";
732         case BLUETOOTH_ERROR_NOT_CONNECTED:
733                 return "NOT_CONNECTED";
734         case BLUETOOTH_ERROR_ALREADY_CONNECT:
735                 return "ALREADY_CONNECT";
736         case BLUETOOTH_ERROR_CONNECTION_BUSY:
737                 return "CONNECTION_BUSY";
738         case BLUETOOTH_ERROR_CONNECTION_ERROR:
739                 return "CONNECTION_ERROR";
740         case BLUETOOTH_ERROR_MAX_CONNECTION:
741                 return "MAX_CONNECTION";
742         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
743                 return "NOT_IN_OPERATION";
744         case BLUETOOTH_ERROR_CANCEL_BY_USER:
745                 return "CANCEL_BY_USER";
746         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
747                 return "REGISTRATION_FAILED";
748         case BLUETOOTH_ERROR_IN_PROGRESS:
749                 return "IN_PROGRESS";
750         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
751                 return "AUTHENTICATION_FAILED";
752         case BLUETOOTH_ERROR_HOST_DOWN:
753                 return "HOST_DOWN";
754         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
755                 return "END_OF_DEVICE_LIST";
756         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
757                 return "AGENT_ALREADY_EXIST";
758         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
759                 return "AGENT_DOES_NOT_EXIST";
760         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
761                 return "ALREADY_INITIALIZED";
762         case BLUETOOTH_ERROR_PERMISSION_DEINED:
763                 return "PERMISSION_DEINED";
764         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
765                 return "ALREADY_DEACTIVATED";
766         case BLUETOOTH_ERROR_NOT_INITIALIZED:
767                 return "NOT_INITIALIZED";
768         case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
769                 return "AUTHENTICATION REJECTED";
770         default:
771                 return "UNKNOWN";
772         }
773 }
774
775 char * _bt_convert_disc_reason_to_string(int reason)
776 {
777         switch(reason) {
778         case (int)BLUETOOTH_ERROR_PAGE_TIMEOUT:
779                 return "Page timeout";
780         case (int)BLUETOOTH_ERROR_CONNECTION_TIMEOUT:
781                 return "Link loss";
782         case (int)BLUETOOTH_ERROR_PIN_OR_KEY_MISSING:
783                 return "PIN or Key missing";
784         case (int)BLUETOOTH_ERROR_LOCAL_HOST_TERM:
785                 return "Connection terminated by local host";
786         case (int)BLUETOOTH_ERROR_REMOTE_USER_TERM:
787         case (int)BLUETOOTH_ERROR_REMOTE_LOW_RESOURCES:
788         case (int)BLUETOOTH_ERROR_REMOTE_POWER_OFF:
789                 return "Remote user terminated connection";
790         case (int)BLUETOOTH_ERROR_AUTH_FAILURE:
791                 return "Authentication Failure";
792         case (int)BLUETOOTH_ERROR_REPEATED_ATTEMPTS:
793                 return "Repeated attempts";
794         case (int)BLUETOOTH_ERROR_LMP_RESPONSE_TIMEOUT:
795                 return "LMP response timeout";
796         case (int)BLUETOOTH_ERROR_CONNECTION_FAILED_TO_BE_ESTABLISHED:
797                 return "Connection failed to be established";
798         default:
799                 return "Unknown";
800         }
801 }
802
803 void _bt_logging_connection(gboolean connect, int addr_type)
804 {
805         static int le_conn = 0;
806         static int le_disc = 0;
807         static int edr_conn = 0;
808         static int edr_disc = 0;
809
810         if (connect) {
811                 if (addr_type)
812                         le_conn++;
813                 else
814                         edr_conn++;
815         } else {
816                 if (addr_type)
817                         le_disc++;
818                 else
819                         edr_disc++;
820         }
821
822         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
823                         le_conn, le_disc, edr_conn, edr_disc);
824 }
825
826 void _bt_swap_byte_ordering(char *data, int data_len)
827 {
828         char temp;
829         int i, j;
830         int half = data_len / 2;
831
832         ret_if(data == NULL);
833         /* Swap to opposite endian */
834         for (i = 0, j = data_len - 1; i < half; i++, j--) {
835                 temp = data[i];
836                 data[i] = data[j];
837                 data[j] = temp;
838         }
839 }
840
841 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
842 {
843         int i;
844
845         retv_if(data1 == NULL, -1);
846         retv_if(data2 == NULL, -1);
847         for (i = 0; i < data_len; i++) {
848                 if (data1[i] != data2[i])
849                         return data1[i] - data2[i];
850                 }
851         return 0;
852 }
853 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
854         const char *mask, int data_len)
855 {
856         int i;
857         char a, b;
858
859         retv_if(data1 == NULL, -1);
860         retv_if(data2 == NULL, -1);
861         retv_if(mask == NULL, -1);
862         for (i = 0; i < data_len; i++) {
863                 a = data1[i] & mask[i];
864                 b = data2[i] & mask[i];
865                 if (a != b)
866                         return (int)(a - b);
867                 }
868         return 0;
869 }
870
871 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
872 {
873         int ret;
874         bundle *b = NULL;
875
876         b = bundle_create();
877
878         bundle_add_str(b, key, value);
879
880         ret = eventsystem_send_system_event(event, b);
881
882         BT_DBG("eventsystem_send_system_event result: %d", ret);
883
884         bundle_free(b);
885
886         return ret;
887 }
888
889 void __bt_get_auth_info(GVariant *reply,  char *auth_info)
890 {
891         int cursor;
892         GVariant *tmp_value;
893         char *manufacturer_data = NULL;
894         int manufacturer_data_len;
895         gboolean is_alias_set;
896         GVariantIter *value_iter;
897         guint8 m_value;
898         int i = 0;
899
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 }