-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Bluetooth: Userchan: Parse multiple hci packets #61764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bluetooth: Userchan: Parse multiple hci packets #61764
Conversation
1afe8e4 to
40bd425
Compare
e35b5ca to
35cc0d2
Compare
49a7616 to
e6f3dda
Compare
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem correct. You have the packet type as the first byte and the packet-specific header comes after that. In all other cases you read &packet_buff[1] but here you read &packet_buff[0]. Actually, I don't see any reason why you need two parameters to this function. Just pass the original buffer and inside the function you can then do uint8_t type = buf[0];.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry mistake on my part while changing to the suggestions of the other reviewer.
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This generates unnecessarily complicated code. Just do size = sys_le16_to_cpu(hdr->len);. The structs are packed, so accessing the struct members generates automatically alignment-safe code (if needed by the CPU architecture).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the packed attribute deal with endianness ? Thats the reason I thought parts like these ones are needed in the current bluetooth stack
Edit: I read again your comment, ok are you only referring to changing the name of the variable from payload size to size ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the other review comments accordingly but this one is still is not clear to me.
Could you be more specific on your suggestion?
Just do
size = sys_le16_to_cpu(hdr->len)
I understood either:
- Change the name of the variable
payload_lentosize? - Obtain the size of the payload via
size = sys_le16_to_cpu(hdr->len);before the switch case? Its a trivial change and only the edge case for the structbt_hci_cmd_hdrhas to be taken into account (param_len).
Sorry for the misunderstanding on my part, looking forward for your comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think I overthought the comment. Changed to using sys_le16_to_cpu instead of sys_get_le16 and the lenght member of the respective hci header struct instead of the address.
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an unnecessary variable (doesn't add much to readability, and the way you've named it it's even longer thansizeof(packet_type).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed it to now use it directly in the the return statement of the function.
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call these len instead of size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just call this function packet_len()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
include/zephyr/bluetooth/hci_types.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the same struct alignment style as elsewhere in the headers, so that the struct member names are aligned. That means two spaces after uint8_t but only one after uint16_t.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
dd35ea2 to
57fdff7
Compare
4ff40fd to
0f51a22
Compare
0f51a22 to
3df2e5f
Compare
jhedberg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more review round (thanks for your patience!). After that I think this is ready to be merged.
I think it'd also make sense to split the hci_types.h changes into its own commit (still in this same PR) before the userchan.c commit, since those changes are not strictly related to the userchan driver.
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if this is getting a bit overly pedantic, but I'd prefer if we stick to existing variable naming conventions and also avoid unnecessarily long names. This helps keep the code more readable (for someone who has read a lot of other Bluetooth subsystem code). and also avoids excessively long lines.
Just call the input parameter const uint8_t *buf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no problem, I am aware that style and naming conventions are highly dependent on organisations policies. As I am still not that familiar with the codebase of Zephyr I know this could come up.
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const uint8_t type = buf[0];
const uint8_t hdr = &buf[sizeof(type)];There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const struct bt_hci_cmd_hdr *cmd = (const struct bt_hci_cmd_hdr *)hdr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const struct bt_hci_acl_hdr *acl = (const struct bt_hci_acl_hdr *)hdr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const struct bt_hci_sco_hdr *sco = (const struct bt_hci_sco_hdr *)hdr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const struct bt_hci_evt_hdr *evt = (const struct bt_hci_evt_hdr *)hdr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const struct bt_hci_iso_hdr *iso = (const struct bt_hci_iso_hdr *)hdr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might make sense to add a LOG_WRN("Unknown packet type 0x%02x", type) before the return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
include/zephyr/bluetooth/hci_types.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other connection header structs (ACL & ISO) use simply handle here, so I'd stick to that for consistency (unless the HCI spec is actually fundamentally different in its naming conventions for these headers)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I just checked and the HCI spec uses Connection_Handle both for SCO and ISO, but just Handle for ACL. However, since the ISO struct already uses simply handle I'd follow that convention for SCO as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
3df2e5f to
758d0f8
Compare
|
Added last suggestions. Pending separation of commits... |
758d0f8 to
05d08cf
Compare
Added HCI SCO header definition and included comments that refer to the Bluetooth core spec sections for the other HCI header types. Signed-off-by: Victor Chavez <[email protected]>
05d08cf to
22b5012
Compare
|
I have separated the commits. |
b5a6c51 to
5deee69
Compare
drivers/bluetooth/hci/userchan.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| payload_len = bt_iso_hdr_len(sys_le16_to_cpu(iso->len)); | |
| header_len = BT_HCI_ISO_HDR_SIZE; | |
| } | |
| payload_len = bt_iso_hdr_len(sys_le16_to_cpu(iso->len)); | |
| header_len = BT_HCI_ISO_HDR_SIZE; | |
| break; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing this, have been changing different parts that I must have deleted the break while refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it existed earlier :D
After conducting tests with a a virtual Bluetooth controller over TCP it was noticed that some HCI packets may arrive on the same buffer if sent over a short period of time. This update ensures the hci packets are parsed correctly in the case multiple packets are in the same recieving buffer according to the Bluetooth Spec v5.4 Part E. Signed-off-by: Victor Chavez <[email protected]>
5deee69 to
fee9dc4
Compare
After conducting tests with a a virtual Bluetooth controller over TCP it was noticed that some HCI packets may arrive on the same buffer if sent over a short period of time.
This update ensures the hci packets are parsed correctly in the case multiple packets are in the same recieving buffer according to how HCI packets are decoded in the Bluetooth Spec v5.2 Part E.
This should fix #61762
Open for suggestions and discussion on how to address this the best way.