Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Template cast (uses PR #49) #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/NimBLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,28 @@ class NimBLEAdvertisedDevice {
uint8_t getAdvType();
uint16_t getAppearance();
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();
int getRSSI();
NimBLEScan* getScan();
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 getServiceUUID();
int8_t getTXPower();
Expand Down
1 change: 1 addition & 0 deletions src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ uint16_t NimBLEClient::getMTU() {
(*characteristic)->m_timestamp = time(nullptr);
(*characteristic)->m_semaphoreReadCharEvt.give();
}

if ((*characteristic)->m_notifyCallback != nullptr) {
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
(*characteristic)->toString().c_str());
Expand Down
13 changes: 11 additions & 2 deletions src/NimBLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ uint8_t NimBLERemoteCharacteristic::readUInt8() {
* @brief Read 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",
getUUID().toString().c_str(), getHandle(), getHandle());

Expand Down Expand Up @@ -437,7 +437,15 @@ std::string NimBLERemoteCharacteristic::readValue() {
} while(rc != 0 && retryCount--);

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


Expand All @@ -451,6 +459,7 @@ std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
if(timestamp != nullptr) {
*timestamp = m_timestamp;
}

m_semaphoreReadCharEvt.give();
return value;
}
Expand Down
26 changes: 22 additions & 4 deletions src/NimBLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,29 @@ class NimBLERemoteCharacteristic {
uint16_t getHandle();
uint16_t getDefHandle();
NimBLEUUID getUUID();
std::string readValue();
uint8_t readUInt8();
uint16_t readUInt16();
uint32_t readUInt32();
std::string readValue(time_t *timestamp = nullptr);

template<typename T>
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);

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 notifications = true,
bool response = true);
Expand Down