mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-21 20:50:55 +01:00
Add template casting to readValue and advertisement data (#52)
The value returned by reading a remote characteristic or by getting a notification for it is kept in the class instance of the NimBLERemoteCharacteristic. This value can be accessed as a std::string type using the getValue() function. This adds templates to read the value in the type used by the peripheral. The same functionality is implemented for getting the manufacturer data or the service data of an advertised device.
This commit is contained in:
parent
10f544f80a
commit
99ad62cdd4
4 changed files with 52 additions and 6 deletions
|
@ -45,10 +45,28 @@ public:
|
||||||
uint8_t getAdvType();
|
uint8_t getAdvType();
|
||||||
uint16_t getAppearance();
|
uint16_t getAppearance();
|
||||||
std::string getManufacturerData();
|
std::string getManufacturerData();
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getManufacturerData(bool skipSizeCheck = false) {
|
||||||
|
std::string data = getManufacturerData();
|
||||||
|
if(!skipSizeCheck && data.size() < sizeof(T)) return T();
|
||||||
|
const char *pData = data.data();
|
||||||
|
return *((T *)pData);
|
||||||
|
}
|
||||||
|
|
||||||
std::string getName();
|
std::string getName();
|
||||||
int getRSSI();
|
int getRSSI();
|
||||||
NimBLEScan* getScan();
|
NimBLEScan* getScan();
|
||||||
std::string getServiceData();
|
std::string getServiceData();
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getServiceData(bool skipSizeCheck = false) {
|
||||||
|
std::string data = getServiceData();
|
||||||
|
if(!skipSizeCheck && data.size() < sizeof(T)) return T();
|
||||||
|
const char *pData = data.data();
|
||||||
|
return *((T *)pData);
|
||||||
|
}
|
||||||
|
|
||||||
NimBLEUUID getServiceDataUUID();
|
NimBLEUUID getServiceDataUUID();
|
||||||
NimBLEUUID getServiceUUID();
|
NimBLEUUID getServiceUUID();
|
||||||
int8_t getTXPower();
|
int8_t getTXPower();
|
||||||
|
|
|
@ -725,6 +725,7 @@ uint16_t NimBLEClient::getMTU() {
|
||||||
(*characteristic)->m_timestamp = time(nullptr);
|
(*characteristic)->m_timestamp = time(nullptr);
|
||||||
(*characteristic)->m_semaphoreReadCharEvt.give();
|
(*characteristic)->m_semaphoreReadCharEvt.give();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*characteristic)->m_notifyCallback != nullptr) {
|
if ((*characteristic)->m_notifyCallback != nullptr) {
|
||||||
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
|
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
|
||||||
(*characteristic)->toString().c_str());
|
(*characteristic)->toString().c_str());
|
||||||
|
|
|
@ -384,7 +384,7 @@ uint8_t NimBLERemoteCharacteristic::readUInt8() {
|
||||||
* @brief Read the value of the remote characteristic.
|
* @brief Read the value of the remote characteristic.
|
||||||
* @return The value of the remote characteristic.
|
* @return The value of the remote characteristic.
|
||||||
*/
|
*/
|
||||||
std::string NimBLERemoteCharacteristic::readValue() {
|
std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
|
||||||
NIMBLE_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x",
|
NIMBLE_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x",
|
||||||
getUUID().toString().c_str(), getHandle(), getHandle());
|
getUUID().toString().c_str(), getHandle(), getHandle());
|
||||||
|
|
||||||
|
@ -437,7 +437,15 @@ std::string NimBLERemoteCharacteristic::readValue() {
|
||||||
} while(rc != 0 && retryCount--);
|
} while(rc != 0 && retryCount--);
|
||||||
|
|
||||||
NIMBLE_LOGD(LOG_TAG, "<< readValue(): length: %d", m_value.length());
|
NIMBLE_LOGD(LOG_TAG, "<< readValue(): length: %d", m_value.length());
|
||||||
return m_value;
|
|
||||||
|
m_semaphoreReadCharEvt.take("returnValue");
|
||||||
|
std::string value = m_value;
|
||||||
|
if(timestamp != nullptr) {
|
||||||
|
*timestamp = m_timestamp;
|
||||||
|
}
|
||||||
|
m_semaphoreReadCharEvt.give();
|
||||||
|
|
||||||
|
return value;
|
||||||
} // readValue
|
} // readValue
|
||||||
|
|
||||||
|
|
||||||
|
@ -451,6 +459,7 @@ std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
|
||||||
if(timestamp != nullptr) {
|
if(timestamp != nullptr) {
|
||||||
*timestamp = m_timestamp;
|
*timestamp = m_timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_semaphoreReadCharEvt.give();
|
m_semaphoreReadCharEvt.give();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,29 @@ public:
|
||||||
uint16_t getHandle();
|
uint16_t getHandle();
|
||||||
uint16_t getDefHandle();
|
uint16_t getDefHandle();
|
||||||
NimBLEUUID getUUID();
|
NimBLEUUID getUUID();
|
||||||
std::string readValue();
|
std::string readValue(time_t *timestamp = nullptr);
|
||||||
uint8_t readUInt8();
|
|
||||||
uint16_t readUInt16();
|
template<typename T>
|
||||||
uint32_t readUInt32();
|
T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
||||||
|
std::string value = readValue(timestamp);
|
||||||
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
||||||
|
const char *pData = value.data();
|
||||||
|
return *((T *)pData);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t readUInt8() __attribute__ ((deprecated));
|
||||||
|
uint16_t readUInt16() __attribute__ ((deprecated));
|
||||||
|
uint32_t readUInt32() __attribute__ ((deprecated));
|
||||||
std::string getValue(time_t *timestamp = nullptr);
|
std::string getValue(time_t *timestamp = nullptr);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
||||||
|
std::string value = getValue(timestamp);
|
||||||
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
||||||
|
const char *pData = value.data();
|
||||||
|
return *((T *)pData);
|
||||||
|
}
|
||||||
|
|
||||||
bool registerForNotify(notify_callback _callback,
|
bool registerForNotify(notify_callback _callback,
|
||||||
bool notifications = true,
|
bool notifications = true,
|
||||||
bool response = true);
|
bool response = true);
|
||||||
|
|
Loading…
Reference in a new issue