2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <net_connection.h>
27 #include <dbus/dbus.h>
33 #include <net_connection.h>
35 #include <eventsystem.h>
36 #include <arpa/inet.h>
39 #include "bluetooth-api.h"
40 #include "bt-service-common.h"
42 #include <oal-manager.h>
43 #include <oal-device-mgr.h>
45 #ifdef TIZEN_FEATURE_BT_IPSP
46 static GDBusProxy *ipsp_proxy;
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;
54 static GDBusProxy *adapter_properties_proxy;
56 static GDBusConnection *system_gconn = NULL;
58 GDBusConnection *_bt_gdbus_init_system_gconn(void)
62 dbus_threads_init_default();
64 if (system_gconn != NULL)
67 system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
70 BT_ERR("Unable to connect to dbus: %s", error->message);
71 g_clear_error(&error);
77 GDBusConnection *_bt_gdbus_get_system_gconn(void)
79 GDBusConnection *local_system_gconn = NULL;
82 if (system_gconn == NULL) {
83 system_gconn = _bt_gdbus_init_system_gconn();
84 } else if (g_dbus_connection_is_closed(system_gconn)) {
86 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
88 if (!local_system_gconn) {
89 BT_ERR("Unable to connect to dbus: %s", error->message);
90 g_clear_error(&error);
93 system_gconn = local_system_gconn;
99 static GDBusProxy *__bt_init_manager_proxy(void)
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);
108 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
110 BT_MANAGER_PATH, BT_MANAGER_INTERFACE, NULL, NULL);
112 retv_if(proxy == NULL, NULL);
114 manager_proxy_g = proxy;
119 static GDBusProxy *__bt_init_adapter_proxy(void)
121 GDBusProxy *manager_proxy;
123 char *adapter_path = NULL;
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);
130 manager_proxy = _bt_get_manager_proxy();
131 retv_if(manager_proxy == NULL, NULL);
133 adapter_path = _bt_get_adapter_path();
134 retv_if(adapter_path == NULL, NULL);
136 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
138 adapter_path, BT_ADAPTER_INTERFACE, NULL, NULL);
140 g_free(adapter_path);
142 retv_if(proxy == NULL, NULL);
144 adapter_proxy_g = proxy;
148 #ifdef TIZEN_FEATURE_BT_IPSP
149 static GDBusProxy *__bt_init_ipsp_proxy(void)
153 GDBusConnection *g_conn;
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,
159 "/org/projectx/bt_ipsp", BT_IPSP_INTERFACE, NULL, NULL);
161 BT_ERR("Unable to get proxy");
169 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
171 GDBusProxy *manager_proxy;
173 char *adapter_path = NULL;
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);
180 manager_proxy = _bt_get_manager_proxy();
181 retv_if(manager_proxy == NULL, NULL);
183 adapter_path = _bt_get_adapter_path();
184 retv_if(adapter_path == NULL, NULL);
186 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
188 adapter_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
190 g_free(adapter_path);
192 retv_if(proxy == NULL, NULL);
194 adapter_properties_proxy = proxy;
199 GDBusConnection *__bt_init_system_gconn(void)
201 if (system_conn == NULL)
202 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
207 GDBusConnection *__bt_init_session_conn(void)
209 if (session_conn == NULL)
210 session_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
215 GDBusConnection *_bt_get_session_gconn(void)
217 return (session_conn) ? session_conn : __bt_init_session_conn();
220 GDBusConnection *_bt_get_system_gconn(void)
222 return (system_conn) ? system_conn : __bt_init_system_gconn();
225 GDBusConnection *_bt_get_system_conn(void)
227 GDBusConnection *g_conn;
229 if (system_conn == NULL)
230 g_conn = __bt_init_system_gconn();
232 g_conn = system_conn;
234 retv_if(g_conn == NULL, NULL);
239 GDBusProxy *_bt_get_manager_proxy(void)
241 if (manager_proxy_g) {
242 const gchar *path = g_dbus_proxy_get_object_path(manager_proxy_g);
244 BT_ERR("Already proxy released hence creating new proxy");
245 return __bt_init_manager_proxy();
247 return manager_proxy_g;
249 return __bt_init_manager_proxy();
251 #ifdef TIZEN_FEATURE_BT_IPSP
252 GDBusProxy *_bt_get_ipsp_proxy(void)
255 const char *path = g_dbus_proxy_get_object_path(ipsp_proxy);
257 BT_ERR("Already proxy released hence creating new proxy");
258 return __bt_init_ipsp_proxy();
262 return __bt_init_ipsp_proxy();
266 static void *__bt_init_net_conn(void)
269 connection_h connection = NULL;
271 if (net_conn == NULL) {
272 result = connection_create(&connection);
274 if (result != CONNECTION_ERROR_NONE ||
275 connection == NULL) {
276 BT_DBG("connection_create() failed: %d", result);
280 net_conn = connection;
285 void *_bt_get_net_conn(void)
287 return (net_conn) ? net_conn : __bt_init_net_conn();
290 GDBusProxy *_bt_get_adapter_proxy(void)
292 if (adapter_proxy_g) {
293 const char *path = g_dbus_proxy_get_object_path(adapter_proxy_g);
295 BT_ERR("Already proxy released hence creating new proxy");
296 return __bt_init_adapter_proxy();
299 return adapter_proxy_g;
301 return __bt_init_adapter_proxy();
305 GDBusProxy *_bt_get_adapter_properties_proxy(void)
307 return (adapter_properties_proxy) ? adapter_properties_proxy :
308 __bt_init_adapter_properties_proxy();
311 static char *__bt_extract_adapter_path(GVariantIter *iter)
313 char *object_path = NULL;
314 GVariantIter *interface_iter;
315 GVariantIter *svc_iter;
316 char *interface_str = NULL;
318 /* Parse the signature: oa{sa{sv}}} */
319 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
322 if (object_path == NULL)
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)
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);
339 char *_bt_get_adapter_path(void)
341 GDBusConnection *conn;
342 GDBusProxy *manager_proxy;
343 GVariant *result = NULL;
344 GVariantIter *iter = NULL;
345 char *adapter_path = NULL;
347 conn = _bt_get_system_conn();
348 retv_if(conn == NULL, NULL);
350 manager_proxy = _bt_get_manager_proxy();
351 retv_if(manager_proxy == NULL, NULL);
353 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
355 G_DBUS_CALL_FLAGS_NONE,
360 BT_ERR("Can't get managed objects");
364 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
365 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
367 adapter_path = __bt_extract_adapter_path(iter);
368 g_variant_iter_free(iter);
369 g_variant_unref(result);
373 void _bt_deinit_bluez_proxy(void)
375 if (manager_proxy_g) {
376 g_object_unref(manager_proxy_g);
377 manager_proxy_g = NULL;
380 if (adapter_proxy_g) {
381 g_object_unref(adapter_proxy_g);
382 adapter_proxy_g = NULL;
384 if (adapter_properties_proxy) {
385 g_object_unref(adapter_properties_proxy);
386 adapter_properties_proxy = NULL;
390 void _bt_deinit_proxys(void)
393 _bt_deinit_bluez_proxy();
396 g_object_unref(system_conn);
401 g_object_unref(session_conn);
406 ret = connection_destroy(net_conn);
409 BT_ERR("connection_destroy failed : %d", ret);
413 void _bt_convert_device_path_to_address(const char *device_path,
414 char *device_address)
416 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
419 ret_if(device_path == NULL);
420 ret_if(device_address == NULL);
422 dev_addr = strstr(device_path, "dev_");
423 if (dev_addr != NULL) {
426 g_strlcpy(address, dev_addr, sizeof(address));
428 while ((pos = strchr(address, '_')) != NULL)
431 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
436 void _bt_convert_addr_string_to_type(unsigned char *addr,
442 ret_if(address == NULL);
443 ret_if(addr == NULL);
445 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
446 addr[i] = strtol(address, &ptr, 16);
447 if (ptr[0] != '\0') {
456 void _bt_convert_addr_type_to_string(char *address,
459 ret_if(address == NULL);
460 ret_if(addr == NULL);
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]);
468 gboolean _bt_compare_adddress(const bluetooth_device_address_t *addr1,
469 const bluetooth_device_address_t *addr2)
471 if (memcmp(&addr1->addr, &addr2->addr, 6) == 0)
477 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
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]);
484 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
487 ret_if(device_class == NULL);
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));
493 if (cod & 0x002000) {
494 device_class->service_class |=
495 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
499 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
501 ret_if(le_dev_info == NULL);
503 g_free(le_dev_info->adv_data);
507 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
514 if (dest == NULL || src == NULL)
515 return BLUETOOTH_ERROR_INVALID_PARAM;
517 BT_DBG("+src : %s", src);
518 BT_DBG("+dest : %s", dest);
521 while (*p != '\0' && i < length) {
522 next = g_utf8_next_char(p);
525 while (count > 0 && ((i + count) < length)) {
532 return BLUETOOTH_ERROR_NONE;
535 gboolean _bt_utf8_validate(char *name)
539 glong items_written = 0;
541 if (FALSE == g_utf8_validate(name, -1, NULL))
544 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
550 if (items_written != g_utf8_strlen(name, -1))
557 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
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;
565 if (device_register_osp_server(type, uuid, path, fd) != 0)
566 return BLUETOOTH_ERROR_INTERNAL;
568 return BLUETOOTH_ERROR_NONE;
571 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
575 #if TODO_40 /* Need to add this function */
576 if (!_bt_agent_unregister_osp_server(type, uuid))
577 return BLUETOOTH_ERROR_INTERNAL;
579 if (device_unregister_osp_server(type, uuid) != 0)
580 return BLUETOOTH_ERROR_INTERNAL;
582 return BLUETOOTH_ERROR_NONE;
585 int _bt_set_socket_non_blocking(int socket_fd)
587 /* Set Nonblocking */
590 arg = fcntl(socket_fd, F_GETFL);
595 if (arg & O_NONBLOCK)
596 BT_ERR("Already Non-blocking \n");
600 if (fcntl(socket_fd, F_SETFL, arg) < 0)
603 return BLUETOOTH_ERROR_NONE;
606 int _bt_set_non_blocking_tty(int sk)
608 struct termios ti = {0,};
611 err = _bt_set_socket_non_blocking(sk);
614 BT_ERR("Error in set non blocking!\n");
618 tcflush(sk, TCIOFLUSH);
620 /* Switch tty to RAW mode */
622 tcsetattr(sk, TCSANOW, &ti);
624 return BLUETOOTH_ERROR_NONE;
627 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
629 char *object_path = NULL;
630 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
632 /* Parse the signature: oa{sa{sv}}} */
633 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
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);
643 char *_bt_get_device_object_path(char *address)
645 char *object_path = NULL;
646 GDBusConnection *conn;
647 GDBusProxy *manager_proxy;
648 GVariant *result = NULL;
649 GVariantIter *iter = NULL;
651 conn = _bt_get_system_conn();
652 retv_if(conn == NULL, NULL);
654 manager_proxy = _bt_get_manager_proxy();
655 retv_if(manager_proxy == NULL, NULL);
657 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
659 G_DBUS_CALL_FLAGS_NONE,
664 BT_ERR("Can't get managed objects");
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);
676 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
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*/
700 const char *_bt_convert_error_to_string(int error)
703 case BLUETOOTH_ERROR_CANCEL:
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:
714 case BLUETOOTH_ERROR_NO_RESOURCES:
715 return "NO_RESOURCES";
716 case BLUETOOTH_ERROR_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:
730 case BLUETOOTH_ERROR_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:
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:
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";
781 const char * _bt_convert_disc_reason_to_string(int reason)
787 return "Connection terminated by local host";
789 return "Remote user terminated connection";
796 void _bt_logging_connection(gboolean connect, int addr_type)
798 static int le_conn = 0;
799 static int le_disc = 0;
800 static int edr_conn = 0;
801 static int edr_disc = 0;
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);
819 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
826 bundle_add_str(b, key, value);
828 ret = eventsystem_send_system_event(event, b);
830 BT_DBG("eventsystem_send_system_event result: %d", ret);
837 void _bt_swap_byte_ordering(char *data, int data_len)
842 ret_if(data == NULL);
843 /* Swap to opposite endian */
844 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
851 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
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];
863 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
864 const char *mask, int data_len)
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];
881 void _bt_copy_remote_dev(bt_remote_dev_info_t *dev_info, remote_device_t *oal_device)
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);
891 if (strlen(oal_device->name) == 0)
892 dev_info->name = NULL;
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);
899 if (strlen(oal_device->alias) == 0) {
900 dev_info->alias = NULL;
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);
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);
918 if (dev_info->uuid_count > 0)
919 dev_info->uuids = g_new0(char *, dev_info->uuid_count);
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]);
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);
935 void _bt_free_remote_dev(bt_remote_dev_info_t *dev_info)
939 ret_if(NULL == dev_info);
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);
948 for (c = 0; c < dev_info->uuid_count; c++)
949 g_free(dev_info->uuids[c]);
950 g_free(dev_info->uuids);
956 static void __bt_get_service_list(bt_remote_dev_info_t *info, bluetooth_device_info_t *dev)
964 ret_if(info == NULL);
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);
978 parts = g_strsplit(uuids[i], "-", -1);
980 if (parts == NULL || parts[0] == NULL)
983 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
986 dev->service_index++;
992 void _bt_copy_remote_device(bt_remote_dev_info_t *rem_dev, bluetooth_device_info_t *dev)
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);
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);
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);
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;
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);
1023 dev->manufacturer_data.data_len = 0;
1029 void _bt_service_print_dev_info(bluetooth_device_info_t *dev_info)
1033 ret_if(dev_info == NULL);
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]);
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]);
1054 void _bt_uuid_to_string(service_uuid_t *p_uuid, char *str)
1056 uint32_t uuid0, uuid4;
1057 uint16_t uuid1, uuid2, uuid3, uuid5;
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);
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));
1073 void _bt_string_to_uuid(char *str, service_uuid_t *p_uuid)
1075 uint32_t uuid0, uuid4;
1076 uint16_t uuid1, uuid2, uuid3, uuid5;
1078 sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
1079 &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
1081 uuid0 = htonl(uuid0);
1082 uuid1 = htons(uuid1);
1083 uuid2 = htons(uuid2);
1084 uuid3 = htons(uuid3);
1085 uuid4 = htonl(uuid4);
1086 uuid5 = htons(uuid5);
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);
1098 void _bt_convert_byte_ordering(char *data, int data_len,
1099 char *converted_data)
1102 char temp[BT_UUID_LEN];
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];
1110 /* Trim string at first non-utf8 char */
1111 void _bt_truncate_non_utf8_chars(char * str)
1114 const char *ptr = NULL;
1116 if (strlen(str) != 0) {
1117 if (!g_utf8_validate(str, -1, &ptr)) {
1118 while (*(str + i) != *ptr)
1125 invocation_info_t* _bt_get_request_info_data(int service_function, char *address)
1128 invocation_info_t *req_info = NULL;
1132 retv_if(NULL == address, NULL);
1134 /* Get method invocation context */
1135 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1137 if (req_info == NULL || req_info->service_function != service_function)
1140 if (!strncasecmp((char *)req_info->user_data, address, BT_ADDRESS_STRING_SIZE))
1148 #ifdef TIZEN_GATT_CLIENT
1149 invocation_info_t* _bt_get_request_info_data_from_function_name(int service_function)
1152 invocation_info_t *req_info = NULL;
1156 /* Get method invocation context */
1157 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
1159 if (req_info == NULL || req_info->service_function != service_function)
1169 void _bt_set_device_values(gboolean connected, int state)
1171 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
1173 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) != 0)
1174 BT_ERR("vconf_get_int failed");
1176 if (connected == TRUE)
1177 bt_device_state |= state;
1178 else if (bt_device_state & state)
1179 bt_device_state ^= state;
1181 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0)
1182 BT_ERR("vconf_set_int failed");
1185 int _bt_get_ad_data_by_type(char *in_data, int in_len,
1186 char in_type, char **data, int *data_len)
1188 if (in_data == NULL || data == NULL || data_len == NULL)
1189 return BLUETOOTH_ERROR_INVALID_PARAM;
1192 return BLUETOOTH_ERROR_INVALID_PARAM;
1198 for (i = 0; i < in_len; i++) {
1200 if (len <= 0 || i + 1 >= in_len) {
1201 BT_ERR("Invalid advertising data");
1202 return BLUETOOTH_ERROR_INVALID_PARAM;
1205 type = in_data[i + 1];
1206 if (type == in_type) {
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);
1223 return BLUETOOTH_ERROR_NONE;
1226 *data = g_memdup(&in_data[i], len);
1228 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
1231 return BLUETOOTH_ERROR_NONE;
1234 void _bt_copy_remote_dev_info(bt_remote_dev_info_t *dest, bt_remote_dev_info_t *src)
1238 ret_if(NULL == src);
1240 if (!dest->address) {
1241 dest->address = g_strdup(src->address);
1242 BT_DBG("Address: [%s]", dest->address);
1247 dest->name = g_strdup(src->name);
1248 BT_DBG("Name: [%s]", dest->name);
1252 g_free(dest->alias);
1253 dest->alias = g_strdup(src->alias);
1254 BT_DBG("Alias: [%s]", dest->alias);
1257 if (src->uuid_count > 0 && src->uuids) {
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);
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]);
1271 dest->uuid_count = src->uuid_count;
1272 BT_DBG("UUID count: %d", dest->uuid_count);
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);
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);
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;
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, ...)
1314 strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", lt);
1316 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "r");
1318 fseek(fp, 0, SEEK_END);
1322 if (lsize > PERMANENT_LOG_MAX) {
1325 fp2 = fopen(BT_PERMANENT_LOG_OLD_FILE_PATH, "w");
1328 fseek(fp, 0, SEEK_SET);
1330 character = fgetc(fp);
1331 fputc(character, fp2);
1335 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "w");
1339 fp = fopen(BT_PERMANENT_LOG_FILE_PATH, "a");
1345 vsnprintf(buf, 100, fmt, ap);
1347 fprintf(fp, "%s %s\n", time_buf, buf);