+int _bt_hal_connect_l2cap_le(char *address,
+ bt_hal_l2cap_le_profile_info_t *info, void *cb, gpointer func_data)
+{
+ GVariantBuilder *option_builder;
+ char *object_path;
+ GDBusProxy *proxy;
+ GDBusConnection *conn;
+ GDBusProxy *adapter_proxy;
+ GError *error = NULL;
+ GVariant *result;
+
+ DBG("+");
+ conn = _bt_hal_get_system_gconn();
+ if (conn == NULL)
+ return BT_HAL_ERROR_INTERNAL;
+
+ object_path = _bt_hal_get_device_object_path(address);
+ if (object_path == NULL) {
+ ERR("No searched device");
+
+ adapter_proxy = _bt_hal_get_adapter_proxy();
+ if (adapter_proxy == NULL)
+ return BT_HAL_ERROR_INTERNAL;
+
+ result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
+ g_variant_new("(s)", address), G_DBUS_CALL_FLAGS_NONE,
+ -1, NULL, &error);
+
+ if (!result) {
+ if (error) {
+ ERR("CreateDevice Fail: %s", error->message);
+ g_error_free(error);
+ }
+ } else
+ g_variant_unref(result);
+
+ object_path = _bt_hal_get_device_object_path(address);
+ }
+
+ if (object_path == NULL) {
+ INFO("Not able to find device object");
+ return BT_HAL_ERROR_INTERNAL;
+ }
+
+ proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+ NULL, BT_HAL_BLUEZ_NAME,
+ object_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
+ g_free(object_path);
+
+ if (proxy == NULL) {
+ INFO("proxy is null");
+ return BT_HAL_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));
+
+ g_dbus_proxy_call(proxy, "ConnectL2capLESocket",
+ g_variant_new("(oia{sv})", info->obj_path,
+ info->psm, option_builder),
+ G_DBUS_CALL_FLAGS_NONE, BT_HAL_MAX_DBUS_TIMEOUT, NULL,
+ (GAsyncReadyCallback)cb, func_data);
+
+ g_variant_builder_unref(option_builder);
+
+ DBG("-");
+ return BT_HAL_ERROR_NONE;
+}
+
+int _bt_hal_listen_l2cap_le(bt_hal_l2cap_le_profile_info_t *info)
+{
+ GVariantBuilder *option_builder;
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+ int result = BT_STATUS_SUCCESS;
+
+ DBG("+");
+ proxy = _bt_hal_get_adapter_proxy();
+ if (proxy == NULL) {
+ ERR("Getting adapter profile proxy failed");
+ return BT_STATUS_FAIL;
+ }
+
+ 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));
+
+ ret = g_dbus_proxy_call_sync(proxy, "ListenL2capLESocket",
+ g_variant_new("(oia{sv})", info->obj_path, info->psm,
+ option_builder),
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+
+ if (err) {
+ ERR("Listen L2cap_LE failed: %s", err->message);
+
+ if (g_strrstr(err->message, BT_HAL_ACCESS_DENIED_MSG))
+ result = BT_STATUS_AUTH_REJECTED;
+ else
+ result = BT_STATUS_FAIL;
+
+ g_clear_error(&err);
+ }
+
+ g_variant_builder_unref(option_builder);
+
+ if (ret)
+ g_variant_unref(ret);
+
+ DBG("-");
+ return result;
+}
+
+void _bt_hal_remove_l2cap_le_socket(char *path)
+{
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+
+ proxy = _bt_hal_get_adapter_proxy();
+ if (proxy == NULL) {
+ ERR("Getting adapter profile proxy failed");
+ return;
+ }
+
+ ret = g_dbus_proxy_call_sync(proxy, "RemoveL2capLESocket",
+ g_variant_new("(o)", path),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+ if (err) {
+ ERR("Remove socket l2cap le failed : %s", err->message);
+ g_clear_error(&err);
+ }
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return;
+}
+
+int _bt_hal_get_psm_l2cap_le(bt_hal_l2cap_le_profile_info_t *info, uint32_t *psm)
+{
+ GVariant *ret;
+ GDBusProxy *proxy;
+ GError *err = NULL;
+ int result = BT_STATUS_SUCCESS;
+
+ proxy = _bt_hal_get_adapter_proxy();
+ if (proxy == NULL) {
+ ERR("Getting adapter profile proxy failed");
+ return BT_STATUS_FAIL;
+ }
+
+ ret = g_dbus_proxy_call_sync(proxy, "GetPSML2capLE",
+ g_variant_new("(o)", info->obj_path),
+ G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, &err);
+ if (err) {
+ ERR("Get PSM L2cap_LE failed: %s", err->message);
+ result = BT_STATUS_FAIL;
+ g_clear_error(&err);
+ return result;
+ }
+
+ g_variant_get(ret, "(u)", psm);
+ INFO("PSM: %d", *psm);
+
+ if (ret)
+ g_variant_unref(ret);
+
+ return result;
+}
+
+static void __hal_l2cap_le_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)
+{
+ INFO("method %s", method_name);
+ if (g_strcmp0(method_name, "NewConnection") == 0) {
+ INFO("L2cap_LE New connection method");
+ int index;
+ int fd;
+ GUnixFDList *fd_list;
+ GDBusMessage *msg;
+ char *obj_path;
+ bt_bdaddr_t remote_addr1;
+ char addr[BT_HAL_ADDRESS_STRING_SIZE];
+ bt_hal_new_connection_cb cb = user_data;
+
+ g_variant_get(parameters, "(oh)", &obj_path, &index);
+
+ msg = g_dbus_method_invocation_get_message(invocation);
+ fd_list = g_dbus_message_get_unix_fd_list(msg);
+ if (fd_list == NULL) {
+ ERR("No fd in message");
+ GQuark quark = g_quark_from_string("l2cap_le-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;
+ }
+
+ fd = g_unix_fd_list_get(fd_list, index, NULL);
+ if (fd == -1) {
+ ERR("Invalid fd return");
+ GQuark quark = g_quark_from_string("l2cap_le-app");
+ GError *err = g_error_new(quark, 0, "Invalid FD return");
+ g_dbus_method_invocation_return_gerror(invocation, err);
+ g_error_free(err);
+ return;
+ }
+
+ _bt_hal_convert_device_path_to_address(obj_path, addr);
+ _bt_hal_convert_addr_string_to_type(remote_addr1.address, (const char *)addr);
+ INFO("Object path %s, fd: %d, address %s", obj_path, fd, addr);
+
+ g_dbus_method_invocation_return_value(invocation, NULL);
+
+ if (cb)
+ cb(object_path, fd, &remote_addr1);
+ else
+ close(fd);
+ }
+}
+