Skip to content

GATT: UnixStream -> UnixDatagram#172

Merged
surban merged 3 commits intobluez:masterfrom
jonasrudloff:gatt_sock_dgram_stream_fix
Jun 6, 2025
Merged

GATT: UnixStream -> UnixDatagram#172
surban merged 3 commits intobluez:masterfrom
jonasrudloff:gatt_sock_dgram_stream_fix

Conversation

@jonasrudloff
Copy link
Contributor

GATT communication with bluetoothd happens over a SOCK_SEQPACKET socket, however bluer uses UnixStream socket for those sockets, this leads to issues with tokio/mio/epoll, when receiving notification data from from a remote GATT characteristic. The problem works as follows:

  • the bluer application does initialization and subscribes to GATT notification events, tokio/mio puts the UnixStream socket into epoll using edge trigger mode.
  • Remote service send multiple notification event(at least 3?)
  • The bluer application wakes from epoll and receives the first event, it then begins processing the first event.
  • While the application processes the event, more events are received(at least 2 more)
  • The application finishes processing of the event and call epoll_wait, as there has been more new events since last epoll the application is woken immediately to process the second event.
  • When the process enter epoll_wait this time it will not get woken again, even if there are still events to process, as there are no NEW events since last epoll. The application will only receive the next event when an additional new event is received, however this new event will remain in the queue and not be processed.

This happens because tokio/mio assumes that on a UnixStream socket there is no more data available if the read does not fill the whole buffer. However the correct thing to do on a SOCK_SEQPACKET socket is to read from the socket until it returns EAGAIN processing every packet in the queue.

This is what UnixDatagram socket does.

This pull request uses UnixDatagram sockets instead of UnixStream sockets for GATT Characteristic communication.

@surban surban self-assigned this May 12, 2025
@jonasrudloff
Copy link
Contributor Author

@surban Whats the status on this? Do you need anything from me to get this moving?

@surban
Copy link
Collaborator

surban commented May 14, 2025

No time for review yet. I hope next week.

@jonasrudloff
Copy link
Contributor Author

@surban Whats the status on this? Do I need to do anymore work on this to get it merged?

I'm not sure about what to do with the AsyncRead and AsyncWrite traits implemented for characteristics, these traits seems like a bad match for characteristics as the model for characteristics is chunked or fixed sized data, while AsyncRead and AsyncWrite assumes stream like behavior.

@jonasrudloff
Copy link
Contributor Author

@surban Whats the status on this?

@surban surban merged commit ec21000 into bluez:master Jun 6, 2025
2 checks passed
@surban
Copy link
Collaborator

surban commented Jun 6, 2025

Looks good. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants