Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: coap: Fixed discovery response formatting according to RFC6690 #31610

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions subsys/net/lib/coap/coap_link_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ static int format_attributes(const char * const *attributes,
bool res;

if (!attributes) {
goto terminator;
*more = false;
return 0;
}

for (attr = attributes; *attr; ) {
int attr_len = strlen(*attr);
for (attr = attributes; *attr; attr++) {
int attr_len;

res = append_to_coap_pkt(response, *attr, attr_len,
res = append_to_coap_pkt(response, ";", 1,
remaining, offset, current);
if (!res) {
return -ENOMEM;
Expand All @@ -347,36 +348,21 @@ static int format_attributes(const char * const *attributes,
return 0;
}

attr++;
if (!*attr) {
continue;
}
attr_len = strlen(*attr);

res = append_to_coap_pkt(response, ";", 1,
res = append_to_coap_pkt(response, *attr, attr_len,
remaining, offset, current);
if (!res) {
return -ENOMEM;
}

if (!*remaining) {
if (*(attr + 1) && !*remaining) {
*more = true;
return 0;
}
}

terminator:
res = append_to_coap_pkt(response, ";", 1, remaining, offset, current);
if (!res) {
return -ENOMEM;
}

if (!*remaining) {
*more = true;
return 0;
}

*more = false;

return 0;
}

Expand Down Expand Up @@ -535,6 +521,14 @@ int coap_well_known_core_get(struct coap_resource *resource,
if (r < 0) {
goto end;
}

if ((resource + 1) && (resource + 1)->path) {
r = append_to_coap_pkt(response, ",", 1, &remaining,
&offset, ctx.current);
if (!r) {
goto end;
}
}
}

/* Offset is the total size now, but block2 option is already
Expand Down Expand Up @@ -601,30 +595,21 @@ static int format_attributes(const char * const *attributes,
bool res;

if (!attributes) {
goto terminator;
return 0;
}

for (attr = attributes; *attr; ) {
res = append(response, (uint8_t *) *attr, strlen(*attr));
for (attr = attributes; *attr; attr++) {
res = append_u8(response, (uint8_t) ';');
if (!res) {
return -ENOMEM;
}

attr++;
if (*attr) {
res = append_u8(response, (uint8_t) ';');
if (!res) {
return -ENOMEM;
}
res = append(response, (uint8_t *) *attr, strlen(*attr));
if (!res) {
return -ENOMEM;
}
}

terminator:
res = append_u8(response, (uint8_t) ';');
if (!res) {
return -ENOMEM;
}

return 0;
}

Expand Down Expand Up @@ -702,6 +687,13 @@ int coap_well_known_core_get(struct coap_resource *resource,
if (r < 0) {
return r;
}

if ((resource + 1) && (resource + 1)->path) {
r = append_u8(response, (uint8_t) ',');
if (!r) {
return -ENOMEM;
}
}
}

return 0;
Expand Down