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

nanocoap: add Uri-Query handling capabilities #16

Merged
merged 3 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 14 additions & 13 deletions nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,31 +313,32 @@ size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type
}
}

size_t coap_put_option_url(uint8_t *buf, uint16_t lastonum, const char *url)
size_t coap_put_option_uri(uint8_t *buf, uint16_t lastonum, const char *uri, uint16_t optnum)
{
size_t url_len = strlen(url);
assert(url_len);
char seperator = (optnum == COAP_OPT_URI_PATH) ? '/' : '&';
Copy link
Contributor

Choose a reason for hiding this comment

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

seperator -> separator

size_t uri_len = strlen(uri);
assert(uri_len);

uint8_t *bufpos = buf;
char *urlpos = (char*)url;
char *uripos = (char*)uri;

while(url_len) {
while(uri_len) {
size_t part_len;
urlpos++;
uint8_t *part_start = (uint8_t*)urlpos;
uripos++;
uint8_t *part_start = (uint8_t*)uripos;

while (url_len--) {
if ((*urlpos == '/') || (*urlpos == '\0')) {
while (uri_len--) {
if ((*uripos == seperator) || (*uripos == '\0')) {
break;
}
urlpos++;
uripos++;
}

part_len = (uint8_t*)urlpos - part_start;
part_len = (uint8_t*)uripos - part_start;

if (part_len) {
bufpos += coap_put_option(bufpos, lastonum, COAP_OPT_URI_PATH, part_start, part_len);
lastonum = COAP_OPT_URI_PATH;
bufpos += coap_put_option(bufpos, lastonum, optnum, part_start, part_len);
lastonum = optnum;
}
}

Expand Down
5 changes: 4 additions & 1 deletion nanocoap/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#define COAP_PORT (5683)
#define NANOCOAP_URL_MAX (64)
#define NANOCOAP_QS_MAX (64)

#define COAP_OPT_URI_HOST (3)
#define COAP_OPT_OBSERVE (6)
#define COAP_OPT_URI_PATH (11)
#define COAP_OPT_CONTENT_FORMAT (12)
#define COAP_OPT_URI_QUERY (15)

#define COAP_REQ (0)
#define COAP_RESP (2)
Expand Down Expand Up @@ -137,6 +139,7 @@ typedef struct {
typedef struct {
coap_hdr_t *hdr;
uint8_t url[NANOCOAP_URL_MAX];
uint8_t qs[NANOCOAP_QS_MAX];
uint8_t *token;
uint8_t *payload;
unsigned payload_len;
Expand Down Expand Up @@ -170,7 +173,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le
ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token, size_t token_len, unsigned code, uint16_t id);
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, uint8_t *odata, size_t olen);
size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type);
size_t coap_put_option_url(uint8_t *buf, uint16_t lastonum, const char *url);
size_t coap_put_option_uri(uint8_t *buf, uint16_t lastonum, const char *uri, uint16_t optnum);

static inline unsigned coap_get_ver(coap_pkt_t *pkt)
{
Expand Down
2 changes: 1 addition & 1 deletion nanocoap/nanocoap_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ssize_t nanocoap_get(sock_udp_ep_t *remote, const char *path, uint8_t *buf, size

uint8_t *pktpos = buf;
pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_REQ, NULL, 0, COAP_METHOD_GET, 1);
pktpos += coap_put_option_url(pktpos, 0, path);
pktpos += coap_put_option_uri(pktpos, 0, path, COAP_OPT_URI_PATH);

/* TODO: timeout random between between ACK_TIMEOUT and (ACK_TIMEOUT *
* ACK_RANDOM_FACTOR) */
Expand Down