mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
Merge pull request #10926 from kb2ma/gcoap/opt_finish_in_apps
examples: use coap_opt_finish() in gcoap based examples
This commit is contained in:
commit
e8d06baebf
@ -56,8 +56,9 @@ static ssize_t _handler_dummy(coap_pkt_t *pdu,
|
||||
int16_t val = 23;
|
||||
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
size_t plen = fmt_s16_dec((char *)pdu->payload, val);
|
||||
return gcoap_finish(pdu, plen, COAP_FORMAT_TEXT);
|
||||
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
resp_len += fmt_s16_dec((char *)pdu->payload, val);
|
||||
return resp_len;
|
||||
}
|
||||
|
||||
static ssize_t _handler_info(coap_pkt_t *pdu,
|
||||
@ -66,9 +67,10 @@ static ssize_t _handler_info(coap_pkt_t *pdu,
|
||||
(void)ctx;
|
||||
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
size_t slen = sizeof("SOME NODE INFOMRATION");
|
||||
memcpy(pdu->payload, "SOME NODE INFOMRATION", slen);
|
||||
return gcoap_finish(pdu, slen, COAP_FORMAT_TEXT);
|
||||
return resp_len + slen;
|
||||
}
|
||||
|
||||
static const coap_resource_t _resources[] = {
|
||||
|
@ -34,9 +34,11 @@ static ssize_t text_resp(coap_pkt_t *pdu, uint8_t *buf, size_t len,
|
||||
const char *text, unsigned format)
|
||||
{
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
coap_opt_add_format(pdu, format);
|
||||
ssize_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
size_t slen = strlen(text);
|
||||
memcpy(pdu->payload, text, slen);
|
||||
return gcoap_finish(pdu, slen, format);
|
||||
return resp_len + slen;
|
||||
}
|
||||
|
||||
static ssize_t handler_info(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
|
||||
|
@ -109,11 +109,12 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *c
|
||||
switch(method_flag) {
|
||||
case COAP_GET:
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
|
||||
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
|
||||
/* write the response buffer with the request count value */
|
||||
size_t payload_len = fmt_u16_dec((char *)pdu->payload, req_count);
|
||||
|
||||
return gcoap_finish(pdu, payload_len, COAP_FORMAT_TEXT);
|
||||
resp_len += fmt_u16_dec((char *)pdu->payload, req_count);
|
||||
return resp_len;
|
||||
|
||||
case COAP_PUT:
|
||||
/* convert the payload to an integer and update the internal
|
||||
@ -136,11 +137,13 @@ static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, vo
|
||||
{
|
||||
(void)ctx;
|
||||
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
|
||||
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
|
||||
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
|
||||
/* write the RIOT board name in the response buffer */
|
||||
/* must be 'greater than' to account for payload marker byte */
|
||||
if (pdu->payload_len > strlen(RIOT_BOARD)) {
|
||||
if (pdu->payload_len >= strlen(RIOT_BOARD)) {
|
||||
memcpy(pdu->payload, RIOT_BOARD, strlen(RIOT_BOARD));
|
||||
return gcoap_finish(pdu, strlen(RIOT_BOARD), COAP_FORMAT_TEXT);
|
||||
return resp_len + strlen(RIOT_BOARD);
|
||||
}
|
||||
else {
|
||||
puts("gcoap_cli: msg buffer too small");
|
||||
@ -249,23 +252,23 @@ int gcoap_cli_cmd(int argc, char **argv)
|
||||
if (((argc == apos + 3) && (code_pos == 0)) ||
|
||||
((argc == apos + 4) && (code_pos != 0))) {
|
||||
gcoap_req_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, code_pos+1, argv[apos+2]);
|
||||
if (argc == apos + 4) {
|
||||
/* must be 'greater than' to account for payload marker byte */
|
||||
if (pdu.payload_len > strlen(argv[apos+3])) {
|
||||
memcpy(pdu.payload, argv[apos+3], strlen(argv[apos+3]));
|
||||
coap_hdr_set_type(pdu.hdr, msg_type);
|
||||
|
||||
size_t paylen = (argc == apos + 4) ? strlen(argv[apos+3]) : 0;
|
||||
if (paylen) {
|
||||
coap_opt_add_format(&pdu, COAP_FORMAT_TEXT);
|
||||
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
if (pdu.payload_len >= paylen) {
|
||||
memcpy(pdu.payload, argv[apos+3], paylen);
|
||||
len += paylen;
|
||||
}
|
||||
else {
|
||||
puts("gcoap_cli: msg buffer too small");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
coap_hdr_set_type(pdu.hdr, msg_type);
|
||||
|
||||
if (argc == apos + 4) {
|
||||
len = gcoap_finish(&pdu, strlen(argv[apos+3]), COAP_FORMAT_TEXT);
|
||||
}
|
||||
else {
|
||||
len = gcoap_finish(&pdu, 0, COAP_FORMAT_NONE);
|
||||
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_NONE);
|
||||
}
|
||||
|
||||
printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu),
|
||||
@ -279,8 +282,9 @@ int gcoap_cli_cmd(int argc, char **argv)
|
||||
&_resources[0])) {
|
||||
case GCOAP_OBS_INIT_OK:
|
||||
DEBUG("gcoap_cli: creating /cli/stats notification\n");
|
||||
size_t payload_len = fmt_u16_dec((char *)pdu.payload, req_count);
|
||||
len = gcoap_finish(&pdu, payload_len, COAP_FORMAT_TEXT);
|
||||
coap_opt_add_format(&pdu, COAP_FORMAT_TEXT);
|
||||
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||
len += fmt_u16_dec((char *)pdu.payload, req_count);
|
||||
gcoap_obs_send(&buf[0], len, &_resources[0]);
|
||||
break;
|
||||
case GCOAP_OBS_INIT_UNUSED:
|
||||
|
@ -146,7 +146,7 @@ static int _update_remove(unsigned code, gcoap_resp_handler_t handle)
|
||||
return CORD_EP_ERR;
|
||||
}
|
||||
coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON);
|
||||
ssize_t pkt_len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE);
|
||||
ssize_t pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE);
|
||||
|
||||
/* send request */
|
||||
gcoap_req_send2(buf, pkt_len, &_rd_remote, handle);
|
||||
@ -220,7 +220,7 @@ static int _discover_internal(const sock_udp_ep_t *remote,
|
||||
}
|
||||
coap_hdr_set_type(pkt.hdr, COAP_TYPE_CON);
|
||||
gcoap_add_qstring(&pkt, "rt", "core.rd");
|
||||
size_t pkt_len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE);
|
||||
size_t pkt_len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE);
|
||||
res = gcoap_req_send2(buf, pkt_len, remote, _on_discover);
|
||||
if (res < 0) {
|
||||
return CORD_EP_ERR;
|
||||
|
@ -59,7 +59,7 @@ int cord_epsim_register(void)
|
||||
return CORD_EPSIM_ERROR;
|
||||
}
|
||||
/* finish, we don't have any payload */
|
||||
ssize_t len = gcoap_finish(&pkt, 0, COAP_FORMAT_NONE);
|
||||
ssize_t len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE);
|
||||
if (gcoap_req_send2(buf, len, &remote, NULL) == 0) {
|
||||
return CORD_EPSIM_ERROR;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user