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