d6ed3b73b38b809dce59bd002f166e27849ffd73
[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         gunichar2 *u16;
538         glong items_written = 0;
539
540         if (FALSE == g_utf8_validate(name, -1, NULL))
541                 return FALSE;
542
543         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
544         if (u16 == NULL)
545                 return FALSE;
546
547         g_free(u16);
548
549         if (items_written != g_utf8_strlen(name, -1))
550                 return FALSE;
551
552         return TRUE;
553 }
554
555 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
556 {
557         BT_DBG("+");
558
559 #if TODO_40 /* Need to add this function */
560         if (!_bt_agent_register_osp_server(type, uuid, path, fd))
561                 return BLUETOOTH_ERROR_INTERNAL;
562 #else
563         if (device_register_osp_server(type, uuid, path, fd) != 0)
564                 return BLUETOOTH_ERROR_INTERNAL;
565 #endif
566         return BLUETOOTH_ERROR_NONE;
567 }
568
569 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
570 {
571         BT_DBG("+");
572
573 #if TODO_40 /* Need to add this function */
574         if (!_bt_agent_unregister_osp_server(type, uuid))
575                 return BLUETOOTH_ERROR_INTERNAL;
576 #else
577         if (device_unregister_osp_server(type, uuid) != 0)
578                 return BLUETOOTH_ERROR_INTERNAL;
579 #endif
580         return BLUETOOTH_ERROR_NONE;
581 }
582
583 int _bt_set_socket_non_blocking(int socket_fd)
584 {
585         /* Set Nonblocking */
586         long arg;
587
588         arg = fcntl(socket_fd, F_GETFL);
589
590         if (arg < 0)
591                 return -errno;
592
593         if (arg & O_NONBLOCK)
594                 BT_ERR("Already Non-blocking \n");
595
596         arg |= O_NONBLOCK;
597
598         if (fcntl(socket_fd, F_SETFL, arg) < 0)
599                 return -errno;
600
601         return BLUETOOTH_ERROR_NONE;
602 }
603
604 int _bt_set_non_blocking_tty(int sk)
605 {
606         struct termios ti = {0,};
607         int err;
608
609         err = _bt_set_socket_non_blocking(sk);
610
611         if (err < 0) {
612                 BT_ERR("Error in set non blocking!\n");
613                 return err;
614         }
615
616         tcflush(sk, TCIOFLUSH);
617
618         /* Switch tty to RAW mode */
619         cfmakeraw(&ti);
620         tcsetattr(sk, TCSANOW, &ti);
621
622         return BLUETOOTH_ERROR_NONE;
623 }
624
625 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
626 {
627         char *object_path = NULL;
628         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
629
630         /* Parse the signature: oa{sa{sv}}} */
631         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
632                         NULL)) {
633                 retv_if(object_path == NULL, NULL);
634                 _bt_convert_device_path_to_address(object_path, device_address);
635                 if (g_strcmp0(address, device_address) == 0)
636                         return g_strdup(object_path);
637         }
638         return NULL;
639 }
640
641 char *_bt_get_device_object_path(char *address)
642 {
643         char *object_path = NULL;
644         GDBusConnection *conn;
645         GDBusProxy *manager_proxy;
646         GVariant *result = NULL;
647         GVariantIter *iter = NULL;
648
649         conn = _bt_get_system_conn();
650         retv_if(conn == NULL, NULL);
651
652         manager_proxy = _bt_get_manager_proxy();
653         retv_if(manager_proxy == NULL, NULL);
654
655         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
656                                 NULL,
657                                 G_DBUS_CALL_FLAGS_NONE,
658                                 -1,
659                                 NULL,
660                                 NULL);
661         if (!result) {
662                 BT_ERR("Can't get managed objects");
663                 return NULL;
664         }
665
666         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
667         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
668         object_path = __bt_extract_device_path(iter, address);
669         g_variant_iter_free(iter);
670         g_variant_unref(result);
671         return object_path;
672 }
673
674 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
675 {
676         switch (profile_type) {
677         case BT_PROFILE_CONN_RFCOMM:
678                 return strdup(RFCOMM_UUID_STR);
679         case BT_PROFILE_CONN_A2DP:
680                 return strdup(A2DP_SINK_UUID);
681         case BT_PROFILE_CONN_A2DP_SINK:
682                 return strdup(A2DP_SOURCE_UUID);
683         case BT_PROFILE_CONN_HSP:
684                 return strdup(HFP_HS_UUID);
685         case BT_PROFILE_CONN_HID:
686                 return strdup(HID_UUID);
687         case BT_PROFILE_CONN_NAP:
688                 return strdup(NAP_UUID);
689         case BT_PROFILE_CONN_HFG:
690                 return strdup(HFP_AG_UUID);
691         case BT_PROFILE_CONN_GATT:
692         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
693         default:
694                 return NULL;
695         };
696 }
697
698 const char *_bt_convert_uuid_to_string(const char *uuid)
699 {
700 #define SHORT_UUID_COUNT        199
701 #define LONG_UUID_COUNT         17
702
703         if (!uuid)
704                 return NULL;
705
706         int offset = 0;
707         int uuid_len = 4;
708         typedef struct {
709                 const char *uuid;
710                 const char *specification_name;
711         } uuid_name_s;
712         static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = {
713                 // List should be sorted by UUID
714                 /* BT Classic Services */
715                 {"1101", "Serial Port Service"},
716                 {"1102", "LAN Access Using PPP Service"},
717                 {"1103", "Dialup Networking Service"},
718                 {"1104", "IrMCSync Service"},
719                 {"1105", "OBEX Object Push Service"},
720                 {"1106", "OBEX File Transfer Service"},
721                 {"1107", "IrMC Sync Command Service"},
722                 {"1108", "Headset Service"},
723                 {"1109", "Cordless Telephony Service"},
724                 {"110A", "Audio Source Service"},
725                 {"110B", "Audio Sink Service"},
726                 {"110C", "AV Remote Control Target Service"},
727                 {"110D", "Advanced Audio Distribution Profile"},
728                 {"110E", "AV Remote Control Service"},
729                 {"110F", "Video Conferencing Service"},
730                 {"1110", "Intercom Service"},
731                 {"1111", "Fax Service"},
732                 {"1112", "Headset Audio Gateway Service"},
733                 {"1113", "WAP Service"},
734                 {"1114", "WAP Client Service"},
735                 {"1115", "PANU Service"},
736                 {"1116", "NAP Service"},
737                 {"1117", "GN Service"},
738                 {"1118", "Direct Printing Service"},
739                 {"1119", "Reference Printing Service"},
740                 {"111A", "Basic Imaging Profile"},
741                 {"111B", "Imaging Responder Service"},
742                 {"111C", "Imaging Automatic Archive Service"},
743                 {"111D", "Imaging Reference Objects Service"},
744                 {"111E", "Handsfree Service"},
745                 {"111F", "Handsfree Audio Gateway Service"},
746                 {"1120", "Direct Printing Reference Objects Service"},
747                 {"1121", "Reflected UI Service"},
748                 {"1122", "Basic Printing Profile"},
749                 {"1123", "Printing Status Service"},
750                 {"1124", "Human Interface Device Service"},
751                 {"1125", "Hardcopy Cable Replacement Profile"},
752                 {"1126", "HCR Print Service"},
753                 {"1127", "HCR Scan Service"},
754                 {"112D", "SIM Access Service"},
755                 {"112E", "Phonebook Access PCE Service"},
756                 {"112F", "Phonebook Access PSE Service"},
757                 {"1130", "Phonebook Access Profile"},
758                 {"1132", "Message Access Server Service"},
759                 {"1133", "Message Notification Server Service"},
760                 {"1134", "Message Access Profile"},
761                 {"1200", "PnP Information Service"},
762                 {"1201", "Generic Networking Service"},
763                 {"1202", "Generic File Transfer Service"},
764                 {"1203", "Generic Audio Service"},
765                 {"1204", "Generic Telephony Service"},
766                 {"1205", "UPnP Service"},
767                 {"1206", "UPnP Ip Service"},
768                 {"1303", "Video Source Service"},
769                 {"1304", "Video Sink Service"},
770                 {"1305", "Video Distribution Profile"},
771                 {"1400", "Health Device Profile"},
772                 {"1401", "HDP Source Service"},
773                 {"1402", "HDP Sink Service"},
774
775                 /* GATT Services */
776                 {"1800", "Generic Access"},
777                 {"1801", "Generic Attribute"},
778                 {"1802", "Immediate Alert"},
779                 {"1803", "Link Loss"},
780                 {"1804", "Tx Power"},
781                 {"1805", "Current Time Service"},
782                 {"1806", "Reference Time Update Service"},
783                 {"1807", "Next DST Change Service"},
784                 {"1808", "Glucose"},
785                 {"1809", "Health Thermometer"},
786                 {"180A", "Device Information"},
787                 {"180D", "Heart Rate"},
788                 {"180F", "Battery Service"},
789                 {"1810", "Blood Pressure"},
790                 {"1811", "Alert Notification Service"},
791                 {"1812", "Human Interface Device"},
792                 {"1813", "Scan Parameters"},
793                 {"1814", "Running Speed and Cadence"},
794                 {"1815", "Automation IO"},
795                 {"1816", "Cycling Speed and Cadence"},
796                 {"1818", "Cycling Power"},
797                 {"1819", "Location and Navigation"},
798                 {"181A", "Environmental Sensing"},
799                 {"181B", "Body Composition"},
800                 {"181C", "User Data"},
801                 {"181D", "Weight Scale"},
802                 {"181E", "Bond Management"},
803                 {"181F", "Continuous Glucose Monitoring"},
804                 {"1820", "Internet Protocol Support Service"},
805                 {"1821", "Indoor Positioning"},
806                 {"1822", "Pulse Oximeter Service"},
807                 {"1823", "HTTP Proxy"},
808                 {"1824", "Transport Discovery"},
809                 {"1825", "Object Transfer Service"},
810                 {"1826", "Fitness Machine"},
811                 {"1827", "Mesh Provisioning Service"},
812                 {"1828", "Mesh Proxy Service"},
813                 {"1829", "Reconnection Configuration"},
814                 {"183A", "Insulin Delivery"},
815
816                 /* GATT Declarations */
817                 {"2800", "Primary Service Declaration"},
818                 {"2801", "Secondary Service Declaration"},
819                 {"2802", "Include Declaration"},
820                 {"2803", "Characteristic Declaration"},
821
822                 /* GATT Descriptors */
823                 {"2900", "Characteristic Extended Properties"},
824                 {"2901", "Characteristic User Description"},
825                 {"2902", "Client Characteristic Configuration"},
826                 {"2903", "Server Characteristic Configuration"},
827                 {"2904", "Characteristic Format"},
828                 {"2905", "Characteristic Aggregate Formate"},
829                 {"2906", "Valid Range"},
830                 {"2907", "External Report Reference"},
831                 {"2908", "Report Reference"},
832
833                 /* GATT Characteristics */
834                 {"2A00", "Device Name"},
835                 {"2A01", "Appearance"},
836                 {"2A02", "Peripheral Privacy Flag"},
837                 {"2A03", "Reconnection Address"},
838                 {"2A04", "Peripheral Preferred Connection Parameters"},
839                 {"2A05", "Service Changed"},
840                 {"2A06", "Alert Level"},
841                 {"2A07", "Tx Power Level"},
842                 {"2A08", "Date Time"},
843                 {"2A09", "Day of Week"},
844                 {"2A0A", "Day Date Time"},
845                 {"2A11", "Time with DST"},
846                 {"2A12", "Time Accuracy"},
847                 {"2A13", "Time Source"},
848                 {"2A14", "Reference Time Information"},
849                 {"2A16", "Time Update Control Point"},
850                 {"2A17", "Time Update State"},
851                 {"2A18", "Glucose Measurement"},
852                 {"2A19", "Battery Level"},
853                 {"2A1C", "Temperature Measurement"},
854                 {"2A1D", "Temperature Type"},
855                 {"2A1E", "Intermediate Temperature"},
856                 {"2A21", "Measurement Interval"},
857                 {"2A23", "System ID"},
858                 {"2A24", "Model Number String"},
859                 {"2A25", "Serial Number String"},
860                 {"2A26", "Firmware Revision String"},
861                 {"2A27", "Hardware Revision String"},
862                 {"2A28", "Software Revision String"},
863                 {"2A29", "Manufacturer Name String"},
864                 {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
865                 {"2A2B", "Current Time"},
866                 {"2A34", "Glucose Measurement Context"},
867                 {"2A35", "Blood Pressure Measurement"},
868                 {"2A37", "Heart Rate Measurement"},
869                 {"2A38", "Body Sensor Location"},
870                 {"2A39", "Heart Rate Control Point"},
871                 {"2A3F", "Alert Status"},
872                 {"2A46", "New Alert"},
873                 {"2A49", "Blood Pressure Feature"},
874                 {"2A4A", "HID Information"},
875                 {"2A4C", "HID Control Point"},
876                 {"2A50", "PnP ID"},
877                 {"2A51", "Glucose Feature"},
878                 {"2A52", "Record Access Control Point"},
879                 {"2A53", "RSC Measurement"},
880                 {"2A54", "RSC Feature"},
881                 {"2A55", "SC Control Point"},
882                 {"2A56", "Digital"},
883                 {"2A58", "Analog"},
884                 {"2A5A", "Aggregate"},
885                 {"2A5B", "CSC Measurement"},
886                 {"2A5C", "CSC Feature"},
887                 {"2A5D", "Sensor Location"},
888                 {"2A63", "Cycling Power Measurement"},
889                 {"2A64", "Cycling Power Vector"},
890                 {"2A65", "Cycling Power Feature"},
891                 {"2A66", "Cycling Power Control Point"},
892                 {"2A67", "Location and Speed"},
893                 {"2A68", "Navigation"},
894                 {"2A6D", "Pressure"},
895                 {"2A6E", "Temperature"},
896                 {"2A8E", "Height"},
897                 {"2A90", "Last Name"},
898                 {"2A91", "Maximum Recommended Heart Rate"},
899                 {"2A92", "Resting Heart Rate"},
900                 {"2A98", "Weight"},
901                 {"2A9B", "Body Composition Feature"},
902                 {"2A9C", "Body Composition Measurement"},
903                 {"2A9D", "Weight Measurement"},
904                 {"2AA2", "Language"},
905                 {"2AA4", "Bond Management Control Point"},
906                 {"2AA5", "Bond Management Features"},
907                 {"2AA6", "Central Address Resolution"},
908                 {"2AAD", "Indoor Positioning Configuration"},
909                 {"2AB5", "Location Name"},
910                 {"2AB6", "URI"},
911                 {"2ABC", "TDS Control Point"},
912                 {"2AC9", "Resolvable Private Address Only"},
913                 {"2ACC", "Fitness Machine Feature"},
914                 {"2ACE", "Cross Trainer Data"},
915                 {"2AD3", "Training Status"},
916                 {"2AD7", "Supported Heart Rate Range"},
917                 {"2AD9", "Fitness Machine Control Point"},
918                 {"2ADA", "Fitness Machine Status"},
919                 {"2B1D", "RC Feature"},
920                 {"2B1E", "RC Settings"},
921                 {"2B1F", "Reconnection Configuration Control Point"},
922         };
923         static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = {
924                 // List should be sorted by UUID
925                 /* Custom uuids */
926                 {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"},
927                 {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
928                 {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
929                 {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
930                 {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
931                 {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
932                 {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
933                 {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
934                 {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
935                 {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
936                 {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"},
937                 {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
938                 {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
939                 {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"},
940                 {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
941                 {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"},
942                 {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
943         };
944         const uuid_name_s *uuid_name = short_uuid_name;
945         static const char *unknown_name = "Unknown";
946         int start = 0;
947         int end = SHORT_UUID_COUNT - 1;
948         int p;
949         int ret;
950
951         if (strlen(uuid) == 36) {
952                 if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
953                         offset = 4;
954                 else {
955                         offset = 0;
956                         uuid_len = 36;
957                         end = LONG_UUID_COUNT - 1;
958                         uuid_name = long_uuid_name;
959                 }
960         } else if (strlen(uuid) >= 8)
961                 offset = 4;
962
963         while (start <= end) {
964                 p = start + (end - start) / 2;
965                 ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len);
966                 if (ret == 0)
967                         return uuid_name[p].specification_name;
968                 else if (ret < 0)
969                         end = p - 1;
970                 else
971                         start = p + 1;
972         }
973
974         return unknown_name;
975 }
976
977 const char *_bt_convert_error_to_string(int error)
978 {
979         switch (error) {
980         case BLUETOOTH_ERROR_CANCEL:
981                 return "CANCELLED";
982         case BLUETOOTH_ERROR_INVALID_PARAM:
983                 return "INVALID_PARAMETER";
984         case BLUETOOTH_ERROR_INVALID_DATA:
985                 return "INVALID DATA";
986         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
987         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
988                 return "OUT_OF_MEMORY";
989         case BLUETOOTH_ERROR_TIMEOUT:
990                 return "TIMEOUT";
991         case BLUETOOTH_ERROR_NO_RESOURCES:
992                 return "NO_RESOURCES";
993         case BLUETOOTH_ERROR_INTERNAL:
994                 return "INTERNAL";
995         case BLUETOOTH_ERROR_NOT_SUPPORT:
996                 return "NOT_SUPPORT";
997         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
998                 return "NOT_ENABLED";
999         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
1000                 return "ALREADY_ENABLED";
1001         case BLUETOOTH_ERROR_DEVICE_BUSY:
1002                 return "DEVICE_BUSY";
1003         case BLUETOOTH_ERROR_ACCESS_DENIED:
1004                 return "ACCESS_DENIED";
1005         case BLUETOOTH_ERROR_MAX_CLIENT:
1006                 return "MAX_CLIENT";
1007         case BLUETOOTH_ERROR_NOT_FOUND:
1008                 return "NOT_FOUND";
1009         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
1010                 return "SERVICE_SEARCH_ERROR";
1011         case BLUETOOTH_ERROR_PARING_FAILED:
1012                 return "PARING_FAILED";
1013         case BLUETOOTH_ERROR_NOT_PAIRED:
1014                 return "NOT_PAIRED";
1015         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
1016                 return "SERVICE_NOT_FOUND";
1017         case BLUETOOTH_ERROR_NOT_CONNECTED:
1018                 return "NOT_CONNECTED";
1019         case BLUETOOTH_ERROR_ALREADY_CONNECT:
1020                 return "ALREADY_CONNECT";
1021         case BLUETOOTH_ERROR_CONNECTION_BUSY:
1022                 return "CONNECTION_BUSY";
1023         case BLUETOOTH_ERROR_CONNECTION_ERROR:
1024                 return "CONNECTION_ERROR";
1025         case BLUETOOTH_ERROR_MAX_CONNECTION:
1026                 return "MAX_CONNECTION";
1027         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
1028                 return "NOT_IN_OPERATION";
1029         case BLUETOOTH_ERROR_CANCEL_BY_USER:
1030                 return "CANCEL_BY_USER";
1031         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
1032                 return "REGISTRATION_FAILED";
1033         case BLUETOOTH_ERROR_IN_PROGRESS:
1034                 return "IN_PROGRESS";
1035         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
1036                 return "AUTHENTICATION_FAILED";
1037         case BLUETOOTH_ERROR_HOST_DOWN:
1038                 return "HOST_DOWN";
1039         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
1040                 return "END_OF_DEVICE_LIST";
1041         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
1042                 return "AGENT_ALREADY_EXIST";
1043         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
1044                 return "AGENT_DOES_NOT_EXIST";
1045         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
1046                 return "ALREADY_INITIALIZED";
1047         case BLUETOOTH_ERROR_PERMISSION_DEINED:
1048                 return "PERMISSION_DEINED";
1049         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
1050                 return "ALREADY_DEACTIVATED";
1051         case BLUETOOTH_ERROR_NOT_INITIALIZED:
1052                 return "NOT_INITIALIZED";
1053         default:
1054                 return "UNKNOWN";
1055         }
1056 }
1057
1058 const char * _bt_convert_disc_reason_to_string(int reason)
1059 {
1060         switch (reason) {
1061         case 1:
1062                 return "Link loss";
1063         case 2:
1064                 return "Connection terminated by local host";
1065         case 3:
1066                 return "Remote user terminated connection";
1067         case 0:
1068         default:
1069                 return "Unknown";
1070         }
1071 }
1072
1073
1074 void _bt_logging_connection(gboolean connect, int addr_type)
1075 {
1076         static int le_conn = 0;
1077         static int le_disc = 0;
1078         static int edr_conn = 0;
1079         static int edr_disc = 0;
1080
1081         if (connect) {
1082                 if (addr_type)
1083                         le_conn++;
1084                 else
1085                         edr_conn++;
1086         } else {
1087                 if (addr_type)
1088                         le_disc++;
1089                 else
1090                         edr_disc++;
1091         }
1092
1093         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
1094                         le_conn, le_disc, edr_conn, edr_disc);
1095 }
1096
1097 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
1098 {
1099         int ret;
1100         bundle *b = NULL;
1101
1102         b = bundle_create();
1103
1104         bundle_add_str(b, key, value);
1105
1106         ret = eventsystem_send_system_event(event, b);
1107
1108         BT_DBG("eventsystem_send_system_event result: %d", ret);
1109
1110         bundle_free(b);
1111
1112         return ret;
1113 }
1114
1115 void _bt_swap_byte_ordering(char *data, int data_len)
1116 {
1117         char temp;
1118         int i, j;
1119
1120         ret_if(data == NULL);
1121         /* Swap to opposite endian */
1122         for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
1123                 temp = data[i];
1124                 data[i] = data[j];
1125                 data[j] = temp;
1126                 }
1127 }
1128
1129 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
1130 {
1131         int i;
1132
1133         retv_if(data1 == NULL, -1);
1134         retv_if(data2 == NULL, -1);
1135         for (i = 0; i < data_len; i++) {
1136                 if (data1[i] != data2[i])
1137                         return data1[i] - data2[i];
1138                 }
1139         return 0;
1140 }
1141 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
1142         const char *mask, int data_len)
1143 {
1144         int i;
1145         char a, b;
1146
1147         retv_if(data1 == NULL, -1);
1148         retv_if(data2 == NULL, -1);
1149         retv_if(mask == NULL, -1);
1150         for (i = 0; i < data_len; i++) {
1151                 a = data1[i] & mask[i];
1152                 b = data2[i] & mask[i];
1153                 if (a != b)
1154                         return (int)(a - b);
1155                 }
1156         return 0;
1157 }
1158
1159 void _bt_copy_remote_dev(bt_remote_dev_info_t *dev_info, remote_device_t *oal_device)
1160 {
1161         unsigned int i;
1162
1163         dev_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
1164         _bt_convert_addr_type_to_string(dev_info->address, oal_device->address.addr);
1165         BT_INFO("Address [%s]", dev_info->address);
1166
1167         if (strlen(oal_device->name) == 0)
1168                 dev_info->name = NULL;
1169         else {
1170                 dev_info->name = g_strdup(oal_device->name);
1171                 _bt_truncate_non_utf8_chars(dev_info->name);
1172                 BT_INFO("Name [%s]", dev_info->name);
1173         }
1174
1175         if (strlen(oal_device->alias) == 0) {
1176                 dev_info->alias = NULL;
1177         } else {
1178                 dev_info->alias = g_strdup(oal_device->alias);
1179                 _bt_truncate_non_utf8_chars(dev_info->alias);
1180                 BT_INFO("Alias [%s]", dev_info->alias);
1181         }
1182
1183         dev_info->class = oal_device->cod;
1184         dev_info->paired = oal_device->is_bonded;
1185         BT_INFO("Is Bonded [%d]", dev_info->paired);
1186         dev_info->connected = oal_device->is_connected;
1187         BT_INFO("iS Connected [%d]", dev_info->connected);
1188         dev_info->rssi = oal_device->rssi;
1189         dev_info->addr_type = oal_device->type;
1190         dev_info->uuid_count = oal_device->uuid_count;
1191         BT_INFO("UUID Count [%d]", dev_info->uuid_count);
1192         dev_info->trust = ((oal_device->is_trusted == 0) ? FALSE : TRUE);
1193
1194         if (dev_info->uuid_count > 0)
1195                 dev_info->uuids = g_new0(char *, dev_info->uuid_count);
1196
1197         /* Fill Remote Device Service List list */
1198         for (i = 0; i < dev_info->uuid_count; i++) {
1199                 dev_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
1200                 _bt_uuid_to_string((service_uuid_t *)&oal_device->uuid[i].uuid, dev_info->uuids[i]);
1201         }
1202
1203         dev_info->vid = (unsigned short)oal_device->vid;
1204         dev_info->pid = (unsigned short)oal_device->pid;
1205         dev_info->device_type = (int)oal_device->type;
1206         dev_info->is_alias_set = ((oal_device->is_alias_set == 0) ? FALSE : TRUE);
1207
1208 }
1209
1210 void _bt_free_remote_dev(bt_remote_dev_info_t *dev_info)
1211 {
1212         unsigned int c;
1213
1214         ret_if(NULL == dev_info);
1215
1216         g_free(dev_info->address);
1217         g_free(dev_info->name);
1218         g_free(dev_info->alias);
1219         g_free(dev_info->manufacturer_data);
1220
1221         for (c = 0; c < dev_info->uuid_count; c++)
1222                 g_free(dev_info->uuids[c]);
1223         g_free(dev_info->uuids);
1224         g_free(dev_info);
1225
1226 }
1227
1228 static void __bt_get_service_list(bt_remote_dev_info_t *info, bluetooth_device_info_t *dev)
1229 {
1230         unsigned int i;
1231         char **uuids;
1232         char **parts;
1233
1234         ret_if(info == NULL);
1235         ret_if(dev == NULL);
1236
1237         uuids = info->uuids;
1238         if (uuids == NULL) {
1239                 BT_ERR("No UUID's");
1240                 return;
1241         }
1242
1243         dev->service_index = 0;
1244         BT_DBG("Total UUID count [%d]", info->uuid_count);
1245         for (i = 0; i < info->uuid_count; i++) {
1246                 g_strlcpy(dev->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
1247
1248                 parts = g_strsplit(uuids[i], "-", -1);
1249
1250                 if (parts == NULL || parts[0] == NULL)
1251                         break;
1252
1253                 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
1254                 g_strfreev(parts);
1255
1256                 dev->service_index++;
1257         }
1258
1259 }
1260
1261 void _bt_copy_remote_device(bt_remote_dev_info_t *rem_dev, bluetooth_device_info_t *dev)
1262 {
1263         memset(dev, 0x00, sizeof(bluetooth_device_info_t));
1264         __bt_get_service_list(rem_dev, dev);
1265         _bt_convert_addr_string_to_type(dev->device_address.addr, rem_dev->address);
1266         _bt_divide_device_class(&dev->device_class, rem_dev->class);
1267
1268         if (rem_dev->alias) {
1269                 g_strlcpy(dev->device_name.name, rem_dev->alias,
1270                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1271                 BT_DBG("Name: %s", dev->device_name.name);
1272         } else {
1273                 g_strlcpy(dev->device_name.name, rem_dev->name,
1274                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1275                 BT_DBG("Name: %s", dev->device_name.name);
1276         }
1277
1278         dev->rssi = rem_dev->rssi;
1279         dev->trust = rem_dev->trust;
1280         dev->paired = rem_dev->paired;
1281         dev->connected = rem_dev->connected;
1282         dev->is_alias_set = rem_dev->is_alias_set;
1283
1284         /* Fill Manufacturer data */
1285         if (rem_dev->manufacturer_data_len > 0) {
1286                 dev->manufacturer_data.data_len = rem_dev->manufacturer_data_len;
1287                 memcpy(dev->manufacturer_data.data,
1288                         rem_dev->manufacturer_data, rem_dev->manufacturer_data_len);
1289         } else {
1290                 dev->manufacturer_data.data_len = 0;
1291         }
1292
1293 }
1294
1295 void _bt_service_print_dev_info(bluetooth_device_info_t *dev_info)
1296 {
1297         int i;
1298
1299         ret_if(dev_info == NULL);
1300
1301         _bt_print_device_address_t(&(dev_info->device_address));
1302         BT_INFO("Device Name:[%s]", dev_info->device_name.name);
1303         BT_INFO("Device Major Class:[0x%X]", dev_info->device_class.major_class);
1304         BT_INFO("Device Minor Class:[0x%X]", dev_info->device_class.minor_class);
1305         BT_INFO("Device Service Class:[0x%X]", dev_info->device_class.minor_class);
1306         BT_INFO("Device Paired:[%s]", (dev_info->paired ? "TRUE" : "FALSE"));
1307         BT_INFO("Device Trusted:[%s]", (dev_info->trust ? "TRUE" : "FALSE"));
1308         BT_INFO("Device Connected:[%d]", dev_info->connected);
1309         BT_INFO("Device Service index:[%d]", dev_info->service_index);
1310         for (i = 0; i < dev_info->service_index; i++) {
1311                 BT_INFO("Device Service List:[%d]", dev_info->service_list_array[i]);
1312                 BT_INFO("Device UUID:[%s]", dev_info->uuids[i]);
1313         }
1314
1315         BT_INFO("Device manufacturer data len:[%d]", dev_info->manufacturer_data.data_len);
1316         for (i = 0; i < dev_info->manufacturer_data.data_len; i++)
1317                 BT_INFO("%2.2X", dev_info->manufacturer_data.data[i]);
1318 }
1319
1320 void _bt_uuid_to_string(service_uuid_t *p_uuid, char *str)
1321 {
1322         uint32_t uuid0, uuid4;
1323         uint16_t uuid1, uuid2, uuid3, uuid5;
1324
1325         memcpy(&uuid0, &(p_uuid->uuid[0]), 4);
1326         memcpy(&uuid1, &(p_uuid->uuid[4]), 2);
1327         memcpy(&uuid2, &(p_uuid->uuid[6]), 2);
1328         memcpy(&uuid3, &(p_uuid->uuid[8]), 2);
1329         memcpy(&uuid4, &(p_uuid->uuid[10]), 4);
1330         memcpy(&uuid5, &(p_uuid->uuid[14]), 2);
1331
1332         snprintf((char *)str, BLUETOOTH_UUID_STRING_MAX, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
1333                         ntohl(uuid0), ntohs(uuid1),
1334                         ntohs(uuid2), ntohs(uuid3),
1335                         ntohl(uuid4), ntohs(uuid5));
1336         return;
1337 }
1338
1339 void _bt_string_to_uuid(char *str, service_uuid_t *p_uuid)
1340 {
1341         uint32_t uuid0, uuid4;
1342         uint16_t uuid1, uuid2, uuid3, uuid5;
1343
1344         sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1345                         &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
1346
1347         uuid0 = htonl(uuid0);
1348         uuid1 = htons(uuid1);
1349         uuid2 = htons(uuid2);
1350         uuid3 = htons(uuid3);
1351         uuid4 = htonl(uuid4);
1352         uuid5 = htons(uuid5);
1353
1354         memcpy(&(p_uuid->uuid[0]), &uuid0, 4);
1355         memcpy(&(p_uuid->uuid[4]), &uuid1, 2);
1356         memcpy(&(p_uuid->uuid[6]), &uuid2, 2);
1357         memcpy(&(p_uuid->uuid[8]), &uuid3, 2);
1358         memcpy(&(p_uuid->uuid[10]), &uuid4, 4);
1359         memcpy(&(p_uuid->uuid[14]), &uuid5, 2);
1360
1361         return;
1362 }
1363
1364 void _bt_convert_byte_ordering(char *data, int data_len,
1365                 char *converted_data)
1366 {
1367         int i, j;
1368         char temp[BT_UUID_LEN];
1369
1370         memcpy(&temp, data, data_len);
1371         for (i = 0, j = data_len - 1; i < data_len; i++, j--)
1372                 converted_data[i] = temp[j];
1373
1374 }
1375
1376 /* Trim string at first non-utf8 char */
1377 void _bt_truncate_non_utf8_chars(char * str)
1378 {
1379         guint i = 0;
1380         const char *ptr = NULL;
1381
1382         if (strlen(str) != 0) {
1383                 if (!g_utf8_validate(str, -1, &ptr)) {
1384                         while (*(str + i) != *ptr)
1385                                 i++;
1386                         *(str + i) = '\0';
1387                 }
1388         }
1389 }
1390
1391 invocation_info_t* _bt_get_request_info_data(int service_function, char *address)
1392 {
1393         GSList *l;
1394         invocation_info_t *req_info = NULL;
1395
1396         retv_if(NULL == address, NULL);
1397
1398         /* Get method invocation context */
1399         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1400                 req_info = l->data;
1401                 if (req_info == NULL || req_info->service_function != service_function)
1402                         continue;
1403
1404                 if (!strncasecmp((char *)req_info->user_data, address, BT_ADDRESS_STRING_SIZE))
1405                         return req_info;
1406         }
1407
1408         return NULL;
1409 }
1410
1411 #ifdef TIZEN_GATT_CLIENT
1412 invocation_info_t* _bt_get_request_info_data_from_function_name(int service_function)
1413 {
1414         GSList *l;
1415         invocation_info_t *req_info = NULL;
1416
1417         /* Get method invocation context */
1418         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1419                 req_info = l->data;
1420                 if (req_info == NULL || req_info->service_function != service_function)
1421                         continue;
1422                 return req_info;
1423         }
1424
1425         BT_DBG("Not existed");
1426         return NULL;
1427 }
1428 #endif
1429
1430 void _bt_set_device_values(gboolean connected, int state)
1431 {
1432         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1433
1434         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1435                 BT_ERR("vconf_get_int failed");
1436
1437         if (connected == TRUE)
1438                 bt_device_state |= state;
1439         else if (bt_device_state & state)
1440                 bt_device_state ^= state;
1441
1442         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
1443                 BT_ERR("vconf_set_int failed");
1444 }
1445
1446 int _bt_get_ad_data_by_type(char *in_data, int in_len,
1447                 char in_type, char **data, int *data_len)
1448 {
1449         if (in_data == NULL || data == NULL || data_len == NULL)
1450                 return BLUETOOTH_ERROR_INVALID_PARAM;
1451
1452         if (in_len < 0)
1453                 return BLUETOOTH_ERROR_INVALID_PARAM;
1454
1455         int i;
1456         int len = 0;
1457         int type = 0;
1458
1459         for (i = 0; i < in_len; i++) {
1460                 len = in_data[i];
1461                 if (len <= 0 || i + 1 >= in_len) {
1462                         BT_ERR("Invalid advertising data");
1463                         return BLUETOOTH_ERROR_INVALID_PARAM;
1464                 }
1465
1466                 type = in_data[i + 1];
1467                 if (type == in_type) {
1468                         i = i + 2;
1469                         len--;
1470                         break;
1471                 }
1472
1473                 i += len;
1474                 len = 0;
1475         }
1476
1477         if (i + len > in_len) {
1478                 BT_ERR("Invalid advertising data");
1479                 return BLUETOOTH_ERROR_INVALID_PARAM;
1480         } else if (len == 0) {
1481                 BT_INFO("AD Type 0x%02x data is not set", in_type);
1482                 *data = NULL;
1483                 *data_len = 0;
1484                 return BLUETOOTH_ERROR_NONE;
1485         }
1486
1487         *data = g_memdup(&in_data[i], len);
1488         if (*data == NULL)
1489                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
1490         *data_len = len;
1491
1492         return BLUETOOTH_ERROR_NONE;
1493 }
1494
1495 void _bt_copy_remote_dev_info(bt_remote_dev_info_t *dest, bt_remote_dev_info_t *src)
1496 {
1497         ret_if(NULL == src);
1498
1499         if (!dest->address) {
1500                 dest->address = g_strdup(src->address);
1501         }
1502
1503         if (src->name) {
1504                 g_free(dest->name);
1505                 dest->name = g_strdup(src->name);
1506         }
1507
1508         if (src->alias) {
1509                 g_free(dest->alias);
1510                 dest->alias = g_strdup(src->alias);
1511         }
1512
1513         if (src->uuid_count > 0 && src->uuids) {
1514                 int i;
1515
1516                 /* Release previously allocated UUIDs */
1517                 if (dest->uuid_count > 0 && dest->uuids)
1518                         for (i = 0; i < dest->uuid_count; i++)
1519                                 g_free(dest->uuids[i]);
1520                 g_free(dest->uuids);
1521
1522                 /* Copy new UUID list */
1523                 dest->uuids = g_malloc0(src->uuid_count * sizeof(char *));
1524                 for (i = 0; i < src->uuid_count; i++)
1525                         dest->uuids[i] = g_strdup(src->uuids[i]);
1526
1527                 dest->uuid_count = src->uuid_count;
1528         }
1529
1530         if (src->manufacturer_data_len > 0 && src->manufacturer_data) {
1531                 if (dest->manufacturer_data)
1532                         g_free(dest->manufacturer_data);
1533
1534                 dest->manufacturer_data_len = src->manufacturer_data_len;
1535                 dest->manufacturer_data = g_memdup(
1536                         src->manufacturer_data, src->manufacturer_data_len);
1537         }
1538
1539         dest->rssi = src->rssi;
1540         dest->class = src->class;
1541         dest->paired = src->paired;
1542         dest->connected = src->connected;
1543         dest->trust = src->trust;
1544         dest->addr_type = src->addr_type;
1545         dest->vid = src->vid;
1546         dest->pid = src->pid;
1547         dest->device_type = src->device_type;
1548         dest->is_alias_set = src->is_alias_set;
1549
1550 }
1551
1552 #define BT_PERMANENT_LOG_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log.txt"
1553 #define BT_PERMANENT_LOG_OLD_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log_old.txt"
1554 #define PERMANENT_LOG_MAX (128 * 1024)
1555 void _bt_permanent_log(const char *fmt, ...)
1556 {
1557         FILE *fp;
1558         time_t t;
1559         struct tm *lt;
1560         char time_buf[32];
1561         long lsize = 0;
1562         va_list ap;
1563
1564 #ifndef TIZEN_FEATURE_BT_PERMANENT_LOG
1565         return;
1566 #endif
1567
1568         t = time(&t);
1569         lt = localtime(&t);
1570         strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", lt);
1571
1572         fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "r");
1573         if (fp != NULL) {
1574                 fseek(fp, 0, SEEK_END);
1575                 lsize = ftell(fp);
1576         }
1577
1578         if (lsize > PERMANENT_LOG_MAX) {
1579                 FILE *fp2;
1580                 int character;
1581                 fp2 = fopen(BT_PERMANENT_LOG_OLD_FILE_PATH, "w");
1582                 if (fp2 == NULL)
1583                         goto DONE;
1584                 fseek(fp, 0, SEEK_SET);
1585                 while (!feof(fp)) {
1586                         character = fgetc(fp);
1587                         fputc(character, fp2);
1588                 }
1589                 fclose(fp);
1590                 fclose(fp2);
1591                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "w");
1592         } else {
1593                 if (fp != NULL)
1594                         fclose(fp);
1595                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "a");
1596         }
1597
1598         if (fp != NULL) {
1599                 char buf[100];
1600                 va_start(ap, fmt);
1601                 vsnprintf(buf, 100, fmt, ap);
1602                 va_end(ap);
1603                 fprintf(fp, "%s %s\n", time_buf, buf);
1604         }
1605
1606 DONE:
1607         if (fp != NULL)
1608                 fclose(fp);
1609 }
1610