Not enable BT on booting time in factory mode
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / 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 <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <dlog.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <termios.h>
26 #include <net_connection.h>
27 #include <dbus/dbus.h>
28 #include <glib.h>
29 #include <dlog.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <termios.h>
33 #include <net_connection.h>
34 #include <bundle.h>
35 #include <eventsystem.h>
36 #include <arpa/inet.h>
37 #include <vconf.h>
38
39 #include "bluetooth-api.h"
40 #include "bt-service-common.h"
41
42 #include <oal-manager.h>
43 #include <oal-device-mgr.h>
44
45 #ifdef TIZEN_FEATURE_BT_IPSP
46 static GDBusProxy *ipsp_proxy;
47 #endif
48 static GDBusConnection *system_conn;
49 static GDBusConnection *session_conn;
50 static GDBusProxy *manager_proxy_g;
51 static GDBusProxy *adapter_proxy_g;
52 static void *net_conn;
53
54 static GDBusProxy *adapter_properties_proxy;
55
56 static GDBusConnection *system_gconn = NULL;
57
58 GDBusConnection *_bt_gdbus_init_system_gconn(void)
59 {
60         GError *error = NULL;
61
62         dbus_threads_init_default();
63
64         if (system_gconn != NULL)
65                 return system_gconn;
66
67         system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
68
69         if (!system_gconn) {
70                 BT_ERR("Unable to connect to dbus: %s", error->message);
71                 g_clear_error(&error);
72         }
73
74         return system_gconn;
75 }
76
77 GDBusConnection *_bt_gdbus_get_system_gconn(void)
78 {
79         GDBusConnection *local_system_gconn = NULL;
80         GError *error = NULL;
81
82         if (system_gconn == NULL) {
83                 system_gconn = _bt_gdbus_init_system_gconn();
84         } else if (g_dbus_connection_is_closed(system_gconn)) {
85
86                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
87
88                 if (!local_system_gconn) {
89                         BT_ERR("Unable to connect to dbus: %s", error->message);
90                         g_clear_error(&error);
91                 }
92
93                 system_gconn = local_system_gconn;
94         }
95
96         return system_gconn;
97 }
98
99 static GDBusProxy *__bt_init_manager_proxy(void)
100 {
101         GDBusProxy *proxy;
102
103         if (system_conn == NULL) {
104                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
105                 retv_if(system_conn == NULL, NULL);
106         }
107
108         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
109                                                                 NULL, BT_BLUEZ_NAME,
110                                                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE,  NULL, NULL);
111
112         retv_if(proxy == NULL, NULL);
113
114         manager_proxy_g = proxy;
115
116         return proxy;
117 }
118
119 static GDBusProxy *__bt_init_adapter_proxy(void)
120 {
121         GDBusProxy *manager_proxy;
122         GDBusProxy *proxy;
123         char *adapter_path = NULL;
124
125         if (system_conn == NULL) {
126                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
127                 retv_if(system_conn == NULL, NULL);
128         }
129
130         manager_proxy = _bt_get_manager_proxy();
131         retv_if(manager_proxy == NULL, NULL);
132
133         adapter_path = _bt_get_adapter_path();
134         retv_if(adapter_path == NULL, NULL);
135
136         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
137                                                                 NULL, BT_BLUEZ_NAME,
138                                                                 adapter_path, BT_ADAPTER_INTERFACE,  NULL, NULL);
139
140         g_free(adapter_path);
141
142         retv_if(proxy == NULL, NULL);
143
144         adapter_proxy_g = proxy;
145
146         return proxy;
147 }
148 #ifdef TIZEN_FEATURE_BT_IPSP
149 static GDBusProxy *__bt_init_ipsp_proxy(void)
150 {
151         BT_DBG("+");
152
153         GDBusConnection *g_conn;
154         GDBusProxy *proxy;
155         g_conn = _bt_gdbus_get_system_gconn();
156         retv_if(g_conn == NULL, NULL);
157         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
158                                                                 NULL, BT_IPSP_NAME,
159                                                                 "/org/projectx/bt_ipsp", BT_IPSP_INTERFACE,  NULL, NULL);
160         if (!proxy) {
161                 BT_ERR("Unable to get proxy");
162                 return NULL;
163         }
164         ipsp_proxy = proxy;
165         BT_DBG("-");
166         return proxy;
167 }
168 #endif
169 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
170 {
171         GDBusProxy *manager_proxy;
172         GDBusProxy *proxy;
173         char *adapter_path = NULL;
174
175         if (system_conn == NULL) {
176                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
177                 retv_if(system_conn == NULL, NULL);
178         }
179
180         manager_proxy = _bt_get_manager_proxy();
181         retv_if(manager_proxy == NULL, NULL);
182
183         adapter_path = _bt_get_adapter_path();
184         retv_if(adapter_path == NULL, NULL);
185
186         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
187                                                                         NULL, BT_BLUEZ_NAME,
188                                                                         adapter_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
189
190         g_free(adapter_path);
191
192         retv_if(proxy == NULL, NULL);
193
194         adapter_properties_proxy = proxy;
195
196         return proxy;
197 }
198
199 GDBusConnection *__bt_init_system_gconn(void)
200 {
201         if (system_conn == NULL)
202                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
203
204         return system_conn;
205 }
206
207 GDBusConnection *__bt_init_session_conn(void)
208 {
209         if (session_conn == NULL)
210                 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
211
212         return session_conn;
213 }
214
215 GDBusConnection *_bt_get_session_gconn(void)
216 {
217         return (session_conn) ? session_conn : __bt_init_session_conn();
218 }
219
220 GDBusConnection *_bt_get_system_gconn(void)
221 {
222         return (system_conn) ? system_conn : __bt_init_system_gconn();
223 }
224
225 GDBusConnection *_bt_get_system_conn(void)
226 {
227         GDBusConnection *g_conn;
228
229         if (system_conn == NULL)
230                 g_conn = __bt_init_system_gconn();
231         else
232                 g_conn = system_conn;
233
234         retv_if(g_conn == NULL, NULL);
235
236         return g_conn;
237 }
238
239 GDBusProxy *_bt_get_manager_proxy(void)
240 {
241         if (manager_proxy_g) {
242                 const gchar *path =  g_dbus_proxy_get_object_path(manager_proxy_g);
243                 if (path == NULL) {
244                         BT_ERR("Already proxy released hence creating new proxy");
245                         return  __bt_init_manager_proxy();
246                 }
247                 return manager_proxy_g;
248         }
249         return  __bt_init_manager_proxy();
250 }
251 #ifdef TIZEN_FEATURE_BT_IPSP
252 GDBusProxy *_bt_get_ipsp_proxy(void)
253 {
254         if (ipsp_proxy) {
255                 const char *path =  g_dbus_proxy_get_object_path(ipsp_proxy);
256                 if (path == NULL) {
257                         BT_ERR("Already proxy released hence creating new proxy");
258                         return  __bt_init_ipsp_proxy();
259                 }
260                 return ipsp_proxy;
261         }
262         return  __bt_init_ipsp_proxy();
263 }
264 #endif
265
266 static void *__bt_init_net_conn(void)
267 {
268         int result;
269         connection_h connection = NULL;
270
271         if (net_conn == NULL) {
272                 result = connection_create(&connection);
273
274         if (result != CONNECTION_ERROR_NONE ||
275                                         connection == NULL) {
276                 BT_DBG("connection_create() failed: %d", result);
277                 net_conn = NULL;
278                 return NULL;
279         }
280                 net_conn = connection;
281         }
282         return net_conn;
283 }
284
285 void *_bt_get_net_conn(void)
286 {
287         return (net_conn) ? net_conn : __bt_init_net_conn();
288 }
289
290 GDBusProxy *_bt_get_adapter_proxy(void)
291 {
292         if (adapter_proxy_g) {
293                 const char *path =  g_dbus_proxy_get_object_path(adapter_proxy_g);
294                 if (path == NULL) {
295                         BT_ERR("Already proxy released hence creating new proxy");
296                         return  __bt_init_adapter_proxy();
297                 }
298
299                 return adapter_proxy_g;
300         }
301         return  __bt_init_adapter_proxy();
302
303 }
304
305 GDBusProxy *_bt_get_adapter_properties_proxy(void)
306 {
307         return (adapter_properties_proxy) ? adapter_properties_proxy :
308                                         __bt_init_adapter_properties_proxy();
309 }
310
311 static char *__bt_extract_adapter_path(GVariantIter *iter)
312 {
313         char *object_path = NULL;
314         GVariantIter *interface_iter;
315         GVariantIter *svc_iter;
316         char *interface_str = NULL;
317
318         /* Parse the signature: oa{sa{sv}}} */
319         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
320                         &interface_iter)) {
321
322                 if (object_path == NULL)
323                         continue;
324
325                 while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
326                                 &interface_str, &svc_iter)) {
327                         if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
328                                 continue;
329
330                         BT_DBG("Object Path: %s", object_path);
331                         g_variant_iter_free(svc_iter);
332                         g_variant_iter_free(interface_iter);
333                         return g_strdup(object_path);
334                 }
335         }
336         return NULL;
337 }
338
339 char *_bt_get_adapter_path(void)
340 {
341         GDBusConnection *conn;
342         GDBusProxy *manager_proxy;
343         GVariant *result = NULL;
344         GVariantIter *iter = NULL;
345         char *adapter_path = NULL;
346
347         conn = _bt_get_system_conn();
348         retv_if(conn == NULL, NULL);
349
350         manager_proxy = _bt_get_manager_proxy();
351         retv_if(manager_proxy == NULL, NULL);
352
353         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
354                                 NULL,
355                                 G_DBUS_CALL_FLAGS_NONE,
356                                 -1,
357                                 NULL,
358                                 NULL);
359         if (!result) {
360                 BT_ERR("Can't get managed objects");
361                 return NULL;
362         }
363
364         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
365         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
366
367         adapter_path = __bt_extract_adapter_path(iter);
368         g_variant_iter_free(iter);
369         g_variant_unref(result);
370         return adapter_path;
371 }
372
373 void _bt_deinit_bluez_proxy(void)
374 {
375         if (manager_proxy_g) {
376                 g_object_unref(manager_proxy_g);
377                 manager_proxy_g = NULL;
378         }
379
380         if (adapter_proxy_g) {
381                 g_object_unref(adapter_proxy_g);
382                 adapter_proxy_g = NULL;
383         }
384         if (adapter_properties_proxy) {
385                 g_object_unref(adapter_properties_proxy);
386                 adapter_properties_proxy = NULL;
387         }
388 }
389
390 void _bt_deinit_proxys(void)
391 {
392         int ret;
393         _bt_deinit_bluez_proxy();
394
395         if (system_conn) {
396                 g_object_unref(system_conn);
397                 system_conn = NULL;
398         }
399
400         if (session_conn) {
401                 g_object_unref(session_conn);
402                 session_conn = NULL;
403         }
404
405         if (net_conn) {
406                 ret = connection_destroy(net_conn);
407                 net_conn = NULL;
408                 if (ret != 0)
409                         BT_ERR("connection_destroy failed : %d", ret);
410         }
411 }
412
413 void _bt_convert_device_path_to_address(const char *device_path,
414                                                 char *device_address)
415 {
416         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
417         char *dev_addr;
418
419         ret_if(device_path == NULL);
420         ret_if(device_address == NULL);
421
422         dev_addr = strstr(device_path, "dev_");
423         if (dev_addr != NULL) {
424                 char *pos = NULL;
425                 dev_addr += 4;
426                 g_strlcpy(address, dev_addr, sizeof(address));
427
428                 while ((pos = strchr(address, '_')) != NULL)
429                         *pos = ':';
430
431                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
432         }
433 }
434
435
436 void _bt_convert_addr_string_to_type(unsigned char *addr,
437                                         const char *address)
438 {
439         int i;
440         char *ptr = NULL;
441
442         ret_if(address == NULL);
443         ret_if(addr == NULL);
444
445         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
446                 addr[i] = strtol(address, &ptr, 16);
447                 if (ptr[0] != '\0') {
448                         if (ptr[0] != ':')
449                                 return;
450
451                         address = ptr + 1;
452                 }
453         }
454 }
455
456 void _bt_convert_addr_type_to_string(char *address,
457                                 unsigned char *addr)
458 {
459         ret_if(address == NULL);
460         ret_if(addr == NULL);
461
462         snprintf(address, BT_ADDRESS_STRING_SIZE,
463                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
464                         addr[0], addr[1], addr[2],
465                         addr[3], addr[4], addr[5]);
466 }
467
468 gboolean _bt_compare_adddress(const bluetooth_device_address_t *addr1,
469                 const bluetooth_device_address_t *addr2)
470 {
471         if (memcmp(&addr1->addr, &addr2->addr, 6) == 0)
472                 return TRUE;
473         else
474                 return FALSE;
475 }
476
477 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
478 {
479         BT_INFO("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
480                 addr->addr[0], addr->addr[1], addr->addr[2],
481                 addr->addr[3], addr->addr[4], addr->addr[5]);
482 }
483
484 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
485                                 unsigned int cod)
486 {
487         ret_if(device_class == NULL);
488
489         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
490         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
491         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
492
493         if (cod & 0x002000) {
494                 device_class->service_class |=
495                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
496         }
497 }
498
499 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
500 {
501         ret_if(le_dev_info == NULL);
502
503         g_free(le_dev_info->adv_data);
504         g_free(le_dev_info);
505 }
506
507 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
508 {
509         unsigned int i;
510         const char *p = src;
511         char *next;
512         int count;
513
514         if (dest == NULL || src == NULL)
515                 return BLUETOOTH_ERROR_INVALID_PARAM;
516
517         BT_DBG("+src : %s", src);
518         BT_DBG("+dest : %s", dest);
519
520         i = 0;
521         while (*p != '\0' && i < length) {
522                 next = g_utf8_next_char(p);
523                 count = next - p;
524
525                 while (count > 0 && ((i + count) < length)) {
526                         dest[i++] = *p;
527                         p++;
528                         count--;
529                 }
530                 p = next;
531         }
532         return BLUETOOTH_ERROR_NONE;
533 }
534
535 gboolean _bt_utf8_validate(char *name)
536 {
537         BT_DBG("+");
538         gunichar2 *u16;
539         glong items_written = 0;
540
541         if (FALSE == g_utf8_validate(name, -1, NULL))
542                 return FALSE;
543
544         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
545         if (u16 == NULL)
546                 return FALSE;
547
548         g_free(u16);
549
550         if (items_written != g_utf8_strlen(name, -1))
551                 return FALSE;
552
553         BT_DBG("-");
554         return TRUE;
555 }
556
557 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
558 {
559         BT_DBG("+");
560
561 #if TODO_40 /* Need to add this function */
562         if (!_bt_agent_register_osp_server(type, uuid, path, fd))
563                 return BLUETOOTH_ERROR_INTERNAL;
564 #else
565         if (device_register_osp_server(type, uuid, path, fd) != 0)
566                 return BLUETOOTH_ERROR_INTERNAL;
567 #endif
568         return BLUETOOTH_ERROR_NONE;
569 }
570
571 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
572 {
573         BT_DBG("+");
574
575 #if TODO_40 /* Need to add this function */
576         if (!_bt_agent_unregister_osp_server(type, uuid))
577                 return BLUETOOTH_ERROR_INTERNAL;
578 #else
579         if (device_unregister_osp_server(type, uuid) != 0)
580                 return BLUETOOTH_ERROR_INTERNAL;
581 #endif
582         return BLUETOOTH_ERROR_NONE;
583 }
584
585 int _bt_set_socket_non_blocking(int socket_fd)
586 {
587         /* Set Nonblocking */
588         long arg;
589
590         arg = fcntl(socket_fd, F_GETFL);
591
592         if (arg < 0)
593                 return -errno;
594
595         if (arg & O_NONBLOCK)
596                 BT_ERR("Already Non-blocking \n");
597
598         arg |= O_NONBLOCK;
599
600         if (fcntl(socket_fd, F_SETFL, arg) < 0)
601                 return -errno;
602
603         return BLUETOOTH_ERROR_NONE;
604 }
605
606 int _bt_set_non_blocking_tty(int sk)
607 {
608         struct termios ti = {0,};
609         int err;
610
611         err = _bt_set_socket_non_blocking(sk);
612
613         if (err < 0) {
614                 BT_ERR("Error in set non blocking!\n");
615                 return err;
616         }
617
618         tcflush(sk, TCIOFLUSH);
619
620         /* Switch tty to RAW mode */
621         cfmakeraw(&ti);
622         tcsetattr(sk, TCSANOW, &ti);
623
624         return BLUETOOTH_ERROR_NONE;
625 }
626
627 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
628 {
629         char *object_path = NULL;
630         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
631
632         /* Parse the signature: oa{sa{sv}}} */
633         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
634                         NULL)) {
635                 retv_if(object_path == NULL, NULL);
636                 _bt_convert_device_path_to_address(object_path, device_address);
637                 if (g_strcmp0(address, device_address) == 0)
638                         return g_strdup(object_path);
639         }
640         return NULL;
641 }
642
643 char *_bt_get_device_object_path(char *address)
644 {
645         char *object_path = NULL;
646         GDBusConnection *conn;
647         GDBusProxy *manager_proxy;
648         GVariant *result = NULL;
649         GVariantIter *iter = NULL;
650
651         conn = _bt_get_system_conn();
652         retv_if(conn == NULL, NULL);
653
654         manager_proxy = _bt_get_manager_proxy();
655         retv_if(manager_proxy == NULL, NULL);
656
657         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
658                                 NULL,
659                                 G_DBUS_CALL_FLAGS_NONE,
660                                 -1,
661                                 NULL,
662                                 NULL);
663         if (!result) {
664                 BT_ERR("Can't get managed objects");
665                 return NULL;
666         }
667
668         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
669         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
670         object_path = __bt_extract_device_path(iter, address);
671         g_variant_iter_free(iter);
672         g_variant_unref(result);
673         return object_path;
674 }
675
676 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
677 {
678         switch (profile_type) {
679         case BT_PROFILE_CONN_RFCOMM:
680                 return strdup(RFCOMM_UUID_STR);
681         case BT_PROFILE_CONN_A2DP:
682                 return strdup(A2DP_SINK_UUID);
683         case BT_PROFILE_CONN_A2DP_SINK:
684                 return strdup(A2DP_SOURCE_UUID);
685         case BT_PROFILE_CONN_HSP:
686                 return strdup(HFP_HS_UUID);
687         case BT_PROFILE_CONN_HID:
688                 return strdup(HID_UUID);
689         case BT_PROFILE_CONN_NAP:
690                 return strdup(NAP_UUID);
691         case BT_PROFILE_CONN_HFG:
692                 return strdup(HFP_AG_UUID);
693         case BT_PROFILE_CONN_GATT:
694         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
695         default:
696                 return NULL;
697         };
698 }
699
700 const char *_bt_convert_uuid_to_string(const char *uuid)
701 {
702 #define SHORT_UUID_COUNT        199
703 #define LONG_UUID_COUNT         17
704
705         if (!uuid)
706                 return NULL;
707
708         int offset = 0;
709         int uuid_len = 4;
710         typedef struct {
711                 const char *uuid;
712                 const char *specification_name;
713         } uuid_name_s;
714         static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = {
715                 // List should be sorted by UUID
716                 /* BT Classic Services */
717                 {"1101", "Serial Port Service"},
718                 {"1102", "LAN Access Using PPP Service"},
719                 {"1103", "Dialup Networking Service"},
720                 {"1104", "IrMCSync Service"},
721                 {"1105", "OBEX Object Push Service"},
722                 {"1106", "OBEX File Transfer Service"},
723                 {"1107", "IrMC Sync Command Service"},
724                 {"1108", "Headset Service"},
725                 {"1109", "Cordless Telephony Service"},
726                 {"110A", "Audio Source Service"},
727                 {"110B", "Audio Sink Service"},
728                 {"110C", "AV Remote Control Target Service"},
729                 {"110D", "Advanced Audio Distribution Profile"},
730                 {"110E", "AV Remote Control Service"},
731                 {"110F", "Video Conferencing Service"},
732                 {"1110", "Intercom Service"},
733                 {"1111", "Fax Service"},
734                 {"1112", "Headset Audio Gateway Service"},
735                 {"1113", "WAP Service"},
736                 {"1114", "WAP Client Service"},
737                 {"1115", "PANU Service"},
738                 {"1116", "NAP Service"},
739                 {"1117", "GN Service"},
740                 {"1118", "Direct Printing Service"},
741                 {"1119", "Reference Printing Service"},
742                 {"111A", "Basic Imaging Profile"},
743                 {"111B", "Imaging Responder Service"},
744                 {"111C", "Imaging Automatic Archive Service"},
745                 {"111D", "Imaging Reference Objects Service"},
746                 {"111E", "Handsfree Service"},
747                 {"111F", "Handsfree Audio Gateway Service"},
748                 {"1120", "Direct Printing Reference Objects Service"},
749                 {"1121", "Reflected UI Service"},
750                 {"1122", "Basic Printing Profile"},
751                 {"1123", "Printing Status Service"},
752                 {"1124", "Human Interface Device Service"},
753                 {"1125", "Hardcopy Cable Replacement Profile"},
754                 {"1126", "HCR Print Service"},
755                 {"1127", "HCR Scan Service"},
756                 {"112D", "SIM Access Service"},
757                 {"112E", "Phonebook Access PCE Service"},
758                 {"112F", "Phonebook Access PSE Service"},
759                 {"1130", "Phonebook Access Profile"},
760                 {"1132", "Message Access Server Service"},
761                 {"1133", "Message Notification Server Service"},
762                 {"1134", "Message Access Profile"},
763                 {"1200", "PnP Information Service"},
764                 {"1201", "Generic Networking Service"},
765                 {"1202", "Generic File Transfer Service"},
766                 {"1203", "Generic Audio Service"},
767                 {"1204", "Generic Telephony Service"},
768                 {"1205", "UPnP Service"},
769                 {"1206", "UPnP Ip Service"},
770                 {"1303", "Video Source Service"},
771                 {"1304", "Video Sink Service"},
772                 {"1305", "Video Distribution Profile"},
773                 {"1400", "Health Device Profile"},
774                 {"1401", "HDP Source Service"},
775                 {"1402", "HDP Sink Service"},
776
777                 /* GATT Services */
778                 {"1800", "Generic Access"},
779                 {"1801", "Generic Attribute"},
780                 {"1802", "Immediate Alert"},
781                 {"1803", "Link Loss"},
782                 {"1804", "Tx Power"},
783                 {"1805", "Current Time Service"},
784                 {"1806", "Reference Time Update Service"},
785                 {"1807", "Next DST Change Service"},
786                 {"1808", "Glucose"},
787                 {"1809", "Health Thermometer"},
788                 {"180A", "Device Information"},
789                 {"180D", "Heart Rate"},
790                 {"180F", "Battery Service"},
791                 {"1810", "Blood Pressure"},
792                 {"1811", "Alert Notification Service"},
793                 {"1812", "Human Interface Device"},
794                 {"1813", "Scan Parameters"},
795                 {"1814", "Running Speed and Cadence"},
796                 {"1815", "Automation IO"},
797                 {"1816", "Cycling Speed and Cadence"},
798                 {"1818", "Cycling Power"},
799                 {"1819", "Location and Navigation"},
800                 {"181A", "Environmental Sensing"},
801                 {"181B", "Body Composition"},
802                 {"181C", "User Data"},
803                 {"181D", "Weight Scale"},
804                 {"181E", "Bond Management"},
805                 {"181F", "Continuous Glucose Monitoring"},
806                 {"1820", "Internet Protocol Support Service"},
807                 {"1821", "Indoor Positioning"},
808                 {"1822", "Pulse Oximeter Service"},
809                 {"1823", "HTTP Proxy"},
810                 {"1824", "Transport Discovery"},
811                 {"1825", "Object Transfer Service"},
812                 {"1826", "Fitness Machine"},
813                 {"1827", "Mesh Provisioning Service"},
814                 {"1828", "Mesh Proxy Service"},
815                 {"1829", "Reconnection Configuration"},
816                 {"183A", "Insulin Delivery"},
817
818                 /* GATT Declarations */
819                 {"2800", "Primary Service Declaration"},
820                 {"2801", "Secondary Service Declaration"},
821                 {"2802", "Include Declaration"},
822                 {"2803", "Characteristic Declaration"},
823
824                 /* GATT Descriptors */
825                 {"2900", "Characteristic Extended Properties"},
826                 {"2901", "Characteristic User Description"},
827                 {"2902", "Client Characteristic Configuration"},
828                 {"2903", "Server Characteristic Configuration"},
829                 {"2904", "Characteristic Format"},
830                 {"2905", "Characteristic Aggregate Formate"},
831                 {"2906", "Valid Range"},
832                 {"2907", "External Report Reference"},
833                 {"2908", "Report Reference"},
834
835                 /* GATT Characteristics */
836                 {"2A00", "Device Name"},
837                 {"2A01", "Appearance"},
838                 {"2A02", "Peripheral Privacy Flag"},
839                 {"2A03", "Reconnection Address"},
840                 {"2A04", "Peripheral Preferred Connection Parameters"},
841                 {"2A05", "Service Changed"},
842                 {"2A06", "Alert Level"},
843                 {"2A07", "Tx Power Level"},
844                 {"2A08", "Date Time"},
845                 {"2A09", "Day of Week"},
846                 {"2A0A", "Day Date Time"},
847                 {"2A11", "Time with DST"},
848                 {"2A12", "Time Accuracy"},
849                 {"2A13", "Time Source"},
850                 {"2A14", "Reference Time Information"},
851                 {"2A16", "Time Update Control Point"},
852                 {"2A17", "Time Update State"},
853                 {"2A18", "Glucose Measurement"},
854                 {"2A19", "Battery Level"},
855                 {"2A1C", "Temperature Measurement"},
856                 {"2A1D", "Temperature Type"},
857                 {"2A1E", "Intermediate Temperature"},
858                 {"2A21", "Measurement Interval"},
859                 {"2A23", "System ID"},
860                 {"2A24", "Model Number String"},
861                 {"2A25", "Serial Number String"},
862                 {"2A26", "Firmware Revision String"},
863                 {"2A27", "Hardware Revision String"},
864                 {"2A28", "Software Revision String"},
865                 {"2A29", "Manufacturer Name String"},
866                 {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
867                 {"2A2B", "Current Time"},
868                 {"2A34", "Glucose Measurement Context"},
869                 {"2A35", "Blood Pressure Measurement"},
870                 {"2A37", "Heart Rate Measurement"},
871                 {"2A38", "Body Sensor Location"},
872                 {"2A39", "Heart Rate Control Point"},
873                 {"2A3F", "Alert Status"},
874                 {"2A46", "New Alert"},
875                 {"2A49", "Blood Pressure Feature"},
876                 {"2A4A", "HID Information"},
877                 {"2A4C", "HID Control Point"},
878                 {"2A50", "PnP ID"},
879                 {"2A51", "Glucose Feature"},
880                 {"2A52", "Record Access Control Point"},
881                 {"2A53", "RSC Measurement"},
882                 {"2A54", "RSC Feature"},
883                 {"2A55", "SC Control Point"},
884                 {"2A56", "Digital"},
885                 {"2A58", "Analog"},
886                 {"2A5A", "Aggregate"},
887                 {"2A5B", "CSC Measurement"},
888                 {"2A5C", "CSC Feature"},
889                 {"2A5D", "Sensor Location"},
890                 {"2A63", "Cycling Power Measurement"},
891                 {"2A64", "Cycling Power Vector"},
892                 {"2A65", "Cycling Power Feature"},
893                 {"2A66", "Cycling Power Control Point"},
894                 {"2A67", "Location and Speed"},
895                 {"2A68", "Navigation"},
896                 {"2A6D", "Pressure"},
897                 {"2A6E", "Temperature"},
898                 {"2A8E", "Height"},
899                 {"2A90", "Last Name"},
900                 {"2A91", "Maximum Recommended Heart Rate"},
901                 {"2A92", "Resting Heart Rate"},
902                 {"2A98", "Weight"},
903                 {"2A9B", "Body Composition Feature"},
904                 {"2A9C", "Body Composition Measurement"},
905                 {"2A9D", "Weight Measurement"},
906                 {"2AA2", "Language"},
907                 {"2AA4", "Bond Management Control Point"},
908                 {"2AA5", "Bond Management Features"},
909                 {"2AA6", "Central Address Resolution"},
910                 {"2AAD", "Indoor Positioning Configuration"},
911                 {"2AB5", "Location Name"},
912                 {"2AB6", "URI"},
913                 {"2ABC", "TDS Control Point"},
914                 {"2AC9", "Resolvable Private Address Only"},
915                 {"2ACC", "Fitness Machine Feature"},
916                 {"2ACE", "Cross Trainer Data"},
917                 {"2AD3", "Training Status"},
918                 {"2AD7", "Supported Heart Rate Range"},
919                 {"2AD9", "Fitness Machine Control Point"},
920                 {"2ADA", "Fitness Machine Status"},
921                 {"2B1D", "RC Feature"},
922                 {"2B1E", "RC Settings"},
923                 {"2B1F", "Reconnection Configuration Control Point"},
924         };
925         static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = {
926                 // List should be sorted by UUID
927                 /* Custom uuids */
928                 {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"},
929                 {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
930                 {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
931                 {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
932                 {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
933                 {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
934                 {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
935                 {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
936                 {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
937                 {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
938                 {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"},
939                 {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
940                 {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
941                 {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"},
942                 {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
943                 {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"},
944                 {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
945         };
946         const uuid_name_s *uuid_name = short_uuid_name;
947         static const char *unknown_name = "Unknown";
948         int start = 0;
949         int end = SHORT_UUID_COUNT - 1;
950         int p;
951         int ret;
952
953         if (strlen(uuid) == 36) {
954                 if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
955                         offset = 4;
956                 else {
957                         offset = 0;
958                         uuid_len = 36;
959                         end = LONG_UUID_COUNT - 1;
960                         uuid_name = long_uuid_name;
961                 }
962         } else if (strlen(uuid) >= 8)
963                 offset = 4;
964
965         while (start <= end) {
966                 p = start + (end - start) / 2;
967                 ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len);
968                 if (ret == 0)
969                         return uuid_name[p].specification_name;
970                 else if (ret < 0)
971                         end = p - 1;
972                 else
973                         start = p + 1;
974         }
975
976         return unknown_name;
977 }
978
979 const char *_bt_convert_error_to_string(int error)
980 {
981         switch (error) {
982         case BLUETOOTH_ERROR_CANCEL:
983                 return "CANCELLED";
984         case BLUETOOTH_ERROR_INVALID_PARAM:
985                 return "INVALID_PARAMETER";
986         case BLUETOOTH_ERROR_INVALID_DATA:
987                 return "INVALID DATA";
988         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
989         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
990                 return "OUT_OF_MEMORY";
991         case BLUETOOTH_ERROR_TIMEOUT:
992                 return "TIMEOUT";
993         case BLUETOOTH_ERROR_NO_RESOURCES:
994                 return "NO_RESOURCES";
995         case BLUETOOTH_ERROR_INTERNAL:
996                 return "INTERNAL";
997         case BLUETOOTH_ERROR_NOT_SUPPORT:
998                 return "NOT_SUPPORT";
999         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
1000                 return "NOT_ENABLED";
1001         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
1002                 return "ALREADY_ENABLED";
1003         case BLUETOOTH_ERROR_DEVICE_BUSY:
1004                 return "DEVICE_BUSY";
1005         case BLUETOOTH_ERROR_ACCESS_DENIED:
1006                 return "ACCESS_DENIED";
1007         case BLUETOOTH_ERROR_MAX_CLIENT:
1008                 return "MAX_CLIENT";
1009         case BLUETOOTH_ERROR_NOT_FOUND:
1010                 return "NOT_FOUND";
1011         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
1012                 return "SERVICE_SEARCH_ERROR";
1013         case BLUETOOTH_ERROR_PARING_FAILED:
1014                 return "PARING_FAILED";
1015         case BLUETOOTH_ERROR_NOT_PAIRED:
1016                 return "NOT_PAIRED";
1017         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
1018                 return "SERVICE_NOT_FOUND";
1019         case BLUETOOTH_ERROR_NOT_CONNECTED:
1020                 return "NOT_CONNECTED";
1021         case BLUETOOTH_ERROR_ALREADY_CONNECT:
1022                 return "ALREADY_CONNECT";
1023         case BLUETOOTH_ERROR_CONNECTION_BUSY:
1024                 return "CONNECTION_BUSY";
1025         case BLUETOOTH_ERROR_CONNECTION_ERROR:
1026                 return "CONNECTION_ERROR";
1027         case BLUETOOTH_ERROR_MAX_CONNECTION:
1028                 return "MAX_CONNECTION";
1029         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
1030                 return "NOT_IN_OPERATION";
1031         case BLUETOOTH_ERROR_CANCEL_BY_USER:
1032                 return "CANCEL_BY_USER";
1033         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
1034                 return "REGISTRATION_FAILED";
1035         case BLUETOOTH_ERROR_IN_PROGRESS:
1036                 return "IN_PROGRESS";
1037         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
1038                 return "AUTHENTICATION_FAILED";
1039         case BLUETOOTH_ERROR_HOST_DOWN:
1040                 return "HOST_DOWN";
1041         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
1042                 return "END_OF_DEVICE_LIST";
1043         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
1044                 return "AGENT_ALREADY_EXIST";
1045         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
1046                 return "AGENT_DOES_NOT_EXIST";
1047         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
1048                 return "ALREADY_INITIALIZED";
1049         case BLUETOOTH_ERROR_PERMISSION_DEINED:
1050                 return "PERMISSION_DEINED";
1051         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
1052                 return "ALREADY_DEACTIVATED";
1053         case BLUETOOTH_ERROR_NOT_INITIALIZED:
1054                 return "NOT_INITIALIZED";
1055         default:
1056                 return "UNKNOWN";
1057         }
1058 }
1059
1060 const char * _bt_convert_disc_reason_to_string(int reason)
1061 {
1062         switch (reason) {
1063         case 1:
1064                 return "Link loss";
1065         case 2:
1066                 return "Connection terminated by local host";
1067         case 3:
1068                 return "Remote user terminated connection";
1069         case 0:
1070         default:
1071                 return "Unknown";
1072         }
1073 }
1074
1075
1076 void _bt_logging_connection(gboolean connect, int addr_type)
1077 {
1078         static int le_conn = 0;
1079         static int le_disc = 0;
1080         static int edr_conn = 0;
1081         static int edr_disc = 0;
1082
1083         if (connect) {
1084                 if (addr_type)
1085                         le_conn++;
1086                 else
1087                         edr_conn++;
1088         } else {
1089                 if (addr_type)
1090                         le_disc++;
1091                 else
1092                         edr_disc++;
1093         }
1094
1095         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
1096                         le_conn, le_disc, edr_conn, edr_disc);
1097 }
1098
1099 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
1100 {
1101         int ret;
1102         bundle *b = NULL;
1103
1104         b = bundle_create();
1105
1106         bundle_add_str(b, key, value);
1107
1108         ret = eventsystem_send_system_event(event, b);
1109
1110         BT_DBG("eventsystem_send_system_event result: %d", ret);
1111
1112         bundle_free(b);
1113
1114         return ret;
1115 }
1116
1117 void _bt_swap_byte_ordering(char *data, int data_len)
1118 {
1119         char temp;
1120         int i, j;
1121
1122         ret_if(data == NULL);
1123         /* Swap to opposite endian */
1124         for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
1125                 temp = data[i];
1126                 data[i] = data[j];
1127                 data[j] = temp;
1128                 }
1129 }
1130
1131 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
1132 {
1133         int i;
1134
1135         retv_if(data1 == NULL, -1);
1136         retv_if(data2 == NULL, -1);
1137         for (i = 0; i < data_len; i++) {
1138                 if (data1[i] != data2[i])
1139                         return data1[i] - data2[i];
1140                 }
1141         return 0;
1142 }
1143 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
1144         const char *mask, int data_len)
1145 {
1146         int i;
1147         char a, b;
1148
1149         retv_if(data1 == NULL, -1);
1150         retv_if(data2 == NULL, -1);
1151         retv_if(mask == NULL, -1);
1152         for (i = 0; i < data_len; i++) {
1153                 a = data1[i] & mask[i];
1154                 b = data2[i] & mask[i];
1155                 if (a != b)
1156                         return (int)(a - b);
1157                 }
1158         return 0;
1159 }
1160
1161 void _bt_copy_remote_dev(bt_remote_dev_info_t *dev_info, remote_device_t *oal_device)
1162 {
1163         unsigned int i;
1164
1165         dev_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
1166         _bt_convert_addr_type_to_string(dev_info->address, oal_device->address.addr);
1167         BT_INFO("Address [%s]", dev_info->address);
1168
1169         if (strlen(oal_device->name) == 0)
1170                 dev_info->name = NULL;
1171         else {
1172                 dev_info->name = g_strdup(oal_device->name);
1173                 _bt_truncate_non_utf8_chars(dev_info->name);
1174                 BT_INFO("Name [%s]", dev_info->name);
1175         }
1176
1177         if (strlen(oal_device->alias) == 0) {
1178                 dev_info->alias = NULL;
1179         } else {
1180                 dev_info->alias = g_strdup(oal_device->alias);
1181                 _bt_truncate_non_utf8_chars(dev_info->alias);
1182                 BT_INFO("Alias [%s]", dev_info->alias);
1183         }
1184
1185         dev_info->class = oal_device->cod;
1186         dev_info->paired = oal_device->is_bonded;
1187         BT_INFO("Is Bonded [%d]", dev_info->paired);
1188         dev_info->connected = oal_device->is_connected;
1189         BT_INFO("iS Connected [%d]", dev_info->connected);
1190         dev_info->rssi = oal_device->rssi;
1191         dev_info->addr_type = oal_device->type;
1192         dev_info->uuid_count = oal_device->uuid_count;
1193         BT_INFO("UUID Count [%d]", dev_info->uuid_count);
1194         dev_info->trust = ((oal_device->is_trusted == 0) ? FALSE : TRUE);
1195
1196         if (dev_info->uuid_count > 0)
1197                 dev_info->uuids = g_new0(char *, dev_info->uuid_count);
1198
1199         /* Fill Remote Device Service List list */
1200         for (i = 0; i < dev_info->uuid_count; i++) {
1201                 dev_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
1202                 _bt_uuid_to_string((service_uuid_t *)&oal_device->uuid[i].uuid, dev_info->uuids[i]);
1203         }
1204
1205         dev_info->vid = (unsigned short)oal_device->vid;
1206         dev_info->pid = (unsigned short)oal_device->pid;
1207         dev_info->device_type = (int)oal_device->type;
1208         dev_info->is_alias_set = ((oal_device->is_alias_set == 0) ? FALSE : TRUE);
1209
1210 }
1211
1212 void _bt_free_remote_dev(bt_remote_dev_info_t *dev_info)
1213 {
1214         unsigned int c;
1215
1216         ret_if(NULL == dev_info);
1217
1218         g_free(dev_info->address);
1219         g_free(dev_info->name);
1220         g_free(dev_info->alias);
1221         g_free(dev_info->manufacturer_data);
1222
1223         for (c = 0; c < dev_info->uuid_count; c++)
1224                 g_free(dev_info->uuids[c]);
1225         g_free(dev_info->uuids);
1226         g_free(dev_info);
1227
1228 }
1229
1230 static void __bt_get_service_list(bt_remote_dev_info_t *info, bluetooth_device_info_t *dev)
1231 {
1232         unsigned int i;
1233         char **uuids;
1234         char **parts;
1235
1236         ret_if(info == NULL);
1237         ret_if(dev == NULL);
1238
1239         uuids = info->uuids;
1240         if (uuids == NULL) {
1241                 BT_ERR("No UUID's");
1242                 return;
1243         }
1244
1245         dev->service_index = 0;
1246         BT_DBG("Total UUID count [%d]", info->uuid_count);
1247         for (i = 0; i < info->uuid_count; i++) {
1248                 g_strlcpy(dev->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
1249
1250                 parts = g_strsplit(uuids[i], "-", -1);
1251
1252                 if (parts == NULL || parts[0] == NULL)
1253                         break;
1254
1255                 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
1256                 g_strfreev(parts);
1257
1258                 dev->service_index++;
1259         }
1260
1261 }
1262
1263 void _bt_copy_remote_device(bt_remote_dev_info_t *rem_dev, bluetooth_device_info_t *dev)
1264 {
1265         memset(dev, 0x00, sizeof(bluetooth_device_info_t));
1266         __bt_get_service_list(rem_dev, dev);
1267         _bt_convert_addr_string_to_type(dev->device_address.addr, rem_dev->address);
1268         _bt_divide_device_class(&dev->device_class, rem_dev->class);
1269
1270         if (rem_dev->alias) {
1271                 g_strlcpy(dev->device_name.name, rem_dev->alias,
1272                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1273                 BT_DBG("Name: %s", dev->device_name.name);
1274         } else {
1275                 g_strlcpy(dev->device_name.name, rem_dev->name,
1276                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1277                 BT_DBG("Name: %s", dev->device_name.name);
1278         }
1279
1280         dev->rssi = rem_dev->rssi;
1281         dev->trust = rem_dev->trust;
1282         dev->paired = rem_dev->paired;
1283         dev->connected = rem_dev->connected;
1284         dev->is_alias_set = rem_dev->is_alias_set;
1285
1286         /* Fill Manufacturer data */
1287         if (rem_dev->manufacturer_data_len > 0) {
1288                 dev->manufacturer_data.data_len = rem_dev->manufacturer_data_len;
1289                 memcpy(dev->manufacturer_data.data,
1290                         rem_dev->manufacturer_data, rem_dev->manufacturer_data_len);
1291         } else {
1292                 dev->manufacturer_data.data_len = 0;
1293         }
1294
1295 }
1296
1297 void _bt_service_print_dev_info(bluetooth_device_info_t *dev_info)
1298 {
1299         int i;
1300
1301         ret_if(dev_info == NULL);
1302
1303         _bt_print_device_address_t(&(dev_info->device_address));
1304         BT_INFO("Device Name:[%s]", dev_info->device_name.name);
1305         BT_INFO("Device Major Class:[0x%X]", dev_info->device_class.major_class);
1306         BT_INFO("Device Minor Class:[0x%X]", dev_info->device_class.minor_class);
1307         BT_INFO("Device Service Class:[0x%X]", dev_info->device_class.minor_class);
1308         BT_INFO("Device Paired:[%s]", (dev_info->paired ? "TRUE" : "FALSE"));
1309         BT_INFO("Device Trusted:[%s]", (dev_info->trust ? "TRUE" : "FALSE"));
1310         BT_INFO("Device Connected:[%d]", dev_info->connected);
1311         BT_INFO("Device Service index:[%d]", dev_info->service_index);
1312         for (i = 0; i < dev_info->service_index; i++) {
1313                 BT_INFO("Device Service List:[%d]", dev_info->service_list_array[i]);
1314                 BT_INFO("Device UUID:[%s]", dev_info->uuids[i]);
1315         }
1316
1317         BT_INFO("Device manufacturer data len:[%d]", dev_info->manufacturer_data.data_len);
1318         for (i = 0; i < dev_info->manufacturer_data.data_len; i++)
1319                 BT_INFO("%2.2X", dev_info->manufacturer_data.data[i]);
1320 }
1321
1322 void _bt_uuid_to_string(service_uuid_t *p_uuid, char *str)
1323 {
1324         uint32_t uuid0, uuid4;
1325         uint16_t uuid1, uuid2, uuid3, uuid5;
1326
1327         memcpy(&uuid0, &(p_uuid->uuid[0]), 4);
1328         memcpy(&uuid1, &(p_uuid->uuid[4]), 2);
1329         memcpy(&uuid2, &(p_uuid->uuid[6]), 2);
1330         memcpy(&uuid3, &(p_uuid->uuid[8]), 2);
1331         memcpy(&uuid4, &(p_uuid->uuid[10]), 4);
1332         memcpy(&uuid5, &(p_uuid->uuid[14]), 2);
1333
1334         snprintf((char *)str, BLUETOOTH_UUID_STRING_MAX, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
1335                         ntohl(uuid0), ntohs(uuid1),
1336                         ntohs(uuid2), ntohs(uuid3),
1337                         ntohl(uuid4), ntohs(uuid5));
1338         return;
1339 }
1340
1341 void _bt_string_to_uuid(char *str, service_uuid_t *p_uuid)
1342 {
1343         uint32_t uuid0, uuid4;
1344         uint16_t uuid1, uuid2, uuid3, uuid5;
1345
1346         sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1347                         &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
1348
1349         uuid0 = htonl(uuid0);
1350         uuid1 = htons(uuid1);
1351         uuid2 = htons(uuid2);
1352         uuid3 = htons(uuid3);
1353         uuid4 = htonl(uuid4);
1354         uuid5 = htons(uuid5);
1355
1356         memcpy(&(p_uuid->uuid[0]), &uuid0, 4);
1357         memcpy(&(p_uuid->uuid[4]), &uuid1, 2);
1358         memcpy(&(p_uuid->uuid[6]), &uuid2, 2);
1359         memcpy(&(p_uuid->uuid[8]), &uuid3, 2);
1360         memcpy(&(p_uuid->uuid[10]), &uuid4, 4);
1361         memcpy(&(p_uuid->uuid[14]), &uuid5, 2);
1362
1363         return;
1364 }
1365
1366 void _bt_convert_byte_ordering(char *data, int data_len,
1367                 char *converted_data)
1368 {
1369         int i, j;
1370         char temp[BT_UUID_LEN];
1371
1372         memcpy(&temp, data, data_len);
1373         for (i = 0, j = data_len - 1; i < data_len; i++, j--)
1374                 converted_data[i] = temp[j];
1375
1376 }
1377
1378 /* Trim string at first non-utf8 char */
1379 void _bt_truncate_non_utf8_chars(char * str)
1380 {
1381         guint i = 0;
1382         const char *ptr = NULL;
1383
1384         if (strlen(str) != 0) {
1385                 if (!g_utf8_validate(str, -1, &ptr)) {
1386                         while (*(str + i) != *ptr)
1387                                 i++;
1388                         *(str + i) = '\0';
1389                 }
1390         }
1391 }
1392
1393 invocation_info_t* _bt_get_request_info_data(int service_function, char *address)
1394 {
1395         GSList *l;
1396         invocation_info_t *req_info = NULL;
1397
1398         BT_DBG("+");
1399
1400         retv_if(NULL == address, NULL);
1401
1402         /* Get method invocation context */
1403         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1404                 req_info = l->data;
1405                 if (req_info == NULL || req_info->service_function != service_function)
1406                         continue;
1407
1408                 if (!strncasecmp((char *)req_info->user_data, address, BT_ADDRESS_STRING_SIZE))
1409                         return req_info;
1410         }
1411
1412         BT_DBG("-");
1413         return NULL;
1414 }
1415
1416 #ifdef TIZEN_GATT_CLIENT
1417 invocation_info_t* _bt_get_request_info_data_from_function_name(int service_function)
1418 {
1419         GSList *l;
1420         invocation_info_t *req_info = NULL;
1421
1422         /* Get method invocation context */
1423         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1424                 req_info = l->data;
1425                 if (req_info == NULL || req_info->service_function != service_function)
1426                         continue;
1427                 return req_info;
1428         }
1429
1430         BT_DBG("Not existed");
1431         return NULL;
1432 }
1433 #endif
1434
1435 void _bt_set_device_values(gboolean connected, int state)
1436 {
1437         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1438
1439         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1440                 BT_ERR("vconf_get_int failed");
1441
1442         if (connected == TRUE)
1443                 bt_device_state |= state;
1444         else if (bt_device_state & state)
1445                 bt_device_state ^= state;
1446
1447         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
1448                 BT_ERR("vconf_set_int failed");
1449 }
1450
1451 int _bt_get_ad_data_by_type(char *in_data, int in_len,
1452                 char in_type, char **data, int *data_len)
1453 {
1454         if (in_data == NULL || data == NULL || data_len == NULL)
1455                 return BLUETOOTH_ERROR_INVALID_PARAM;
1456
1457         if (in_len < 0)
1458                 return BLUETOOTH_ERROR_INVALID_PARAM;
1459
1460         int i;
1461         int len = 0;
1462         int type = 0;
1463
1464         for (i = 0; i < in_len; i++) {
1465                 len = in_data[i];
1466                 if (len <= 0 || i + 1 >= in_len) {
1467                         BT_ERR("Invalid advertising data");
1468                         return BLUETOOTH_ERROR_INVALID_PARAM;
1469                 }
1470
1471                 type = in_data[i + 1];
1472                 if (type == in_type) {
1473                         i = i + 2;
1474                         len--;
1475                         break;
1476                 }
1477
1478                 i += len;
1479                 len = 0;
1480         }
1481
1482         if (i + len > in_len) {
1483                 BT_ERR("Invalid advertising data");
1484                 return BLUETOOTH_ERROR_INVALID_PARAM;
1485         } else if (len == 0) {
1486                 BT_INFO("AD Type 0x%02x data is not set", in_type);
1487                 *data = NULL;
1488                 *data_len = 0;
1489                 return BLUETOOTH_ERROR_NONE;
1490         }
1491
1492         *data = g_memdup(&in_data[i], len);
1493         if (*data == NULL)
1494                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
1495         *data_len = len;
1496
1497         return BLUETOOTH_ERROR_NONE;
1498 }
1499
1500 void _bt_copy_remote_dev_info(bt_remote_dev_info_t *dest, bt_remote_dev_info_t *src)
1501 {
1502         ret_if(NULL == src);
1503
1504         if (!dest->address) {
1505                 dest->address = g_strdup(src->address);
1506         }
1507
1508         if (src->name) {
1509                 g_free(dest->name);
1510                 dest->name = g_strdup(src->name);
1511         }
1512
1513         if (src->alias) {
1514                 g_free(dest->alias);
1515                 dest->alias = g_strdup(src->alias);
1516         }
1517
1518         if (src->uuid_count > 0 && src->uuids) {
1519                 int i;
1520
1521                 /* Release previously allocated UUIDs */
1522                 if (dest->uuid_count > 0 && dest->uuids)
1523                         for (i = 0; i < dest->uuid_count; i++)
1524                                 g_free(dest->uuids[i]);
1525                 g_free(dest->uuids);
1526
1527                 /* Copy new UUID list */
1528                 dest->uuids = g_malloc0(src->uuid_count * sizeof(char *));
1529                 for (i = 0; i < src->uuid_count; i++)
1530                         dest->uuids[i] = g_strdup(src->uuids[i]);
1531
1532                 dest->uuid_count = src->uuid_count;
1533         }
1534
1535         if (src->manufacturer_data_len > 0 && src->manufacturer_data) {
1536                 if (dest->manufacturer_data)
1537                         g_free(dest->manufacturer_data);
1538
1539                 dest->manufacturer_data_len = src->manufacturer_data_len;
1540                 dest->manufacturer_data = g_memdup(
1541                         src->manufacturer_data, src->manufacturer_data_len);
1542         }
1543
1544         dest->rssi = src->rssi;
1545         dest->class = src->class;
1546         dest->paired = src->paired;
1547         dest->connected = src->connected;
1548         dest->trust = src->trust;
1549         dest->addr_type = src->addr_type;
1550         dest->vid = src->vid;
1551         dest->pid = src->pid;
1552         dest->device_type = src->device_type;
1553         dest->is_alias_set = src->is_alias_set;
1554
1555 }
1556
1557 #define BT_PERMANENT_LOG_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log.txt"
1558 #define BT_PERMANENT_LOG_OLD_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log_old.txt"
1559 #define PERMANENT_LOG_MAX (128 * 1024)
1560 void _bt_permanent_log(const char *fmt, ...)
1561 {
1562         FILE *fp;
1563         time_t t;
1564         struct tm *lt;
1565         char time_buf[32];
1566         long lsize = 0;
1567         va_list ap;
1568
1569 #ifndef TIZEN_FEATURE_BT_PERMANENT_LOG
1570         return;
1571 #endif
1572
1573         t = time(&t);
1574         lt = localtime(&t);
1575         strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", lt);
1576
1577         fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "r");
1578         if (fp != NULL) {
1579                 fseek(fp, 0, SEEK_END);
1580                 lsize = ftell(fp);
1581         }
1582
1583         if (lsize > PERMANENT_LOG_MAX) {
1584                 FILE *fp2;
1585                 int character;
1586                 fp2 = fopen(BT_PERMANENT_LOG_OLD_FILE_PATH, "w");
1587                 if (fp2 == NULL)
1588                         goto DONE;
1589                 fseek(fp, 0, SEEK_SET);
1590                 while (!feof(fp)) {
1591                         character = fgetc(fp);
1592                         fputc(character, fp2);
1593                 }
1594                 fclose(fp);
1595                 fclose(fp2);
1596                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "w");
1597         } else {
1598                 if (fp != NULL)
1599                         fclose(fp);
1600                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "a");
1601         }
1602
1603         if (fp != NULL) {
1604                 char buf[100];
1605                 va_start(ap, fmt);
1606                 vsnprintf(buf, 100, fmt, ap);
1607                 va_end(ap);
1608                 fprintf(fp, "%s %s\n", time_buf, buf);
1609         }
1610
1611 DONE:
1612         if (fp != NULL)
1613                 fclose(fp);
1614 }
1615