Skip to content

Commit 645e62f

Browse files
walzsijhedberg
authored andcommitted
tests: lwm2m: add test for senml cbor composite writer
This adds a test for the composite writer with time serialized resource and resource instance values. Signed-off-by: Simon Walz <[email protected]>
1 parent fd780ae commit 645e62f

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

tests/net/lib/lwm2m/content_senml_cbor/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ CONFIG_LWM2M_VERSION_1_1=y
99
CONFIG_LWM2M_RW_CBOR_SUPPORT=y
1010
CONFIG_LWM2M_RW_SENML_CBOR_SUPPORT=y
1111
CONFIG_ZCBOR_CANONICAL=y
12+
CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT=y
13+
CONFIG_RING_BUFFER=y

tests/net/lib/lwm2m/content_senml_cbor/src/main.c

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
#define TEST_RES_OBJLNK 7
2525
#define TEST_RES_OPAQUE 8
2626
#define TEST_RES_TIME 9
27+
#define TEST_RES_MULTI_OPT 10
2728

28-
#define TEST_OBJ_RES_MAX_ID 10
29+
#define TEST_OBJ_RES_MAX_ID 11
2930

30-
#define TEST_MAX_PAYLOAD_BUFFER_LENGTH 40
31+
#define TEST_MAX_PAYLOAD_BUFFER_LENGTH 52
3132

3233
static struct lwm2m_engine_obj test_obj;
3334

@@ -46,7 +47,8 @@ static struct lwm2m_engine_obj_field test_fields[] = {
4647
OBJ_FIELD_DATA(TEST_RES_BOOL, RW, BOOL),
4748
OBJ_FIELD_DATA(TEST_RES_OBJLNK, RW, OBJLNK),
4849
OBJ_FIELD_DATA(TEST_RES_OPAQUE, RW, OPAQUE),
49-
OBJ_FIELD_DATA(TEST_RES_TIME, RW, TIME)
50+
OBJ_FIELD_DATA(TEST_RES_TIME, RW, TIME),
51+
OBJ_FIELD_DATA(TEST_RES_MULTI_OPT, RW, S32)
5052
};
5153

5254
static struct lwm2m_engine_obj_inst test_inst;
@@ -66,6 +68,7 @@ static bool test_bool;
6668
static struct lwm2m_objlnk test_objlnk;
6769
static uint8_t test_opaque[TEST_OPAQUE_MAX_SIZE];
6870
static time_t test_time;
71+
static int32_t test_multi_opt;
6972

7073
static struct lwm2m_engine_obj_inst *test_obj_create(uint16_t obj_inst_id)
7174
{
@@ -93,6 +96,8 @@ static struct lwm2m_engine_obj_inst *test_obj_create(uint16_t obj_inst_id)
9396
&test_opaque, sizeof(test_opaque));
9497
INIT_OBJ_RES_DATA(TEST_RES_TIME, test_res, i, test_res_inst, j,
9598
&test_time, sizeof(test_time));
99+
INIT_OBJ_RES_MULTI_DATA(TEST_RES_MULTI_OPT, test_res, i, test_res_inst, j, 1, true,
100+
&test_multi_opt, sizeof(test_multi_opt));
96101

97102
test_inst.resources = test_res;
98103
test_inst.resource_count = i;
@@ -955,6 +960,124 @@ ZTEST(net_content_senml_cbor_nomem, test_put_time_nomem)
955960
zassert_equal(ret, -ENOMEM, "Invalid error code returned");
956961
}
957962

963+
ZTEST(net_content_senml_cbor, test_put_composite)
964+
{
965+
int ret;
966+
struct lwm2m_obj_path_list lwm2m_obj_path_list_buf[1];
967+
sys_slist_t lwm2m_path_list;
968+
sys_slist_t lwm2m_path_free_list;
969+
970+
struct test_payload_buffer expected_payload = {
971+
.data = {
972+
0x83,
973+
0xA4,
974+
0x21, 0x69, '/', '6', '5', '5', '3', '5', '/', '0', '/',
975+
0x22, 0x1A, 0x68, 0xFA, 0x2F, 0x04,
976+
0x00, 0x61, '2',
977+
0x02, 0x0A,
978+
0xA3, 0x00, 0x61, '2', 0x06, 0x18, 0x3C, 0x02, 0x0B,
979+
0xA3, 0x00, 0x61, '2', 0x06, 0x18, 0x78, 0x02, 0x0C
980+
},
981+
.len = 42
982+
};
983+
984+
struct lwm2m_time_series_elem test_time_series_cache[3];
985+
struct lwm2m_cache_read_info cache_temp_info = {
986+
.entry_size = 3,
987+
.entry_limit = 3
988+
};
989+
990+
test_msg.path.res_id = TEST_RES_S32;
991+
test_msg.cache_info = &cache_temp_info;
992+
993+
ret = lwm2m_enable_cache(&test_msg.path, test_time_series_cache,
994+
ARRAY_SIZE(test_time_series_cache));
995+
zassert_equal(ret, 0, "Failed to enable cache");
996+
997+
struct lwm2m_time_series_elem test_time_series_temp;
998+
struct lwm2m_time_series_resource *cache_entry =
999+
lwm2m_cache_entry_get_by_object(&test_msg.path);
1000+
1001+
zassert_not_null(cache_entry, "Failed to get cache entry");
1002+
1003+
for (int i = 0; i < ARRAY_SIZE(test_time_series_cache); i++) {
1004+
test_time_series_temp.t = 1761226500 + i * 60;
1005+
test_time_series_temp.i32 = i + 10;
1006+
zassert_true(lwm2m_cache_write(cache_entry, &test_time_series_temp));
1007+
}
1008+
1009+
lwm2m_engine_path_list_init(&lwm2m_path_list, &lwm2m_path_free_list,
1010+
lwm2m_obj_path_list_buf, 1);
1011+
1012+
lwm2m_engine_add_path_to_list(&lwm2m_path_list, &lwm2m_path_free_list, &test_msg.path);
1013+
1014+
ret = do_send_op_senml_cbor(&test_msg, &lwm2m_path_list);
1015+
zassert_true(ret >= 0, "Error reported");
1016+
1017+
zassert_mem_equal(test_msg.msg_data + TEST_PAYLOAD_OFFSET, expected_payload.data,
1018+
expected_payload.len, "Invalid payload format");
1019+
}
1020+
1021+
ZTEST(net_content_senml_cbor, test_put_composite_res_inst)
1022+
{
1023+
int ret;
1024+
struct lwm2m_obj_path_list lwm2m_obj_path_list_buf[1];
1025+
sys_slist_t lwm2m_path_list;
1026+
sys_slist_t lwm2m_path_free_list;
1027+
1028+
struct test_payload_buffer expected_payload = {
1029+
.data = {
1030+
0x83,
1031+
0xA4,
1032+
0x21, 0x69, '/', '6', '5', '5', '3', '5', '/', '0', '/',
1033+
0x22, 0x1A, 0x68, 0xFA, 0x2F, 0x04,
1034+
0x00, 0x64, '1', '0', '/', '0',
1035+
0x02, 0x0A,
1036+
0xA3, 0x00, 0x64, '1', '0', '/', '0', 0x06, 0x18, 0x3C, 0x02, 0x0B,
1037+
0xA3, 0x00, 0x64, '1', '0', '/', '0', 0x06, 0x18, 0x78, 0x02, 0x0C
1038+
},
1039+
.len = 51
1040+
};
1041+
1042+
struct lwm2m_time_series_elem test_time_series_cache[3];
1043+
struct lwm2m_cache_read_info cache_temp_info = {
1044+
.entry_size = 3,
1045+
.entry_limit = 3
1046+
};
1047+
1048+
test_msg.path.res_id = TEST_RES_MULTI_OPT;
1049+
test_msg.path.res_inst_id = 0;
1050+
test_msg.path.level = 4;
1051+
test_msg.cache_info = &cache_temp_info;
1052+
1053+
ret = lwm2m_enable_cache(&test_msg.path, test_time_series_cache,
1054+
ARRAY_SIZE(test_time_series_cache));
1055+
zassert_equal(ret, 0, "Failed to enable cache");
1056+
1057+
struct lwm2m_time_series_elem test_time_series_temp;
1058+
struct lwm2m_time_series_resource *cache_entry =
1059+
lwm2m_cache_entry_get_by_object(&test_msg.path);
1060+
1061+
zassert_not_null(cache_entry, "Failed to get cache entry");
1062+
1063+
for (int i = 0; i < ARRAY_SIZE(test_time_series_cache); i++) {
1064+
test_time_series_temp.t = 1761226500 + i * 60;
1065+
test_time_series_temp.i32 = i + 10;
1066+
zassert_true(lwm2m_cache_write(cache_entry, &test_time_series_temp));
1067+
}
1068+
1069+
lwm2m_engine_path_list_init(&lwm2m_path_list, &lwm2m_path_free_list,
1070+
lwm2m_obj_path_list_buf, 1);
1071+
1072+
lwm2m_engine_add_path_to_list(&lwm2m_path_list, &lwm2m_path_free_list, &test_msg.path);
1073+
1074+
ret = do_send_op_senml_cbor(&test_msg, &lwm2m_path_list);
1075+
zassert_true(ret >= 0, "Error reported");
1076+
1077+
zassert_mem_equal(test_msg.msg_data + TEST_PAYLOAD_OFFSET, expected_payload.data,
1078+
expected_payload.len, "Invalid payload format");
1079+
}
1080+
9581081
ZTEST(net_content_senml_cbor, test_get_s32)
9591082
{
9601083
int ret;

0 commit comments

Comments
 (0)