Skip to content

Commit

Permalink
nimle/tests: reset conn->client_att_busy between requests
Browse files Browse the repository at this point in the history
In real case, requests are scheduled to be sent after previous one
completes. That happens after response is received. In unittests,
internal API is used for holding pending requests, from which they can
be manually dequeued. Because dequeue operation is always manual and
does not happed after response reception (when will never arrive, there
is no second device) we need to clear flag to be able to start another
procedure.
  • Loading branch information
KKopyscinski committed Jul 17, 2023
1 parent 9cc5083 commit 7720d63
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
21 changes: 20 additions & 1 deletion nimble/host/test/src/ble_att_clt_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,34 @@ TEST_CASE_SELF(ble_att_clt_test_tx_find_info)
{
uint16_t conn_handle;
int rc;
struct ble_hs_conn *conn;

ble_hs_test_util_assert_mbufs_freed(NULL);

conn_handle = ble_att_clt_test_misc_init();
ble_hs_lock();
conn = ble_hs_conn_find(conn_handle);
ble_hs_unlock();

/*** Success. */
rc = ble_att_clt_tx_find_info(conn_handle, BLE_L2CAP_CID_ATT, 1, 0xffff);
TEST_ASSERT(rc == 0);

/*** Error: start handle of 0. */
/** In unit tests we don't are not receiving response - procedure will
* not complete. Reset `client_att_busy` flag so new request can be sent
*/
conn->client_att_busy = false;
rc = ble_att_clt_tx_find_info(conn_handle, BLE_L2CAP_CID_ATT, 0, 0xffff);
TEST_ASSERT(rc == BLE_HS_EINVAL);

/*** Error: start handle greater than end handle. */
conn->client_att_busy = false;
rc = ble_att_clt_tx_find_info(conn_handle, BLE_L2CAP_CID_ATT, 500, 499);
TEST_ASSERT(rc == BLE_HS_EINVAL);

/*** Success; start and end handles equal. */
conn->client_att_busy = false;
rc = ble_att_clt_tx_find_info(conn_handle, BLE_L2CAP_CID_ATT, 500, 500);
TEST_ASSERT(rc == 0);

Expand Down Expand Up @@ -175,16 +185,25 @@ ble_att_clt_test_case_tx_write_req_or_cmd(int is_req)
uint16_t conn_handle;
uint8_t value300[500] = { 0 };
uint8_t value5[5] = { 6, 7, 54, 34, 8 };
struct ble_hs_conn *conn;

conn_handle = ble_att_clt_test_misc_init();

ble_hs_lock();
conn = ble_hs_conn_find(conn_handle);
ble_hs_unlock();

/*** 5-byte write. */
ble_att_clt_test_tx_write_req_or_cmd(conn_handle, 0x1234, value5,
sizeof value5, is_req);
ble_att_clt_test_misc_verify_tx_write(0x1234, value5, sizeof value5,
is_req);

/*** Overlong write; verify command truncated to ATT MTU. */
/** In unit tests we are not receiving response - procedure will
* not complete. Reset `client_att_busy` flag so new request can be sent
*/
conn->client_att_busy = false;
ble_att_clt_test_tx_write_req_or_cmd(conn_handle, 0xab83, value300,
sizeof value300, is_req);
ble_att_clt_test_misc_verify_tx_write(0xab83, value300,
Expand Down Expand Up @@ -501,13 +520,13 @@ TEST_CASE_SELF(ble_att_clt_test_tx_exec_write)
uint16_t conn_handle;
int rc;

conn_handle = ble_att_clt_test_misc_init();

/*** Success. */
ble_att_clt_test_misc_exec_good(BLE_ATT_EXEC_WRITE_F_CANCEL);
ble_att_clt_test_misc_exec_good(BLE_ATT_EXEC_WRITE_F_EXECUTE);

/*** Success: nonzero == execute. */
conn_handle = ble_att_clt_test_misc_init();
rc = ble_att_clt_tx_exec_write(conn_handle, BLE_L2CAP_CID_ATT, 0x02);
TEST_ASSERT(rc == 0);

Expand Down
36 changes: 36 additions & 0 deletions nimble/host/test/src/ble_gatt_conn_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ TEST_CASE_SELF(ble_gatt_conn_test_disconnect)
uint16_t attr_handle;
uint16_t offset = 0;
int rc;
struct ble_hs_conn *conn;

ble_gatt_conn_test_util_init();

Expand All @@ -405,72 +406,100 @@ TEST_CASE_SELF(ble_gatt_conn_test_disconnect)
/*** Schedule some GATT procedures. */
/* Connection 1. */
mtu_arg.exp_conn_handle = 1;
ble_hs_lock();
conn = ble_hs_conn_find(1);
ble_hs_unlock();

/** In unit tests we are not receiving response - procedure will
* not complete. Reset `client_att_busy` flag so new request can be sent
*/
conn->client_att_busy = false;
ble_gattc_exchange_mtu(1, ble_gatt_conn_test_mtu_cb, &mtu_arg);

disc_all_svcs_arg.exp_conn_handle = 1;
conn->client_att_busy = false;
rc = ble_gattc_disc_all_svcs(1, ble_gatt_conn_test_disc_all_svcs_cb,
&disc_all_svcs_arg);
TEST_ASSERT_FATAL(rc == 0);

disc_svc_uuid_arg.exp_conn_handle = 1;
conn->client_att_busy = false;
rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16_DECLARE(0x1111),
ble_gatt_conn_test_disc_svc_uuid_cb,
&disc_svc_uuid_arg);
TEST_ASSERT_FATAL(rc == 0);

find_inc_svcs_arg.exp_conn_handle = 1;
conn->client_att_busy = false;
rc = ble_gattc_find_inc_svcs(1, 1, 0xffff,
ble_gatt_conn_test_find_inc_svcs_cb,
&find_inc_svcs_arg);
TEST_ASSERT_FATAL(rc == 0);

disc_all_chrs_arg.exp_conn_handle = 1;
conn->client_att_busy = false;
rc = ble_gattc_disc_all_chrs(1, 1, 0xffff,
ble_gatt_conn_test_disc_all_chrs_cb,
&disc_all_chrs_arg);
TEST_ASSERT_FATAL(rc == 0);

/* Connection 2. */
ble_hs_lock();
conn = ble_hs_conn_find(2);
ble_hs_unlock();

disc_all_dscs_arg.exp_conn_handle = 2;
conn->client_att_busy = false;
rc = ble_gattc_disc_all_dscs(2, 3, 0xffff,
ble_gatt_conn_test_disc_all_dscs_cb,
&disc_all_dscs_arg);

disc_chr_uuid_arg.exp_conn_handle = 2;
conn->client_att_busy = false;
rc = ble_gattc_disc_chrs_by_uuid(2, 2, 0xffff, BLE_UUID16_DECLARE(0x2222),
ble_gatt_conn_test_disc_chr_uuid_cb,
&disc_chr_uuid_arg);

read_arg.exp_conn_handle = 2;
conn->client_att_busy = false;
rc = ble_gattc_read(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE,
ble_gatt_conn_test_read_cb, &read_arg);
TEST_ASSERT_FATAL(rc == 0);

read_uuid_arg.exp_conn_handle = 2;
conn->client_att_busy = false;
rc = ble_gattc_read_by_uuid(2, 1, 0xffff, BLE_UUID16_DECLARE(0x3333),
ble_gatt_conn_test_read_uuid_cb,
&read_uuid_arg);
TEST_ASSERT_FATAL(rc == 0);

read_long_arg.exp_conn_handle = 2;
conn->client_att_busy = false;
rc = ble_gattc_read_long(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE, offset,
ble_gatt_conn_test_read_long_cb, &read_long_arg);
TEST_ASSERT_FATAL(rc == 0);

/* Connection 3. */
ble_hs_lock();
conn = ble_hs_conn_find(3);
ble_hs_unlock();

read_mult_arg.exp_conn_handle = 3;
conn->client_att_busy = false;
rc = ble_gattc_read_mult(3, ((uint16_t[3]){5,6,7}), 3,
ble_gatt_conn_test_read_mult_cb, &read_mult_arg);
TEST_ASSERT_FATAL(rc == 0);

write_arg.exp_conn_handle = 3;
conn->client_att_busy = false;
rc = ble_hs_test_util_gatt_write_flat(
3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
ble_gatt_conn_test_write_cb, &write_arg);
TEST_ASSERT_FATAL(rc == 0);

write_long_arg.exp_conn_handle = 3;
conn->client_att_busy = false;
rc = ble_hs_test_util_gatt_write_long_flat(
3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
Expand All @@ -481,10 +510,12 @@ TEST_CASE_SELF(ble_gatt_conn_test_disconnect)
attr.offset = 0;
attr.om = os_msys_get_pkthdr(0, 0);
write_rel_arg.exp_conn_handle = 3;
conn->client_att_busy = false;
rc = ble_gattc_write_reliable(
3, &attr, 1, ble_gatt_conn_test_write_rel_cb, &write_rel_arg);
TEST_ASSERT_FATAL(rc == 0);

conn->client_att_busy = false;
rc = ble_gatts_indicate(3, attr_handle);
TEST_ASSERT_FATAL(rc == 0);

Expand Down Expand Up @@ -528,6 +559,11 @@ TEST_CASE_SELF(ble_gatt_conn_test_disconnect)
TEST_ASSERT(ble_gatt_conn_test_gap_event.type == 255);

/* Connection 3. */
/** This is required because of call ble_att_clt_tx_exec_write
* from ble_att_clt_tx_exec_write after broken connection -
* ble_gattc_fail_procs is called
*/
conn->client_att_busy = false;
ble_gattc_connection_broken(3);
TEST_ASSERT(mtu_arg.called == 1);
TEST_ASSERT(disc_all_svcs_arg.called == 1);
Expand Down

0 comments on commit 7720d63

Please sign in to comment.