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");
/*-----------------------------------------------------------------------------------*/
/*- Variables -----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*- Variables ----------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
static service_callback_t service_cbk = NULL;
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
static
int
handle_incoming_data(void)
@ -279,27 +279,27 @@ handle_incoming_data(void)
return coap_error_code;
}
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void
coap_receiver_init()
{
process_start(&coap_receiver, NULL);
}
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void
coap_set_service_callback(service_callback_t callback)
{
service_cbk = callback;
}
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
rest_resource_flags_t
coap_get_rest_method(void *packet)
{
return (rest_resource_flags_t)(1 << (((coap_packet_t *)packet)->code - 1));
}
/*-----------------------------------------------------------------------------------*/
/*- Server part ---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*- Server part --------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* The discover resource is automatically included for CoAP. */
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;
}
}
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
PROCESS_THREAD(coap_receiver, ev, data)
{
PROCESS_BEGIN();
@ -433,15 +433,15 @@ PROCESS_THREAD(coap_receiver, ev, data)
PROCESS_END();
}
/*-----------------------------------------------------------------------------------*/
/*- Client part ---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*- Client part --------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void blocking_request_callback(void *callback_data, void *response) {
struct request_state_t *state = (struct request_state_t *) callback_data;
state->response = (coap_packet_t*) response;
process_poll(state->process);
}
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
PT_THREAD(coap_blocking_request(struct request_state_t *state, process_event_t ev,
uip_ipaddr_t *remote_ipaddr, uint16_t remote_port,
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);
}
/*-----------------------------------------------------------------------------------*/
/*- Engine Interface ----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*- Engine Interface ---------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
const struct rest_implementation coap_rest_implementation = {
"CoAP-07",

View File

@ -53,41 +53,40 @@
#define PRINTLLADDR(addr)
#endif
/*-----------------------------------------------------------------------------------*/
int coap_separate_handler(resource_t *resource, void *request, void *response)
{
coap_packet_t *const coap_req = (coap_packet_t *) request;
PRINTF("Separate response for /%s MID %u\n", resource->url, coap_res->mid);
/* Only ack CON requests. */
if (coap_req->type==COAP_TYPE_CON)
{
/* send separate ACK. */
coap_packet_t ack[1];
/* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
/* Serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct. */
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. */
return 1;
}
/*----------------------------------------------------------------------------*/
int
coap_separate_yield(void *request, coap_separate_t *separate_store)
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_transaction_t *const t = coap_get_transaction_by_mid(coap_req->mid);
PRINTF("Separate ACCEPT: /%.*s MID %u\n", coap_req->uri_path_len, coap_req->uri_path, coap_req->mid);
if (t)
{
/* Send separate ACK for CON. */
if (coap_req->type==COAP_TYPE_CON)
{
coap_packet_t ack[1];
/* ACK with empty code (0) */
coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
/* Serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct. */
coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, (uip_appdata), coap_serialize_message(ack, uip_appdata));
}
/* Store remote address. */
uip_ipaddr_copy(&separate_store->addr, &t->addr);
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->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);
separate_store->token_len = coap_req->token_len;
@ -102,13 +101,17 @@ coap_separate_yield(void *request, coap_separate_t *separate_store)
}
else
{
PRINTF("ERROR: Response transaction for separate request not found!\n");
return 0;
}
}
/*----------------------------------------------------------------------------*/
void
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_set_header_token(response, separate_store->token, separate_store->token_len);
if (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;
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);
#endif /* COAP_SEPARATE_H_ */

View File

@ -207,10 +207,8 @@ separate_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
PRINTF("/separate ");
if (separate_active)
{
PRINTF("BUSY ");
REST.set_response_status(response, REST.status.SERVICE_UNAVAILABLE);
const char *msg = "AlreadyInUse";
REST.set_response_payload(response, msg, strlen(msg));
PRINTF("REJECTED ");
coap_separate_reject();
}
else
{
@ -218,7 +216,7 @@ separate_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
separate_active = 1;
/* 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. */
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);
#endif
#if REST_RES_SEPARATE
rest_set_pre_handler(&resource_separate, coap_separate_handler);
rest_activate_periodic_resource(&periodic_resource_separate);
#endif
#if REST_RES_LARGE