bt-proximity.c
bt-tds.c
bt-otp.c
-bt-mesh.c)
+bt-mesh.c
+bt-l2cap-le-client.c
+bt-l2cap-le-server.c)
SET(HEADERS
bluetooth-api.h
{BT_OTP_WRITE_VALUE, "BT_OTP_WRITE_VALUE"},
{BT_LE_OTC_CONNECT, "BT_LE_OTC_CONNECT"},
{BT_LE_OTC_DISCONNECT, "BT_LE_OTC_DISCONNECT"},
+ {BT_L2CAP_LE_CLIENT_CONNECT, "BT_L2CAP_LE_CLIENT_CONNECT"},
+ {BT_L2CAP_LE_CLIENT_CANCEL_CONNECT, "BT_L2CAP_LE_CLIENT_CANCEL_CONNECT"},
+ {BT_L2CAP_LE_CLIENT_IS_CONNECTED, "BT_L2CAP_LE_CLIENT_IS_CONNECTED"},
+ {BT_L2CAP_LE_SOCKET_DISCONNECT, "BT_L2CAP_LE_SOCKET_DISCONNECT"},
+ {BT_L2CAP_LE_SOCKET_WRITE, "BT_L2CAP_LE_SOCKET_WRITE"},
+ {BT_L2CAP_LE_CREATE_SOCKET, "BT_L2CAP_LE_CREATE_SOCKET"},
+ {BT_L2CAP_LE_REMOVE_SOCKET, "BT_L2CAP_LE_REMOVE_SOCKET"},
+ {BT_L2CAP_LE_LISTEN_AND_ACCEPT, "BT_L2CAP_LE_LISTEN_AND_ACCEPT"},
+ {BT_L2CAP_LE_LISTEN, "BT_L2CAP_LE_LISTEN"},
+ {BT_L2CAP_LE_IS_PSM_AVAILABLE, "BT_L2CAP_LE_IS_PSM_AVAILABLE"},
+ {BT_L2CAP_LE_ACCEPT_CONNECTION, "BT_L2CAP_LE_ACCEPT_CONNECTION"},
+ {BT_L2CAP_LE_REJECT_CONNECTION, "BT_L2CAP_LE_REJECT_CONNECTION"},
{-1, ""},
};
return result;
}
+int _bt_check_privilege_le(int service_type, int service_function)
+{
+ int result;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ result = _bt_sync_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;
ret = _bt_register_event(BT_HDP_EVENT, (void *)callback_ptr, user_data);
if (ret != BLUETOOTH_ERROR_NONE)
goto fail;
+ ret = _bt_register_event(BT_L2CAP_LE_SERVER_EVENT, (void *)callback_ptr, user_data);
+ if (ret != BLUETOOTH_ERROR_NONE)
+ goto fail;
}
_bt_register_name_owner_changed();
}
}
+void __bt_l2cap_le_server_event_filter(GDBusConnection *connection,
+ const gchar *sender_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *signal_name,
+ GVariant *parameters,
+ gpointer user_data)
+{
+ bt_event_info_t *event_info;
+ int result = BLUETOOTH_ERROR_NONE;
+ event_info = (bt_event_info_t *)user_data;
+ ret_if(event_info == NULL);
+
+ BT_DBG("+");
+
+ if (strcasecmp(object_path, BT_L2CAP_LE_SERVER_PATH) != 0)
+ return;
+ if (strcasecmp(interface_name, BT_EVENT_SERVICE) != 0)
+ return;
+
+ ret_if(signal_name == NULL);
+
+ if (strcasecmp(signal_name, BT_CONNECTION_AUTHORIZED) == 0) {
+ bluetooth_l2cap_le_connection_request_t req_ind;
+ char *address = NULL;
+ int socket_fd = 0;
+ int psm = -1;
+ gboolean auto_accept = FALSE;
+
+ BT_INFO("l2cap_le socket request coming for connection authorization");
+
+ g_variant_get(parameters, "(i&si)", &result, &address, &psm);
+
+ socket_fd = _get_l2cap_le_server_id(psm, &auto_accept);
+ if (0 > socket_fd)
+ return;
+
+ _bt_l2cap_le_server_set_pending_conn(socket_fd, address);
+ if (auto_accept) {
+ BT_DBG("auto accept is active");
+ bluetooth_l2cap_le_accept_connection(socket_fd);
+ return;
+ }
+
+ memset(&req_ind, 0x00, sizeof(bluetooth_l2cap_le_connection_request_t));
+ _bt_convert_addr_string_to_type(req_ind.device_addr.addr,
+ address);
+
+ req_ind.socket_fd = socket_fd;
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_AUTHORIZE,
+ result, &req_ind, event_info->cb, event_info->user_data);
+ }
+ BT_DBG("-");
+}
+
void __bt_hf_agent_event_filter(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
event_func = __bt_mesh_event_filter;
path = BT_MESH_PATH;
break;
+ case BT_L2CAP_LE_SERVER_EVENT:
+ event_func = __bt_l2cap_le_server_event_filter;
+ path = BT_L2CAP_LE_SERVER_PATH;
+ break;
default:
BT_ERR("Unknown event");
return BLUETOOTH_ERROR_INTERNAL;
--- /dev/null
+/*
+ * Copyright (c) 2022 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <string.h>
+#include <gio/gunixfdlist.h>
+#include <sys/socket.h>
+
+#include "bluetooth-api.h"
+#include "bt-internal-types.h"
+
+#include "bt-common.h"
+#include "bt-request-sender.h"
+#include "bt-event-handler.h"
+
+/* Variable for privilege, only for write API,
+ before we should reduce time to bt-service dbus calling
+ -1 : Don't have a permission to access API
+ 0 : Initial value, not yet check
+ 1 : Have a permission to access API
+*/
+static int privilege_token;
+
+GSList *l2cap_le_clients;
+
+typedef struct {
+ int psm;
+ char *remote_addr;
+ int sock_fd;
+ int watch_id;
+} l2cap_le_client_conn_info_t;
+
+static gboolean __is_error_by_disconnect(GError *err)
+{
+ return !g_strcmp0(err->message, "Connection reset by peer") ||
+ !g_strcmp0(err->message, "Connection timed out") ||
+ !g_strcmp0(err->message, "Software caused connection abort");
+}
+
+static l2cap_le_client_conn_info_t *__find_l2cap_le_conn_info_with_fd(int fd)
+{
+ GSList *l;
+
+ BT_DBG("+");
+
+ for (l = l2cap_le_clients; l != NULL; l = l->next) {
+ l2cap_le_client_conn_info_t *info = l->data;
+
+ if (info && info->sock_fd == fd) {
+ BT_INFO("Match found");
+ return info;
+ }
+ }
+
+ BT_DBG("-");
+ return NULL;
+}
+
+static void __l2cap_le_remove_client_conn_info_t(
+ l2cap_le_client_conn_info_t *info)
+{
+ ret_if(info == NULL);
+
+ l2cap_le_clients = g_slist_remove(l2cap_le_clients, info);
+ g_free(info->remote_addr);
+
+ if (info->sock_fd > 0) {
+ shutdown(info->sock_fd, SHUT_RDWR);
+ close(info->sock_fd);
+ }
+
+ if (info->watch_id > 0)
+ g_source_remove(info->watch_id);
+ g_free(info);
+}
+
+static void __bt_l2cap_le_client_disconnected(
+ l2cap_le_client_conn_info_t *conn_info)
+{
+ bluetooth_l2cap_le_disconnection_t disconn_info;
+ bt_event_info_t *event_info = NULL;
+
+ BT_DBG("+");
+
+ ret_if(conn_info == NULL);
+
+ event_info = _bt_event_get_cb_data(BT_L2CAP_LE_CLIENT_EVENT);
+ ret_if(event_info == NULL);
+
+ memset(&disconn_info, 0x00, sizeof(bluetooth_l2cap_le_disconnection_t));
+ disconn_info.device_role = L2CAP_LE_ROLE_CLIENT;
+ disconn_info.socket_fd = conn_info->sock_fd;
+ disconn_info.psm = conn_info->psm;
+ _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
+ conn_info->remote_addr);
+
+ BT_INFO("Disconnection Result[%d] BT_ADDRESS[%s] FD[%d] PSM[%d]",
+ BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
+ conn_info->sock_fd, conn_info->psm);
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DISCONNECTED,
+ BLUETOOTH_ERROR_NONE, &disconn_info,
+ event_info->cb, event_info->user_data);
+
+ BT_DBG("-");
+}
+
+static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
+{
+ bt_event_info_t *event_info;
+ bluetooth_l2cap_le_received_data_t data_r;
+ l2cap_le_client_conn_info_t *conn_info;
+
+ int fd;
+#ifdef TIZEN_BLUEDROID_PORTING
+ char len_buf[2] = {0, 0};
+#endif
+ gsize len = 0;
+ char *buffer;
+ GError *err = NULL;
+ GIOStatus status = G_IO_STATUS_NORMAL;
+
+ BT_DBG("+");
+
+ fd = g_io_channel_unix_get_fd(chan);
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR_C("L2cap_le Client disconnected: %d", fd);
+ goto fail;
+ }
+
+#ifdef TIZEN_BLUEDROID_PORTING
+ /* Read buffer length from socket */
+ status = g_io_channel_read_chars(chan, len_buf, sizeof(len_buf), &len, &err);
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR &&
+ !g_strcmp0(err->message,
+ "Connection reset by peer"))
+ BT_ERR("cond : %d", cond);
+
+ g_error_free(err);
+ }
+ goto fail;
+ }
+
+ if (len == 0) {
+ BT_ERR("Length is zero, remote end hang up");
+ goto fail;
+ }
+
+ len = (unsigned char)len_buf[0] + ((unsigned char)len_buf[1] << 8);
+ BT_INFO("lecap_le Recieved buffer len: %d", len);
+
+ /* Read len bytes in buffer from socket */
+ buffer = g_malloc0(len + 1);
+ status = g_io_channel_read_chars(chan, buffer, len, &len, &err);
+#else
+ buffer = g_malloc0(BT_L2CAP_LE_BUFFER_LEN + 1);
+ status = g_io_channel_read_chars(chan, buffer, BT_L2CAP_LE_BUFFER_LEN,
+ &len, &err);
+#endif
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ g_free(buffer);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR &&
+ __is_error_by_disconnect(err)) {
+ BT_ERR("cond : %d", cond);
+ g_error_free(err);
+ goto fail;
+ }
+ g_error_free(err);
+ }
+
+ return TRUE;
+ }
+
+ if (len == 0) {
+ BT_ERR("Length is zero, remote end hang up");
+ g_free(buffer);
+ goto fail;
+ }
+
+ BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
+
+ event_info = _bt_event_get_cb_data(BT_L2CAP_LE_CLIENT_EVENT);
+ if (event_info == NULL) {
+ BT_INFO("event_info == NULL");
+ g_free(buffer);
+ return TRUE;
+ }
+
+ data_r.socket_fd = fd;
+ data_r.buffer_size = len;
+ data_r.buffer = buffer;
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DATA_RECEIVED,
+ BLUETOOTH_ERROR_NONE, &data_r,
+ event_info->cb, event_info->user_data);
+
+ g_free(buffer);
+ return TRUE;
+
+fail:
+ conn_info = __find_l2cap_le_conn_info_with_fd(fd);
+ if (conn_info) {
+ BT_INFO("Disconnecting client, fd %d", fd);
+ close(conn_info->sock_fd);
+ __bt_l2cap_le_client_disconnected(conn_info);
+ __l2cap_le_remove_client_conn_info_t(conn_info);
+ } else {
+ BT_ERR("l2cap_le client conn_info not found");
+ }
+
+ BT_DBG("-");
+ return FALSE;
+}
+
+static void __l2cap_le_client_connection_create_watch(
+ l2cap_le_client_conn_info_t *conn_info)
+{
+ GIOChannel *data_io;
+
+ BT_DBG("+");
+
+ ret_if(NULL == conn_info);
+
+ data_io = g_io_channel_unix_new(conn_info->sock_fd);
+ g_io_channel_set_encoding(data_io, NULL, NULL);
+ g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+ conn_info->watch_id = g_io_add_watch(data_io,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ __client_data_received_cb, NULL);
+ g_io_channel_unref(data_io);
+
+ BT_DBG("-");
+}
+
+static void __bt_l2cap_le_handle_new_client_connection(
+ bluetooth_l2cap_le_connection_t *info)
+{
+ l2cap_le_client_conn_info_t *conn_info;
+
+ BT_DBG("+");
+
+ ret_if(NULL == info);
+
+ conn_info = g_malloc0(sizeof(l2cap_le_client_conn_info_t));
+ conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(
+ conn_info->remote_addr, info->device_addr.addr);
+ conn_info->sock_fd = info->socket_fd;
+ conn_info->psm = info->psm;
+
+ BT_INFO("Address:%s, Socket: %d, psm: %d",
+ conn_info->remote_addr, conn_info->sock_fd, conn_info->psm);
+
+ l2cap_le_clients = g_slist_append(l2cap_le_clients, conn_info);
+ __l2cap_le_client_connection_create_watch(conn_info);
+
+ BT_DBG("-");
+}
+
+static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
+{
+ char *data;
+ int size;
+
+ size = g_variant_get_size(var);
+ if (size > 0) {
+ data = (char *)g_variant_get_data(var);
+ if (data)
+ param = g_array_append_vals(param, data, size);
+
+ }
+}
+
+/* TODO_40 : 4.0 merge */
+/* Don't use this function directly. Instead of it, get the out parameter only */
+static void __bt_get_event_info(int service_function, GArray *output,
+ int *event, int *event_type, void **param_data)
+{
+ ret_if(event == NULL);
+
+ BT_INFO("service_function : %s (0x%x)",
+ _bt_convert_service_function_to_string(service_function),
+ service_function);
+ switch (service_function) {
+ case BT_L2CAP_LE_CLIENT_CONNECT:
+ *event_type = BT_L2CAP_LE_CLIENT_EVENT;
+ *event = BLUETOOTH_EVENT_L2CAP_LE_CONNECTED;
+ ret_if(output == NULL);
+ *param_data = &g_array_index(output,
+ bluetooth_l2cap_le_connection_t, 0);
+ break;
+ default:
+ BT_ERR("Unknown function");
+ return;
+ }
+}
+
+static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy,
+ GAsyncResult *res, gpointer user_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ int event_type = BT_LE_ADAPTER_EVENT;
+ bt_req_info_t *cb_data = user_data;
+ bluetooth_event_param_t bt_event;
+
+ GError *error = NULL;
+ GVariant *value;
+ GVariant *param1;
+ GArray *out_param1 = NULL;
+ GUnixFDList *out_fd_list = NULL;
+
+ BT_DBG("+");
+
+ memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
+
+ value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
+ if (value == NULL) {
+ if (error) {
+ /* dBUS gives error cause */
+ BT_ERR("D-Bus API failure: message[%s]",
+ error->message);
+ g_clear_error(&error);
+ }
+ result = BLUETOOTH_ERROR_TIMEOUT;
+
+ ret_if(cb_data == NULL);
+
+ __bt_get_event_info(cb_data->service_function, NULL,
+ &bt_event.event, &event_type,
+ &bt_event.param_data);
+ goto failed;
+ }
+
+ g_variant_get(value, "(iv)", &result, ¶m1);
+ g_variant_unref(value);
+
+ if (param1) {
+ out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+ __bt_fill_garray_from_variant(param1, out_param1);
+ g_variant_unref(param1);
+ }
+
+ if (!cb_data)
+ goto done;
+
+ __bt_get_event_info(cb_data->service_function, out_param1,
+ &bt_event.event, &event_type,
+ &bt_event.param_data);
+
+ if (result == BLUETOOTH_ERROR_NONE && out_param1) {
+ if (BT_L2CAP_LE_CLIENT_CONNECT == cb_data->service_function) {
+ int *fd_list_array;
+ int len = 0;
+ bluetooth_l2cap_le_connection_t *conn_info;
+
+ conn_info = (bluetooth_l2cap_le_connection_t *)bt_event.param_data;
+ if (!out_fd_list) {
+ BT_ERR("out_fd_list is NULL");
+ goto failed;
+ }
+
+ fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+ BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+ conn_info->socket_fd = fd_list_array[0];
+
+ BT_INFO("conn_info->socket_fd: %d", conn_info->socket_fd);
+ __bt_l2cap_le_handle_new_client_connection(conn_info);
+
+ if (cb_data->cb != NULL) {
+ /* Send client connected event */
+ bt_event.result = result;
+ BT_INFO("send client connected event event_type[%d], result=[%d]", event_type, result);
+ ((bluetooth_cb_func_ptr)cb_data->cb)(
+ bt_event.event, &bt_event, cb_data->user_data);
+ }
+
+ g_free(fd_list_array);
+ g_object_unref(out_fd_list);
+ }
+ goto done;
+ }
+
+failed:
+ if (cb_data->cb == NULL)
+ goto done;
+
+ /* Only if fail case, call the callback function*/
+ bt_event.result = result;
+
+ BT_INFO("send fail event event_type[%d], result=[%d]", event_type, result);
+ if (event_type == BT_L2CAP_LE_CLIENT_EVENT) {
+ BT_INFO("l2cap_le client event");
+ ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
+ &bt_event, cb_data->user_data);
+ } else {
+ BT_INFO("Not handled event type : %d", event_type);
+ }
+done:
+ if (out_param1)
+ g_array_free(out_param1, TRUE);
+
+ g_free(cb_data);
+ BT_DBG("-");
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_connect(
+ const bluetooth_device_address_t *remote_bt_address, int psm)
+{
+ int result;
+ bt_user_info_t *user_info;
+ int t_psm;
+
+ BT_CHECK_PARAMETER(remote_bt_address, return);
+ BT_CHECK_ENABLED_LE(return);
+
+ BT_INFO_C("connect l2cap_le psm %d", psm);
+ user_info = _bt_get_user_data(BT_COMMON);
+ retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+
+ if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_CLIENT_CONNECT)
+ == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ }
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ g_array_append_vals(in_param1, remote_bt_address,
+ sizeof(bluetooth_device_address_t));
+
+ t_psm = psm;
+ g_array_append_vals(in_param2, &t_psm, sizeof(int));
+
+ result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
+ BT_L2CAP_LE_CLIENT_CONNECT,
+ in_param1, in_param2,
+ in_param3, in_param4,
+ user_info->cb, user_info->user_data,
+ NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
+
+ BT_INFO("result: %x", result);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_client_is_connected(
+ const bluetooth_device_address_t *device_address, gboolean *connected)
+{
+ GSList *l;
+ char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+ BT_CHECK_PARAMETER(device_address, return);
+ BT_CHECK_PARAMETER(connected, return);
+
+ BT_DBG("+");
+
+ *connected = FALSE;
+ _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
+ BT_INFO("Client address: [%s]", address);
+
+ for (l = l2cap_le_clients; l != NULL; l = l->next) {
+ l2cap_le_client_conn_info_t *info = l->data;
+
+ if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
+ BT_INFO("Match found");
+ *connected = TRUE;
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ BT_DBG("-");
+ return BLUETOOTH_ERROR_NONE;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_disconnect(int socket_fd)
+{
+ l2cap_le_client_conn_info_t *conn_info;
+
+ BT_INFO_C("<<<<<<<<< @@ayush L2CAP_LE Disconnect request from app >>>>>>>>");
+
+ BT_CHECK_ENABLED_ANY(return);
+ retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
+
+ if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_SOCKET_DISCONNECT)
+ == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("@@ayush Don't have a privilege to use this API");
+ }
+
+ BT_INFO("FD %d", socket_fd);
+
+ conn_info = __find_l2cap_le_conn_info_with_fd(socket_fd);
+ if (conn_info == NULL) {
+ BT_INFO("Could not find in client, so check in server");
+ /* Check for fd in server list and perform the disconnection if present */
+ return bluetooth_l2cap_le_server_disconnect(socket_fd);
+ }
+
+ if (conn_info->watch_id <= 0) {
+ BT_ERR("Invalid state");
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ close(conn_info->sock_fd);
+ __bt_l2cap_le_client_disconnected(conn_info);
+ __l2cap_le_remove_client_conn_info_t(conn_info);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+static int __write_all(int fd, const char *buf, int len)
+{
+ int sent = 0, try = 0;
+
+ BT_DBG("+");
+ while (len > 0) {
+ int written;
+
+ written = write(fd, buf, len);
+ BT_DBG("written: %d, len %d", written, len);
+ if (written < 0) {
+ if (errno == EINTR || errno == EAGAIN) {
+ try++;
+ if (try <= 49)
+ continue;
+ }
+ return -1;
+ }
+
+ if (!written)
+ return 0;
+
+ len -= written;
+ buf += written;
+ sent += written;
+ try = 0;
+ }
+
+ BT_DBG("-");
+ return sent;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_write(int fd, const char *buf, int length)
+{
+ int result;
+
+ BT_CHECK_ENABLED_LE(return);
+ BT_CHECK_PARAMETER(buf, return);
+
+ if (fd < 0) {
+ BT_ERR("Invalid FD");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
+
+ switch (privilege_token) {
+ case 0:
+ result = _bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_SOCKET_WRITE);
+
+ if (result == BLUETOOTH_ERROR_NONE) {
+ privilege_token = 1; /* Have a permission */
+ } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ privilege_token = -1; /* Don't have a permission */
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ } else {
+ BT_ERR("Some error occurred");
+ /* Just break - It is not related with permission error */
+ }
+ break;
+ case 1:
+ /* Already have a privilege */
+ break;
+ case -1:
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ default:
+ /* Invalid privilge token value */
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+
+ result = __write_all(fd, buf, length);
+
+ return result;
+}
--- /dev/null
+/*
+ * Copyright (c) 2022 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <string.h>
+#include <sys/socket.h>
+#include <gio/gunixfdlist.h>
+#include "bluetooth-api.h"
+#include "bt-internal-types.h"
+#include "bt-common.h"
+#include "bt-request-sender.h"
+#include "bt-event-handler.h"
+
+#define SOCK_INT_LEN 4
+
+#ifdef TIZEN_BLUEDROID_PORTING
+#define BLUETOOTH_SOCK_CONNECT_INFO_LEN 20
+#else
+#define BLUETOOTH_SOCK_CONNECT_INFO_LEN 16
+#endif
+
+#define BT_L2CAP_LE_SERVER_ID_MAX 254
+
+typedef struct {
+ char addr[BT_ADDRESS_STRING_SIZE];
+ int sock_fd;
+ int watch_id;
+ int server_id;
+} l2cap_le_remote_client_info_t;
+
+typedef struct {
+ int psm;
+ int server_id;
+ int server_fd;
+ int watch_id;
+ int max_pending_conn;
+ gboolean auto_accept;
+ char pending_addr[BT_ADDRESS_STRING_SIZE];
+ GSList *conn_list;
+} l2cap_le_server_info_t;
+
+static GSList *l2cap_le_servers;
+static gboolean id_used[BT_L2CAP_LE_SERVER_ID_MAX];
+static int latest_id = 0;
+
+int __l2cap_le_assign_server_id(void)
+{
+ int index;
+
+ BT_DBG("latest_id: %d", latest_id);
+
+ index = latest_id + 1;
+ if (index >= BT_L2CAP_LE_SERVER_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_L2CAP_LE_SERVER_ID_MAX)
+ index = 0;
+ }
+
+ latest_id = index;
+ id_used[index] = TRUE;
+ BT_DBG("Assigned Id: %d", latest_id);
+
+ return latest_id;
+}
+
+void __l2cap_le_delete_server_id(int id)
+{
+ ret_if(id >= BT_L2CAP_LE_SERVER_ID_MAX);
+ ret_if(id < 0);
+
+ id_used[id] = FALSE;
+
+ /* Next server will use this ID */
+ latest_id = id - 1;
+}
+
+static l2cap_le_server_info_t *__get_l2cap_le_server_info_from_psm(int psm)
+{
+ GSList *l;
+
+ if (psm < 0)
+ return NULL;
+
+ for (l = l2cap_le_servers; l != NULL; l = l->next) {
+ l2cap_le_server_info_t *info = l->data;
+
+ if (info->psm == psm) {
+ BT_INFO("server found with psm %d fd %d", info->psm,
+ info->server_fd);
+ return info;
+ }
+ }
+
+ return NULL;
+}
+
+int _get_l2cap_le_server_id(int psm, gboolean *auto_accept)
+{
+ l2cap_le_server_info_t *server_info;
+
+ server_info = __get_l2cap_le_server_info_from_psm(psm);
+ if (!server_info)
+ return -1;
+
+ *auto_accept = server_info->auto_accept;
+ return server_info->server_id;
+}
+
+static l2cap_le_server_info_t *__get_l2cap_le_server_info_with_id(int server_id)
+{
+ GSList *l;
+
+ for (l = l2cap_le_servers; l != NULL; l = l->next) {
+ l2cap_le_server_info_t *info = l->data;
+ if (!info)
+ continue;
+
+ BT_DBG("info->server_fd: %d, sock_fd:%d", info->server_id, server_id);
+ if (info->server_id == server_id)
+ return info;
+ }
+
+ return NULL;
+}
+
+void _bt_l2cap_le_server_set_pending_conn(int server_id, char *address)
+{
+ l2cap_le_server_info_t *server_info;
+
+ if (!address)
+ return;
+
+ server_info = __get_l2cap_le_server_info_with_id(server_id);
+ if (!server_info)
+ return;
+
+ g_strlcpy(server_info->pending_addr, address, BT_ADDRESS_STRING_SIZE);
+}
+
+static l2cap_le_remote_client_info_t *__get_l2cap_le_rem_client_info_with_fd(
+ int sock_fd)
+{
+ GSList *l;
+ GSList *l1;
+
+ for (l = l2cap_le_servers; l != NULL; l = l->next) {
+ l2cap_le_server_info_t *info = l->data;
+
+ if (!info)
+ continue;
+
+ for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
+ l2cap_le_remote_client_info_t *client_info = l1->data;
+ if (!client_info)
+ continue;
+
+ if (client_info->sock_fd == sock_fd)
+ return client_info;
+ }
+ }
+
+ return NULL;
+}
+
+static l2cap_le_remote_client_info_t *__get_l2cap_le_rem_client_info_with_addr(
+ char *addr)
+{
+ GSList *l;
+ GSList *l1;
+
+ retv_if(NULL == addr, NULL);
+
+ for (l = l2cap_le_servers; l != NULL; l = l->next) {
+ l2cap_le_server_info_t *info = l->data;
+
+ if (!info)
+ continue;
+
+ for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
+ l2cap_le_remote_client_info_t *client_info = l1->data;
+ if (!client_info)
+ continue;
+
+ if (!strncasecmp(client_info->addr, addr, strlen(client_info->addr)))
+ return client_info;
+ }
+ }
+
+ return NULL;
+}
+
+static void __remove_remote_client_info(l2cap_le_remote_client_info_t *rem_client)
+{
+ BT_DBG("+");
+
+ if (rem_client == NULL)
+ return;
+
+ if (0 < rem_client->sock_fd) {
+ shutdown(rem_client->sock_fd, SHUT_RDWR);
+ close(rem_client->sock_fd);
+ }
+
+ if (rem_client->watch_id > 0)
+ g_source_remove(rem_client->watch_id);
+
+ g_free(rem_client);
+
+ BT_DBG("-");
+}
+
+static void __handle_l2cap_le_client_disconnected(
+ l2cap_le_server_info_t *server_info, l2cap_le_remote_client_info_t *rem_client)
+{
+ bluetooth_l2cap_le_disconnection_t disconn_info;
+ bt_event_info_t *event_info;
+
+ BT_DBG("+");
+
+ if (rem_client == NULL || server_info == NULL)
+ return;
+
+ event_info = _bt_event_get_cb_data(BT_L2CAP_LE_SERVER_EVENT);
+ if (event_info == NULL)
+ return;
+
+ memset(&disconn_info, 0x00, sizeof(bluetooth_l2cap_le_disconnection_t));
+ disconn_info.device_role = L2CAP_LE_ROLE_SERVER;
+ disconn_info.psm = server_info->psm;
+ _bt_convert_addr_string_to_type(disconn_info.device_addr.addr, rem_client->addr);
+ BT_DBG("Disconnected FD [%d] PSM [%d]", rem_client->sock_fd, server_info->psm);
+ disconn_info.socket_fd = rem_client->sock_fd;
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DISCONNECTED,
+ BLUETOOTH_ERROR_NONE, &disconn_info,
+ event_info->cb, event_info->user_data);
+ BT_DBG("-");
+}
+
+static void __remove_l2cap_le_server(l2cap_le_server_info_t *info)
+{
+ l2cap_le_remote_client_info_t *client_info;
+
+ BT_DBG("+");
+
+ if (!info)
+ return;
+
+ l2cap_le_servers = g_slist_remove(l2cap_le_servers, info);
+ if (info->conn_list) {
+ do {
+ client_info = info->conn_list->data;
+ if (!client_info)
+ break;
+
+ BT_INFO("Disconnect l2cap_le client fd %d", client_info->sock_fd);
+ info->conn_list = g_slist_remove(info->conn_list, client_info);
+ __handle_l2cap_le_client_disconnected(info, client_info);
+ __remove_remote_client_info(client_info);
+ } while (info->conn_list);
+ }
+
+ if (info->server_fd) {
+ shutdown(info->server_fd, SHUT_RDWR);
+ close(info->server_fd);
+ }
+
+ if (info->watch_id)
+ g_source_remove(info->watch_id);
+
+ __l2cap_le_delete_server_id(info->server_id);
+ g_free(info);
+
+ BT_DBG("-");
+}
+
+static void __connected_cb(l2cap_le_remote_client_info_t *client_info,
+ bt_event_info_t *event_info)
+{
+ bluetooth_l2cap_le_connection_t conn_info;
+ l2cap_le_server_info_t *server_info;
+
+ server_info = __get_l2cap_le_server_info_with_id(client_info->server_id);
+ ret_if(server_info == NULL);
+
+ memset(&conn_info, 0x00, sizeof(bluetooth_l2cap_le_connection_t));
+ conn_info.device_role = L2CAP_LE_ROLE_SERVER;
+ conn_info.socket_fd = client_info->sock_fd;
+ conn_info.psm = server_info->psm;
+ _bt_convert_addr_string_to_type(conn_info.device_addr.addr, client_info->addr);
+ conn_info.server_id = server_info->server_id;
+
+ BT_INFO_C("Connected [L2CAP_LE Server] psm %d", server_info->psm);
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_CONNECTED,
+ BLUETOOTH_ERROR_NONE, &conn_info,
+ event_info->cb, event_info->user_data);
+}
+
+static int __process_cmsg(struct msghdr *msg)
+{
+ int sock_fd = -1;
+ struct cmsghdr *cmsg_ptr = NULL;
+
+ for (cmsg_ptr = CMSG_FIRSTHDR(msg); cmsg_ptr != NULL;
+ cmsg_ptr = CMSG_NXTHDR(msg, cmsg_ptr)) {
+
+ if (cmsg_ptr->cmsg_level != SOL_SOCKET)
+ continue;
+
+ if (cmsg_ptr->cmsg_type == SCM_RIGHTS) {
+ int count
+ = ((cmsg_ptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
+
+ if (count < 0) {
+ BT_ERR("ERROR Invalid count of descriptors");
+ continue;
+ }
+
+ memcpy(&sock_fd, CMSG_DATA(cmsg_ptr), sizeof(sock_fd));
+ BT_DBG("Remote client fd: %d", sock_fd);
+ }
+ }
+ return sock_fd;
+}
+
+static int __sock_read(int server_fd, char *buf, unsigned int len,
+ int *client_fd)
+{
+ int ret;
+ struct msghdr msg;
+ struct iovec iv;
+ struct cmsghdr cmsgbuf[2 * sizeof(struct cmsghdr) + 4];
+ int retryCount = 0;
+
+ retv_if(0 > server_fd, -1);
+
+ BT_INFO("server_fd = %d", server_fd);
+
+ memset(&msg, 0, sizeof(msg));
+ memset(&iv, 0, sizeof(iv));
+
+ iv.iov_base = buf;
+ iv.iov_len = len;
+ msg.msg_iov = &iv;
+ msg.msg_iovlen = 1;
+ msg.msg_control = cmsgbuf;
+ msg.msg_controllen = sizeof(cmsgbuf);
+
+ for (retryCount = 0; retryCount < 5; retryCount++) {
+ ret = recvmsg(server_fd, &msg, 0);
+ BT_DBG("recvmsg ret = %d", ret);
+ if (ret < 0 && errno == EINTR)
+ continue;
+ else
+ break;
+ }
+
+ if (ret < 0 && errno == EPIPE) {
+ /* End of stream, server listining stopped */
+ BT_ERR("EOS errno: %d", errno);
+ return 0;
+ }
+
+ if (ret < 0) {
+ BT_ERR("Ret errno: %d", errno);
+ return -1;
+ }
+
+ if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) {
+ BT_ERR("MSG Flags errno: %d", errno);
+ return -1;
+ }
+
+ if (ret >= 0 && client_fd) {
+ BT_INFO("Connection received");
+ *client_fd = __process_cmsg(&msg);
+ if (*client_fd < 0)
+ BT_ERR("Invalid client_fd received");
+ }
+
+ return ret;
+}
+
+static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
+{
+#ifdef TIZEN_BLUEDROID_PORTING
+ char len_buf[2] = {0, 0};
+#endif
+ char *buffer = NULL;
+ gsize len = 0;
+ int result = BLUETOOTH_ERROR_NONE;
+ bt_event_info_t *event_info;
+ bluetooth_l2cap_le_received_data_t data_r;
+ GIOStatus status = G_IO_STATUS_NORMAL;
+ GError *err = NULL;
+ l2cap_le_remote_client_info_t *client_info = data;
+ l2cap_le_server_info_t *server_info;
+
+ retv_if(client_info == NULL, FALSE);
+
+ server_info = __get_l2cap_le_server_info_with_id(client_info->server_id);
+
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR_C("l2cap_le Server disconnected: %d", client_info->sock_fd);
+ goto fail;
+ }
+
+#ifdef TIZEN_BLUEDROID_PORTING
+ /* Read buffer length from socket */
+ status = g_io_channel_read_chars(chan, len_buf, sizeof(len_buf), &len, &err);
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR &&
+ !g_strcmp0(err->message,
+ "Connection reset by peer"))
+ BT_ERR("cond : %d", cond);
+
+ g_error_free(err);
+ }
+ goto fail;
+ }
+
+ if (len == 0) {
+ BT_ERR("Length is zero, remote end hang up");
+ goto fail;
+ }
+
+ len = (unsigned char)len_buf[0] + ((unsigned char)len_buf[1] << 8);
+ BT_INFO("Recieved buffer len: %d", len);
+
+ /* Read len bytes in buffer from socket */
+ buffer = g_malloc0(len + 1);
+ status = g_io_channel_read_chars(chan, buffer, len, &len, &err);
+#else
+ buffer = g_malloc0(BT_L2CAP_LE_BUFFER_LEN + 1);
+ status = g_io_channel_read_chars(chan, buffer,
+ BT_L2CAP_LE_BUFFER_LEN, &len, &err);
+#endif
+
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ g_free(buffer);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR &&
+ !g_strcmp0(err->message, "Connection reset by peer")) {
+ BT_ERR("cond : %d", cond);
+ g_error_free(err);
+ goto fail;
+ }
+ g_error_free(err);
+ }
+
+ return TRUE;
+ }
+
+ if (len == 0) {
+ BT_ERR("Length is zero, remote end hang up");
+ g_free(buffer);
+ goto fail;
+ }
+
+ event_info = _bt_event_get_cb_data(BT_L2CAP_LE_SERVER_EVENT);
+ if (event_info == NULL) {
+ g_free(buffer);
+ return TRUE;
+ }
+
+ data_r.socket_fd = client_info->sock_fd;
+ data_r.buffer_size = len;
+ data_r.buffer = buffer;
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DATA_RECEIVED,
+ result, &data_r, event_info->cb, event_info->user_data);
+
+ g_free(buffer);
+ return TRUE;
+fail:
+ BT_ERR("Failure occured, remove client connection");
+ server_info->conn_list = g_slist_remove(
+ server_info->conn_list, client_info);
+ __handle_l2cap_le_client_disconnected(server_info, client_info);
+ client_info->watch_id = -1;
+ __remove_remote_client_info(client_info);
+ return FALSE;
+}
+
+static gboolean __new_connection_request_cb(GIOChannel *chan,
+ GIOCondition cond, gpointer data)
+{
+ int len;
+ int size;
+ int channel;
+ int status;
+ int client_fd;
+ char buf[BLUETOOTH_SOCK_CONNECT_INFO_LEN];
+ unsigned char addr[BT_ADDRESS_LENGTH_MAX];
+
+ bt_event_info_t *event_info;
+ GIOChannel *io;
+ l2cap_le_remote_client_info_t *rem_client;
+ l2cap_le_server_info_t *server_info = data;
+
+ if (!server_info) {
+ BT_ERR("Server info is invalid");
+ return FALSE;
+ }
+
+ if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
+ BT_INFO("l2cap_le Server with fd:%d is closed with cond:0x%X",
+ server_info->server_fd, cond);
+ goto err;
+ }
+
+ BT_INFO("Server fd: %d", server_info->server_fd);
+ len = __sock_read( server_info->server_fd, buf,
+ BLUETOOTH_SOCK_CONNECT_INFO_LEN, &client_fd);
+ BT_DBG("Socket Read len: %d", len);
+ if (len == 0) {
+ BT_ERR("Listen stopped");
+ goto err;
+ } else if (len != BLUETOOTH_SOCK_CONNECT_INFO_LEN) {
+ BT_ERR("Read length is not same as socket info length");
+ goto err;
+ }
+
+ len = 0;
+ /* Read size of data */
+ size = buf[len] | (buf[len + 1] << 8);
+ len += 2;
+
+ /* Read bluetooth address */
+ memcpy(addr, buf + len, BT_ADDRESS_LENGTH_MAX);
+ len += BT_ADDRESS_LENGTH_MAX;
+
+ /* Read channel */
+ channel = buf[len] | (buf[len + 1] << 8) |
+ (buf[len + 2] << 16) | (buf[len + 3] << 24);
+ len += 4;
+
+ /* Read status */
+ status = buf[len] | (buf[len + 1] << 8) |
+ (buf[len + 2] << 16) | (buf[len + 3] << 24);
+ len += 4;
+
+ BT_DBG("size: %d, channel: %d, status: %d", size, channel, status);
+
+ rem_client = g_malloc0(sizeof(l2cap_le_remote_client_info_t));
+ rem_client->sock_fd = client_fd;
+ rem_client->server_id = server_info->server_id;
+ _bt_convert_addr_type_to_string(rem_client->addr, addr);
+
+ BT_INFO("New client [%s] connection with socket_fd: %d, server_id: %d",
+ rem_client->addr, rem_client->sock_fd, rem_client->server_id);
+
+ io = g_io_channel_unix_new(rem_client->sock_fd);
+ g_io_channel_set_encoding(io, NULL, NULL);
+ g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+ rem_client->watch_id = g_io_add_watch(io,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ __data_received_cb, rem_client);
+ g_io_channel_unref(io);
+
+ server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
+ event_info = _bt_event_get_cb_data(BT_L2CAP_LE_SERVER_EVENT);
+ if (event_info)
+ __connected_cb(rem_client, event_info);
+
+ return TRUE;
+
+err:
+ /* Error occurred, Remove l2cap_le server*/
+ BT_ERR("some error has occured, remove server");
+ __remove_l2cap_le_server(server_info);
+ return FALSE;
+}
+
+static int __getInt(char *buf, int len)
+{
+ int val = 0;
+
+ if (len != SOCK_INT_LEN)
+ return -1;
+
+ val = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
+ return val;
+}
+
+static int __sock_wait_for_psm(int sock_fd)
+{
+ int readlen = -1;
+ char buf[SOCK_INT_LEN];
+
+ readlen = __sock_read(sock_fd, buf, SOCK_INT_LEN, NULL);
+ return __getInt(buf, readlen);
+}
+
+static int __l2cap_le_listen(l2cap_le_server_info_t *server_info, bool accept)
+{
+ int result;
+ GUnixFDList *out_fd_list = NULL;
+ GIOChannel *server_io;
+ int psm;
+
+ retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ psm = server_info->psm;
+ g_array_append_vals(in_param1, &psm, sizeof(int));
+
+ if (accept == false)
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_L2CAP_LE_LISTEN,
+ in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+ else
+ result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_L2CAP_LE_LISTEN_AND_ACCEPT,
+ in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+
+ BT_INFO("result: %x", result);
+ if (result != BLUETOOTH_ERROR_NONE) {
+ BT_ERR("Fail to send request");
+ return result;
+ } else if (NULL == out_fd_list) {
+ BT_ERR("out_fd_list is NULL");
+ return BLUETOOTH_ERROR_INTERNAL;
+ } else {
+ int *fd_list_array;
+ int len = 0;
+
+ if (!out_fd_list)
+ return BLUETOOTH_ERROR_INTERNAL;
+
+ fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+ BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+ server_info->server_fd = fd_list_array[0];
+ BT_INFO("Socket fd: %d", server_info->server_fd);
+
+ g_free(fd_list_array);
+ g_object_unref(out_fd_list);
+ }
+
+ psm = __sock_wait_for_psm(server_info->server_fd);
+ if (psm < 0) {
+ BT_ERR("Reading PSM failed, psm %d", psm);
+ return BLUETOOTH_ERROR_INTERNAL;
+ }
+ server_info->psm = psm;
+
+ BT_INFO("socket fd: %d psm %d", server_info->server_fd, server_info->psm);
+ server_io = g_io_channel_unix_new(server_info->server_fd);
+ g_io_channel_set_encoding(server_io, NULL, NULL);
+ g_io_channel_set_flags(server_io, G_IO_FLAG_NONBLOCK, NULL);
+ server_info->watch_id = g_io_add_watch(server_io,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ __new_connection_request_cb, server_info);
+ g_io_channel_unref(server_io);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_create_socket(int psm)
+{
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_CREATE_SOCKET)
+ == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ }
+
+ BT_INFO("<<<<<<<<< L2CAP_LE Create socket from app, psm %d >>>>>>>>>", psm);
+
+ server_info = __get_l2cap_le_server_info_from_psm(psm);
+ if (!server_info) {
+ server_info = g_malloc0(sizeof(l2cap_le_server_info_t));
+ server_info->psm = psm;
+ server_info->server_id = __l2cap_le_assign_server_id();
+ server_info->server_fd = -1;
+ server_info->watch_id = -1;
+ server_info->auto_accept = FALSE;
+ l2cap_le_servers = g_slist_append(l2cap_le_servers, server_info);
+ }
+
+ return server_info->server_id;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_remove_socket(int id)
+{
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_REMOVE_SOCKET)
+ == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
+ }
+
+ if (id < 0) {
+ BT_ERR("Invalid ID");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ BT_INFO("<<<<<< L2CAP_LE Remove socket request from app, fd=[%d] >>>>>>", id);
+
+ server_info = __get_l2cap_le_server_info_with_id(id);
+ if (!server_info) {
+ BT_ERR("server_info not found for socket_fd: %d", id);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ __remove_l2cap_le_server(server_info);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_server_disconnect(int socket_fd)
+{
+ l2cap_le_remote_client_info_t *client_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ BT_INFO("<<<<<< L2CAP_LE server disconnect request from APP >>>>>>");
+ if (socket_fd < 0) {
+ BT_ERR("Invalid FD");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ client_info = __get_l2cap_le_rem_client_info_with_fd(socket_fd);
+ if (!client_info) {
+ BT_ERR("client_info not found for socket_fd: %d", socket_fd);
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
+
+ if (0 < client_info->sock_fd) {
+ l2cap_le_server_info_t *server_info;
+
+ /* Remove IO watch for client socket */
+ if (0 < client_info->watch_id)
+ g_source_remove(client_info->watch_id);
+ client_info->watch_id = -1;
+
+ /* close client socket and send L2CAP_LE disconneted event */
+ shutdown(client_info->sock_fd, SHUT_RDWR);
+ close(client_info->sock_fd);
+ server_info = __get_l2cap_le_server_info_with_id(
+ client_info->server_id);
+ __handle_l2cap_le_client_disconnected(
+ server_info, client_info);
+ client_info->sock_fd = -1;
+
+ /* Remove remote client info from l2cap_le server context */
+ server_info->conn_list = g_slist_remove(
+ server_info->conn_list, client_info);
+
+ /* Release remote client info */
+ __remove_remote_client_info(client_info);
+ }
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+BT_EXPORT_API gboolean bluetooth_l2cap_le_is_server_psm_available(int psm)
+{
+ int result;
+ gboolean available = TRUE;
+ int t_psm;
+
+ retv_if(bluetooth_check_adapter_le() ==
+ BLUETOOTH_ADAPTER_DISABLED, FALSE);
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ t_psm = psm;
+ g_array_append_vals(in_param1, &t_psm, sizeof(int));
+
+ /* TODO: Need to implement BT_L2CAP_LE_IS_PSM_AVAILABLE in BT-Service */
+ result = _bt_send_request(BT_BLUEZ_SERVICE, BT_L2CAP_LE_IS_PSM_AVAILABLE,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_DBG("result: %x", result);
+
+ if (result == BLUETOOTH_ERROR_NONE)
+ available = g_array_index(out_param, gboolean, 0);
+
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ BT_DBG("available: %d", available);
+
+ return available;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_server_is_connected(
+ const bluetooth_device_address_t *device_address, gboolean *connected)
+{
+ char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
+ l2cap_le_remote_client_info_t *info;
+
+ BT_CHECK_PARAMETER(device_address, return);
+ BT_CHECK_PARAMETER(connected, return);
+
+ *connected = FALSE;
+
+ _bt_convert_addr_type_to_string(input_addr,
+ (unsigned char *)device_address->addr);
+ info = __get_l2cap_le_rem_client_info_with_addr(input_addr);
+ if (info)
+ *connected = TRUE;
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_listen_and_accept(int id, int max_pending_connection)
+{
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+ if (id < 0) {
+ BT_ERR("Invalid ID");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ BT_INFO("<<<<<<<<< L2CAP_LE Listen & accept from app >>>>>>>>>>>");
+
+ server_info = __get_l2cap_le_server_info_with_id(id);
+ if (!server_info) {
+ BT_ERR("server_info not found for id: %d", id);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ if (server_info->server_fd >= 0) {
+ BT_ERR("server already listening");
+ return BLUETOOTH_ERROR_DEVICE_BUSY;
+ }
+
+ server_info->max_pending_conn = max_pending_connection;
+ server_info->auto_accept = TRUE;
+
+ return __l2cap_le_listen(server_info, true);
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_listen(int id, int max_pending_connection)
+{
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+ if (id < 0) {
+ BT_ERR("Invalid ID");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ BT_INFO("<<<<<<<<< L2CAP_LE Listen >>>>>>>>>>>");
+
+ server_info = __get_l2cap_le_server_info_with_id(id);
+ if (!server_info) {
+ BT_ERR("server_info not found for id: %d", id);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ if (server_info->server_fd >= 0) {
+ BT_ERR("server already listening");
+ return BLUETOOTH_ERROR_DEVICE_BUSY;
+ }
+
+ server_info->max_pending_conn = max_pending_connection;
+ server_info->auto_accept = FALSE;
+
+ return __l2cap_le_listen(server_info, false);
+}
+
+
+BT_EXPORT_API int bluetooth_l2cap_le_accept_connection(int server_fd)
+{
+ int result;
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ if (server_fd < 0) {
+ BT_ERR("Invalid FD");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ server_info = __get_l2cap_le_server_info_with_id(server_fd);
+ if (!server_info) {
+ BT_ERR("No server with fd: %d", server_fd);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE, BT_L2CAP_LE_ACCEPT_CONNECTION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_DBG("result: %x", result);
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_reject_connection(int server_fd)
+{
+ int result;
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ if (server_fd < 0) {
+ BT_ERR("Invalid FD");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ BT_INIT_PARAMS();
+ BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ server_info = __get_l2cap_le_server_info_with_id(server_fd);
+ if (!server_info) {
+ BT_ERR("No server with fd: %d", server_fd);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
+
+ result = _bt_send_request(BT_BLUEZ_SERVICE, BT_L2CAP_LE_REJECT_CONNECTION,
+ in_param1, in_param2, in_param3, in_param4, &out_param);
+
+ BT_DBG("result: %x", result);
+ BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+ return result;
+}
+
+BT_EXPORT_API int bluetooth_l2cap_le_get_psm(int id, int *psm)
+{
+ l2cap_le_server_info_t *server_info;
+
+ BT_CHECK_ENABLED_LE(return);
+
+ if (id < 0) {
+ BT_ERR("Invalid ID");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ server_info = __get_l2cap_le_server_info_with_id(id);
+ if (!server_info) {
+ BT_ERR("server_info not found for sock_fd: %d", id);
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
+ *psm = server_info->psm;
+
+ return BLUETOOTH_ERROR_NONE;
+}
#define BT_ADDRESS_STRING_SIZE 18
#define BT_ADAPTER_OBJECT_PATH_MAX 50
#define BT_RFCOMM_BUFFER_LEN 1024
+#define BT_L2CAP_LE_BUFFER_LEN 1024
#define BT_ACCESS_DENIED_MSG "Rejected send message"
int _bt_unregister_osp_server_in_agent(int type, char *uuid);
int _bt_check_privilege(int service_type, int service_function);
+int _bt_check_privilege_le(int service_type, int service_function);
GDBusConnection *_bt_get_system_shared_conn(void);
GDBusConnection *_bt_get_system_common_conn(void);
void _bt_rfcomm_client_reset_timer(void);
+int _get_l2cap_le_server_id(int psm, gboolean *auto_accept);
+void _bt_l2cap_le_server_set_pending_conn(int server_id, char *address);
+
void _bt_reset_battery_monitor_info(void);
int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr);
} bluetooth_rfcomm_received_data_t;
/**
+ * Stucture to l2cap_le receive data
+ */
+typedef struct {
+ int socket_fd; /**< the socket fd */
+ int buffer_size; /**< the length of the receive buffer */
+ char *buffer; /**< the receive data buffer */
+} bluetooth_l2cap_le_received_data_t;
+
+/**
* HID Header type
*/
typedef enum {
} bluetooth_l2cap_le_connection_t;
/**
+ * Stucture to l2cap_le disconnection
+ */
+typedef struct {
+ int socket_fd; /**< the socket fd */
+ int device_role;
+ /** < Device role - L2CAP_LE_ROLE_SERVER or L2CAP_LE_ROLE_CLIENT */
+ bluetooth_device_address_t device_addr; /**< device address */
+ int psm;
+} bluetooth_l2cap_le_disconnection_t;
+
+/**
+ * Stucture to l2cap_le connection request
+ */
+typedef struct {
+ int socket_fd; /**< the socket fd */
+ bluetooth_device_address_t device_addr; /**< device address */
+} bluetooth_l2cap_le_connection_request_t;
+
+/**
* HDP QOS types
*/
typedef enum {
int bluetooth_is_le_coded_phy_supported(gboolean *is_supported);
/**
+ * @fn int bluetooth_l2cap_le_create_socket(int psm)
+ * @brief Register l2cap_le socket with a specific PSM
+ *
+ *
+ * This API register l2cap_le socket with the given PSM. The return value of this API is the socket
+ * descriptor of the server.
+ * This is the first API which is called to create the server. Once we created the server socket,
+ * we will listen on that return socket.
+ * So a bluetooth_l2cap_le_listen_and_accept should follow this API call. This is a synchronous call.
+ *
+ *
+ * @return socket FD on Success \n
+ * BLUETOOTH_ERROR_DEVICE_NOT_ENABLED - Device is not enabled \n
+ * BLUETOOTH_ERROR_INTERNAL - Internal error\n
+ * BLUETOOTH_ERROR_MAX_CONNECTION - Maximum connection reached\n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * @param[in] int psm
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_listen_and_accept, bluetooth_l2cap_le_remove_socket
+ *
+ @code
+
+ int test_psm = 0;
+ fd = bluetooth_l2cap_le_create_socket(test_psm);
+
+ @endcode
+ */
+int bluetooth_l2cap_le_create_socket(int psm);
+
+/**
+ * @fn int bluetooth_l2cap_le_remove_socket(int socket_fd)
+ * @brief De-register the l2cap_le socket
+ *
+ *
+ * This API deregister l2cap_le socket with the given socket fd. If the remote device is
+ * already connected then we will receive the BLUETOOTH_EVENT_L2CAP_LE_DISCONNECTED with socket
+ * descriptor else no event will come. We will call this API only after the
+ * bluetooth_l2cap_le_listen_and_accept.
+ * This is a synchronous call.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_DEVICE_NOT_ENABLED - Device is not enabled \n
+ * BLUETOOTH_ERROR_NOT_FOUND - Cannot find the proxy\n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * @param[in] int socket_fd
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_create_socket, bluetooth_l2cap_le_listen_and_accept
+ *
+ @code
+ void bt_event_callback(int event, bluetooth_event_param_t *param)
+ {
+ switch(event)
+ {
+ case BLUETOOTH_EVENT_L2CAP_LE_DISCONNECTED:
+ {
+ bluetooth_l2cap_le_connection_t *discon_ind =
+ (bluetooth_l2cap_le_connection_t *)param->param_data;
+
+ printf("\nDisconnected from FD %d", discon_ind->socket_fd);
+ }
+ }
+ }
+
+ ...
+
+ int ret = 0;
+ fd = bluetooth_l2cap_le_create_socket(test_psm);
+ ret = bluetooth_l2cap_le_listen_and_accept(fd, 1);
+ ....
+ ret = bluetooth_l2cap_le_remove_socket(fd);
+ @endcode
+ */
+int bluetooth_l2cap_le_remove_socket(int socket_fd);
+
+/**
+ * @fn int bluetooth_l2cap_le_server_disconnect(int socket_fd)
+ * @brief Disconnect l2cap_le connection
+ *
+ *
+ * Disconnect a specific(device node fd) l2cap_le connection. This is a Synchronous call and there
+ * is no callback events for this API. We have to provide the valid client fd to disconnect from the
+ * remote server.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_NOT_CONNECTED - Not connected \n
+ * @param[in] int socket_fd
+ *
+ * @remark None
+ *
+ @code
+
+ ret = bluetooth_l2cap_le_server_disconnect(g_ret_fd);
+ if (ret < 0)
+ printf("Disconnection failed");
+ else
+ printf("Disconnection Success");
+
+ @endcode
+ */
+int bluetooth_l2cap_le_server_disconnect(int socket_fd);
+
+/**
+ * @fn int bluetooth_l2cap_le_listen_and_accept(int socket_fd, int max_pending_connection)
+ * @brief L2CAP_LE socket listen
+ *
+ *
+ * This API make l2cap_le socket listen and accept with socket. We will call this API immediately
+ * after the bluetooth_l2cap_le_create_socket API.
+ * This API listen for the incoming connection and once it receives a connection, it will give
+ * BLUETOOTH_EVENT_L2CAP_LE_CONNECTED
+ * event to the application. This is an Asynchronous API call.
+ *
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_CONNECTION_ERROR - Listen failed \n
+
+ * @param[in] int socket_fd
+ * @param[in] max pending connection.
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_create_socket
+ *
+ @code
+ void bt_event_callback(int event, bluetooth_event_param_t* param)
+ {
+ switch(event)
+ {
+ case BLUETOOTH_EVENT_L2CAP_LE_CONNECTED:
+ {
+ bluetooth_l2cap_le_connection_t *conn_ind =
+ (bluetooth_l2cap_le_connection_t *)param->param_data;
+
+ printf("\nConnected from FD %d", conn_ind->socket_fd);
+ }
+ }
+ }
+
+ ...
+
+ int ret = 0;
+ fd = bluetooth_l2cap_le_create_socket(test_psm);
+ ret = bluetooth_l2cap_le_listen_and_accept(fd, 1);
+
+ @endcode
+ */
+int bluetooth_l2cap_le_listen_and_accept(int socket_fd, int max_pending_connection);
+
+/**
+ * @fn int bluetooth_l2cap_le_listen(int socket_fd, int max_pending_connection)
+ * @brief L2cap_le socket listen
+ *
+ *
+ * This API make l2cap_le socket listen and accept with socket. We will call this API immediately
+ * after the bluetooth_l2cap_le_create_socket API.
+ * This API listen for the incoming connection and once it receives a connection, it will give
+ * BLUETOOTH_EVENT_L2CAP_LE_AUTHORIZE
+ * event to the application. This is an Asynchronous API call.
+ *
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_CONNECTION_ERROR - Listen failed \n
+
+ * @param[in] int socket_fd
+ * @param[in] max pending connection.
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_create_socket
+ *
+ @code
+ void bt_event_callback(int event, bluetooth_event_param_t* param)
+ {
+ switch(event)
+ {
+ case BLUETOOTH_EVENT_L2CAP_LE_AUTHORIZE:
+ {
+ char *name = (char *)param->param_data;
+
+ printf("\nConnected from %s", name);
+
+ bluetooth_l2cap_le_accept_connection();
+ }
+ }
+ }
+
+ ...
+
+ int ret = 0;
+ fd = bluetooth_l2cap_le_create_socket(test_psm);
+ ret = bluetooth_l2cap_le_listen(fd, 1);
+
+ @endcode
+ */
+int bluetooth_l2cap_le_listen(int socket_fd, int max_pending_connection);
+
+/**
+ * @fn int bluetooth_l2cap_le_accept_connection()
+ * @brief Accepts the authorization request indicated by the event
+ * BLUETOOTH_EVENT_L2CAP_LE_AUTHORIZE.
+ *
+ * This function is a synchronous call.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INTERNAL - Internal error \n
+ *
+ * @param[in] the socket fd of the server
+ * @param[out] the socket fd of the client
+ *
+ * @exception None
+ * @remark None
+ * @see bluetooth_l2cap_le_reject_connection
+ */
+int bluetooth_l2cap_le_accept_connection(int server_fd);
+
+/**
+ * @fn int bluetooth_l2cap_le_reject_connection()
+ * @brief Rejects the authorization request indicated by the event
+ * BLUETOOTH_EVENT_L2CAP_LE_AUTHORIZE.
+ *
+ * This function is a synchronous call.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INTERNAL - Internal error \n
+ *
+ * @param[in] the socket fd of the server
+ *
+ * @exception None
+ * @remark None
+ * @see bluetooth_l2cap_le_accept_connection
+ */
+int bluetooth_l2cap_le_reject_connection(int server_fd);
+
+/**
+ * @fn gboolean bluetooth_l2cap_le_is_server_psm_available(int psm)
+ * @brief Informs whether l2cap_le server psm is available or not.
+ *
+ * This function is a synchronous call.
+ *
+ * @return TRUE - L2CAP_LE PSM is available \n
+ * FALSE - L2CAP_LE PSM is not available \n
+ *
+ * @param[in] psm (int)
+ *
+ * @exception None
+ *
+ * @remark None
+ */
+gboolean bluetooth_l2cap_le_is_server_psm_available(int psm);
+
+/**
+ * @fn int bluetooth_l2cap_le_connect(const bluetooth_device_address_t *remote_bt_address,
+ * int psm)
+ * @brief Connect to the remote device l2cap_le *
+ *
+ * Connect to a specific l2cap_le socket on a remote device psm. This is a Async call. Once
+ * the connection is successful callback BLUETOOTH_EVENT_L2CAP_LE_CONNECTED events is generated,
+ * which contains the socket_fd, device role (L2CAP_LE_ROLE_SERVER/L2CAP_LE_ROLE_CLIENT), device addess
+ * etc. The socket_fd can be further used to send the data. It better to do a ble scanning before
+ * initiating a connection.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_DEVICE_NOT_ENABLED - Device is not enabled \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_CONNECTION_BUSY - Connection in progress \n
+ * BLUETOOTH_ERROR_INTERNAL - Internal Error\n
+ * @param[in] bluetooth_device_address_t remote bt_address
+ * @param[in] int psm
+ * @remark None
+ * @see bluetooth_l2cap_le_disconnect, bluetooth_l2cap_le_write
+ *
+ @code
+
+ void bt_event_callback(int event, bluetooth_event_param_t *param)
+ {
+ switch(event)
+ {
+ case BLUETOOTH_EVENT_L2CAP_LE_CONNECTED:
+ {
+ bluetooth_l2cap_le_connection_t *conn_ind =
+ (bluetooth_l2cap_le_connection_t *)param->param_data;
+
+ printf("\nConnected from FD %d, Role = %s", conn_ind->socket_fd,
+ (conn_ind->device_role == L2CAP_LE_ROLE_SERVER) ?
+ "SERVER" : "CLIENT");
+ }
+ }
+ }
+
+ bluetooth_device_address_t remote_address = {{0},};
+ remote_address.addr[0] = 0x0; remote_address.addr[1] = 0x0A; remote_address.addr[2] = 0x3A;
+ remote_address.addr[3]= 0x54; remote_address.addr[4] = 0x19; remote_address.addr[5]= 0x36;
+
+ ret = bluetooth_l2cap_le_connect(&remote_address, test_psm);
+ if (ret < 0)
+ printf("Connection failed, Reason = %d", ret);
+ else
+ printf("Connection Success, Ret = %d", ret);
+
+ @endcode
+ */
+int bluetooth_l2cap_le_connect(const bluetooth_device_address_t *remote_bt_address,
+ int psm);
+
+/**
+ * @fn int bluetooth_l2cap_le_disconnect(int socket_fd)
+ * @brief Disconnect l2cap_le connection
+ *
+ *
+ * Disconnect a specific(device node fd) L2CAP_LE connection. This is a Synchronous call and there
+ * is no callback events for this API. We have to provide the valid client fd to disconnect from the
+ * remote server.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_NOT_CONNECTED - Not connected \n
+ * @param[in] int remote socket_fd
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_connect
+ *
+ @code
+
+ ret = bluetooth_l2cap_le_disconnect(g_ret_fd);
+ if (ret < 0)
+ printf("Disconnection failed");
+ else
+ printf("Disconnection Success");
+
+ @endcode
+ */
+
+int bluetooth_l2cap_le_disconnect(int socket_fd);
+
+/**
+ * @fn int bluetooth_l2cap_le_write (int fd, const char *buf, int length)
+ * @brief Write to l2cap_le connection
+ *
+ *
+ * This API is used to send the data over the l2cap_le connection. This is a
+ * synchronous API. The same API is used to send the data for server and the client.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_NOT_IN_OPERATION - The Fd is currently not in operation\n
+ * @param[in] int fd
+ * @param[in] const char *buff Data buffer to send
+ * @param[in] int length Length of the data
+ *
+ * @remark None
+ * @see bluetooth_l2cap_le_connect
+ *
+ @code
+ char *buff = "Test data 123456789"
+ ret = bluetooth_l2cap_le_write(g_ret_fd, buff, 15);
+ if (ret < 0)
+ printf("Send failed");
+ else
+ printf("Send success");
+
+ @endcode
+ */
+int bluetooth_l2cap_le_write(int fd, const char *buf, int length);
+
+/**
+ * @fn gboolean bluetooth_l2cap_le_client_is_connected(
+ * const bluetooth_device_address_t *device_address, gboolean *connected)
+ * @brief Informs whether l2cap_le client is connected.
+ *
+ * This function is a synchronous call.
+ *
+ * @return TRUE - L2CAP_LE client is connected \n
+ * FALSE - L2CAP_LE client is not connected \n
+ *
+ * @exception None
+ *
+ * @remark None
+ */
+int bluetooth_l2cap_le_client_is_connected(
+ const bluetooth_device_address_t *device_address, gboolean *connected);
+
+/**
+ * @fn gboolean bluetooth_l2cap_le_server_is_connected(
+ * const bluetooth_device_address_t *device_address, gboolean *connected)
+ * @brief Informs whether l2cap_le server is connected.
+ *
+ * This function is a synchronous call.
+ *
+ * @return TRUE - L2CAP_LE server is connected \n
+ * FALSE - L2CAP_LE server is not connected \n
+ *
+ * @exception None
+ *
+ * @remark None
+ */
+int bluetooth_l2cap_le_server_is_connected(
+ const bluetooth_device_address_t *device_address, gboolean *connected);
+
+/**
+ * @fn gboolean bluetooth_l2cap_le_get_psm(int server_fd, int *psm)
+ * @brief gives the psm of the specific l2cap_le socket connection.
+ *
+ * This function is a synchronous call.
+ *
+ * @return BLUETOOTH_ERROR_NONE - Success \n
+ * BLUETOOTH_ERROR_INVALID_PARAM - Invalid parameter \n
+ * BLUETOOTH_ERROR_DEVICE_NOT_ENABLED - Adapter is disabled \n
+ *
+ * @param[in] the socket fd of the server
+ * @param[out] int *psm
+ *
+ * @exception None
+ *
+ * @remark None
+ */
+int bluetooth_l2cap_le_get_psm(int id, int *psm);
+
+
+/**
* @}
*/