From dd71e3e11a122172459ac0983a8c5dd65daf11b0 Mon Sep 17 00:00:00 2001 From: Julio Contreras Fuica <50180934+jcontrerasf@users.noreply.github.com> Date: Sat, 25 Nov 2023 11:23:37 -0300 Subject: [PATCH] Add generic advertisement 'type' functions (#575) --- src/NimBLEAdvertisedDevice.cpp | 26 ++++++++++++++++++++++++++ src/NimBLEAdvertisedDevice.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/NimBLEAdvertisedDevice.cpp b/src/NimBLEAdvertisedDevice.cpp index 0c38ca4..d427787 100644 --- a/src/NimBLEAdvertisedDevice.cpp +++ b/src/NimBLEAdvertisedDevice.cpp @@ -203,6 +203,24 @@ std::string NimBLEAdvertisedDevice::getURI() { return ""; } // getURI +/** + * @brief Get the data from any type available in the advertisement + * @param [in] type The advertised data type BLE_HS_ADV_TYPE + * @return The data available under the type `type` +*/ +std::string NimBLEAdvertisedDevice::getPayloadByType(uint16_t type) { + size_t data_loc = 0; + + if(findAdvField(type, 0, &data_loc) > 0) { + ble_hs_adv_field *field = (ble_hs_adv_field *)&m_payload[data_loc]; + if(field->length > 1) { + return std::string((char*)field->value, field->length - 1); + } + } + + return ""; +} // getPayloadByType + /** * @brief Get the advertised name. @@ -556,6 +574,14 @@ bool NimBLEAdvertisedDevice::haveURI() { return findAdvField(BLE_HS_ADV_TYPE_URI) > 0; } // haveURI +/** + * @brief Does this advertisement have a adv type `type`? + * @return True if there is a `type` present. +*/ +bool NimBLEAdvertisedDevice::haveType(uint16_t type) { + return findAdvField(type) > 0; +} + /** * @brief Does the advertisement contain a target address? diff --git a/src/NimBLEAdvertisedDevice.h b/src/NimBLEAdvertisedDevice.h index 43d59eb..71b99b2 100644 --- a/src/NimBLEAdvertisedDevice.h +++ b/src/NimBLEAdvertisedDevice.h @@ -53,6 +53,7 @@ public: uint8_t getManufacturerDataCount(); std::string getManufacturerData(uint8_t index = 0); std::string getURI(); + std::string getPayloadByType(uint16_t type); /** * @brief A template to convert the service data to . @@ -134,6 +135,7 @@ public: bool haveAdvInterval(); bool haveTargetAddress(); bool haveURI(); + bool haveType(uint16_t type); std::string toString(); bool isConnectable(); bool isLegacyAdvertisement();