Skip to content

Commit 6a0f93e

Browse files
m-alperen-senerPavelVPV
authored andcommitted
[nrf fromtree] tests: bluetooth: tester: add support for wid 145 and 167
wid 145 requests handle of a UUID of a long characteristic. wid 167 requests to remove the characteristic by handle requested in wid 145. 2 new commands are added to support these wids: - BTP_GATT_GET_HANDLE_FROM_UUID to request handle of a certain UUID - BTP_GATT_REMOVE_HANDLE_FROM_DB to remove attribute by handle. Signed-off-by: alperen sener <[email protected]> (cherry picked from commit 7ae81e6) Signed-off-by: Pavel Vasilyev <[email protected]>
1 parent 0783923 commit 6a0f93e

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

tests/bluetooth/tester/src/btp/btp_gatt.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,21 @@ struct btp_gatt_cfg_notify_mult_cmd {
347347
uint16_t cnt;
348348
uint16_t attr_id[];
349349
} __packed;
350+
351+
#define BTP_GATT_GET_HANDLE_FROM_UUID 0x22
352+
struct btp_gatt_get_handle_from_uuid_cmd {
353+
uint8_t uuid_length;
354+
uint8_t uuid[];
355+
} __packed;
356+
struct btp_gatt_get_handle_from_uuid_rp {
357+
uint16_t handle;
358+
} __packed;
359+
360+
#define BTP_GATT_REMOVE_HANDLE_FROM_DB 0x23
361+
struct btp_gatt_remove_handle_from_db_cmd {
362+
uint16_t handle;
363+
} __packed;
364+
350365
/* GATT events */
351366
#define BTP_GATT_EV_NOTIFICATION 0x80
352367
struct btp_gatt_notification_ev {

tests/bluetooth/tester/src/btp_gatt.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ static int alloc_characteristic(struct add_characteristic *ch)
452452
chrc_data->uuid = attr_value->uuid;
453453

454454
ch->char_id = attr_chrc->handle;
455+
455456
return 0;
456457
}
457458

@@ -2131,6 +2132,71 @@ static uint8_t notify_mult(const void *cmd, uint16_t cmd_len,
21312132
}
21322133
#endif /* CONFIG_BT_GATT_NOTIFY_MULTIPLE */
21332134

2135+
static uint8_t get_handle_from_uuid(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
2136+
{
2137+
const struct btp_gatt_get_handle_from_uuid_cmd *cp = cmd;
2138+
struct btp_gatt_get_handle_from_uuid_rp *rp = rsp;
2139+
struct bt_uuid search_uuid;
2140+
2141+
if (btp2bt_uuid(cp->uuid, cp->uuid_length, &search_uuid)) {
2142+
return BTP_STATUS_FAILED;
2143+
}
2144+
2145+
__maybe_unused char uuid_str[BT_UUID_STR_LEN];
2146+
2147+
bt_uuid_to_str(&search_uuid, uuid_str, sizeof(uuid_str));
2148+
2149+
LOG_DBG("Searching handle for UUID %s", uuid_str);
2150+
2151+
for (int i = 0; i < attr_count; i++) {
2152+
if (server_db[i].uuid != NULL &&
2153+
bt_uuid_cmp(server_db[i].uuid, &search_uuid) == 0) {
2154+
rp->handle = sys_cpu_to_le16(server_db[i].handle);
2155+
*rsp_len = sizeof(*rp);
2156+
2157+
return BTP_STATUS_SUCCESS;
2158+
}
2159+
}
2160+
2161+
LOG_DBG("No handle found");
2162+
return BTP_STATUS_FAILED;
2163+
}
2164+
2165+
static uint8_t remove_by_handle_from_db(const void *cmd, uint16_t cmd_len, void *rsp,
2166+
uint16_t *rsp_len)
2167+
{
2168+
const struct btp_gatt_remove_handle_from_db_cmd *cp = cmd;
2169+
uint16_t handle = sys_le16_to_cpu(cp->handle);
2170+
2171+
/* Search for the service that contains the attribute with the given handle and unregister
2172+
* it.
2173+
*/
2174+
2175+
for (int i = 0; i < svc_count; i++) {
2176+
for (int j = 0; j < server_svcs[i].attr_count; j++) {
2177+
if (server_svcs[i].attrs[j].handle == handle) {
2178+
int err;
2179+
2180+
err = bt_gatt_service_unregister(&server_svcs[i]);
2181+
if (err < 0 && err != -ENOENT) {
2182+
LOG_ERR("Failed to unregister service [%d]: %d", i, err);
2183+
return BTP_STATUS_FAILED;
2184+
}
2185+
2186+
if (err == -ENOENT) {
2187+
LOG_WRN("Service [%d] already unregistered", i);
2188+
} else {
2189+
LOG_DBG("Service [%d] unregistered", i);
2190+
}
2191+
2192+
return BTP_STATUS_SUCCESS;
2193+
}
2194+
}
2195+
}
2196+
2197+
return BTP_STATUS_FAILED;
2198+
}
2199+
21342200
struct get_attrs_foreach_data {
21352201
struct net_buf_simple *buf;
21362202
const struct bt_uuid *uuid;
@@ -2557,6 +2623,16 @@ static const struct btp_handler handlers[] = {
25572623
.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
25582624
.func = notify_mult,
25592625
},
2626+
{
2627+
.opcode = BTP_GATT_GET_HANDLE_FROM_UUID,
2628+
.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
2629+
.func = get_handle_from_uuid,
2630+
},
2631+
{
2632+
.opcode = BTP_GATT_REMOVE_HANDLE_FROM_DB,
2633+
.expect_len = sizeof(struct btp_gatt_remove_handle_from_db_cmd),
2634+
.func = remove_by_handle_from_db,
2635+
},
25602636
};
25612637

25622638
uint8_t tester_init_gatt(void)

0 commit comments

Comments
 (0)