/*
- * bluetooth-frwk
- *
- * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
+#include <sys/socket.h>
+
+
+#include <gio/gunixfdlist.h>
+
#include "bluetooth-api.h"
#include "bluetooth-audio-api.h"
#include "bluetooth-hid-api.h"
#include "bluetooth-media-control.h"
#include "bt-internal-types.h"
+#include "bluetooth-ipsp-api.h"
#include "bt-common.h"
#include "bt-request-sender.h"
#include "bt-event-handler.h"
+#ifdef TIZEN_FEATURE_BT_DPM
+#include "bt-dpm.h"
+#endif
+
+
static bt_user_info_t user_info[BT_MAX_USER_INFO];
-static DBusGConnection *system_conn = NULL;
+static GDBusConnection *system_gdbus_conn = NULL;
+
+
+static guint bus_id;
+
+static GDBusConnection *system_gconn = NULL;
+
+static gboolean bt_enabled = FALSE;
+
+#define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
+
+GDBusConnection *g_bus_get_private_conn(void)
+{
+ GError *error = NULL;
+ char *address;
+ GDBusConnection *private_gconn = NULL;
+
+ address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (address == NULL) {
+ if (error) {
+ BT_ERR("Failed to get bus address: %s", error->message);
+ g_clear_error(&error);
+ }
+ return NULL;
+ }
+
+ private_gconn = g_dbus_connection_new_for_address_sync(address,
+ G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
+ G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+ NULL, /* GDBusAuthObserver */
+ NULL,
+ &error);
+ if (!private_gconn) {
+ if (error) {
+ BT_ERR("Unable to connect to dbus: %s", error->message);
+ g_clear_error(&error);
+ }
+ return NULL;
+ }
+
+ return private_gconn;
+}
+
+GDBusConnection *_bt_gdbus_init_system_gconn(void)
+{
+ dbus_threads_init_default();
+
+ if (system_gconn != NULL)
+ return system_gconn;
+
+ system_gconn = g_bus_get_private_conn();
+
+ return system_gconn;
+}
+
+GDBusConnection *_bt_gdbus_get_system_gconn(void)
+{
+ if (system_gconn == NULL)
+ system_gconn = _bt_gdbus_init_system_gconn();
+ else if (g_dbus_connection_is_closed(system_gconn))
+ system_gconn = g_bus_get_private_conn();
+
+ return system_gconn;
+}
void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
{
user_data);
}
+void _bt_a2dp_source_event_cb(int event, int result, void *param,
+ void *callback, void *user_data)
+{
+ bt_audio_event_param_t bt_event = { 0, };
+ bt_event.event = event;
+ bt_event.result = result;
+ bt_event.param_data = param;
+ if (callback)
+ ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
+ user_data);
+}
+
+void _bt_hf_event_cb(int event, int result, void *param,
+ void *callback, void *user_data)
+{
+ bt_hf_event_param_t bt_event = { 0, };
+ bt_event.event = event;
+ bt_event.result = result;
+ bt_event.param_data = param;
+
+ if (callback)
+ ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
+ user_data);
+}
+
+
void _bt_avrcp_event_cb(int event, int result, void *param,
void *callback, void *user_data)
{
void _bt_convert_addr_string_to_type(unsigned char *addr,
const char *address)
{
- int i;
- char *ptr = NULL;
+ int i;
+ char *ptr = NULL;
ret_if(address == NULL);
ret_if(addr == NULL);
- for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
- addr[i] = strtol(address, &ptr, 16);
- if (ptr != NULL) {
- if (ptr[0] != ':')
- return;
+ for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
+ addr[i] = strtol(address, &ptr, 16);
+ if (ptr[0] != '\0') {
+ if (ptr[0] != ':')
+ return;
- address = ptr + 1;
- }
- }
+ address = ptr + 1;
+ }
+ }
+}
+
+void _bt_convert_addr_string_to_secure_string(char *addr,
+ const char *address)
+{
+ int len;
+
+ ret_if(address == NULL);
+ ret_if(addr == NULL);
+
+ len = strlen(address);
+ ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
+
+ strncpy(addr, address, len);
+
+ addr[len-1] = 'X';
+ addr[len-2] = 'X';
+
+ return;
}
void _bt_convert_addr_type_to_string(char *address,
int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
{
int i;
- char *p = src;
+ const char *p = src;
char *next;
int count;
while (count > 0 && ((i + count) < length)) {
dest[i++] = *p;
p++;
- count --;
+ count--;
}
p = next;
}
return BLUETOOTH_ERROR_NONE;
}
-static char *__bt_extract_adapter_path(DBusMessageIter *msg_iter)
+gboolean _bt_utf8_validate(char *name)
{
- char *object_path = NULL;
- DBusMessageIter value_iter;
+ BT_DBG("+");
+ gunichar2 *u16;
+ glong items_written = 0;
- /* Parse the signature: oa{sa{sv}}} */
- retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
- DBUS_TYPE_OBJECT_PATH, NULL);
+ if (FALSE == g_utf8_validate(name, -1, NULL))
+ return FALSE;
- dbus_message_iter_get_basic(msg_iter, &object_path);
- retv_if(object_path == NULL, NULL);
+ u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
+ if (u16 == NULL)
+ return FALSE;
- /* object array (oa) */
- retv_if(dbus_message_iter_next(msg_iter) == FALSE, NULL);
- retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
- DBUS_TYPE_ARRAY, NULL);
+ g_free(u16);
- dbus_message_iter_recurse(msg_iter, &value_iter);
+ if (items_written != g_utf8_strlen(name, -1))
+ return FALSE;
- /* string array (sa) */
- while (dbus_message_iter_get_arg_type(&value_iter) ==
- DBUS_TYPE_DICT_ENTRY) {
- char *interface_name = NULL;
- DBusMessageIter interface_iter;
+ BT_DBG("-");
+ return TRUE;
+}
- dbus_message_iter_recurse(&value_iter, &interface_iter);
- retv_if(dbus_message_iter_get_arg_type(&interface_iter) !=
- DBUS_TYPE_STRING, NULL);
+static GDBusProxy *profile_gproxy;
+static GDBusConnection *gconn;
+static int latest_id = -1;
+#define BT_RFCOMM_ID_MAX 245
+static gboolean id_used[BT_RFCOMM_ID_MAX];
+GDBusNodeInfo *new_conn_node;
+
+static const gchar rfcomm_agent_xml[] =
+"<node name='/'>"
+" <interface name='org.bluez.Profile1'>"
+" <method name='NewConnection'>"
+" <arg type='o' name='object' direction='in'/>"
+" <arg type='h' name='fd' direction='in'/>"
+" <arg type='a{sv}' name='properties' direction='in'/>"
+" </method>"
+" <method name='RequestDisconnection'>"
+" <arg type='o' name='device' direction='in'/>"
+" </method>"
+" </interface>"
+"</node>";
+
+static void __new_connection_method(GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ BT_DBG("method %s", method_name);
+ if (g_strcmp0(method_name, "NewConnection") == 0) {
+ int index;
+ GDBusMessage *msg;
+ GUnixFDList *fd_list;
+ GVariantBuilder *properties;
+ char *obj_path;
+ char addr[20];
+ bluetooth_device_address_t remote_addr1;
+ bt_new_connection_cb cb = user_data;
+ char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+ int fd;
+
+ g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
+ &properties);
+
+ msg = g_dbus_method_invocation_get_message(invocation);
+ fd_list = g_dbus_message_get_unix_fd_list(msg);
+ if (fd_list == NULL) {
+ GQuark quark = g_quark_from_string("rfcomm-app");
+ GError *err = g_error_new(quark, 0, "No fd in message");
+ g_dbus_method_invocation_return_gerror(invocation, err);
+ g_error_free(err);
+ return;
+ }
- dbus_message_iter_get_basic(&interface_iter, &interface_name);
- if (g_strcmp0(interface_name, "org.bluez.Adapter1") == 0) {
- /* Tizen don't allow the multi-adapter */
- BT_DBG("Found an adapter: %s", object_path);
- return g_strdup(object_path);
+ fd = g_unix_fd_list_get(fd_list, index, NULL);
+ if (fd == -1) {
+ BT_ERR("Invalid fd return");
+ GQuark quark = g_quark_from_string("rfcomm-app");
+ GError *err = g_error_new(quark, 0, "Invalid FD return");
+ g_dbus_method_invocation_return_gerror(invocation, err);
+ g_error_free(err);
+ return;
}
- dbus_message_iter_next(&value_iter);
+ _bt_convert_device_path_to_address(obj_path, addr);
+ _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
+
+ _bt_convert_addr_string_to_secure_string(secure_address, addr);
+ BT_INFO("fd: %d, address %s", fd, secure_address);
+
+ g_dbus_method_invocation_return_value(invocation, NULL);
+
+ if (cb)
+ cb(object_path, fd, &remote_addr1);
+ } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
+ g_dbus_method_invocation_return_value(invocation, NULL);
}
+}
- return NULL;
+
+static const GDBusInterfaceVTable method_table = {
+ __new_connection_method,
+ NULL,
+ NULL,
+};
+
+void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
+{
+ int i;
+
+ for (i = 0; i < 6; i++)
+ dst[i] = src[5-i];
}
-/* TO DO */
-/* Change DBusGConnection to DBusConnection*/
-int _bt_get_adapter_path(DBusGConnection *g_conn, char *path)
+int __rfcomm_assign_id(void)
{
- DBusMessage *msg;
- DBusMessage *reply;
- DBusMessageIter reply_iter;
- DBusMessageIter value_iter;
- DBusError err;
- DBusConnection *conn;
+ int index;
+
+ BT_DBG("latest_id: %d", latest_id);
+
+ index = latest_id + 1;
+
+ if (index >= BT_RFCOMM_ID_MAX)
+ index = 0;
+
+ BT_DBG("index: %d", index);
+
+ while (id_used[index] == TRUE) {
+ if (index == latest_id) {
+ /* No available ID */
+ BT_ERR("All request ID is used");
+ return -1;
+ }
+
+ index++;
+
+ if (index >= BT_RFCOMM_ID_MAX)
+ index = 0;
+ }
+
+ latest_id = index;
+ id_used[index] = TRUE;
+
+ BT_DBG("Assigned Id: %d", latest_id);
+
+ return latest_id;
+}
+
+void __rfcomm_delete_id(int id)
+{
+ ret_if(id >= BT_RFCOMM_ID_MAX);
+ ret_if(id < 0);
+
+ id_used[id] = FALSE;
+
+ /* Next server will use this ID */
+ latest_id = id - 1;
+}
+
+static GDBusConnection *__get_gdbus_connection()
+{
+ if (gconn == NULL)
+ gconn = g_bus_get_private_conn();
+
+ return gconn;
+}
+
+static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
+{
+ GDBusConnection *gconn;
+ GError *err = NULL;
+
+ if (profile_gproxy)
+ return profile_gproxy;
+
+ gconn = __get_gdbus_connection();
+ if (gconn == NULL)
+ return NULL;
+
+ profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_BLUEZ_NAME,
+ "/org/bluez",
+ "org.bluez.ProfileManager1",
+ NULL, &err);
+ if (err) {
+ BT_ERR("Unable to create proxy: %s", err->message);
+ g_clear_error(&err);
+ return NULL;
+ }
+
+ return profile_gproxy;
+}
+
+static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
+{
+ GDBusConnection *gconn;
+ GError *err = NULL;
+ GDBusProxy *device_gproxy;
+
+ gconn = __get_gdbus_connection();
+ if (gconn == NULL)
+ return NULL;
+
+ device_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_BLUEZ_NAME,
+ object_path,
+ BT_DEVICE_INTERFACE,
+ NULL, &err);
+
+ if (err) {
+ BT_ERR("Unable to create proxy: %s", err->message);
+ g_clear_error(&err);
+ return NULL;
+ }
+
+ return device_gproxy;
+}
+
+void _bt_unregister_gdbus(int object_id)
+{
+ GDBusConnection *gconn;
+
+ gconn = __get_gdbus_connection();
+ if (gconn == NULL)
+ return;
+
+ g_dbus_connection_unregister_object(gconn, object_id);
+}
+
+int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
+{
+ GDBusConnection *gconn;
+ int id;
+ GError *error = NULL;
+
+ gconn = __get_gdbus_connection();
+ if (gconn == NULL)
+ return -1;
+
+ if (new_conn_node == NULL)
+ new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
+
+ if (new_conn_node == NULL)
+ return -1;
+
+ id = g_dbus_connection_register_object(gconn, path,
+ new_conn_node->interfaces[0],
+ &method_table,
+ cb, NULL, &error);
+ if (id == 0) {
+ BT_ERR("Failed to register: %s", error->message);
+ g_error_free(error);
+ return -1;
+ }
+
+ BT_DBG("NEW CONNECTION ID %d", id);
+
+ return id;
+}
+
+static GDBusProxy * __bt_gdbus_get_adapter_proxy()
+{
+ GError *err = NULL;
+ GDBusProxy *manager_proxy = NULL;
+ GDBusProxy *adapter_proxy = NULL;
+ GDBusConnection *conn;
+ GVariant *result = NULL;
char *adapter_path = NULL;
- retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+ conn = __get_gdbus_connection();
+ retv_if(conn == NULL, NULL);
- conn = dbus_g_connection_get_connection(g_conn);
- retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+ manager_proxy = g_dbus_proxy_new_sync(conn,
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME,
+ BT_MANAGER_PATH,
+ BT_MANAGER_INTERFACE,
+ NULL, &err);
+
+ if (!manager_proxy) {
+ BT_ERR("Unable to create proxy: %s", err->message);
+ goto fail;
+ }
- msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
- BT_MANAGER_INTERFACE,
- "GetManagedObjects");
+ result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+ if (!result) {
+ if (err != NULL)
+ BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
+ else
+ BT_ERR("Fail to get DefaultAdapter");
- if (msg == NULL) {
- BT_ERR("Can't allocate D-Bus message");
goto fail;
}
- /* Synchronous call */
- dbus_error_init(&err);
- reply = dbus_connection_send_with_reply_and_block(
- conn, msg,
- -1, &err);
- dbus_message_unref(msg);
+ if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
+ BT_ERR("Incorrect result\n");
+ goto fail;
+ }
- if (!reply) {
- BT_ERR("Can't get managed objects");
+ g_variant_get(result, "(&o)", &adapter_path);
- if (dbus_error_is_set(&err)) {
- BT_ERR("%s", err.message);
- dbus_error_free(&err);
- }
+ if (adapter_path == NULL ||
+ strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
+ BT_ERR("Adapter path is inproper\n");
goto fail;
}
- if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
- BT_ERR("Fail to iterate the reply");
- goto fail;
+ BT_INFO("Adapter Path %s", adapter_path);
+
+ adapter_proxy = g_dbus_proxy_new_sync(conn,
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME,
+ adapter_path,
+ BT_ADAPTER_INTERFACE,
+ NULL, &err);
+ if (err) {
+ BT_ERR("DBus Error message: [%s]", err->message);
+ g_clear_error(&err);
}
- dbus_message_iter_recurse(&reply_iter, &value_iter);
+fail:
+ if (manager_proxy)
+ g_object_unref(manager_proxy);
+ if (result)
+ g_variant_unref(result);
+ return adapter_proxy;
+}
- /* signature of GetManagedObjects: a{oa{sa{sv}}} */
- while (dbus_message_iter_get_arg_type(&value_iter) ==
- DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter msg_iter;
+int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb)
+{
+ GDBusConnection *gconn;
+ int id;
+ GError *error = NULL;
+
+ gconn = __get_gdbus_connection();
+ if (gconn == NULL)
+ return -1;
+
+ if (new_conn_node == NULL)
+ new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
+
+ if (new_conn_node == NULL)
+ return -1;
+
+ id = g_dbus_connection_register_object(gconn, path,
+ new_conn_node->interfaces[0],
+ &method_table,
+ cb, NULL, &error);
+ if (id == 0) {
+ BT_ERR("Failed to register: %s", error->message);
+ g_error_free(error);
+ return -1;
+ }
- dbus_message_iter_recurse(&value_iter, &msg_iter);
+ BT_DBG("NEW CONNECTION ID %d", id);
- adapter_path = __bt_extract_adapter_path(&msg_iter);
- if (adapter_path != NULL) {
- BT_DBG("Found the adapter path");
- break;
+ return id;
+}
+
+int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
+{
+ GVariantBuilder *option_builder;
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+ int result = BLUETOOTH_ERROR_NONE;
+
+ proxy = __bt_gdbus_get_profile_proxy();
+ if (proxy == NULL) {
+ BT_ERR("Getting profile proxy failed");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+ if (info->authentication)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthentication",
+ g_variant_new_boolean(TRUE));
+ if (info->authorization)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthorization",
+ g_variant_new_boolean(TRUE));
+ if (info->role)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Role",
+ g_variant_new_string(info->role));
+
+ /* Setting RFCOMM channel to default value 0; would allow bluez to assign
+ * RFCOMM channels based on the availability when two services want
+ * to use the RFCOMM along with SPP. Hence bluez makes sure that no
+ * two services use the same SPP RFCOMM channel. */
+ if (use_default_rfcomm)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Channel",
+ g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
+ if (info->service)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Service",
+ g_variant_new_string(info->service));
+
+ ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
+ g_variant_new("(osa{sv})", info->obj_path,
+ info->uuid,
+ option_builder),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+ if (err) {
+ g_dbus_error_strip_remote_error(err);
+ BT_ERR("RegisterProfile failed: %s", err->message);
+
+ if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
+ result = BLUETOOTH_ERROR_ACCESS_DENIED;
+ else
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ g_clear_error(&err);
+ }
+
+ g_variant_builder_unref(option_builder);
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return result;
+}
+
+int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path)
+{
+ GVariantBuilder *option_builder;
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+ int result = BLUETOOTH_ERROR_NONE;
+
+ proxy = __bt_gdbus_get_profile_proxy();
+ if (proxy == NULL) {
+ BT_ERR("Getting profile proxy failed");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+ if (info->authentication)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthentication",
+ g_variant_new_boolean(TRUE));
+ if (info->authorization)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthorization",
+ g_variant_new_boolean(TRUE));
+ if (info->role)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Role",
+ g_variant_new_string(info->role));
+
+ /* Setting RFCOMM channel to default value 0; would allow bluez to assign
+ * RFCOMM channels based on the availability when two services want
+ * to use the RFCOMM along with SPP. Hence bluez makes sure that no
+ * two services use the same SPP RFCOMM channel. */
+ if (use_default_rfcomm)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Channel",
+ g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
+ if (info->service)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Service",
+ g_variant_new_string(info->service));
+
+ ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile2",
+ g_variant_new("(osssa{sv})", info->obj_path,
+ info->uuid,
+ name,
+ path,
+ option_builder),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+ if (err) {
+ g_dbus_error_strip_remote_error(err);
+ BT_ERR("RegisterProfile failed: %s", err->message);
+
+ if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
+ result = BLUETOOTH_ERROR_ACCESS_DENIED;
+ else
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ g_clear_error(&err);
+ }
+
+ g_variant_builder_unref(option_builder);
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return result;
+}
+
+int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
+{
+ GVariantBuilder *option_builder;
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+ int result = BLUETOOTH_ERROR_NONE;
+
+ proxy = __bt_gdbus_get_profile_proxy();
+ if (proxy == NULL) {
+ BT_ERR("Getting profile proxy failed");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+ if (info->authentication)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthentication",
+ g_variant_new_boolean(TRUE));
+ if (info->authorization)
+ g_variant_builder_add(option_builder, "{sv}",
+ "RequireAuthorization",
+ g_variant_new_boolean(TRUE));
+ if (info->role)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Role",
+ g_variant_new_string(info->role));
+
+ /* Setting RFCOMM channel to default value 0; would allow bluez to assign
+ * RFCOMM channels based on the availability when two services want
+ * to use the RFCOMM along with SPP. Hence bluez makes sure that no
+ * two services use the same SPP RFCOMM channel. */
+ if (use_default_rfcomm)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Channel",
+ g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
+ if (info->service)
+ g_variant_builder_add(option_builder, "{sv}",
+ "Service",
+ g_variant_new_string(info->service));
+
+ ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
+ g_variant_new("(osa{sv})", info->obj_path,
+ info->uuid,
+ option_builder),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+
+ if (err) {
+ g_dbus_error_strip_remote_error(err);
+ BT_ERR("RegisterProfile failed: %s", err->message);
+
+ if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
+ result = BLUETOOTH_ERROR_ACCESS_DENIED;
+ else
+ result = BLUETOOTH_ERROR_INTERNAL;
+
+ g_clear_error(&err);
+ }
+
+ g_variant_builder_unref(option_builder);
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return result;
+}
+
+
+void _bt_unregister_profile(char *path)
+{
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+
+ proxy = __bt_gdbus_get_profile_proxy();
+ if (proxy == NULL) {
+ BT_ERR("Getting profile proxy failed");
+ return;
+ }
+
+ ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
+ g_variant_new("(o)", path),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+ if (err) {
+ BT_ERR("UnregisterProfile failed : %s", err->message);
+ g_clear_error(&err);
+ }
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return;
+}
+
+GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
+{
+ if (bus_id == 0) {
+ char *name = g_strdup_printf("org.bt.frwk%d", getpid());
+
+ bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ name,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+ BT_DBG("Got bus id %d", bus_id);
+ g_free(name);
+ }
+
+ return g_dbus_node_info_new_for_xml(xml_data, NULL);
+}
+
+GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name)
+{
+ if (bus_id == 0) {
+ char *name = g_strdup(bus_name);
+ bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ name,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL);
+ BT_DBG("Got bus id %d", bus_id);
+ g_free(name);
+ }
+
+ return g_dbus_node_info_new_for_xml(xml_data, NULL);
+}
+
+int _bt_connect_profile(char *address, char *uuid, void *cb,
+ gpointer func_data)
+{
+ GDBusProxy *proxy;
+ GDBusProxy *adapter_proxy;
+ char *object_path;
+ GError *err = NULL;
+
+ object_path = _bt_get_device_object_path(address);
+
+ if (object_path == NULL) {
+ GVariant *ret = NULL;
+ BT_ERR("No searched device");
+ adapter_proxy = __bt_gdbus_get_adapter_proxy();
+
+ if (adapter_proxy == NULL) {
+ BT_ERR("adapter proxy is NULL");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
+ g_variant_new("(s)", address),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+
+ if (err != NULL) {
+ BT_ERR("CreateDevice Failed: %s", err->message);
+ g_clear_error(&err);
+ }
+ if (ret)
+ g_variant_unref(ret);
+ g_object_unref(adapter_proxy);
+ object_path = _bt_get_device_object_path(address);
+
+ if (object_path == NULL)
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ proxy = __bt_gdbus_get_device_proxy(object_path);
+ g_free(object_path);
+
+ if (proxy == NULL) {
+ BT_ERR("Error while getting proxy");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ g_dbus_proxy_call(proxy, "ConnectProfile",
+ g_variant_new("(s)", uuid),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ (GAsyncReadyCallback)cb,
+ func_data);
+ BT_DBG("-");
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_discover_services(char *address, char *uuid, void *cb,
+ gpointer func_data)
+{
+ char *object_path;
+ GDBusProxy *proxy;
+ GDBusProxy *adapter_proxy;
+ GError *err = NULL;
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL) {
+ GVariant *ret = NULL;
+ BT_ERR("No searched device");
+ adapter_proxy = __bt_gdbus_get_adapter_proxy();
+ retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+ ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
+ g_variant_new("(s)", address),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err != NULL) {
+ BT_ERR("CreateDevice Failed: %s", err->message);
+ g_clear_error(&err);
}
+ if (ret)
+ g_variant_unref(ret);
+ g_object_unref(adapter_proxy);
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL)
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ proxy = __bt_gdbus_get_device_proxy(object_path);
+ g_free(object_path);
+ if (proxy == NULL) {
+ BT_ERR("Error while getting proxy");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_dbus_proxy_call(proxy, "DiscoverServices",
+ g_variant_new("(s)", uuid),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ (GAsyncReadyCallback)cb,
+ func_data);
+ BT_DBG("-");
+ return BLUETOOTH_ERROR_NONE;
+}
- dbus_message_iter_next(&value_iter);
+int _bt_cancel_discovers(char *address)
+{
+ char *object_path;
+ GDBusProxy *proxy;
+ GDBusProxy *adapter_proxy;
+ GVariant *ret = NULL;
+ GError *err = NULL;
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL) {
+ GVariant *ret = NULL;
+ BT_ERR("No searched device");
+ adapter_proxy = __bt_gdbus_get_adapter_proxy();
+ retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+ ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
+ g_variant_new("(s)", address),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err != NULL) {
+ BT_ERR("CreateDevice Failed: %s", err->message);
+ g_clear_error(&err);
+ }
+ if (ret)
+ g_variant_unref(ret);
+ g_object_unref(adapter_proxy);
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL)
+ return BLUETOOTH_ERROR_INTERNAL;
}
+ proxy = __bt_gdbus_get_device_proxy(object_path);
+ g_free(object_path);
+ ret = g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err) {
+ BT_ERR("DBus Error message: [%s]", err->message);
+ g_clear_error(&err);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ if (ret)
+ g_variant_unref(ret);
+ if (proxy)
+ g_object_unref(proxy);
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_discover_service_uuids(char *address, char *remote_uuid)
+{
+ char *object_path;
+ GDBusProxy *proxy;
+ GDBusConnection *gconn;
+ GError *err = NULL;
+ char **uuid_value = NULL;
+ gsize size = 0;
+ int i = 0;
+ GVariant *value = NULL;
+ GVariant *ret = NULL;
+ int result = BLUETOOTH_ERROR_INTERNAL;
+ BT_INFO("+");
+ retv_if(remote_uuid == NULL, BLUETOOTH_ERROR_INTERNAL);
+ gconn = __get_gdbus_connection();
+ retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
+ object_path = _bt_get_device_object_path(address);
+ retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
+ &err);
+ retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+ if (err) {
+ BT_ERR("DBus Error: [%s]", err->message);
+ g_clear_error(&err);
+ }
+ ret = g_dbus_proxy_call_sync(proxy, "GetAll",
+ g_variant_new("(s)", BT_DEVICE_INTERFACE),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err) {
+ result = BLUETOOTH_ERROR_INTERNAL;
+ BT_ERR("DBus Error : %s", err->message);
+ g_clear_error(&err);
+ goto done;
+ }
+ if (ret == NULL) {
+ BT_ERR("g_dbus_proxy_call_sync function return NULL");
+ result = BLUETOOTH_ERROR_INTERNAL;
+ goto done;
+ }
+
+ g_variant_get(ret, "(@a{sv})", &value);
+ g_variant_unref(ret);
+ if (value) {
+ GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
+ G_VARIANT_TYPE_STRING_ARRAY);
+
+ if (temp_value) {
+ size = g_variant_get_size(temp_value);
+ if (size > 0) {
+ uuid_value = (char **)g_variant_get_strv(temp_value, &size);
+ BT_DBG("Size items %d", size);
+
+ if (uuid_value) {
+ for (i = 0; uuid_value[i] != NULL; i++) {
+ BT_DBG("Remote uuids %s", uuid_value[i]);
+ if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
+ result = BLUETOOTH_ERROR_NONE;
+ g_variant_unref(temp_value);
+ goto done;
+ }
+ }
+ }
+ }
+ g_variant_unref(temp_value);
+ }
+ }
+done:
+ if (proxy)
+ g_object_unref(proxy);
+ if (value)
+ g_variant_unref(value);
+ if (uuid_value)
+ g_free(uuid_value);
+
+ BT_DBG("-");
+ return result;
+}
+
+int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
+{
+ char *object_path;
+ GDBusProxy *proxy;
+ GDBusConnection *gconn;
+ GError *err = NULL;
+ GVariant *value = NULL;
+ GVariant *result = NULL;
+ unsigned int class = 0x00;
+ int ret = BLUETOOTH_ERROR_INTERNAL;
+
+ gconn = __get_gdbus_connection();
+ retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
+ object_path = _bt_get_device_object_path(address);
+
+ retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
+ &err);
+ retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+ if (err) {
+ BT_ERR("DBus Error: [%s]", err->message);
+ g_clear_error(&err);
+ }
+
+ result = g_dbus_proxy_call_sync(proxy, "GetAll",
+ g_variant_new("(s)", BT_DEVICE_INTERFACE),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err) {
+ ret = BLUETOOTH_ERROR_INTERNAL;
+ BT_ERR("DBus Error : %s", err->message);
+ g_clear_error(&err);
+ goto done;
+ }
+ if (result == NULL) {
+ BT_ERR("g_dbus_proxy_call_sync function return NULL");
+ ret = BLUETOOTH_ERROR_INTERNAL;
+ goto done;
+ }
+ g_variant_get(result, "(@a{sv})", &value);
+ g_variant_unref(result);
+ if (value) {
+ GVariant *temp_value = g_variant_lookup_value(value, "Class",
+ G_VARIANT_TYPE_UINT32);
+ class = g_variant_get_uint32(temp_value);
+ _bt_divide_device_class(dev_class, class);
+ if (temp_value)
+ g_variant_unref(temp_value);
+ }
+
+done:
+ if (proxy)
+ g_object_unref(proxy);
+ if (value)
+ g_variant_unref(value);
+
+ BT_DBG("-");
+ return ret;
+}
+
+int _bt_disconnect_profile(char *address, char *uuid, void *cb,
+ gpointer func_data)
+{
+ GDBusProxy *proxy;
+ char *object_path;
+ GError *err = NULL;
+ GDBusProxy *adapter_proxy;
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL) {
+ GVariant *ret = NULL;
+ BT_ERR("No searched device");
+ adapter_proxy = __bt_gdbus_get_adapter_proxy();
+ retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+ ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
+ g_variant_new("(s)", address),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ &err);
+ if (err != NULL) {
+ BT_ERR("CreateDevice Failed: %s", err->message);
+ g_error_free(err);
+ }
+ if (ret)
+ g_variant_unref(ret);
+ g_object_unref(adapter_proxy);
+ object_path = _bt_get_device_object_path(address);
+ if (object_path == NULL)
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ proxy = __bt_gdbus_get_device_proxy(object_path);
+ g_free(object_path);
+ if (proxy == NULL) {
+ BT_ERR("Error while getting proxy");
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ g_dbus_proxy_call(proxy, "DisconnectProfile",
+ g_variant_new("(s)", uuid),
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_TIMEOUT, NULL,
+ (GAsyncReadyCallback)cb,
+ func_data);
+ BT_DBG("-");
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_get_adapter_path(GDBusConnection *conn, char *path)
+{
+ GError *err = NULL;
+ GDBusProxy *manager_proxy = NULL;
+ GVariant *result = NULL;
+ char *adapter_path = NULL;
+
+ retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ manager_proxy = g_dbus_proxy_new_sync(conn,
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME,
+ BT_MANAGER_PATH,
+ BT_MANAGER_INTERFACE,
+ NULL, &err);
+
+ if (!manager_proxy) {
+ BT_ERR("Unable to create proxy: %s", err->message);
+ goto fail;
+ }
+
+ result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+ if (!result) {
+ if (err != NULL) {
+ if (!g_strrstr(err->message, "ServiceUnknown"))
+ BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
+ } else {
+ BT_ERR("Fail to get DefaultAdapter");
+ }
+
+ goto fail;
+ }
+
+ if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
+ BT_ERR("Incorrect result\n");
+ goto fail;
+ }
+
+ g_variant_get(result, "(&o)", &adapter_path);
if (adapter_path == NULL ||
- strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
+ strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
BT_ERR("Adapter path is inproper\n");
goto fail;
}
- BT_DBG("adapter path: %s", adapter_path);
-
if (path)
g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
- g_free(adapter_path);
+ g_variant_unref(result);
+ g_object_unref(manager_proxy);
return BLUETOOTH_ERROR_NONE;
fail:
- g_free(adapter_path);
-
- return BLUETOOTH_ERROR_INTERNAL;
-}
+ g_clear_error(&err);
-DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
-{
- DBusGProxy *adapter_proxy = NULL;
+ if (result)
+ g_variant_unref(result);
- retv_if(conn == NULL, NULL);
+ if (manager_proxy)
+ g_object_unref(manager_proxy);
- adapter_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
- BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
+ return BLUETOOTH_ERROR_INTERNAL;
- return adapter_proxy;
}
-void _bt_device_path_to_address(const char *device_path, char *device_address)
+void _bt_convert_device_path_to_address(const char *device_path,
+ char *device_address)
{
char address[BT_ADDRESS_STRING_SIZE] = { 0 };
- char *dev_addr = NULL;
+ char *dev_addr;
- if (!device_path || !device_address)
- return;
+ ret_if(device_path == NULL);
+ ret_if(device_address == NULL);
dev_addr = strstr(device_path, "dev_");
if (dev_addr != NULL) {
dev_addr += 4;
g_strlcpy(address, dev_addr, sizeof(address));
- while ((pos = strchr(address, '_')) != NULL) {
+ while ((pos = strchr(address, '_')) != NULL)
*pos = ':';
- }
g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
}
}
-DBusGConnection *__bt_init_system_gconn(void)
+static char *__bt_extract_device_path(GVariantIter *iter, char *address)
{
- g_type_init();
+ char *object_path = NULL;
+ char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+ /* Parse the signature: oa{sa{sv}}} */
+ while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
+ NULL)) {
+ retv_if(object_path == NULL, NULL);
+ _bt_convert_device_path_to_address(object_path, device_address);
+
+ if (g_strcmp0(address, device_address) == 0)
+ return g_strdup(object_path);
+ }
+ return NULL;
+}
+
+char *_bt_get_device_object_path(char *address)
+{
+ GError *err = NULL;
+ GDBusProxy *proxy = NULL;
+ GVariant *result = NULL;
+ GVariantIter *iter = NULL;
+ GDBusConnection *conn = NULL;
+ char *object_path = NULL;
+
+ conn = _bt_gdbus_get_system_gconn();
+ retv_if(conn == NULL, NULL);
+
+ proxy = g_dbus_proxy_new_sync(conn,
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME,
+ BT_MANAGER_PATH,
+ BT_MANAGER_INTERFACE,
+ NULL, &err);
+
+ if (!proxy) {
+ BT_ERR("Unable to create proxy: %s", err->message);
+ goto fail;
+ }
+
+ result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+ if (!result) {
+ if (err != NULL)
+ BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
+ else
+ BT_ERR("Fail to get GetManagedObjects");
+
+ goto fail;
+ }
+
+ g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
+ object_path = __bt_extract_device_path(iter, address);
- if (system_conn == NULL)
- system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
+ g_variant_unref(result);
+ g_object_unref(proxy);
+ g_variant_iter_free(iter);
+ return object_path;
- return system_conn;
+fail:
+ g_clear_error(&err);
+
+ if (proxy)
+ g_object_unref(proxy);
+
+ return object_path;
}
-DBusGConnection *_bt_get_system_gconn(void)
+GDBusConnection *_bt_init_system_gdbus_conn(void)
{
- return (system_conn) ? system_conn : __bt_init_system_gconn();
+ GError *error = NULL;
+ if (system_gdbus_conn == NULL) {
+ system_gdbus_conn =
+ g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (error) {
+ BT_ERR("GDBus connection Error : %s \n",
+ error->message);
+ g_clear_error(&error);
+ return NULL;
+ }
+ }
+ return system_gdbus_conn;
}
-DBusConnection *_bt_get_system_conn(void)
+
+int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
{
- DBusGConnection *g_conn;
+ int ret;
+ char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
+ char path_str[BLUETOOTH_PATH_STRING] = { 0, };
+
+ BT_DBG("+");
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ g_array_append_vals(in_param1, &type, sizeof(int));
+ g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
+ g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
+ g_strlcpy(path_str, path, sizeof(path_str));
+ g_array_append_vals(in_param3, &path_str, BLUETOOTH_PATH_STRING);
+ g_array_append_vals(in_param4, &fd, sizeof(int));
+
+ ret = _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ BT_DBG("-");
+ return ret;
+}
- if (system_conn == NULL) {
- g_conn = __bt_init_system_gconn();
- } else {
- g_conn = system_conn;
- }
+int _bt_unregister_osp_server_in_agent(int type, char *uuid)
+{
+ int ret;
+ char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
+
+ BT_DBG("+");
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ g_array_append_vals(in_param1, &type, sizeof(int));
+ g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
+ g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
+
+ ret = _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ BT_DBG("-");
+ return ret;
+}
+
+int _bt_check_privilege(int service_type, int service_function)
+{
+ int result;
+
+ BT_CHECK_ENABLED(return);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ result = _bt_send_request(service_type, service_function,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+GVariant *_bt_get_managed_objects(void)
+{
+ GDBusConnection *g_conn;
+ GDBusProxy *manager_proxy = NULL;
+ GVariant *result = NULL;
+ GError *error = NULL;
+
+ BT_DBG("+");
+
+ g_conn = _bt_gdbus_get_system_gconn();
retv_if(g_conn == NULL, NULL);
- return dbus_g_connection_get_connection(g_conn);
+ manager_proxy = g_dbus_proxy_new_sync(g_conn,
+ G_DBUS_PROXY_FLAGS_NONE, NULL,
+ BT_BLUEZ_NAME,
+ BT_MANAGER_PATH,
+ BT_MANAGER_INTERFACE,
+ NULL, &error);
+
+ if (error) {
+ BT_ERR("Unable to create proxy: %s", error->message);
+ g_clear_error(&error);
+ return NULL;
+ }
+
+ result = g_dbus_proxy_call_sync(manager_proxy,
+ "GetManagedObjects", NULL,
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &error);
+
+ if (error) {
+ BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
+ g_clear_error(&error);
+ }
+
+ g_object_unref(manager_proxy);
+
+ BT_DBG("-");
+ return result;
+}
+
+gboolean _bt_check_enabled_internal(void)
+{
+ if (bt_enabled == TRUE)
+ return TRUE;
+
+ if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED)
+ bt_enabled = TRUE;
+
+ return bt_enabled;
+}
+
+void _bt_set_adapter_internal_status(gboolean enabled)
+{
+ bt_enabled = enabled;
+}
+
+int _bt_get_uuid_specification_name(const char *uuid, char **name)
+{
+ int i;
+ int offset = 0;
+ int uuid_len = 4;
+ static struct {
+ const char *uuid;
+ const char *specification_name;
+ } bt_uuid_name[] = {
+ /* GATT Services */
+ {"1800", "Generic Access"},
+ {"1801", "Generic Attribute"},
+ {"1802", "Immediate Alert"},
+ {"1803", "Link Loss"},
+ {"1804", "Tx Power"},
+ {"1805", "Current Time Service"},
+ {"1806", "Reference Time Update Service"},
+ {"1807", "Next DST Change Service"},
+ {"1808", "Glucose"},
+ {"1809", "Health Thermometer"},
+ {"180A", "Device Information"},
+ {"180D", "Heart Rate"},
+ {"180F", "Battery Service"},
+ {"1810", "Blood Pressure"},
+ {"1811", "Alert Notification Service"},
+ {"1812", "Human Interface Device"},
+
+ /* GATT Declarations */
+ {"2800", "Primary Service Declaration"},
+ {"2801", "Secondary Service Declaration"},
+ {"2802", "Include Declaration"},
+ {"2803", "Characteristic Declaration"},
+
+ /* GATT Descriptors */
+ {"2900", "Characteristic Extended Properties"},
+ {"2901", "Characteristic User Description"},
+ {"2902", "Client Characteristic Configuration"},
+ {"2903", "Server Characteristic Configuration"},
+ {"2904", "Characteristic Format"},
+ {"2905", "Characteristic Aggregate Formate"},
+ {"2906", "Valid Range"},
+ {"2907", "External Report Reference"},
+ {"2908", "Report Reference"},
+
+ /* GATT Characteristics */
+ {"2A00", "Device Name"},
+ {"2A01", "Appearance"},
+ {"2A02", "Peripheral Privacy Flag"},
+ {"2A03", "Reconnection Address"},
+ {"2A04", "Peripheral Preferred Connection Parameters"},
+ {"2A05", "Service Changed"},
+ {"2A06", "Alert Level"},
+ {"2A07", "Tx Power Level"},
+ {"2A08", "Date Time"},
+ {"2A09", "Day of Week"},
+ {"2A0A", "Day Date Time"},
+ {"2A19", "Battery Level"},
+ {"2A1E", "Intermediate Temperature"},
+ {"2A23", "System ID"},
+ {"2A24", "Model Number String"},
+ {"2A25", "Serial Number String"},
+ {"2A26", "Firmware Revision String"},
+ {"2A27", "Hardware Revision String"},
+ {"2A28", "Software Revision String"},
+ {"2A29", "Manufacturer Name String"},
+ {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
+ {"2A2B", "Current Time"},
+ {"2A37", "Heart Rate Measurement"},
+ {"2A38", "Body Sensor Location"},
+ {"2A3F", "Alert Status"},
+ {"2A46", "New Alert"},
+ {"2A4A", "HID Information"},
+ {"2A4C", "HID Control Point"},
+ {"2A50", "PnP ID"},
+
+ /* Custom uuids */
+ {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
+ {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
+ {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
+ {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
+ {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
+ {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
+ {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+ {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
+ {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
+ {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"},
+ {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"},
+ {NULL, NULL}
+ };
+
+ if (!uuid || !name)
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ if (strlen(uuid) == 36) {
+ if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
+ offset = 4;
+ else {
+ offset = 0;
+ uuid_len = 36;
+ }
+ } else if (strlen(uuid) >= 8)
+ offset = 4;
+
+ for (i = 0; bt_uuid_name[i].uuid; i++) {
+ if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len)) {
+ *name = g_strdup(bt_uuid_name[i].specification_name);
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ *name = g_strdup("Unknown");
+ return BLUETOOTH_ERROR_NONE;
}
BT_EXPORT_API int bluetooth_is_supported(void)
fd = open(RFKILL_NODE, O_RDONLY);
if (fd < 0) {
- BT_DBG("Fail to open RFKILL node");
+ BT_ERR("Fail to open RFKILL node");
return BLUETOOTH_ERROR_INTERNAL;
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
- BT_DBG("Fail to set RFKILL node to non-blocking");
+ BT_ERR("Fail to set RFKILL node to non-blocking");
close(fd);
return BLUETOOTH_ERROR_INTERNAL;
}
while (1) {
len = read(fd, &event, sizeof(event));
if (len < 0) {
- BT_DBG("Fail to read events");
+ BT_ERR("Fail to read events");
break;
}
if (len != RFKILL_EVENT_SIZE) {
- BT_DBG("The size is wrong\n");
+ BT_ERR("The size is wrong\n");
continue;
}
{
int ret;
- __bt_init_system_gconn();
+ _bt_gdbus_init_system_gconn();
ret = _bt_init_event_handler();
-
if (ret != BLUETOOTH_ERROR_NONE &&
ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
BT_ERR("Fail to init the event handler");
return ret;
}
+
_bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
/* Register All events */
- _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
- _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
- _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
- _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
- _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
+ ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+ ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+ ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+ ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+ ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+
+#ifdef GATT_NO_RELAY
+ ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
+#endif
+ }
+
+ _bt_register_name_owner_changed();
return BLUETOOTH_ERROR_NONE;
+fail:
+ BT_ERR("Fail to do _bt_register_event()");
+ bluetooth_unregister_callback();
+ return ret;
}
BT_EXPORT_API int bluetooth_unregister_callback(void)
{
- _bt_unregister_event(BT_ADAPTER_EVENT);
- _bt_unregister_event(BT_DEVICE_EVENT);
- _bt_unregister_event(BT_NETWORK_EVENT);
- _bt_unregister_event(BT_RFCOMM_CLIENT_EVENT);
- _bt_unregister_event(BT_RFCOMM_SERVER_EVENT);
+ int ret;
+
+
+ ret = _bt_deinit_event_handler();
+ if (ret != BLUETOOTH_ERROR_NONE)
+ BT_ERR("Fail to deinit the event handler");
+
+ _bt_unregister_name_owner_changed();
_bt_set_user_data(BT_COMMON, NULL, NULL);
- if (system_conn) {
- dbus_g_connection_unref(system_conn);
- system_conn = NULL;
+ if (system_gconn) {
+ g_object_unref(system_gconn);
+ system_gconn = NULL;
}
-
return BLUETOOTH_ERROR_NONE;
}