Skip to content

Commit

Permalink
feat(ble_gatt_server): return status on init, start, start advertising (
Browse files Browse the repository at this point in the history
  • Loading branch information
finger563 authored Mar 25, 2024
1 parent c883203 commit 19cc502
Show file tree
Hide file tree
Showing 102 changed files with 267 additions and 221 deletions.
27 changes: 20 additions & 7 deletions components/ble_gatt_server/include/ble_gatt_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ class BleGattServer : public BaseComponent {

/// Initialize the GATT server
/// This method creates the GATT server and sets the callbacks to this class.
void init(const std::string &device_name) {
/// It also initializes the device info and battery services.
/// @param device_name The name of the device.
/// @note This method must be called before creating any other services or
/// characteristics.
/// @note This method must be called before starting the server.
/// @return Whether the GATT server was initialized successfully.
bool init(const std::string &device_name) {
logger_.info("Initializing GATT server with device name: '{}'", device_name);
// create the device
NimBLEDevice::init(device_name);
Expand All @@ -213,7 +219,7 @@ class BleGattServer : public BaseComponent {
server_ = NimBLEDevice::createServer();
if (!server_) {
logger_.error("Failed to create server");
return;
return false;
}

// set the server callbacks
Expand All @@ -226,6 +232,8 @@ class BleGattServer : public BaseComponent {
logger_.info("Creating battery service");
// create the battery service
battery_service_.init(server_);

return true;
}

/// Deinitialize the GATT server
Expand Down Expand Up @@ -258,12 +266,14 @@ class BleGattServer : public BaseComponent {
/// This method starts the GATT server.
/// This method must be called after the server has been initialized, and
/// after any services / characteristics have been added to the server.
void start() {
/// @return Whether the server was started successfully.
bool start() {
if (!server_) {
logger_.error("Server not created");
return;
return false;
}
server_->start();
return true;
}

#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
Expand Down Expand Up @@ -338,20 +348,22 @@ class BleGattServer : public BaseComponent {
/// the advertising will not timeout. If non-zero, the
/// advertising will stop after the specified duration.
/// @param instance The advertising instance to start.
/// @return Whether advertising was started successfully.
/// @note This is only available when CONFIG_BT_NIMBLE_EXT_ADV is enabled.
void start_advertising(uint32_t duration_ms = 0, uint8_t instance = 0);
bool start_advertising(uint32_t duration_ms = 0, uint8_t instance = 0);
#endif // CONFIG_BT_NIMBLE_EXT_ADV

#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
/// Start Advertising using the previously set advertising data
/// This method simply starts advertising using the previously set advertising
/// data.
/// @param params The advertising parameters for the device.
/// @return Whether advertising was started successfully.
/// @note This method is only used when CONFIG_BT_NIMBLE_EXT_ADV is not
/// enabled, ane legacy advertising is used. Otherwise, use the
/// NimBLEExtAdvertisement class for advertising and setting the
/// advertising parameters.
void start_advertising(const AdvertisingParameters &params);
bool start_advertising(const AdvertisingParameters &params);

/// Start Advertising using the previously set advertising data
/// This method simply starts advertising using the previously set advertising
Expand All @@ -360,9 +372,10 @@ class BleGattServer : public BaseComponent {
/// If 0, the advertising will not timeout. If non-zero,
/// the advertising will stop after the specified duration.
/// @param directed_address The address to direct advertising to, if any.
/// @return Whether advertising was started successfully.
/// @note This method is only used when CONFIG_BT_NIMBLE_EXT_ADV is not
/// enabled, ane legacy advertising is used.
void start_advertising(uint32_t duration_ms = 0, NimBLEAddress *directed_address = nullptr);
bool start_advertising(uint32_t duration_ms = 0, NimBLEAddress *directed_address = nullptr);
#endif // CONFIG_BT_NIMBLE_EXT_ADV

/// @brief Get the GATT server.
Expand Down
14 changes: 8 additions & 6 deletions components/ble_gatt_server/src/ble_gatt_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ void BleGattServer::set_scan_response_data(const AdvertisedData &scan_response_d
advertising->setScanResponseData(const_cast<AdvertisedData &>(scan_response_data));
}

void BleGattServer::start_advertising(const AdvertisingParameters &advertising_params) {
bool BleGattServer::start_advertising(const AdvertisingParameters &advertising_params) {
if (!server_) {
logger_.error("Server not created");
return;
return false;
}
auto advertising = NimBLEDevice::getAdvertising();
if (!advertising) {
logger_.error("Advertising not created");
return;
return false;
}

logger_.info("Starting legacy advertising");
Expand Down Expand Up @@ -107,17 +107,18 @@ void BleGattServer::start_advertising(const AdvertisingParameters &advertising_p
if (!success) {
logger_.error("Failed to start advertising");
}
return success;
}

void BleGattServer::start_advertising(uint32_t duration_ms, NimBLEAddress *directed_address) {
bool BleGattServer::start_advertising(uint32_t duration_ms, NimBLEAddress *directed_address) {
if (!server_) {
logger_.error("Server not created");
return;
return false;
}
auto advertising = NimBLEDevice::getAdvertising();
if (!advertising) {
logger_.error("Advertising not created");
return;
return false;
}
logger_.info("Starting legacy advertising for {} ms", duration_ms);
// assume connectable
Expand All @@ -133,6 +134,7 @@ void BleGattServer::start_advertising(uint32_t duration_ms, NimBLEAddress *direc
if (!success) {
logger_.error("Failed to start advertising");
}
return success;
}

#endif // !CONFIG_BT_NIMBLE_EXT_ADV
Expand Down
7 changes: 4 additions & 3 deletions components/ble_gatt_server/src/ble_gatt_server_ext_adv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ void BleGattServer::stop_advertising(uint8_t instance) {
advertising->stop(instance);
}

void BleGattServer::start_advertising(uint32_t duration_ms, uint8_t instance) {
bool BleGattServer::start_advertising(uint32_t duration_ms, uint8_t instance) {
if (!server_) {
logger_.error("Server not created");
return;
return false;
}
auto advertising = NimBLEDevice::getAdvertising();
if (!advertising) {
logger_.error("Advertising not created");
return;
return false;
}

logger_.info("Starting ext advertising for instance {} with duration {} ms", instance,
Expand All @@ -91,6 +91,7 @@ void BleGattServer::start_advertising(uint32_t duration_ms, uint8_t instance) {
if (!success) {
logger_.error("Failed to start advertising for instance {}", instance);
}
return success;
}

#endif // CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/adc_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADC Types</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads1x15.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS1x15 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -169,7 +169,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads7138.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS7138 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -174,7 +174,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/continuous_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Continuous ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -174,7 +174,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>ADC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/oneshot_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Oneshot ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -173,7 +173,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/tla2528.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>TLA2528 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -174,7 +174,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/base_component.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Base Compoenent</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/base_component.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/base_component.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -163,7 +163,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/base_component/include/base_component.hpp">components/base_component/include/base_component.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/base_component/include/base_component.hpp">components/base_component/include/base_component.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/base_peripheral.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Base Peripheral</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/base_peripheral.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/base_peripheral.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -167,7 +167,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/base_peripheral/include/base_peripheral.hpp">components/base_peripheral/include/base_peripheral.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/base_peripheral/include/base_peripheral.hpp">components/base_peripheral/include/base_peripheral.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/battery/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>Battery APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/battery/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/battery/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/battery/max1704x.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<li><a href="index.html">Battery APIs</a> &raquo;</li>
<li>MAX1704X</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/battery/max1704x.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/battery/max1704x.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -181,7 +181,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/max1704x/include/max1704x.hpp">components/max1704x/include/max1704x.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/max1704x/include/max1704x.hpp">components/max1704x/include/max1704x.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/bldc/bldc_driver.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Driver</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9dc5dedd/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c883203f/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -165,7 +165,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9dc5dedd/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c883203f/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
Loading

0 comments on commit 19cc502

Please sign in to comment.