Skip to content

Commit

Permalink
drv/bluetooth_stm32_cc2640: respond to prepared write
Browse files Browse the repository at this point in the history
If a host tries to write more than MTU - 3 bytes to an attribute, the
host should do a prepared write to split up the write into multiple long
writes. We currently don't support this. So we need to send an error
response to the prepared write request. The host may also follow this
with an execute write command to cancel the prepared write, so we need
to respond to that too.

Issue: pybricks/support#947
  • Loading branch information
dlech committed Jan 22, 2024
1 parent aab2c4a commit 086d327
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/ble5stack/central/att.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ bStatus_t ATT_WriteRsp(uint16_t connHandle) {
return HCI_sendHCICommand(ATT_CMD_WRITE_RSP, buf, 2);
}

bStatus_t ATT_ExecuteWriteRsp(uint16_t connHandle) {
uint8_t buf[2];

buf[0] = connHandle & 0xFF;
buf[1] = (connHandle >> 8) & 0xFF;

return HCI_sendHCICommand(ATT_CMD_EXECUTE_WRITE_RSP, buf, 2);
}

bStatus_t ATT_HandleValueNoti(uint16_t connHandle, attHandleValueNoti_t *pNoti) {
uint8_t buf[32];

Expand Down
6 changes: 3 additions & 3 deletions lib/ble5stack/central/att.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extern "C"
#define ATT_CMD_PREPAREWRITEREQ 0xFD16
#define ATT_CMD_PREPAREWRITERSP 0xFD17
#define ATT_CMD_EXECUTEWRITEREQ 0xFD18
#define ATT_CMD_EXECUTEWRITERSP 0xFD19
#define ATT_CMD_EXECUTE_WRITE_RSP 0xFD19
#define ATT_CMD_HANDLE_VALUE_NOTI 0xFD1B
#define ATT_CMD_HANDLEVALUEIND 0xFD1D
#define ATT_CMD_HANDLEVALUECFM 0xFD1E
Expand Down Expand Up @@ -125,9 +125,9 @@ extern "C"
#define ATT_EVENT_READ_BY_GRP_TYPE_RSP 0x0511
#define ATT_EVENT_WRITE_REQ 0x0512
#define ATT_EVENT_WRITE_RSP 0x0513
#define ATT_EVENT_PREPAREWRITEREQ 0x0516
#define ATT_EVENT_PREPARE_WRITE_REQ 0x0516
#define ATT_EVENT_PREPAREWRITERSP 0x0517
#define ATT_EVENT_EXECUTEWRITEREQ 0x0518
#define ATT_EVENT_EXECUTE_WRITE_REQ 0x0518
#define ATT_EVENT_EXECUTEWRITERSP 0x0519
#define ATT_EVENT_HANDLE_VALUE_NOTI 0x051B
#define ATT_EVENT_HANDLEVALUEIND 0x051D
Expand Down
21 changes: 21 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,27 @@ static void handle_event(uint8_t *packet) {
}
break;

case ATT_EVENT_PREPARE_WRITE_REQ: {
uint16_t char_handle = (data[7] << 8) | data[6];

// long writes are not currently supported for any characteristic
attErrorRsp_t rsp;
rsp.reqOpcode = ATT_PREPARE_WRITE_REQ;
rsp.handle = char_handle;
rsp.errCode = ATT_ERR_ATTR_NOT_LONG;
ATT_ErrorRsp(connection_handle, &rsp);
}
break;

case ATT_EVENT_EXECUTE_WRITE_REQ: {
// uint8_t flags = data[6];

// Send this to keep host happy. Should only be receiving
// cancel since we don't support long writes.
ATT_ExecuteWriteRsp(connection_handle);
}
break;

case ATT_EVENT_HANDLE_VALUE_NOTI: {
// TODO: match callback to handle
// uint8_t attr_handle = (data[7] << 8) | data[6];
Expand Down

0 comments on commit 086d327

Please sign in to comment.