Skip to content

Commit

Permalink
nanocoap: implement coap_reply_simple() using coap_build_reply_header()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 22, 2024
1 parent 5a9136e commit 7c4faab
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,23 +595,23 @@ ssize_t coap_reply_simple(coap_pkt_t *pkt,
unsigned ct,
const void *payload, size_t payload_len)
{
uint8_t *payload_start = buf + coap_get_total_hdr_len(pkt);
uint8_t *bufpos = payload_start;
void *payload_start;
size_t payload_len_max;

if (payload_len) {
bufpos += coap_put_option_ct(bufpos, 0, ct);
*bufpos++ = 0xff;
size_t header_len = coap_build_reply_header(pkt, code, buf, len, ct,
payload ? &payload_start : NULL,
&payload_len_max);
if (payload == NULL) {
return header_len;
}

ssize_t res = coap_build_reply(pkt, code, buf, len,
bufpos - payload_start + payload_len);

if (payload_len && (res > 0)) {
assert(payload);
memcpy(bufpos, payload, payload_len);
if (payload_len > payload_len_max) {
return -ENOBUFS;
}

return res;
memcpy(payload_start, payload, payload_len);

return header_len + payload_len;
}

ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
Expand Down

0 comments on commit 7c4faab

Please sign in to comment.