diff --git a/sys/include/net/gcoap.h b/sys/include/net/gcoap.h index 47c25c65fd..1cc757d8a0 100644 --- a/sys/include/net/gcoap.h +++ b/sys/include/net/gcoap.h @@ -184,7 +184,8 @@ * gcoap includes server and client capability. Available features include: * * - Message Type: Supports non-confirmable (NON) messaging. Additionally - * provides a callback on timeout. + * provides a callback on timeout. Provides piggybacked ACK response to a + * confirmable (CON) request. * - Observe extension: Provides server-side registration and notifications. * - Server and Client provide helper functions for writing the * response/request. See the CoAP topic in the source documentation for diff --git a/sys/net/application_layer/coap/gcoap.c b/sys/net/application_layer/coap/gcoap.c index 0bfb0c376b..0483431014 100644 --- a/sys/net/application_layer/coap/gcoap.c +++ b/sys/net/application_layer/coap/gcoap.c @@ -137,9 +137,16 @@ static void _listen(sock_udp_t *sock) /* incoming request */ if (coap_get_code_class(&pdu) == COAP_CLASS_REQ) { - size_t pdu_len = _handle_req(&pdu, buf, sizeof(buf), &remote); - if (pdu_len > 0) { - sock_udp_send(sock, buf, pdu_len, &remote); + if (coap_get_type(&pdu) == COAP_TYPE_NON + || coap_get_type(&pdu) == COAP_TYPE_CON) { + size_t pdu_len = _handle_req(&pdu, buf, sizeof(buf), &remote); + if (pdu_len > 0) { + sock_udp_send(sock, buf, pdu_len, &remote); + } + } + else { + DEBUG("gcoap: illegal request type: %u\n", coap_get_type(&pdu)); + return; } } /* incoming response */ @@ -730,9 +737,10 @@ size_t gcoap_req_send2(const uint8_t *buf, size_t len, int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code) { - /* Assume NON type request, so response type is the same. */ + if (coap_get_type(pdu) == COAP_TYPE_CON) { + coap_hdr_set_type(pdu->hdr, COAP_TYPE_ACK); + } coap_hdr_set_code(pdu->hdr, code); - /* Create message ID since NON? */ /* Reserve some space between the header and payload to write options later */ pdu->payload = buf + coap_get_total_hdr_len(pdu) + GCOAP_RESP_OPTIONS_BUF;