Skip to content

Commit f8d3561

Browse files
authored
Merge branch 'master' into idf-release/v3.3
2 parents 38b6245 + 9be784f commit f8d3561

File tree

5 files changed

+69
-27
lines changed

5 files changed

+69
-27
lines changed

libraries/AsyncUDP/src/AsyncUDP.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ void AsyncUDPMessage::flush()
277277
_index = 0;
278278
}
279279

280+
AsyncUDPPacket::AsyncUDPPacket(AsyncUDPPacket &packet){
281+
_udp = packet._udp;
282+
_pb = packet._pb;
283+
_if = packet._if;
284+
_data = packet._data;
285+
_len = packet._len;
286+
_index = 0;
287+
288+
pbuf_ref(_pb);
289+
}
280290

281291
AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr, uint16_t rport, struct netif * ntif)
282292
{
@@ -683,9 +693,8 @@ void AsyncUDP::_recv(udp_pcb *upcb, pbuf *pb, const ip_addr_t *addr, uint16_t po
683693
if(_handler) {
684694
AsyncUDPPacket packet(this, this_pb, addr, port, netif);
685695
_handler(packet);
686-
} else {
687-
pbuf_free(this_pb);
688696
}
697+
pbuf_free(this_pb);
689698
}
690699
}
691700

libraries/AsyncUDP/src/AsyncUDP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class AsyncUDPPacket : public Stream
5858
size_t _len;
5959
size_t _index;
6060
public:
61+
AsyncUDPPacket(AsyncUDPPacket &packet);
6162
AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif);
6263
virtual ~AsyncUDPPacket();
6364

libraries/BLE/src/BLEAdvertisedDevice.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
2525
m_manufacturerData = "";
2626
m_name = "";
2727
m_rssi = -9999;
28+
m_serviceUUIDs = {};
2829
m_serviceData = {};
2930
m_serviceDataUUIDs = {};
3031
m_txPower = 0;
@@ -34,8 +35,6 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
3435
m_haveManufacturerData = false;
3536
m_haveName = false;
3637
m_haveRSSI = false;
37-
m_haveServiceData = false;
38-
m_haveServiceUUID = false;
3938
m_haveTXPower = false;
4039

4140
} // BLEAdvertisedDevice
@@ -107,19 +106,15 @@ BLEScan* BLEAdvertisedDevice::getScan() {
107106
* @return Number of service data discovered.
108107
*/
109108
int BLEAdvertisedDevice::getServiceDataCount() {
110-
if (m_haveServiceData)
111-
return m_serviceData.size();
112-
else
113-
return 0;
114-
109+
return m_serviceData.size();
115110
} //getServiceDataCount
116111

117112
/**
118113
* @brief Get the service data.
119114
* @return The ServiceData of the advertised device.
120115
*/
121116
std::string BLEAdvertisedDevice::getServiceData() {
122-
return m_serviceData[0];
117+
return m_serviceData.empty() ? std::string() : m_serviceData.front();
123118
} //getServiceData
124119

125120
/**
@@ -130,12 +125,20 @@ std::string BLEAdvertisedDevice::getServiceData(int i) {
130125
return m_serviceData[i];
131126
} //getServiceData
132127

128+
/**
129+
* @brief Get the number of service data UUIDs.
130+
* @return Number of service data UUIDs discovered.
131+
*/
132+
int BLEAdvertisedDevice::getServiceDataUUIDCount() {
133+
return m_serviceDataUUIDs.size();
134+
} //getServiceDataUUIDCount
135+
133136
/**
134137
* @brief Get the service data UUID.
135138
* @return The service data UUID.
136139
*/
137140
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
138-
return m_serviceDataUUIDs[0];
141+
return m_serviceDataUUIDs.empty() ? BLEUUID() : m_serviceDataUUIDs.front();
139142
} // getServiceDataUUID
140143

141144
/**
@@ -146,12 +149,20 @@ BLEUUID BLEAdvertisedDevice::getServiceDataUUID(int i) {
146149
return m_serviceDataUUIDs[i];
147150
} // getServiceDataUUID
148151

152+
/**
153+
* @brief Get the number of service UUIDs.
154+
* @return Number of service UUIDs discovered.
155+
*/
156+
int BLEAdvertisedDevice::getServiceUUIDCount() {
157+
return m_serviceUUIDs.size();
158+
} //getServiceUUIDCount
159+
149160
/**
150161
* @brief Get the Service UUID.
151162
* @return The Service UUID of the advertised device.
152163
*/
153164
BLEUUID BLEAdvertisedDevice::getServiceUUID() {
154-
return m_serviceUUIDs[0];
165+
return m_serviceUUIDs.empty() ? BLEUUID() : m_serviceUUIDs.front();
155166
} // getServiceUUID
156167

157168
/**
@@ -167,7 +178,7 @@ BLEUUID BLEAdvertisedDevice::getServiceUUID(int i) {
167178
* @return Return true if service is advertised
168179
*/
169180
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
170-
for (int i = 0; i < m_serviceUUIDs.size(); i++) {
181+
for (int i = 0; i < getServiceUUIDCount(); i++) {
171182
if (m_serviceUUIDs[i].equals(uuid)) return true;
172183
}
173184
return false;
@@ -224,7 +235,7 @@ bool BLEAdvertisedDevice::haveRSSI() {
224235
* @return True if there is a service data value present.
225236
*/
226237
bool BLEAdvertisedDevice::haveServiceData() {
227-
return m_haveServiceData;
238+
return !m_serviceData.empty();
228239
} // haveServiceData
229240

230241

@@ -233,7 +244,7 @@ bool BLEAdvertisedDevice::haveServiceData() {
233244
* @return True if there is a service UUID value present.
234245
*/
235246
bool BLEAdvertisedDevice::haveServiceUUID() {
236-
return m_haveServiceUUID;
247+
return !m_serviceUUIDs.empty();
237248
} // haveServiceUUID
238249

239250

@@ -486,7 +497,6 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
486497
*/
487498
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
488499
m_serviceUUIDs.push_back(serviceUUID);
489-
m_haveServiceUUID = true;
490500
log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
491501
} // setServiceUUID
492502

@@ -496,7 +506,6 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
496506
* @param [in] data ServiceData value.
497507
*/
498508
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
499-
m_haveServiceData = true; // Set the flag that indicates we have service data.
500509
m_serviceData.push_back(serviceData); // Save the service data that we received.
501510
} //setServiceData
502511

@@ -506,7 +515,6 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
506515
* @param [in] data ServiceDataUUID value.
507516
*/
508517
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
509-
m_haveServiceData = true; // Set the flag that indicates we have service data.
510518
m_serviceDataUUIDs.push_back(uuid);
511519
log_d("- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str());
512520
} // setServiceDataUUID
@@ -542,7 +550,7 @@ std::string BLEAdvertisedDevice::toString() {
542550
free(pHex);
543551
}
544552
if (haveServiceUUID()) {
545-
for (int i=0; i < m_serviceUUIDs.size(); i++) {
553+
for (int i=0; i < getServiceUUIDCount(); i++) {
546554
res += ", serviceUUID: " + getServiceUUID(i).toString();
547555
}
548556
}

libraries/BLE/src/BLEAdvertisedDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class BLEAdvertisedDevice {
4242
BLEUUID getServiceUUID();
4343
BLEUUID getServiceUUID(int i);
4444
int getServiceDataCount();
45+
int getServiceDataUUIDCount();
46+
int getServiceUUIDCount();
4547
int8_t getTXPower();
4648
uint8_t* getPayload();
4749
size_t getPayloadLength();
@@ -83,8 +85,6 @@ class BLEAdvertisedDevice {
8385
bool m_haveManufacturerData;
8486
bool m_haveName;
8587
bool m_haveRSSI;
86-
bool m_haveServiceData;
87-
bool m_haveServiceUUID;
8888
bool m_haveTXPower;
8989

9090

libraries/BLE/src/BLEClient.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,17 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
110110
return false;
111111
}
112112

113-
m_semaphoreRegEvt.wait("connect");
113+
uint32_t rc = m_semaphoreRegEvt.wait("connect");
114+
115+
if (rc != ESP_GATT_OK) {
116+
// fixes ESP_GATT_NO_RESOURCES error mostly
117+
log_e("esp_ble_gattc_app_register_error: rc=%d", rc);
118+
BLEDevice::removePeerDevice(m_appId, true);
119+
// not sure if this is needed here
120+
// esp_ble_gattc_app_unregister(m_gattc_if);
121+
// m_gattc_if = ESP_GATT_IF_NONE;
122+
return false;
123+
}
114124

115125
m_peerAddress = address;
116126

@@ -128,7 +138,13 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
128138
return false;
129139
}
130140

131-
uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
141+
rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
142+
// check the status of the connection and cleanup in case of failure
143+
if (rc != ESP_GATT_OK) {
144+
BLEDevice::removePeerDevice(m_appId, true);
145+
esp_ble_gattc_app_unregister(m_gattc_if);
146+
m_gattc_if = ESP_GATT_IF_NONE;
147+
}
132148
log_v("<< connect(), rc=%d", rc==ESP_GATT_OK);
133149
return rc == ESP_GATT_OK;
134150
} // connect
@@ -160,6 +176,11 @@ void BLEClient::gattClientEventHandler(
160176
log_d("gattClientEventHandler [esp_gatt_if: %d] ... %s",
161177
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
162178

179+
// it is possible to receive events from other connections while waiting for registration
180+
if (m_gattc_if == ESP_GATT_IF_NONE && event != ESP_GATTC_REG_EVT) {
181+
return;
182+
}
183+
163184
// Execute handler code based on the type of event received.
164185
switch(event) {
165186

@@ -184,15 +205,17 @@ void BLEClient::gattClientEventHandler(
184205
if (evtParam->disconnect.conn_id != getConnId()) break;
185206
// If we receive a disconnect event, set the class flag that indicates that we are
186207
// no longer connected.
187-
if (m_isConnected && m_pClientCallbacks != nullptr) {
188-
m_pClientCallbacks->onDisconnect(this);
189-
}
208+
bool m_wasConnected = m_isConnected;
190209
m_isConnected = false;
191210
esp_ble_gattc_app_unregister(m_gattc_if);
211+
m_gattc_if = ESP_GATT_IF_NONE;
192212
m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE);
193213
m_semaphoreRssiCmplEvt.give();
194214
m_semaphoreSearchCmplEvt.give(1);
195215
BLEDevice::removePeerDevice(m_appId, true);
216+
if (m_wasConnected && m_pClientCallbacks != nullptr) {
217+
m_pClientCallbacks->onDisconnect(this);
218+
}
196219
break;
197220
} // ESP_GATTC_DISCONNECT_EVT
198221

@@ -228,7 +251,8 @@ void BLEClient::gattClientEventHandler(
228251
//
229252
case ESP_GATTC_REG_EVT: {
230253
m_gattc_if = gattc_if;
231-
m_semaphoreRegEvt.give();
254+
// pass on the registration status result, in case of failure
255+
m_semaphoreRegEvt.give(evtParam->reg.status);
232256
break;
233257
} // ESP_GATTC_REG_EVT
234258

0 commit comments

Comments
 (0)