Skip to content

Commit 7ae81e6

Browse files
m-alperen-senernashif
authored andcommitted
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]> Signed-off-by: Pavel Vasilyev <[email protected]>
1 parent f186218 commit 7ae81e6

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

@@ -2139,6 +2140,71 @@ static uint8_t notify_mult(const void *cmd, uint16_t cmd_len,
21392140
}
21402141
#endif /* CONFIG_BT_GATT_NOTIFY_MULTIPLE */
21412142

2143+
static uint8_t get_handle_from_uuid(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len)
2144+
{
2145+
const struct btp_gatt_get_handle_from_uuid_cmd *cp = cmd;
2146+
struct btp_gatt_get_handle_from_uuid_rp *rp = rsp;
2147+
struct bt_uuid search_uuid;
2148+
2149+
if (btp2bt_uuid(cp->uuid, cp->uuid_length, &search_uuid)) {
2150+
return BTP_STATUS_FAILED;
2151+
}
2152+
2153+
__maybe_unused char uuid_str[BT_UUID_STR_LEN];
2154+
2155+
bt_uuid_to_str(&search_uuid, uuid_str, sizeof(uuid_str));
2156+
2157+
LOG_DBG("Searching handle for UUID %s", uuid_str);
2158+
2159+
for (int i = 0; i < attr_count; i++) {
2160+
if (server_db[i].uuid != NULL &&
2161+
bt_uuid_cmp(server_db[i].uuid, &search_uuid) == 0) {
2162+
rp->handle = sys_cpu_to_le16(server_db[i].handle);
2163+
*rsp_len = sizeof(*rp);
2164+
2165+
return BTP_STATUS_SUCCESS;
2166+
}
2167+
}
2168+
2169+
LOG_DBG("No handle found");
2170+
return BTP_STATUS_FAILED;
2171+
}
2172+
2173+
static uint8_t remove_by_handle_from_db(const void *cmd, uint16_t cmd_len, void *rsp,
2174+
uint16_t *rsp_len)
2175+
{
2176+
const struct btp_gatt_remove_handle_from_db_cmd *cp = cmd;
2177+
uint16_t handle = sys_le16_to_cpu(cp->handle);
2178+
2179+
/* Search for the service that contains the attribute with the given handle and unregister
2180+
* it.
2181+
*/
2182+
2183+
for (int i = 0; i < svc_count; i++) {
2184+
for (int j = 0; j < server_svcs[i].attr_count; j++) {
2185+
if (server_svcs[i].attrs[j].handle == handle) {
2186+
int err;
2187+
2188+
err = bt_gatt_service_unregister(&server_svcs[i]);
2189+
if (err < 0 && err != -ENOENT) {
2190+
LOG_ERR("Failed to unregister service [%d]: %d", i, err);
2191+
return BTP_STATUS_FAILED;
2192+
}
2193+
2194+
if (err == -ENOENT) {
2195+
LOG_WRN("Service [%d] already unregistered", i);
2196+
} else {
2197+
LOG_DBG("Service [%d] unregistered", i);
2198+
}
2199+
2200+
return BTP_STATUS_SUCCESS;
2201+
}
2202+
}
2203+
}
2204+
2205+
return BTP_STATUS_FAILED;
2206+
}
2207+
21422208
struct get_attrs_foreach_data {
21432209
struct net_buf_simple *buf;
21442210
const struct bt_uuid *uuid;
@@ -2565,6 +2631,16 @@ static const struct btp_handler handlers[] = {
25652631
.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
25662632
.func = notify_mult,
25672633
},
2634+
{
2635+
.opcode = BTP_GATT_GET_HANDLE_FROM_UUID,
2636+
.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
2637+
.func = get_handle_from_uuid,
2638+
},
2639+
{
2640+
.opcode = BTP_GATT_REMOVE_HANDLE_FROM_DB,
2641+
.expect_len = sizeof(struct btp_gatt_remove_handle_from_db_cmd),
2642+
.func = remove_by_handle_from_db,
2643+
},
25682644
};
25692645

25702646
uint8_t tester_init_gatt(void)

0 commit comments

Comments
 (0)