struct l2cap_conn_cb_data {
uint16_t psm;
bthost_l2cap_connect_cb func;
+ bthost_l2cap_disconnect_cb disconn_func;
void *user_data;
struct l2cap_conn_cb_data *next;
};
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_disconn_req *req = data;
+ struct l2cap_conn_cb_data *cb_data;
struct bt_l2cap_pdu_disconn_rsp rsp;
+ struct l2conn *l2conn;
if (len < sizeof(*req))
return false;
l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_DISCONN_RSP, ident, &rsp,
sizeof(rsp));
+ l2conn = btconn_find_l2cap_conn_by_scid(conn, rsp.scid);
+ if (!l2conn)
+ return true;
+
+ cb_data = bthost_find_l2cap_cb_by_psm(bthost, l2conn->psm);
+
+ if (cb_data && cb_data->disconn_func)
+ cb_data->disconn_func(cb_data->user_data);
+
return true;
}
}
void bthost_add_l2cap_server(struct bthost *bthost, uint16_t psm,
- bthost_l2cap_connect_cb func, void *user_data)
+ bthost_l2cap_connect_cb func,
+ bthost_l2cap_disconnect_cb disconn_func,
+ void *user_data)
{
struct l2cap_conn_cb_data *data;
data->psm = psm;
data->user_data = user_data;
data->func = func;
+ data->disconn_func = disconn_func;
data->next = bthost->new_l2cap_conn_data;
bthost->new_l2cap_conn_data = data;
uint16_t dcid;
int sk;
int sk2;
+ bool host_disconnected;
};
struct l2cap_data {
bool server_not_advertising;
bool direct_advertising;
bool close_1;
+
+ bool shut_sock_wr;
};
static void mgmt_debug(const char *str, void *user_data)
.data_len = sizeof(l2_data),
};
+static const struct l2cap_data client_connect_shut_wr_success_test = {
+ .client_psm = 0x1001,
+ .server_psm = 0x1001,
+ .shut_sock_wr = true,
+};
+
static const struct l2cap_data client_connect_nval_psm_test_1 = {
.client_psm = 0x1001,
.expect_err = ECONNREFUSED,
tester_test_passed();
}
+static gboolean socket_closed_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct test_data *data = tester_get_data();
+ const struct l2cap_data *l2data = data->test_data;
+
+ if (l2data->shut_sock_wr) {
+ /* if socket is closed using SHUT_WR, L2CAP disconnection
+ * response must be received first before G_IO_HUP event.
+ */
+ if (data->host_disconnected)
+ tester_test_passed();
+ else {
+ tester_warn("G_IO_HUP received before receiving L2CAP disconnection");
+ tester_test_failed();
+ }
+ }
+
+ return FALSE;
+}
+
static bool check_mtu(struct test_data *data, int sk)
{
const struct l2cap_data *l2data = data->test_data;
}
return FALSE;
+ } else if (l2data->shut_sock_wr) {
+ g_io_add_watch(io, G_IO_HUP, socket_closed_cb, NULL);
+ shutdown(sk, SHUT_WR);
+
+ return FALSE;
}
failed:
data->handle = handle;
}
+static void client_l2cap_disconnect_cb(void *user_data)
+{
+ struct test_data *data = user_data;
+
+ data->host_disconnected = true;
+}
+
static void direct_adv_cmd_complete(uint16_t opcode, const void *param,
uint8_t len, void *user_data)
{
if (l2data->server_psm) {
struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ bthost_l2cap_connect_cb host_connect_cb = NULL;
+ bthost_l2cap_disconnect_cb host_disconnect_cb = NULL;
- if (!l2data->data_len)
- bthost_add_l2cap_server(bthost, l2data->server_psm,
- NULL, NULL);
- else
- bthost_add_l2cap_server(bthost, l2data->server_psm,
- client_l2cap_connect_cb, data);
+ if (l2data->data_len)
+ host_connect_cb = client_l2cap_connect_cb;
+
+ if (l2data->shut_sock_wr)
+ host_disconnect_cb = client_l2cap_disconnect_cb;
+
+ bthost_add_l2cap_server(bthost, l2data->server_psm,
+ host_connect_cb, host_disconnect_cb,
+ data);
}
if (l2data->direct_advertising)
if (!l2data->data_len)
bthost_add_l2cap_server(bthost, l2data->server_psm,
- NULL, NULL);
+ NULL, NULL, NULL);
}
defer = (l2data->mode == BT_MODE_EXT_FLOWCTL);
&client_connect_nval_psm_test_3,
setup_powered_client, test_connect);
+ test_l2cap_bredr("L2CAP BR/EDR Client - Socket Shut WR Success",
+ &client_connect_shut_wr_success_test,
+ setup_powered_client, test_connect);
+
test_l2cap_bredr("L2CAP BR/EDR Server - Success",
&l2cap_server_success_test,
setup_powered_server, test_server);