Skip to content

Stop copying read values in characteristic #26

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 8 additions & 15 deletions src/NimBLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ static const char* LOG_TAG = "NimBLERemoteCharacteristic";
*/
NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() {
removeDescriptors(); // Release resources for any descriptor information we may have allocated.
if(m_rawData != nullptr) free(m_rawData);
} // ~NimBLERemoteCharacteristic

/*
Expand Down Expand Up @@ -314,18 +313,19 @@ 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() {
NIMBLE_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle());

int rc = 0;
int retryCount = 1;
m_value = "";

NimBLEClient* pClient = getRemoteService()->getClient();

// Check to see that we are connected.
if (!pClient->isConnected()) {
NIMBLE_LOGE(LOG_TAG, "Disconnected");
return "";
return m_value;
}

do {
Expand All @@ -341,7 +341,7 @@ std::string NimBLERemoteCharacteristic::readValue() {
if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Error: Failed to read characteristic; rc=%d", rc);
m_semaphoreReadCharEvt.give();
return "";
return m_value;
}

rc = m_semaphoreReadCharEvt.wait("readValue");
Expand All @@ -356,12 +356,12 @@ std::string NimBLERemoteCharacteristic::readValue() {
break;
/* Else falls through. */
default:
return "";
return m_value;
}
} while(rc != 0 && retryCount--);

NIMBLE_LOGD(LOG_TAG, "<< readValue(): length: %d", m_value.length());
return (rc == 0) ? m_value : "";
return m_value;
} // readValue


Expand All @@ -387,17 +387,10 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
}
*/

if(characteristic->m_rawData != nullptr) {
free(characteristic->m_rawData);
}

if (error->status == 0) {
characteristic->m_value = std::string((char*) attr->om->om_data, attr->om->om_len);
characteristic->m_rawData = (uint8_t*) calloc(attr->om->om_len, sizeof(uint8_t));
memcpy(characteristic->m_rawData, attr->om->om_data, attr->om->om_len);
characteristic->m_semaphoreReadCharEvt.give(0);
} else {
characteristic->m_rawData = nullptr;
characteristic->m_value = "";
characteristic->m_semaphoreReadCharEvt.give(error->status);
}
Expand Down Expand Up @@ -615,8 +608,8 @@ int NimBLERemoteCharacteristic::onWriteCB(uint16_t conn_handle,
* @brief Read raw data from remote characteristic as hex bytes
* @return return pointer data read
*/
uint8_t* NimBLERemoteCharacteristic::readRawData() {
return m_rawData;
const uint8_t* NimBLERemoteCharacteristic::readRawData() {
return reinterpret_cast<const uint8_t*>(readValue().c_str());
}


Expand Down
5 changes: 2 additions & 3 deletions src/NimBLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NimBLERemoteCharacteristic {
uint16_t getHandle();
uint16_t getDefHandle();
NimBLEUUID getUUID();
std::string readValue();
std::string &readValue();
uint8_t readUInt8();
uint16_t readUInt16();
uint32_t readUInt32();
Expand All @@ -59,7 +59,7 @@ class NimBLERemoteCharacteristic {
bool writeValue(std::string newValue, bool response = false);
bool writeValue(uint8_t newValue, bool response = false);
std::string toString();
uint8_t* readRawData();
const uint8_t* readRawData();
NimBLERemoteService* getRemoteService();

private:
Expand Down Expand Up @@ -90,7 +90,6 @@ class NimBLERemoteCharacteristic {
FreeRTOS::Semaphore m_semaphoreReadCharEvt = FreeRTOS::Semaphore("ReadCharEvt");
FreeRTOS::Semaphore m_semaphoreWriteCharEvt = FreeRTOS::Semaphore("WriteCharEvt");
std::string m_value;
uint8_t* m_rawData = nullptr;
notify_callback m_notifyCallback;

// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
Expand Down