2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <gio/gunixfdlist.h>
20 #include <sys/socket.h>
22 #include "bluetooth-api.h"
23 #include "bt-internal-types.h"
25 #include "bt-common.h"
26 #include "bt-request-sender.h"
27 #include "bt-event-handler.h"
29 /* Variable for privilege, only for write API,
30 before we should reduce time to bt-service dbus calling
31 -1 : Don't have a permission to access API
32 0 : Initial value, not yet check
33 1 : Have a permission to access API
35 static int privilege_token;
37 GSList *l2cap_le_clients;
44 } l2cap_le_client_conn_info_t;
46 static gboolean __is_error_by_disconnect(GError *err)
48 return !g_strcmp0(err->message, "Connection reset by peer") ||
49 !g_strcmp0(err->message, "Connection timed out") ||
50 !g_strcmp0(err->message, "Software caused connection abort");
53 static l2cap_le_client_conn_info_t *__find_l2cap_le_conn_info_with_fd(int fd)
59 for (l = l2cap_le_clients; l != NULL; l = l->next) {
60 l2cap_le_client_conn_info_t *info = l->data;
62 if (info && info->sock_fd == fd) {
63 BT_INFO("Match found");
72 static void __l2cap_le_remove_client_conn_info_t(
73 l2cap_le_client_conn_info_t *info)
77 l2cap_le_clients = g_slist_remove(l2cap_le_clients, info);
78 g_free(info->remote_addr);
80 if (info->sock_fd > 0) {
81 shutdown(info->sock_fd, SHUT_RDWR);
85 if (info->watch_id > 0)
86 g_source_remove(info->watch_id);
90 static void __bt_l2cap_le_client_disconnected(
91 l2cap_le_client_conn_info_t *conn_info)
93 bluetooth_l2cap_le_disconnection_t disconn_info;
94 bt_event_info_t *event_info = NULL;
98 ret_if(conn_info == NULL);
100 event_info = _bt_event_get_cb_data(BT_L2CAP_LE_CLIENT_EVENT);
101 ret_if(event_info == NULL);
103 memset(&disconn_info, 0x00, sizeof(bluetooth_l2cap_le_disconnection_t));
104 disconn_info.device_role = L2CAP_LE_ROLE_CLIENT;
105 disconn_info.socket_fd = conn_info->sock_fd;
106 disconn_info.psm = conn_info->psm;
107 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
108 conn_info->remote_addr);
110 BT_INFO("Disconnection Result[%d] BT_ADDRESS[%s] FD[%d] PSM[%d]",
111 BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
112 conn_info->sock_fd, conn_info->psm);
113 _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DISCONNECTED,
114 BLUETOOTH_ERROR_NONE, &disconn_info,
115 event_info->cb, event_info->user_data);
120 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
123 bt_event_info_t *event_info;
124 bluetooth_l2cap_le_received_data_t data_r;
125 l2cap_le_client_conn_info_t *conn_info;
130 GIOStatus status = G_IO_STATUS_NORMAL;
131 static int resource_unavailable_cnt = 0;
135 fd = g_io_channel_unix_get_fd(chan);
136 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
137 BT_ERR_C("L2cap_le Client disconnected: %d", fd);
141 buffer = g_malloc0(BT_L2CAP_LE_BUFFER_LEN + 1);
142 status = g_io_channel_read_chars(chan, buffer, BT_L2CAP_LE_BUFFER_LEN,
144 if (status != G_IO_STATUS_NORMAL) {
145 BT_ERR("IO Channel read is failed with %d", status);
148 BT_ERR("IO Channel read error [%s]", err->message);
149 if (status == G_IO_STATUS_ERROR &&
150 __is_error_by_disconnect(err)) {
151 BT_ERR("cond : %d", cond);
158 if (status == G_IO_STATUS_ERROR ||
159 status == G_IO_STATUS_EOF) {
161 } else if (status == G_IO_STATUS_AGAIN) {
162 resource_unavailable_cnt++;
163 if (resource_unavailable_cnt > 10)
169 resource_unavailable_cnt = 0;
172 BT_ERR("Length is zero, remote end hang up");
177 BT_DBG("fd: %d, len: %zd, buffer: %s", fd, len, buffer);
179 event_info = _bt_event_get_cb_data(BT_L2CAP_LE_CLIENT_EVENT);
180 if (event_info == NULL) {
181 BT_INFO("event_info == NULL");
186 data_r.socket_fd = fd;
187 data_r.buffer_size = len;
188 data_r.buffer = buffer;
190 _bt_common_event_cb(BLUETOOTH_EVENT_L2CAP_LE_DATA_RECEIVED,
191 BLUETOOTH_ERROR_NONE, &data_r,
192 event_info->cb, event_info->user_data);
198 conn_info = __find_l2cap_le_conn_info_with_fd(fd);
200 BT_INFO("Disconnecting client, fd %d", fd);
201 close(conn_info->sock_fd);
202 __bt_l2cap_le_client_disconnected(conn_info);
203 __l2cap_le_remove_client_conn_info_t(conn_info);
205 BT_ERR("l2cap_le client conn_info not found");
212 static void __l2cap_le_client_connection_create_watch(
213 l2cap_le_client_conn_info_t *conn_info)
219 ret_if(NULL == conn_info);
221 data_io = g_io_channel_unix_new(conn_info->sock_fd);
222 g_io_channel_set_encoding(data_io, NULL, NULL);
223 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
224 conn_info->watch_id = g_io_add_watch(data_io,
225 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
226 __client_data_received_cb, NULL);
227 g_io_channel_unref(data_io);
232 static void __bt_l2cap_le_handle_new_client_connection(
233 bluetooth_l2cap_le_connection_t *info)
235 l2cap_le_client_conn_info_t *conn_info;
239 ret_if(NULL == info);
241 conn_info = g_malloc0(sizeof(l2cap_le_client_conn_info_t));
242 conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
243 _bt_convert_addr_type_to_string(
244 conn_info->remote_addr, info->device_addr.addr);
245 conn_info->sock_fd = info->socket_fd;
246 conn_info->psm = info->psm;
248 BT_INFO("Address:%s, Socket: %d, psm: %d",
249 conn_info->remote_addr, conn_info->sock_fd, conn_info->psm);
251 l2cap_le_clients = g_slist_append(l2cap_le_clients, conn_info);
252 __l2cap_le_client_connection_create_watch(conn_info);
257 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy,
258 GAsyncResult *res, gpointer user_data)
260 int result = BLUETOOTH_ERROR_NONE;
261 int event_type = BT_LE_ADAPTER_EVENT;
262 gboolean fail = false;
264 bt_req_info_t *cb_data = user_data;
265 bluetooth_event_param_t bt_event;
266 GArray *out_param1 = NULL;
267 GUnixFDList *out_fd_list = NULL;
268 bt_l2cap_user_info_t *l2cap_user_info = NULL;
273 l2cap_user_info = (bt_l2cap_user_info_t *)cb_data->user_data;
276 cb_data->user_data = (void *)l2cap_user_info->user_data;
278 _bt_get_fd_list_info(proxy, res, user_data, &bt_event, &out_param1,
279 &event_type, &out_fd_list, &result, &fail);
282 BT_INFO("Connection failed due to error: %d", result);
283 bluetooth_l2cap_le_connection_t *conn_info;
285 conn_info = g_malloc0(sizeof(bluetooth_l2cap_le_connection_t));
286 memset(conn_info, 0x00, sizeof(bluetooth_l2cap_le_connection_t));
288 conn_info->psm = l2cap_user_info->psm;
289 memcpy(&conn_info->device_addr, &l2cap_user_info->device_addr,
290 sizeof(bluetooth_device_address_t));
292 bt_event.param_data = (void *)conn_info;
299 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
300 if (BT_L2CAP_LE_CLIENT_CONNECT == cb_data->service_function) {
303 bluetooth_l2cap_le_connection_t *conn_info;
305 conn_info = (bluetooth_l2cap_le_connection_t *)bt_event.param_data;
307 BT_ERR("out_fd_list is NULL");
311 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
312 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
313 conn_info->socket_fd = fd_list_array[0];
315 BT_INFO("conn_info->socket_fd: %d", conn_info->socket_fd);
316 __bt_l2cap_le_handle_new_client_connection(conn_info);
318 if (cb_data->cb != NULL) {
319 /* Send client connected event */
320 bt_event.result = result;
321 BT_INFO("send client connected event event_type[%d], result=[%d]", event_type, result);
322 ((bluetooth_cb_func_ptr)cb_data->cb)(
323 bt_event.event, &bt_event, cb_data->user_data);
326 g_free(fd_list_array);
327 g_object_unref(out_fd_list);
333 if (cb_data->cb == NULL)
336 /* Only if fail case, call the callback function*/
337 bt_event.result = result;
339 BT_INFO("send fail event event_type[%d], result=[%d]", event_type, result);
340 if (event_type == BT_L2CAP_LE_CLIENT_EVENT) {
341 BT_INFO("l2cap_le client event");
342 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
343 &bt_event, cb_data->user_data);
345 BT_INFO("Not handled event type : %d", event_type);
349 g_array_free(out_param1, TRUE);
351 g_free(l2cap_user_info);
356 BT_EXPORT_API int bluetooth_l2cap_le_connect(
357 const bluetooth_device_address_t *remote_bt_address, int psm)
360 bt_user_info_t *user_info;
362 bt_l2cap_user_info_t *l2cap_user_info;
364 BT_CHECK_PARAMETER(remote_bt_address, return);
365 BT_CHECK_ENABLED_LE(return);
367 BT_INFO_C("connect l2cap_le psm %d", psm);
368 user_info = _bt_get_user_data(BT_COMMON);
369 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
372 if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_CLIENT_CONNECT)
373 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
374 BT_ERR("Don't have a privilege to use this API");
377 l2cap_user_info = g_malloc0(sizeof(bt_l2cap_user_info_t));
378 l2cap_user_info->psm = psm;
379 l2cap_user_info->user_data = user_info->user_data;
380 memcpy(&l2cap_user_info->device_addr, remote_bt_address,
381 sizeof(bluetooth_device_address_t));
384 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
386 g_array_append_vals(in_param1, remote_bt_address,
387 sizeof(bluetooth_device_address_t));
390 g_array_append_vals(in_param2, &t_psm, sizeof(int));
392 result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
393 BT_L2CAP_LE_CLIENT_CONNECT,
394 in_param1, in_param2,
395 in_param3, in_param4,
396 user_info->cb, (void *)l2cap_user_info,
397 NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
399 BT_INFO("result: %x", result);
401 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
406 BT_EXPORT_API int bluetooth_l2cap_le_client_is_connected(
407 const bluetooth_device_address_t *device_address, gboolean *connected)
410 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
412 BT_CHECK_PARAMETER(device_address, return);
413 BT_CHECK_PARAMETER(connected, return);
418 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
419 BT_INFO("Client address: [%s]", address);
421 for (l = l2cap_le_clients; l != NULL; l = l->next) {
422 l2cap_le_client_conn_info_t *info = l->data;
424 if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
425 BT_INFO("Match found");
427 return BLUETOOTH_ERROR_NONE;
432 return BLUETOOTH_ERROR_NONE;
435 BT_EXPORT_API int bluetooth_l2cap_le_disconnect(int socket_fd)
437 l2cap_le_client_conn_info_t *conn_info;
439 BT_INFO_C("<<<<<<<<< L2CAP_LE Disconnect request from app >>>>>>>>");
441 BT_CHECK_ENABLED_ANY(return);
442 retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
444 if (_bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_SOCKET_DISCONNECT)
445 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
446 BT_ERR("Don't have a privilege to use this API");
449 BT_INFO("FD %d", socket_fd);
451 conn_info = __find_l2cap_le_conn_info_with_fd(socket_fd);
452 if (conn_info == NULL) {
453 BT_INFO("Could not find in client, so check in server");
454 /* Check for fd in server list and perform the disconnection if present */
455 return bluetooth_l2cap_le_server_disconnect(socket_fd);
458 if (conn_info->watch_id <= 0) {
459 BT_ERR("Invalid state");
460 return BLUETOOTH_ERROR_NOT_CONNECTED;
463 close(conn_info->sock_fd);
464 __bt_l2cap_le_client_disconnected(conn_info);
465 __l2cap_le_remove_client_conn_info_t(conn_info);
467 return BLUETOOTH_ERROR_NONE;
470 static int __write_all(int fd, const char *buf, int len)
472 int sent = 0, try = 0;
478 written = write(fd, buf, len);
479 BT_DBG("written: %d, len %d", written, len);
481 if (errno == EINTR || errno == EAGAIN) {
502 BT_EXPORT_API int bluetooth_l2cap_le_write(int fd, const char *buf, int length)
506 BT_CHECK_ENABLED_LE(return);
507 BT_CHECK_PARAMETER(buf, return);
510 BT_ERR("Invalid FD");
511 return BLUETOOTH_ERROR_INVALID_PARAM;
514 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
516 switch (privilege_token) {
518 result = _bt_check_privilege_le(BT_CHECK_PRIVILEGE, BT_L2CAP_LE_SOCKET_WRITE);
520 if (result == BLUETOOTH_ERROR_NONE) {
521 privilege_token = 1; /* Have a permission */
522 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
523 BT_ERR("Don't have a privilege to use this API");
524 privilege_token = -1; /* Don't have a permission */
525 return BLUETOOTH_ERROR_PERMISSION_DEINED;
527 BT_ERR("Some error occurred");
528 /* Just break - It is not related with permission error */
532 /* Already have a privilege */
535 return BLUETOOTH_ERROR_PERMISSION_DEINED;
537 /* Invalid privilge token value */
538 return BLUETOOTH_ERROR_INTERNAL;
541 result = __write_all(fd, buf, length);