Skip to content

Commit

Permalink
Merge pull request nkolban#783 from wakwak-koba/master
Browse files Browse the repository at this point in the history
fixed bugs
  • Loading branch information
chegewara committed Jan 18, 2019
2 parents 30ee858 + e63c941 commit 69fb1c6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
33 changes: 20 additions & 13 deletions cpp_utils/BLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,22 @@ void BLEClient::gattClientEventHandler(
// - uint16_t conn_id
// - esp_bd_addr_t remote_bda
case ESP_GATTC_DISCONNECT_EVT: {
// If we receive a disconnect event, set the class flag that indicates that we are
// no longer connected.
m_isConnected = false;
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onDisconnect(this);
}
BLEDevice::removePeerDevice(m_appId, true);
esp_ble_gattc_app_unregister(m_gattc_if);
m_semaphoreRssiCmplEvt.give();
m_semaphoreSearchCmplEvt.give(1);
ESP_LOGE(__func__, "disconnect event, conn_id: %d", evtParam->disconnect.conn_id);
if(getConnId() != evtParam->disconnect.conn_id)
break;
m_semaphoreOpenEvt.give(evtParam->disconnect.reason);
if(!m_isConnected)
break;
// If we receive a disconnect event, set the class flag that indicates that we are
// no longer connected.
esp_ble_gattc_close(m_gattc_if, m_conn_id);
m_isConnected = false;
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onDisconnect(this);
}
break;
} // ESP_GATTC_DISCONNECT_EVT

//
// ESP_GATTC_OPEN_EVT
//
Expand Down Expand Up @@ -224,8 +227,12 @@ void BLEClient::gattClientEventHandler(
// uint16_t app_id
//
case ESP_GATTC_REG_EVT: {
m_gattc_if = gattc_if;
m_semaphoreRegEvt.give();
if(m_appId == evtParam->reg.app_id){
ESP_LOGI(__func__, "register app id: %d, %d, gattc_if: %d", m_appId, evtParam->reg.app_id, gattc_if);
m_gattc_if = gattc_if;
m_appId = evtParam->reg.app_id;
m_semaphoreRegEvt.give();
}
break;
} // ESP_GATTC_REG_EVT

Expand Down
14 changes: 10 additions & 4 deletions cpp_utils/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ static bool compareGattId(esp_gatt_id_t id1, esp_gatt_id_t id2) {
* @returns N/A
*/
void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) {

ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());

switch(event) {
// ESP_GATTC_NOTIFY_EVT
//
Expand Down Expand Up @@ -262,7 +266,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
uint16_t offset = 0;
esp_gattc_descr_elem_t result;
while(true) {
uint16_t count = 10;
uint16_t count = 1;
esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr(
getRemoteService()->getClient()->getGattcIf(),
getRemoteService()->getClient()->getConnId(),
Expand All @@ -272,7 +276,7 @@ void BLERemoteCharacteristic::retrieveDescriptors() {
offset
);

if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
break;
}

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

uint8_t val[] = {0x00, 0x00};
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
desc->writeValue(val, 2);
if(desc != nullptr)
desc->writeValue(val, 2);
} // End Unregister

m_semaphoreRegForNotifyEvt.wait("registerForNotify");
Expand Down
2 changes: 1 addition & 1 deletion cpp_utils/BLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class BLERemoteCharacteristic {
std::string toString();
uint8_t* readRawData();
BLEAddress getRemoteAddress();
BLERemoteService* getRemoteService();

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

BLERemoteService* getRemoteService();
void removeDescriptors();
void retrieveDescriptors();

Expand Down
34 changes: 25 additions & 9 deletions cpp_utils/BLERemoteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ void BLERemoteService::gattClientEventHandler(
esp_gattc_cb_event_t event,
esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t* evtParam) {

ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());

switch (event) {
//
// ESP_GATTC_GET_CHAR_EVT
Expand Down Expand Up @@ -162,14 +166,14 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
* @return N/A
*/
void BLERemoteService::retrieveCharacteristics() {
ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str());
ESP_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str());

removeCharacteristics(); // Forget any previous characteristics.

uint16_t offset = 0;
esp_gattc_char_elem_t result;
while (true) {
uint16_t count = 10; // this value is used as in parameter that allows to search max 10 chars with the same uuid
uint16_t count = 1;
esp_gatt_status_t status = ::esp_ble_gattc_get_all_char(
getClient()->getGattcIf(),
getClient()->getConnId(),
Expand All @@ -180,7 +184,7 @@ void BLERemoteService::retrieveCharacteristics() {
offset
);

if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) { // We have reached the end of the entries.
break;
}

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

m_haveCharacteristics = true; // Remember that we have received the characteristics.
ESP_LOGD(LOG_TAG, "<< getCharacteristics()");
} // getCharacteristics
ESP_LOGD(LOG_TAG, "<< retrieveCharacteristics()");
} // retrieveCharacteristics


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

/**
* @brief Retrieve a map of all the characteristics of this service.
* @return A map of all the characteristics of this service.
*/
std::map<uint16_t, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristicsByHandle() {
ESP_LOGD(LOG_TAG, ">> getCharacteristicsByHandle() for service: %s", getUUID().toString().c_str());
// If is possible that we have not read the characteristics associated with the service so do that
// now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
// call and does not return until all the characteristics are available.
if (!m_haveCharacteristics) {
retrieveCharacteristics();
}
ESP_LOGD(LOG_TAG, "<< getCharacteristicsByHandle() for service: %s", getUUID().toString().c_str());
return &m_characteristicMapByHandle;
} // getCharacteristicsByHandle

/**
* @brief This function is designed to get characteristics map when we have multiple characteristics with the same UUID
*/
Expand Down Expand Up @@ -292,10 +312,6 @@ std::string BLERemoteService::getValue(BLEUUID characteristicUuid) {
* @return N/A.
*/
void BLERemoteService::removeCharacteristics() {
for (auto &myPair : m_characteristicMap) {
delete myPair.second;
//m_characteristicMap.erase(myPair.first); // Should be no need to delete as it will be deleted by the clear
}
m_characteristicMap.clear(); // Clear the map
for (auto &myPair : m_characteristicMapByHandle) {
delete myPair.second;
Expand Down
20 changes: 0 additions & 20 deletions cpp_utils/BLEUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@ static std::string gattIdToString(esp_gatt_id_t gattId) {
*/
const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
switch (type) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case BLE_ADDR_TYPE_PUBLIC:
return "BLE_ADDR_TYPE_PUBLIC";
case BLE_ADDR_TYPE_RANDOM:
Expand All @@ -646,7 +645,6 @@ const char* BLEUtils::addressTypeToString(esp_ble_addr_type_t type) {
return "BLE_ADDR_TYPE_RPA_PUBLIC";
case BLE_ADDR_TYPE_RPA_RANDOM:
return "BLE_ADDR_TYPE_RPA_RANDOM";
#endif
default:
return " esp_ble_addr_type_t";
}
Expand Down Expand Up @@ -689,7 +687,6 @@ std::string BLEUtils::adFlagsToString(uint8_t adFlags) {
*/
const char* BLEUtils::advTypeToString(uint8_t advType) {
switch (advType) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_BLE_AD_TYPE_FLAG: // 0x01
return "ESP_BLE_AD_TYPE_FLAG";
case ESP_BLE_AD_TYPE_16SRV_PART: // 0x02
Expand Down Expand Up @@ -740,7 +737,6 @@ const char* BLEUtils::advTypeToString(uint8_t advType) {
return "ESP_BLE_AD_TYPE_128SERVICE_DATA";
case ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE: // 0xff
return "ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE";
#endif
default:
ESP_LOGV(LOG_TAG, " adv data type: 0x%x", advType);
return "";
Expand Down Expand Up @@ -824,7 +820,6 @@ std::string BLEUtils::buildPrintData(uint8_t* source, size_t length) {
*/
std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
switch (reason) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GATT_CONN_UNKNOWN: {
return "ESP_GATT_CONN_UNKNOWN";
}
Expand Down Expand Up @@ -852,7 +847,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {
case ESP_GATT_CONN_NONE: {
return "ESP_GATT_CONN_NONE";
}
#endif
default: {
return "Unknown";
}
Expand All @@ -862,7 +856,6 @@ std::string BLEUtils::gattCloseReasonToString(esp_gatt_conn_reason_t reason) {

std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType) {
switch (eventType) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GATTC_ACL_EVT:
return "ESP_GATTC_ACL_EVT";
case ESP_GATTC_ADV_DATA_EVT:
Expand Down Expand Up @@ -945,7 +938,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
return "ESP_GATTC_WRITE_CHAR_EVT";
case ESP_GATTC_WRITE_DESCR_EVT:
return "ESP_GATTC_WRITE_DESCR_EVT";
#endif
default:
ESP_LOGV(LOG_TAG, "Unknown GATT Client event type: %d", eventType);
return "Unknown";
Expand All @@ -960,7 +952,6 @@ std::string BLEUtils::gattClientEventTypeToString(esp_gattc_cb_event_t eventType
*/
std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType) {
switch (eventType) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GATTS_REG_EVT:
return "ESP_GATTS_REG_EVT";
case ESP_GATTS_READ_EVT:
Expand Down Expand Up @@ -1011,7 +1002,6 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
return "ESP_GATTS_SET_ATTR_VAL_EVT";
case ESP_GATTS_SEND_SERVICE_CHANGE_EVT:
return "ESP_GATTS_SEND_SERVICE_CHANGE_EVT";
#endif
default:
return "Unknown";
}
Expand All @@ -1025,14 +1015,12 @@ std::string BLEUtils::gattServerEventTypeToString(esp_gatts_cb_event_t eventType
*/
const char* BLEUtils::devTypeToString(esp_bt_dev_type_t type) {
switch (type) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_BT_DEVICE_TYPE_BREDR:
return "ESP_BT_DEVICE_TYPE_BREDR";
case ESP_BT_DEVICE_TYPE_BLE:
return "ESP_BT_DEVICE_TYPE_BLE";
case ESP_BT_DEVICE_TYPE_DUMO:
return "ESP_BT_DEVICE_TYPE_DUMO";
#endif
default:
return "Unknown";
}
Expand Down Expand Up @@ -1729,7 +1717,6 @@ void BLEUtils::dumpGattServerEvent(
*/
const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
switch (eventType) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_BLE_EVT_CONN_ADV:
return "ESP_BLE_EVT_CONN_ADV";
case ESP_BLE_EVT_CONN_DIR_ADV:
Expand All @@ -1740,7 +1727,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
return "ESP_BLE_EVT_NON_CONN_ADV";
case ESP_BLE_EVT_SCAN_RSP:
return "ESP_BLE_EVT_SCAN_RSP";
#endif
default:
ESP_LOGV(LOG_TAG, "Unknown esp_ble_evt_type_t: %d (0x%.2x)", eventType, eventType);
return "*** Unknown ***";
Expand All @@ -1756,7 +1742,6 @@ const char* BLEUtils::eventTypeToString(esp_ble_evt_type_t eventType) {
*/
const char* BLEUtils::gapEventToString(uint32_t eventType) {
switch (eventType) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
return "ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT";
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
Expand Down Expand Up @@ -1811,7 +1796,6 @@ const char* BLEUtils::gapEventToString(uint32_t eventType) {
return "ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT";
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
return "ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT";
#endif
default:
ESP_LOGV(LOG_TAG, "gapEventToString: Unknown event type %d 0x%.2x", eventType, eventType);
return "Unknown event type";
Expand Down Expand Up @@ -1892,7 +1876,6 @@ std::string BLEUtils::gattServiceToString(uint32_t serviceId) {
*/
std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
switch (status) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GATT_OK:
return "ESP_GATT_OK";
case ESP_GATT_INVALID_HANDLE:
Expand Down Expand Up @@ -1979,7 +1962,6 @@ std::string BLEUtils::gattStatusToString(esp_gatt_status_t status) {
return "ESP_GATT_PRC_IN_PROGRESS";
case ESP_GATT_OUT_OF_RANGE:
return "ESP_GATT_OUT_OF_RANGE";
#endif
default:
return "Unknown";
}
Expand All @@ -2006,7 +1988,6 @@ std::string BLEUtils::getMember(uint32_t memberId) {
*/
const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
switch (searchEvt) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_GAP_SEARCH_INQ_RES_EVT:
return "ESP_GAP_SEARCH_INQ_RES_EVT";
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
Expand All @@ -2021,7 +2002,6 @@ const char* BLEUtils::searchEventTypeToString(esp_gap_search_evt_t searchEvt) {
return "ESP_GAP_SEARCH_DI_DISC_CMPL_EVT";
case ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT:
return "ESP_GAP_SEARCH_SEARCH_CANCEL_CMPL_EVT";
#endif
default:
ESP_LOGV(LOG_TAG, "Unknown event type: 0x%x", searchEvt);
return "Unknown event type";
Expand Down

0 comments on commit 69fb1c6

Please sign in to comment.