Changed separate API and fixed token bug.

This commit is contained in:
Matthias Kovatsch 2012-03-23 14:49:52 +01:00 committed by U-tiLaptop\merliin
parent c7c7bcbf81
commit d102d8c607
4 changed files with 54 additions and 53 deletions

View file

@ -67,13 +67,13 @@
PROCESS(coap_receiver, "CoAP Receiver"); PROCESS(coap_receiver, "CoAP Receiver");
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*- Variables -----------------------------------------------------------------------*/ /*- Variables ----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
static service_callback_t service_cbk = NULL; static service_callback_t service_cbk = NULL;
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
static static
int int
handle_incoming_data(void) handle_incoming_data(void)
@ -279,27 +279,27 @@ handle_incoming_data(void)
return coap_error_code; return coap_error_code;
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void void
coap_receiver_init() coap_receiver_init()
{ {
process_start(&coap_receiver, NULL); process_start(&coap_receiver, NULL);
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void void
coap_set_service_callback(service_callback_t callback) coap_set_service_callback(service_callback_t callback)
{ {
service_cbk = callback; service_cbk = callback;
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
rest_resource_flags_t rest_resource_flags_t
coap_get_rest_method(void *packet) coap_get_rest_method(void *packet)
{ {
return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1)); return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1));
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*- Server part ---------------------------------------------------------------------*/ /*- Server part --------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/* The discover resource is automatically included for CoAP. */ /* The discover resource is automatically included for CoAP. */
RESOURCE(well_known_core, METHOD_GET, ".well-known/core", "ct=40"); RESOURCE(well_known_core, METHOD_GET, ".well-known/core", "ct=40");
@ -408,7 +408,7 @@ well_known_core_handler(void* request, void* response, uint8_t *buffer, uint16_t
*offset += preferred_size; *offset += preferred_size;
} }
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
PROCESS_THREAD(coap_receiver, ev, data) PROCESS_THREAD(coap_receiver, ev, data)
{ {
PROCESS_BEGIN(); PROCESS_BEGIN();
@ -433,15 +433,15 @@ PROCESS_THREAD(coap_receiver, ev, data)
PROCESS_END(); PROCESS_END();
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*- Client part ---------------------------------------------------------------------*/ /*- Client part --------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
void blocking_request_callback(void *callback_data, void *response) { void blocking_request_callback(void *callback_data, void *response) {
struct request_state_t *state = (struct request_state_t *) callback_data; struct request_state_t *state = (struct request_state_t *) callback_data;
state->response = (coap_packet_t*) response; state->response = (coap_packet_t*) response;
process_poll(state->process); process_poll(state->process);
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev, PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port, uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
coap_packet_t *request, coap_packet_t *request,
@ -509,9 +509,9 @@ PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t e
PT_END(&state->pt); PT_END(&state->pt);
} }
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
/*- Engine Interface ----------------------------------------------------------------*/ /*- Engine Interface ---------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
const struct rest_implementation coap_rest_implementation = { const struct rest_implementation coap_rest_implementation = {
"CoAP-07", "CoAP-07",

View file

@ -53,17 +53,26 @@
#define PRINTLLADDR(addr) #define PRINTLLADDR(addr)
#endif #endif
/*-----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
int coap_separate_handler(resource_t *resource, void *request, void *response) int
coap_separate_reject()
{
coap_error_code = SERVICE_UNAVAILABLE_5_03;
coap_error_message = "AlreadyInUse";
}
/*----------------------------------------------------------------------------*/
int
coap_separate_accept(void *request, coap_separate_t *separate_store)
{ {
coap_packet_t *const coap_req = (coap_packet_t *) request; coap_packet_t *const coap_req = (coap_packet_t *) request;
coap_transaction_t *const t = coap_get_transaction_by_mid(coap_req->mid);
PRINTF("Separate response for /%s MID %u\n", resource->url, coap_res->mid); PRINTF("Separate ACCEPT: /%.*s MID %u\n", coap_req->uri_path_len, coap_req->uri_path, coap_req->mid);
if (t)
/* Only ack CON requests. */ {
/* Send separate ACK for CON. */
if (coap_req->type==COAP_TYPE_CON) if (coap_req->type==COAP_TYPE_CON)
{ {
/* send separate ACK. */
coap_packet_t ack[1]; coap_packet_t ack[1];
/* ACK with empty code (0) */ /* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid); coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
@ -71,23 +80,13 @@ int coap_separate_handler(resource_t *resource, void *request, void *response)
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata), coap_serialize_message(ack, uip_appdata)); coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata), coap_serialize_message(ack, uip_appdata));
} }
/* Pre-handlers could skip the handling by returning 0. */ /* Store remote address. */
return 1;
}
int
coap_separate_yield(void *request, coap_separate_t *separate_store)
{
coap_packet_t *const coap_req = (coap_packet_t *) request;
coap_transaction_t *const t = coap_get_transaction_by_mid(coap_req->mid);
if (t)
{
uip_ipaddr_copy(&separate_store->addr, &t->addr); uip_ipaddr_copy(&separate_store->addr, &t->addr);
separate_store->port = t->port; separate_store->port = t->port;
/* Store correct response type. */
separate_store->type = coap_req->type==COAP_TYPE_CON ? COAP_TYPE_CON : COAP_TYPE_NON; separate_store->type = coap_req->type==COAP_TYPE_CON ? COAP_TYPE_CON : COAP_TYPE_NON;
separate_store->mid = coap_get_mid(); // if it was NON, we burned one MID in the engine... separate_store->mid = coap_get_mid(); /* if it was a NON, we burned one MID in the engine... */
memcpy(separate_store->token, coap_req->token, coap_req->token_len); memcpy(separate_store->token, coap_req->token, coap_req->token_len);
separate_store->token_len = coap_req->token_len; separate_store->token_len = coap_req->token_len;
@ -102,13 +101,17 @@ coap_separate_yield(void *request, coap_separate_t *separate_store)
} }
else else
{ {
PRINTF("ERROR: Response transaction for separate request not found!\n");
return 0; return 0;
} }
} }
/*----------------------------------------------------------------------------*/
void void
coap_separate_resume(void *response, coap_separate_t *separate_store, uint8_t code) coap_separate_resume(void *response, coap_separate_t *separate_store, uint8_t code)
{ {
coap_init_message(response, separate_store->type, code, separate_store->mid); coap_init_message(response, separate_store->type, code, separate_store->mid);
if (separate_store->token_len)
{
coap_set_header_token(response, separate_store->token, separate_store->token_len); coap_set_header_token(response, separate_store->token, separate_store->token_len);
} }
}

View file

@ -59,7 +59,8 @@ typedef struct coap_separate {
} coap_separate_t; } coap_separate_t;
int coap_separate_handler(resource_t *resource, void *request, void *response); int coap_separate_handler(resource_t *resource, void *request, void *response);
int coap_separate_yield(void *request, coap_separate_t *separate_store); int coap_separate_reject();
int coap_separate_accept(void *request, coap_separate_t *separate_store);
void coap_separate_resume(void *response, coap_separate_t *separate_store, uint8_t code); void coap_separate_resume(void *response, coap_separate_t *separate_store, uint8_t code);
#endif /* COAP_SEPARATE_H_ */ #endif /* COAP_SEPARATE_H_ */

View file

@ -207,10 +207,8 @@ separate_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
PRINTF("/separate "); PRINTF("/separate ");
if (separate_active) if (separate_active)
{ {
PRINTF("BUSY "); PRINTF("REJECTED ");
REST.set_response_status(response, REST.status.SERVICE_UNAVAILABLE); coap_separate_reject();
const char *msg = "AlreadyInUse";
REST.set_response_payload(response, msg, strlen(msg));
} }
else else
{ {
@ -218,7 +216,7 @@ separate_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
separate_active = 1; separate_active = 1;
/* Take over and skip response by engine. */ /* Take over and skip response by engine. */
coap_separate_yield(request, &separate_store->request_metadata); coap_separate_accept(request, &separate_store->request_metadata);
/* Be aware to respect the Block2 option, which is also stored in the coap_separate_t. */ /* Be aware to respect the Block2 option, which is also stored in the coap_separate_t. */
snprintf(separate_store->buffer, MAX_PLUGFEST_PAYLOAD, "Type: %u\nCode: %u\nMID: %u", coap_req->type, coap_req->code, coap_req->mid); snprintf(separate_store->buffer, MAX_PLUGFEST_PAYLOAD, "Type: %u\nCode: %u\nMID: %u", coap_req->type, coap_req->code, coap_req->mid);
@ -537,7 +535,6 @@ PROCESS_THREAD(plugtest_server, ev, data)
rest_activate_resource(&resource_query); rest_activate_resource(&resource_query);
#endif #endif
#if REST_RES_SEPARATE #if REST_RES_SEPARATE
rest_set_pre_handler(&resource_separate, coap_separate_handler);
rest_activate_periodic_resource(&periodic_resource_separate); rest_activate_periodic_resource(&periodic_resource_separate);
#endif #endif
#if REST_RES_LARGE #if REST_RES_LARGE