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/gcoap: Provide piggybacked ACK response to confirmable request #7223

Merged
merged 3 commits into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions sys/net/application_layer/coap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

@smlng smlng Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use a switch-case over the possible coap types here, something like

switch(coap_get_type(&pdu)) {
case COAP_TYPE_NON:
case 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);
    }
    break;
case COAP_TYPE_RST:
case COAP_TYPE_ACK:
    DEBUG("gcoap: RST and ACK handler not implemented yet!\n");
    break;
default:
    DEBUG("gcoap: illegal request type: %u\n", coap_get_type(&pdu));
    return;
}

[edit] fixed indention above

|| 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 */
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions tests/unittests/tests-gcoap/tests-gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,72 @@ static void test_gcoap__server_get_resp(void)
}
}

/*
* Helper for server_con_* tests below.
* Confirmable request from libcoap example for gcoap_cli /cli/stats resource.
* Include 2-byte token.
*/
static int _read_cli_stats_req_con(coap_pkt_t *pdu, uint8_t *buf)
{
uint8_t pdu_data[] = {
0x42, 0x01, 0x8e, 0x03, 0x35, 0x61, 0xb3, 0x63,
0x6c, 0x69, 0x05, 0x73, 0x74, 0x61, 0x74, 0x73
};
memcpy(buf, pdu_data, sizeof(pdu_data));

return coap_parse(pdu, buf, sizeof(pdu_data));
}

/* Server CON GET request success case. Validate request is confirmable. */
static void test_gcoap__server_con_req(void)
{
uint8_t buf[GCOAP_PDU_BUF_SIZE];
coap_pkt_t pdu;

int res = _read_cli_stats_req_con(&pdu, &buf[0]);

TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu));
TEST_ASSERT_EQUAL_INT(COAP_TYPE_CON, coap_get_type(&pdu));
}

/*
* Server CON GET response success case. Test response is ACK.
* Response for libcoap example for gcoap_cli /cli/stats resource
*/
static void test_gcoap__server_con_resp(void)
{
uint8_t buf[GCOAP_PDU_BUF_SIZE];
coap_pkt_t pdu;

/* read request */
_read_cli_stats_req_con(&pdu, &buf[0]);

/* generate response */
gcoap_resp_init(&pdu, &buf[0], sizeof(buf), COAP_CODE_CONTENT);
char resp_payload[] = "2";
memcpy(&pdu.payload[0], &resp_payload[0], strlen(resp_payload));
ssize_t res = gcoap_finish(&pdu, strlen(resp_payload), COAP_FORMAT_TEXT);

uint8_t resp_data[] = {
0x62, 0x45, 0x8e, 0x03, 0x35, 0x61, 0xc0, 0xff,
0x30
};

TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu));
TEST_ASSERT_EQUAL_INT(COAP_TYPE_ACK, coap_get_type(&pdu));
TEST_ASSERT_EQUAL_INT(sizeof(resp_data), res);
}

Test *tests_gcoap_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_gcoap__client_get_req),
new_TestFixture(test_gcoap__client_get_resp),
new_TestFixture(test_gcoap__server_get_req),
new_TestFixture(test_gcoap__server_get_resp),
new_TestFixture(test_gcoap__server_con_req),
new_TestFixture(test_gcoap__server_con_resp),
};

EMB_UNIT_TESTCALLER(gcoap_tests, NULL, NULL, fixtures);
Expand Down