Add the permanent log feature
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / bt-service-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <dlog.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <termios.h>
26 #include <net_connection.h>
27 #include <dbus/dbus.h>
28 #include <glib.h>
29 #include <dlog.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <termios.h>
33 #include <net_connection.h>
34 #include <bundle.h>
35 #include <eventsystem.h>
36 #include <arpa/inet.h>
37 #include <vconf.h>
38
39 #include "bluetooth-api.h"
40 #include "bt-service-common.h"
41
42 #include <oal-manager.h>
43 #include <oal-device-mgr.h>
44
45 #ifdef TIZEN_FEATURE_BT_IPSP
46 static GDBusProxy *ipsp_proxy;
47 #endif
48 static GDBusConnection *system_conn;
49 static GDBusConnection *session_conn;
50 static GDBusProxy *manager_proxy_g;
51 static GDBusProxy *adapter_proxy_g;
52 static void *net_conn;
53
54 static GDBusProxy *adapter_properties_proxy;
55
56 static GDBusConnection *system_gconn = NULL;
57
58 GDBusConnection *_bt_gdbus_init_system_gconn(void)
59 {
60         GError *error = NULL;
61
62         dbus_threads_init_default();
63
64         if (system_gconn != NULL)
65                 return system_gconn;
66
67         system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
68
69         if (!system_gconn) {
70                 BT_ERR("Unable to connect to dbus: %s", error->message);
71                 g_clear_error(&error);
72         }
73
74         return system_gconn;
75 }
76
77 GDBusConnection *_bt_gdbus_get_system_gconn(void)
78 {
79         GDBusConnection *local_system_gconn = NULL;
80         GError *error = NULL;
81
82         if (system_gconn == NULL) {
83                 system_gconn = _bt_gdbus_init_system_gconn();
84         } else if (g_dbus_connection_is_closed(system_gconn)) {
85
86                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
87
88                 if (!local_system_gconn) {
89                         BT_ERR("Unable to connect to dbus: %s", error->message);
90                         g_clear_error(&error);
91                 }
92
93                 system_gconn = local_system_gconn;
94         }
95
96         return system_gconn;
97 }
98
99 static GDBusProxy *__bt_init_manager_proxy(void)
100 {
101         GDBusProxy *proxy;
102
103         if (system_conn == NULL) {
104                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
105                 retv_if(system_conn == NULL, NULL);
106         }
107
108         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
109                                                                 NULL, BT_BLUEZ_NAME,
110                                                                 BT_MANAGER_PATH, BT_MANAGER_INTERFACE,  NULL, NULL);
111
112         retv_if(proxy == NULL, NULL);
113
114         manager_proxy_g = proxy;
115
116         return proxy;
117 }
118
119 static GDBusProxy *__bt_init_adapter_proxy(void)
120 {
121         GDBusProxy *manager_proxy;
122         GDBusProxy *proxy;
123         char *adapter_path = NULL;
124
125         if (system_conn == NULL) {
126                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
127                 retv_if(system_conn == NULL, NULL);
128         }
129
130         manager_proxy = _bt_get_manager_proxy();
131         retv_if(manager_proxy == NULL, NULL);
132
133         adapter_path = _bt_get_adapter_path();
134         retv_if(adapter_path == NULL, NULL);
135
136         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
137                                                                 NULL, BT_BLUEZ_NAME,
138                                                                 adapter_path, BT_ADAPTER_INTERFACE,  NULL, NULL);
139
140         g_free(adapter_path);
141
142         retv_if(proxy == NULL, NULL);
143
144         adapter_proxy_g = proxy;
145
146         return proxy;
147 }
148 #ifdef TIZEN_FEATURE_BT_IPSP
149 static GDBusProxy *__bt_init_ipsp_proxy(void)
150 {
151         BT_DBG("+");
152
153         GDBusConnection *g_conn;
154         GDBusProxy *proxy;
155         g_conn = _bt_gdbus_get_system_gconn();
156         retv_if(g_conn == NULL, NULL);
157         proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
158                                                                 NULL, BT_IPSP_NAME,
159                                                                 "/org/projectx/bt_ipsp", BT_IPSP_INTERFACE,  NULL, NULL);
160         if (!proxy) {
161                 BT_ERR("Unable to get proxy");
162                 return NULL;
163         }
164         ipsp_proxy = proxy;
165         BT_DBG("-");
166         return proxy;
167 }
168 #endif
169 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
170 {
171         GDBusProxy *manager_proxy;
172         GDBusProxy *proxy;
173         char *adapter_path = NULL;
174
175         if (system_conn == NULL) {
176                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
177                 retv_if(system_conn == NULL, NULL);
178         }
179
180         manager_proxy = _bt_get_manager_proxy();
181         retv_if(manager_proxy == NULL, NULL);
182
183         adapter_path = _bt_get_adapter_path();
184         retv_if(adapter_path == NULL, NULL);
185
186         proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
187                                                                         NULL, BT_BLUEZ_NAME,
188                                                                         adapter_path, BT_PROPERTIES_INTERFACE,  NULL, NULL);
189
190         g_free(adapter_path);
191
192         retv_if(proxy == NULL, NULL);
193
194         adapter_properties_proxy = proxy;
195
196         return proxy;
197 }
198
199 GDBusConnection *__bt_init_system_gconn(void)
200 {
201         if (system_conn == NULL)
202                 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
203
204         return system_conn;
205 }
206
207 GDBusConnection *__bt_init_session_conn(void)
208 {
209         if (session_conn == NULL)
210                 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
211
212         return session_conn;
213 }
214
215 GDBusConnection *_bt_get_session_gconn(void)
216 {
217         return (session_conn) ? session_conn : __bt_init_session_conn();
218 }
219
220 GDBusConnection *_bt_get_system_gconn(void)
221 {
222         return (system_conn) ? system_conn : __bt_init_system_gconn();
223 }
224
225 GDBusConnection *_bt_get_system_conn(void)
226 {
227         GDBusConnection *g_conn;
228
229         if (system_conn == NULL)
230                 g_conn = __bt_init_system_gconn();
231         else
232                 g_conn = system_conn;
233
234         retv_if(g_conn == NULL, NULL);
235
236         return g_conn;
237 }
238
239 GDBusProxy *_bt_get_manager_proxy(void)
240 {
241         if (manager_proxy_g) {
242                 const gchar *path =  g_dbus_proxy_get_object_path(manager_proxy_g);
243                 if (path == NULL) {
244                         BT_ERR("Already proxy released hence creating new proxy");
245                         return  __bt_init_manager_proxy();
246                 }
247                 return manager_proxy_g;
248         }
249         return  __bt_init_manager_proxy();
250 }
251 #ifdef TIZEN_FEATURE_BT_IPSP
252 GDBusProxy *_bt_get_ipsp_proxy(void)
253 {
254         if (ipsp_proxy) {
255                 const char *path =  g_dbus_proxy_get_object_path(ipsp_proxy);
256                 if (path == NULL) {
257                         BT_ERR("Already proxy released hence creating new proxy");
258                         return  __bt_init_ipsp_proxy();
259                 }
260                 return ipsp_proxy;
261         }
262         return  __bt_init_ipsp_proxy();
263 }
264 #endif
265
266 static void *__bt_init_net_conn(void)
267 {
268         int result;
269         connection_h connection = NULL;
270
271         if (net_conn == NULL) {
272                 result = connection_create(&connection);
273
274         if (result != CONNECTION_ERROR_NONE ||
275                                         connection == NULL) {
276                 BT_DBG("connection_create() failed: %d", result);
277                 net_conn = NULL;
278                 return NULL;
279         }
280                 net_conn = connection;
281         }
282         return net_conn;
283 }
284
285 void *_bt_get_net_conn(void)
286 {
287         return (net_conn) ? net_conn : __bt_init_net_conn();
288 }
289
290 GDBusProxy *_bt_get_adapter_proxy(void)
291 {
292         if (adapter_proxy_g) {
293                 const char *path =  g_dbus_proxy_get_object_path(adapter_proxy_g);
294                 if (path == NULL) {
295                         BT_ERR("Already proxy released hence creating new proxy");
296                         return  __bt_init_adapter_proxy();
297                 }
298
299                 return adapter_proxy_g;
300         }
301         return  __bt_init_adapter_proxy();
302
303 }
304
305 GDBusProxy *_bt_get_adapter_properties_proxy(void)
306 {
307         return (adapter_properties_proxy) ? adapter_properties_proxy :
308                                         __bt_init_adapter_properties_proxy();
309 }
310
311 static char *__bt_extract_adapter_path(GVariantIter *iter)
312 {
313         char *object_path = NULL;
314         GVariantIter *interface_iter;
315         GVariantIter *svc_iter;
316         char *interface_str = NULL;
317
318         /* Parse the signature: oa{sa{sv}}} */
319         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
320                         &interface_iter)) {
321
322                 if (object_path == NULL)
323                         continue;
324
325                 while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
326                                 &interface_str, &svc_iter)) {
327                         if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
328                                 continue;
329
330                         BT_DBG("Object Path: %s", object_path);
331                         g_variant_iter_free(svc_iter);
332                         g_variant_iter_free(interface_iter);
333                         return g_strdup(object_path);
334                 }
335         }
336         return NULL;
337 }
338
339 char *_bt_get_adapter_path(void)
340 {
341         GDBusConnection *conn;
342         GDBusProxy *manager_proxy;
343         GVariant *result = NULL;
344         GVariantIter *iter = NULL;
345         char *adapter_path = NULL;
346
347         conn = _bt_get_system_conn();
348         retv_if(conn == NULL, NULL);
349
350         manager_proxy = _bt_get_manager_proxy();
351         retv_if(manager_proxy == NULL, NULL);
352
353         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
354                                 NULL,
355                                 G_DBUS_CALL_FLAGS_NONE,
356                                 -1,
357                                 NULL,
358                                 NULL);
359         if (!result) {
360                 BT_ERR("Can't get managed objects");
361                 return NULL;
362         }
363
364         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
365         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
366
367         adapter_path = __bt_extract_adapter_path(iter);
368         g_variant_iter_free(iter);
369         g_variant_unref(result);
370         return adapter_path;
371 }
372
373 void _bt_deinit_bluez_proxy(void)
374 {
375         if (manager_proxy_g) {
376                 g_object_unref(manager_proxy_g);
377                 manager_proxy_g = NULL;
378         }
379
380         if (adapter_proxy_g) {
381                 g_object_unref(adapter_proxy_g);
382                 adapter_proxy_g = NULL;
383         }
384         if (adapter_properties_proxy) {
385                 g_object_unref(adapter_properties_proxy);
386                 adapter_properties_proxy = NULL;
387         }
388 }
389
390 void _bt_deinit_proxys(void)
391 {
392         int ret;
393         _bt_deinit_bluez_proxy();
394
395         if (system_conn) {
396                 g_object_unref(system_conn);
397                 system_conn = NULL;
398         }
399
400         if (session_conn) {
401                 g_object_unref(session_conn);
402                 session_conn = NULL;
403         }
404
405         if (net_conn) {
406                 ret = connection_destroy(net_conn);
407                 net_conn = NULL;
408                 if (ret != 0)
409                         BT_ERR("connection_destroy failed : %d", ret);
410         }
411 }
412
413 void _bt_convert_device_path_to_address(const char *device_path,
414                                                 char *device_address)
415 {
416         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
417         char *dev_addr;
418
419         ret_if(device_path == NULL);
420         ret_if(device_address == NULL);
421
422         dev_addr = strstr(device_path, "dev_");
423         if (dev_addr != NULL) {
424                 char *pos = NULL;
425                 dev_addr += 4;
426                 g_strlcpy(address, dev_addr, sizeof(address));
427
428                 while ((pos = strchr(address, '_')) != NULL)
429                         *pos = ':';
430
431                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
432         }
433 }
434
435
436 void _bt_convert_addr_string_to_type(unsigned char *addr,
437                                         const char *address)
438 {
439         int i;
440         char *ptr = NULL;
441
442         ret_if(address == NULL);
443         ret_if(addr == NULL);
444
445         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
446                 addr[i] = strtol(address, &ptr, 16);
447                 if (ptr[0] != '\0') {
448                         if (ptr[0] != ':')
449                                 return;
450
451                         address = ptr + 1;
452                 }
453         }
454 }
455
456 void _bt_convert_addr_type_to_string(char *address,
457                                 unsigned char *addr)
458 {
459         ret_if(address == NULL);
460         ret_if(addr == NULL);
461
462         snprintf(address, BT_ADDRESS_STRING_SIZE,
463                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
464                         addr[0], addr[1], addr[2],
465                         addr[3], addr[4], addr[5]);
466 }
467
468 gboolean _bt_compare_adddress(const bluetooth_device_address_t *addr1,
469                 const bluetooth_device_address_t *addr2)
470 {
471         if (memcmp(&addr1->addr, &addr2->addr, 6) == 0)
472                 return TRUE;
473         else
474                 return FALSE;
475 }
476
477 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
478 {
479         BT_INFO("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
480                 addr->addr[0], addr->addr[1], addr->addr[2],
481                 addr->addr[3], addr->addr[4], addr->addr[5]);
482 }
483
484 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
485                                 unsigned int cod)
486 {
487         ret_if(device_class == NULL);
488
489         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
490         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
491         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
492
493         if (cod & 0x002000) {
494                 device_class->service_class |=
495                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
496         }
497 }
498
499 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
500 {
501         ret_if(le_dev_info == NULL);
502
503         g_free(le_dev_info->adv_data);
504         g_free(le_dev_info);
505 }
506
507 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
508 {
509         unsigned int i;
510         const char *p = src;
511         char *next;
512         int count;
513
514         if (dest == NULL || src == NULL)
515                 return BLUETOOTH_ERROR_INVALID_PARAM;
516
517         BT_DBG("+src : %s", src);
518         BT_DBG("+dest : %s", dest);
519
520         i = 0;
521         while (*p != '\0' && i < length) {
522                 next = g_utf8_next_char(p);
523                 count = next - p;
524
525                 while (count > 0 && ((i + count) < length)) {
526                         dest[i++] = *p;
527                         p++;
528                         count--;
529                 }
530                 p = next;
531         }
532         return BLUETOOTH_ERROR_NONE;
533 }
534
535 gboolean _bt_utf8_validate(char *name)
536 {
537         BT_DBG("+");
538         gunichar2 *u16;
539         glong items_written = 0;
540
541         if (FALSE == g_utf8_validate(name, -1, NULL))
542                 return FALSE;
543
544         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
545         if (u16 == NULL)
546                 return FALSE;
547
548         g_free(u16);
549
550         if (items_written != g_utf8_strlen(name, -1))
551                 return FALSE;
552
553         BT_DBG("-");
554         return TRUE;
555 }
556
557 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
558 {
559         BT_DBG("+");
560
561 #if TODO_40 /* Need to add this function */
562         if (!_bt_agent_register_osp_server(type, uuid, path, fd))
563                 return BLUETOOTH_ERROR_INTERNAL;
564 #else
565         if (device_register_osp_server(type, uuid, path, fd) != 0)
566                 return BLUETOOTH_ERROR_INTERNAL;
567 #endif
568         return BLUETOOTH_ERROR_NONE;
569 }
570
571 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
572 {
573         BT_DBG("+");
574
575 #if TODO_40 /* Need to add this function */
576         if (!_bt_agent_unregister_osp_server(type, uuid))
577                 return BLUETOOTH_ERROR_INTERNAL;
578 #else
579         if (device_unregister_osp_server(type, uuid) != 0)
580                 return BLUETOOTH_ERROR_INTERNAL;
581 #endif
582         return BLUETOOTH_ERROR_NONE;
583 }
584
585 int _bt_set_socket_non_blocking(int socket_fd)
586 {
587         /* Set Nonblocking */
588         long arg;
589
590         arg = fcntl(socket_fd, F_GETFL);
591
592         if (arg < 0)
593                 return -errno;
594
595         if (arg & O_NONBLOCK)
596                 BT_ERR("Already Non-blocking \n");
597
598         arg |= O_NONBLOCK;
599
600         if (fcntl(socket_fd, F_SETFL, arg) < 0)
601                 return -errno;
602
603         return BLUETOOTH_ERROR_NONE;
604 }
605
606 int _bt_set_non_blocking_tty(int sk)
607 {
608         struct termios ti = {0,};
609         int err;
610
611         err = _bt_set_socket_non_blocking(sk);
612
613         if (err < 0) {
614                 BT_ERR("Error in set non blocking!\n");
615                 return err;
616         }
617
618         tcflush(sk, TCIOFLUSH);
619
620         /* Switch tty to RAW mode */
621         cfmakeraw(&ti);
622         tcsetattr(sk, TCSANOW, &ti);
623
624         return BLUETOOTH_ERROR_NONE;
625 }
626
627 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
628 {
629         char *object_path = NULL;
630         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
631
632         /* Parse the signature: oa{sa{sv}}} */
633         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
634                         NULL)) {
635                 retv_if(object_path == NULL, NULL);
636                 _bt_convert_device_path_to_address(object_path, device_address);
637                 if (g_strcmp0(address, device_address) == 0)
638                         return g_strdup(object_path);
639         }
640         return NULL;
641 }
642
643 char *_bt_get_device_object_path(char *address)
644 {
645         char *object_path = NULL;
646         GDBusConnection *conn;
647         GDBusProxy *manager_proxy;
648         GVariant *result = NULL;
649         GVariantIter *iter = NULL;
650
651         conn = _bt_get_system_conn();
652         retv_if(conn == NULL, NULL);
653
654         manager_proxy = _bt_get_manager_proxy();
655         retv_if(manager_proxy == NULL, NULL);
656
657         result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
658                                 NULL,
659                                 G_DBUS_CALL_FLAGS_NONE,
660                                 -1,
661                                 NULL,
662                                 NULL);
663         if (!result) {
664                 BT_ERR("Can't get managed objects");
665                 return NULL;
666         }
667
668         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
669         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
670         object_path = __bt_extract_device_path(iter, address);
671         g_variant_iter_free(iter);
672         g_variant_unref(result);
673         return object_path;
674 }
675
676 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
677 {
678         switch (profile_type) {
679         case BT_PROFILE_CONN_RFCOMM:
680                 return strdup(RFCOMM_UUID_STR);
681         case BT_PROFILE_CONN_A2DP:
682                 return strdup(A2DP_SINK_UUID);
683         case BT_PROFILE_CONN_A2DP_SINK:
684                 return strdup(A2DP_SOURCE_UUID);
685         case BT_PROFILE_CONN_HSP:
686                 return strdup(HFP_HS_UUID);
687         case BT_PROFILE_CONN_HID:
688                 return strdup(HID_UUID);
689         case BT_PROFILE_CONN_NAP:
690                 return strdup(NAP_UUID);
691         case BT_PROFILE_CONN_HFG:
692                 return strdup(HFP_AG_UUID);
693         case BT_PROFILE_CONN_GATT:
694         case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
695         default:
696                 return NULL;
697         };
698 }
699
700 const char *_bt_convert_error_to_string(int error)
701 {
702         switch (error) {
703         case BLUETOOTH_ERROR_CANCEL:
704                 return "CANCELLED";
705         case BLUETOOTH_ERROR_INVALID_PARAM:
706                 return "INVALID_PARAMETER";
707         case BLUETOOTH_ERROR_INVALID_DATA:
708                 return "INVALID DATA";
709         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
710         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
711                 return "OUT_OF_MEMORY";
712         case BLUETOOTH_ERROR_TIMEOUT:
713                 return "TIMEOUT";
714         case BLUETOOTH_ERROR_NO_RESOURCES:
715                 return "NO_RESOURCES";
716         case BLUETOOTH_ERROR_INTERNAL:
717                 return "INTERNAL";
718         case BLUETOOTH_ERROR_NOT_SUPPORT:
719                 return "NOT_SUPPORT";
720         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
721                 return "NOT_ENABLED";
722         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
723                 return "ALREADY_ENABLED";
724         case BLUETOOTH_ERROR_DEVICE_BUSY:
725                 return "DEVICE_BUSY";
726         case BLUETOOTH_ERROR_ACCESS_DENIED:
727                 return "ACCESS_DENIED";
728         case BLUETOOTH_ERROR_MAX_CLIENT:
729                 return "MAX_CLIENT";
730         case BLUETOOTH_ERROR_NOT_FOUND:
731                 return "NOT_FOUND";
732         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
733                 return "SERVICE_SEARCH_ERROR";
734         case BLUETOOTH_ERROR_PARING_FAILED:
735                 return "PARING_FAILED";
736         case BLUETOOTH_ERROR_NOT_PAIRED:
737                 return "NOT_PAIRED";
738         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
739                 return "SERVICE_NOT_FOUND";
740         case BLUETOOTH_ERROR_NOT_CONNECTED:
741                 return "NOT_CONNECTED";
742         case BLUETOOTH_ERROR_ALREADY_CONNECT:
743                 return "ALREADY_CONNECT";
744         case BLUETOOTH_ERROR_CONNECTION_BUSY:
745                 return "CONNECTION_BUSY";
746         case BLUETOOTH_ERROR_CONNECTION_ERROR:
747                 return "CONNECTION_ERROR";
748         case BLUETOOTH_ERROR_MAX_CONNECTION:
749                 return "MAX_CONNECTION";
750         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
751                 return "NOT_IN_OPERATION";
752         case BLUETOOTH_ERROR_CANCEL_BY_USER:
753                 return "CANCEL_BY_USER";
754         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
755                 return "REGISTRATION_FAILED";
756         case BLUETOOTH_ERROR_IN_PROGRESS:
757                 return "IN_PROGRESS";
758         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
759                 return "AUTHENTICATION_FAILED";
760         case BLUETOOTH_ERROR_HOST_DOWN:
761                 return "HOST_DOWN";
762         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
763                 return "END_OF_DEVICE_LIST";
764         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
765                 return "AGENT_ALREADY_EXIST";
766         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
767                 return "AGENT_DOES_NOT_EXIST";
768         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
769                 return "ALREADY_INITIALIZED";
770         case BLUETOOTH_ERROR_PERMISSION_DEINED:
771                 return "PERMISSION_DEINED";
772         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
773                 return "ALREADY_DEACTIVATED";
774         case BLUETOOTH_ERROR_NOT_INITIALIZED:
775                 return "NOT_INITIALIZED";
776         default:
777                 return "UNKNOWN";
778         }
779 }
780
781 const char * _bt_convert_disc_reason_to_string(int reason)
782 {
783         switch (reason) {
784         case 1:
785                 return "Link loss";
786         case 2:
787                 return "Connection terminated by local host";
788         case 3:
789                 return "Remote user terminated connection";
790         case 0:
791         default:
792                 return "Unknown";
793         }
794 }
795
796 void _bt_logging_connection(gboolean connect, int addr_type)
797 {
798         static int le_conn = 0;
799         static int le_disc = 0;
800         static int edr_conn = 0;
801         static int edr_disc = 0;
802
803         if (connect) {
804                 if (addr_type)
805                         le_conn++;
806                 else
807                         edr_conn++;
808         } else {
809                 if (addr_type)
810                         le_disc++;
811                 else
812                         edr_disc++;
813         }
814
815         BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
816                         le_conn, le_disc, edr_conn, edr_disc);
817 }
818
819 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
820 {
821         int ret;
822         bundle *b = NULL;
823
824         b = bundle_create();
825
826         bundle_add_str(b, key, value);
827
828         ret = eventsystem_send_system_event(event, b);
829
830         BT_DBG("eventsystem_send_system_event result: %d", ret);
831
832         bundle_free(b);
833
834         return ret;
835 }
836
837 void _bt_swap_byte_ordering(char *data, int data_len)
838 {
839         char temp;
840         int i, j;
841
842         ret_if(data == NULL);
843         /* Swap to opposite endian */
844         for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
845                 temp = data[i];
846                 data[i] = data[j];
847                 data[j] = temp;
848                 }
849 }
850
851 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
852 {
853         int i;
854
855         retv_if(data1 == NULL, -1);
856         retv_if(data2 == NULL, -1);
857         for (i = 0; i < data_len; i++) {
858                 if (data1[i] != data2[i])
859                         return data1[i] - data2[i];
860                 }
861         return 0;
862 }
863 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
864         const char *mask, int data_len)
865 {
866         int i;
867         char a, b;
868
869         retv_if(data1 == NULL, -1);
870         retv_if(data2 == NULL, -1);
871         retv_if(mask == NULL, -1);
872         for (i = 0; i < data_len; i++) {
873                 a = data1[i] & mask[i];
874                 b = data2[i] & mask[i];
875                 if (a != b)
876                         return (int)(a - b);
877                 }
878         return 0;
879 }
880
881 void _bt_copy_remote_dev(bt_remote_dev_info_t *dev_info, remote_device_t *oal_device)
882 {
883         unsigned int i;
884
885         BT_DBG("+");
886
887         dev_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
888         _bt_convert_addr_type_to_string(dev_info->address, oal_device->address.addr);
889         BT_INFO("Address [%s]", dev_info->address);
890
891         if (strlen(oal_device->name) == 0)
892                 dev_info->name = NULL;
893         else {
894                 dev_info->name = g_strdup(oal_device->name);
895                 _bt_truncate_non_utf8_chars(dev_info->name);
896                 BT_INFO("Name [%s]", dev_info->name);
897         }
898
899         if (strlen(oal_device->alias) == 0) {
900                 dev_info->alias = NULL;
901         } else {
902                 dev_info->alias = g_strdup(oal_device->alias);
903                 _bt_truncate_non_utf8_chars(dev_info->alias);
904                 BT_INFO("Alias [%s]", dev_info->alias);
905         }
906
907         dev_info->class = oal_device->cod;
908         dev_info->paired = oal_device->is_bonded;
909         BT_INFO("Is Bonded [%d]", dev_info->paired);
910         dev_info->connected = oal_device->is_connected;
911         BT_INFO("iS Connected [%d]", dev_info->connected);
912         dev_info->rssi = oal_device->rssi;
913         dev_info->addr_type = oal_device->type;
914         dev_info->uuid_count = oal_device->uuid_count;
915         BT_INFO("UUID Count [%d]", dev_info->uuid_count);
916         dev_info->trust = ((oal_device->is_trusted == 0) ? FALSE : TRUE);
917
918         if (dev_info->uuid_count > 0)
919                 dev_info->uuids = g_new0(char *, dev_info->uuid_count);
920
921         /* Fill Remote Device Service List list */
922         for (i = 0; i < dev_info->uuid_count; i++) {
923                 dev_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
924                 _bt_uuid_to_string((service_uuid_t *)&oal_device->uuid[i].uuid, dev_info->uuids[i]);
925         }
926
927         dev_info->vid = (unsigned short)oal_device->vid;
928         dev_info->pid = (unsigned short)oal_device->pid;
929         dev_info->device_type = (int)oal_device->type;
930         dev_info->is_alias_set = ((oal_device->is_alias_set == 0) ? FALSE : TRUE);
931
932         BT_DBG("-");
933 }
934
935 void _bt_free_remote_dev(bt_remote_dev_info_t *dev_info)
936 {
937         unsigned int c;
938
939         ret_if(NULL == dev_info);
940
941         BT_INFO("+");
942
943         g_free(dev_info->address);
944         g_free(dev_info->name);
945         g_free(dev_info->alias);
946         g_free(dev_info->manufacturer_data);
947
948         for (c = 0; c < dev_info->uuid_count; c++)
949                 g_free(dev_info->uuids[c]);
950         g_free(dev_info->uuids);
951         g_free(dev_info);
952
953         BT_INFO("-");
954 }
955
956 static void __bt_get_service_list(bt_remote_dev_info_t *info, bluetooth_device_info_t *dev)
957 {
958         unsigned int i;
959         char **uuids;
960         char **parts;
961
962         BT_DBG("+");
963
964         ret_if(info == NULL);
965         ret_if(dev == NULL);
966
967         uuids = info->uuids;
968         if (uuids == NULL) {
969                 BT_ERR("No UUID's");
970                 return;
971         }
972
973         dev->service_index = 0;
974         BT_DBG("Total UUID count [%d]", info->uuid_count);
975         for (i = 0; i < info->uuid_count; i++) {
976                 g_strlcpy(dev->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
977
978                 parts = g_strsplit(uuids[i], "-", -1);
979
980                 if (parts == NULL || parts[0] == NULL)
981                         break;
982
983                 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
984                 g_strfreev(parts);
985
986                 dev->service_index++;
987         }
988
989         BT_DBG("-");
990 }
991
992 void _bt_copy_remote_device(bt_remote_dev_info_t *rem_dev, bluetooth_device_info_t *dev)
993 {
994         BT_DBG("+");
995
996         memset(dev, 0x00, sizeof(bluetooth_device_info_t));
997         __bt_get_service_list(rem_dev, dev);
998         _bt_convert_addr_string_to_type(dev->device_address.addr, rem_dev->address);
999         _bt_divide_device_class(&dev->device_class, rem_dev->class);
1000
1001         if (rem_dev->alias) {
1002                 g_strlcpy(dev->device_name.name, rem_dev->alias,
1003                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1004                 BT_DBG("Name: %s", dev->device_name.name);
1005         } else {
1006                 g_strlcpy(dev->device_name.name, rem_dev->name,
1007                                 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1008                 BT_DBG("Name: %s", dev->device_name.name);
1009         }
1010
1011         dev->rssi = rem_dev->rssi;
1012         dev->trust = rem_dev->trust;
1013         dev->paired = rem_dev->paired;
1014         dev->connected = rem_dev->connected;
1015         dev->is_alias_set = rem_dev->is_alias_set;
1016
1017         /* Fill Manufacturer data */
1018         if (rem_dev->manufacturer_data_len > 0) {
1019                 dev->manufacturer_data.data_len = rem_dev->manufacturer_data_len;
1020                 memcpy(dev->manufacturer_data.data,
1021                         rem_dev->manufacturer_data, rem_dev->manufacturer_data_len);
1022         } else {
1023                 dev->manufacturer_data.data_len = 0;
1024         }
1025
1026         BT_DBG("-");
1027 }
1028
1029 void _bt_service_print_dev_info(bluetooth_device_info_t *dev_info)
1030 {
1031         int i;
1032
1033         ret_if(dev_info == NULL);
1034
1035         _bt_print_device_address_t(&(dev_info->device_address));
1036         BT_INFO("Device Name:[%s]", dev_info->device_name.name);
1037         BT_INFO("Device Major Class:[0x%X]", dev_info->device_class.major_class);
1038         BT_INFO("Device Minor Class:[0x%X]", dev_info->device_class.minor_class);
1039         BT_INFO("Device Service Class:[0x%X]", dev_info->device_class.minor_class);
1040         BT_INFO("Device Paired:[%s]", (dev_info->paired ? "TRUE" : "FALSE"));
1041         BT_INFO("Device Trusted:[%s]", (dev_info->trust ? "TRUE" : "FALSE"));
1042         BT_INFO("Device Connected:[%d]", dev_info->connected);
1043         BT_INFO("Device Service index:[%d]", dev_info->service_index);
1044         for (i = 0; i < dev_info->service_index; i++) {
1045                 BT_INFO("Device Service List:[%d]", dev_info->service_list_array[i]);
1046                 BT_INFO("Device UUID:[%s]", dev_info->uuids[i]);
1047         }
1048
1049         BT_INFO("Device manufacturer data len:[%d]", dev_info->manufacturer_data.data_len);
1050         for (i = 0; i < dev_info->manufacturer_data.data_len; i++)
1051                 BT_INFO("%2.2X", dev_info->manufacturer_data.data[i]);
1052 }
1053
1054 void _bt_uuid_to_string(service_uuid_t *p_uuid, char *str)
1055 {
1056         uint32_t uuid0, uuid4;
1057         uint16_t uuid1, uuid2, uuid3, uuid5;
1058
1059         memcpy(&uuid0, &(p_uuid->uuid[0]), 4);
1060         memcpy(&uuid1, &(p_uuid->uuid[4]), 2);
1061         memcpy(&uuid2, &(p_uuid->uuid[6]), 2);
1062         memcpy(&uuid3, &(p_uuid->uuid[8]), 2);
1063         memcpy(&uuid4, &(p_uuid->uuid[10]), 4);
1064         memcpy(&uuid5, &(p_uuid->uuid[14]), 2);
1065
1066         snprintf((char *)str, BLUETOOTH_UUID_STRING_MAX, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
1067                         ntohl(uuid0), ntohs(uuid1),
1068                         ntohs(uuid2), ntohs(uuid3),
1069                         ntohl(uuid4), ntohs(uuid5));
1070         return;
1071 }
1072
1073 void _bt_string_to_uuid(char *str, service_uuid_t *p_uuid)
1074 {
1075         uint32_t uuid0, uuid4;
1076         uint16_t uuid1, uuid2, uuid3, uuid5;
1077
1078         sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1079                         &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
1080
1081         uuid0 = htonl(uuid0);
1082         uuid1 = htons(uuid1);
1083         uuid2 = htons(uuid2);
1084         uuid3 = htons(uuid3);
1085         uuid4 = htonl(uuid4);
1086         uuid5 = htons(uuid5);
1087
1088         memcpy(&(p_uuid->uuid[0]), &uuid0, 4);
1089         memcpy(&(p_uuid->uuid[4]), &uuid1, 2);
1090         memcpy(&(p_uuid->uuid[6]), &uuid2, 2);
1091         memcpy(&(p_uuid->uuid[8]), &uuid3, 2);
1092         memcpy(&(p_uuid->uuid[10]), &uuid4, 4);
1093         memcpy(&(p_uuid->uuid[14]), &uuid5, 2);
1094
1095         return;
1096 }
1097
1098 void _bt_convert_byte_ordering(char *data, int data_len,
1099                 char *converted_data)
1100 {
1101         int i, j;
1102         char temp[BT_UUID_LEN];
1103
1104         memcpy(&temp, data, data_len);
1105         for (i = 0, j = data_len - 1; i < data_len; i++, j--)
1106                 converted_data[i] = temp[j];
1107
1108 }
1109
1110 /* Trim string at first non-utf8 char */
1111 void _bt_truncate_non_utf8_chars(char * str)
1112 {
1113         guint i = 0;
1114         const char *ptr = NULL;
1115
1116         if (strlen(str) != 0) {
1117                 if (!g_utf8_validate(str, -1, &ptr)) {
1118                         while (*(str + i) != *ptr)
1119                                 i++;
1120                         *(str + i) = '\0';
1121                 }
1122         }
1123 }
1124
1125 invocation_info_t* _bt_get_request_info_data(int service_function, char *address)
1126 {
1127         GSList *l;
1128         invocation_info_t *req_info = NULL;
1129
1130         BT_DBG("+");
1131
1132         retv_if(NULL == address, NULL);
1133
1134         /* Get method invocation context */
1135         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1136                 req_info = l->data;
1137                 if (req_info == NULL || req_info->service_function != service_function)
1138                         continue;
1139
1140                 if (!strncasecmp((char *)req_info->user_data, address, BT_ADDRESS_STRING_SIZE))
1141                         return req_info;
1142         }
1143
1144         BT_DBG("-");
1145         return NULL;
1146 }
1147
1148 #ifdef TIZEN_GATT_CLIENT
1149 invocation_info_t* _bt_get_request_info_data_from_function_name(int service_function)
1150 {
1151         GSList *l;
1152         invocation_info_t *req_info = NULL;
1153
1154         BT_DBG("+");
1155
1156         /* Get method invocation context */
1157         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1158                 req_info = l->data;
1159                 if (req_info == NULL || req_info->service_function != service_function)
1160                         continue;
1161                 return req_info;
1162         }
1163
1164         BT_DBG("-");
1165         return NULL;
1166 }
1167 #endif
1168
1169 void _bt_set_device_values(gboolean connected, int state)
1170 {
1171         int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1172
1173         if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1174                 BT_ERR("vconf_get_int failed");
1175
1176         if (connected == TRUE)
1177                 bt_device_state |= state;
1178         else if (bt_device_state & state)
1179                 bt_device_state ^= state;
1180
1181         if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
1182                 BT_ERR("vconf_set_int failed");
1183 }
1184
1185 int _bt_get_ad_data_by_type(char *in_data, int in_len,
1186                 char in_type, char **data, int *data_len)
1187 {
1188         if (in_data == NULL || data == NULL || data_len == NULL)
1189                 return BLUETOOTH_ERROR_INVALID_PARAM;
1190
1191         if (in_len < 0)
1192                 return BLUETOOTH_ERROR_INVALID_PARAM;
1193
1194         int i;
1195         int len = 0;
1196         int type = 0;
1197
1198         for (i = 0; i < in_len; i++) {
1199                 len = in_data[i];
1200                 if (len <= 0 || i + 1 >= in_len) {
1201                         BT_ERR("Invalid advertising data");
1202                         return BLUETOOTH_ERROR_INVALID_PARAM;
1203                 }
1204
1205                 type = in_data[i + 1];
1206                 if (type == in_type) {
1207                         i = i + 2;
1208                         len--;
1209                         break;
1210                 }
1211
1212                 i += len;
1213                 len = 0;
1214         }
1215
1216         if (i + len > in_len) {
1217                 BT_ERR("Invalid advertising data");
1218                 return BLUETOOTH_ERROR_INVALID_PARAM;
1219         } else if (len == 0) {
1220                 BT_INFO("AD Type 0x%02x data is not set", in_type);
1221                 *data = NULL;
1222                 *data_len = 0;
1223                 return BLUETOOTH_ERROR_NONE;
1224         }
1225
1226         *data = g_memdup(&in_data[i], len);
1227         if (*data == NULL)
1228                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
1229         *data_len = len;
1230
1231         return BLUETOOTH_ERROR_NONE;
1232 }
1233
1234 void _bt_copy_remote_dev_info(bt_remote_dev_info_t *dest, bt_remote_dev_info_t *src)
1235 {
1236         BT_DBG("+");
1237
1238         ret_if(NULL == src);
1239
1240         if (!dest->address) {
1241                 dest->address = g_strdup(src->address);
1242                 BT_DBG("Address: [%s]", dest->address);
1243         }
1244
1245         if (src->name) {
1246                 g_free(dest->name);
1247                 dest->name = g_strdup(src->name);
1248                 BT_DBG("Name: [%s]", dest->name);
1249         }
1250
1251         if (src->alias) {
1252                 g_free(dest->alias);
1253                 dest->alias = g_strdup(src->alias);
1254                 BT_DBG("Alias: [%s]", dest->alias);
1255         }
1256
1257         if (src->uuid_count > 0 && src->uuids) {
1258                 int i;
1259
1260                 /* Release previously allocated UUIDs */
1261                 if (dest->uuid_count > 0 && dest->uuids)
1262                         for (i = 0; i < dest->uuid_count; i++)
1263                                 g_free(dest->uuids[i]);
1264                 g_free(dest->uuids);
1265
1266                 /* Copy new UUID list */
1267                 dest->uuids = g_malloc0(src->uuid_count * sizeof(char *));
1268                 for (i = 0; i < src->uuid_count; i++)
1269                         dest->uuids[i] = g_strdup(src->uuids[i]);
1270
1271                 dest->uuid_count = src->uuid_count;
1272                 BT_DBG("UUID count: %d", dest->uuid_count);
1273         }
1274
1275         if (src->manufacturer_data_len > 0 && src->manufacturer_data) {
1276                 BT_DBG("Manufacturer data length: %d", dest->manufacturer_data_len);
1277                 if (dest->manufacturer_data)
1278                         g_free(dest->manufacturer_data);
1279
1280                 dest->manufacturer_data_len = src->manufacturer_data_len;
1281                 dest->manufacturer_data = g_memdup(
1282                         src->manufacturer_data, src->manufacturer_data_len);
1283                 BT_DBG("Manufacturer data length: %d", dest->manufacturer_data_len);
1284         }
1285
1286         dest->rssi = src->rssi;
1287         dest->class = src->class;
1288         dest->paired = src->paired;
1289         dest->connected = src->connected;
1290         dest->trust = src->trust;
1291         dest->addr_type = src->addr_type;
1292         dest->vid = src->vid;
1293         dest->pid = src->pid;
1294         dest->device_type = src->device_type;
1295         dest->is_alias_set = src->is_alias_set;
1296
1297         BT_DBG("-");
1298 }
1299
1300 #define BT_PERMANENT_LOG_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log.txt"
1301 #define BT_PERMANENT_LOG_OLD_FILE_PATH "/opt/usr/home/owner/media/Downloads/.bt_dump/permanent_log_old.txt"
1302 #define PERMANENT_LOG_MAX (128 * 1024)
1303 void _bt_permanent_log(const char *fmt, ...)
1304 {
1305         FILE *fp;
1306         time_t t;
1307         struct tm *lt;
1308         char time_buf[32];
1309         long lsize = 0;
1310         va_list ap;
1311
1312 #ifndef TIZEN_FEATURE_BT_PERMANENT_LOG
1313         return;
1314 #endif
1315
1316         t = time(&t);
1317         lt = localtime(&t);
1318         strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", lt);
1319
1320         fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "r");
1321         if (fp != NULL) {
1322                 fseek(fp, 0, SEEK_END);
1323                 lsize = ftell(fp);
1324         }
1325
1326         if (lsize > PERMANENT_LOG_MAX) {
1327                 FILE *fp2;
1328                 int character;
1329                 fp2 = fopen(BT_PERMANENT_LOG_OLD_FILE_PATH, "w");
1330                 if (fp2 == NULL)
1331                         goto DONE;
1332                 fseek(fp, 0, SEEK_SET);
1333                 while (!feof(fp)) {
1334                         character = fgetc(fp);
1335                         fputc(character, fp2);
1336                 }
1337                 fclose(fp);
1338                 fclose(fp2);
1339                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "w");
1340         } else {
1341                 if (fp != NULL)
1342                         fclose(fp);
1343                 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "a");
1344         }
1345
1346         if (fp != NULL) {
1347                 char buf[100];
1348                 va_start(ap, fmt);
1349                 vsnprintf(buf, 100, fmt, ap);
1350                 va_end(ap);
1351                 fprintf(fp, "%s %s\n", time_buf, buf);
1352         }
1353
1354 DONE:
1355         if (fp != NULL)
1356                 fclose(fp);
1357 }
1358