8000 Merge pull request #783 from wakwak-koba/master · hilam8899/esp32-snippets@69fb1c6 · GitHub
[go: up one dir, main page]

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

Appearance settings

Commit 69fb1c6

Browse files
authored
Merge pull request nkolban#783 from wakwak-koba/master
fixed bugs
2 parents 30ee858 + e63c941 commit 69fb1c6

File tree

5 files changed

+56
-47
lines changed

5 files changed

+56
-47
lines changed

cpp_utils/BLEClient.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,22 @@ void BLEClient::gattClientEventHandler(
182182
// - uint16_t conn_id
183183
// - esp_bd_addr_t remote_bda
184184
case ESP_GATTC_DISCONNECT_EVT: {
185-
// If we receive a disconnect event, set the class flag that indicates that we are
186-
// no longer connected.
187-
m_isConnected = false;
188-
if (m_pClientCallbacks != nullptr) {
189-
m_pClientCallbacks->onDisconnect(this);
190-
}
191-
BLEDevice::removePeerDevice(m_appId, true);
192-
esp_ble_gattc_app_unregister(m_gattc_if);
193-
m_semaphoreRssiCmplEvt.give();
194-
m_semaphoreSearchCmplEvt.give(1);
185+
ESP_LOGE(__func__, "disconnect event, conn_id: %d", evtParam->disconnect.conn_id);
186+
if(getConnId() != evtParam->disconnect.conn_id)
195187
break;
188+
m_semaphoreOpenEvt.give(evtParam->disconnect.reason);
189+
if(!m_isConnected)
190+
break;
191+
// If we receive a disconnect event, set the class flag that indicates that we are
192+
// no longer connected.
193+
esp_ble_gattc_close(m_gattc_if, m_conn_id);
194+
m_isConnected = false;
195+
if (m_pClientCallbacks != nullptr) {
196+
m_pClientCallbacks->onDisconnect(this);
197+
}
198+
break;
196199
} // ESP_GATTC_DISCONNECT_EVT
197-
200+
198201
//
199202
// ESP_GATTC_OPEN_EVT
200203
//
@@ -224,8 +227,12 @@ void BLEClient::gattClientEventHandler(
224227
// uint16_t app_id
225228
//
226229
case ESP_GATTC_REG_EVT: {
227-
m_gattc_if = gattc_if;
228-
m_semaphoreRegEvt.give();
230+
if(m_appId == evtParam->reg.app_id){
231+
ESP_LOGI(__func__, "register app id: %d, %d, gattc_if: %d", m_appId, evtParam->reg.app_id, gattc_if);
232+
m_gattc_if = gattc_if;
233+
m_appId = evtParam->reg.app_id;
234+
m_semaphoreRegEvt.give();
235+
}
229236
break;
230237
} // ESP_GATTC_REG_EVT
231238

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ static bool compareGattId(esp_gatt_id_t id1, esp_gatt_id_t id2) {
149149
* @returns N/A
150150
*/
151151
void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) {
152+
153+
ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
154+
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
155+
152156
switch(event) {
153157
// ESP_GATTC_NOTIFY_EVT
154158
//
@@ -262,7 +266,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
262266
uint16_t offset = 0;
263267
esp_gattc_descr_elem_t result;
264268
while(true) {
265-
uint16_t count = 10;
269+
uint16_t count = 1;
266270
esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr(
267271
getRemoteService()->getClient()->getGattcIf(),
268272
getRemoteService()->getClient()->getConnId(),
@@ -272,7 +276,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
272276
offset
273277
);
274278

275-
if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
279+
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
276280
break;
277281
}
278282

@@ -461,7 +465,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
461465
uint8_t val[] = {0x01, 0x00};
462466
if(!notifications) val[0] = 0x02;
463467
BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902));
464-
desc->writeValue(val, 2);
468+
if(desc != nullptr)
469+
desc->writeValue(val, 2);
465470
} // End Register
466471
else { // If we weren't passed a callback function, then this is an unregistration.
467472
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
@@ -476,7 +481,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
476481

477482
uint8_t val[] = {0x00, 0x00};
478483
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
479-
desc->writeValue(val, 2);
484+
if(desc != nullptr)
485+
desc->writeValue(val, 2);
480486
} // End Unregister
481487

482488
m_semaphoreRegForNotifyEvt.wait("registerForNotify");

cpp_utils/BLERemoteCharacteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class BLERemoteCharacteristic {
5353
std::string toString();
5454
uint8_t* readRawData();
5555
BLEAddress getRemoteAddress();
56+
BLERemoteService* getRemoteService();
5657

5758
private:
5859
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
@@ -63,7 +64,6 @@ class BLERemoteCharacteristic {
6364
// Private member functions
6465
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
6566

66-
BLERemoteService* getRemoteService();
6767
void removeDescriptors();
6868
void retrieveDescriptors();
6969

cpp_utils/BLERemoteService.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ void BLERemoteService::gattClientEventHandler(
6161
esp_gattc_cb_event_t event,
6262
esp_gatt_if_t gattc_if,
6363
esp_ble_gattc_cb_param_t* evtParam) {
64+
65+
ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
66+
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
67+
6468
switch (event) {
6569
//
6670
// ESP_GATTC_GET_CHAR_EVT
@@ -162,14 +166,14 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
162166
* @return N/A
163167
*/
164168
void BLERemoteService::retrieveCharacteristics() {
165-
ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str());
169+
ESP_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());
166170

167171
removeCharacteristics(); // Forget any previous characteristics.
168172

169173
uint16_t offset = 0;
170174
esp_gattc_char_elem_t result;
171175
while (true) {
172-
uint16_t count = 10; // this value is used as in parameter that allows to search max 10 chars with the same uuid
176+
uint16_t count = 1;
173177
esp_gatt_status_t status = ::esp_ble_gattc_get_all_char(
174178
getClient()->getGattcIf(),
175179
getClient()->getConnId(),
@@ -180,7 +184,7 @@ void BLERemoteService::retrieveCharacteristics() {
180184
offset
181185
);
182186

183-
if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
187+
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
184188
break;
185189
}
186190

@@ -209,8 +213,8 @@ void BLERemoteService::retrieveCharacteristics() {
209213
} // Loop forever (until we break inside the loop).
210214

211215
m_haveCharacteristics = true; // Remember that we have received the characteristics.
212-
ESP_LOGD(LOG_TAG, "<< getCharacteristics()");
213-
} // getCharacteristics
216+
ESP_LOGD(LOG_TAG, "<< retrieveCharacteristics()");
217+
} // retrieveCharacteristics
214218

215219

216220
/**
@@ -229,6 +233,22 @@ std::map<std::string, BLERemoteCharacteristic*>* BLERemoteService::getCharacteri
229233
return &m_characteristicMap;
230234
} // getCharacteristics
231235

236+
/**
237+
* @brief Retrieve a map of all the characteristics of this service.
238+
* @return A map of all the characteristics of this service.
239+
*/
240+
std::map<uint16_t, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristicsByHandle() {
241+
ESP_LOGD(LOG_TAG, ">> getCharacteristicsByHandle() for service: %s", getUUID().toString().c_str());
242+
// If is possible that we have not read the characteristics associated with the service so do that
243+
// now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
244+
// call and does not return until all the characteristics are available.
245+
if (!m_haveCharacteristics) {
246+
retrieveCharacteristics();
247+
}
248+
ESP_LOGD(LOG_TAG, "<< getCharacteristicsByHandle() for service: %s", getUUID().toString().c_str());
249+
return &m_characteristicMapByHandle;
250+
} // getCharacteristicsByHandle
251+
232252
/**
233253
* @brief This function is designed to get characteristics map when we have multiple characteristics with the same UUID
234254
*/
@@ -292,10 +312,6 @@ std::string BLERemoteService::getValue(BLEUUID characteristicUuid) {
292312
* @return N/A.
293313
*/
294314
void BLERemoteService::removeCharacteristics() {
295-
for (auto &myPair : m_characteristicMap) {
296-
delete myPair.second;
297-
//m_characteristicMap.erase(myPair.first); // Should be no need to delete as it will be deleted by the clear
298-
}
299315
m_characteristicMap.clear(); // Clear the map
300316
for (auto &myPair : m_characteristicMapByHandle) {
301317
delete myPair.second;

cpp_utils/BLEUtils.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ static std::string gattIdToString(esp_gatt_id_t gattId) {
637637
*/
638638
const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
639639
switch (type) {
640-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
641640
case BLE_ADDR_TYPE_PUBLIC:
642641
return "BLE_ADDR_TYPE_PUBLIC";
643642
case BLE_ADDR_TYPE_RANDOM:
@@ -646,7 +645,6 @@ const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
646645
return "BLE_ADDR_TYPE_RPA_PUBLIC";
647646
case BLE_ADDR_TYPE_RPA_RANDOM:
648647
return "BLE_ADDR_TYPE_RPA_RANDOM";
649-
#endif
650648
default:
651649
return " esp_ble_addr_type_t";
652650
}
@@ -689,7 +687,6 @@ std::string BLEUtils::adFlagsToString(uint8_t adFlags) {
689687
*/
690688
const char* BLEUtils::advTypeToString(uint8_t advType) {
691689
switch (advType) {
692-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
693690
case ESP_BLE_AD_TYPE_FLAG: // 0x01
694691
return "ESP_BLE_AD_TYPE_FLAG";
695692
case ESP_BLE_AD_TYPE_16SRV_PART: // 0x02
@@ -740,7 +737,6 @@ const char* BLEUtils::advTypeToString(uint8_t advType) {
740737
return "ESP_BLE_AD_TYPE_128SERVICE_DATA";
741738
case ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE: // 0xff
742739
return "ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE";
743-
#endif
744740
default:
745741
ESP_LOGV(LOG_TAG, " adv data type: 0x%x", advType);
746742
return "";
@@ -824,7 +820,6 @@ std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) {
824820
*/
825821
std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
826822
switch (reason) {
827-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
828823
case ESP_GATT_CONN_UNKNOWN: {
829824
return "ESP_GATT_CONN_UNKNOWN";
830825
}
@@ -852,7 +847,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
852847
case ESP_GATT_CONN_NONE: {
853848
return "ESP_GATT_CONN_NONE";
854849
}
855-
#endif
856850
default: {
857851
return "Unknown";
858852
}
@@ -862,7 +856,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
862856

863857
std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType) {
864858
switch (eventType) {
865-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
866859
case ESP_GATTC_ACL_EVT:
867860
return "ESP_GATTC_ACL_EVT";
868861
case ESP_GATTC_ADV_DATA_EVT:
@@ -945,7 +938,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
945938
return "ESP_GATTC_WRITE_CHAR_EVT";
946939
case ESP_GATTC_WRITE_DESCR_EVT:
947940
return "ESP_GATTC_WRITE_DESCR_EVT";
948-
#endif
949941
default:
950942
ESP_LOGV(LOG_TAG, "Unknown GATT Client event type: %d", eventType);
951943
return "Unknown";
@@ -960,7 +952,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
960952
*/
961953
std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType) {
962954
switch (eventType) {
963-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
964955
case ESP_GATTS_REG_EVT:
965956
return "ESP_GATTS_REG_EVT";
966957
case ESP_GATTS_READ_EVT:
@@ -1011,7 +1002,6 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
10111002
return "ESP_GATTS_SET_ATTR_VAL_EVT";
10121003
case ESP_GATTS_SEND_SERVICE_CHANGE_EVT:
10131004
return "ESP_GATTS_SEND_SERVICE_CHANGE_EVT";
1014-
#endif
10151005
default:
10161006
return "Unknown";
10171007
}
@@ -1025,14 +1015,12 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
10251015
*/
10261016
const char* BLEUtils::devTypeToString(esp_bt_dev_type_t type) {
10271017
switch (type) {
1028-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
10291018
case ESP_BT_DEVICE_TYPE_BREDR:
10301019
return "ESP_BT_DEVICE_TYPE_BREDR";
10311020
case ESP_BT_DEVICE_TYPE_BLE:
10321021
return "ESP_BT_DEVICE_TYPE_BLE";
10331022
case ESP_BT_DEVICE_TYPE_DUMO:
10341023
return "ESP_BT_DEVICE_TYPE_DUMO";
1035-
#endif
10361024
default:
10371025
return "Unknown";
10381026
}
@@ -1729,7 +1717,6 @@ void BLEUtils::dumpGattServerEvent(
17291717
*/
17301718
const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17311719
switch (eventType) {
1732-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
17331720
case ESP_BLE_EVT_CONN_ADV:
17341721
return "ESP_BLE_EVT_CONN_ADV";
17351722
case ESP_BLE_EVT_CONN_DIR_ADV:
@@ -1740,7 +1727,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17401727
return "ESP_BLE_EVT_NON_CONN_ADV";
17411728
case ESP_BLE_EVT_SCAN_RSP:
17421729
return "ESP_BLE_EVT_SCAN_RSP";
1743-
#endif
17441730
default:
17451731
ESP_LOGV(LOG_TAG, "Unknown esp_ble_evt_type_t: %d (0x%.2x)", eventType, eventType);
17461732
return "*** Unknown ***";
@@ -1756,7 +1742,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17561742
*/
17571743
const char* BLEUtils::gapEventToString(uint32_t eventType) {
17581744
switch (eventType) {
1759-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
17601745
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
17611746
return "ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT";
17621747
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
@@ -1811,7 +1796,6 @@ const char* BLEUtils::gapEventToString(uint32_t eventType) {
18111796
return "ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT";
18121797
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
18131798
return "ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT";
1814-
#endif
18151799
default:
18161800
ESP_LOGV(LOG_TAG, "gapEventToString: Unknown event type %d 0x%.2x", eventType, eventType);
18171801
return "Unknown event type";
@@ -1892,7 +1876,6 @@ std::string BLEUtils::gattServiceToString(uint32_t serviceId) {
18921876
*/
18931877
std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
18941878
switch (status) {
1895-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
18961879
case ESP_GATT_OK:
18971880
return "ESP_GATT_OK";
18981881
case ESP_GATT_INVALID_HANDLE:
@@ -1979,7 +1962,6 @@ std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
19791962
return "ESP_GATT_PRC_IN_PROGRESS";
19801963
case ESP_GATT_OUT_OF_RANGE:
19811964
return "ESP_GATT_OUT_OF_RANGE";
1982-
#endif
19831965
default:
19841966
return "Unknown";
19851967
}
@@ -2006,7 +1988,6 @@ std::string BLEUtils::getMember(uint32_t memberId) {
20061988
*/
20071989
const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
20081990
switch (searchEvt) {
2009-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
20101991
case ESP_GAP_SEARCH_INQ_RES_EVT:
20111992
return "ESP_GAP_SEARCH_INQ_RES_EVT";
20121993
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
@@ -2021,7 +2002,6 @@ const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
20212002
return "ESP_GAP_SEARCH_DI_DISC_CMPL_EVT";
20222003
case ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT:
20232004
return "ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT";
2024-
#endif
20252005
default:
20262006
ESP_LOGV(LOG_TAG, "Unknown event type: 0x%x", searchEvt);
20272007
return "Unknown event type";

0 commit comments

Comments
 (0)
0