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.
25 #include <net_connection.h>
26 #include <dbus/dbus.h>
32 #include <net_connection.h>
34 #include <eventsystem.h>
36 #include "bluetooth-api.h"
37 #include "bt-service-common.h"
38 #include "bt-service-agent.h"
40 static GDBusConnection *system_conn;
41 static GDBusConnection *session_conn;
42 static GDBusProxy *manager_proxy;
43 static GDBusProxy *adapter_proxy;
44 static void *net_conn;
46 static GDBusProxy *adapter_properties_proxy;
48 static GDBusConnection *system_gconn = NULL;
50 GDBusConnection *_bt_gdbus_init_system_gconn(void)
54 dbus_threads_init_default();
56 if (system_gconn != NULL)
59 system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
62 BT_ERR("Unable to connect to dbus: %s", error->message);
63 g_clear_error(&error);
69 GDBusConnection *_bt_gdbus_get_system_gconn(void)
71 GDBusConnection *local_system_gconn = NULL;
74 if (system_gconn == NULL) {
75 system_gconn = _bt_gdbus_init_system_gconn();
76 } else if (g_dbus_connection_is_closed(system_gconn)){
78 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
80 if (!local_system_gconn) {
81 BT_ERR("Unable to connect to dbus: %s", error->message);
82 g_clear_error(&error);
85 system_gconn = local_system_gconn;
91 static GDBusProxy *__bt_init_manager_proxy(void)
95 if (system_conn == NULL) {
96 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
97 retv_if(system_conn == NULL, NULL);
100 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
102 BT_MANAGER_PATH, BT_MANAGER_INTERFACE, NULL, NULL);
104 retv_if(proxy == NULL, NULL);
106 manager_proxy = proxy;
111 static GDBusProxy *__bt_init_adapter_proxy(void)
113 GDBusProxy *manager_proxy;
115 char *adapter_path = NULL;
117 if (system_conn == NULL) {
118 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
119 retv_if(system_conn == NULL, NULL);
122 manager_proxy = _bt_get_manager_proxy();
123 retv_if(manager_proxy == NULL, NULL);
125 adapter_path = _bt_get_adapter_path();
126 retv_if(adapter_path == NULL, NULL);
128 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
130 adapter_path, BT_ADAPTER_INTERFACE, NULL, NULL);
132 g_free(adapter_path);
134 retv_if(proxy == NULL, NULL);
136 adapter_proxy = proxy;
141 static GDBusProxy *__bt_init_adapter_properties_proxy(void)
143 GDBusProxy *manager_proxy;
145 char *adapter_path = NULL;
147 if (system_conn == NULL) {
148 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
149 retv_if(system_conn == NULL, NULL);
152 manager_proxy = _bt_get_manager_proxy();
153 retv_if(manager_proxy == NULL, NULL);
155 adapter_path = _bt_get_adapter_path();
156 retv_if(adapter_path == NULL, NULL);
158 proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE,
160 adapter_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
162 g_free(adapter_path);
164 retv_if(proxy == NULL, NULL);
166 adapter_properties_proxy = proxy;
171 GDBusConnection *__bt_init_system_gconn(void)
173 if (system_conn == NULL)
174 system_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
179 GDBusConnection *__bt_init_session_conn(void)
181 if (session_conn == NULL)
182 session_conn =g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
187 GDBusConnection *_bt_get_session_gconn(void)
189 return (session_conn) ? session_conn : __bt_init_session_conn();
192 GDBusConnection *_bt_get_system_gconn(void)
194 return (system_conn) ? system_conn : __bt_init_system_gconn();
197 GDBusConnection *_bt_get_system_conn(void)
199 GDBusConnection *g_conn;
201 if (system_conn == NULL) {
202 g_conn = __bt_init_system_gconn();
204 g_conn = system_conn;
207 retv_if(g_conn == NULL, NULL);
212 GDBusProxy *_bt_get_manager_proxy(void)
215 const gchar *path = g_dbus_proxy_get_object_path(manager_proxy);
217 BT_ERR("Already proxy released hence creating new proxy");
218 return __bt_init_manager_proxy();
220 return manager_proxy;
222 return __bt_init_manager_proxy();
225 static void *__bt_init_net_conn(void)
228 connection_h connection = NULL;
230 if (net_conn == NULL) {
231 result = connection_create(&connection);
233 if (result != CONNECTION_ERROR_NONE ||
234 connection == NULL) {
235 BT_DBG("connection_create() failed: %d", result);
239 net_conn = connection;
244 void *_bt_get_net_conn(void)
246 return (net_conn) ? net_conn : __bt_init_net_conn();
249 GDBusProxy *_bt_get_adapter_proxy(void)
252 const char *path = g_dbus_proxy_get_object_path(adapter_proxy);
254 BT_ERR("Already proxy released hence creating new proxy");
255 return __bt_init_adapter_proxy();
258 return adapter_proxy;
260 return __bt_init_adapter_proxy();
264 GDBusProxy *_bt_get_adapter_properties_proxy(void)
266 return (adapter_properties_proxy) ? adapter_properties_proxy :
267 __bt_init_adapter_properties_proxy();
270 static char *__bt_extract_adapter_path(GVariantIter *iter)
272 char *object_path = NULL;
273 GVariantIter *interface_iter;
274 GVariantIter *svc_iter;
275 char *interface_str = NULL;
277 /* Parse the signature: oa{sa{sv}}} */
278 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
281 if (object_path == NULL)
284 while (g_variant_iter_loop(interface_iter, "{&sa{sv}}",
285 &interface_str, &svc_iter)) {
286 if (g_strcmp0(interface_str, "org.bluez.Adapter1") != 0)
289 BT_DBG("Object Path: %s", object_path);
290 g_variant_iter_free(svc_iter);
291 g_variant_iter_free(interface_iter);
292 return g_strdup(object_path);
298 char *_bt_get_adapter_path(void)
300 GDBusConnection *conn;
301 GDBusProxy *manager_proxy;
302 GVariant *result = NULL;
303 GVariantIter *iter = NULL;
304 char *adapter_path = NULL;
306 conn = _bt_get_system_conn();
307 retv_if(conn == NULL, NULL);
309 manager_proxy = _bt_get_manager_proxy();
310 retv_if(manager_proxy == NULL, NULL);
312 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
314 G_DBUS_CALL_FLAGS_NONE,
319 BT_ERR("Can't get managed objects");
323 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
324 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
326 adapter_path = __bt_extract_adapter_path(iter);
327 g_variant_iter_free(iter);
328 g_variant_unref(result);
332 void _bt_deinit_bluez_proxy(void)
335 g_object_unref(manager_proxy);
336 manager_proxy = NULL;
340 g_object_unref(adapter_proxy);
341 adapter_proxy = NULL;
343 if (adapter_properties_proxy) {
344 g_object_unref(adapter_properties_proxy);
345 adapter_properties_proxy = NULL;
349 void _bt_deinit_proxys(void)
352 _bt_deinit_bluez_proxy();
355 g_object_unref(system_conn);
360 g_object_unref(session_conn);
365 ret = connection_destroy(net_conn);
368 BT_ERR("connection_destroy failed : %d", ret);
372 void _bt_convert_device_path_to_address(const char *device_path,
373 char *device_address)
375 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
378 ret_if(device_path == NULL);
379 ret_if(device_address == NULL);
381 dev_addr = strstr(device_path, "dev_");
382 if (dev_addr != NULL) {
385 g_strlcpy(address, dev_addr, sizeof(address));
387 while ((pos = strchr(address, '_')) != NULL) {
391 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
396 void _bt_convert_addr_string_to_type(unsigned char *addr,
402 ret_if(address == NULL);
403 ret_if(addr == NULL);
405 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
406 addr[i] = strtol(address, &ptr, 16);
407 if (ptr[0] != '\0') {
416 void _bt_convert_addr_type_to_string(char *address,
419 ret_if(address == NULL);
420 ret_if(addr == NULL);
422 snprintf(address, BT_ADDRESS_STRING_SIZE,
423 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
424 addr[0], addr[1], addr[2],
425 addr[3], addr[4], addr[5]);
428 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
430 BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
431 addr->addr[3], addr->addr[4], addr->addr[5]);
434 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
437 ret_if(device_class == NULL);
439 device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
440 device_class->minor_class = (unsigned short)((cod & 0x000000FC));
441 device_class->service_class = (unsigned long)((cod & 0x00FF0000));
443 if (cod & 0x002000) {
444 device_class->service_class |=
445 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
449 void _bt_free_device_info(bt_remote_dev_info_t *dev_info)
453 ret_if(dev_info == NULL);
455 g_free(dev_info->address);
456 g_free(dev_info->name);
457 g_free(dev_info->manufacturer_data);
459 if (dev_info->uuids) {
460 for (i = 0; i < dev_info->uuid_count && dev_info->uuids[i]; i++)
461 g_free(dev_info->uuids[i]);
463 g_free(dev_info->uuids);
469 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info)
471 ret_if(le_dev_info == NULL);
473 g_free(le_dev_info->adv_data);
477 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
484 if (dest == NULL || src == NULL)
485 return BLUETOOTH_ERROR_INVALID_PARAM;
487 BT_DBG("+src : %s", src);
488 BT_DBG("+dest : %s", dest);
491 while (*p != '\0' && i < length) {
492 next = g_utf8_next_char(p);
495 while (count > 0 && ((i + count) < length)) {
502 return BLUETOOTH_ERROR_NONE;
505 gboolean _bt_utf8_validate(char *name)
509 glong items_written = 0;
511 if (FALSE == g_utf8_validate(name, -1, NULL))
514 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
520 if (items_written != g_utf8_strlen(name, -1))
527 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
530 if (!_bt_agent_register_osp_server( type, uuid, path, fd))
531 return BLUETOOTH_ERROR_INTERNAL;
533 return BLUETOOTH_ERROR_NONE;
536 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
539 if (!_bt_agent_unregister_osp_server( type, uuid))
540 return BLUETOOTH_ERROR_INTERNAL;
542 return BLUETOOTH_ERROR_NONE;
545 int _bt_set_socket_non_blocking(int socket_fd)
547 /* Set Nonblocking */
550 arg = fcntl(socket_fd, F_GETFL);
555 if (arg & O_NONBLOCK) {
556 BT_ERR("Already Non-blocking \n");
561 if (fcntl(socket_fd, F_SETFL, arg) < 0)
564 return BLUETOOTH_ERROR_NONE;
567 int _bt_set_non_blocking_tty(int sk)
569 struct termios ti = {0,};
572 err = _bt_set_socket_non_blocking(sk);
575 BT_ERR("Error in set non blocking!\n");
579 tcflush(sk, TCIOFLUSH);
581 /* Switch tty to RAW mode */
583 tcsetattr(sk, TCSANOW, &ti);
585 return BLUETOOTH_ERROR_NONE;
588 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
590 char *object_path = NULL;
591 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
593 /* Parse the signature: oa{sa{sv}}} */
594 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
596 retv_if(object_path == NULL, NULL);
597 _bt_convert_device_path_to_address(object_path, device_address);
598 if (g_strcmp0(address, device_address) == 0) {
599 return g_strdup(object_path);
605 char *_bt_get_device_object_path(char *address)
607 char *object_path = NULL;
608 GDBusConnection *conn;
609 GDBusProxy *manager_proxy;
610 GVariant *result = NULL;
611 GVariantIter *iter = NULL;
613 conn = _bt_get_system_conn();
614 retv_if(conn == NULL, NULL);
616 manager_proxy = _bt_get_manager_proxy();
617 retv_if(manager_proxy == NULL, NULL);
619 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
621 G_DBUS_CALL_FLAGS_NONE,
626 BT_ERR("Can't get managed objects");
630 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
631 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
632 object_path = __bt_extract_device_path(iter, address);
633 g_variant_iter_free(iter);
634 g_variant_unref(result);
638 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type)
640 switch(profile_type) {
641 case BT_PROFILE_CONN_RFCOMM:
642 return strdup(RFCOMM_UUID_STR);
643 case BT_PROFILE_CONN_A2DP:
644 return strdup(A2DP_SINK_UUID);
645 case BT_PROFILE_CONN_A2DP_SINK:
646 return strdup(A2DP_SOURCE_UUID);
647 case BT_PROFILE_CONN_HSP:
648 return strdup(HFP_HS_UUID);
649 case BT_PROFILE_CONN_HID:
650 return strdup(HID_UUID);
651 case BT_PROFILE_CONN_NAP:
652 return strdup(NAP_UUID);
653 case BT_PROFILE_CONN_HFG:
654 return strdup(HFP_AG_UUID);
655 case BT_PROFILE_CONN_GATT:
656 case BT_PROFILE_CONN_ALL: /* NULL UUID will connect to both the audio profiles*/
662 char *_bt_convert_error_to_string(int error)
665 case BLUETOOTH_ERROR_CANCEL:
667 case BLUETOOTH_ERROR_INVALID_PARAM:
668 return "INVALID_PARAMETER";
669 case BLUETOOTH_ERROR_INVALID_DATA:
670 return "INVALID DATA";
671 case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
672 case BLUETOOTH_ERROR_OUT_OF_MEMORY:
673 return "OUT_OF_MEMORY";
674 case BLUETOOTH_ERROR_TIMEOUT:
676 case BLUETOOTH_ERROR_NO_RESOURCES:
677 return "NO_RESOURCES";
678 case BLUETOOTH_ERROR_INTERNAL:
680 case BLUETOOTH_ERROR_NOT_SUPPORT:
681 return "NOT_SUPPORT";
682 case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
683 return "NOT_ENABLED";
684 case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
685 return "ALREADY_ENABLED";
686 case BLUETOOTH_ERROR_DEVICE_BUSY:
687 return "DEVICE_BUSY";
688 case BLUETOOTH_ERROR_ACCESS_DENIED:
689 return "ACCESS_DENIED";
690 case BLUETOOTH_ERROR_MAX_CLIENT:
692 case BLUETOOTH_ERROR_NOT_FOUND:
694 case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
695 return "SERVICE_SEARCH_ERROR";
696 case BLUETOOTH_ERROR_PARING_FAILED:
697 return "PARING_FAILED";
698 case BLUETOOTH_ERROR_NOT_PAIRED:
700 case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
701 return "SERVICE_NOT_FOUND";
702 case BLUETOOTH_ERROR_NOT_CONNECTED:
703 return "NOT_CONNECTED";
704 case BLUETOOTH_ERROR_ALREADY_CONNECT:
705 return "ALREADY_CONNECT";
706 case BLUETOOTH_ERROR_CONNECTION_BUSY:
707 return "CONNECTION_BUSY";
708 case BLUETOOTH_ERROR_CONNECTION_ERROR:
709 return "CONNECTION_ERROR";
710 case BLUETOOTH_ERROR_MAX_CONNECTION:
711 return "MAX_CONNECTION";
712 case BLUETOOTH_ERROR_NOT_IN_OPERATION:
713 return "NOT_IN_OPERATION";
714 case BLUETOOTH_ERROR_CANCEL_BY_USER:
715 return "CANCEL_BY_USER";
716 case BLUETOOTH_ERROR_REGISTRATION_FAILED:
717 return "REGISTRATION_FAILED";
718 case BLUETOOTH_ERROR_IN_PROGRESS:
719 return "IN_PROGRESS";
720 case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
721 return "AUTHENTICATION_FAILED";
722 case BLUETOOTH_ERROR_HOST_DOWN:
724 case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
725 return "END_OF_DEVICE_LIST";
726 case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
727 return "AGENT_ALREADY_EXIST";
728 case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
729 return "AGENT_DOES_NOT_EXIST";
730 case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
731 return "ALREADY_INITIALIZED";
732 case BLUETOOTH_ERROR_PERMISSION_DEINED:
733 return "PERMISSION_DEINED";
734 case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
735 return "ALREADY_DEACTIVATED";
736 case BLUETOOTH_ERROR_NOT_INITIALIZED:
737 return "NOT_INITIALIZED";
743 char * _bt_convert_disc_reason_to_string(int reason)
749 return "Connection terminated by local host";
751 return "Remote user terminated connection";
758 void _bt_logging_connection(gboolean connect, int addr_type)
760 static int le_conn = 0;
761 static int le_disc = 0;
762 static int edr_conn = 0;
763 static int edr_disc = 0;
777 BT_INFO("[PM] Number of LE conn: %d disc: %d, Number of BR/EDR conn: %d disc: %d",
778 le_conn, le_disc, edr_conn, edr_disc);
781 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value)
788 bundle_add_str(b, key, value);
790 ret = eventsystem_send_system_event(event, b);
792 BT_DBG("eventsystem_send_system_event result: %d", ret);
799 void _bt_swap_byte_ordering(char *data, int data_len)
804 ret_if(data == NULL);
805 /* Swap to opposite endian */
806 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
813 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len)
817 retv_if(data1 == NULL, -1);
818 retv_if(data2 == NULL, -1);
819 for (i = 0; i < data_len; i++) {
820 if (data1[i] != data2[i])
821 return data1[i] - data2[i];
825 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
826 const char *mask, int data_len)
831 retv_if(data1 == NULL, -1);
832 retv_if(data2 == NULL, -1);
833 retv_if(mask == NULL, -1);
834 for (i = 0; i < data_len; i++) {
835 a = data1[i] & mask[i];
836 b = data2[i] & mask[i];