diff options
author | Neil Kolban <kolban1@kolban.com> | 2018-05-27 10:56:49 -0500 |
---|---|---|
committer | Neil Kolban <kolban1@kolban.com> | 2018-05-27 10:56:49 -0500 |
commit | e885eea75617598e3b1dff9e972d2e59f297ae28 (patch) | |
tree | 7244f26854294af16fef14605e321016f213d98a | |
parent | Fixes for BLE_notify sample (diff) | |
download | thermostat-e885eea75617598e3b1dff9e972d2e59f297ae28.tar.gz thermostat-e885eea75617598e3b1dff9e972d2e59f297ae28.tar.bz2 thermostat-e885eea75617598e3b1dff9e972d2e59f297ae28.zip |
0.4.13
-rw-r--r-- | library.properties | 2 | ||||
-rw-r--r-- | src/BLEAdvertisedDevice.cpp | 7 | ||||
-rw-r--r-- | src/BLEAdvertisedDevice.h | 5 | ||||
-rw-r--r-- | src/BLECharacteristic.cpp | 37 | ||||
-rw-r--r-- | src/BLECharacteristic.h | 5 | ||||
-rw-r--r-- | src/BLEClient.cpp | 11 | ||||
-rw-r--r-- | src/BLEScan.cpp | 15 | ||||
-rw-r--r-- | src/BLEScan.h | 3 | ||||
-rw-r--r-- | src/BLEServer.cpp | 12 | ||||
-rw-r--r-- | src/BLEServer.h | 7 | ||||
-rw-r--r-- | src/BLEService.cpp | 4 | ||||
-rw-r--r-- | src/BLEService.h | 1 | ||||
-rw-r--r-- | src/BLEServiceMap.cpp | 36 |
13 files changed, 121 insertions, 24 deletions
diff --git a/library.properties b/library.properties index af0b716..d99e2f1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP32 BLE Arduino -version=0.4.12 +version=0.4.13 author=Neil Kolban <kolban1@kolban.com> maintainer=Neil Kolban <kolban1@kolban.com> sentence=BLE functions for ESP32 diff --git a/src/BLEAdvertisedDevice.cpp b/src/BLEAdvertisedDevice.cpp index 351c5e1..67603df 100644 --- a/src/BLEAdvertisedDevice.cpp +++ b/src/BLEAdvertisedDevice.cpp @@ -234,6 +234,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) { uint8_t ad_type; uint8_t sizeConsumed = 0; bool finished = false; + setPayload(payload); while(!finished) { length = *payload; // Retrieve the length of the record. @@ -506,7 +507,13 @@ std::string BLEAdvertisedDevice::toString() { return ss.str(); } // toString +uint8_t* BLEAdvertisedDevice::getPayload() { + return m_payload; +} +void BLEAdvertisedDevice::setPayload(uint8_t* payload) { + m_payload = payload; +} #endif /* CONFIG_BT_ENABLED */ diff --git a/src/BLEAdvertisedDevice.h b/src/BLEAdvertisedDevice.h index 41bc4c6..a3b1e6e 100644 --- a/src/BLEAdvertisedDevice.h +++ b/src/BLEAdvertisedDevice.h @@ -39,9 +39,10 @@ public: BLEUUID getServiceDataUUID(); BLEUUID getServiceUUID(); int8_t getTXPower(); + uint8_t* getPayload(); - bool isAdvertisingService(BLEUUID uuid); + bool isAdvertisingService(BLEUUID uuid); bool haveAppearance(); bool haveManufacturerData(); bool haveName(); @@ -69,6 +70,7 @@ private: void setServiceUUID(const char* serviceUUID); void setServiceUUID(BLEUUID serviceUUID); void setTXPower(int8_t txPower); + void setPayload(uint8_t* payload); bool m_haveAppearance; @@ -92,6 +94,7 @@ private: int8_t m_txPower; std::string m_serviceData; BLEUUID m_serviceDataUUID; + uint8_t* m_payload; }; /** diff --git a/src/BLECharacteristic.cpp b/src/BLECharacteristic.cpp index 5e5aa2a..931c753 100644 --- a/src/BLECharacteristic.cpp +++ b/src/BLECharacteristic.cpp @@ -683,6 +683,43 @@ void BLECharacteristic::setValue(std::string value) { setValue((uint8_t*)(value.data()), value.length()); } // setValue +void BLECharacteristic::setValue(uint16_t& data16) { + uint8_t temp[2]; + temp[0]=data16; + temp[1]=data16>>8; + setValue(temp, 2); +} // setValue + +void BLECharacteristic::setValue(uint32_t& data32) { + uint8_t temp[4]; + temp[0]=data32; + temp[1]=data32>>8; + temp[2]=data32>>16; + temp[3]=data32>>24; + setValue(temp, 4); +} // setValue + +void BLECharacteristic::setValue(int& data32) { + uint8_t temp[4]; + temp[0]=data32; + temp[1]=data32>>8; + temp[2]=data32>>16; + temp[3]=data32>>24; + setValue(temp, 4); +} // setValue + +void BLECharacteristic::setValue(float& data32) { + uint8_t temp[4]; + *((float *)temp) = data32; + setValue(temp, 4); +} // setValue + +void BLECharacteristic::setValue(double& data64) { + uint8_t temp[8]; + *((double *)temp) = data64; + setValue(temp, 8); +} // setValue + /** * @brief Set the Write No Response property value. diff --git a/src/BLECharacteristic.h b/src/BLECharacteristic.h index 10dc787..b3f8d2e 100644 --- a/src/BLECharacteristic.h +++ b/src/BLECharacteristic.h @@ -75,6 +75,11 @@ public: void setReadProperty(bool value); void setValue(uint8_t* data, size_t size); void setValue(std::string value); + void setValue(uint16_t& data16); + void setValue(uint32_t& data32); + void setValue(int& data32); + void setValue(float& data32); + void setValue(double& data64); void setWriteProperty(bool value); void setWriteNoResponseProperty(bool value); std::string toString(); diff --git a/src/BLEClient.cpp b/src/BLEClient.cpp index 57ff4d2..f09cf91 100644 --- a/src/BLEClient.cpp +++ b/src/BLEClient.cpp @@ -77,6 +77,7 @@ void BLEClient::clearServices() { delete myPair.second; } m_servicesMap.clear(); + m_haveServices = false; ESP_LOGD(LOG_TAG, "<< clearServices"); } // clearServices @@ -111,7 +112,7 @@ bool BLEClient::connect(BLEAddress address) { getGattcIf(), *getPeerAddress().getNative(), // address BLE_ADDR_TYPE_PUBLIC, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature. - 1 // direct connection + true // direct connection ); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); @@ -166,6 +167,8 @@ void BLEClient::gattClientEventHandler( m_pClientCallbacks->onDisconnect(this); } m_isConnected = false; + m_semaphoreRssiCmplEvt.give(); + m_semaphoreSearchCmplEvt.give(1); break; } // ESP_GATTC_DISCONNECT_EVT @@ -213,7 +216,7 @@ void BLEClient::gattClientEventHandler( // - uint16_t conn_id // case ESP_GATTC_SEARCH_CMPL_EVT: { - m_semaphoreSearchCmplEvt.give(); + m_semaphoreSearchCmplEvt.give(0); break; } // ESP_GATTC_SEARCH_CMPL_EVT @@ -366,8 +369,8 @@ std::map<std::string, BLERemoteService*>* BLEClient::getServices() { ESP_LOGE(LOG_TAG, "esp_ble_gattc_search_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return &m_servicesMap; } - m_semaphoreSearchCmplEvt.wait("getServices"); - m_haveServices = true; // Remember that we now have services. + // If sucessfull, remember that we now have services. + m_haveServices = (m_semaphoreSearchCmplEvt.wait("getServices") == 0); ESP_LOGD(LOG_TAG, "<< getServices"); return &m_servicesMap; } // getServices diff --git a/src/BLEScan.cpp b/src/BLEScan.cpp index e450664..73086c6 100644 --- a/src/BLEScan.cpp +++ b/src/BLEScan.cpp @@ -74,6 +74,9 @@ void BLEScan::handleGAPEvent( // asked to stop. case ESP_GAP_SEARCH_INQ_CMPL_EVT: { m_stopped = true; + if (m_scanCompleteCB != nullptr) { + m_scanCompleteCB(m_scanResults); + } m_semaphoreScanEnd.give(); break; } // ESP_GAP_SEARCH_INQ_CMPL_EVT @@ -186,10 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) { /** * @brief Start scanning. * @param [in] duration The duration in seconds for which to scan. - * @return N/A. + * @param [in] scanCompleteCB A function to be called when scanning has completed. This can + * be supplied as nullptr (the default) in which case the call to start will block until scanning has + * been completed. + * @return The BLEScanResults. Only applicable if we are waiting for results. */ -BLEScanResults BLEScan::start(uint32_t duration) { +BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) { ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration); + m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes. m_semaphoreScanEnd.take(std::string("start")); @@ -213,7 +220,9 @@ BLEScanResults BLEScan::start(uint32_t duration) { m_stopped = false; - m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release. + if (m_scanCompleteCB == nullptr) { + m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release. + } ESP_LOGD(LOG_TAG, "<< start()"); return m_scanResults; diff --git a/src/BLEScan.h b/src/BLEScan.h index c905c43..3e53ce6 100644 --- a/src/BLEScan.h +++ b/src/BLEScan.h @@ -53,7 +53,7 @@ public: bool wantDuplicates = false); void setInterval(uint16_t intervalMSecs); void setWindow(uint16_t windowMSecs); - BLEScanResults start(uint32_t duration); + BLEScanResults start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults) = nullptr); void stop(); private: @@ -71,6 +71,7 @@ private: FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd"); BLEScanResults m_scanResults; bool m_wantDuplicates; + void (*m_scanCompleteCB)(BLEScanResults scanResults); }; // BLEScan #endif /* CONFIG_BT_ENABLED */ diff --git a/src/BLEServer.cpp b/src/BLEServer.cpp index 9d26eb5..8fbdd43 100644 --- a/src/BLEServer.cpp +++ b/src/BLEServer.cpp @@ -11,14 +11,12 @@ #include <esp_bt.h> #include <esp_bt_main.h> #include <esp_gap_ble_api.h> -//#include <esp_gatts_api.h> #include "BLEDevice.h" #include "BLEServer.h" #include "BLEService.h" #include "BLEUtils.h" #include <string.h> #include <string> -#include <gatt_api.h> #include <unordered_set> #ifdef ARDUINO_ARCH_ESP32 #include "esp32-hal-log.h" @@ -70,21 +68,23 @@ BLEService* BLEServer::createService(const char* uuid) { * of a new service. Every service must have a unique UUID. * @param [in] uuid The UUID of the new service. * @param [in] numHandles The maximum number of handles associated with this service. + * @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service. * @return A reference to the new service object. */ -BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) { +BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles, uint8_t inst_id) { ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str()); m_semaphoreCreateEvt.take("createService"); // Check that a service with the supplied UUID does not already exist. if (m_serviceMap.getByUUID(uuid) != nullptr) { - ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.", + ESP_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.", uuid.toString().c_str()); - m_semaphoreCreateEvt.give(); - return nullptr; + //m_semaphoreCreateEvt.give(); + //return nullptr; } BLEService* pService = new BLEService(uuid, numHandles); + pService->m_id = inst_id; m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server. pService->executeCreate(this); // Perform the API calls to actually create the service. diff --git a/src/BLEServer.h b/src/BLEServer.h index bc0ef05..6c71679 100644 --- a/src/BLEServer.h +++ b/src/BLEServer.h @@ -40,10 +40,13 @@ public: void setByUUID(const char* uuid, BLEService* service); void setByUUID(BLEUUID uuid, BLEService* service); std::string toString(); + BLEService* getFirst(); + BLEService* getNext(); private: - std::map<std::string, BLEService*> m_uuidMap; std::map<uint16_t, BLEService*> m_handleMap; + std::map<BLEService*, std::string> m_uuidMap; + std::map<BLEService*, std::string>::iterator m_iterator; }; @@ -54,7 +57,7 @@ class BLEServer { public: uint32_t getConnectedCount(); BLEService* createService(const char* uuid); - BLEService* createService(BLEUUID uuid, uint32_t numHandles=15); + BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0); BLEAdvertising* getAdvertising(); void setCallbacks(BLEServerCallbacks* pCallbacks); void startAdvertising(); diff --git a/src/BLEService.cpp b/src/BLEService.cpp index 4e59c4a..6636ca5 100644 --- a/src/BLEService.cpp +++ b/src/BLEService.cpp @@ -73,7 +73,7 @@ void BLEService::executeCreate(BLEServer *pServer) { esp_gatt_srvc_id_t srvc_id; srvc_id.is_primary = true; - srvc_id.id.inst_id = 0; + srvc_id.id.inst_id = m_id; srvc_id.id.uuid = *m_uuid.getNative(); esp_err_t errRc = ::esp_ble_gatts_create_service( getServer()->getGattsIf(), @@ -292,7 +292,7 @@ void BLEService::handleGATTServerEvent( // * - bool is_primary // case ESP_GATTS_CREATE_EVT: { - if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) { + if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) { setHandle(param->create.service_handle); m_semaphoreCreateEvt.give(); } diff --git a/src/BLEService.h b/src/BLEService.h index 2b78e62..79958f3 100644 --- a/src/BLEService.h +++ b/src/BLEService.h @@ -64,6 +64,7 @@ public: void start(); std::string toString(); uint16_t getHandle(); + uint8_t m_id = 0; private: BLEService(const char* uuid, uint32_t numHandles); diff --git a/src/BLEServiceMap.cpp b/src/BLEServiceMap.cpp index 8fdbd5a..96811ad 100644 --- a/src/BLEServiceMap.cpp +++ b/src/BLEServiceMap.cpp @@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) { */ BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) { for (auto &myPair : m_uuidMap) { - if (myPair.second->getUUID().equals(uuid)) { - return myPair.second; + if (myPair.first->getUUID().equals(uuid)) { + return myPair.first; } } //return m_uuidMap.at(uuid.toString()); @@ -54,7 +54,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) { */ void BLEServiceMap::setByUUID(BLEUUID uuid, BLEService *service) { - m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service)); + m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString())); } // setByUUID @@ -89,7 +89,35 @@ void BLEServiceMap::handleGATTServerEvent( esp_ble_gatts_cb_param_t *param) { // Invoke the handler for every Service we have. for (auto &myPair : m_uuidMap) { - myPair.second->handleGATTServerEvent(event, gatts_if, param); + myPair.first->handleGATTServerEvent(event, gatts_if, param); } } + +/** + * @brief Get the first service in the map. + * @return The first service in the map. + */ +BLEService* BLEServiceMap::getFirst() { + m_iterator = m_uuidMap.begin(); + if (m_iterator == m_uuidMap.end()) { + return nullptr; + } + BLEService* pRet = m_iterator->first; + m_iterator++; + return pRet; +} // getFirst + +/** + * @brief Get the next service in the map. + * @return The next service in the map. + */ +BLEService* BLEServiceMap::getNext() { + if (m_iterator == m_uuidMap.end()) { + return nullptr; + } + BLEService* pRet = m_iterator->first; + m_iterator++; + return pRet; +} // getNext + #endif /* CONFIG_BT_ENABLED */ |