Skip to content
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

Write method for returning the number of bytes transferred #371

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fabik111
Copy link
Contributor

@fabik111 fabik111 commented Oct 3, 2024

The current writeValue method, used for writing the value of a characteristic, returns an integer value.
This value is used as a Boolean to indicate the success or the failure of the writing operation, without giving any indication about the amount of bytes actually transferred (in case of subscription) or "loaded" into the characteristic (in case of a readable characteristic).
Considering that the MTU, the amount of transferable data, is decided by the central and varies device by device, it is impossible for the user to determinate how many bytes are transferred and upon that to take an action.
In this PR a write method is added for the generic BLECharacteristic returning the number of bytes transferred.

It works both when the device is in peripheral and central mode.

This comment was marked as outdated.

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Oct 3, 2024

This comment was marked as outdated.

This comment was marked as outdated.

Copy link

Memory usage change @ 166debb

Board flash % RAM for global variables %
arduino:mbed_nano:nano33ble 🔺 +72 - +136 +0.01 - +0.01 0 - 0 0.0 - 0.0
arduino:mbed_nano:nanorp2040connect 🔺 +46 - +46 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:uno2018:mode=on 🔺 +140 - +140 +0.29 - +0.29 0 - 0 0.0 - 0.0
arduino:renesas_uno:unor4wifi 🔺 +72 - +88 +0.03 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 🔺 +48 - +48 +0.02 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 🔺 +48 - +48 +0.02 - +0.02 0 - 0 0.0 - 0.0
Click for full report table
Board examples/Central/LedControl
flash
% examples/Central/LedControl
RAM for global variables
% examples/Central/PeripheralExplorer
flash
% examples/Central/PeripheralExplorer
RAM for global variables
% examples/Central/Scan
flash
% examples/Central/Scan
RAM for global variables
% examples/Central/ScanCallback
flash
% examples/Central/ScanCallback
RAM for global variables
% examples/Central/SensorTagButton
flash
% examples/Central/SensorTagButton
RAM for global variables
% examples/Peripheral/Advertising/EnhancedAdvertising
flash
% examples/Peripheral/Advertising/EnhancedAdvertising
RAM for global variables
% examples/Peripheral/Advertising/RawDataAdvertising
flash
% examples/Peripheral/Advertising/RawDataAdvertising
RAM for global variables
% examples/Peripheral/BatteryMonitor
flash
% examples/Peripheral/BatteryMonitor
RAM for global variables
% examples/Peripheral/ButtonLED
flash
% examples/Peripheral/ButtonLED
RAM for global variables
% examples/Peripheral/CallbackLED
flash
% examples/Peripheral/CallbackLED
RAM for global variables
% examples/Peripheral/EncryptedBatteryMonitor
flash
% examples/Peripheral/EncryptedBatteryMonitor
RAM for global variables
% examples/Peripheral/LED
flash
% examples/Peripheral/LED
RAM for global variables
%
arduino:mbed_nano:nano33ble 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 136 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0 72 0.01 0 0.0
arduino:mbed_nano:nanorp2040connect 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0 46 0.0 0 0.0
arduino:megaavr:uno2018:mode=on 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0 140 0.29 0 0.0
arduino:renesas_uno:unor4wifi 88 0.03 0 0.0 72 0.03 0 0.0 72 0.03 0 0.0 72 0.03 0 0.0 88 0.03 0 0.0 88 0.03 0 0.0 72 0.03 0 0.0 72 0.03 0 0.0 88 0.03 0 0.0 72 0.03 0 0.0 72 0.03 0 0.0 88 0.03 0 0.0
arduino:samd:mkrwifi1010 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0
arduino:samd:nano_33_iot 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0 48 0.02 0 0.0
Click for full report CSV
Board,examples/Central/LedControl<br>flash,%,examples/Central/LedControl<br>RAM for global variables,%,examples/Central/PeripheralExplorer<br>flash,%,examples/Central/PeripheralExplorer<br>RAM for global variables,%,examples/Central/Scan<br>flash,%,examples/Central/Scan<br>RAM for global variables,%,examples/Central/ScanCallback<br>flash,%,examples/Central/ScanCallback<br>RAM for global variables,%,examples/Central/SensorTagButton<br>flash,%,examples/Central/SensorTagButton<br>RAM for global variables,%,examples/Peripheral/Advertising/EnhancedAdvertising<br>flash,%,examples/Peripheral/Advertising/EnhancedAdvertising<br>RAM for global variables,%,examples/Peripheral/Advertising/RawDataAdvertising<br>flash,%,examples/Peripheral/Advertising/RawDataAdvertising<br>RAM for global variables,%,examples/Peripheral/BatteryMonitor<br>flash,%,examples/Peripheral/BatteryMonitor<br>RAM for global variables,%,examples/Peripheral/ButtonLED<br>flash,%,examples/Peripheral/ButtonLED<br>RAM for global variables,%,examples/Peripheral/CallbackLED<br>flash,%,examples/Peripheral/CallbackLED<br>RAM for global variables,%,examples/Peripheral/EncryptedBatteryMonitor<br>flash,%,examples/Peripheral/EncryptedBatteryMonitor<br>RAM for global variables,%,examples/Peripheral/LED<br>flash,%,examples/Peripheral/LED<br>RAM for global variables,%
arduino:mbed_nano:nano33ble,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,136,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0,72,0.01,0,0.0
arduino:mbed_nano:nanorp2040connect,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0,46,0.0,0,0.0
arduino:megaavr:uno2018:mode=on,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0,140,0.29,0,0.0
arduino:renesas_uno:unor4wifi,88,0.03,0,0.0,72,0.03,0,0.0,72,0.03,0,0.0,72,0.03,0,0.0,88,0.03,0,0.0,88,0.03,0,0.0,72,0.03,0,0.0,72,0.03,0,0.0,88,0.03,0,0.0,72,0.03,0,0.0,72,0.03,0,0.0,88,0.03,0,0.0
arduino:samd:mkrwifi1010,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0
arduino:samd:nano_33_iot,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0,48,0.02,0,0.0

@andreagilardoni
Copy link
Contributor

I would like to open a discussion about changing the behaviour of the function writeValue instead of writing a new one I would still use the same and return the length instead of 1 or 0.
The reason is if there is an error we can return 0 and there is no breaking change in that, if there is no error we return the length which can be evaluated to true in a boolean expression. The only case where this could not work is if someone is directly comparing the result of write with 1, in which the expected behaviour is not preserved.

I would also like to open the same discussion for handleInd and handleInd which can be changed to the definition I put below. The behaviour is changed in the sense that the length is returned (which cannot be higher than a 16 bit, if I recall correctly). For the same reasons I described above I don't see a reason why this cannot work in the same way.

virtual bool handleNotify(uint16_t handle, const uint8_t* value, int length);
virtual bool handleInd(uint16_t handle, const uint8_t* value, int length);

virtual uint16_t handleNotify(uint16_t handle, const uint8_t* value, uint16_t length);
virtual uint16_t handleInd(uint16_t handle, const uint8_t* value, uint16_t length);

This will help us shrink the code size and avoid repeating the definition of functions that are performing the same operation but slightly different.

@pennam
Copy link

pennam commented Mar 27, 2025

handleNotify and handleInd are returning a bool value that is not strictly related to the length, respectively

return (numNotifications > 0); and return (numIndications > 0);

returning the length would change how the function is working

@andreagilardoni
Copy link
Contributor

@pennam Then we can exclude these functions from the discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants