diff --git a/examples/client.c b/examples/client.c index 48e4b50a..c31c73bf 100644 --- a/examples/client.c +++ b/examples/client.c @@ -352,6 +352,7 @@ order_opts(void *a, void *b) { return COAP_OPTION_KEY(*(coap_option *)a) == COAP_OPTION_KEY(*(coap_option *)b); } + coap_list_t * new_option_node(unsigned short key, unsigned int length, unsigned char *data) { coap_option *option; @@ -364,8 +365,10 @@ new_option_node(unsigned short key, unsigned int length, unsigned char *data) { COAP_OPTION_KEY(*option) = key; COAP_OPTION_LENGTH(*option) = length; memcpy(COAP_OPTION_DATA(*option), data, length); - - node = coap_new_listnode(option); + + /* we can pass NULL here as delete function since option is released automatically */ + node = coap_new_listnode(option, NULL); + if ( node ) return node; diff --git a/list.c b/list.c index a123d0c2..da87cf1e 100644 --- a/list.c +++ b/list.c @@ -47,6 +47,8 @@ coap_delete(coap_list_t *node) { if ( !node ) return 0; + if ( node->delete ) + node->delete( node->data ); coap_free( node->data ); coap_free( node ); @@ -63,7 +65,7 @@ coap_delete_list(coap_list_t *queue) { } coap_list_t * -coap_new_listnode(void *data) { +coap_new_listnode(void *data, void (*delete)(void *) ) { coap_list_t *node = coap_malloc( sizeof(coap_list_t) ); if ( ! node ) { perror ("coap_new_listnode: malloc"); @@ -72,6 +74,7 @@ coap_new_listnode(void *data) { memset(node, 0, sizeof(coap_list_t)); node->data = data; + node->delete = delete; return node; } diff --git a/list.h b/list.h index 05fb60ca..5755bd64 100644 --- a/list.h +++ b/list.h @@ -9,11 +9,20 @@ struct coap_linkedlistnode { struct coap_linkedlistnode *next; void *data; + + /** + * Callback function that is called from coap_delete to release + * additional memory allocated by data Set to NULL if you do not + * need this. Note that data is free'd automatically. */ + void (*delete)(void *); }; typedef struct coap_linkedlistnode coap_list_t; -/* adds node to given queue, ordered by specified order function */ +/** + * Adds node to given queue, ordered by specified order function. Returns 1 + * when insert was successful, 0 otherwise. + */ int coap_insert(coap_list_t **queue, coap_list_t *node, int (*order)(void *, void *) ); @@ -28,6 +37,6 @@ void coap_delete_list(coap_list_t *queue); * by data will be released by coap_delete() with the new node. Returns the * new list node. */ -coap_list_t *coap_new_listnode(void *data); +coap_list_t *coap_new_listnode(void *data, void (*delete)(void *) ); #endif /* _COAP_LIST_H_ */ diff --git a/main.c b/main.c index 32659c93..9461a395 100644 --- a/main.c +++ b/main.c @@ -16,28 +16,38 @@ #include #include +#include "subscribe.h" #include "coap.h" +#define COAP_RESOURCE_CHECK_TIME 2 + +#define GENERATE_PDU(var,t,c,i) { \ + var = coap_new_pdu(); \ + if (var) { \ + var->hdr->type = (t); \ + var->hdr->code = (c); \ + var->hdr->id = (i); \ + } \ + } + coap_pdu_t * new_ack( coap_context_t *ctx, coap_queue_t *node ) { - coap_pdu_t *pdu = coap_new_pdu(); - - if (pdu) { - pdu->hdr->type = COAP_MESSAGE_ACK; - pdu->hdr->code = 0; - pdu->hdr->id = node->pdu->hdr->id; - } + coap_pdu_t *pdu; + GENERATE_PDU(pdu,COAP_MESSAGE_ACK,0,node->pdu->hdr->id); + return pdu; +} +coap_pdu_t * +new_rst( coap_context_t *ctx, coap_queue_t *node ) { + coap_pdu_t *pdu; + GENERATE_PDU(pdu,COAP_MESSAGE_RST,0,node->pdu->hdr->id); return pdu; } coap_pdu_t * new_response( coap_context_t *ctx, coap_queue_t *node, unsigned int code ) { - coap_pdu_t *pdu = new_ack(ctx, node); - - if (pdu) - pdu->hdr->code = code; - + coap_pdu_t *pdu; + GENERATE_PDU(pdu,COAP_MESSAGE_ACK,code,node->pdu->hdr->id); return pdu; } @@ -58,12 +68,157 @@ static unsigned char resources[] = ";sh=/f;n=Example Resource 1," ";sh=/s;ct=41,42;n=second example"; +/* Fills result with URI components that are present in pdu. Returns 0 on error, + * 1 otherwise. */ +int +get_request_uri(coap_pdu_t *pdu, coap_uri_t *result) { + coap_opt_t *opt; + + if ( !pdu || !result ) + return 0; + + opt = coap_check_option(pdu, COAP_OPTION_URI_SCHEME); + result->scheme = opt ? (char*)COAP_OPT_VALUE(*opt) : NULL; + + opt = coap_check_option(pdu, COAP_OPTION_URI_AUTHORITY); + result->na = opt ? (char*)COAP_OPT_VALUE(*opt) : NULL; + + opt = coap_check_option(pdu, COAP_OPTION_URI_PATH); + result->path = opt ? (char*)COAP_OPT_VALUE(*opt) : NULL; + + return 1; +} + +coap_resource_t * +coap_get_resource(coap_context_t *ctx, coap_uri_t *uri) { + coap_list_t *node; + coap_key_t key; + + if ( !ctx || !uri ) + return NULL; + + key = coap_uri_hash(uri); + /* TODO: use hash table for resources with key to access */ + for (node = ctx->resources; node; node = node->next) { + if ( key == coap_uri_hash(COAP_RESOURCE(node)->uri) ) + return COAP_RESOURCE(node); + } + + return NULL; +} + +#define INDEX "Hi there!" + +coap_opt_t * +coap_next_option(coap_pdu_t *pdu, coap_opt_t *opt) { + coap_opt_t *next; + if ( !pdu || !opt ) + return NULL; + + next = (coap_opt_t *)( (unsigned char *)opt + COAP_OPT_SIZE(*opt) ); + return (unsigned char *)next < pdu->data && COAP_OPT_DELTA(*next) == 0 ? next : NULL; +} + +int +mediatype_matches(coap_pdu_t *pdu, unsigned char mediatype) { + coap_opt_t *ct; + + for (ct = coap_check_option(pdu, COAP_OPTION_CONTENT_TYPE); ct; ct = coap_next_option(pdu, ct)) { + if ( *COAP_OPT_VALUE(*ct) == mediatype ) + return 1; + } + + return 0; +} + +coap_pdu_t * +handle_get(coap_context_t *ctx, coap_queue_t *node, void *data) { + coap_pdu_t *pdu; + coap_uri_t uri; + coap_resource_t *resource; + coap_opt_t *block; + int len, blklen; + unsigned int blk; + static unsigned char buf[COAP_MAX_PDU_SIZE]; + + + if ( !get_request_uri( node->pdu, &uri ) ) + return NULL; + + if ( !uri.path ) { + pdu = new_response(ctx, node, COAP_RESPONSE_200); + if ( !pdu ) + return NULL; + + add_contents( pdu, COAP_MEDIATYPE_TEXT_PLAIN, sizeof(INDEX) - 1, (unsigned char *)INDEX ); + goto ok; + } + + if ( strlen(uri.path) == sizeof(COAP_DEFAULT_URI_WELLKNOWN) - 1 + && memcmp(uri.path, COAP_DEFAULT_URI_WELLKNOWN, sizeof(COAP_DEFAULT_URI_WELLKNOWN - 1) == 0 )) { + /* handle .well-known/r */ + + /* FIXME: generate resource list from ctx->resources (use key as short URI) */ + pdu = new_response(ctx, node, COAP_RESPONSE_200); + if ( !pdu ) + return NULL; + + add_contents( pdu, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, + (unsigned int)strlen((char *)resources), resources ); + goto ok; + } + + /* any other resource */ + resource = coap_get_resource(ctx, &uri); + if ( !resource ) + return new_response(ctx, node, COAP_RESPONSE_404); + + /* check if requested mediatypes match */ + if ( coap_check_option(node->pdu, COAP_OPTION_CONTENT_TYPE) + && !mediatype_matches(node->pdu, resource->mediatype) ) + return new_response(ctx, node, COAP_RESPONSE_415); + + block = coap_check_option(node->pdu, COAP_OPTION_BLOCK); + if ( block ) { + blk = coap_decode_var_bytes( COAP_OPT_VALUE(*block), COAP_OPT_LENGTH(*block) ); + blklen = 16 << (blk & 0x07); + blk >>= 4; + } else { + blklen = COAP_MAX_PDU_SIZE; + blk = 0; + } + + if ( !resource->data ) + return new_response(ctx, node, COAP_RESPONSE_200); + + len = resource->data(&uri, resource->mediatype, blk, buf, blklen); + if ( len > 0 ) { + pdu = new_response(ctx, node, COAP_RESPONSE_200); + if ( !pdu ) + return NULL; + + add_contents(pdu, resource->mediatype, len, buf); + /* FIXME: add block option; where to get the M-bit from? */ + goto ok; + } + + /* if we reach this point, something went wrong */ + return new_response(ctx, node, COAP_RESPONSE_500); + + ok: + /* pdu is set, handle subscription if requested */ +#if 0 + coap_add_subscription(ctx, coap_new_subscription(ctx, r->uri, &sub1, time(&now)+20)); +#endif + return pdu; +} + void -message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { +message_handler(coap_context_t *ctx, coap_queue_t *node, void *data) { coap_pdu_t *pdu = NULL; #ifndef NDEBUG - printf("** process pdu: "); + debug("** process pdu: "); coap_show_pdu( node->pdu ); #endif @@ -74,11 +229,10 @@ message_handler( coap_context_t *ctx, coap_queue_t *node, void *data) { switch (node->pdu->hdr->code) { case COAP_REQUEST_GET : - pdu = new_response( ctx, node, COAP_RESPONSE_200 ); - if (pdu) { - add_contents( pdu, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, - (unsigned int)strlen((char *)resources), resources ); - } + pdu = handle_get(ctx, node, data); + + if ( !pdu && node->pdu->hdr->type == COAP_MESSAGE_CON ) + pdu = new_rst( ctx, node ); break; case COAP_REQUEST_POST: case COAP_REQUEST_PUT: @@ -176,6 +330,68 @@ join( coap_context_t *ctx, char *group_name ){ return result; } + +unsigned int +create_plain_data(coap_uri_t *uri, unsigned short mediatype, unsigned int offset16, unsigned char *buf, unsigned int buflen) { + memcpy(buf, "some data\n", 10); + return 10; +} + +void +init_resources(coap_context_t *ctx) { + coap_resource_t *r; + struct sockaddr_in6 sub1, sub2; + time_t now; + + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + return; + + r->uri = coap_new_uri( "/first" ); + r->mediatype = COAP_MEDIATYPE_TEXT_PLAIN; + r->dirty = 1; + r->data = create_plain_data; + coap_add_resource( ctx, r ); + + /* build two subscriptions */ + sub1.sin6_family = AF_INET6; + sub1.sin6_port = htons(12345);; + + inet_pton(AF_INET6, "ff02:1234:dead:7890::1", &sub1.sin6_addr); + + sub2.sin6_family = AF_INET6; + sub2.sin6_port = htons(54321);; + + inet_pton(AF_INET6, "ABCD:2001:DEAF::beed", &sub2.sin6_addr); + + coap_add_subscription(ctx, coap_new_subscription(ctx, r->uri, &sub1, time(&now)+20)); + coap_add_subscription(ctx, coap_new_subscription(ctx, r->uri, &sub2, time(&now)+12)); + + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + return; + + r->uri = coap_new_uri( "/second" ); + r->mediatype = COAP_MEDIATYPE_TEXT_PLAIN; + r->dirty = 0; + coap_add_resource( ctx, r ); + + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + return; + + r->uri = coap_new_uri( "/third" ); + r->mediatype = COAP_MEDIATYPE_TEXT_PLAIN; + r->dirty = 1; + coap_add_resource( ctx, r ); + + if ( !(r = coap_malloc( sizeof(coap_resource_t) ))) + return; + + r->uri = coap_new_uri( "/fourth" ); + r->mediatype = COAP_MEDIATYPE_TEXT_PLAIN; + r->dirty = 0; + coap_add_resource( ctx, r ); + +} + int main(int argc, char **argv) { coap_context_t *ctx; @@ -210,7 +426,7 @@ main(int argc, char **argv) { join( ctx, group ); coap_register_message_handler( ctx, message_handler ); - + init_resources(ctx); while ( 1 ) { FD_ZERO(&readfds); FD_SET( ctx->sockfd, &readfds ); @@ -223,14 +439,17 @@ main(int argc, char **argv) { nextpdu = coap_peek_next( ctx ); } - if ( nextpdu ) { /* set timeout if there is a pdu to send */ + if ( nextpdu && nextpdu->t <= now + COAP_RESOURCE_CHECK_TIME ) { + /* set timeout if there is a pdu to send before our automatic timeout occurs */ tv.tv_usec = 0; tv.tv_sec = nextpdu->t - now; timeout = &tv; - } else - timeout = NULL; /* no timeout otherwise */ - - result = select( ctx->sockfd + 1, &readfds, 0, 0, timeout ); + } else { + tv.tv_usec = 0; + tv.tv_sec = COAP_RESOURCE_CHECK_TIME; + timeout = &tv; + } + result = select( FD_SETSIZE, &readfds, 0, 0, timeout ); if ( result < 0 ) { /* error */ perror("select"); @@ -238,7 +457,10 @@ main(int argc, char **argv) { if ( FD_ISSET( ctx->sockfd, &readfds ) ) { coap_read( ctx ); /* read received data */ coap_dispatch( ctx ); /* and dispatch PDUs from receivequeue */ - } + } + } else { /* timeout */ + coap_check_resource_list( ctx ); + coap_check_subscriptions( ctx ); } } diff --git a/net.h b/net.h index bf9a2364..37764716 100644 --- a/net.h +++ b/net.h @@ -41,7 +41,8 @@ coap_queue_t *coap_new_node(); /* The CoAP stack's global state is stored in a coap_context_t object */ typedef struct { - coap_queue_t *sendqueue, *recvqueue; + coap_list_t *resources, *subscriptions; /* FIXME: make these hash tables */ + coap_queue_t *sendqueue, *recvqueue; /* FIXME make these coap_list_t */ int sockfd; /* send/receive socket */ void ( *msg_handler )( void *, coap_queue_t *, void *); diff --git a/pdu.h b/pdu.h index 5e8b699b..7b66f177 100644 --- a/pdu.h +++ b/pdu.h @@ -41,6 +41,10 @@ #define COAP_OPTION_LOCATION 6 /* E, String, 1-270 B, - */ #define COAP_OPTION_URI_PATH 9 /* C, String, 1-270 B, "" */ +/* option types from draft-hartke-coap-observe-01 */ + +#define COAP_OPTION_SUBSCRIPTION 10 /* E, Duration, 1 B, 0 */ + /* selected option types from draft-bormann-coap-misc-04 */ #define COAP_OPTION_BLOCK 13 /* C, unsigned integer, 1--3 B, 0 */ diff --git a/uri.h b/uri.h index 5b865304..e3b16fc9 100644 --- a/uri.h +++ b/uri.h @@ -19,4 +19,16 @@ typedef struct { */ int coap_split_uri( char *str, coap_uri_t *uri); +/** + * Creates a new coap_uri_t object from the specified URI. Returns the new + * object or NULL on error. The memory allocated by the new coap_uri_t + * must be released using coap_free(). */ +coap_uri_t *coap_new_uri( const char *uri); + +/** + * Clones the specified coap_uri_t object. Thie function allocates sufficient + * memory to hold the coap_uri_t structure and its contents. The object must + * be released with coap_free(). */ +coap_uri_t *coap_clone_uri( const coap_uri_t *uri); + #endif /* _COAP_URI_H_ */