diff --git a/.gitignore b/.gitignore index b37ecce2..89963ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ docs/doxydocs .development +_codeql_detected_source_root diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 9116a485..3eb229db 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -82,14 +82,12 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const char* uuid, uint3 * @return The new BLE descriptor. */ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID& uuid, uint32_t properties, uint16_t maxLen) { - NimBLEDescriptor* pDescriptor = nullptr; if (uuid == NimBLEUUID(static_cast(0x2904))) { NIMBLE_LOGW(LOG_TAG, "0x2904 descriptor should be created with create2904()"); - pDescriptor = create2904(); - } else { - pDescriptor = new NimBLEDescriptor(uuid, properties, maxLen, this); + return create2904(); } + NimBLEDescriptor* pDescriptor = new NimBLEDescriptor(uuid, properties, maxLen, this); addDescriptor(pDescriptor); return pDescriptor; } // createDescriptor @@ -277,9 +275,10 @@ bool NimBLECharacteristic::sendValue(const uint8_t* value, size_t length, bool i const auto subs = getSubscribers(); // make a copy to avoid issues if subscribers change while sending ble_npl_hw_exit_critical(0); - bool chSpecified = connHandle != BLE_HS_CONN_HANDLE_NONE; - bool requireSecure = m_properties & (BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_READ_AUTHEN | BLE_GATT_CHR_F_READ_AUTHOR); + const bool chSpecified = connHandle != BLE_HS_CONN_HANDLE_NONE; + const bool requireSecure = m_properties & (BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_READ_AUTHEN | BLE_GATT_CHR_F_READ_AUTHOR); int rc = chSpecified ? BLE_HS_ENOENT : 0; // if handle specified, assume not found until sent + auto sendFunc = isNotification ? ble_gatts_notify_custom : ble_gatts_indicate_custom; // Notify all connected peers unless a specific handle is provided for (const auto& entry : subs) { @@ -306,11 +305,7 @@ bool NimBLECharacteristic::sendValue(const uint8_t* value, size_t length, bool i break; } - if (isNotification) { - rc = ble_gatts_notify_custom(ch, m_handle, om); - } else { - rc = ble_gatts_indicate_custom(ch, m_handle, om); - } + rc = sendFunc(ch, m_handle, om); if (rc != 0 || chSpecified) { break; @@ -437,11 +432,7 @@ void NimBLECharacteristic::writeEvent(const uint8_t* val, uint16_t len, NimBLECo * used to define any callbacks for the characteristic. */ void NimBLECharacteristic::setCallbacks(NimBLECharacteristicCallbacks* pCallbacks) { - if (pCallbacks != nullptr) { - m_pCallbacks = pCallbacks; - } else { - m_pCallbacks = &defaultCallback; - } + m_pCallbacks = pCallbacks != nullptr ? pCallbacks : &defaultCallback; } // setCallbacks /** diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 543787b8..9e7d11d4 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -636,19 +636,21 @@ NimBLERemoteService* NimBLEClient::getService(const char* uuid) { * @return A pointer to the service or nullptr if not found. */ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) { - NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str()); + const std::string uuidStr = uuid.toString(); + NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuidStr.c_str()); for (auto& it : m_svcVec) { if (it->getUUID() == uuid) { - NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str()); + NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuidStr.c_str()); return it; } } size_t prevSize = m_svcVec.size(); + auto getLastIfAdded = [this, prevSize]() -> NimBLERemoteService* { return m_svcVec.size() > prevSize ? m_svcVec.back() : nullptr; }; if (retrieveServices(&uuid)) { - if (m_svcVec.size() > prevSize) { - return m_svcVec.back(); + if (NimBLERemoteService* svc = getLastIfAdded()) { + return svc; } // If the request was successful but 16/32 bit uuid not found @@ -657,8 +659,8 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) { NimBLEUUID uuid128(uuid); uuid128.to128(); if (retrieveServices(&uuid128)) { - if (m_svcVec.size() > prevSize) { - return m_svcVec.back(); + if (NimBLERemoteService* svc = getLastIfAdded()) { + return svc; } } } else { @@ -669,8 +671,8 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) { // if the uuid was 128 bit but not of the BLE base type this check will fail if (uuid16.bitSize() == BLE_UUID_TYPE_16) { if (retrieveServices(&uuid16)) { - if (m_svcVec.size() > prevSize) { - return m_svcVec.back(); + if (NimBLERemoteService* svc = getLastIfAdded()) { + return svc; } } } diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 41da8912..ee7aacc8 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -718,7 +718,7 @@ bool NimBLEDevice::onWhiteList(const NimBLEAddress& address) { bool NimBLEDevice::whiteListAdd(const NimBLEAddress& address) { if (!NimBLEDevice::onWhiteList(address)) { m_whiteList.push_back(address); - int rc = ble_gap_wl_set(reinterpret_cast(&m_whiteList[0]), m_whiteList.size()); + int rc = ble_gap_wl_set(reinterpret_cast(m_whiteList.data()), m_whiteList.size()); if (rc != 0) { NIMBLE_LOGE(LOG_TAG, "Failed adding to whitelist rc=%d", rc); m_whiteList.pop_back(); @@ -738,14 +738,16 @@ bool NimBLEDevice::whiteListRemove(const NimBLEAddress& address) { for (auto it = m_whiteList.begin(); it < m_whiteList.end(); ++it) { if (*it == address) { m_whiteList.erase(it); - int rc = ble_gap_wl_set(reinterpret_cast(&m_whiteList[0]), m_whiteList.size()); + auto* list = m_whiteList.empty() ? nullptr : reinterpret_cast(m_whiteList.data()); + int rc = ble_gap_wl_set(list, m_whiteList.size()); if (rc != 0) { m_whiteList.push_back(address); NIMBLE_LOGE(LOG_TAG, "Failed removing from whitelist rc=%d", rc); return false; } - std::vector(m_whiteList).swap(m_whiteList); + m_whiteList.shrink_to_fit(); + break; } } @@ -766,7 +768,7 @@ size_t NimBLEDevice::getWhiteListCount() { * @returns The NimBLEAddress at the whitelist index or null address if not found. */ NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) { - if (index > m_whiteList.size()) { + if (index >= m_whiteList.size()) { // HA HA AI FOUND AN ERROR! NIMBLE_LOGE(LOG_TAG, "Invalid index; %u", index); return NimBLEAddress{}; }