Skip to content

Commit

Permalink
net/gcoap: remove gcoap attributes from coap_pkt_t
Browse files Browse the repository at this point in the history
  • Loading branch information
kb2ma committed Jun 12, 2018
1 parent c5113dd commit d047a58
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
5 changes: 3 additions & 2 deletions examples/gcoap/gcoap_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ static void _resp_handler(unsigned req_state, coap_pkt_t* pdu,
coap_get_code_class(pdu),
coap_get_code_detail(pdu));
if (pdu->payload_len) {
if (pdu->content_type == COAP_FORMAT_TEXT
|| pdu->content_type == COAP_FORMAT_LINK
unsigned content_type = coap_get_content_type(pdu);
if (content_type == COAP_FORMAT_TEXT
|| content_type == COAP_FORMAT_LINK
|| coap_get_code_class(pdu) == COAP_CLASS_CLIENT_FAILURE
|| coap_get_code_class(pdu) == COAP_CLASS_SERVER_FAILURE) {
/* Expecting diagnostic payload in failure cases */
Expand Down
8 changes: 0 additions & 8 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ extern "C" {
#define NANOCOAP_URI_MAX (64)
/** @} */

#ifdef MODULE_GCOAP
#define NANOCOAP_URL_MAX NANOCOAP_URI_MAX
#define NANOCOAP_QS_MAX (64)
#endif

/**
* @name CoAP option numbers
* @{
Expand Down Expand Up @@ -277,9 +272,6 @@ typedef struct {
uint16_t options_len; /**< length of options array */
coap_optpos_t options[NANOCOAP_NOPTS_MAX]; /**< option offset array */
#ifdef MODULE_GCOAP
uint8_t url[NANOCOAP_URI_MAX]; /**< parsed request URL */
uint8_t qs[NANOCOAP_QS_MAX]; /**< parsed query string */
uint16_t content_type; /**< content type */
uint32_t observe_value; /**< observe value */
#endif
} coap_pkt_t;
Expand Down
25 changes: 15 additions & 10 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,20 @@ static int _find_resource(coap_pkt_t *pdu, coap_resource_t **resource_ptr,

/* Find path for CoAP msg among listener resources and execute callback. */
gcoap_listener_t *listener = _coap_state.listeners;

uint8_t uri[NANOCOAP_URI_MAX];
if (coap_get_uri(pdu, uri) <= 0) {
return GCOAP_RESOURCE_NO_PATH;
}

while (listener) {
coap_resource_t *resource = listener->resources;
for (size_t i = 0; i < listener->resources_len; i++) {
if (i) {
resource++;
}

int res = strcmp((char *)&pdu->url[0], resource->path);
int res = strcmp((char *)&uri[0], resource->path);
if (res > 0) {
continue;
}
Expand Down Expand Up @@ -982,26 +988,25 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf)

int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
{
size_t qs_len = strlen((char *)pdu->qs);
char qs[NANOCOAP_URI_MAX];
size_t qs_len = 0;
size_t key_len = strlen(key);
size_t val_len = (val) ? (strlen(val) + 1) : 0;

/* make sure if url_len + the new query string fit into the url buffer */
if ((qs_len + key_len + val_len + 2) >= NANOCOAP_QS_MAX) {
if ((key_len + val_len + 2) >= NANOCOAP_URI_MAX) {
return -1;
}

pdu->qs[qs_len++] = '&';
memcpy(&pdu->qs[qs_len], key, key_len);
memcpy(&qs[0], key, key_len);
qs_len += key_len;
if (val) {
pdu->qs[qs_len++] = '=';
memcpy(&pdu->qs[qs_len], val, val_len);
qs[qs_len++] = '=';
memcpy(&qs[qs_len], val, val_len);
qs_len += val_len;
}
pdu->qs[qs_len] = '\0';
qs[qs_len] = '\0';

return (int)qs_len;
return coap_opt_add_string(pdu, COAP_OPT_URI_QUERY, &qs[0], '&');
}

/** @} */
7 changes: 0 additions & 7 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
}

#ifdef MODULE_GCOAP
coap_get_uri(pkt, pkt->url);
pkt->content_type = coap_get_content_type(pkt);

if (coap_get_option_uint(pkt, COAP_OPT_OBSERVE, &pkt->observe_value) != 0) {
pkt->observe_value = UINT32_MAX;
}
Expand Down Expand Up @@ -292,14 +289,10 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le

unsigned method_flag = coap_method2flag(coap_get_code_detail(pkt));

#ifdef MODULE_GCOAP
uint8_t *uri = pkt->url;
#else
uint8_t uri[NANOCOAP_URI_MAX];
if (coap_get_uri(pkt, uri) <= 0) {
return -EBADMSG;
}
#endif
DEBUG("nanocoap: URI path: \"%s\"\n", uri);

for (unsigned i = 0; i < coap_resources_numof; i++) {
Expand Down
7 changes: 5 additions & 2 deletions tests/unittests/tests-gcoap/tests-gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static void test_gcoap__client_get_req(void)
TEST_ASSERT_EQUAL_INT(hdr_fixed_len + GCOAP_TOKENLEN, coap_get_total_hdr_len(&pdu));
TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu));

char uri[10] = {0};
char uri[NANOCOAP_URI_MAX] = {0};
coap_get_uri(&pdu, (uint8_t *)&uri[0]);
TEST_ASSERT_EQUAL_STRING(&path[0], &uri[0]);
TEST_ASSERT_EQUAL_INT(0, pdu.payload_len);
Expand Down Expand Up @@ -161,7 +161,10 @@ static void test_gcoap__server_get_req(void)
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]);

char uri[NANOCOAP_URI_MAX] = {0};
coap_get_uri(&pdu, (uint8_t *)&uri[0]);
TEST_ASSERT_EQUAL_STRING("/cli/stats", &uri[0]);
}

/*
Expand Down

0 comments on commit d047a58

Please sign in to comment.