diff options
author | Vinicius Costa Gomes <vinicius.gomes@intel.com> | 2016-10-11 18:09:34 -0300 |
---|---|---|
committer | Anas Nashif <nashif@linux.intel.com> | 2016-10-17 02:12:12 +0000 |
commit | fcf98d1534054cfd3eaa0956f0a66277aae1ea7e (patch) | |
tree | e85b0b757f15e4cc0508e10bed3d8987e9f6c8b4 /samples | |
parent | 2f042d65a0e1fa6170f1b9d12464ce7828fcfbd1 (diff) |
samples/zoap_server: Add preliminary support for validation
Implement resources so they conform to what the ETSI plugtest suite
expects.
This allows the zoap-server sample to pass most of the tests of the
CORE[1] group, only TD_COAP_CORE_09 is not implemented.
Tests involving lossy networks weren't run as well.
The tests were run against the libcoap[2] client.
[1] ETSI CoAP test description
http://www.etsi.org/plugtests/CoAP/Document/CoAP_TestDescriptions_v015.pdf
[2] libcoap
https://libcoap.net/
Change-Id: Ifa3ed21a93052e02f47e99d7cb6d9d4b28e576d8
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/net/zoap_server/src/zoap-server.c | 232 |
1 files changed, 229 insertions, 3 deletions
diff --git a/samples/net/zoap_server/src/zoap-server.c b/samples/net/zoap_server/src/zoap-server.c index 2e267de47..460cb426c 100644 --- a/samples/net/zoap_server/src/zoap-server.c +++ b/samples/net/zoap_server/src/zoap-server.c @@ -42,7 +42,49 @@ char fiberStack[STACKSIZE]; static struct net_context *context; -static int test_get(struct zoap_resource *resource, +static int test_del(struct zoap_resource *resource, + struct zoap_packet *request, + const uip_ipaddr_t *addr, + uint16_t port) +{ + struct net_buf *buf; + struct zoap_packet response; + uint8_t code, type, *payload; + uint16_t id, len; + int r; + + payload = zoap_packet_get_payload(request, &len); + if (!payload) { + printf("packet without payload"); + return -EINVAL; + } + + code = zoap_header_get_code(request); + type = zoap_header_get_type(request); + id = zoap_header_get_id(request); + + NET_INFO("*******\n"); + NET_INFO("type: %u code %u id %u\n", type, code, id); + NET_INFO("*******\n"); + + /* Re-using the request buffer for the response */ + buf = request->buf; + + r = zoap_packet_init(&response, buf); + if (r < 0) { + return -EINVAL; + } + + /* FIXME: Could be that zoap_packet_init() sets some defaults */ + zoap_header_set_version(&response, 1); + zoap_header_set_type(&response, ZOAP_TYPE_ACK); + zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_DELETED); + zoap_header_set_id(&response, id); + + return net_reply(context, buf); +} + +static int test_put(struct zoap_resource *resource, struct zoap_packet *request, const uip_ipaddr_t *addr, uint16_t port) @@ -63,6 +105,10 @@ static int test_get(struct zoap_resource *resource, type = zoap_header_get_type(request); id = zoap_header_get_id(request); + NET_INFO("*******\n"); + NET_INFO("type: %u code %u id %u\n", type, code, id); + NET_INFO("*******\n"); + /* Re-using the request buffer for the response */ buf = request->buf; @@ -74,7 +120,174 @@ static int test_get(struct zoap_resource *resource, /* FIXME: Could be that zoap_packet_init() sets some defaults */ zoap_header_set_version(&response, 1); zoap_header_set_type(&response, ZOAP_TYPE_ACK); - zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_OK); + zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CHANGED); + zoap_header_set_id(&response, id); + + return net_reply(context, buf); +} + +static int test_post(struct zoap_resource *resource, + struct zoap_packet *request, + const uip_ipaddr_t *addr, + uint16_t port) +{ + struct net_buf *buf; + struct zoap_packet response; + uint8_t *payload, code, type; + uint16_t len, id; + int r; + + payload = zoap_packet_get_payload(request, &len); + if (!payload) { + printf("packet without payload"); + return -EINVAL; + } + + code = zoap_header_get_code(request); + type = zoap_header_get_type(request); + id = zoap_header_get_id(request); + + NET_INFO("*******\n"); + NET_INFO("type: %u code %u id %u\n", type, code, id); + NET_INFO("*******\n"); + + /* Re-using the request buffer for the response */ + buf = request->buf; + + r = zoap_packet_init(&response, buf); + if (r < 0) { + return -EINVAL; + } + + /* FIXME: Could be that zoap_packet_init() sets some defaults */ + zoap_header_set_version(&response, 1); + zoap_header_set_type(&response, ZOAP_TYPE_ACK); + zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CREATED); + zoap_header_set_id(&response, id); + + return net_reply(context, buf); +} + +static int piggyback_get(struct zoap_resource *resource, + struct zoap_packet *request, + const uip_ipaddr_t *addr, + uint16_t port) +{ + struct net_buf *buf; + struct zoap_packet response; + uint8_t *payload, code, type; + uint16_t len, id; + int r; + + payload = zoap_packet_get_payload(request, &len); + if (!payload) { + printf("packet without payload"); + return -EINVAL; + } + + code = zoap_header_get_code(request); + type = zoap_header_get_type(request); + id = zoap_header_get_id(request); + + NET_INFO("*******\n"); + NET_INFO("type: %u code %u id %u\n", type, code, id); + NET_INFO("*******\n"); + + /* Re-using the request buffer for the response */ + buf = request->buf; + + r = zoap_packet_init(&response, buf); + if (r < 0) { + return -EINVAL; + } + + /* FIXME: Could be that zoap_packet_init() sets some defaults */ + zoap_header_set_version(&response, 1); + zoap_header_set_type(&response, ZOAP_TYPE_ACK); + zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CONTENT); + zoap_header_set_id(&response, id); + + payload = zoap_packet_get_payload(&response, &len); + if (!payload) { + return -EINVAL; + } + + /* The response that coap-client expects */ + r = snprintf((char *) payload, len, "Type: %u\nCode: %u\nMID: %u\n", + type, code, id); + if (r < 0 || r > len) { + return -EINVAL; + } + + r = zoap_packet_set_used(&response, r); + if (r) { + return -EINVAL; + } + + return net_reply(context, buf); +} + +static int query_get(struct zoap_resource *resource, + struct zoap_packet *request, + const uip_ipaddr_t *addr, + uint16_t port) +{ + struct zoap_option options[4]; + struct net_buf *buf; + struct zoap_packet response; + uint8_t *payload, code, type; + uint16_t len, id; + int i, r; + + payload = zoap_packet_get_payload(request, &len); + if (!payload) { + printf("packet without payload"); + return -EINVAL; + } + + code = zoap_header_get_code(request); + type = zoap_header_get_type(request); + id = zoap_header_get_id(request); + + r = zoap_find_options(request, ZOAP_OPTION_URI_QUERY, options, 4); + if (r <= 0) { + return -EINVAL; + } + + NET_INFO("*******\n"); + NET_INFO("type: %u code %u id %u\n", type, code, id); + NET_INFO("num queries: %d\n", r); + + for (i = 0; i < r; i++) { + char str[16]; + + if (options[i].len + 1 > sizeof(str)) { + NET_INFO("Unexpected length of query: " + "%d (expected %zu)\n", + options[i].len, sizeof(str)); + break; + } + + memcpy(str, options[i].value, options[i].len); + str[options[i].len] = '\0'; + + NET_INFO("query[%d]: %s\n", i + 1, str); + } + + NET_INFO("*******\n"); + + /* Re-using the request buffer for the response */ + buf = request->buf; + + r = zoap_packet_init(&response, buf); + if (r < 0) { + return -EINVAL; + } + + /* FIXME: Could be that zoap_packet_init() sets some defaults */ + zoap_header_set_version(&response, 1); + zoap_header_set_type(&response, ZOAP_TYPE_ACK); + zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_CONTENT); zoap_header_set_id(&response, id); payload = zoap_packet_get_payload(&response, &len); @@ -99,9 +312,22 @@ static int test_get(struct zoap_resource *resource, static const char * const test_path[] = { "test", NULL }; +static const char * const segments_path[] = { "seg1", "seg2", "seg3", NULL }; + +static const char * const query_path[] = { "query", NULL }; + static struct zoap_resource resources[] = { - { .get = test_get, + { .get = piggyback_get, + .post = test_post, + .del = test_del, + .put = test_put, .path = test_path }, + { .get = piggyback_get, + .path = segments_path, + }, + { .get = query_get, + .path = query_path, + }, { }, }; |