Skip to content

Commit 6fbd861

Browse files
rbtchcAnas Nashif
authored andcommitted
net: lwm2m: fix reporting attributes with negative fraction
Fraction could be stored with negative value. The implementation was only considering the positive value case. Therefore, we have to modify the code to take care of the case. To test it ====================================================================== 1. launch eclipse/wakaama lwm2m server 2. launch zephyr lwm2m client and wait for registration completed 3. Issue commands from server * attr 0 /1/0/1 -0.1 0.1 * disc 0 /1/0 Current output ---------------------------------------------------------------------- Client #0 /1/0 : 2.05 (COAP_205_CONTENT) 105 bytes received of type application/link-format: </1/0>,</1/0/0>,</1/0/1>;gt=0.1;lt=0/00000,</1/0/2>,</1/0/3>,</1/0/4>, </1/0/5>,</1/0/6>,</1/0/7>,</1/0/8> Expected output ---------------------------------------------------------------------- Client #0 /1/0 : 2.05 (COAP_205_CONTENT) 102 bytes received of type application/link-format: </1/0>,</1/0/0>,</1/0/1>;gt=0.1;lt=-0.1,</1/0/2>,</1/0/3>,</1/0/4>, </1/0/5>,</1/0/6>,</1/0/7>,</1/0/8> Signed-off-by: Robert Chou <[email protected]>
1 parent abd5448 commit 6fbd861

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,13 +2725,17 @@ static int print_attr(struct net_pkt *pkt, char *buf, u16_t buflen,
27252725

27262726
SYS_SLIST_FOR_EACH_CONTAINER(attr_list, attr, node) {
27272727
/* assuming integer will have float_val.val2 set as 0 */
2728-
used = snprintk(buf, buflen, ";%s=%d%s",
2728+
2729+
used = snprintk(buf, buflen, ";%s=%s%d%s",
27292730
LWM2M_ATTR_STR[attr->type],
2731+
attr->float_val.val1 == 0 &&
2732+
attr->float_val.val2 < 0 ? "-" : "",
27302733
attr->float_val.val1,
2731-
attr->float_val.val2 > 0 ? "." : "");
2734+
attr->float_val.val2 != 0 ? "." : "");
27322735

27332736
base = 100000;
2734-
fraction = attr->float_val.val2;
2737+
fraction = attr->float_val.val2 < 0 ?
2738+
-attr->float_val.val2 : attr->float_val.val2;
27352739
while (fraction && used < buflen && base > 0) {
27362740
digit = fraction / base;
27372741
buf[used++] = '0' + digit;

0 commit comments

Comments
 (0)