Skip to content

Commit 0713629

Browse files
apps: btshell: add support for authorization
For async authorization testing purposes characteristic and authorize shell command are added. We always respond with BLE_GAP_AUTHORIZE_PENDING to GAP_EVENT_AUTHORIZE.
1 parent 3b12f5a commit 0713629

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

apps/btshell/src/cmd.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,58 @@ 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+
int 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+
ble_gatts_pending_req_auth(conn_handle, attr_handle, auth);
211+
212+
return 0;
213+
214+
}
215+
216+
#if MYNEWT_VAL(SHELL_CMD_HELP)
217+
static const struct shell_param authorize_params[] = {
218+
{"conn", "connection handle parameter, usage: =<UINT16>"},
219+
{"attr", "attribute hndle parameter to authorize, usage: =<UINT16>"},
220+
{"auth", "whether to authorize access, usage: =[0-1], default=1"},
221+
{NULL, NULL}
222+
};
223+
224+
static const struct shell_cmd_help authorize_help = {
225+
.summary = "authorize command",
226+
.usage = NULL,
227+
.params = authorize_params,
228+
};
229+
#endif
230+
179231
/*****************************************************************************
180232
* $advertise *
181233
*****************************************************************************/
@@ -4374,6 +4426,11 @@ static const struct shell_cmd_help leaudio_broadcast_stop_help = {
43744426
#endif /* BLE_AUDIO && BLE_ISO_BROADCAST_SOURCE */
43754427

43764428
static const struct shell_cmd btshell_commands[] = {
4429+
{
4430+
.sc_cmd = "authorize",
4431+
.sc_cmd_func = pending_operation_authorize,
4432+
.help = &authorize_help,
4433+
},
43774434
#if MYNEWT_VAL(BLE_EXT_ADV)
43784435
{
43794436
.sc_cmd = "advertise-configure",

apps/btshell/src/gatt_svr.c

Lines changed: 21 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
@@ -178,8 +179,14 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
178179
0, /* No more descriptors in this characteristic. */
179180
} }
180181
}, {
181-
0, /* No more characteristics in this service. */
182-
} },
182+
.uuid = PTS_UUID_DECLARE(PTS_CHR_READ_WRITE_AUTHOR),
183+
.access_cb = gatt_svr_access_test,
184+
.flags = BLE_GATT_CHR_F_READ_AUTHOR | BLE_GATT_CHR_F_READ |
185+
BLE_GATT_CHR_F_WRITE_AUTHOR | BLE_GATT_CHR_F_WRITE
186+
}, {
187+
0, /* No more characteristics in this service. */
188+
}
189+
},
183190
},
184191

185192
{
@@ -465,6 +472,18 @@ gatt_svr_access_test(uint16_t conn_handle, uint16_t attr_handle,
465472
sizeof gatt_svr_pts_static_val);
466473
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
467474
}
475+
476+
case PTS_CHR_READ_WRITE_AUTHOR:
477+
if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
478+
rc = os_mbuf_append(ctxt->om, &gatt_svr_pts_static_val,
479+
sizeof gatt_svr_pts_static_val);
480+
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
481+
} else if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
482+
rc = gatt_svr_chr_write(ctxt->om,0,
483+
sizeof gatt_svr_pts_static_val,
484+
&gatt_svr_pts_static_val, NULL);
485+
return rc;
486+
}
468487
assert(0);
469488
break;
470489
default:

apps/btshell/src/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,14 @@ btshell_gap_event(struct ble_gap_event *event, void *arg)
15551555
return 0;
15561556
#endif
15571557
#endif
1558+
case BLE_GAP_EVENT_AUTHORIZE:
1559+
console_printf("Authorize access to attribute: conn_handle=%d, "
1560+
"attr_handle=%d, "
1561+
"att_opcode=%d\n",
1562+
event->authorize.conn_handle,
1563+
event->authorize.attr_handle,
1564+
event->authorize.access_opcode);
1565+
return BLE_GAP_AUTHORIZE_PENDING;
15581566
default:
15591567
return 0;
15601568
}

0 commit comments

Comments
 (0)