Skip to content

Commit 2c97bb8

Browse files
committed
Created NimBLERemoteGattUtils and moved getAttr inside
1 parent 310a60a commit 2c97bb8

8 files changed

+115
-51
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ idf_component_register(
5757
"src/NimBLEHIDDevice.cpp"
5858
"src/NimBLERemoteCharacteristic.cpp"
5959
"src/NimBLERemoteDescriptor.cpp"
60+
"src/NimBLERemoteGattUtils.cpp"
6061
"src/NimBLERemoteService.cpp"
6162
"src/NimBLERemoteValueAttribute.cpp"
6263
"src/NimBLEScan.cpp"

src/NimBLEClient.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# include "NimBLEClient.h"
2222
# include "NimBLERemoteService.h"
2323
# include "NimBLERemoteCharacteristic.h"
24+
# include "NimBLERemoteGattUtils.h"
2425
# include "NimBLEDevice.h"
2526
# include "NimBLELog.h"
2627

@@ -632,7 +633,8 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID& uuid) {
632633
NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str());
633634
NimBLERemoteService *pSvc = nullptr;
634635

635-
NimBLEUtils::getAttr<NimBLERemoteService>(uuid, &pSvc, m_svcVec, [this](const NimBLEUUID* u, NimBLERemoteService** svc) {
636+
NimBLERemoteGattUtils::getAttr<NimBLERemoteService>(uuid, &pSvc, m_svcVec,
637+
[this](const NimBLEUUID* u, NimBLERemoteService** svc) {
636638
return retrieveServices(u, svc);
637639
});
638640

src/NimBLERemoteCharacteristic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
# include "NimBLERemoteCharacteristic.h"
2222
# include "NimBLERemoteDescriptor.h"
23+
# include "NimBLERemoteGattUtils.h"
2324
# include "NimBLERemoteService.h"
2425
# include "NimBLEClient.h"
2526
# include "NimBLEUtils.h"
@@ -140,7 +141,8 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
140141
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
141142
NimBLERemoteDescriptor* pDsc = nullptr;
142143

143-
NimBLEUtils::getAttr<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors, [this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) {
144+
NimBLERemoteGattUtils::getAttr<NimBLERemoteDescriptor>(uuid, &pDsc, m_vDescriptors,
145+
[this](const NimBLEUUID* u, NimBLERemoteDescriptor** dsc) {
144146
return retrieveDescriptors(u, dsc);
145147
});
146148

src/NimBLERemoteGattUtils.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020-2024 Ryan Powell <[email protected]> and
3+
* esp-nimble-cpp, NimBLE-Arduino contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "nimconfig.h"
19+
#if defined(CONFIG_BT_ENABLED)
20+
21+
# include "NimBLERemoteGattUtils.h"
22+
# include "NimBLEAddress.h"
23+
# include "NimBLERemoteService.h"
24+
# include "NimBLERemoteDescriptor.h"
25+
# include "NimBLERemoteCharacteristic.h"
26+
27+
/**
28+
* @brief Get an attribute matching a uuid.
29+
* @param [in] uuid Search for this uuid.
30+
* @param [in] attr Pointer to hold result.
31+
* @param [in] vec Vector to search through before trying to get attribute.
32+
* @param [in] getter Attribute getter function to call.
33+
*/
34+
template <typename T>
35+
void NimBLERemoteGattUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& getter) {
36+
// Check if already exists.
37+
for (const auto& v : vec) {
38+
if (v->getUUID() == uuid) {
39+
*attr = v;
40+
return;
41+
}
42+
}
43+
// Exit if request failed or uuid was found.
44+
if (!getter(&uuid, attr) || *attr) {
45+
return;
46+
}
47+
// Try again with 128 bit uuid if request succeeded with no uuid found.
48+
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
49+
NimBLEUUID uuid128 = NimBLEUUID(uuid).to128();
50+
getter(&uuid128, attr);
51+
return;
52+
}
53+
// Try again with 16 bit uuid if request succeeded with no uuid found.
54+
// If the uuid was 128 bit but not of the BLE base type this check will fail.
55+
NimBLEUUID uuid16 = NimBLEUUID(uuid).to16();
56+
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
57+
getter(&uuid16, attr);
58+
}
59+
}
60+
61+
using svc = NimBLERemoteService; using chr = NimBLERemoteCharacteristic; using dsc = NimBLERemoteDescriptor;
62+
template void NimBLERemoteGattUtils::getAttr<svc>(const NimBLEUUID&, svc**, const std::vector<svc*>&, const std::function<bool(const NimBLEUUID*, svc**)>&);
63+
template void NimBLERemoteGattUtils::getAttr<chr>(const NimBLEUUID&, chr**, const std::vector<chr*>&, const std::function<bool(const NimBLEUUID*, chr**)>&);
64+
template void NimBLERemoteGattUtils::getAttr<dsc>(const NimBLEUUID&, dsc**, const std::vector<dsc*>&, const std::function<bool(const NimBLEUUID*, dsc**)>&);
65+
66+
#endif // CONFIG_BT_ENABLED

src/NimBLERemoteGattUtils.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2020-2024 Ryan Powell <[email protected]> and
3+
* esp-nimble-cpp, NimBLE-Arduino contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef NIMBLE_CPP_GATT_UTILS_H_
19+
#define NIMBLE_CPP_GATT_UTILS_H_
20+
21+
#include "nimconfig.h"
22+
#if defined(CONFIG_BT_ENABLED)
23+
# include "NimBLEUUID.h"
24+
# include <functional>
25+
# include <vector>
26+
# include <string>
27+
28+
/**
29+
* @brief A BLE Remote GATT Utility class for getting an attribute.
30+
*/
31+
class NimBLERemoteGattUtils {
32+
public:
33+
template <typename T>
34+
static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec,
35+
const std::function<bool(const NimBLEUUID*, T**)>& getter);
36+
}; // NimBLERemoteGattUtils
37+
38+
#endif /* CONFIG_BT_ENABLED */
39+
#endif /* NIMBLE_CPP_GATT_UTILS_H_ */

src/NimBLERemoteService.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
# include "NimBLERemoteService.h"
2222
# include "NimBLERemoteCharacteristic.h"
23+
# include "NimBLERemoteGattUtils.h"
2324
# include "NimBLEClient.h"
2425
# include "NimBLEAttValue.h"
2526
# include "NimBLEUtils.h"
@@ -78,7 +79,8 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU
7879
NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str());
7980
NimBLERemoteCharacteristic* pChar = nullptr;
8081

81-
NimBLEUtils::getAttr<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars, [this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) {
82+
NimBLERemoteGattUtils::getAttr<NimBLERemoteCharacteristic>(uuid, &pChar, m_vChars,
83+
[this](const NimBLEUUID* u, NimBLERemoteCharacteristic** chr) {
8284
return retrieveCharacteristics(u, chr);
8385
});
8486

src/NimBLEUtils.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
# include "NimBLEUtils.h"
2222
# include "NimBLEAddress.h"
23-
# include "NimBLERemoteService.h"
24-
# include "NimBLERemoteDescriptor.h"
25-
# include "NimBLERemoteCharacteristic.h"
2623
# include "NimBLELog.h"
2724

2825
# if defined(CONFIG_NIMBLE_CPP_IDF)
@@ -578,43 +575,4 @@ NimBLEAddress NimBLEUtils::generateAddr(bool nrpa) {
578575
return NimBLEAddress{addr};
579576
} // generateAddr
580577

581-
/**
582-
* @brief Get an attribute matching a uuid.
583-
* @param [in] uuid Search for this uuid.
584-
* @param [in] attr Pointer to hold result.
585-
* @param [in] vec Vector to search through before trying to get attribute.
586-
* @param [in] getter Attribute getter function to call.
587-
*/
588-
template <typename T>
589-
void NimBLEUtils::getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec, const std::function<bool(const NimBLEUUID*, T**)>& getter) {
590-
// Check if already exists.
591-
for (const auto& v : vec) {
592-
if (v->getUUID() == uuid) {
593-
*attr = v;
594-
return;
595-
}
596-
}
597-
// Exit if request failed or uuid was found.
598-
if (!getter(&uuid, attr) || *attr) {
599-
return;
600-
}
601-
// Try again with 128 bit uuid if request succeeded with no uuid found.
602-
if (uuid.bitSize() == BLE_UUID_TYPE_16 || uuid.bitSize() == BLE_UUID_TYPE_32) {
603-
NimBLEUUID uuid128 = NimBLEUUID(uuid).to128();
604-
getter(&uuid128, attr);
605-
return;
606-
}
607-
// Try again with 16 bit uuid if request succeeded with no uuid found.
608-
// If the uuid was 128 bit but not of the BLE base type this check will fail.
609-
NimBLEUUID uuid16 = NimBLEUUID(uuid).to16();
610-
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
611-
getter(&uuid16, attr);
612-
}
613-
}
614-
615-
using svc = NimBLERemoteService; using chr = NimBLERemoteCharacteristic; using dsc = NimBLERemoteDescriptor;
616-
template void NimBLEUtils::getAttr<svc>(const NimBLEUUID&, svc**, const std::vector<svc*>&, const std::function<bool(const NimBLEUUID*, svc**)>&);
617-
template void NimBLEUtils::getAttr<chr>(const NimBLEUUID&, chr**, const std::vector<chr*>&, const std::function<bool(const NimBLEUUID*, chr**)>&);
618-
template void NimBLEUtils::getAttr<dsc>(const NimBLEUUID&, dsc**, const std::vector<dsc*>&, const std::function<bool(const NimBLEUUID*, dsc**)>&);
619-
620578
#endif // CONFIG_BT_ENABLED

src/NimBLEUtils.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
#include "nimconfig.h"
2222
#if defined(CONFIG_BT_ENABLED)
23-
# include "NimBLEUUID.h"
24-
# include <functional>
25-
# include <vector>
2623
# include <string>
2724

2825
class NimBLEAddress;
@@ -56,9 +53,6 @@ class NimBLEUtils {
5653
static NimBLEAddress generateAddr(bool nrpa);
5754
static bool taskWait(const NimBLETaskData& taskData, uint32_t timeout);
5855
static void taskRelease(const NimBLETaskData& taskData, int rc = 0);
59-
template <typename T>
60-
static void getAttr(const NimBLEUUID& uuid, T** attr, const std::vector<T*>& vec,
61-
const std::function<bool(const NimBLEUUID*, T**)>& getter);
6256
};
6357

6458
#endif // CONFIG_BT_ENABLED

0 commit comments

Comments
 (0)