Made Erbium function parameters const where applicable.\nMinor clean-up.

This commit is contained in:
Matthias Kovatsch 2012-01-20 18:30:12 +01:00
parent 0a4c77dbff
commit 4c3e858df5
10 changed files with 99 additions and 116 deletions

View file

@ -191,7 +191,7 @@ coap_get_tid()
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void void
coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) coap_send_message(uip_ipaddr_t *addr, uint16_t port, const uint8_t *data, uint16_t length)
{ {
/*configure connection to reply to client*/ /*configure connection to reply to client*/
uip_ipaddr_copy(&udp_conn->ripaddr, addr); uip_ipaddr_copy(&udp_conn->ripaddr, addr);
@ -586,7 +586,7 @@ coap_get_header_etag(void *packet, const uint8_t **etag)
} }
int int
coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len) coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len)
{ {
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len); ((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len);
memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len); memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len);
@ -605,9 +605,9 @@ coap_get_header_uri_host(void *packet, const char **host)
} }
int int
coap_set_header_uri_host(void *packet, char *host) coap_set_header_uri_host(void *packet, const char *host)
{ {
((coap_packet_t *)packet)->uri_host = (char *) host; ((coap_packet_t *)packet)->uri_host = host;
((coap_packet_t *)packet)->uri_host_len = strlen(host); ((coap_packet_t *)packet)->uri_host_len = strlen(host);
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST); SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST);
@ -615,20 +615,20 @@ coap_set_header_uri_host(void *packet, char *host)
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
int int
coap_get_header_location(void *packet, const char **uri) coap_get_header_location(void *packet, const char **location)
{ {
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) return 0; if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH)) return 0;
*uri = ((coap_packet_t *)packet)->location_path; *location = ((coap_packet_t *)packet)->location_path;
return ((coap_packet_t *)packet)->location_path_len; return ((coap_packet_t *)packet)->location_path_len;
} }
int int
coap_set_header_location(void *packet, char *location) coap_set_header_location(void *packet, const char *location)
{ {
while (location[0]=='/') ++location; while (location[0]=='/') ++location;
((coap_packet_t *)packet)->location_path = (char *) location; ((coap_packet_t *)packet)->location_path = location;
((coap_packet_t *)packet)->location_path_len = strlen(location); ((coap_packet_t *)packet)->location_path_len = strlen(location);
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH); SET_OPTION((coap_packet_t *)packet, COAP_OPTION_LOCATION_PATH);
@ -645,11 +645,11 @@ coap_get_header_uri_path(void *packet, const char **path)
} }
int int
coap_set_header_uri_path(void *packet, char *path) coap_set_header_uri_path(void *packet, const char *path)
{ {
while (path[0]=='/') ++path; while (path[0]=='/') ++path;
((coap_packet_t *)packet)->uri_path = (char *) path; ((coap_packet_t *)packet)->uri_path = path;
((coap_packet_t *)packet)->uri_path_len = strlen(path); ((coap_packet_t *)packet)->uri_path_len = strlen(path);
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH); SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_PATH);
@ -683,7 +683,7 @@ coap_get_header_token(void *packet, const uint8_t **token)
} }
int int
coap_set_header_token(void *packet, uint8_t *token, size_t token_len) coap_set_header_token(void *packet, const uint8_t *token, size_t token_len)
{ {
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len); ((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len);
memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len); memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len);
@ -731,7 +731,7 @@ coap_get_header_uri_query(void *packet, const char **query)
} }
int int
coap_set_header_uri_query(void *packet, char *query) coap_set_header_uri_query(void *packet, const char *query)
{ {
while (query[0]=='?') ++query; while (query[0]=='?') ++query;

View file

@ -200,11 +200,11 @@ typedef struct {
uint8_t etag_len; uint8_t etag_len;
uint8_t etag[COAP_ETAG_LEN]; uint8_t etag[COAP_ETAG_LEN];
uint8_t uri_host_len; uint8_t uri_host_len;
char *uri_host; const char *uri_host;
uint8_t location_path_len; uint8_t location_path_len;
char *location_path; const char *location_path;
uint8_t uri_path_len; uint8_t uri_path_len;
char *uri_path; const char *uri_path;
uint32_t observe; /* 0-4 bytes for coap-03 */ uint32_t observe; /* 0-4 bytes for coap-03 */
uint8_t token_len; uint8_t token_len;
uint8_t token[COAP_TOKEN_LEN]; uint8_t token[COAP_TOKEN_LEN];
@ -213,7 +213,7 @@ typedef struct {
uint16_t block_size; uint16_t block_size;
uint32_t block_offset; uint32_t block_offset;
uint8_t uri_query_len; uint8_t uri_query_len;
char *uri_query; const char *uri_query;
uint16_t payload_len; uint16_t payload_len;
uint8_t *payload; uint8_t *payload;
@ -236,7 +236,7 @@ typedef enum
void coap_init_connection(uint16_t port); void coap_init_connection(uint16_t port);
uint16_t coap_get_tid(void); uint16_t coap_get_tid(void);
void coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length); void coap_send_message(uip_ipaddr_t *addr, uint16_t port, const uint8_t *data, uint16_t length);
void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid); void coap_init_message(void *packet, coap_message_type_t type, uint8_t code, uint16_t tid);
int coap_serialize_message(void *packet, uint8_t *buffer); int coap_serialize_message(void *packet, uint8_t *buffer);
@ -255,28 +255,28 @@ int coap_get_header_max_age(void *packet, uint32_t *age);
int coap_set_header_max_age(void *packet, uint32_t age); int coap_set_header_max_age(void *packet, uint32_t age);
int coap_get_header_etag(void *packet, const uint8_t **etag); int coap_get_header_etag(void *packet, const uint8_t **etag);
int coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len); int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
int coap_get_header_uri_host(void *packet, const char **host); /*CAUTION in-place string might not be 0-terminated */ int coap_get_header_uri_host(void *packet, const char **host); /*CAUTION in-place string might not be 0-terminated */
int coap_set_header_uri_host(void *packet, char *host); int coap_set_header_uri_host(void *packet, const char *host);
int coap_get_header_location(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */ int coap_get_header_location(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */
int coap_set_header_location(void *packet, char *uri); int coap_set_header_location(void *packet, const char *uri);
int coap_get_header_uri_path(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */ int coap_get_header_uri_path(void *packet, const char **uri); /*CAUTION in-place string might not be 0-terminated */
int coap_set_header_uri_path(void *packet, char *uri); int coap_set_header_uri_path(void *packet, const char *uri);
int coap_get_header_observe(void *packet, uint32_t *observe); int coap_get_header_observe(void *packet, uint32_t *observe);
int coap_set_header_observe(void *packet, uint32_t observe); int coap_set_header_observe(void *packet, uint32_t observe);
int coap_get_header_token(void *packet, const uint8_t **token); int coap_get_header_token(void *packet, const uint8_t **token);
int coap_set_header_token(void *packet, uint8_t *token, size_t token_len); int coap_set_header_token(void *packet, const uint8_t *token, size_t token_len);
int coap_get_header_block(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset); int coap_get_header_block(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
int coap_set_header_block(void *packet, uint32_t num, uint8_t more, uint16_t size); int coap_set_header_block(void *packet, uint32_t num, uint8_t more, uint16_t size);
int coap_get_header_uri_query(void *packet, const char **query); /*CAUTION in-place string might not be 0-terminated */ int coap_get_header_uri_query(void *packet, const char **query); /*CAUTION in-place string might not be 0-terminated */
int coap_set_header_uri_query(void *packet, char *query); int coap_set_header_uri_query(void *packet, const char *query);
int coap_get_payload(void *packet, const uint8_t **payload); int coap_get_payload(void *packet, const uint8_t **payload);
int coap_set_payload(void *packet, uint8_t *payload, size_t length); int coap_set_payload(void *packet, uint8_t *payload, size_t length);

View file

@ -209,6 +209,7 @@ coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t opt
/* Merge multiple options. */ /* Merge multiple options. */
if (*dst_len > 0) if (*dst_len > 0)
{ {
/* dst already contains an option: concatenate */
(*dst)[*dst_len] = separator; (*dst)[*dst_len] = separator;
*dst_len += 1; *dst_len += 1;
@ -219,7 +220,8 @@ coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t opt
} }
else else
{ {
*dst = (char *) option; /* thus pointer pointer */ /* dst is empty: set to option */
*dst = (char *) option;
*dst_len = option_len; *dst_len = option_len;
} }
} }
@ -613,7 +615,8 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
PRINTF("Uri-Host [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host); PRINTF("Uri-Host [%.*s]\n", ((coap_packet_t *)packet)->uri_host_len, ((coap_packet_t *)packet)->uri_host);
break; break;
case COAP_OPTION_LOCATION_PATH: case COAP_OPTION_LOCATION_PATH:
coap_merge_multi_option(&(((coap_packet_t *)packet)->location_path), &(((coap_packet_t *)packet)->location_path_len), current_option, option_len, '/'); /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option( (char **) &(((coap_packet_t *)packet)->location_path), &(((coap_packet_t *)packet)->location_path_len), current_option, option_len, '/');
PRINTF("Location-Path [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path); PRINTF("Location-Path [%.*s]\n", ((coap_packet_t *)packet)->location_path_len, ((coap_packet_t *)packet)->location_path);
break; break;
case COAP_OPTION_URI_PORT: case COAP_OPTION_URI_PORT:
@ -621,11 +624,13 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
PRINTF("Uri-Port [%u]\n", ((coap_packet_t *)packet)->uri_port); PRINTF("Uri-Port [%u]\n", ((coap_packet_t *)packet)->uri_port);
break; break;
case COAP_OPTION_LOCATION_QUERY: case COAP_OPTION_LOCATION_QUERY:
coap_merge_multi_option(&(((coap_packet_t *)packet)->location_query), &(((coap_packet_t *)packet)->location_query_len), current_option, option_len, '&'); /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option( (char **) &(((coap_packet_t *)packet)->location_query), &(((coap_packet_t *)packet)->location_query_len), current_option, option_len, '&');
PRINTF("Location-Query [%.*s]\n", ((coap_packet_t *)packet)->location_query_len, ((coap_packet_t *)packet)->location_query); PRINTF("Location-Query [%.*s]\n", ((coap_packet_t *)packet)->location_query_len, ((coap_packet_t *)packet)->location_query);
break; break;
case COAP_OPTION_URI_PATH: case COAP_OPTION_URI_PATH:
coap_merge_multi_option(&(((coap_packet_t *)packet)->uri_path), &(((coap_packet_t *)packet)->uri_path_len), current_option, option_len, '/'); /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option( (char **) &(((coap_packet_t *)packet)->uri_path), &(((coap_packet_t *)packet)->uri_path_len), current_option, option_len, '/');
PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path); PRINTF("Uri-Path [%.*s]\n", ((coap_packet_t *)packet)->uri_path_len, ((coap_packet_t *)packet)->uri_path);
break; break;
case COAP_OPTION_OBSERVE: case COAP_OPTION_OBSERVE:
@ -673,7 +678,8 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
PRINTF("Fence-Post\n"); PRINTF("Fence-Post\n");
break; break;
case COAP_OPTION_URI_QUERY: case COAP_OPTION_URI_QUERY:
coap_merge_multi_option(&(((coap_packet_t *)packet)->uri_query), &(((coap_packet_t *)packet)->uri_query_len), current_option, option_len, '&'); /* coap_merge_multi_option() operates in-place on the IPBUF, but final packet field should be const string -> cast to string */
coap_merge_multi_option( (char **) &(((coap_packet_t *)packet)->uri_query), &(((coap_packet_t *)packet)->uri_query_len), current_option, option_len, '&');
PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query); PRINTF("Uri-Query [%.*s]\n", ((coap_packet_t *)packet)->uri_query_len, ((coap_packet_t *)packet)->uri_query);
break; break;
case COAP_OPTION_BLOCK2: case COAP_OPTION_BLOCK2:
@ -752,7 +758,7 @@ coap_set_header_content_type(void *packet, unsigned int content_type)
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
int int
coap_get_header_accept(void *packet, uint16_t **accept) coap_get_header_accept(void *packet, const uint16_t **accept)
{ {
if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ACCEPT)) return 0; if (!IS_OPTION((coap_packet_t *)packet, COAP_OPTION_ACCEPT)) return 0;
@ -802,7 +808,7 @@ coap_get_header_etag(void *packet, const uint8_t **etag)
} }
int int
coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len) coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len)
{ {
((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len); ((coap_packet_t *)packet)->etag_len = MIN(COAP_ETAG_LEN, etag_len);
memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len); memcpy(((coap_packet_t *)packet)->etag, etag, ((coap_packet_t *)packet)->etag_len);
@ -822,7 +828,7 @@ coap_get_header_if_match(void *packet, const uint8_t **etag)
} }
int int
coap_set_header_if_match(void *packet, uint8_t *etag, size_t etag_len) coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len)
{ {
((coap_packet_t *)packet)->if_match_len = MIN(COAP_ETAG_LEN, etag_len); ((coap_packet_t *)packet)->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
memcpy(((coap_packet_t *)packet)->if_match, etag, ((coap_packet_t *)packet)->if_match_len); memcpy(((coap_packet_t *)packet)->if_match, etag, ((coap_packet_t *)packet)->if_match_len);
@ -854,7 +860,7 @@ coap_get_header_token(void *packet, const uint8_t **token)
} }
int int
coap_set_header_token(void *packet, uint8_t *token, size_t token_len) coap_set_header_token(void *packet, const uint8_t *token, size_t token_len)
{ {
((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len); ((coap_packet_t *)packet)->token_len = MIN(COAP_TOKEN_LEN, token_len);
memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len); memcpy(((coap_packet_t *)packet)->token, token, ((coap_packet_t *)packet)->token_len);
@ -873,7 +879,7 @@ coap_get_header_proxy_uri(void *packet, const char **uri)
} }
int int
coap_set_header_proxy_uri(void *packet, char *uri) coap_set_header_proxy_uri(void *packet, const char *uri)
{ {
((coap_packet_t *)packet)->proxy_uri = uri; ((coap_packet_t *)packet)->proxy_uri = uri;
((coap_packet_t *)packet)->proxy_uri_len = strlen(uri); ((coap_packet_t *)packet)->proxy_uri_len = strlen(uri);
@ -892,7 +898,7 @@ coap_get_header_uri_host(void *packet, const char **host)
} }
int int
coap_set_header_uri_host(void *packet, char *host) coap_set_header_uri_host(void *packet, const char *host)
{ {
((coap_packet_t *)packet)->uri_host = host; ((coap_packet_t *)packet)->uri_host = host;
((coap_packet_t *)packet)->uri_host_len = strlen(host); ((coap_packet_t *)packet)->uri_host_len = strlen(host);
@ -911,7 +917,7 @@ coap_get_header_uri_path(void *packet, const char **path)
} }
int int
coap_set_header_uri_path(void *packet, char *path) coap_set_header_uri_path(void *packet, const char *path)
{ {
while (path[0]=='/') ++path; while (path[0]=='/') ++path;
@ -932,7 +938,7 @@ coap_get_header_uri_query(void *packet, const char **query)
} }
int int
coap_set_header_uri_query(void *packet, char *query) coap_set_header_uri_query(void *packet, const char *query)
{ {
while (query[0]=='?') ++query; while (query[0]=='?') ++query;
@ -953,7 +959,7 @@ coap_get_header_location_path(void *packet, const char **path)
} }
int int
coap_set_header_location_path(void *packet, char *path) coap_set_header_location_path(void *packet, const char *path)
{ {
char *query; char *query;
@ -985,7 +991,7 @@ coap_get_header_location_query(void *packet, const char **query)
} }
int int
coap_set_header_location_query(void *packet, char *query) coap_set_header_location_query(void *packet, const char *query)
{ {
while (query[0]=='?') ++query; while (query[0]=='?') ++query;

View file

@ -213,18 +213,18 @@ typedef struct {
coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */ coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */
uint32_t max_age; uint32_t max_age;
size_t proxy_uri_len; size_t proxy_uri_len;
char *proxy_uri; const char *proxy_uri;
uint8_t etag_len; uint8_t etag_len;
uint8_t etag[COAP_ETAG_LEN]; uint8_t etag[COAP_ETAG_LEN];
size_t uri_host_len; size_t uri_host_len;
char *uri_host; const char *uri_host;
size_t location_path_len; size_t location_path_len;
char *location_path; const char *location_path;
uint16_t uri_port; uint16_t uri_port;
size_t location_query_len; size_t location_query_len;
char *location_query; const char *location_query;
size_t uri_path_len; size_t uri_path_len;
char *uri_path; const char *uri_path;
uint16_t observe; uint16_t observe;
uint8_t token_len; uint8_t token_len;
uint8_t token[COAP_TOKEN_LEN]; uint8_t token[COAP_TOKEN_LEN];
@ -241,7 +241,7 @@ typedef struct {
uint16_t block1_size; uint16_t block1_size;
uint32_t block1_offset; uint32_t block1_offset;
size_t uri_query_len; size_t uri_query_len;
char *uri_query; const char *uri_query;
uint8_t if_none_match; uint8_t if_none_match;
uint16_t payload_len; uint16_t payload_len;
@ -268,41 +268,41 @@ int coap_get_post_variable(void *packet, const char *name, const char **output);
unsigned int coap_get_header_content_type(void *packet); unsigned int coap_get_header_content_type(void *packet);
int coap_set_header_content_type(void *packet, unsigned int content_type); int coap_set_header_content_type(void *packet, unsigned int content_type);
int coap_get_header_accept(void *packet, uint16_t **accept); int coap_get_header_accept(void *packet, const uint16_t **accept);
int coap_set_header_accept(void *packet, uint16_t accept); int coap_set_header_accept(void *packet, uint16_t accept);
int coap_get_header_max_age(void *packet, uint32_t *age); int coap_get_header_max_age(void *packet, uint32_t *age);
int coap_set_header_max_age(void *packet, uint32_t age); int coap_set_header_max_age(void *packet, uint32_t age);
int coap_get_header_etag(void *packet, const uint8_t **etag); int coap_get_header_etag(void *packet, const uint8_t **etag);
int coap_set_header_etag(void *packet, uint8_t *etag, size_t etag_len); int coap_set_header_etag(void *packet, const uint8_t *etag, size_t etag_len);
int coap_get_header_if_match(void *packet, const uint8_t **etag); int coap_get_header_if_match(void *packet, const uint8_t **etag);
int coap_set_header_if_match(void *packet, uint8_t *etag, size_t etag_len); int coap_set_header_if_match(void *packet, const uint8_t *etag, size_t etag_len);
int coap_get_header_if_none_match(void *packet); int coap_get_header_if_none_match(void *packet);
int coap_set_header_if_none_match(void *packet); int coap_set_header_if_none_match(void *packet);
int coap_get_header_token(void *packet, const uint8_t **token); int coap_get_header_token(void *packet, const uint8_t **token);
int coap_set_header_token(void *packet, uint8_t *token, size_t token_len); int coap_set_header_token(void *packet, const uint8_t *token, size_t token_len);
int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */ int coap_get_header_proxy_uri(void *packet, const char **uri); /* In-place string might not be 0-terminated. */
int coap_set_header_proxy_uri(void *packet, char *uri); int coap_set_header_proxy_uri(void *packet, const char *uri);
int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */ int coap_get_header_uri_host(void *packet, const char **host); /* In-place string might not be 0-terminated. */
int coap_set_header_uri_host(void *packet, char *host); int coap_set_header_uri_host(void *packet, const char *host);
int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */ int coap_get_header_uri_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
int coap_set_header_uri_path(void *packet, char *path); int coap_set_header_uri_path(void *packet, const char *path);
int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */ int coap_get_header_uri_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
int coap_set_header_uri_query(void *packet, char *query); int coap_set_header_uri_query(void *packet, const char *query);
int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */ int coap_get_header_location_path(void *packet, const char **path); /* In-place string might not be 0-terminated. */
int coap_set_header_location_path(void *packet, char *path); /* Also splits optional query into Location-Query option. */ int coap_set_header_location_path(void *packet, const char *path); /* Also splits optional query into Location-Query option. */
int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */ int coap_get_header_location_query(void *packet, const char **query); /* In-place string might not be 0-terminated. */
int coap_set_header_location_query(void *packet, char *query); int coap_set_header_location_query(void *packet, const char *query);
int coap_get_header_observe(void *packet, uint32_t *observe); int coap_get_header_observe(void *packet, uint32_t *observe);
int coap_set_header_observe(void *packet, uint32_t observe); int coap_set_header_observe(void *packet, uint32_t observe);

View file

@ -59,33 +59,6 @@ LIST(restful_services);
LIST(restful_periodic_services); LIST(restful_periodic_services);
#ifdef WITH_HTTP
char *
rest_to_http_max_age(uint32_t age)
{
/* Cache-Control: max-age=age for HTTP */
static char temp_age[19];
snprintf(temp_age, sizeof(temp_age), "max-age=%lu", age);
return temp_age;
}
char *
rest_to_http_etag(uint8_t *etag, uint8_t etag_len)
{
static char temp_etag[17];
int index = 0;
for (index = 0; index<sizeof(temp_etag) && index<etag_len; ++index) {
snprintf(temp_etag+2*index, sizeof(temp_etag), "%02x", etag[index]);
}
temp_etag[2*index] = '\0';
return temp_etag;
}
#endif /*WITH_COAP*/
void void
rest_init_framework(void) rest_init_framework(void)
{ {

View file

@ -146,7 +146,7 @@ struct rest_implementation {
/** Get request URI path */ /** Get request URI path */
int (* get_url)(void *request, const char **url); int (* get_url)(void *request, const char **url);
int (* set_url)(void *request, char *url); int (* set_url)(void *request, const char *url);
/** Get the method of a request. */ /** Get the method of a request. */
rest_resource_flags_t (* get_method_type)(void *request); rest_resource_flags_t (* get_method_type)(void *request);
@ -160,7 +160,7 @@ struct rest_implementation {
/** Set the content-type of a response. */ /** Set the content-type of a response. */
int (* set_header_content_type)(void *response, unsigned int content_type); int (* set_header_content_type)(void *response, unsigned int content_type);
int (* get_header_accept)(void *request, uint16_t **accept); int (* get_header_accept)(void *request, const uint16_t **accept);
/** Get the Max-Age option of a request. */ /** Get the Max-Age option of a request. */
int (* get_header_max_age)(void *request, uint32_t *age); int (* get_header_max_age)(void *request, uint32_t *age);
@ -169,7 +169,7 @@ struct rest_implementation {
int (* set_header_max_age)(void *response, uint32_t age); int (* set_header_max_age)(void *response, uint32_t age);
/** Set the ETag option of a response. */ /** Set the ETag option of a response. */
int (* set_header_etag)(void *response, uint8_t *etag, size_t length); int (* set_header_etag)(void *response, const uint8_t *etag, size_t length);
/** Get the If-Match option of a request. */ /** Get the If-Match option of a request. */
int (* get_header_if_match)(void *request, const uint8_t **etag); int (* get_header_if_match)(void *request, const uint8_t **etag);
@ -181,7 +181,7 @@ struct rest_implementation {
int (* get_header_host)(void *request, const char **host); int (* get_header_host)(void *request, const char **host);
/** Set the location option of a response. */ /** Set the location option of a response. */
int (* set_header_location)(void *response, char *location); int (* set_header_location)(void *response, const char *location);
/** Get the payload option of a request. */ /** Get the payload option of a request. */
int (* get_request_payload)(void *request, const uint8_t **payload); int (* get_request_payload)(void *request, const uint8_t **payload);

View file

@ -14,7 +14,7 @@ WITH_UIP6=1
UIP_CONF_IPV6=1 UIP_CONF_IPV6=1
# variable for this Makefile # variable for this Makefile
# configure CoAP implementation (3|6|7) # configure CoAP implementation (3|7)
WITH_COAP=7 WITH_COAP=7
# must be CFLAGS not variables # must be CFLAGS not variables
@ -33,12 +33,6 @@ CFLAGS += -DWITH_COAP=7
CFLAGS += -DREST=coap_rest_implementation CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0 CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-07 APPS += er-coap-07
else ifeq ($(WITH_COAP), 6)
${info INFO: compiling with CoAP-06}
CFLAGS += -DWITH_COAP=6
CFLAGS += -DREST=coap_rest_implementation
CFLAGS += -DUIP_CONF_TCP=0
APPS += er-coap-06
else ifeq ($(WITH_COAP), 3) else ifeq ($(WITH_COAP), 3)
${info INFO: compiling with CoAP-03} ${info INFO: compiling with CoAP-03}
CFLAGS += -DWITH_COAP=3 CFLAGS += -DWITH_COAP=3
@ -50,7 +44,7 @@ ${info INFO: compiling with HTTP}
CFLAGS += -DWITH_HTTP CFLAGS += -DWITH_HTTP
CFLAGS += -DREST=http_rest_implementation CFLAGS += -DREST=http_rest_implementation
CFLAGS += -DUIP_CONF_TCP=1 CFLAGS += -DUIP_CONF_TCP=1
APPS += rest-http-engine APPS += er-http-engine
endif endif
APPS += erbium APPS += erbium

View file

@ -8,12 +8,16 @@ coap-client-example.c: A CoAP client that polls the /toggle resource every 10 se
PRELIMINARIES PRELIMINARIES
------------- -------------
a) For convenience, define the Cooja addresses in /etc/hosts a) Make sure rpl-border-router has the same stack and fits into mote memory, e.g.:
Disable RDC in border-router project-conf.h
#undef NETSTACK_CONF_RDC
#define NETSTACK_CONF_RDC nullrdc_driver
b) For convenience, define the Cooja addresses in /etc/hosts
aaaa::0212:7401:0001:0101 cooja1 aaaa::0212:7401:0001:0101 cooja1
aaaa::0212:7402:0002:0202 cooja2 aaaa::0212:7402:0002:0202 cooja2
... ...
b) Get the Copper CoAP browser from https://addons.mozilla.org/en-US/firefox/addon/copper-270430/ c) Get the Copper CoAP browser from https://addons.mozilla.org/en-US/firefox/addon/copper-270430/
c) Optional: Save Tmotes as default target d) Optional: Save Tmotes as default target
$ make TARGET=sky savetarget $ make TARGET=sky savetarget
COOJA HOWTO COOJA HOWTO
@ -26,8 +30,10 @@ Server only:
With client: With client:
1) $ make TARGET=cooja coap-client-server-example.csc 1) $ make TARGET=cooja coap-client-server-example.csc
2) Wait until red LED toggles on mote 2 (server) 2) Open new terminal
3) Choose "Click button on Sky 3" from the context menu of mote 3 (client) and watch serial output 3) $ make connect-router-cooja
4) Wait until red LED toggles on mote 2 (server)
5) Choose "Click button on Sky 3" from the context menu of mote 3 (client) and watch serial output
TMOTES HOWTO TMOTES HOWTO
------------ ------------
@ -50,7 +56,7 @@ Add a client:
DETAILS DETAILS
------- -------
The Erbium CoAP currently implements draft 07. The Erbium CoAP currently implements draft 08.
Central features are commented in rest-server-example.c. Central features are commented in rest-server-example.c.
In general, apps/er-coap-07 supports: In general, apps/er-coap-07 supports:
* All CoAP-07 header options * All CoAP-07 header options
@ -60,6 +66,15 @@ In general, apps/er-coap-07 supports:
* Resource discovery * Resource discovery
* Observing Resources (see EVENT_ and PRERIODIC_RESOURCE, note COAP_MAX_OBSERVERS) * Observing Resources (see EVENT_ and PRERIODIC_RESOURCE, note COAP_MAX_OBSERVERS)
REST IMPLEMENTATIONS
--------------------
The Makefile uses WITH_COAP to configure different implementations for the Erbium REST Engine.
* WITH_COAP=7 uses Erbium CoAP 07 apps/er-coap-07/.
The default port for coap-07 is 5683.
* WITH_COAP=3 uses Erbium CoAP 03 apps/er-coap-03/.
The default port for coap-03 is 61616.
* WITH_COAP=0 is a stub to link an Erbium HTTP engine that uses the same resource abstraction (REST.x() functions and RESOURCE macros.
TODOs TODOs
----- -----
* Blockwise uploads (for POST/PUT payload) * Blockwise uploads (for POST/PUT payload)

View file

@ -57,7 +57,7 @@
/* Must be <= open transaction number. */ /* Must be <= open transaction number. */
#ifndef COAP_MAX_OBSERVERS #ifndef COAP_MAX_OBSERVERS
#define COAP_MAX_OBSERVERS COAP_MAX_OPEN_TRANSACTIONS #define COAP_MAX_OBSERVERS COAP_MAX_OPEN_TRANSACTIONS-1
#endif #endif

View file

@ -84,8 +84,6 @@
/* For CoAP-specific example: not required for normal RESTful Web service. */ /* For CoAP-specific example: not required for normal RESTful Web service. */
#if WITH_COAP == 3 #if WITH_COAP == 3
#include "er-coap-03.h" #include "er-coap-03.h"
#elif WITH_COAP == 6
#include "er-coap-06.h"
#elif WITH_COAP == 7 #elif WITH_COAP == 7
#include "er-coap-07.h" #include "er-coap-07.h"
#else #else
@ -227,7 +225,7 @@ mirror_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
{ {
strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "Bl %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size); strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "Bl %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size);
} }
#elif WITH_COAP >= 5 #else
if (strpos<=REST_MAX_CHUNK_SIZE && (len = coap_get_header_location_path(request, &str))) if (strpos<=REST_MAX_CHUNK_SIZE && (len = coap_get_header_location_path(request, &str)))
{ {
strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "LP %.*s\n", len, str); strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "LP %.*s\n", len, str);
@ -240,15 +238,15 @@ mirror_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
{ {
strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "B2 %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size); strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "B2 %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size);
} }
if (strpos<=REST_MAX_CHUNK_SIZE && coap_get_header_block1(request, &block_num, &block_more, &block_size, NULL)) /* This getter allows NULL pointers to get only a subset of the block parameters. */ /*
* Critical Block1 option is currently rejected by engine.
*
if (strpos<=REST_MAX_CHUNK_SIZE && coap_get_header_block1(request, &block_num, &block_more, &block_size, NULL))
{ {
strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "B1 %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size); strpos += snprintf((char *)buffer+strpos, REST_MAX_CHUNK_SIZE-strpos+1, "B1 %lu%s (%u)\n", block_num, block_more ? "+" : "", block_size);
} }
#if WITH_COAP >= 7 */
#endif /* CoAP > 03 */
#endif
#endif
#endif /* CoAP-specific example */ #endif /* CoAP-specific example */
if (strpos<=REST_MAX_CHUNK_SIZE && (len = REST.get_query(request, &query))) if (strpos<=REST_MAX_CHUNK_SIZE && (len = REST.get_query(request, &query)))
@ -281,16 +279,13 @@ mirror_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
coap_set_header_observe(response, 10); coap_set_header_observe(response, 10);
#if WITH_COAP == 3 #if WITH_COAP == 3
coap_set_header_block(response, 42, 0, 64); /* The block option might be overwritten by the framework when blockwise transfer is requested. */ coap_set_header_block(response, 42, 0, 64); /* The block option might be overwritten by the framework when blockwise transfer is requested. */
#elif WITH_COAP >= 5 #else
coap_set_header_proxy_uri(response, "ftp://x"); coap_set_header_proxy_uri(response, "ftp://x");
coap_set_header_block2(response, 42, 0, 64); /* The block option might be overwritten by the framework when blockwise transfer is requested. */ coap_set_header_block2(response, 42, 0, 64); /* The block option might be overwritten by the framework when blockwise transfer is requested. */
coap_set_header_block1(response, 23, 0, 16); coap_set_header_block1(response, 23, 0, 16);
#if WITH_COAP >= 7
coap_set_header_accept(response, TEXT_PLAIN); coap_set_header_accept(response, TEXT_PLAIN);
coap_set_header_if_none_match(response); coap_set_header_if_none_match(response);
#endif #endif /* CoAP > 03 */
#endif
#endif /* CoAP-specific example */ #endif /* CoAP-specific example */
} }
#endif /* REST_RES_MIRROR */ #endif /* REST_RES_MIRROR */
@ -497,7 +492,7 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
uint16_t light_photosynthetic = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC); uint16_t light_photosynthetic = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
uint16_t light_solar = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR); uint16_t light_solar = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);
uint16_t *accept = NULL; const uint16_t *accept = NULL;
int num = REST.get_header_accept(request, &accept); int num = REST.get_header_accept(request, &accept);
if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN)) if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))
@ -537,7 +532,7 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
{ {
int battery = battery_sensor.value(0); int battery = battery_sensor.value(0);
uint16_t *accept = NULL; const uint16_t *accept = NULL;
int num = REST.get_header_accept(request, &accept); int num = REST.get_header_accept(request, &accept);
if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN)) if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))