[Breaking] Change all time input parameters to milliseconds. (#78)

Changes all functions that accept a time parameter to use milliseconds instead of seconds.

* Adds duration input to NimBLEDevice::startAdvertising and NimBLEServer::startAdvertising.
This commit is contained in:
h2zero 2022-08-26 19:32:01 -06:00 committed by GitHub
parent 0b6337538c
commit bb3dd5f114
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 36 additions and 35 deletions

View file

@ -17,6 +17,7 @@ For more information on the improvements and additions please refer to the [clas
* [Remote Services](#remote-services) * [Remote Services](#remote-services)
* [Remote characteristics](#remote-characteristics) * [Remote characteristics](#remote-characteristics)
* [Security](#client-security) * [Security](#client-security)
* [Scanning](#scan-api)
* [General Security](#security-api) * [General Security](#security-api)
* [Configuration](#arduino-configuration) * [Configuration](#arduino-configuration)
<br/> <br/>
@ -321,6 +322,11 @@ The client will automatically initiate security when the peripheral responds tha
The default configuration will use "just-works" pairing with no bonding, if you wish to enable bonding see below. The default configuration will use "just-works" pairing with no bonding, if you wish to enable bonding see below.
<br/> <br/>
<a name="scan-api"></a>
### BLE Scan
The scan API is mostly unchanged from the original except for `NimBLEScan::start`, in which the duration parameter is now in milliseconds instead of seconds.
<br/>
<a name="security-api"></a> <a name="security-api"></a>
## Security API ## Security API
Security operations have been moved to `BLEDevice` (`NimBLEDevice`). Security operations have been moved to `BLEDevice` (`NimBLEDevice`).

View file

@ -146,7 +146,7 @@ After initializing the NimBLE stack we create a scan instance by calling `NimBLE
Once we have created the scan we can start looking for advertising servers. Once we have created the scan we can start looking for advertising servers.
To do this we call `NimBLEScan::start(duration)`, the duration parameter is a uint32_t that specifies the number of seconds to scan for, To do this we call `NimBLEScan::start(duration)`, the duration parameter is a uint32_t that specifies the number of milliseconds to scan for,
passing 0 will scan forever. passing 0 will scan forever.
In this example we will scan for 10 seconds. This is a blocking function (a non blocking overload is also available). In this example we will scan for 10 seconds. This is a blocking function (a non blocking overload is also available).
@ -162,7 +162,7 @@ void app_main(void)
NimBLEDevice::init(""); NimBLEDevice::init("");
NimBLEScan *pScan = NimBLEDevice::getScan(); NimBLEScan *pScan = NimBLEDevice::getScan();
NimBLEScanResults results = pScan->start(10); NimBLEScanResults results = pScan->start(10 * 1000);
} }
``` ```
<br/> <br/>
@ -302,7 +302,7 @@ void app_main(void)
NimBLEDevice::init(""); NimBLEDevice::init("");
NimBLEScan *pScan = NimBLEDevice::getScan(); NimBLEScan *pScan = NimBLEDevice::getScan();
NimBLEScanResults results = pScan->start(10); NimBLEScanResults results = pScan->start(10 * 1000);
NimBLEUUID serviceUuid("ABCD"); NimBLEUUID serviceUuid("ABCD");

View file

@ -16,7 +16,7 @@ void scanEndedCB(NimBLEScanResults results);
static NimBLEAdvertisedDevice* advDevice; static NimBLEAdvertisedDevice* advDevice;
static bool doConnect = false; static bool doConnect = false;
static uint32_t scanTime = 0; /** 0 = scan forever */ static uint32_t scanTime = 0; /** scan time in milliseconds, 0 = scan forever */
/** None of these are required as they will be handled by the library with defaults. ** /** None of these are required as they will be handled by the library with defaults. **

View file

@ -18,7 +18,7 @@ void scanEndedCB(NimBLEScanResults results);
static NimBLEAdvertisedDevice* advDevice; static NimBLEAdvertisedDevice* advDevice;
static bool doConnect = false; static bool doConnect = false;
static uint32_t scanTime = 10; /* 0 = scan forever */ static uint32_t scanTime = 10 * 1000; // In milliseconds, 0 = scan forever
/* Define the PHY's to use when connecting to peer devices, can be 1, 2, or all 3 (default).*/ /* Define the PHY's to use when connecting to peer devices, can be 1, 2, or all 3 (default).*/
static uint8_t connectPhys = BLE_GAP_LE_PHY_CODED_MASK | BLE_GAP_LE_PHY_1M_MASK /*| BLE_GAP_LE_PHY_2M_MASK */ ; static uint8_t connectPhys = BLE_GAP_LE_PHY_CODED_MASK | BLE_GAP_LE_PHY_1M_MASK /*| BLE_GAP_LE_PHY_2M_MASK */ ;
@ -79,8 +79,8 @@ bool connectToServer() {
*/ */
pClient->setConnectPhy(connectPhys); pClient->setConnectPhy(connectPhys);
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */ /** Set how long we are willing to wait for the connection to complete (milliseconds), default is 30000. */
pClient->setConnectTimeout(10); pClient->setConnectTimeout(10 * 1000);
if (!pClient->connect(advDevice)) { if (!pClient->connect(advDevice)) {
/* Created a client but failed to connect, don't need to keep it as it has no data */ /* Created a client but failed to connect, don't need to keep it as it has no data */

View file

@ -171,7 +171,7 @@ void connectTask (void * parameter){
/*** Note: write value now returns true if successful, false otherwise - try again or disconnect ***/ /*** Note: write value now returns true if successful, false otherwise - try again or disconnect ***/
pRemoteCharacteristic->writeValue((uint8_t*)buf, strlen(buf), false); pRemoteCharacteristic->writeValue((uint8_t*)buf, strlen(buf), false);
}else if(doScan){ }else if(doScan){
BLEDevice::getScan()->start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino BLEDevice::getScan()->start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it
} }
vTaskDelay(1000/portTICK_PERIOD_MS); // Delay a second between loops. vTaskDelay(1000/portTICK_PERIOD_MS); // Delay a second between loops.
@ -195,6 +195,6 @@ void app_main(void) {
pBLEScan->setActiveScan(true); pBLEScan->setActiveScan(true);
xTaskCreate(connectTask, "connectTask", 5000, NULL, 1, NULL); xTaskCreate(connectTask, "connectTask", 5000, NULL, 1, NULL);
pBLEScan->start(5, false); pBLEScan->start(5 * 1000, false);
} // End of setup. } // End of setup.

View file

@ -17,7 +17,7 @@
extern "C"{void app_main(void);} extern "C"{void app_main(void);}
int scanTime = 5; //In seconds int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
BLEScan* pBLEScan; BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {

View file

@ -385,7 +385,7 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
/** /**
* @brief Start advertising. * @brief Start advertising.
* @param [in] duration The duration, in seconds, to advertise, 0 == advertise forever. * @param [in] duration The duration, in milliseconds, to advertise, 0 == advertise forever.
* @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends. * @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends.
* @return True if advertising started successfully. * @return True if advertising started successfully.
*/ */
@ -423,9 +423,6 @@ bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdv
if(duration == 0){ if(duration == 0){
duration = BLE_HS_FOREVER; duration = BLE_HS_FOREVER;
} }
else{
duration = duration*1000; // convert duration to milliseconds
}
m_advCompCB = advCompleteCB; m_advCompCB = advCompleteCB;

View file

@ -533,10 +533,10 @@ NimBLEConnInfo NimBLEClient::getConnInfo() {
/** /**
* @brief Set the timeout to wait for connection attempt to complete. * @brief Set the timeout to wait for connection attempt to complete.
* @param [in] time The number of seconds before timeout. * @param [in] time The number of milliseconds before timeout.
*/ */
void NimBLEClient::setConnectTimeout(uint8_t time) { void NimBLEClient::setConnectTimeout(uint32_t time) {
m_connectTimeout = (uint32_t)(time * 1000); m_connectTimeout = time;
} // setConnectTimeout } // setConnectTimeout

View file

@ -63,7 +63,7 @@ public:
uint16_t getConnId(); uint16_t getConnId();
uint16_t getMTU(); uint16_t getMTU();
bool secureConnection(); bool secureConnection();
void setConnectTimeout(uint8_t timeout); void setConnectTimeout(uint32_t timeout);
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval, void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout, uint16_t latency, uint16_t timeout,
uint16_t scanInterval=16, uint16_t scanWindow=16); uint16_t scanInterval=16, uint16_t scanWindow=16);

View file

@ -170,10 +170,11 @@ NimBLEAdvertising* NimBLEDevice::getAdvertising() {
/** /**
* @brief Convenience function to begin advertising. * @brief Convenience function to begin advertising.
* @param [in] duration The duration in milliseconds to advertise for, default = forever.
* @return True if advertising started successfully. * @return True if advertising started successfully.
*/ */
bool NimBLEDevice::startAdvertising() { bool NimBLEDevice::startAdvertising(uint32_t duration) {
return getAdvertising()->start(); return getAdvertising()->start(duration);
} // startAdvertising } // startAdvertising
# endif # endif

View file

@ -151,7 +151,7 @@ public:
static bool stopAdvertising(); static bool stopAdvertising();
# else # else
static NimBLEAdvertising* getAdvertising(); static NimBLEAdvertising* getAdvertising();
static bool startAdvertising(); static bool startAdvertising(uint32_t duration = 0);
static bool stopAdvertising(); static bool stopAdvertising();
# endif # endif
#endif #endif

View file

@ -302,7 +302,7 @@ bool NimBLEScan::isScanning() {
/** /**
* @brief Start scanning. * @brief Start scanning.
* @param [in] duration The duration in seconds for which to scan. * @param [in] duration The duration in milliseconds for which to scan.
* @param [in] scanCompleteCB A function to be called when scanning has completed. * @param [in] scanCompleteCB A function to be called when scanning has completed.
* @param [in] is_continue Set to true to save previous scan results, false to clear them. * @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @return True if scan started or false if there was an error. * @return True if scan started or false if there was an error.
@ -319,10 +319,6 @@ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResul
if(duration == 0){ if(duration == 0){
duration = BLE_HS_FOREVER; duration = BLE_HS_FOREVER;
} }
else{
// convert duration to milliseconds
duration = duration * 1000;
}
// Set the flag to ignore the results while we are deleting the vector // Set the flag to ignore the results while we are deleting the vector
if(!is_continue) { if(!is_continue) {

View file

@ -748,15 +748,16 @@ bool NimBLEServer::stopAdvertising(uint8_t inst_id) {
} // stopAdvertising } // stopAdvertising
#endif #endif
#if !CONFIG_BT_NIMBLE_EXT_ADV|| defined(_DOXYGEN_) #if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
/** /**
* @brief Start advertising. * @brief Start advertising.
* @param [in] duration The duration in milliseconds to advertise for, default = forever.
* @return True if advertising started successfully. * @return True if advertising started successfully.
* @details Start the server advertising its existence. This is a convenience function and is equivalent to * @details Start the server advertising its existence. This is a convenience function and is equivalent to
* retrieving the advertising object and invoking start upon it. * retrieving the advertising object and invoking start upon it.
*/ */
bool NimBLEServer::startAdvertising() { bool NimBLEServer::startAdvertising(uint32_t duration) {
return getAdvertising()->start(); return getAdvertising()->start(duration);
} // startAdvertising } // startAdvertising
#endif #endif

View file

@ -59,7 +59,7 @@ public:
bool stopAdvertising(uint8_t inst_id); bool stopAdvertising(uint8_t inst_id);
#else #else
NimBLEAdvertising* getAdvertising(); NimBLEAdvertising* getAdvertising();
bool startAdvertising(); bool startAdvertising(uint32_t duration = 0);
#endif #endif
bool stopAdvertising(); bool stopAdvertising();
void start(); void start();