Skip to content

Commit

Permalink
feat(ble): Update esp-nimble-cpp and simplify BleGattServer (#350)
Browse files Browse the repository at this point in the history
* Update esp-nimble-cpp submodule to have new server-client code
* Update `BleGattServer` to remove extra `client` pointer used for getting name / rssi from connected clients, instead using the new client that is managed within the server itself.
  • Loading branch information
finger563 authored Dec 6, 2024
1 parent 70a230a commit 7976f35
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
38 changes: 8 additions & 30 deletions components/ble_gatt_server/include/ble_gatt_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,6 @@ class BleGattServer : public BaseComponent {
return false;
}

client_ = NimBLEDevice::createClient();
if (!client_) {
logger_.error("Failed to create client");
return false;
}

// set the server callbacks
server_->setCallbacks(new BleGattServerCallbacks(this));

Expand All @@ -286,8 +280,8 @@ class BleGattServer : public BaseComponent {
/// @note This method will also deinitialize the device info and battery
/// services.
void deinit() {
if (!server_ || !client_) {
logger_.info("Server / Client are nullptr; already deinitialized, not deinitializing again");
if (!server_) {
logger_.info("Server is nullptr; already deinitialized, not deinitializing again");
return;
}

Expand All @@ -298,9 +292,8 @@ class BleGattServer : public BaseComponent {
// invalidates any references/pointers to them
bool clear_all = true;
NimBLEDevice::deinit(clear_all);
// clear the server and client
// clear the server
server_ = nullptr;
client_ = nullptr;
}

/// Start the services
Expand Down Expand Up @@ -532,19 +525,14 @@ class BleGattServer : public BaseComponent {
logger_.error("Server not created");
return {};
}
if (!client_) {
logger_.error("Client not created");
return {};
}
// since this connection is handled by the server, we won't manually
// connect, and instead inform the client that we are already connected
// using this conn handle
client_->clearConnection();
client_->setConnection(conn_info);
auto client = server_->getClient(conn_info);
// refresh the services
client_->getServices(true);
client->getServices(true);
// now get Generic Access Service
auto gas = client_->getService(NimBLEUUID("1800"));
auto gas = client->getService(NimBLEUUID("1800"));
if (!gas) {
logger_.error("Failed to get Generic Access Service");
return {};
Expand All @@ -562,8 +550,6 @@ class BleGattServer : public BaseComponent {
}
// and read it
auto name = name_char->readValue();
// unset the client connection
client_->clearConnection();
return name;
}

Expand Down Expand Up @@ -600,10 +586,6 @@ class BleGattServer : public BaseComponent {
logger_.error("Server not created");
return {};
}
if (!client_) {
logger_.error("Client not created");
return {};
}
if (!is_connected()) {
logger_.error("Not connected to any devices");
return {};
Expand All @@ -613,12 +595,9 @@ class BleGattServer : public BaseComponent {
// since this connection is handled by the server, we won't manually
// connect, and instead inform the client that we are already connected
// using this conn handle
client_->clearConnection();
client_->setConnection(conn_info);
auto client = server_->getClient(conn_info);
// and read the RSSI from the client
auto rssi = client_->getRssi();
// unset the client connection
client_->clearConnection();
auto rssi = client->getRssi();
logger_.info("RSSI for connected device {}: {}", peer_address.toString(), rssi);
return rssi;
}
Expand Down Expand Up @@ -691,7 +670,6 @@ class BleGattServer : public BaseComponent {

Callbacks callbacks_{}; ///< The callbacks for the GATT server.
NimBLEServer *server_{nullptr}; ///< The GATT server.
NimBLEClient *client_{nullptr}; ///< The client.
DeviceInfoService device_info_service_; ///< The device info service.
BatteryService battery_service_; ///< The battery service.
};
Expand Down

0 comments on commit 7976f35

Please sign in to comment.