net: lwm2m: Fix plain text floating point handling
Floating point parser for plain text format was parsing floats wrongly, ignoring leading zeros after decimal points. Fix this, by reusing atof32() function, already avaialbe in a different part of the engine, which did the parsing correctly. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
parent
b6f69a6df1
commit
20a4d181e1
4 changed files with 95 additions and 71 deletions
|
@ -43,6 +43,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|||
#include "lwm2m_rw_link_format.h"
|
||||
#include "lwm2m_rw_plain_text.h"
|
||||
#include "lwm2m_rw_oma_tlv.h"
|
||||
#include "lwm2m_util.h"
|
||||
#ifdef CONFIG_LWM2M_RW_JSON_SUPPORT
|
||||
#include "lwm2m_rw_json.h"
|
||||
#endif
|
||||
|
@ -829,50 +830,6 @@ static uint16_t atou16(uint8_t *buf, uint16_t buflen, uint16_t *len)
|
|||
return val;
|
||||
}
|
||||
|
||||
static int atof32(const char *input, float32_value_t *out)
|
||||
{
|
||||
char *pos, *end, buf[24];
|
||||
long int val;
|
||||
int32_t base = 1000000, sign = 1;
|
||||
|
||||
if (!input || !out) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
strncpy(buf, input, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
|
||||
if (strchr(buf, '-')) {
|
||||
sign = -1;
|
||||
}
|
||||
|
||||
pos = strchr(buf, '.');
|
||||
if (pos) {
|
||||
*pos = '\0';
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
val = strtol(buf, &end, 10);
|
||||
if (errno || *end || val < INT_MIN) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
out->val1 = (int32_t) val;
|
||||
out->val2 = 0;
|
||||
|
||||
if (!pos) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*(++pos) && base > 1 && isdigit((unsigned char)*pos)) {
|
||||
out->val2 = out->val2 * 10 + (*pos - '0');
|
||||
base /= 10;
|
||||
}
|
||||
|
||||
out->val2 *= sign * base;
|
||||
return !*pos || base == 1 ? 0 : -EINVAL;
|
||||
}
|
||||
|
||||
static int coap_options_to_path(struct coap_option *opt, int options_count,
|
||||
struct lwm2m_obj_path *path)
|
||||
{
|
||||
|
@ -2831,7 +2788,7 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
val.val1 = v;
|
||||
} else {
|
||||
/* gt/lt/st: type float */
|
||||
ret = atof32(opt_buf, &val);
|
||||
ret = lwm2m_atof32(opt_buf, &val);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue