static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len) { gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); /* write the RIOT board name in the response buffer */ memcpy(pdu->payload, RIOT_BOARD, strlen(RIOT_BOARD)); return gcoap_finish(pdu, strlen(RIOT_BOARD), COAP_FORMAT_TEXT); }
/* * 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]); } }
/* * Server callback for /cli/stats. Accepts either a GET or a PUT. * * GET: Returns the count of packets sent by the CLI. * PUT: Updates the count of packets. Rejects an obviously bad request, but * allows any two byte value for example purposes. Semantically, the only * valid action is to set the value to 0. */ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len) { /* read coap method type in packet */ unsigned method_flag = coap_method2flag(coap_get_code_detail(pdu)); switch(method_flag) { case COAP_GET: gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); /* 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); case COAP_PUT: /* convert the payload to an integer and update the internal value */ if (pdu->payload_len <= 5) { char payload[6] = { 0 }; memcpy(payload, (char *)pdu->payload, pdu->payload_len); req_count = (uint16_t)strtoul(payload, NULL, 10); return gcoap_response(pdu, buf, len, COAP_CODE_CHANGED); } else { return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST); } } return 0; }
static ssize_t _luke_points(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx) { unsigned p = 0; unsigned code; (void)ctx; switch (coap_method2flag(coap_get_code_detail(pdu))) { case COAP_GET: { size_t payload_len; gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT); mutex_lock(&_points_mutex); payload_len = sprintf((char *)pdu->payload, LUKE_PAYLOAD_FMT, _points); mutex_unlock(&_points_mutex); return gcoap_finish(pdu, payload_len, COAP_FORMAT_JSON); } case COAP_POST: printf("POST %s\n", (char *)pdu->url); if ((pdu->payload_len < LUKE_PAYLOAD_MIN_SIZE) || (pdu->payload_len > LUKE_PAYLOAD_MAX_SIZE) || (pdu->content_type != COAP_FORMAT_JSON) || (sscanf((char *)pdu->payload, LUKE_PAYLOAD_FMT, &p) != 1)) { printf("(%u < %u) || (%u > %u) || (%u != %u) || " "(payload unparsable)\n", pdu->payload_len, LUKE_PAYLOAD_MIN_SIZE, pdu->payload_len, LUKE_PAYLOAD_MAX_SIZE, pdu->content_type, COAP_FORMAT_JSON); code = COAP_CODE_BAD_REQUEST; } else { code = COAP_CODE_VALID; _increment_points(p); } puts("Sending response"); return gcoap_response(pdu, buf, len, code); default: return 0; } }