mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-15 15:22:34 +00:00
Fix ble rssi crash (#8661)
* Fix BLE crash occuring when trying to get RSSI from Android with a bad connection handle * Cleanup
This commit is contained in:
@@ -20,13 +20,14 @@
|
|||||||
#include "PowerStatus.h"
|
#include "PowerStatus.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)
|
|
||||||
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
||||||
#include "host/ble_gap.h"
|
#include "host/ble_gap.h"
|
||||||
#else
|
#else
|
||||||
#include "nimble/nimble/host/include/host/ble_gap.h"
|
#include "nimble/nimble/host/include/host/ble_gap.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
constexpr uint16_t kPreferredBleMtu = 517;
|
constexpr uint16_t kPreferredBleMtu = 517;
|
||||||
@@ -776,16 +777,35 @@ bool NimbleBluetooth::isConnected()
|
|||||||
|
|
||||||
int NimbleBluetooth::getRssi()
|
int NimbleBluetooth::getRssi()
|
||||||
{
|
{
|
||||||
if (bleServer && isConnected()) {
|
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6)
|
||||||
auto service = bleServer->getServiceByUUID(MESH_SERVICE_UUID);
|
if (!bleServer || !isConnected()) {
|
||||||
uint16_t handle = service->getHandle();
|
return 0; // No active BLE connection
|
||||||
#ifdef NIMBLE_TWO
|
|
||||||
return NimBLEDevice::getClientByHandle(handle)->getRssi();
|
|
||||||
#else
|
|
||||||
return NimBLEDevice::getClientByID(handle)->getRssi();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
return 0; // FIXME figure out where to source this
|
|
||||||
|
uint16_t connHandle = nimbleBluetoothConnHandle.load();
|
||||||
|
|
||||||
|
if (connHandle == BLE_HS_CONN_HANDLE_NONE) {
|
||||||
|
const auto peers = bleServer->getPeerDevices();
|
||||||
|
if (!peers.empty()) {
|
||||||
|
connHandle = peers.front();
|
||||||
|
nimbleBluetoothConnHandle = connHandle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connHandle == BLE_HS_CONN_HANDLE_NONE) {
|
||||||
|
return 0; // Connection handle not available yet
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t rssi = 0;
|
||||||
|
const int rc = ble_gap_conn_rssi(connHandle, &rssi);
|
||||||
|
|
||||||
|
if (rc == 0) {
|
||||||
|
return rssi;
|
||||||
|
}
|
||||||
|
LOG_DEBUG("BLE RSSI read failed, rc=%d", rc);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NimbleBluetooth::setup()
|
void NimbleBluetooth::setup()
|
||||||
|
|||||||
Reference in New Issue
Block a user