Skip to content

Commit 129408d

Browse files
apps: btshell: add support for authorization
For authorization testing purposes two characteristics with authorize flags and authorize shell command are added.
1 parent 91ec1fe commit 129408d

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed

apps/btshell/src/btshell.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ struct btshell_scan_opts {
9797
extern struct btshell_conn btshell_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
9898
extern int btshell_num_conns;
9999

100+
/* BLE_GATT_READ_MAX_ATTRS * (1 ATT + 1 EATT chan) */
101+
#define PENDING_ATTR_MAX MYNEWT_VAL(BLE_GATT_READ_MAX_ATTRS) * 2
102+
103+
struct auth_attr {
104+
uint16_t conn_handle;
105+
uint16_t attr_handle;
106+
};
107+
108+
extern struct auth_attr authorized_attrs[PENDING_ATTR_MAX];
109+
110+
struct pend_attr {
111+
uint16_t attr_handle;
112+
uint16_t cid;
113+
};
114+
115+
extern struct pend_attr pending_attr;
116+
100117
int btshell_exchange_mtu(uint16_t conn_handle);
101118
int btshell_disc_svcs(uint16_t conn_handle);
102119
int btshell_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid);

apps/btshell/src/cmd.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,72 @@ cmd_parse_addr(const char *prefix, ble_addr_t *addr)
176176
return parse_dev_addr(prefix, cmd_addr_type, addr);
177177
}
178178

179+
static int
180+
pending_operation_authorize(int argc, char **argv)
181+
{
182+
uint16_t conn_handle;
183+
uint16_t attr_handle;
184+
bool auth;
185+
int rc;
186+
187+
rc = parse_arg_init(argc - 1, argv + 1);
188+
if (rc != 0) {
189+
return rc;
190+
}
191+
192+
conn_handle = parse_arg_uint16("conn", &rc);
193+
if (rc != 0) {
194+
console_printf("invalid 'conn' parameter\n");
195+
return rc;
196+
}
197+
198+
attr_handle = parse_arg_uint16("attr", &rc);
199+
if (rc != 0) {
200+
console_printf("invalid 'attr' parameter\n");
201+
return rc;
202+
}
203+
204+
auth = parse_arg_bool_dflt("auth", 1, &rc);
205+
if (rc != 0) {
206+
console_printf("invalid 'auth' parameter\n");
207+
return rc;
208+
}
209+
210+
if (auth) {
211+
for (int i = 0; i < PENDING_ATTR_MAX; i++) {
212+
if (authorized_attrs[i].conn_handle == 0 &&
213+
authorized_attrs[i].attr_handle == 0) {
214+
authorized_attrs[i].conn_handle = conn_handle;
215+
authorized_attrs[i].attr_handle = attr_handle;
216+
break;
217+
}
218+
}
219+
attr_handle = 0;
220+
} else {
221+
attr_handle = pending_attr.attr_handle;
222+
}
223+
224+
ble_gatts_pending_req_auth(conn_handle, attr_handle, pending_attr.cid);
225+
226+
return 0;
227+
}
228+
229+
#if MYNEWT_VAL(SHELL_CMD_HELP)
230+
static const struct shell_param authorize_params[] = {
231+
{"conn", "connection handle parameter, usage: =<UINT16>"},
232+
{"attr", "attribute hndle parameter to authorize, usage: =<UINT16>"},
233+
{"attr", "attribute handles parameter to authorize, usage: =<UINT16>"},
234+
{"auth", "whether to authorize access, usage: =[0-1], default=1"},
235+
{NULL, NULL}
236+
};
237+
238+
static const struct shell_cmd_help authorize_help = {
239+
.summary = "authorize command",
240+
.usage = NULL,
241+
.params = authorize_params,
242+
};
243+
#endif
244+
179245
/*****************************************************************************
180246
* $advertise *
181247
*****************************************************************************/
@@ -4374,6 +4440,11 @@ static const struct shell_cmd_help leaudio_broadcast_stop_help = {
43744440
#endif /* BLE_AUDIO && BLE_ISO_BROADCAST_SOURCE */
43754441

43764442
static const struct shell_cmd btshell_commands[] = {
4443+
{
4444+
.sc_cmd = "authorize",
4445+
.sc_cmd_func = pending_operation_authorize,
4446+
.help = &authorize_help,
4447+
},
43774448
#if MYNEWT_VAL(BLE_EXT_ADV)
43784449
{
43794450
.sc_cmd = "advertise-configure",

apps/btshell/src/gatt_svr.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define PTS_DSC_READ_WRITE 0x000b
4747
#define PTS_DSC_READ_WRITE_ENC 0x000c
4848
#define PTS_DSC_READ_WRITE_AUTHEN 0x000d
49+
#define PTS_CHR_READ_WRITE_AUTHOR 0x000e
4950

5051
#define PTS_LONG_SVC 0x0011
5152
#define PTS_LONG_CHR_READ 0x0012
@@ -60,6 +61,7 @@
6061
#define PTS_LONG_DSC_READ_WRITE 0x001b
6162
#define PTS_LONG_DSC_READ_WRITE_ENC 0x001c
6263
#define PTS_LONG_DSC_READ_WRITE_AUTHEN 0x001d
64+
#define PTS_LONG_CHR_READ_WRITE_AUTHOR 0x0020
6365

6466
#define PTS_INC_SVC 0x001e
6567
#define PTS_CHR_READ_WRITE_ALT 0x001f
@@ -178,8 +180,14 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
178180
0, /* No more descriptors in this characteristic. */
179181
} }
180182
}, {
181-
0, /* No more characteristics in this service. */
182-
} },
183+
.uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE_AUTHOR),
184+
.access_cb = gatt_svr_access_test,
185+
.flags = BLE_GATT_CHR_F_READ_AUTHOR | BLE_GATT_CHR_F_READ |
186+
BLE_GATT_CHR_F_WRITE_AUTHOR | BLE_GATT_CHR_F_WRITE
187+
}, {
188+
0, /* No more characteristics in this service. */
189+
}
190+
},
183191
},
184192

185193
{
@@ -206,6 +214,11 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
206214
.uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_ALT),
207215
.access_cb = gatt_svr_long_access_test,
208216
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
217+
},{
218+
.uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_AUTHOR),
219+
.access_cb = gatt_svr_long_access_test,
220+
.flags = BLE_GATT_CHR_F_READ_AUTHOR | BLE_GATT_CHR_F_READ |
221+
BLE_GATT_CHR_F_WRITE_AUTHOR | BLE_GATT_CHR_F_WRITE
209222
}, {
210223
.uuid = PTS_UUID_DECLARE(PTS_LONG_CHR_READ_WRITE_ENC),
211224
.access_cb = gatt_svr_long_access_test,
@@ -427,6 +440,7 @@ gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle,
427440
case PTS_CHR_READ_WRITE_ENC:
428441
case PTS_CHR_READ_WRITE_AUTHEN:
429442
case PTS_CHR_READ_WRITE_ALT:
443+
case PTS_CHR_READ_WRITE_AUTHOR:
430444
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
431445
rc = gatt_svr_chr_write(ctxt->om,0,
432446
sizeof gatt_svr_pts_static_val,
@@ -467,6 +481,7 @@ gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle,
467481
}
468482
assert(0);
469483
break;
484+
470485
default:
471486
assert(0);
472487
break;
@@ -531,6 +546,7 @@ gatt_svr_long_access_test(uint16_t conn_handle, uint16_t attr_handle,
531546

532547
case PTS_LONG_CHR_READ_WRITE_ENC:
533548
case PTS_LONG_CHR_READ_WRITE_AUTHEN:
549+
case PTS_LONG_CHR_READ_WRITE_AUTHOR:
534550
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
535551
rc = gatt_svr_chr_write(ctxt->om,0,
536552
sizeof gatt_svr_pts_static_long_val,

apps/btshell/src/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ int btshell_full_disc_prev_chr_val;
135135

136136
struct ble_sm_sc_oob_data oob_data_local;
137137
struct ble_sm_sc_oob_data oob_data_remote;
138+
struct auth_attr authorized_attrs[PENDING_ATTR_MAX];
139+
struct pend_attr pending_attr;
138140

139141
#if MYNEWT_VAL(BLE_AUDIO) && MYNEWT_VAL(BLE_ISO_BROADCAST_SOURCE)
140142
static struct {struct ble_audio_base *base; uint8_t adv_instance;}
@@ -1292,6 +1294,7 @@ btshell_gap_event(struct ble_gap_event *event, void *arg)
12921294
struct ble_gap_conn_desc desc;
12931295
int conn_idx;
12941296
int rc;
1297+
int i;
12951298
#if MYNEWT_VAL(BLE_PERIODIC_ADV)
12961299
struct psync *psync;
12971300
#endif
@@ -1318,6 +1321,7 @@ btshell_gap_event(struct ble_gap_event *event, void *arg)
13181321
if (conn_idx != -1) {
13191322
btshell_conn_delete_idx(conn_idx);
13201323
}
1324+
memset(&authorized_attrs, 0, sizeof(authorized_attrs));
13211325

13221326
return btshell_restart_adv(event);
13231327
#if MYNEWT_VAL(BLE_EXT_ADV)
@@ -1555,6 +1559,25 @@ btshell_gap_event(struct ble_gap_event *event, void *arg)
15551559
return 0;
15561560
#endif
15571561
#endif
1562+
case BLE_GAP_EVENT_AUTHORIZE:
1563+
for (i = 0; i < PENDING_ATTR_MAX; i++) {
1564+
if (authorized_attrs[i].conn_handle ==
1565+
event->authorize.conn_handle && authorized_attrs[i].attr_handle ==
1566+
event->authorize.attr_handle) {
1567+
console_printf("Access to attribute %d already authorized\n",
1568+
event->authorize.attr_handle);
1569+
return BLE_GAP_AUTHORIZE_ACCEPT;
1570+
}
1571+
}
1572+
console_printf("Authorize access to attribute: conn_handle=%d,"
1573+
"access_opcode=%d, cid=%d, attr=%d\n",
1574+
event->authorize.conn_handle,
1575+
event->authorize.access_opcode,
1576+
event->authorize.cid,
1577+
event->authorize.attr_handle);
1578+
pending_attr.cid = event->authorize.cid;
1579+
pending_attr.attr_handle = event->authorize.attr_handle;
1580+
return BLE_GAP_AUTHORIZE_PENDING;
15581581
default:
15591582
return 0;
15601583
}

0 commit comments

Comments
 (0)