Erbium code cleanup.

This commit is contained in:
Matthias Kovatsch 2012-01-28 18:52:14 +01:00
parent 4c3e858df5
commit 2240289d1f
9 changed files with 430 additions and 340 deletions

View file

@ -167,7 +167,7 @@ handle_incoming_data(void)
PRINTF("handle_incoming_data(): block_offset >= response->payload_len\n");
response->code = BAD_OPTION_4_02;
coap_set_payload(response, (uint8_t*)"BlockOutOfScope", 15);
coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */
}
else
{
@ -259,7 +259,7 @@ handle_incoming_data(void)
}
/* Reuse input buffer for error message. */
coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->tid);
coap_set_payload(message, (uint8_t *) coap_error_message, strlen(coap_error_message));
coap_set_payload(message, coap_error_message, strlen(coap_error_message));
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, uip_appdata, coap_serialize_message(message, uip_appdata));
}
} /* if (new data) */
@ -396,7 +396,7 @@ well_known_core_handler(void* request, void* response, uint8_t *buffer, uint16_t
PRINTF("well_known_core_handler(): bufpos<=0\n");
coap_set_rest_status(response, BAD_OPTION_4_02);
coap_set_payload(response, (uint8_t*)"BlockOutOfScope", 15);
coap_set_payload(response, "BlockOutOfScope", 15);
}
if (resource==NULL) {

View file

@ -186,27 +186,25 @@ coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payl
void
coap_observe_handler(resource_t *resource, void *request, void *response)
{
coap_packet_t *const coap_req = (coap_packet_t *) request;
coap_packet_t *const coap_res = (coap_packet_t *) response;
static char content[26];
if (response && ((coap_packet_t *)response)->code<128) /* response without error code */
if (coap_res && coap_res->code<128) /* response without error code */
{
if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE))
if (IS_OPTION(coap_req, COAP_OPTION_OBSERVE))
{
if (!IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN))
{
/* Set default token. */
coap_set_header_token(request, (uint8_t *)"", 1);
}
if (coap_add_observer(resource->url, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len))
if (coap_add_observer(resource->url, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len))
{
coap_set_header_observe(response, 0);
coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
coap_set_header_observe(coap_res, 0);
coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
}
else
{
((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_5_03;
coap_set_payload(response, (uint8_t *)"Too many observers", 18);
coap_res->code = SERVICE_UNAVAILABLE_5_03;
coap_set_payload(coap_res, "TooManyObservers", 16);
} /* if (added observer) */
}
else /* if (observe) */

View file

@ -55,15 +55,18 @@
/*-----------------------------------------------------------------------------------*/
void coap_separate_handler(resource_t *resource, void *request, void *response)
{
coap_packet_t *const coap_req = (coap_packet_t *) request;
coap_packet_t *const coap_res = (coap_packet_t *) response;
PRINTF("Separate response for /%s \n", resource->url);
/* send separate ACK. */
coap_packet_t ack[1];
/* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, ((coap_packet_t *)request)->tid);
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->tid);
/* Should only overwrite Header which is already parsed to request. */
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata + uip_ext_len), coap_serialize_message(ack, (uip_appdata + uip_ext_len)));
/* Change response to separate response. */
((coap_packet_t *)response)->type = COAP_TYPE_CON;
((coap_packet_t *)response)->tid = coap_get_tid();
coap_res->type = COAP_TYPE_CON;
coap_res->tid = coap_get_tid();
}

File diff suppressed because it is too large Load diff

View file

@ -313,7 +313,7 @@ int coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t si
int coap_get_header_block1(void *packet, uint32_t *num, uint8_t *more, uint16_t *size, uint32_t *offset);
int coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size);
int coap_get_payload(void *packet, const uint8_t **payload);
int coap_set_payload(void *packet, uint8_t *payload, size_t length);
int coap_get_payload(void *packet, uint8_t **payload);
int coap_set_payload(void *packet, const void *payload, size_t length);
#endif /* COAP_07_H_ */

View file

@ -184,10 +184,10 @@ struct rest_implementation {
int (* set_header_location)(void *response, const char *location);
/** Get the payload option of a request. */
int (* get_request_payload)(void *request, const uint8_t **payload);
int (* get_request_payload)(void *request, uint8_t **payload);
/** Set the payload option of a response. */
int (* set_response_payload)(void *response, uint8_t *payload, size_t length);
int (* set_response_payload)(void *response, const void *payload, size_t length);
/** Get the query string of a request. */
int (* get_query)(void *request, const char **value);

View file

@ -59,7 +59,7 @@ DETAILS
The Erbium CoAP currently implements draft 08.
Central features are commented in rest-server-example.c.
In general, apps/er-coap-07 supports:
* All CoAP-07 header options
* All draft 08 header options
* CON Retransmissions (note COAP_MAX_OPEN_TRANSACTIONS)
* Blockwise Transfers (note REST_MAX_CHUNK_SIZE)
* Separate Responses (see rest_set_pre_handler() and coap_separate_handler())
@ -73,6 +73,7 @@ The Makefile uses WITH_COAP to configure different implementations for the Erbiu
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.
er-coap-03 produces some warnings, as it not fully maintained anymore.
* 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

View file

@ -99,7 +99,7 @@ static int uri_switch = 0;
void
client_chunk_handler(void *response)
{
const uint8_t *chunk;
uint8_t *chunk;
int len = coap_get_payload(response, &chunk);
printf("|%.*s", len, (char *)chunk);
}
@ -128,16 +128,13 @@ PROCESS_THREAD(coap_client_example, ev, data)
if (etimer_expired(&et)) {
printf("--Toggle timer--\n");
#if PLATFORM_HAS_LEDS
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0 );
coap_set_header_uri_path(request, service_urls[1]);
coap_set_payload(request, (uint8_t *)"Toggle!", 8);
#else
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0 );
coap_set_header_uri_path(request, "hello");
#endif
const char msg[] = "Toggle!";
coap_set_payload(request, (uint8_t *)msg, sizeof(msg)-1);
PRINT6ADDR(&server_ipaddr);
PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT));

View file

@ -158,7 +158,7 @@ mirror_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
/* The other getters copy the value (or string/array pointer) to the given pointers and return 1 for success or the length of strings/arrays. */
uint32_t max_age = 0;
const char *str = "";
const char *str = NULL;
uint32_t observe = 0;
const uint8_t *bytes = NULL;
uint32_t block_num = 0;
@ -312,7 +312,9 @@ chunks_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
{
REST.set_response_status(response, REST.status.BAD_OPTION);
/* A block error message should not exceed the minimum block size (16). */
REST.set_response_payload(response, (uint8_t*)"BlockOutOfScope", 15);
const char error_msg[] = "BlockOutOfScope";
REST.set_response_payload(response, (uint8_t *)error_msg, sizeof(error_msg)-1);
return;
}
@ -359,7 +361,10 @@ void
polling_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response, (uint8_t *)"It's periodic!", 14);
/* Usually, a CoAP server would response with the current resource representation. */
const char msg[] = "It's periodic!";
REST.set_response_payload(response, (uint8_t *)msg, sizeof(msg)-1);
/* A post_handler that handles subscriptions will be called for periodic resources by the REST framework. */
}
@ -397,7 +402,10 @@ void
event_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response, (uint8_t *)"It's eventful!", 14);
/* Usually, a CoAP server would response with the current resource representation. */
const char msg[] = "It's eventful!";
REST.set_response_payload(response, (uint8_t *)msg, sizeof(msg)-1);
/* A post_handler that handles subscriptions/observing will be called for periodic resources by the framework. */
}
@ -519,7 +527,8 @@ light_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred
else
{
REST.set_response_status(response, REST.status.UNSUPPORTED_MADIA_TYPE);
REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain, application/xml, and application/json", 74);
const char msg[] = "Supporting content-types text/plain, application/xml, and application/json";
REST.set_response_payload(response, (uint8_t *)msg, sizeof(msg)-1);
}
}
#endif /* PLATFORM_HAS_LIGHT */
@ -552,7 +561,8 @@ battery_handler(void* request, void* response, uint8_t *buffer, uint16_t preferr
else
{
REST.set_response_status(response, REST.status.UNSUPPORTED_MADIA_TYPE);
REST.set_response_payload(response, (uint8_t *)"Supporting content-types text/plain and application/json", 56);
const char msg[] = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, (uint8_t *)msg, sizeof(msg)-1);
}
}
#endif /* PLATFORM_HAS_BATTERY */