Skip to content

Commit c50c0f6

Browse files
rluboscarlescufi
authored andcommitted
net: lwm2m: Use link_format writer for Register/Update
The payload of the Register/Register Update message is also formatted as application/link-format. Therefore it's reasonable to reuse the new content writer instead of filling the payload manually. Signed-off-by: Robert Lubos <[email protected]>
1 parent b17555e commit c50c0f6

File tree

5 files changed

+106
-72
lines changed

5 files changed

+106
-72
lines changed

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
6060
#define ENGINE_UPDATE_INTERVAL_MS 500
6161
#define OBSERVE_COUNTER_START 0U
6262

63-
/*
64-
* TODO: to implement a way for clients to specify alternate path
65-
* via Kconfig (LwM2M specification 8.2.2 Alternate Path)
66-
*
67-
* For now, in order to inform server we support JSON format, we have to
68-
* report 'ct=11543' to the server. '</>' is required in order to append
69-
* content attribute. And resource type attribute is appended because of
70-
* Eclipse wakaama will reject the registration when 'rt="oma.lwm2m"' is
71-
* missing.
72-
*/
73-
74-
#define RESOURCE_TYPE ";rt=\"oma.lwm2m\""
75-
76-
#if defined(CONFIG_LWM2M_RW_JSON_SUPPORT)
77-
#define REG_PREFACE "</>" RESOURCE_TYPE \
78-
";ct=" STRINGIFY(LWM2M_FORMAT_OMA_JSON)
79-
#else
80-
#define REG_PREFACE ""
81-
#endif
82-
8363
#if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
8464
#define COAP_OPTION_BUF_LEN (CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE + 1)
8565
#else
@@ -1100,17 +1080,13 @@ void lwm2m_acknowledge(struct lwm2m_ctx *client_ctx)
11001080
request->acknowledged = true;
11011081
}
11021082

1103-
uint16_t lwm2m_get_rd_data(uint8_t *client_data, uint16_t size)
1083+
int lwm2m_register_payload_handler(struct lwm2m_message *msg)
11041084
{
11051085
struct lwm2m_engine_obj *obj;
11061086
struct lwm2m_engine_obj_inst *obj_inst;
1107-
uint8_t temp[32];
1108-
uint16_t pos = 0U;
1109-
int len;
1087+
int ret;
11101088

1111-
/* Add resource-type/content-type to the registration message */
1112-
memcpy(client_data, REG_PREFACE, sizeof(REG_PREFACE) - 1);
1113-
pos += sizeof(REG_PREFACE) - 1;
1089+
engine_put_begin(&msg->out, NULL);
11141090

11151091
SYS_SLIST_FOR_EACH_CONTAINER(&engine_obj_list, obj, node) {
11161092
/* Security obj MUST NOT be part of registration message */
@@ -1120,43 +1096,37 @@ uint16_t lwm2m_get_rd_data(uint8_t *client_data, uint16_t size)
11201096

11211097
/* Only report <OBJ_ID> when no instance available */
11221098
if (obj->instance_count == 0U) {
1123-
len = snprintk(temp, sizeof(temp), "%s</%u>",
1124-
(pos > 0) ? "," : "", obj->obj_id);
1125-
if (pos + len >= size) {
1126-
/* full buffer -- exit loop */
1127-
break;
1099+
struct lwm2m_obj_path path = {
1100+
.obj_id = obj->obj_id,
1101+
.level = LWM2M_PATH_LEVEL_OBJECT,
1102+
};
1103+
1104+
ret = engine_put_corelink(&msg->out, &path);
1105+
if (ret < 0) {
1106+
return ret;
11281107
}
11291108

1130-
memcpy(&client_data[pos], temp, len);
1131-
pos += len;
11321109
continue;
11331110
}
11341111

11351112
SYS_SLIST_FOR_EACH_CONTAINER(&engine_obj_inst_list,
11361113
obj_inst, node) {
11371114
if (obj_inst->obj->obj_id == obj->obj_id) {
1138-
len = snprintk(temp, sizeof(temp),
1139-
"%s</%u/%u>",
1140-
(pos > 0) ? "," : "",
1141-
obj_inst->obj->obj_id,
1142-
obj_inst->obj_inst_id);
1143-
/*
1144-
* TODO: iterate through resources once block
1145-
* transfer is handled correctly
1146-
*/
1147-
if (pos + len >= size) {
1148-
/* full buffer -- exit loop */
1149-
break;
1150-
}
1115+
struct lwm2m_obj_path path = {
1116+
.obj_id = obj_inst->obj->obj_id,
1117+
.obj_inst_id = obj_inst->obj_inst_id,
1118+
.level = LWM2M_PATH_LEVEL_OBJECT_INST,
1119+
};
11511120

1152-
memcpy(&client_data[pos], temp, len);
1153-
pos += len;
1121+
ret = engine_put_corelink(&msg->out, &path);
1122+
if (ret < 0) {
1123+
return ret;
1124+
}
11541125
}
11551126
}
11561127
}
11571128

1158-
client_data[pos] = '\0';
1159-
return pos;
1129+
return 0;
11601130
}
11611131

11621132
/* input / output selection */

subsys/net/lib/lwm2m/lwm2m_engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int lwm2m_init_message(struct lwm2m_message *msg);
8787
int lwm2m_send_message(struct lwm2m_message *msg);
8888
int lwm2m_send_empty_ack(struct lwm2m_ctx *client_ctx, uint16_t mid);
8989

90-
uint16_t lwm2m_get_rd_data(uint8_t *client_data, uint16_t size);
90+
int lwm2m_register_payload_handler(struct lwm2m_message *msg);
9191

9292
int lwm2m_perform_read_op(struct lwm2m_message *msg, uint16_t content_format);
9393

subsys/net/lib/lwm2m/lwm2m_rd_client.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
5858

5959
#include "lwm2m_object.h"
6060
#include "lwm2m_engine.h"
61+
#include "lwm2m_rw_link_format.h"
6162

6263
#define LWM2M_RD_CLIENT_URI "rd"
6364

@@ -122,7 +123,6 @@ struct lwm2m_rd_client_info {
122123
* documented in the LwM2M specification.
123124
*/
124125
static char query_buffer[MAX(32, sizeof("ep=") + CLIENT_EP_LEN)];
125-
static uint8_t client_data[256]; /* allocate some data for the RD */
126126

127127
void engine_update_tx_time(void)
128128
{
@@ -663,7 +663,6 @@ static int sm_send_registration(bool send_obj_support_data,
663663
lwm2m_message_timeout_cb_t timeout_cb)
664664
{
665665
struct lwm2m_message *msg;
666-
uint16_t client_data_len;
667666
int ret;
668667
char binding[CLIENT_BINDING_LEN];
669668

@@ -766,11 +765,10 @@ static int sm_send_registration(bool send_obj_support_data,
766765
goto cleanup;
767766
}
768767

769-
/* generate the rd data */
770-
client_data_len = lwm2m_get_rd_data(client_data,
771-
sizeof(client_data));
772-
ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), client_data,
773-
client_data_len);
768+
msg->out.out_cpkt = &msg->cpkt;
769+
msg->out.writer = &link_format_writer;
770+
771+
ret = do_register_op_link_format(msg);
774772
if (ret < 0) {
775773
goto cleanup;
776774
}

subsys/net/lib/lwm2m/lwm2m_rw_link_format.c

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,82 @@ LOG_MODULE_REGISTER(net_lwm2m_link_format, CONFIG_LWM2M_LOG_LEVEL);
1414

1515
#define CORELINK_BUF_SIZE 24
1616

17+
#define ENABLER_VERSION "lwm2m=\"" LWM2M_PROTOCOL_VERSION "\""
18+
19+
/*
20+
* TODO: to implement a way for clients to specify alternate path
21+
* via Kconfig (LwM2M specification 8.2.2 Alternate Path)
22+
*
23+
* For now, in order to inform server we support JSON format, we have to
24+
* report 'ct=11543' to the server. '</>' is required in order to append
25+
* content attribute. And resource type attribute is appended because of
26+
* Eclipse wakaama will reject the registration when 'rt="oma.lwm2m"' is
27+
* missing.
28+
*/
29+
30+
#if defined(CONFIG_LWM2M_RW_JSON_SUPPORT)
31+
#define RESOURCE_TYPE ";rt=\"oma.lwm2m\""
32+
33+
#define REG_PREFACE "</>" RESOURCE_TYPE \
34+
";ct=" STRINGIFY(LWM2M_FORMAT_OMA_JSON)
35+
#else
36+
#define REG_PREFACE ""
37+
#endif
38+
39+
enum link_format_mode {
40+
LINK_FORMAT_MODE_DISCOVERY,
41+
LINK_FORMAT_MODE_BOOTSTRAP_DISCOVERY,
42+
LINK_FORMAT_MODE_REGISTER,
43+
};
44+
1745
struct link_format_out_formatter_data {
1846
uint8_t request_level;
19-
bool is_bootstrap : 1;
47+
uint8_t mode;
2048
bool is_first : 1;
2149
};
2250

2351
static size_t put_begin(struct lwm2m_output_context *out,
2452
struct lwm2m_obj_path *path)
2553
{
26-
char enabler_ver[] = "lwm2m=\"" LWM2M_PROTOCOL_VERSION "\"";
54+
char init_string[MAX(sizeof(ENABLER_VERSION), sizeof(REG_PREFACE))];
2755
struct link_format_out_formatter_data *fd;
2856
int ret;
2957

58+
ARG_UNUSED(path);
59+
3060
fd = engine_get_out_user_data(out);
3161
if (fd == NULL) {
3262
return 0;
3363
}
3464

35-
if (!fd->is_bootstrap) {
65+
switch (fd->mode) {
66+
case LINK_FORMAT_MODE_DISCOVERY:
3667
/* Nothing to add in device management mode. */
3768
return 0;
38-
}
3969

40-
fd->is_first = false;
70+
case LINK_FORMAT_MODE_BOOTSTRAP_DISCOVERY:
71+
strcpy(init_string, ENABLER_VERSION);
72+
break;
73+
74+
case LINK_FORMAT_MODE_REGISTER:
75+
/* Only indicate content type if json is enabled. */
76+
if (!IS_ENABLED(CONFIG_LWM2M_RW_JSON_SUPPORT)) {
77+
return 0;
78+
}
79+
80+
strcpy(init_string, REG_PREFACE);
81+
break;
82+
}
4183

42-
ret = buf_append(CPKT_BUF_WRITE(out->out_cpkt), enabler_ver,
43-
strlen(enabler_ver));
84+
ret = buf_append(CPKT_BUF_WRITE(out->out_cpkt), init_string,
85+
strlen(init_string));
4486
if (ret < 0) {
4587
return 0;
4688
}
4789

48-
return strlen(enabler_ver);
90+
fd->is_first = false;
91+
92+
return strlen(init_string);
4993
}
5094

5195
static int put_corelink_separator(struct lwm2m_output_context *out)
@@ -235,7 +279,7 @@ static int put_obj_corelink(struct lwm2m_output_context *out,
235279
return ret;
236280
}
237281

238-
if (!fd->is_bootstrap) {
282+
if (fd->mode == LINK_FORMAT_MODE_DISCOVERY) {
239283
/* Report object attributes only in device management mode
240284
* (5.4.2).
241285
*/
@@ -278,10 +322,14 @@ static int put_obj_inst_corelink(struct lwm2m_output_context *out,
278322
return ret;
279323
}
280324

325+
if (fd->mode == LINK_FORMAT_MODE_REGISTER) {
326+
return len;
327+
}
328+
281329
/* Bootstrap object instance corelink shall only contain ssid
282330
* parameter for Security and Server objects (5.2.7.3).
283331
*/
284-
if (fd->is_bootstrap) {
332+
if (fd->mode == LINK_FORMAT_MODE_BOOTSTRAP_DISCOVERY) {
285333
if (path->obj_id == LWM2M_OBJECT_SECURITY_ID ||
286334
path->obj_id == LWM2M_OBJECT_SERVER_ID) {
287335
ret = put_corelink_ssid(out, path, obj_buf,
@@ -327,8 +375,8 @@ static int put_res_corelink(struct lwm2m_output_context *out,
327375
int len = 0;
328376
int ret;
329377

330-
if (fd->is_bootstrap) {
331-
/* No resource reporting in bootstrap mode. */
378+
if (fd->mode != LINK_FORMAT_MODE_DISCOVERY) {
379+
/* Report resources only in device management discovery. */
332380
return 0;
333381
}
334382

@@ -445,7 +493,8 @@ int do_discover_op_link_format(struct lwm2m_message *msg, bool is_bootstrap)
445493
int ret;
446494

447495
fd.is_first = true;
448-
fd.is_bootstrap = is_bootstrap;
496+
fd.mode = is_bootstrap ? LINK_FORMAT_MODE_BOOTSTRAP_DISCOVERY :
497+
LINK_FORMAT_MODE_DISCOVERY;
449498
fd.request_level = msg->path.level;
450499

451500
engine_set_out_user_data(&msg->out, &fd);
@@ -454,3 +503,19 @@ int do_discover_op_link_format(struct lwm2m_message *msg, bool is_bootstrap)
454503

455504
return ret;
456505
}
506+
507+
int do_register_op_link_format(struct lwm2m_message *msg)
508+
{
509+
struct link_format_out_formatter_data fd;
510+
int ret;
511+
512+
fd.is_first = true;
513+
fd.mode = LINK_FORMAT_MODE_REGISTER;
514+
fd.request_level = LWM2M_PATH_LEVEL_NONE;
515+
516+
engine_set_out_user_data(&msg->out, &fd);
517+
ret = lwm2m_register_payload_handler(msg);
518+
engine_clear_out_user_data(&msg->out);
519+
520+
return ret;
521+
}

subsys/net/lib/lwm2m/lwm2m_rw_link_format.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
extern const struct lwm2m_writer link_format_writer;
1313

1414
int do_discover_op_link_format(struct lwm2m_message *msg, bool is_bootstrap);
15+
int do_register_op_link_format(struct lwm2m_message *msg);
1516

1617
#endif /* LWM2M_RW_LINK_FORMAT_H_ */

0 commit comments

Comments
 (0)