Skip to content

Commit

Permalink
net: lwm2m: Allow setting string to zero length
Browse files Browse the repository at this point in the history
Lwm2m firmware object have defined a write of zero length
string as a cancel operation.
So allow lwm2m_set_opaque(path, NULL, 0);

Signed-off-by: Seppo Takalo <[email protected]>
  • Loading branch information
SeppoTakalo authored and fabiobaltieri committed Jun 27, 2023
1 parent a7d1110 commit 6050a10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
14 changes: 10 additions & 4 deletions subsys/net/lib/lwm2m/lwm2m_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ static int lwm2m_engine_set(const struct lwm2m_obj_path *path, const void *value
return ret;
}

if (memcmp(data_ptr, value, len) != 0) {
if (memcmp(data_ptr, value, len) != 0 || res_inst->data_len != len) {
changed = true;
}

Expand All @@ -644,12 +644,18 @@ static int lwm2m_engine_set(const struct lwm2m_obj_path *path, const void *value
switch (obj_field->data_type) {

case LWM2M_RES_TYPE_OPAQUE:
memcpy((uint8_t *)data_ptr, value, len);
if (len) {
memcpy((uint8_t *)data_ptr, value, len);
}
break;

case LWM2M_RES_TYPE_STRING:
strncpy(data_ptr, value, len - 1);
((char *)data_ptr)[len - 1] = '\0';
if (len) {
strncpy(data_ptr, value, len - 1);
((char *)data_ptr)[len - 1] = '\0';
} else {
((char *)data_ptr)[0] = '\0';
}
break;

case LWM2M_RES_TYPE_U32:
Expand Down
34 changes: 34 additions & 0 deletions tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,37 @@ ZTEST(lwm2m_registry, test_strings)
ret = lwm2m_get_string(&path, buf, len);
zassert_equal(ret, -ENOMEM);
}

ZTEST(lwm2m_registry, test_null_strings)
{
int ret;
char buf[256] = {0};
struct lwm2m_obj_path path = LWM2M_OBJ(0, 0, 0);

ret = lwm2m_register_post_write_callback(&path, post_write_cb);
zassert_equal(ret, 0);

callback_checker = 0;
ret = lwm2m_set_string(&path, "string");
zassert_equal(ret, 0);
zassert_equal(callback_checker, 0x02);
ret = lwm2m_get_string(&path, buf, sizeof(buf));
zassert_equal(ret, 0);
zassert_equal(strlen(buf), strlen("string"));

callback_checker = 0;
ret = lwm2m_set_string(&path, "");
zassert_equal(ret, 0);
zassert_equal(callback_checker, 0x02);
ret = lwm2m_get_string(&path, buf, sizeof(buf));
zassert_equal(ret, 0);
zassert_equal(strlen(buf), 0);

callback_checker = 0;
ret = lwm2m_set_opaque(&path, NULL, 0);
zassert_equal(ret, 0);
zassert_equal(callback_checker, 0x02);
ret = lwm2m_get_string(&path, buf, sizeof(buf));
zassert_equal(ret, 0);
zassert_equal(strlen(buf), 0);
}

0 comments on commit 6050a10

Please sign in to comment.