net: lwm2m: Replace float32_value_t with double
Replace the custom float32_value_t LwM2M type with native double, to facilitate LwM2M API and improve floating point precission. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
parent
3a3e3c041b
commit
875c75c4f9
22 changed files with 344 additions and 432 deletions
|
@ -86,9 +86,9 @@ struct observe_node {
|
|||
|
||||
struct notification_attrs {
|
||||
/* use to determine which value is set */
|
||||
float32_value_t gt;
|
||||
float32_value_t lt;
|
||||
float32_value_t st;
|
||||
double gt;
|
||||
double lt;
|
||||
double st;
|
||||
int32_t pmin;
|
||||
int32_t pmax;
|
||||
uint8_t flags;
|
||||
|
@ -1596,10 +1596,7 @@ static int lwm2m_engine_set(char *pathstr, void *value, uint16_t len)
|
|||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_FLOAT:
|
||||
((float32_value_t *)data_ptr)->val1 =
|
||||
((float32_value_t *)value)->val1;
|
||||
((float32_value_t *)data_ptr)->val2 =
|
||||
((float32_value_t *)value)->val2;
|
||||
*(double *)data_ptr = *(double *)value;
|
||||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_OBJLNK:
|
||||
|
@ -1685,9 +1682,9 @@ int lwm2m_engine_set_bool(char *pathstr, bool value)
|
|||
return lwm2m_engine_set(pathstr, &temp, 1);
|
||||
}
|
||||
|
||||
int lwm2m_engine_set_float32(char *pathstr, float32_value_t *value)
|
||||
int lwm2m_engine_set_float(char *pathstr, double *value)
|
||||
{
|
||||
return lwm2m_engine_set(pathstr, value, sizeof(float32_value_t));
|
||||
return lwm2m_engine_set(pathstr, value, sizeof(double));
|
||||
}
|
||||
|
||||
int lwm2m_engine_set_objlnk(char *pathstr, struct lwm2m_objlnk *value)
|
||||
|
@ -1830,10 +1827,7 @@ static int lwm2m_engine_get(char *pathstr, void *buf, uint16_t buflen)
|
|||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_FLOAT:
|
||||
((float32_value_t *)buf)->val1 =
|
||||
((float32_value_t *)data_ptr)->val1;
|
||||
((float32_value_t *)buf)->val2 =
|
||||
((float32_value_t *)data_ptr)->val2;
|
||||
*(double *)buf = *(double *)data_ptr;
|
||||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_OBJLNK:
|
||||
|
@ -1915,9 +1909,9 @@ int lwm2m_engine_get_bool(char *pathstr, bool *value)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int lwm2m_engine_get_float32(char *pathstr, float32_value_t *buf)
|
||||
int lwm2m_engine_get_float(char *pathstr, double *buf)
|
||||
{
|
||||
return lwm2m_engine_get(pathstr, buf, sizeof(float32_value_t));
|
||||
return lwm2m_engine_get(pathstr, buf, sizeof(double));
|
||||
}
|
||||
|
||||
int lwm2m_engine_get_objlnk(char *pathstr, struct lwm2m_objlnk *buf)
|
||||
|
@ -2325,8 +2319,8 @@ static int lwm2m_read_handler(struct lwm2m_engine_obj_inst *obj_inst,
|
|||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_FLOAT:
|
||||
engine_put_float32fix(&msg->out, &msg->path,
|
||||
(float32_value_t *)data_ptr);
|
||||
engine_put_float(&msg->out, &msg->path,
|
||||
(double *)data_ptr);
|
||||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_OBJLNK:
|
||||
|
@ -2594,9 +2588,8 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
|
|||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_FLOAT:
|
||||
engine_get_float32fix(&msg->in,
|
||||
(float32_value_t *)write_buf);
|
||||
len = sizeof(float32_value_t);
|
||||
engine_get_float(&msg->in, (double *)write_buf);
|
||||
len = sizeof(double);
|
||||
break;
|
||||
|
||||
case LWM2M_RES_TYPE_OBJLNK:
|
||||
|
@ -2725,7 +2718,7 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
/* loop through options to parse attribute */
|
||||
for (i = 0; i < nr_opt; i++) {
|
||||
int limit = MIN(options[i].len, 5), plen = 0, vlen;
|
||||
float32_value_t val = { 0 };
|
||||
struct lwm2m_attr val = { 0 };
|
||||
type = 0U;
|
||||
|
||||
/* search for '=' */
|
||||
|
@ -2758,7 +2751,7 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
|
||||
(void)memset(nattr_ptrs[type], 0,
|
||||
type <= LWM2M_ATTR_PMAX ? sizeof(int32_t) :
|
||||
sizeof(float32_value_t));
|
||||
sizeof(double));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -2785,10 +2778,10 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
ret = -EINVAL;
|
||||
}
|
||||
|
||||
val.val1 = v;
|
||||
val.int_val = v;
|
||||
} else {
|
||||
/* gt/lt/st: type float */
|
||||
ret = lwm2m_atof32(opt_buf, &val);
|
||||
ret = lwm2m_atof(opt_buf, &val.float_val);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
|
@ -2799,9 +2792,10 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
}
|
||||
|
||||
if (type <= LWM2M_ATTR_PMAX) {
|
||||
*(int32_t *)nattr_ptrs[type] = val.val1;
|
||||
*(int32_t *)nattr_ptrs[type] = val.int_val;
|
||||
} else {
|
||||
memcpy(nattr_ptrs[type], &val, sizeof(float32_value_t));
|
||||
memcpy(nattr_ptrs[type], &val.float_val,
|
||||
sizeof(val.float_val));
|
||||
}
|
||||
|
||||
nattrs.flags |= BIT(type);
|
||||
|
@ -2814,18 +2808,13 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
}
|
||||
|
||||
if (nattrs.flags & (BIT(LWM2M_ATTR_LT) | BIT(LWM2M_ATTR_GT))) {
|
||||
if (!((nattrs.lt.val1 < nattrs.gt.val1) ||
|
||||
(nattrs.lt.val2 < nattrs.gt.val2))) {
|
||||
if (nattrs.lt > nattrs.gt) {
|
||||
LOG_DBG("lt > gt");
|
||||
return -EEXIST;
|
||||
}
|
||||
|
||||
if (nattrs.flags & BIT(LWM2M_ATTR_STEP)) {
|
||||
int32_t st1 = nattrs.st.val1 * 2 +
|
||||
nattrs.st.val2 * 2 / 1000000;
|
||||
int32_t st2 = nattrs.st.val2 * 2 % 1000000;
|
||||
if (!(((nattrs.lt.val1 + st1) < nattrs.gt.val1) ||
|
||||
((nattrs.lt.val2 + st2) < nattrs.gt.val2))) {
|
||||
if (nattrs.lt + 2 * nattrs.st > nattrs.gt) {
|
||||
LOG_DBG("lt + 2*st > gt");
|
||||
return -EEXIST;
|
||||
}
|
||||
|
@ -2862,19 +2851,19 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
|
||||
attr->int_val = *(int32_t *)nattr_ptrs[type];
|
||||
update_observe_node = true;
|
||||
|
||||
LOG_DBG("Update %s to %d", log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->int_val);
|
||||
} else {
|
||||
if (!memcmp(&attr->float_val, nattr_ptrs[type],
|
||||
sizeof(float32_value_t))) {
|
||||
if (attr->float_val == *(double *)nattr_ptrs[type]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(&attr->float_val, nattr_ptrs[type],
|
||||
sizeof(float32_value_t));
|
||||
}
|
||||
attr->float_val = *(double *)nattr_ptrs[type];
|
||||
|
||||
LOG_DBG("Update %s to %d.%06d",
|
||||
log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->float_val.val1, attr->float_val.val2);
|
||||
LOG_DBG("Update %s to %f", log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->float_val);
|
||||
}
|
||||
}
|
||||
|
||||
/* add attribute to obj/obj_inst/res */
|
||||
|
@ -2901,14 +2890,17 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
|
|||
if (type <= LWM2M_ATTR_PMAX) {
|
||||
attr->int_val = *(int32_t *)nattr_ptrs[type];
|
||||
update_observe_node = true;
|
||||
|
||||
LOG_DBG("Add %s to %d", log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->int_val);
|
||||
} else {
|
||||
memcpy(&attr->float_val, nattr_ptrs[type],
|
||||
sizeof(float32_value_t));
|
||||
attr->float_val = *(double *)nattr_ptrs[type];
|
||||
|
||||
LOG_DBG("Add %s to %f", log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->float_val);
|
||||
}
|
||||
|
||||
nattrs.flags &= ~BIT(type);
|
||||
LOG_DBG("Add %s to %d.%06d", log_strdup(LWM2M_ATTR_STR[type]),
|
||||
attr->float_val.val1, attr->float_val.val2);
|
||||
}
|
||||
|
||||
/* check only pmin/pmax */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue