diff options
author | kolban <kolban1@kolban.com> | 2017-10-13 10:52:17 -0500 |
---|---|---|
committer | kolban <kolban1@kolban.com> | 2017-10-13 10:52:17 -0500 |
commit | 34902f4e7fc6926a005a5ccfd4e530d6a6321613 (patch) | |
tree | 512593068ed656012ee31fced9526bff80ecad55 /src/BLERemoteService.cpp | |
parent | Merge pull request #5 from sebastiankliem/master (diff) | |
download | thermostat-34902f4e7fc6926a005a5ccfd4e530d6a6321613.tar.gz thermostat-34902f4e7fc6926a005a5ccfd4e530d6a6321613.tar.bz2 thermostat-34902f4e7fc6926a005a5ccfd4e530d6a6321613.zip |
Sync update for 0.4.3 take 2
Diffstat (limited to 'src/BLERemoteService.cpp')
-rw-r--r-- | src/BLERemoteService.cpp | 159 |
1 files changed, 146 insertions, 13 deletions
diff --git a/src/BLERemoteService.cpp b/src/BLERemoteService.cpp index 4d18625..c312e94 100644 --- a/src/BLERemoteService.cpp +++ b/src/BLERemoteService.cpp @@ -17,13 +17,21 @@ static const char* LOG_TAG = "BLERemoteService"; BLERemoteService::BLERemoteService( - esp_gatt_srvc_id_t srvcId, - BLEClient *pClient) { + esp_gatt_id_t srvcId, + BLEClient* pClient, + uint16_t startHandle, + uint16_t endHandle + ) { + ESP_LOGD(LOG_TAG, ">> BLERemoteService()"); m_srvcId = srvcId; m_pClient = pClient; m_uuid = BLEUUID(m_srvcId); m_haveCharacteristics = false; + m_startHandle = startHandle; + m_endHandle = endHandle; + + ESP_LOGD(LOG_TAG, "<< BLERemoteService()"); } @@ -31,6 +39,7 @@ BLERemoteService::~BLERemoteService() { removeCharacteristics(); } +/* static bool compareSrvcId(esp_gatt_srvc_id_t id1, esp_gatt_srvc_id_t id2) { if (id1.id.inst_id != id2.id.inst_id) { return false; @@ -40,7 +49,7 @@ static bool compareSrvcId(esp_gatt_srvc_id_t id1, esp_gatt_srvc_id_t id2) { } return true; } // compareSrvcId - +*/ /** * @brief Handle GATT Client events @@ -60,6 +69,7 @@ void BLERemoteService::gattClientEventHandler( // - esp_gatt_id_t char_id // - esp_gatt_char_prop_t char_prop // + /* case ESP_GATTC_GET_CHAR_EVT: { // Is this event for this service? If yes, then the local srvc_id and the event srvc_id will be // the same. @@ -94,7 +104,7 @@ void BLERemoteService::gattClientEventHandler( //m_semaphoreGetCharEvt.give(); break; } // ESP_GATTC_GET_CHAR_EVT - +*/ default: { break; } @@ -108,13 +118,13 @@ void BLERemoteService::gattClientEventHandler( /** - * @brief Get the characteristic object for the UUID. - * @param [in] uuid Characteristic uuid. - * @return Reference to the characteristic object. + * @brief Get the remote characteristic object for the characteristic UUID. + * @param [in] uuid Remote characteristic uuid. + * @return Reference to the remote characteristic object. */ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(const char* uuid) { return getCharacteristic(BLEUUID(uuid)); -} +} // getCharacteristic /** @@ -130,7 +140,7 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) { // asked the device about its characteristics, then we do that now. Once we get the results we can then // examine the characteristics map to see if it has the characteristic we are looking for. if (!m_haveCharacteristics) { - getCharacteristics(); + retrieveCharacteristics(); } std::string v = uuid.toString(); for (auto &myPair : m_characteristicMap) { @@ -144,14 +154,15 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) { /** * @brief Retrieve all the characteristics for this service. + * This function will not return until we have all the characteristics. * @return N/A */ -void BLERemoteService::getCharacteristics() { +void BLERemoteService::retrieveCharacteristics() { ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str()); removeCharacteristics(); // Forget any previous characteristics. - + /* m_semaphoreGetCharEvt.take("getCharacteristics"); esp_err_t errRc = ::esp_ble_gattc_get_characteristic( @@ -168,18 +179,135 @@ void BLERemoteService::getCharacteristics() { m_semaphoreGetCharEvt.wait("getCharacteristics"); // Wait for the characteristics to become available. m_haveCharacteristics = true; // Remember that we have received the characteristics. + */ + //ESP_LOGE(LOG_TAG, "!!! NOT IMPLEMENTED !!!"); + //ESP_LOGD(LOG_TAG, "--- test code ---"); + /* + uint16_t count; + esp_gatt_status_t status = ::esp_ble_gattc_get_attr_count( + getClient()->getGattcIf(), + getClient()->getConnId(), + ESP_GATT_DB_CHARACTERISTIC, + m_startHandle, + m_endHandle, + 0, // Characteristic handle ... only used for ESP_GATT_DB_DESCRIPTOR + &count + ); + if (status != ESP_GATT_OK) { + ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_attr_count: %s", BLEUtils::gattStatusToString(status).c_str()); + } else { + ESP_LOGD(LOG_TAG, "Number of characteristics associated with service is %d", count); + } + + count = 1; + esp_gattc_service_elem_t srvcElem; + status = ::esp_ble_gattc_get_service( + getClient()->getGattcIf(), + getClient()->getConnId(), + &m_srvcId.uuid, // UUID of service + &srvcElem, // Records + &count, // records retrieved + 0 // offset + ); + if (status != ESP_GATT_OK) { + ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_service: %s", BLEUtils::gattStatusToString(status).c_str()); + } + else { + ESP_LOGD(LOG_TAG, "%s", BLEUtils::gattcServiceElementToString(&srvcElem).c_str()); + } + */ + + uint16_t offset = 0; + esp_gattc_char_elem_t result; + while(1) { + uint16_t count = 1; + esp_gatt_status_t status = ::esp_ble_gattc_get_all_char( + getClient()->getGattcIf(), + getClient()->getConnId(), + m_startHandle, + m_endHandle, + &result, + &count, + offset + ); + + if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries. + break; + } + + if (status != ESP_GATT_OK) { // If we got an error, end. + ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_all_char: %s", BLEUtils::gattStatusToString(status).c_str()); + break; + } + + if (count == 0) { // If we failed to get any new records, end. + break; + } + + ESP_LOGD(LOG_TAG, "Found a characteristic: Handle: %d, UUID: %s", result.char_handle, BLEUUID(result.uuid).toString().c_str()); + + // We now have a new characteristic ... let us add that to our set of known characteristics + BLERemoteCharacteristic *pNewRemoteCharacteristic = new BLERemoteCharacteristic( + result.char_handle, + BLEUUID(result.uuid), + result.properties, + this + ); + + m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(pNewRemoteCharacteristic->getUUID().toString(), pNewRemoteCharacteristic)); + + offset++; // Increment our count of number of descriptors found. + } // Loop forever (until we break inside the loop). + + m_haveCharacteristics = true; // Remember that we have received the characteristics. ESP_LOGD(LOG_TAG, "<< getCharacteristics()"); } // getCharacteristics +/** + * @brief Retrieve a map of all the characteristics of this service. + * @return A map of all the characteristics of this service. + */ +std::map<std::string, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristics() { + ESP_LOGD(LOG_TAG, ">> getCharacteristics() 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, "<< getCharacteristics() for service: %s", getUUID().toString().c_str()); + return &m_characteristicMap; +} // getCharacteristics + + BLEClient* BLERemoteService::getClient() { return m_pClient; } -esp_gatt_srvc_id_t* BLERemoteService::getSrvcId() { + +uint16_t BLERemoteService::getEndHandle() { + return m_endHandle; +} + + +esp_gatt_id_t* BLERemoteService::getSrvcId() { return &m_srvcId; } + +uint16_t BLERemoteService::getStartHandle() { + return m_startHandle; +} + +uint16_t BLERemoteService::getHandle() { + ESP_LOGD(LOG_TAG, ">> getHandle: service: %s", getUUID().toString().c_str()); + //ESP_LOGE(LOG_TAG, "!!! getHandle: NOT IMPLEMENTED !!!"); + ESP_LOGD(LOG_TAG, "<< getHandle: %d 0x%.2x", getStartHandle(), getStartHandle()); + return getStartHandle(); +} + + BLEUUID BLERemoteService::getUUID() { return m_uuid; } @@ -195,8 +323,9 @@ BLEUUID BLERemoteService::getUUID() { void BLERemoteService::removeCharacteristics() { for (auto &myPair : m_characteristicMap) { delete myPair.second; + m_characteristicMap.erase(myPair.first); } - m_characteristicMap.clear(); + m_characteristicMap.clear(); // Clear the map } // removeCharacteristics @@ -208,6 +337,8 @@ void BLERemoteService::removeCharacteristics() { std::string BLERemoteService::toString() { std::ostringstream ss; ss << "Service: uuid: " + m_uuid.toString(); + ss << ", start_handle: " << std::dec << m_startHandle << " 0x" << std::hex << m_startHandle << + ", end_handle: " << std::dec << m_endHandle << " 0x" << std::hex << m_endHandle; for (auto &myPair : m_characteristicMap) { ss << "\n" << myPair.second->toString(); // myPair.second is the value @@ -217,4 +348,6 @@ std::string BLERemoteService::toString() { + + #endif /* CONFIG_BT_ENABLED */ |