Skip to content

Commit 8cfc06f

Browse files
nimble/apps/bttester: Implement GATT Server BTP Service (ID 0x07)
New BTP service is introduced. Most of the changes are just migration of code from btp_gatt.c and btp_gatt.h. Changes also include refactor of notifications sent by server and database initialization: - os_callout_init and functions/variables realted to it are removed. It was responsible for starting a timer when peer has subscribed for notifications/indications and sending notifications right after the timer has expired. An updated version of gatts_set_value command can be used to set values of one or more characteristics and handles sending notifications, indications and multiple handle value notifications - All predefined ble_gatt_svc_def structs will be registred on GATTS service registration with visibility set to invisible. BTP_GATTS_INITIALIZE_DATABASE can be used to set chosen database to be visible.
1 parent 14a0f78 commit 8cfc06f

8 files changed

Lines changed: 1947 additions & 617 deletions

File tree

apps/bttester/src/btp/btp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "btp_gap.h"
3232
#include "btp_gatt.h"
3333
#include "btp_gattc.h"
34+
#include "btp_gatts.h"
3435
#include "btp_l2cap.h"
3536
#include "btp_mesh.h"
3637
#include "btp_pacs.h"
@@ -48,6 +49,7 @@
4849
#define BTP_SERVICE_ID_L2CAP 3
4950
#define BTP_SERVICE_ID_MESH 4
5051
#define BTP_SERVICE_ID_GATTC 6
52+
#define BTP_SERVICE_ID_GATTS 7
5153
#define BTP_SERVICE_ID_PACS 12
5254
#define BTP_SERVICE_ID_BAP 14
5355

apps/bttester/src/btp/btp_gatts.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/* btp_gatts.h - Bluetooth tester GATT Server service headers */
21+
22+
/*
23+
* Copyright (c) 2015-2016 Intel Corporation
24+
* Copyright (C) 2025 Codecoup
25+
*
26+
* SPDX-License-Identifier: Apache-2.0
27+
*/
28+
29+
/* GATT Server Service */
30+
#define BTP_MAX_PTS_SVCS 1
31+
#define BTP_GATT_HL_MAX_CNT 16
32+
struct btp_notify_hlv {
33+
uint16_t handle;
34+
uint16_t len;
35+
};
36+
37+
struct btp_gatt_service {
38+
uint16_t start_handle;
39+
uint16_t end_handle;
40+
uint8_t uuid_length;
41+
uint8_t uuid[0];
42+
} __packed;
43+
44+
struct btp_gatt_included {
45+
uint16_t included_handle;
46+
struct btp_gatt_service service;
47+
} __packed;
48+
49+
struct btp_gatt_read_uuid_chr {
50+
uint16_t handle;
51+
uint8_t data[0];
52+
} __packed;
53+
54+
struct btp_gatt_characteristic {
55+
uint16_t characteristic_handle;
56+
uint16_t value_handle;
57+
uint8_t properties;
58+
uint8_t uuid_length;
59+
uint8_t uuid[0];
60+
} __packed;
61+
62+
struct btp_gatt_descriptor {
63+
uint16_t descriptor_handle;
64+
uint8_t uuid_length;
65+
uint8_t uuid[0];
66+
} __packed;
67+
68+
struct btp_gatt_read_rp {
69+
uint8_t att_response;
70+
uint16_t data_length;
71+
uint8_t data[0];
72+
} __packed;
73+
74+
/* commands */
75+
#define BTP_GATTS_READ_SUPPORTED_COMMANDS 0x01
76+
struct btp_gatts_read_supported_commands_rp {
77+
uint8_t data[0];
78+
} __packed;
79+
80+
#define BTP_GATTS_INITIALIZE_DATABASE 0x02
81+
struct btp_gatts_initialize_database_cmd {
82+
uint8_t id;
83+
uint32_t flags;
84+
} __packed;
85+
86+
#define BTP_GATTS_GET_ATTRIBUTES 0x03
87+
struct btp_gatts_get_attributes_cmd {
88+
uint16_t start_handle;
89+
uint16_t end_handle;
90+
uint8_t type_length;
91+
uint8_t type[0];
92+
} __packed;
93+
struct btp_gatts_get_attributes_rp {
94+
uint8_t attrs_count;
95+
uint8_t attrs[0];
96+
} __packed;
97+
struct btp_gatts_attr {
98+
uint16_t handle;
99+
uint8_t permission;
100+
uint8_t type_length;
101+
uint8_t type[0];
102+
} __packed;
103+
104+
#define BTP_GATTS_GET_ATTRIBUTE_VALUE 0x04
105+
struct btp_gatts_get_attribute_value_cmd {
106+
ble_addr_t address;
107+
uint16_t handle;
108+
} __packed;
109+
struct btp_gatts_get_attribute_value_rp {
110+
uint8_t att_response;
111+
uint16_t value_length;
112+
uint8_t value[0];
113+
} __packed;
114+
115+
#define BTP_GATTS_SET_CHRC_VALUE 0x05
116+
struct btp_gatts_set_chrc_value_cmd {
117+
ble_addr_t address;
118+
uint16_t count;
119+
struct btp_notify_hlv hl[BTP_GATT_HL_MAX_CNT];
120+
uint8_t value[0];
121+
} __packed;
122+
123+
#define BTP_GATTS_CHANGE_DATABASE 0x06
124+
struct btp_gatts_change_database_cmd {
125+
uint16_t start_handle;
126+
uint16_t end_handle;
127+
uint8_t visibility;
128+
} __packed;
129+
130+
#define BTP_GATTS_START_SERVER 0x07
131+
struct btp_gatts_start_server_rp {
132+
uint16_t db_attr_off;
133+
uint8_t db_attr_cnt;
134+
} __packed;
135+
136+
/* GATTS events */
137+
#define BTP_GATTS_EV_ATTR_VALUE_CHANGED 0x81
138+
struct btp_gatts_attr_value_changed_ev {
139+
uint16_t handle;
140+
uint16_t data_length;
141+
uint8_t data[0];
142+
} __packed;

apps/bttester/src/btp/bttester.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ tester_unregister_gatt(void);
113113
int
114114
tester_gattc_notify_rx_ev(uint16_t conn_handle, uint16_t attr_handle,
115115
uint8_t indication, struct os_mbuf *om);
116+
116117
int
117-
tester_gatt_subscribe_ev(uint16_t conn_handle,
118-
uint16_t attr_handle,
119-
uint8_t reason,
120-
uint8_t prev_notify,
121-
uint8_t cur_notify,
122-
uint8_t prev_indicate,
123-
uint8_t cur_indicate);
118+
tester_gatts_subscribe_ev(uint16_t conn_handle,
119+
uint16_t attr_handle,
120+
uint8_t reason,
121+
uint8_t prev_notify,
122+
uint8_t cur_notify,
123+
uint8_t prev_indicate,
124+
uint8_t cur_indicate);
124125

125126
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
126127
uint8_t
@@ -139,11 +140,17 @@ uint8_t
139140
tester_init_gatt_cl(void);
140141
uint8_t
141142
tester_unregister_gatt_cl(void);
143+
uint8_t
144+
tester_init_gatts(void);
145+
uint8_t
146+
tester_unregister_gatts(void);
147+
uint8_t
148+
register_database(void);
142149
void
143-
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
150+
gatts_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
144151

145152
int
146-
gatt_svr_init(void);
153+
gatts_svr_init(void);
147154

148155
#if MYNEWT_VAL(BLE_AUDIO)
149156
uint8_t

apps/bttester/src/btp_core.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ register_service(const void *cmd, uint16_t cmd_len,
7070
{
7171
const struct btp_core_register_service_cmd *cp = cmd;
7272
uint8_t status;
73+
int rc;
7374

7475
/* invalid service */
7576
if ((cp->id == BTP_SERVICE_ID_CORE) || (cp->id > BTP_SERVICE_ID_MAX)) {
@@ -111,6 +112,14 @@ register_service(const void *cmd, uint16_t cmd_len,
111112
case BTP_SERVICE_ID_GATTC:
112113
status = tester_init_gatt_cl();
113114
break;
115+
case BTP_SERVICE_ID_GATTS:
116+
rc = register_database();
117+
if (rc != 0) {
118+
status = rc;
119+
break;
120+
}
121+
status = tester_init_gatts();
122+
break;
114123
default:
115124
status = BTP_STATUS_FAILED;
116125
break;
@@ -160,6 +169,9 @@ unregister_service(const void *cmd, uint16_t cmd_len,
160169
case BTP_SERVICE_ID_GATTC:
161170
status = tester_unregister_gatt_cl();
162171
break;
172+
case BTP_SERVICE_ID_GATTS:
173+
status = tester_unregister_gatts();
174+
break;
163175
#if MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE)
164176
case BTP_SERVICE_ID_BAP:
165177
status = tester_unregister_bap();

apps/bttester/src/btp_gap.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,13 +1436,13 @@ gap_event_cb(struct ble_gap_event *event, void *arg)
14361436
event->subscribe.cur_notify,
14371437
event->subscribe.prev_indicate,
14381438
event->subscribe.cur_indicate);
1439-
tester_gatt_subscribe_ev(event->subscribe.conn_handle,
1440-
event->subscribe.attr_handle,
1441-
event->subscribe.reason,
1442-
event->subscribe.prev_notify,
1443-
event->subscribe.cur_notify,
1444-
event->subscribe.prev_indicate,
1445-
event->subscribe.cur_indicate);
1439+
tester_gatts_subscribe_ev(event->subscribe.conn_handle,
1440+
event->subscribe.attr_handle,
1441+
event->subscribe.reason,
1442+
event->subscribe.prev_notify,
1443+
event->subscribe.cur_notify,
1444+
event->subscribe.prev_indicate,
1445+
event->subscribe.cur_indicate);
14461446
break;
14471447
case BLE_GAP_EVENT_REPEAT_PAIRING:
14481448
console_printf("repeat pairing event; conn_handle=%d "

0 commit comments

Comments
 (0)