diff --git a/apps/er-coap-03/er-coap-03.c b/apps/er-coap-03/er-coap-03.c index b02227ba2..af20f0b5c 100644 --- a/apps/er-coap-03/er-coap-03.c +++ b/apps/er-coap-03/er-coap-03.c @@ -191,7 +191,7 @@ coap_get_tid() } /*-----------------------------------------------------------------------------------*/ 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*/ uip_ipaddr_copy(&udp_conn->ripaddr, addr); @@ -586,7 +586,7 @@ coap_get_header_etag(void *packet, const uint8_t **etag) } 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); 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 -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); SET_OPTION((coap_packet_t *)packet, COAP_OPTION_URI_HOST); @@ -615,20 +615,20 @@ coap_set_header_uri_host(void *packet, char *host) } /*-----------------------------------------------------------------------------------*/ 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; - *uri = ((coap_packet_t *)packet)->location_path; + *location = ((coap_packet_t *)packet)->location_path; return ((coap_packet_t *)packet)->location_path_len; } int -coap_set_header_location(void *packet, char *location) +coap_set_header_location(void *packet, const char *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); 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 -coap_set_header_uri_path(void *packet, char *path) +coap_set_header_uri_path(void *packet, const char *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); 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 -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); 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 -coap_set_header_uri_query(void *packet, char *query) +coap_set_header_uri_query(void *packet, const char *query) { while (query[0]=='?') ++query; diff --git a/apps/er-coap-03/er-coap-03.h b/apps/er-coap-03/er-coap-03.h index 169188aab..9b5f3a887 100644 --- a/apps/er-coap-03/er-coap-03.h +++ b/apps/er-coap-03/er-coap-03.h @@ -200,11 +200,11 @@ typedef struct { uint8_t etag_len; uint8_t etag[COAP_ETAG_LEN]; uint8_t uri_host_len; - char *uri_host; + const char *uri_host; uint8_t location_path_len; - char *location_path; + const char *location_path; uint8_t uri_path_len; - char *uri_path; + const char *uri_path; uint32_t observe; /* 0-4 bytes for coap-03 */ uint8_t token_len; uint8_t token[COAP_TOKEN_LEN]; @@ -213,7 +213,7 @@ typedef struct { uint16_t block_size; uint32_t block_offset; uint8_t uri_query_len; - char *uri_query; + const char *uri_query; uint16_t payload_len; uint8_t *payload; @@ -236,7 +236,7 @@ typedef enum void coap_init_connection(uint16_t port); 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); 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_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_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_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_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_set_header_observe(void *packet, uint32_t observe); 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_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_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_set_payload(void *packet, uint8_t *payload, size_t length); diff --git a/apps/er-coap-07/er-coap-07.c b/apps/er-coap-07/er-coap-07.c index 1e3223b2e..b87fb1928 100644 --- a/apps/er-coap-07/er-coap-07.c +++ b/apps/er-coap-07/er-coap-07.c @@ -209,6 +209,7 @@ coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t opt /* Merge multiple options. */ if (*dst_len > 0) { + /* dst already contains an option: concatenate */ (*dst)[*dst_len] = separator; *dst_len += 1; @@ -219,7 +220,8 @@ coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option, size_t opt } else { - *dst = (char *) option; /* thus pointer pointer */ + /* dst is empty: set to option */ + *dst = (char *) option; *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); break; 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); break; 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); break; 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); break; 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); break; case COAP_OPTION_OBSERVE: @@ -673,7 +678,8 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len) PRINTF("Fence-Post\n"); break; 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); break; case COAP_OPTION_BLOCK2: @@ -752,7 +758,7 @@ coap_set_header_content_type(void *packet, unsigned int content_type) } /*-----------------------------------------------------------------------------------*/ 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; @@ -802,7 +808,7 @@ coap_get_header_etag(void *packet, const uint8_t **etag) } 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); 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 -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); 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 -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); 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 -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_len = strlen(uri); @@ -892,7 +898,7 @@ coap_get_header_uri_host(void *packet, const char **host) } 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_len = strlen(host); @@ -911,7 +917,7 @@ coap_get_header_uri_path(void *packet, const char **path) } int -coap_set_header_uri_path(void *packet, char *path) +coap_set_header_uri_path(void *packet, const char *path) { while (path[0]=='/') ++path; @@ -932,7 +938,7 @@ coap_get_header_uri_query(void *packet, const char **query) } int -coap_set_header_uri_query(void *packet, char *query) +coap_set_header_uri_query(void *packet, const char *query) { while (query[0]=='?') ++query; @@ -953,7 +959,7 @@ coap_get_header_location_path(void *packet, const char **path) } int -coap_set_header_location_path(void *packet, char *path) +coap_set_header_location_path(void *packet, const char *path) { char *query; @@ -985,7 +991,7 @@ coap_get_header_location_query(void *packet, const char **query) } int -coap_set_header_location_query(void *packet, char *query) +coap_set_header_location_query(void *packet, const char *query) { while (query[0]=='?') ++query; diff --git a/apps/er-coap-07/er-coap-07.h b/apps/er-coap-07/er-coap-07.h index 13c83b1e0..f7f622404 100644 --- a/apps/er-coap-07/er-coap-07.h +++ b/apps/er-coap-07/er-coap-07.h @@ -213,18 +213,18 @@ typedef struct { coap_content_type_t content_type; /* Parse options once and store; allows setting options in random order */ uint32_t max_age; size_t proxy_uri_len; - char *proxy_uri; + const char *proxy_uri; uint8_t etag_len; uint8_t etag[COAP_ETAG_LEN]; size_t uri_host_len; - char *uri_host; + const char *uri_host; size_t location_path_len; - char *location_path; + const char *location_path; uint16_t uri_port; size_t location_query_len; - char *location_query; + const char *location_query; size_t uri_path_len; - char *uri_path; + const char *uri_path; uint16_t observe; uint8_t token_len; uint8_t token[COAP_TOKEN_LEN]; @@ -241,7 +241,7 @@ typedef struct { uint16_t block1_size; uint32_t block1_offset; size_t uri_query_len; - char *uri_query; + const char *uri_query; uint8_t if_none_match; 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); 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_get_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_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_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_set_header_if_none_match(void *packet); 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_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_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_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_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_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_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_set_header_observe(void *packet, uint32_t observe); diff --git a/apps/erbium/erbium.c b/apps/erbium/erbium.c index 007b1ee2f..53d129d8f 100644 --- a/apps/erbium/erbium.c +++ b/apps/erbium/erbium.c @@ -59,33 +59,6 @@ LIST(restful_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= 5 +#else 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); @@ -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); } - 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); } -#if WITH_COAP >= 7 - -#endif - -#endif + */ +#endif /* CoAP > 03 */ #endif /* CoAP-specific example */ 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); #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. */ -#elif WITH_COAP >= 5 +#else 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_block1(response, 23, 0, 16); -#if WITH_COAP >= 7 coap_set_header_accept(response, TEXT_PLAIN); coap_set_header_if_none_match(response); -#endif - -#endif +#endif /* CoAP > 03 */ #endif /* CoAP-specific example */ } #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_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); 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); - uint16_t *accept = NULL; + const uint16_t *accept = NULL; int num = REST.get_header_accept(request, &accept); if ((num==0) || (num && accept[0]==REST.type.TEXT_PLAIN))