4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
31 #include <net_connection.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33 #include <dbus/dbus-glib.h>
34 #include <dbus/dbus.h>
40 #include <net_connection.h>
43 #include <eventsystem.h>
46 #include "bluetooth-api.h"
47 #include "bt-service-common.h"
48 #include "bt-service-agent.h"
50 static GDBusConnection *system_conn;
51 static GDBusConnection *session_conn;
52 static GDBusProxy *manager_proxy;
53 static GDBusProxy *adapter_proxy;
54 static void *net_conn;
56 static GDBusProxy *adapter_properties_proxy;
58 static GDBusConnection *system_gconn = NULL;
60 GDBusConnection *_bt_gdbus_init_system_gconn(void)
64 if (!g_thread_supported()) {
68 // dbus_g_thread_init();
72 if (system_gconn != NULL)
75 system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
78 BT_ERR("Unable to connect to dbus: %s", error->message);
79 g_clear_error(&error);
85 GDBusConnection *_bt_gdbus_get_system_gconn(void)
87 GDBusConnection *local_system_gconn = NULL;
90 if (system_gconn == NULL) {
91 system_gconn = _bt_gdbus_init_system_gconn();
92 } else if (g_dbus_connection_is_closed(system_gconn)){
94 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
96 if (!local_system_gconn) {
97 BT_ERR("Unable to connect to dbus: %s", error->message);
98 g_clear_error(&error);
101 system_gconn = local_system_gconn;
107 static GDBusProxy *__bt_init_manager_proxy(void)
113 if (system_conn == NULL) {
114 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
115 retv_if(system_conn == NULL, NULL);
118 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
120 BT_MANAGER_PATH, BT_MANAGER_INTERFACE, NULL, NULL);
122 retv_if(proxy == NULL, NULL);
124 manager_proxy = proxy;
129 static GDBusProxy *__bt_init_adapter_proxy(void)
131 GDBusProxy *manager_proxy;
133 char *adapter_path = NULL;
137 if (system_conn == NULL) {
138 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
139 retv_if(system_conn == NULL, NULL);
142 manager_proxy = _bt_get_manager_proxy();
143 retv_if(manager_proxy == NULL, NULL);
145 adapter_path = _bt_get_adapter_path();
146 retv_if(adapter_path == NULL, NULL);
148 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
150 adapter_path, BT_ADAPTER_INTERFACE, NULL, NULL);
152 g_free(adapter_path);
154 retv_if(proxy == NULL, NULL);
156 adapter_proxy = proxy;
161 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
163 GDBusProxy *manager_proxy;
165 char *adapter_path = NULL;
169 if (system_conn == NULL) {
170 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
171 retv_if(system_conn == NULL, NULL);
174 manager_proxy = _bt_get_manager_proxy();
175 retv_if(manager_proxy == NULL, NULL);
177 adapter_path = _bt_get_adapter_path();
178 retv_if(adapter_path == NULL, NULL);
180 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
182 adapter_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
184 g_free(adapter_path);
186 retv_if(proxy == NULL, NULL);
188 adapter_properties_proxy = proxy;
193 GDBusConnection *__bt_init_system_gconn(void)
197 if (system_conn == NULL)
198 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
203 GDBusConnection *__bt_init_session_conn(void)
205 if (session_conn == NULL)
206 session_conn =g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
211 GDBusConnection *_bt_get_session_gconn(void)
213 return (session_conn) ? session_conn : __bt_init_session_conn();
216 GDBusConnection *_bt_get_system_gconn(void)
218 return (system_conn) ? system_conn : __bt_init_system_gconn();
221 GDBusConnection *_bt_get_system_conn(void)
223 GDBusConnection *g_conn;
225 if (system_conn == NULL) {
226 g_conn = __bt_init_system_gconn();
228 g_conn = system_conn;
231 retv_if(g_conn == NULL, NULL);
236 GDBusProxy *_bt_get_manager_proxy(void)
239 const gchar *path = g_dbus_proxy_get_object_path(manager_proxy);
241 BT_ERR("Already proxy released hence creating new proxy");
242 return __bt_init_manager_proxy();
244 return manager_proxy;
246 return __bt_init_manager_proxy();
249 static void *__bt_init_net_conn(void)
252 connection_h connection = NULL;
254 if (net_conn == NULL) {
255 result = connection_create(&connection);
257 if (result != CONNECTION_ERROR_NONE ||
258 connection == NULL) {
259 BT_DBG("connection_create() failed: %d", result);
263 net_conn = connection;
268 void *_bt_get_net_conn(void)
270 return (net_conn) ? net_conn : __bt_init_net_conn();
273 GDBusProxy *_bt_get_adapter_proxy(void)
276 const char *path = g_dbus_proxy_get_object_path(adapter_proxy);
278 BT_ERR("Already proxy released hence creating new proxy");
279 return __bt_init_adapter_proxy();
282 return adapter_proxy;
284 return __bt_init_adapter_proxy();
288 GDBusProxy *_bt_get_adapter_properties_proxy(void)
290 return (adapter_properties_proxy) ? adapter_properties_proxy :
291 __bt_init_adapter_properties_proxy();
294 static char *__bt_extract_adapter_path(GVariantIter *iter)
296 char *object_path = NULL;
297 GVariantIter *interface_iter;
298 GVariantIter *svc_iter;
299 char *interface_str = NULL;
301 /* Parse the signature: oa{sa{sv}}} */
302 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
305 if (object_path == NULL)
308 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
309 &interface_str, &svc_iter)) {
310 if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
313 BT_DBG("Object Path: %s", object_path);
314 g_free(interface_str);
315 g_variant_iter_free(svc_iter);
316 g_variant_iter_free(interface_iter);
317 return g_strdup(object_path);
323 char *_bt_get_adapter_path(void)
325 GDBusConnection *conn;
326 GDBusProxy *manager_proxy;
327 GVariant *result = NULL;
328 GVariantIter *iter = NULL;
329 char *adapter_path = NULL;
331 conn = _bt_get_system_conn();
332 retv_if(conn == NULL, NULL);
334 manager_proxy = _bt_get_manager_proxy();
335 retv_if(manager_proxy == NULL, NULL);
337 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
339 G_DBUS_CALL_FLAGS_NONE,
344 BT_ERR("Can't get managed objects");
348 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
349 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
351 adapter_path = __bt_extract_adapter_path(iter);
352 g_variant_iter_free(iter);
353 g_variant_unref(result);
357 void _bt_deinit_bluez_proxy(void)
360 g_object_unref(manager_proxy);
361 manager_proxy = NULL;
365 g_object_unref(adapter_proxy);
366 adapter_proxy = NULL;
368 if (adapter_properties_proxy) {
369 g_object_unref(adapter_properties_proxy);
370 adapter_properties_proxy = NULL;
374 void _bt_deinit_proxys(void)
377 _bt_deinit_bluez_proxy();
380 g_object_unref(system_conn);
385 g_object_unref(session_conn);
390 ret = connection_destroy(net_conn);
393 BT_ERR("connection_destroy failed : %d", ret);
397 void _bt_convert_device_path_to_address(const char *device_path,
398 char *device_address)
400 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
403 ret_if(device_path == NULL);
404 ret_if(device_address == NULL);
406 dev_addr = strstr(device_path, "dev_");
407 if (dev_addr != NULL) {
410 g_strlcpy(address, dev_addr, sizeof(address));
412 while ((pos = strchr(address, '_')) != NULL) {
416 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
421 void _bt_convert_addr_string_to_type(unsigned char *addr,
427 ret_if(address == NULL);
428 ret_if(addr == NULL);
430 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
431 addr[i] = strtol(address, &ptr, 16);
432 if (ptr[0] != '\0') {
441 void _bt_convert_addr_type_to_string(char *address,
444 ret_if(address == NULL);
445 ret_if(addr == NULL);
447 snprintf(address, BT_ADDRESS_STRING_SIZE,
448 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
449 addr[0], addr[1], addr[2],
450 addr[3], addr[4], addr[5]);
453 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
455 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
456 addr->addr[3], addr->addr[4], addr->addr[5]);
459 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
462 ret_if(device_class == NULL);
464 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
465 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
466 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
468 if (cod & 0x002000) {
469 device_class->service_class |=
470 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
474 void _bt_free_device_info(bt_remote_dev_info_t *dev_info)
478 ret_if(dev_info == NULL);
480 g_free(dev_info->address);
481 g_free(dev_info->name);
482 g_free(dev_info->manufacturer_data);
484 if (dev_info->uuids) {
485 for (i = 0; i < dev_info->uuid_count && dev_info->uuids[i]; i++)
486 g_free(dev_info->uuids[i]);
488 g_free(dev_info->uuids);
494 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
496 ret_if(le_dev_info == NULL);
498 g_free(le_dev_info->adv_data);
502 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
509 if (dest == NULL || src == NULL)
510 return BLUETOOTH_ERROR_INVALID_PARAM;
512 BT_DBG("+src : %s", src);
513 BT_DBG("+dest : %s", dest);
516 while (*p != '\0' && i < length) {
517 next = g_utf8_next_char(p);
520 while (count > 0 && ((i + count) < length)) {
527 return BLUETOOTH_ERROR_NONE;
530 gboolean _bt_utf8_validate(char *name)
534 glong items_written = 0;
536 if (FALSE == g_utf8_validate(name, -1, NULL))
539 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
545 if (items_written != g_utf8_strlen(name, -1))
552 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
555 if (!_bt_agent_register_osp_server( type, uuid, path, fd))
556 return BLUETOOTH_ERROR_INTERNAL;
558 return BLUETOOTH_ERROR_NONE;
561 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
564 if (!_bt_agent_unregister_osp_server( type, uuid))
565 return BLUETOOTH_ERROR_INTERNAL;
567 return BLUETOOTH_ERROR_NONE;
570 int _bt_set_socket_non_blocking(int socket_fd)
572 /* Set Nonblocking */
575 arg = fcntl(socket_fd, F_GETFL);
580 if (arg & O_NONBLOCK) {
581 BT_ERR("Already Non-blocking \n");
586 if (fcntl(socket_fd, F_SETFL, arg) < 0)
589 return BLUETOOTH_ERROR_NONE;
592 int _bt_set_non_blocking_tty(int sk)
594 struct termios ti = {0,};
597 err = _bt_set_socket_non_blocking(sk);
600 BT_ERR("Error in set non blocking!\n");
604 tcflush(sk, TCIOFLUSH);
606 /* Switch tty to RAW mode */
608 tcsetattr(sk, TCSANOW, &ti);
610 return BLUETOOTH_ERROR_NONE;
613 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
615 char *object_path = NULL;
616 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
618 /* Parse the signature: oa{sa{sv}}} */
619 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
621 retv_if(object_path == NULL, NULL);
622 _bt_convert_device_path_to_address(object_path, device_address);
623 if (g_strcmp0(address, device_address) == 0) {
624 return g_strdup(object_path);
630 char *_bt_get_device_object_path(char *address)
632 char *object_path = NULL;
633 GDBusConnection *conn;
634 GDBusProxy *manager_proxy;
635 GVariant *result = NULL;
636 GVariantIter *iter = NULL;
638 conn = _bt_get_system_conn();
639 retv_if(conn == NULL, NULL);
641 manager_proxy = _bt_get_manager_proxy();
642 retv_if(manager_proxy == NULL, NULL);
644 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
646 G_DBUS_CALL_FLAGS_NONE,
651 BT_ERR("Can't get managed objects");
655 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
656 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
657 object_path = __bt_extract_device_path(iter, address);
658 g_variant_iter_free(iter);
659 g_variant_unref(result);
663 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
665 switch(profile_type) {
666 case BT_PROFILE_CONN_RFCOMM:
667 return strdup(RFCOMM_UUID_STR);
668 case BT_PROFILE_CONN_A2DP:
669 return strdup(A2DP_SINK_UUID);
670 case BT_PROFILE_CONN_A2DP_SINK:
671 return strdup(A2DP_SOURCE_UUID);
672 case BT_PROFILE_CONN_HSP:
673 return strdup(HFP_HS_UUID);
674 case BT_PROFILE_CONN_HID:
675 return strdup(HID_UUID);
676 case BT_PROFILE_CONN_NAP:
677 return strdup(NAP_UUID);
678 case BT_PROFILE_CONN_HFG:
679 return strdup(HFP_AG_UUID);
680 case BT_PROFILE_CONN_GATT:
681 case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
687 char *_bt_convert_error_to_string(int error)
690 case BLUETOOTH_ERROR_CANCEL:
692 case BLUETOOTH_ERROR_INVALID_PARAM:
693 return "INVALID_PARAMETER";
694 case BLUETOOTH_ERROR_INVALID_DATA:
695 return "INVALID DATA";
696 case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
697 case BLUETOOTH_ERROR_OUT_OF_MEMORY:
698 return "OUT_OF_MEMORY";
699 case BLUETOOTH_ERROR_TIMEOUT:
701 case BLUETOOTH_ERROR_NO_RESOURCES:
702 return "NO_RESOURCES";
703 case BLUETOOTH_ERROR_INTERNAL:
705 case BLUETOOTH_ERROR_NOT_SUPPORT:
706 return "NOT_SUPPORT";
707 case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
708 return "NOT_ENABLED";
709 case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
710 return "ALREADY_ENABLED";
711 case BLUETOOTH_ERROR_DEVICE_BUSY:
712 return "DEVICE_BUSY";
713 case BLUETOOTH_ERROR_ACCESS_DENIED:
714 return "ACCESS_DENIED";
715 case BLUETOOTH_ERROR_MAX_CLIENT:
717 case BLUETOOTH_ERROR_NOT_FOUND:
719 case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
720 return "SERVICE_SEARCH_ERROR";
721 case BLUETOOTH_ERROR_PARING_FAILED:
722 return "PARING_FAILED";
723 case BLUETOOTH_ERROR_NOT_PAIRED:
725 case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
726 return "SERVICE_NOT_FOUND";
727 case BLUETOOTH_ERROR_NOT_CONNECTED:
728 return "NOT_CONNECTED";
729 case BLUETOOTH_ERROR_ALREADY_CONNECT:
730 return "ALREADY_CONNECT";
731 case BLUETOOTH_ERROR_CONNECTION_BUSY:
732 return "CONNECTION_BUSY";
733 case BLUETOOTH_ERROR_CONNECTION_ERROR:
734 return "CONNECTION_ERROR";
735 case BLUETOOTH_ERROR_MAX_CONNECTION:
736 return "MAX_CONNECTION";
737 case BLUETOOTH_ERROR_NOT_IN_OPERATION:
738 return "NOT_IN_OPERATION";
739 case BLUETOOTH_ERROR_CANCEL_BY_USER:
740 return "CANCEL_BY_USER";
741 case BLUETOOTH_ERROR_REGISTRATION_FAILED:
742 return "REGISTRATION_FAILED";
743 case BLUETOOTH_ERROR_IN_PROGRESS:
744 return "IN_PROGRESS";
745 case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
746 return "AUTHENTICATION_FAILED";
747 case BLUETOOTH_ERROR_HOST_DOWN:
749 case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
750 return "END_OF_DEVICE_LIST";
751 case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
752 return "AGENT_ALREADY_EXIST";
753 case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
754 return "AGENT_DOES_NOT_EXIST";
755 case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
756 return "ALREADY_INITIALIZED";
757 case BLUETOOTH_ERROR_PERMISSION_DEINED:
758 return "PERMISSION_DEINED";
759 case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
760 return "ALREADY_DEACTIVATED";
761 case BLUETOOTH_ERROR_NOT_INITIALIZED:
762 return "NOT_INITIALIZED";
768 char * _bt_convert_disc_reason_to_string(int reason)
774 return "Connection terminated by local host";
776 return "Remote user terminated connection";
783 void _bt_logging_connection(gboolean connect, int addr_type)
785 static int le_conn = 0;
786 static int le_disc = 0;
787 static int edr_conn = 0;
788 static int edr_disc = 0;
802 BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
803 le_conn, le_disc, edr_conn, edr_disc);
806 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
813 bundle_add_str(b, key, value);
815 ret = eventsystem_request_sending_system_event(event, b);
817 BT_DBG("request_sending_system_event result: %d", ret);
824 void _bt_swap_byte_ordering(char *data, int data_len)
829 ret_if(data == NULL);
830 /* Swap to opposite endian */
831 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
838 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
842 retv_if(data1 == NULL, -1);
843 retv_if(data2 == NULL, -1);
844 for (i = 0; i < data_len; i++) {
845 if (data1[i] != data2[i])
846 return data1[i] - data2[i];
850 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
851 const char *mask, int data_len)
856 retv_if(data1 == NULL, -1);
857 retv_if(data2 == NULL, -1);
858 retv_if(mask == NULL, -1);
859 for (i = 0; i < data_len; i++) {
860 a = data1[i] & mask[i];
861 b = data2[i] & mask[i];