Validate buttonless DFU advertising name length#562
Conversation
|
Hi, There are, however, few things that perhaps would make it easier... So we are only talking about limiting the name from 20 to 18, which perhaps could just be solved by trumcating it to 18...? The name must fit into the advertising packet, which is 31 bytes long. 3 bytes are used for flags, 2 for the Local Name and length, leaving 26 for the max length of name, assuming there's no other data there. By default, the DFU bootloader advertises with a Secure DFU UUID, which is 16-bit long, which consumes additional 6 bytes, hence the limit was set to 20. However, as you said, if the ATT MTU is 23 (default), and 2 bytes are used for the opcode and command, the max size is in fact 18 bytes. As far as I remember, the library is using a default name with format I admit, that the limit of 20 is incorrect, but limit of 18 would seem to be fine. Or am I not seeing the whole picture? :) |
|
Yes, I forgot about it returning 512, and just limited in my code. So a hard limit of 18 might be better. Do you agree with the approach of erroring rather than trimming? I use a custom name for identification in the event the device gets stuck in the bootloader, for recovery. |
|
On one hand, error seems more "fair", as the lib woundn't do any magic. On the other hand it "a bit" breaks compatibility, but on the 3rd hand - it's not that a lot of people are setting the name to 19 characters :) If you find it better, let's keep the error. I like your comments and work you've done. |
Reject user-specified advertising names longer than 18 UTF-8 bytes before CoreBluetooth can fall back to ATT Prepare Write, which Nordic's buttonless DFU set-name command does not handle. The limit is hard-coded rather than derived from `maximumWriteValueLength(for: .withResponse)` because CoreBluetooth always reports 512 once Long Write is supported, so the dynamic value is not a useful guard against the prepare-write fallback. 18 bytes leaves room for the 1-byte op code and 1-byte length field within the 20-byte payload of the default ATT MTU.
|
Personally I think an error is better, especially as it is a fixed limit. I've updated the changes to 18 bytes. Thanks! |
Summary
DFUError.invalidAdvertisementNamewhen the name is too long for the current connection.Problem
The existing implementation truncates a user-specified alternative advertising name to 20 bytes, matching the buttonless DFU service's maximum advertising name length. However, the full set-name write also includes the opcode and length byte, so a 20-byte name requires a 22-byte ATT write payload.
With the default ATT MTU, the write-with-response payload can be only 20 bytes. In that case, CoreBluetooth may send the oversized set-name command using ATT Prepare Write requests instead of a single ATT Write Request. Nordic's buttonless DFU set-name command is handled as a single write, and does not respond to prepare write, resulting in a connection timeout.
This change validates against the actual
maximumWriteValueLength(for: .withResponse)at runtime. User-specified names that do not fit fail clearly instead of being truncated.