Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs/doxydocs
.development
_codeql_detected_source_root
23 changes: 7 additions & 16 deletions src/NimBLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this one

addDescriptor(pDescriptor);
return pDescriptor;
} // createDescriptor
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

} // setCallbacks

/**
Expand Down
18 changes: 10 additions & 8 deletions src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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;
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ble_addr_t*>(&m_whiteList[0]), m_whiteList.size());
int rc = ble_gap_wl_set(reinterpret_cast<ble_addr_t*>(m_whiteList.data()), m_whiteList.size());
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Failed adding to whitelist rc=%d", rc);
m_whiteList.pop_back();
Expand All @@ -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<ble_addr_t*>(&m_whiteList[0]), m_whiteList.size());
auto* list = m_whiteList.empty() ? nullptr : reinterpret_cast<ble_addr_t*>(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<NimBLEAddress>(m_whiteList).swap(m_whiteList);
m_whiteList.shrink_to_fit();
break;
}
}

Expand All @@ -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{};
}
Expand Down
Loading