Skip to content

Commit b1ef06e

Browse files
committed
fixed bugs
retrieveCharacteristics で count = 10; にしたいならば、https://twitter.com/wakwak_koba/status/1084232977599459328 のようなコードを書く必要あり
1 parent b232e7f commit b1ef06e

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/BLERemoteCharacteristic.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
262262
uint16_t offset = 0;
263263
esp_gattc_descr_elem_t result;
264264
while(true) {
265-
uint16_t count = 10;
265+
uint16_t count = 1;
266266
esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr(
267267
getRemoteService()->getClient()->getGattcIf(),
268268
getRemoteService()->getClient()->getConnId(),
@@ -272,7 +272,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
272272
offset
273273
);
274274

275-
if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
275+
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
276276
break;
277277
}
278278

@@ -461,7 +461,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
461461
uint8_t val[] = {0x01, 0x00};
462462
if(!notifications) val[0] = 0x02;
463463
BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902));
464-
desc->writeValue(val, 2);
464+
if(desc != nullptr)
465+
desc->writeValue(val, 2);
465466
} // End Register
466467
else { // If we weren't passed a callback function, then this is an unregistration.
467468
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
@@ -476,7 +477,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
476477

477478
uint8_t val[] = {0x00, 0x00};
478479
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
479-
desc->writeValue(val, 2);
480+
if(desc != nullptr)
481+
desc->writeValue(val, 2);
480482
} // End Unregister
481483

482484
m_semaphoreRegForNotifyEvt.wait("registerForNotify");

src/BLERemoteService.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
165165
* @return N/A
166166
*/
167167
void BLERemoteService::retrieveCharacteristics() {
168-
ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str());
168+
ESP_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());
169169

170170
removeCharacteristics(); // Forget any previous characteristics.
171171

172172
uint16_t offset = 0;
173173
esp_gattc_char_elem_t result;
174174
while (true) {
175-
uint16_t count = 10; // this value is used as in parameter that allows to search max 10 chars with the same uuid
175+
uint16_t count = 1;
176176
esp_gatt_status_t status = ::esp_ble_gattc_get_all_char(
177177
getClient()->getGattcIf(),
178178
getClient()->getConnId(),
@@ -183,7 +183,7 @@ void BLERemoteService::retrieveCharacteristics() {
183183
offset
184184
);
185185

186-
if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
186+
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
187187
break;
188188
}
189189

@@ -212,8 +212,8 @@ void BLERemoteService::retrieveCharacteristics() {
212212
} // Loop forever (until we break inside the loop).
213213

214214
m_haveCharacteristics = true; // Remember that we have received the characteristics.
215-
ESP_LOGD(LOG_TAG, "<< getCharacteristics()");
216-
} // getCharacteristics
215+
ESP_LOGD(LOG_TAG, "<< retrieveCharacteristics()");
216+
} // retrieveCharacteristics
217217

218218

219219
/**
@@ -232,6 +232,22 @@ std::map<std::string, BLERemoteCharacteristic*>* BLERemoteService::getCharacteri
232232
return &m_characteristicMap;
233233
} // getCharacteristics
234234

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

src/BLEUtils.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,6 @@ static std::string gattIdToString(esp_gatt_id_t gattId) {
639639
*/
640640
const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
641641
switch (type) {
642-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
643642
case BLE_ADDR_TYPE_PUBLIC:
644643
return "BLE_ADDR_TYPE_PUBLIC";
645644
case BLE_ADDR_TYPE_RANDOM:
@@ -648,7 +647,6 @@ const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
648647
return "BLE_ADDR_TYPE_RPA_PUBLIC";
649648
case BLE_ADDR_TYPE_RPA_RANDOM:
650649
return "BLE_ADDR_TYPE_RPA_RANDOM";
651-
#endif
652650
default:
653651
return " esp_ble_addr_type_t";
654652
}
@@ -691,7 +689,6 @@ std::string BLEUtils::adFlagsToString(uint8_t adFlags) {
691689
*/
692690
const char* BLEUtils::advTypeToString(uint8_t advType) {
693691
switch (advType) {
694-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
695692
case ESP_BLE_AD_TYPE_FLAG: // 0x01
696693
return "ESP_BLE_AD_TYPE_FLAG";
697694
case ESP_BLE_AD_TYPE_16SRV_PART: // 0x02
@@ -742,7 +739,6 @@ const char* BLEUtils::advTypeToString(uint8_t advType) {
742739
return "ESP_BLE_AD_TYPE_128SERVICE_DATA";
743740
case ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE: // 0xff
744741
return "ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE";
745-
#endif
746742
default:
747743
ESP_LOGV(LOG_TAG, " adv data type: 0x%x", advType);
748744
return "";
@@ -826,7 +822,6 @@ std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) {
826822
*/
827823
std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
828824
switch (reason) {
829-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
830825
case ESP_GATT_CONN_UNKNOWN: {
831826
return "ESP_GATT_CONN_UNKNOWN";
832827
}
@@ -854,7 +849,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
854849
case ESP_GATT_CONN_NONE: {
855850
return "ESP_GATT_CONN_NONE";
856851
}
857-
#endif
858852
default: {
859853
return "Unknown";
860854
}
@@ -864,7 +858,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
864858

865859
std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType) {
866860
switch (eventType) {
867-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
868861
case ESP_GATTC_ACL_EVT:
869862
return "ESP_GATTC_ACL_EVT";
870863
case ESP_GATTC_ADV_DATA_EVT:
@@ -947,7 +940,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
947940
return "ESP_GATTC_WRITE_CHAR_EVT";
948941
case ESP_GATTC_WRITE_DESCR_EVT:
949942
return "ESP_GATTC_WRITE_DESCR_EVT";
950-
#endif
951943
default:
952944
ESP_LOGV(LOG_TAG, "Unknown GATT Client event type: %d", eventType);
953945
return "Unknown";
@@ -962,7 +954,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
962954
*/
963955
std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType) {
964956
switch (eventType) {
965-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
966957
case ESP_GATTS_REG_EVT:
967958
return "ESP_GATTS_REG_EVT";
968959
case ESP_GATTS_READ_EVT:
@@ -1013,7 +1004,6 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
10131004
return "ESP_GATTS_SET_ATTR_VAL_EVT";
10141005
case ESP_GATTS_SEND_SERVICE_CHANGE_EVT:
10151006
return "ESP_GATTS_SEND_SERVICE_CHANGE_EVT";
1016-
#endif
10171007
default:
10181008
return "Unknown";
10191009
}
@@ -1027,14 +1017,12 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
10271017
*/
10281018
const char* BLEUtils::devTypeToString(esp_bt_dev_type_t type) {
10291019
switch (type) {
1030-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
10311020
case ESP_BT_DEVICE_TYPE_BREDR:
10321021
return "ESP_BT_DEVICE_TYPE_BREDR";
10331022
case ESP_BT_DEVICE_TYPE_BLE:
10341023
return "ESP_BT_DEVICE_TYPE_BLE";
10351024
case ESP_BT_DEVICE_TYPE_DUMO:
10361025
return "ESP_BT_DEVICE_TYPE_DUMO";
1037-
#endif
10381026
default:
10391027
return "Unknown";
10401028
}
@@ -1731,7 +1719,6 @@ void BLEUtils::dumpGattServerEvent(
17311719
*/
17321720
const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17331721
switch (eventType) {
1734-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
17351722
case ESP_BLE_EVT_CONN_ADV:
17361723
return "ESP_BLE_EVT_CONN_ADV";
17371724
case ESP_BLE_EVT_CONN_DIR_ADV:
@@ -1742,7 +1729,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17421729
return "ESP_BLE_EVT_NON_CONN_ADV";
17431730
case ESP_BLE_EVT_SCAN_RSP:
17441731
return "ESP_BLE_EVT_SCAN_RSP";
1745-
#endif
17461732
default:
17471733
ESP_LOGV(LOG_TAG, "Unknown esp_ble_evt_type_t: %d (0x%.2x)", eventType, eventType);
17481734
return "*** Unknown ***";
@@ -1758,7 +1744,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
17581744
*/
17591745
const char* BLEUtils::gapEventToString(uint32_t eventType) {
17601746
switch (eventType) {
1761-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
17621747
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
17631748
return "ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT";
17641749
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
@@ -1813,7 +1798,6 @@ const char* BLEUtils::gapEventToString(uint32_t eventType) {
18131798
return "ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT";
18141799
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
18151800
return "ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT";
1816-
#endif
18171801
default:
18181802
ESP_LOGV(LOG_TAG, "gapEventToString: Unknown event type %d 0x%.2x", eventType, eventType);
18191803
return "Unknown event type";
@@ -1894,7 +1878,6 @@ std::string BLEUtils::gattServiceToString(uint32_t serviceId) {
18941878
*/
18951879
std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
18961880
switch (status) {
1897-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
18981881
case ESP_GATT_OK:
18991882
return "ESP_GATT_OK";
19001883
case ESP_GATT_INVALID_HANDLE:
@@ -1981,7 +1964,6 @@ std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
19811964
return "ESP_GATT_PRC_IN_PROGRESS";
19821965
case ESP_GATT_OUT_OF_RANGE:
19831966
return "ESP_GATT_OUT_OF_RANGE";
1984-
#endif
19851967
default:
19861968
return "Unknown";
19871969
}
@@ -2008,7 +1990,6 @@ std::string BLEUtils::getMember(uint32_t memberId) {
20081990
*/
20091991
const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
20101992
switch (searchEvt) {
2011-
#if CONFIG_LOG_DEFAULT_LEVEL > 4
20121993
case ESP_GAP_SEARCH_INQ_RES_EVT:
20131994
return "ESP_GAP_SEARCH_INQ_RES_EVT";
20141995
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
@@ -2023,7 +2004,6 @@ const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
20232004
return "ESP_GAP_SEARCH_DI_DISC_CMPL_EVT";
20242005
case ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT:
20252006
return "ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT";
2026-
#endif
20272007
default:
20282008
ESP_LOGV(LOG_TAG, "Unknown event type: 0x%x", searchEvt);
20292009
return "Unknown event type";

0 commit comments

Comments
 (0)