Fixed token bug in separate response and extended API (thanks to Klaus Hartke for the bug report).

This commit is contained in:
Matthias Kovatsch 2012-02-06 06:21:33 +01:00
parent 2ef46ac47a
commit cd802979da
3 changed files with 28 additions and 16 deletions

View file

@ -57,15 +57,12 @@
int 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 MID %u\n", resource->url, coap_res->mid);
/* Only ack CON requests. */
if (coap_req->type==COAP_TYPE_CON)
{
coap_transaction_t *const t = coap_get_transaction_by_mid(coap_res->mid);
/* send separate ACK. */
coap_packet_t ack[1];
/* ACK with empty code (0) */
@ -79,7 +76,7 @@ int coap_separate_handler(resource_t *resource, void *request, void *response)
}
int
coap_separate_response(void *request, coap_separate_t *separate_store)
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);
@ -96,9 +93,7 @@ coap_separate_response(void *request, coap_separate_t *separate_store)
separate_store->token_len = coap_req->token_len;
separate_store->block2_num = coap_req->block2_num;
separate_store->block2_more = coap_req->block2_more;
separate_store->block2_size = coap_req->block2_size;
separate_store->block2_offset = coap_req->block2_offset;
/* Signal the engine to skip automatic response and clear transaction by engine. */
coap_error_code = MANUAL_RESPONSE;
@ -110,3 +105,10 @@ coap_separate_response(void *request, coap_separate_t *separate_store)
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);
}

View file

@ -54,13 +54,12 @@ typedef struct coap_separate {
/* separate + blockwise is untested! */
uint32_t block2_num;
uint8_t block2_more;
uint16_t block2_size;
uint32_t block2_offset;
} coap_separate_t;
int coap_separate_handler(resource_t *resource, void *request, void *response);
int coap_separate_response(void *response, coap_separate_t *separate_store);
int coap_separate_yield(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

@ -349,7 +349,7 @@ chunks_handler(void* request, void* response, uint8_t *buffer, uint16_t preferre
}
#endif
#if REST_RES_SEPARATE && WITH_COAP > 3
#if defined (PLATFORM_HAS_BUTTON) && REST_RES_SEPARATE && WITH_COAP > 3
/* Required to manually (=not by the engine) handle the response transaction. */
#include "er-coap-07-separate.h"
#include "er-coap-07-transactions.h"
@ -390,7 +390,8 @@ separate_handler(void* request, void* response, uint8_t *buffer, uint16_t prefer
separate_active = 1;
/* Take over and skip response by engine. */
coap_separate_response(request, &separate_store->request_metadata);
coap_separate_yield(request, &separate_store->request_metadata);
/* Be aware to respect the Block2 option, which is also stored in the coap_separate_t. */
/*
* At the moment, only the minimal information is stored in the store (client address, port, token, MID, type, and Block2).
@ -410,10 +411,18 @@ separate_finalize_handler()
if ( (transaction = coap_new_transaction(separate_store->request_metadata.mid, &separate_store->request_metadata.addr, separate_store->request_metadata.port)) )
{
coap_packet_t response[1]; /* This way the packet can be treated as pointer as usual. */
coap_init_message(response, separate_store->request_metadata.type, CONTENT_2_05, separate_store->request_metadata.mid);
/* Restore the request information for the response. */
coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05);
coap_set_payload(response, separate_store->buffer, strlen(separate_store->buffer));
/*
* Be aware to respect the Block2 option, which is also stored in the coap_separate_t.
* As it is a critical option, this example resource pretends to handle it for compliance.
*/
coap_set_header_block2(response, separate_store->request_metadata.block2_num, 0, separate_store->request_metadata.block2_size);
/* Warning: No check for serialization error. */
transaction->packet_len = coap_serialize_message(response, transaction->packet);
coap_send_transaction(transaction);
@ -692,14 +701,16 @@ PROCESS_THREAD(rest_server_example, ev, data)
#if REST_RES_PUSHING
rest_activate_periodic_resource(&periodic_resource_pushing);
#endif
#if REST_RES_SEPARATE && WITH_COAP > 3
#if defined (PLATFORM_HAS_BUTTON) && REST_RES_EVENT
rest_activate_event_resource(&resource_event);
#endif
#if defined (PLATFORM_HAS_BUTTON) && REST_RES_SEPARATE && WITH_COAP > 3
/* Use this pre-handler for separate response resources. */
rest_set_pre_handler(&resource_separate, coap_separate_handler);
rest_activate_resource(&resource_separate);
#endif
#if defined (PLATFORM_HAS_BUTTON) && REST_RES_EVENT
#if defined (PLATFORM_HAS_BUTTON) && (REST_RES_EVENT || (REST_RES_SEPARATE && WITH_COAP > 3))
SENSORS_ACTIVATE(button_sensor);
rest_activate_event_resource(&resource_event);
#endif
#if defined (PLATFORM_HAS_LEDS)
#if REST_RES_LEDS