You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[on_read_observations_notify] data = 0x2001ce34
[on_read_observations_notify] len = 32
[on_read_observations_notify] err = 0
[on_read_observations_notify] value = 1
[00:00:26.929,565] <err> ble: on_read_observations_notify: OBSERVATION DATA:
30 2f 56 42 41 54 2f 28 38 32 2e 31 32 29 |0/VBAT/( 82.12)
In this case I'm sending a placeholder timestamp ("0"), a data descriptor ("VBAT": battery voltage as a percentage) and the actual value generated by the sensor (in this case 82.12).
My issue is that I can't copy that string out into a variable so I can reliably parse it.
I've jumped through multiple hoops to copy the buffer contents one character at a time. I then tokenize the resulting string into the values I want.
This works in every case EXCEPT one, where one of my sensors transmits a rather larger value. In that case, the string length gets calculated incorrectly (which is a mystery to me since I'm using strlen()), and nonsense data is being included in the final string because my method reads past the end of the actual buffer (using the value returned by strlen()).
...
if (length>0){
charobservation[length];
char*data_ptr=data;
inti=0;
for (; i<length; i++){
// only include characters with ascii values between 33 and 122if (data_ptr[i] >32||data_ptr<123){
if (data_ptr[i] ==41){
//If this is a right bracket, add it to the observation string and breakobservation[i] =data_ptr[i];
break;
}
else{
observation[i] =data_ptr[i];
}
}
else{
LOG_ERR("Found unwanted char : %c", data_ptr[i]);
observation[i] ="";
}
}
// Append str termination charobservation[length] ='\0';
/** tokenize the observation string **/char*token=NULL;
char*saveptr;
char*tokens[3];
for (inti=0; i<3; i++)
tokens[i] =NULL;
//get first token - timetoken=strtok_r(observation, "/", &saveptr);
intcount=0;
// process the restwhile (token!=NULL){
LOG_WRN("TOKEN[%d] = %s | STRLEN = %d", count, token, strlen(token));
tokens[count] = (char*) malloc(strlen(token));
sprintf(tokens[count], "%s", token);
token=strtok_r(NULL, "/", &saveptr);
count++;
}
As previously mentioned, there's noise in token[2]. Worse, the calculated strlen is 31, which is both incorrect (it should be 25) and impossible since the total strlen reported by the system to the callback function is 32.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been working with two nrf52840s to create observations and transmit over BLE.
This works fine - I can send and receive the data.
What I'm not seeing is a clear way to copy data OUT of the provided data buffer
void *data
once transmission is complete.For example:
This code shows me EXACTLY what I want:
In this case I'm sending a placeholder timestamp ("0"), a data descriptor ("VBAT": battery voltage as a percentage) and the actual value generated by the sensor (in this case 82.12).
My issue is that I can't copy that string out into a variable so I can reliably parse it.
I've jumped through multiple hoops to copy the buffer contents one character at a time. I then tokenize the resulting string into the values I want.
This works in every case EXCEPT one, where one of my sensors transmits a rather larger value. In that case, the string length gets calculated incorrectly (which is a mystery to me since I'm using
strlen()
), and nonsense data is being included in the final string because my method reads past the end of the actual buffer (using the value returned bystrlen()
).This results in the following:
As previously mentioned, there's noise in
token[2]
. Worse, the calculatedstrlen
is 31, which is both incorrect (it should be 25) and impossible since the total strlen reported by the system to the callback function is 32.There has to be an easier way to do this.
Can anyone shed light on this?
Beta Was this translation helpful? Give feedback.
All reactions