btleplug icon indicating copy to clipboard operation
btleplug copied to clipboard

works only in less than 10% of the time

Open Frank-Buss opened this issue 2 years ago • 1 comments

Describe the bug I'm trying to implement the mcumgr protocol. Here is a simple test program: mcumgr-client.zip The BLE device to which it connects is a board with a nRF5340. The mcumgr Go program works all the time, but the Rust program works only in less than 10% of the tries.

Expected behavior It should connect to the device, send the "image list" command, and show the response.

Actual behavior Here is an example session, showing various errors:

frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
VKA connected
Received data: [1, 0, 0, 141, 0, 1, 128, 0, 191, 102, 105, 109, 97, 103, 101, 115, 159, 191, 101, 105, 109, 97, 103, 101, 0, 100, 115, 108, 111, 116, 0, 103, 118, 101, 114, 115, 105, 111, 110, 101, 48, 46, 48, 46, 48, 100, 104, 97, 115, 104, 88, 32, 174, 31, 95, 201, 218, 130, 94, 94, 163, 205, 136, 0, 165, 49, 46, 129, 146, 178, 65, 54, 136, 68, 11, 125, 100, 168, 57, 241, 179, 76, 191, 47, 104, 98, 111, 111, 116, 97, 98, 108, 101, 245, 103, 112, 101, 110, 100, 105, 110, 103, 244, 105, 99, 111, 110, 102, 105, 114, 109, 101, 100, 245, 102, 97, 99, 116, 105, 118, 101, 245, 105, 112, 101, 114, 109, 97, 110, 101, 110, 116, 244, 255, 255, 107, 115, 112, 108, 105, 116, 83, 116, 97, 116, 117, 115, 0, 255]
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Disconnected early
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Error: deadline has elapsed
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Software caused connection abort
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
Error: Service discovery timed out
frank@hal9000:~/data/projects/vouch/github/mcumgr-client$ ./target/debug/mcumgr-client 
Peripheral found
VKA connected
Received data: [1, 0, 0, 141, 0, 1, 128, 0, 191, 102, 105, 109, 97, 103, 101, 115, 159, 191, 101, 105, 109, 97, 103, 101, 0, 100, 115, 108, 111, 116, 0, 103, 118, 101, 114, 115, 105, 111, 110, 101, 48, 46, 48, 46, 48, 100, 104, 97, 115, 104, 88, 32, 174, 31, 95, 201, 218, 130, 94, 94, 163, 205, 136, 0, 165, 49, 46, 129, 146, 178, 65, 54, 136, 68, 11, 125, 100, 168, 57, 241, 179, 76, 191, 47, 104, 98, 111, 111, 116, 97, 98, 108, 101, 245, 103, 112, 101, 110, 100, 105, 110, 103, 244, 105, 99, 111, 110, 102, 105, 114, 109, 101, 100, 245, 102, 97, 99, 116, 105, 118, 101, 245, 105, 112, 101, 114, 109, 97, 110, 101, 110, 116, 244, 255, 255, 107, 115, 112, 108, 105, 116, 83, 116, 97, 116, 117, 115, 0, 255]

Additional context Tested on Debian Linux.

Frank-Buss avatar Jul 19 '23 12:07 Frank-Buss

In case you are still interseted - i looked deeper at this, as I happen to have nrf5340 devices, and use mcumgr on them.

The underlying cause is that the 5340 isn't responding in time. The connection aborts you see here are really an inability to connect, and the underlying cause is that the peripheral is not responding in the first 6-7 data windows.

This is happneing mainly because your code does not stop scanning before connecting. I haven't debugged whether it's because you are generating enough traffic that the device doesn't get a window to respond to the connect, or what.

However, if I change your code to stop scanning and wait 10ms before connecting, it works ~100% of the time.

dberlin avatar Feb 07 '24 04:02 dberlin