fix(ble): Add checks for BLE initialized#12271
Conversation
👋 Hello lucasssvaz, we appreciate your contribution to this project! 📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more. 🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project. Click to see more instructions ...
Review and merge process you can expect ...
|
Test Results 90 files 90 suites 33m 53s ⏱️ Results for commit 400ce4f. ♻️ This comment has been updated with latest results. |
Memory usage test (comparing PR against master branch)The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.
Click to expand the detailed deltas report [usage change in BYTES]
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description of Change
This pull request improves the robustness of the BLE library by ensuring that BLE operations are only performed after successful initialization. It also changes the
BLEDevice::initmethod to return a boolean indicating success or failure, allowing users to handle initialization errors more gracefully. Most BLE-related methods now check whether initialization has occurred and log an error if not, preventing undefined behavior.Key changes include:
Initialization Handling and API Improvements
BLEDevice::initto return aboolindicating success or failure, instead ofvoid. The method now returns early on failure and sets theinitializedflag only on success. (libraries/BLE/src/BLEDevice.cpp,libraries/BLE/src/BLEDevice.h,libraries/BLE/examples/Server/Server.ino) [1] [2] [3] [4] [5] [6] [7] [8] [9]Defensive Checks for Initialization
createClient,createServer,getScan,getAdvertising,setPower,getPower,whiteListAdd,whiteListRemove,setMTU,getPeerIRK,startAdvertising,stopAdvertising,setCustomGapHandler,setOwnAddrType,setOwnAddr) to ensure BLE is initialized before proceeding, logging an error and returning early if not. (libraries/BLE/src/BLEDevice.cpp) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]Error Handling in Client Operations
getValueandsetValueto prevent dereferencing a null pointer if client creation fails. (libraries/BLE/src/BLEDevice.cpp) [1] [2]These changes make the BLE library safer and more reliable, especially in scenarios where initialization might fail or be omitted.
Test Scenarios
Tested locally