Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9946a98

Browse files
committed
refactor(ble): minor BLE library cleanup
- made many getters `const` (as it supposed to be) - pass string by reference in modified classes - other random code cleanup / fixes
1 parent 0dbaa3f commit 9946a98

15 files changed

+73
-84
lines changed

‎libraries/BLE/src/BLE2902.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ This class will be removed in a future version.")]] BLE2902 : public BLEDescript
5656
bool getIndications();
5757
void setNotifications(bool flag);
5858
void setIndications(bool flag);
59-
60-
private:
61-
friend class BLECharacteristic;
6259
}; // BLE2902
6360

6461
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */

‎libraries/BLE/src/BLE2904.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ class BLE2904 : public BLEDescriptor {
9595
void setUnit(uint16_t unit);
9696

9797
private:
98-
friend class BLECharacteristic;
99-
10098
/***************************************************************************
10199
* Common private properties *
102100
***************************************************************************/

‎libraries/BLE/src/BLEAddress.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ BLEAddress::BLEAddress() {
5959
* @param [in] otherAddress The other address to compare against.
6060
* @return True if the addresses are equal.
6161
*/
62-
bool BLEAddress::equals(BLEAddress otherAddress) {
63-
#if defined(CONFIG_NIMBLE_ENABLED)
64-
if (m_addrType != otherAddress.m_addrType) {
65-
return false;
66-
}
67-
#endif
68-
return memcmp(otherAddress.getNative(), m_address, ESP_BD_ADDR_LEN) == 0;
62+
bool BLEAddress::equals(const BLEAddress &otherAddress) const {
63+
return *this == otherAddress;
6964
}
7065

7166
bool BLEAddress::operator==(const BLEAddress &otherAddress) const {
@@ -116,9 +111,9 @@ uint8_t *BLEAddress::getNative() {
116111
*
117112
* @return The string representation of the address.
118113
*/
119-
String BLEAddress::toString() {
120-
auto size = 18;
121-
char *res = (char *)malloc(size);
114+
String BLEAddress::toString() const{
115+
constexprsize_t size = 18;
116+
char res[size];
122117

123118
#if defined(CONFIG_BLUEDROID_ENABLED)
124119
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
@@ -129,7 +124,6 @@ String BLEAddress::toString() {
129124
#endif
130125

131126
String ret(res);
132-
free(res);
133127
return ret;
134128
}
135129

@@ -158,7 +152,7 @@ BLEAddress::BLEAddress(esp_bd_addr_t address) {
158152
*
159153
* @param [in] stringAddress The hex representation of the address.
160154
*/
161-
BLEAddress::BLEAddress(String stringAddress) {
155+
BLEAddress::BLEAddress(constString &stringAddress) {
162156
if (stringAddress.length() != 17) {
163157
return;
164158
}
@@ -193,7 +187,7 @@ BLEAddress::BLEAddress(ble_addr_t address) {
193187
m_addrType = address.type;
194188
}
195189

196-
uint8_t BLEAddress::getType() {
190+
uint8_t BLEAddress::getType() const{
197191
return m_addrType;
198192
}
199193

@@ -209,7 +203,7 @@ uint8_t BLEAddress::getType() {
209203
* @param [in] stringAddress The hex representation of the address.
210204
* @param [in] type The address type.
211205
*/
212-
BLEAddress::BLEAddress(String stringAddress, uint8_t type) {
206+
BLEAddress::BLEAddress(constString &stringAddress, uint8_t type) {
213207
if (stringAddress.length() != 17) {
214208
return;
215209
}

‎libraries/BLE/src/BLEAddress.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ class BLEAddress {
6262
***************************************************************************/
6363

6464
BLEAddress();
65-
bool equals(BLEAddress otherAddress);
65+
bool equals(constBLEAddress &otherAddress)const;
6666
bool operator==(const BLEAddress &otherAddress) const;
6767
bool operator!=(const BLEAddress &otherAddress) const;
6868
bool operator<(const BLEAddress &otherAddress) const;
6969
bool operator<=(const BLEAddress &otherAddress) const;
7070
bool operator>(const BLEAddress &otherAddress) const;
7171
bool operator>=(const BLEAddress &otherAddress) const;
7272
uint8_t *getNative();
73-
String toString();
73+
String toString()const;
7474

7575
/***************************************************************************
7676
* Bluedroid public declarations *
7777
***************************************************************************/
7878

7979
#if defined(CONFIG_BLUEDROID_ENABLED)
8080
BLEAddress(esp_bd_addr_t address);
81-
BLEAddress(String stringAddress);
81+
BLEAddress(constString &stringAddress);
8282
#endif
8383

8484
/***************************************************************************
@@ -87,9 +87,9 @@ class BLEAddress {
8787

8888
#if defined(CONFIG_NIMBLE_ENABLED)
8989
BLEAddress(ble_addr_t address);
90-
BLEAddress(String stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
90+
BLEAddress(constString &stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
9191
BLEAddress(uint8_t address[ESP_BD_ADDR_LEN], uint8_t type = BLE_ADDR_PUBLIC);
92-
uint8_t getType();
92+
uint8_t getType()const;
9393
#endif
9494

9595
private:

‎libraries/BLE/src/BLEBeacon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int8_t BLEBeacon::getSignalPower() {
6262
return m_beaconData.signalPower;
6363
}
6464

65-
void BLEBeacon::setData(String data) {
65+
void BLEBeacon::setData(constString &data) {
6666
if (data.length() != sizeof(m_beaconData)) {
6767
log_e("Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_beaconData));
6868
return;

‎libraries/BLE/src/BLEBeacon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class BLEBeacon {
5555
uint16_t getManufacturerId();
5656
BLEUUID getProximityUUID();
5757
int8_t getSignalPower();
58-
void setData(String data);
58+
void setData(constString &data);
5959
void setMajor(uint16_t major);
6060
void setMinor(uint16_t minor);
6161
void setManufacturerId(uint16_t manufacturerId);

‎libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void BLECharacteristic::addDescriptor(BLEDescriptor *pDescriptor) {
124124
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
125125
* @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned.
126126
*/
127-
BLEDescriptor *BLECharacteristic::getDescriptorByUUID(const char *descriptorUUID) {
127+
BLEDescriptor *BLECharacteristic::getDescriptorByUUID(const char *descriptorUUID) const{
128128
return m_descriptorMap.getByUUID(BLEUUID(descriptorUUID));
129129
} // getDescriptorByUUID
130130

@@ -133,15 +133,15 @@ BLEDescriptor *BLECharacteristic::getDescriptorByUUID(const char *descriptorUUID
133133
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
134134
* @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned.
135135
*/
136-
BLEDescriptor *BLECharacteristic::getDescriptorByUUID(BLEUUID descriptorUUID) {
136+
BLEDescriptor *BLECharacteristic::getDescriptorByUUID(BLEUUID descriptorUUID) const{
137137
return m_descriptorMap.getByUUID(descriptorUUID);
138138
} // getDescriptorByUUID
139139

140140
/**
141141
* @brief Get the handle of the characteristic.
142142
* @return The handle of the characteristic.
143143
*/
144-
uint16_t BLECharacteristic::getHandle() {
144+
uint16_t BLECharacteristic::getHandle() const{
145145
return m_handle;
146146
} // getHandle
147147

@@ -151,30 +151,30 @@ void BLECharacteristic::setAccessPermissions(uint16_t perm) {
151151
#endif
152152
}
153153

154-
esp_gatt_char_prop_t BLECharacteristic::getProperties() {
154+
esp_gatt_char_prop_t BLECharacteristic::getProperties() const{
155155
return m_properties;
156156
} // getProperties
157157

158158
/**
159159
* @brief Get the service associated with this characteristic.
160160
*/
161-
BLEService *BLECharacteristic::getService() {
161+
BLEService *BLECharacteristic::getService() const{
162162
return m_pService;
163163
} // getService
164164

165165
/**
166166
* @brief Get the UUID of the characteristic.
167167
* @return The UUID of the characteristic.
168168
*/
169-
BLEUUID BLECharacteristic::getUUID() {
169+
BLEUUID BLECharacteristic::getUUID() const{
170170
return m_bleUUID;
171171
} // getUUID
172172

173173
/**
174174
* @brief Retrieve the current value of the characteristic.
175175
* @return A pointer to storage containing the current characteristic value.
176176
*/
177-
String BLECharacteristic::getValue() {
177+
String BLECharacteristic::getValue() const{
178178
return m_value.getValue();
179179
} // getValue
180180

@@ -190,7 +190,7 @@ uint8_t *BLECharacteristic::getData() {
190190
* @brief Retrieve the current length of the data of the characteristic.
191191
* @return Amount of databytes of the characteristic.
192192
*/
193-
size_t BLECharacteristic::getLength() {
193+
size_t BLECharacteristic::getLength() const{
194194
return m_value.getLength();
195195
} // getLength
196196

@@ -425,7 +425,7 @@ void BLECharacteristic::setWriteProperty(bool value) {
425425
* @brief Return a string representation of the characteristic.
426426
* @return A string representation of the characteristic.
427427
*/
428-
String BLECharacteristic::toString() {
428+
String BLECharacteristic::toString() const{
429429
String res = "UUID: " + m_bleUUID.toString() + ", handle : 0x";
430430
char hex[5];
431431
snprintf(hex, sizeof(hex), "%04x", m_handle);
@@ -452,7 +452,7 @@ String BLECharacteristic::toString() {
452452
return res;
453453
} // toString
454454

455-
BLECharacteristicCallbacks::~BLECharacteristicCallbacks() {}
455+
BLECharacteristicCallbacks::~BLECharacteristicCallbacks() = default;
456456

457457
// Common callbacks
458458
void BLECharacteristicCallbacks::onRead(BLECharacteristic *pCharacteristic) {

‎libraries/BLE/src/BLECharacteristic.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ class BLEDescriptorMap {
8585
void setByUUID(const char *uuid, BLEDescriptor *pDescriptor);
8686
void setByUUID(BLEUUID uuid, BLEDescriptor *pDescriptor);
8787
void setByHandle(uint16_t handle, BLEDescriptor *pDescriptor);
88-
BLEDescriptor *getByUUID(const char *uuid);
89-
BLEDescriptor *getByUUID(BLEUUID uuid);
90-
BLEDescriptor *getByHandle(uint16_t handle);
91-
String toString();
88+
BLEDescriptor *getByUUID(const char *uuid)const;
89+
BLEDescriptor *getByUUID(BLEUUID uuid)const;
90+
BLEDescriptor *getByHandle(uint16_t handle)const;
91+
String toString()const;
9292
BLEDescriptor *getFirst();
9393
BLEDescriptor *getNext();
94-
int getRegisteredDescriptorCount();
94+
int getRegisteredDescriptorCount()const;
9595
void removeDescriptor(BLEDescriptor *pDescriptor);
9696

9797
/***************************************************************************
@@ -181,12 +181,12 @@ class BLECharacteristic {
181181
virtual ~BLECharacteristic();
182182

183183
void addDescriptor(BLEDescriptor *pDescriptor);
184-
BLEDescriptor *getDescriptorByUUID(const char *descriptorUUID);
185-
BLEDescriptor *getDescriptorByUUID(BLEUUID descriptorUUID);
186-
BLEUUID getUUID();
187-
String getValue();
184+
BLEDescriptor *getDescriptorByUUID(const char *descriptorUUID)const;
185+
BLEDescriptor *getDescriptorByUUID(BLEUUID descriptorUUID)const;
186+
BLEUUID getUUID()const;
187+
String getValue()const;
188188
uint8_t *getData();
189-
size_t getLength();
189+
size_t getLength()const;
190190
void indicate();
191191
void notify(bool is_notification = true);
192192
void setCallbacks(BLECharacteristicCallbacks *pCallbacks);
@@ -197,10 +197,10 @@ class BLECharacteristic {
197197
void setValue(int data32);
198198
void setValue(float data32);
199199
void setValue(double data64);
200-
String toString();
201-
uint16_t getHandle();
200+
String toString()const;
201+
uint16_t getHandle()const;
202202
void setAccessPermissions(uint16_t perm);
203-
esp_gatt_char_prop_t getProperties();
203+
esp_gatt_char_prop_t getProperties()const;
204204
void setReadProperty(bool value);
205205
void setWriteProperty(bool value);
206206
void setNotifyProperty(bool value);
@@ -253,7 +253,7 @@ class BLECharacteristic {
253253
***************************************************************************/
254254

255255
void executeCreate(BLEService *pService);
256-
BLEService *getService();
256+
BLEService *getService()const;
257257
void setHandle(uint16_t handle);
258258

259259
/***************************************************************************

‎libraries/BLE/src/BLECharacteristicMap.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @param [in] handle The handle to look up the characteristic.
3737
* @return The characteristic.
3838
*/
39-
BLECharacteristic *BLECharacteristicMap::getByHandle(uint16_t handle) {
39+
BLECharacteristic *BLECharacteristicMap::getByHandle(uint16_t handle) const{
4040
return m_handleMap.at(handle);
4141
} // getByHandle
4242

@@ -45,7 +45,7 @@ BLECharacteristic *BLECharacteristicMap::getByHandle(uint16_t handle) {
4545
* @param [in] UUID The UUID to look up the characteristic.
4646
* @return The characteristic.
4747
*/
48-
BLECharacteristic *BLECharacteristicMap::getByUUID(const char *uuid) {
48+
BLECharacteristic *BLECharacteristicMap::getByUUID(const char *uuid) const{
4949
return getByUUID(BLEUUID(uuid));
5050
}
5151

@@ -54,7 +54,7 @@ BLECharacteristic *BLECharacteristicMap::getByUUID(const char *uuid) {
5454
* @param [in] UUID The UUID to look up the characteristic.
5555
* @return The characteristic.
5656
*/
57-
BLECharacteristic *BLECharacteristicMap::getByUUID(BLEUUID uuid) {
57+
BLECharacteristic *BLECharacteristicMap::getByUUID(BLEUUID uuid) const{
5858
for (auto &myPair : m_uuidMap) {
5959
if (myPair.first->getUUID().equals(uuid)) {
6060
return myPair.first;
@@ -95,7 +95,7 @@ BLECharacteristic *BLECharacteristicMap::getNext() {
9595
* @brief Get the number of registered characteristics.
9696
* @return The number of registered characteristics.
9797
*/
98-
int BLECharacteristicMap::getRegisteredCharacteristicCount() {
98+
int BLECharacteristicMap::getRegisteredCharacteristicCount() const{
9999
return m_uuidMap.size();
100100
} // getRegisteredCharacteristicCount
101101

@@ -133,7 +133,7 @@ void BLECharacteristicMap::setByUUID(BLECharacteristic *pCharacteristic, BLEUUID
133133
* @brief Return a string representation of the characteristic map.
134134
* @return A string representation of the characteristic map.
135135
*/
136-
String BLECharacteristicMap::toString() {
136+
String BLECharacteristicMap::toString() const{
137137
String res;
138138
int count = 0;
139139
char hex[5];

‎libraries/BLE/src/BLEDescriptor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,38 +109,38 @@ void BLEDescriptor::executeCreate(BLECharacteristic *pCharacteristic) {
109109
* @brief Get the BLE handle for this descriptor.
110110
* @return The handle for this descriptor.
111111
*/
112-
uint16_t BLEDescriptor::getHandle() {
112+
uint16_t BLEDescriptor::getHandle() const{
113113
return m_handle;
114114
} // getHandle
115115

116116
/**
117117
* @brief Get the length of the value of this descriptor.
118118
* @return The length (in bytes) of the value of this descriptor.
119119
*/
120-
size_t BLEDescriptor::getLength() {
120+
size_t BLEDescriptor::getLength() const{
121121
return m_value.attr_len;
122122
} // getLength
123123

124124
/**
125125
* @brief Get the UUID of the descriptor.
126126
*/
127-
BLEUUID BLEDescriptor::getUUID() {
127+
BLEUUID BLEDescriptor::getUUID() const{
128128
return m_bleUUID;
129129
} // getUUID
130130

131131
/**
132132
* @brief Get the value of this descriptor.
133133
* @return A pointer to the value of this descriptor.
134134
*/
135-
uint8_t *BLEDescriptor::getValue() {
135+
uint8_t *BLEDescriptor::getValue() const{
136136
return m_value.attr_value;
137137
} // getValue
138138

139139
/**
140140
* @brief Get the characteristic this descriptor belongs to.
141141
* @return A pointer to the characteristic this descriptor belongs to.
142142
*/
143-
BLECharacteristic *BLEDescriptor::getCharacteristic() {
143+
BLECharacteristic *BLEDescriptor::getCharacteristic() const{
144144
return m_pCharacteristic;
145145
} // getCharacteristic
146146

@@ -215,14 +215,14 @@ void BLEDescriptor::setAccessPermissions(uint16_t perm) {
215215
* @brief Return a string representation of the descriptor.
216216
* @return A string representation of the descriptor.
217217
*/
218-
String BLEDescriptor::toString() {
218+
String BLEDescriptor::toString() const{
219219
char hex[5];
220220
snprintf(hex, sizeof(hex), "%04x", m_handle);
221221
String res = "UUID: " + m_bleUUID.toString() + ", handle: 0x" + hex;
222222
return res;
223223
} // toString
224224

225-
BLEDescriptorCallbacks::~BLEDescriptorCallbacks() {}
225+
BLEDescriptorCallbacks::~BLEDescriptorCallbacks() = default;
226226

227227
/**
228228
* @brief Callback function to support a read request.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /