Skip to content

Commit

Permalink
fixup! gcoap: add nanocoap_cache support for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Mar 31, 2022
1 parent 7a4d967 commit 584447d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1155,14 +1155,24 @@ static ssize_t _cache_build_response(nanocoap_cache_entry_t *ce, coap_pkt_t *pdu
return -ENOTSUP;
}
if (len < ce->response_len) {
puts("Too short");
return -ENOBUFS;
}
/* Use the same code from the cached content. Use other header
* fields from the incoming request */
gcoap_resp_init(pdu, buf, len, ce->response_pkt.hdr->code);
/* copy all options and possible payload from the cached response
* to the new response */
memcpy(buf, ce->response_buf, ce->response_len);
/* parse into pdu */
coap_parse(pdu, buf, ce->response_len);
unsigned header_len_req = coap_get_total_hdr_len(pdu);
unsigned header_len_cached = coap_get_total_hdr_len(&ce->response_pkt);
unsigned opt_payload_len = ce->response_len - header_len_cached;

/* copy all options and possible payload from the cached response
* to the new response */
memcpy((buf + header_len_req),
(ce->response_buf + header_len_cached),
opt_payload_len);
/* parse into pdu including all options and payload pointers etc */
coap_parse(pdu, buf, header_len_req + opt_payload_len);
return ce->response_len;
}

Expand All @@ -1179,6 +1189,12 @@ static void _receive_from_cache_cb(void *ctx)
if (memo->resp_handler) {
coap_pkt_t pdu;

if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
pdu.hdr = (coap_hdr_t *)(&memo->msg.hdr_buf[0]);
}
else {
pdu.hdr = (coap_hdr_t *)memo->msg.data.pdu_buf;
}
if (_cache_build_response(ce, &pdu, _listen_buf, sizeof(_listen_buf)) >= 0) {
/* TODO somehow find out if cached response was truncated? */
memo->state = GCOAP_MEMO_RESP;
Expand Down

0 comments on commit 584447d

Please sign in to comment.