/* * Client GET request with simple path. Test request generation. * Request /time resource from libcoap example */ static void test_nanocoap__get_req(void) { uint8_t buf[128]; coap_pkt_t pkt; uint16_t msgid = 0xABCD; uint8_t token[2] = {0xDA, 0xEC}; char path[] = "/time"; size_t total_hdr_len = 6; size_t total_opt_len = 5; size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON, &token[0], 2, COAP_METHOD_GET, msgid); TEST_ASSERT_EQUAL_INT(total_hdr_len, len); coap_pkt_init(&pkt, &buf[0], sizeof(buf), len); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); TEST_ASSERT_EQUAL_INT(total_hdr_len, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt)); TEST_ASSERT_EQUAL_INT(122, pkt.payload_len); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(total_opt_len, len); char uri[10] = {0}; coap_get_uri(&pkt, (uint8_t *)&uri[0]); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE); TEST_ASSERT_EQUAL_INT(total_hdr_len + total_opt_len, len); }
/* * Client GET response success case. Test parsing response. * Response for /time resource from libcoap example */ static void test_gcoap__client_get_resp(void) { uint8_t buf[GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; int res; char exp_payload[] = "Oct 22 10:46:48"; uint8_t pdu_data[] = { 0x52, 0x45, 0xe6, 0x02, 0x9b, 0xce, 0xc0, 0x21, 0x01, 0xff, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x32, 0x20, 0x31, 0x30, 0x3a, 0x34, 0x36, 0x3a, 0x34, 0x38 }; memcpy(buf, pdu_data, sizeof(pdu_data)); res = coap_parse(&pdu, &buf[0], sizeof(pdu_data)); TEST_ASSERT_EQUAL_INT(0, res); TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu)); TEST_ASSERT_EQUAL_INT(GCOAP_TOKENLEN, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(4 + GCOAP_TOKENLEN, coap_get_total_hdr_len(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu)); TEST_ASSERT_EQUAL_INT(strlen(exp_payload), pdu.payload_len); for (size_t i = 0; i < strlen(exp_payload); i++) { TEST_ASSERT_EQUAL_INT(exp_payload[i], pdu.payload[i]); } }
/* * Server GET response success case. Test writing response. * Response for libcoap example for gcoap_cli /cli/stats resource */ static void test_gcoap__server_get_resp(void) { uint8_t buf[GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; /* read request */ _read_cli_stats_req(&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[] = { 0x52, 0x45, 0x20, 0xb6, 0x35, 0x61, 0xc0, 0xff, 0x32 }; TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu)); TEST_ASSERT_EQUAL_INT(strlen(resp_payload), pdu.payload_len); TEST_ASSERT_EQUAL_INT(sizeof(resp_data), res); for (size_t i = 0; i < strlen(resp_payload); i++) { TEST_ASSERT_EQUAL_INT(resp_payload[i], pdu.payload[i]); } }
/* * Main request handler: generates response PDU in the provided buffer. * * Caller must finish the PDU and send it. * * return length of response pdu, or < 0 if can't handle */ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, sock_udp_ep_t *remote) { coap_resource_t *resource; gcoap_listener_t *listener; sock_udp_ep_t *observer = NULL; gcoap_observe_memo_t *memo = NULL; gcoap_observe_memo_t *resource_memo = NULL; _find_resource(pdu, &resource, &listener); if (resource == NULL) { return gcoap_response(pdu, buf, len, COAP_CODE_PATH_NOT_FOUND); } else { /* used below to ensure a memo not already recorded for the resource */ _find_obs_memo_resource(&resource_memo, resource); } if (coap_get_observe(pdu) == COAP_OBS_REGISTER) { int empty_slot = _find_obs_memo(&memo, remote, pdu); /* record observe memo */ if (memo == NULL) { if (empty_slot >= 0 && resource_memo == NULL) { int obs_slot = _find_observer(&observer, remote); /* cache new observer */ if (observer == NULL) { if (obs_slot >= 0) { observer = &_coap_state.observers[obs_slot]; memcpy(observer, remote, sizeof(sock_udp_ep_t)); } else { DEBUG("gcoap: can't register observer\n"); } } if (observer != NULL) { memo = &_coap_state.observe_memos[empty_slot]; } } if (memo == NULL) { coap_clear_observe(pdu); DEBUG("gcoap: can't register observe memo\n"); } } if (memo != NULL) { memo->observer = observer; memo->resource = resource; memo->token_len = coap_get_token_len(pdu); if (memo->token_len) { memcpy(&memo->token[0], pdu->token, memo->token_len); } DEBUG("gcoap: Registered observer for: %s\n", memo->resource->path); /* generate initial notification value */ uint32_t now = xtimer_now_usec(); pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF; }
/* Server GET request success case. Validate request example. */ static void test_gcoap__server_get_req(void) { uint8_t buf[GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; int res = _read_cli_stats_req(&pdu, &buf[0]); TEST_ASSERT_EQUAL_INT(0, res); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu)); TEST_ASSERT_EQUAL_INT(0, pdu.payload_len); TEST_ASSERT_EQUAL_STRING("/cli/stats", (char *) &pdu.url[0]); }
/* Response for server GET request using coap_reply_simple(). */ static void test_nanocoap__server_reply_simple(void) { uint8_t buf[_BUF_SIZE]; coap_pkt_t pkt; char *payload = "0"; int res = _read_riot_value_req(&pkt, &buf[0]); coap_reply_simple(&pkt, COAP_CODE_CONTENT, buf, _BUF_SIZE, COAP_FORMAT_TEXT, (uint8_t *)payload, 1); TEST_ASSERT_EQUAL_INT(0, res); TEST_ASSERT_EQUAL_INT(COAP_CODE_CONTENT, coap_get_code_raw(&pkt)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt)); }
/* Server GET request success case. */ static void test_nanocoap__server_get_req(void) { uint8_t buf[_BUF_SIZE]; coap_pkt_t pkt; char path[] = "/riot/value"; int res = _read_riot_value_req(&pkt, &buf[0]); TEST_ASSERT_EQUAL_INT(0, res); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt)); TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt)); TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt)); TEST_ASSERT_EQUAL_INT(0, pkt.payload_len); char uri[64] = {0}; coap_get_uri_path(&pkt, (uint8_t *)&uri[0]); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); }
ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len) { unsigned tkl = coap_get_token_len(pkt); unsigned len = sizeof(coap_hdr_t) + tkl; if ((len + payload_len + 1) > rlen) { return -ENOSPC; } /* if code is COAP_CODE_EMPTY (zero), use RST as type, else RESP */ unsigned type = code ? COAP_RESP : COAP_RST; coap_build_hdr((coap_hdr_t *)rbuf, type, pkt->token, tkl, code, ntohs(pkt->hdr->id)); coap_hdr_set_type((coap_hdr_t *)rbuf, type); coap_hdr_set_code((coap_hdr_t *)rbuf, code); len += payload_len; return len; }
/* * Client GET request success case. Test request generation. * Request /time resource from libcoap example * Includes 2-byte token */ static void test_gcoap__client_get_req(void) { uint8_t buf[GCOAP_PDU_BUF_SIZE]; coap_pkt_t pdu; size_t len; char path[] = "/time"; uint8_t pdu_data[] = { 0x52, 0x01, 0xe6, 0x02, 0x9b, 0xce, 0xb4, 0x74, 0x69, 0x6d, 0x65 }; len = gcoap_request(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET, &path[0]); TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu)); TEST_ASSERT_EQUAL_INT(GCOAP_TOKENLEN, coap_get_token_len(&pdu)); TEST_ASSERT_EQUAL_INT(4 + GCOAP_TOKENLEN, coap_get_total_hdr_len(&pdu)); TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu)); TEST_ASSERT_EQUAL_STRING(&path[0], (char *)&pdu.url[0]); TEST_ASSERT_EQUAL_INT(0, pdu.payload_len); TEST_ASSERT_EQUAL_INT(sizeof(pdu_data), len); }
/* http://tools.ietf.org/html/rfc7252#section-3 * 0 1 2 3 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |Ver| T | TKL | Code | Message ID | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Token (if any, TKL bytes) ... * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Options (if any) ... * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |1 1 1 1 1 1 1 1| Payload (if any) ... * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) { uint8_t *urlpos = pkt->url; coap_hdr_t *hdr = (coap_hdr_t *)buf; pkt->hdr = hdr; uint8_t *pkt_pos = hdr->data; uint8_t *pkt_end = buf + len; memset(pkt->url, '\0', NANOCOAP_URL_MAX); pkt->payload_len = 0; pkt->observe_value = UINT32_MAX; /* token value (tkl bytes) */ if (coap_get_token_len(pkt)) { pkt->token = pkt_pos; pkt_pos += coap_get_token_len(pkt); } else { pkt->token = NULL; } /* parse options */ int option_nr = 0; while (pkt_pos != pkt_end) { uint8_t option_byte = *pkt_pos++; if (option_byte == 0xff) { pkt->payload = pkt_pos; pkt->payload_len = buf + len - pkt_pos; DEBUG("payload len = %u\n", pkt->payload_len); break; } else { int option_delta = _decode_value(option_byte >> 4, &pkt_pos, pkt_end); if (option_delta < 0) { DEBUG("bad op delta\n"); return -EBADMSG; } int option_len = _decode_value(option_byte & 0xf, &pkt_pos, pkt_end); if (option_len < 0) { DEBUG("bad op len\n"); return -EBADMSG; } option_nr += option_delta; DEBUG("option nr=%i len=%i\n", option_nr, option_len); switch (option_nr) { case COAP_OPT_URI_HOST: DEBUG("nanocoap: ignoring Uri-Host option!\n"); break; case COAP_OPT_URI_PATH: *urlpos++ = '/'; memcpy(urlpos, pkt_pos, option_len); urlpos += option_len; break; case COAP_OPT_CONTENT_FORMAT: if (option_len == 0) { pkt->content_type = 0; } else if (option_len == 1) { pkt->content_type = *pkt_pos; } else if (option_len == 2) { memcpy(&pkt->content_type, pkt_pos, 2); pkt->content_type = ntohs(pkt->content_type); } break; case COAP_OPT_OBSERVE: if (option_len < 4) { pkt->observe_value = _decode_uint(pkt_pos, option_len); } else { DEBUG("nanocoap: discarding packet with invalid option length.\n"); return -EBADMSG; } break; default: DEBUG("nanocoap: unhandled option nr=%i len=%i critical=%u\n", option_nr, option_len, option_nr & 1); if (option_nr & 1) { DEBUG("nanocoap: discarding packet with unknown critical option.\n"); return -EBADMSG; } } pkt_pos += option_len; } } DEBUG("coap pkt parsed. code=%u detail=%u payload_len=%u, 0x%02x\n", coap_get_code_class(pkt), coap_get_code_detail(pkt), pkt->payload_len, hdr->code); return 0; }
/* * Main request handler: generates response PDU in the provided buffer. * * Caller must finish the PDU and send it. * * return length of response pdu, or < 0 if can't handle */ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, sock_udp_ep_t *remote) { coap_resource_t *resource = NULL; gcoap_listener_t *listener = NULL; sock_udp_ep_t *observer = NULL; gcoap_observe_memo_t *memo = NULL; gcoap_observe_memo_t *resource_memo = NULL; switch (_find_resource(pdu, &resource, &listener)) { case GCOAP_RESOURCE_WRONG_METHOD: return gcoap_response(pdu, buf, len, COAP_CODE_METHOD_NOT_ALLOWED); case GCOAP_RESOURCE_NO_PATH: return gcoap_response(pdu, buf, len, COAP_CODE_PATH_NOT_FOUND); case GCOAP_RESOURCE_FOUND: _find_obs_memo_resource(&resource_memo, resource); break; } if (coap_get_observe(pdu) == COAP_OBS_REGISTER) { int empty_slot = _find_obs_memo(&memo, remote, pdu); /* record observe memo */ if (memo == NULL) { if ((resource_memo != NULL) && _endpoints_equal(remote, resource_memo->observer)) { /* observer re-registering with new token */ memo = resource_memo; observer = resource_memo->observer; } else if ((empty_slot >= 0) && (resource_memo == NULL)) { int obs_slot = _find_observer(&observer, remote); /* cache new observer */ if (observer == NULL) { if (obs_slot >= 0) { observer = &_coap_state.observers[obs_slot]; memcpy(observer, remote, sizeof(sock_udp_ep_t)); } else { DEBUG("gcoap: can't register observer\n"); } } if (observer != NULL) { memo = &_coap_state.observe_memos[empty_slot]; } } if (memo == NULL) { coap_clear_observe(pdu); DEBUG("gcoap: can't register observe memo\n"); } } if (memo != NULL) { memo->observer = observer; memo->resource = resource; memo->token_len = coap_get_token_len(pdu); if (memo->token_len) { memcpy(&memo->token[0], pdu->token, memo->token_len); } DEBUG("gcoap: Registered observer for: %s\n", memo->resource->path); /* generate initial notification value */ uint32_t now = xtimer_now_usec(); pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF; }