summaryrefslogtreecommitdiff
path: root/ESP32_BLE_Arduino/src/BLERemoteService.cpp
diff options
context:
space:
mode:
authorkolban <kolban1@kolban.com>2017-09-10 13:36:10 -0500
committerkolban <kolban1@kolban.com>2017-09-10 13:36:10 -0500
commit5e5b5b78f0aeca611edb52ff4e885334b6fe46a9 (patch)
tree8d207806891e66ee6ea292d82eb7f120e2138418 /ESP32_BLE_Arduino/src/BLERemoteService.cpp
downloadthermostat-5e5b5b78f0aeca611edb52ff4e885334b6fe46a9.tar.gz
thermostat-5e5b5b78f0aeca611edb52ff4e885334b6fe46a9.tar.bz2
thermostat-5e5b5b78f0aeca611edb52ff4e885334b6fe46a9.zip
0.1.0 release
Diffstat (limited to 'ESP32_BLE_Arduino/src/BLERemoteService.cpp')
-rw-r--r--ESP32_BLE_Arduino/src/BLERemoteService.cpp220
1 files changed, 220 insertions, 0 deletions
diff --git a/ESP32_BLE_Arduino/src/BLERemoteService.cpp b/ESP32_BLE_Arduino/src/BLERemoteService.cpp
new file mode 100644
index 0000000..a29a758
--- /dev/null
+++ b/ESP32_BLE_Arduino/src/BLERemoteService.cpp
@@ -0,0 +1,220 @@
+/*
+ * BLERemoteService.cpp
+ *
+ * Created on: Jul 8, 2017
+ * Author: kolban
+ */
+#include "sdkconfig.h"
+#if defined(CONFIG_BT_ENABLED)
+
+#include <sstream>
+#include "BLERemoteService.h"
+#include "BLEUtils.h"
+#include "GeneralUtils.h"
+#include <esp_log.h>
+#include <esp_err.h>
+
+static const char* LOG_TAG = "BLERemoteService";
+
+BLERemoteService::BLERemoteService(
+ esp_gatt_srvc_id_t srvcId,
+ BLEClient *pClient) {
+
+ m_srvcId = srvcId;
+ m_pClient = pClient;
+ m_uuid = BLEUUID(m_srvcId);
+ m_haveCharacteristics = false;
+}
+
+
+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;
+ }
+ if (!BLEUUID(id1.id.uuid).equals(BLEUUID(id2.id.uuid))) {
+ return false;
+ }
+ return true;
+} // compareSrvcId
+
+
+/**
+ * @brief Handle GATT Client events
+ */
+void BLERemoteService::gattClientEventHandler(
+ esp_gattc_cb_event_t event,
+ esp_gatt_if_t gattc_if,
+ esp_ble_gattc_cb_param_t *evtParam) {
+ switch(event) {
+ //
+ // ESP_GATTC_GET_CHAR_EVT
+ //
+ // get_char:
+ // - esp_gatt_status_t status
+ // - uin1t6_t conn_id
+ // - esp_gatt_srvc_id_t srvc_id
+ // - 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.
+ if (compareSrvcId(m_srvcId, evtParam->get_char.srvc_id) == false) {
+ break;
+ }
+
+ // If the status is NOT OK then we have a problem and continue.
+ if (evtParam->get_char.status != ESP_GATT_OK) {
+ m_semaphoreGetCharEvt.give();
+ break;
+ }
+
+ // This is an indication that we now have the characteristic details for a characteristic owned
+ // by this service so remember it.
+ m_characteristicMap.insert(std::pair<std::string, BLERemoteCharacteristic*>(
+ BLEUUID(evtParam->get_char.char_id.uuid).toString(),
+ new BLERemoteCharacteristic(evtParam->get_char.char_id, evtParam->get_char.char_prop, this) ));
+
+
+ // Now that we have received a characteristic, lets ask for the next one.
+ esp_err_t errRc = ::esp_ble_gattc_get_characteristic(
+ m_pClient->getGattcIf(),
+ m_pClient->getConnId(),
+ &m_srvcId,
+ &evtParam->get_char.char_id);
+ if (errRc != ESP_OK) {
+ ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_characteristic: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
+ break;
+ }
+
+ //m_semaphoreGetCharEvt.give();
+ break;
+ } // ESP_GATTC_GET_CHAR_EVT
+
+ default: {
+ break;
+ }
+ } // switch
+
+ // Send the event to each of the characteristics owned by this service.
+ for (auto &myPair : m_characteristicMap) {
+ myPair.second->gattClientEventHandler(event, gattc_if, evtParam);
+ }
+} // gattClientEventHandler
+
+
+/**
+ * @brief Get the characteristic object for the UUID.
+ * @param [in] uuid Characteristic uuid.
+ * @return Reference to the characteristic object.
+ */
+BLERemoteCharacteristic* BLERemoteService::getCharacteristic(const char* uuid) {
+ return getCharacteristic(BLEUUID(uuid));
+}
+
+
+/**
+ * @brief Get the characteristic object for the UUID.
+ * @param [in] uuid Characteristic uuid.
+ * @return Reference to the characteristic object.
+ */
+BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
+// Design
+// ------
+// We wish to retrieve the characteristic given its UUID. It is possible that we have not yet asked the
+// device what characteristics it has in which case we have nothing to match against. If we have not
+// 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();
+ }
+ std::string v = uuid.toString();
+ for (auto &myPair : m_characteristicMap) {
+ if (myPair.first == v) {
+ return myPair.second;
+ }
+ }
+ return nullptr;
+} // getCharacteristic
+
+
+/**
+ * @brief Retrieve all the characteristics for this service.
+ * @return N/A
+ */
+void BLERemoteService::getCharacteristics() {
+
+ 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(
+ m_pClient->getGattcIf(),
+ m_pClient->getConnId(),
+ &m_srvcId,
+ nullptr);
+
+ if (errRc != ESP_OK) {
+ ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_characteristic: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
+ return;
+ }
+
+ m_semaphoreGetCharEvt.wait("getCharacteristics"); // Wait for the characteristics to become available.
+
+ m_haveCharacteristics = true; // Remember that we have received the characteristics.
+ ESP_LOGD(LOG_TAG, "<< getCharacteristics()");
+} // getCharacteristics
+
+
+BLEClient* BLERemoteService::getClient() {
+ return m_pClient;
+}
+
+esp_gatt_srvc_id_t* BLERemoteService::getSrvcId() {
+ return &m_srvcId;
+}
+
+BLEUUID BLERemoteService::getUUID() {
+ return m_uuid;
+}
+
+
+/**
+ * @brief Delete the characteristics in the characteristics map.
+ * We maintain a map called m_characteristicsMap that contains pointers to BLERemoteCharacteristic
+ * object references. Since we allocated these in this class, we are also responsible for deleteing
+ * them. This method does just that.
+ * @return N/A.
+ */
+void BLERemoteService::removeCharacteristics() {
+ for (auto &myPair : m_characteristicMap) {
+ delete myPair.second;
+ }
+ m_characteristicMap.empty();
+} // removeCharacteristics
+
+
+
+/**
+ * @brief Create a string representation of this remote service.
+ * @return A string representation of this remote service.
+ */
+std::string BLERemoteService::toString() {
+ std::ostringstream ss;
+ ss << "Service: uuid: " + m_uuid.toString();
+ for (auto &myPair : m_characteristicMap) {
+ ss << "\n" << myPair.second->toString();
+ // myPair.second is the value
+ }
+ return ss.str();
+} // toString
+
+
+
+#endif /* CONFIG_BT_ENABLED */