Fixed legacy CoAP versions.

This commit is contained in:
Matthias Kovatsch 2013-02-13 19:22:21 +01:00
parent a522357d20
commit 82884ab63f
10 changed files with 50 additions and 30 deletions

View File

@ -484,6 +484,8 @@ const struct rest_implementation coap_rest_implementation = {
coap_get_header_content_type,
coap_set_header_content_type,
NULL,
NULL,
NULL,
coap_get_header_max_age,
coap_set_header_max_age,
coap_set_header_etag,
@ -518,8 +520,9 @@ const struct rest_implementation coap_rest_implementation = {
METHOD_NOT_ALLOWED_405,
NOT_FOUND_404,
METHOD_NOT_ALLOWED_405,
UNSUPPORTED_MEDIA_TYPE_415,
BAD_REQUEST_400,
UNSUPPORTED_MADIA_TYPE_415,
UNSUPPORTED_MEDIA_TYPE_415,
INTERNAL_SERVER_ERROR_500,
CRITICAL_OPTION_NOT_SUPPORTED,

View File

@ -122,39 +122,47 @@ coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token,
}
/*-----------------------------------------------------------------------------------*/
void
coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len)
coap_notify_observers(resource_t *resource, uint16_t obs_counter, void *notification)
{
coap_packet_t *const coap_res = (coap_packet_t *) notification;
coap_observer_t* obs = NULL;
uint8_t preferred_type = coap_res->type;
PRINTF("Observing: Notification from %s\n", resource->url);
/* Iterate over observers. */
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
{
if (obs->url==url) /* using RESOURCE url string as handle */
if (obs->url==resource->url) /* using RESOURCE url pointer as handle */
{
coap_transaction_t *transaction = NULL;
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers. */
if ( (transaction = coap_new_transaction(coap_get_tid(), &obs->addr, obs->port)) )
{
PRINTF(" Observer ");
PRINT6ADDR(&obs->addr);
PRINTF(":%u\n", obs->port);
/* Prepare response */
coap_res->tid = transaction->tid;
coap_set_header_observe(coap_res, obs_counter);
coap_set_header_token(coap_res, obs->token, obs->token_len);
/* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */
if (stimer_expired(&obs->refresh_timer))
{
PRINTF("Observing: Refresh client with CON\n");
type = COAP_TYPE_CON;
PRINTF(" Refreshing with CON\n");
coap_res->type = COAP_TYPE_CON;
stimer_restart(&obs->refresh_timer);
}
else
{
coap_res->type = preferred_type;
}
/* prepare response */
coap_packet_t push[1]; /* This way the packet can be treated as pointer as usual. */
coap_init_message(push, (coap_message_type_t)type, OK_200, transaction->tid );
coap_set_header_observe(push, observe);
coap_set_header_token(push, obs->token, obs->token_len);
coap_set_payload(push, payload, payload_len);
transaction->packet_len = coap_serialize_message(push, transaction->packet);
PRINTF("Observing: Notify from /%s for ", url);
PRINT6ADDR(&obs->addr);
PRINTF(":%u\n", obs->port);
PRINTF(" %.*s\n", payload_len, payload);
transaction->packet_len = coap_serialize_message(coap_res, transaction->packet);
coap_send_transaction(transaction);
}

View File

@ -69,7 +69,7 @@ coap_observer_t *coap_add_observer(uip_ipaddr_t *addr, uint16_t port, const uint
void coap_remove_observer(coap_observer_t *o);
int coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port);
int coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, uint8_t *token, size_t token_len);
void coap_notify_observers(const char *url, int type, uint32_t observe, uint8_t *payload, size_t payload_len);
void coap_notify_observers(resource_t *resource, uint16_t obs_counter, void *notification);
void coap_observe_handler(resource_t *resource, void *request, void *response);

View File

@ -745,7 +745,7 @@ coap_set_header_uri_query(void *packet, const char *query)
/*- PAYLOAD -------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
int
coap_get_payload(void *packet, uint8_t **payload)
coap_get_payload(void *packet, const uint8_t **payload)
{
if (((coap_packet_t *)packet)->payload) {
*payload = ((coap_packet_t *)packet)->payload;

View File

@ -120,7 +120,7 @@ typedef enum {
BAD_REQUEST_400 = 160,
NOT_FOUND_404 = 164,
METHOD_NOT_ALLOWED_405 = 165,
UNSUPPORTED_MADIA_TYPE_415 = 175,
UNSUPPORTED_MEDIA_TYPE_415 = 175,
INTERNAL_SERVER_ERROR_500 = 200,
BAD_GATEWAY_502 = 202,
SERVICE_UNAVAILABLE_503 = 203,
@ -278,7 +278,7 @@ int coap_set_header_block(void *packet, uint32_t num, uint8_t more, uint16_t siz
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, const char *query);
int coap_get_payload(void *packet, uint8_t **payload);
int coap_get_payload(void *packet, const uint8_t **payload);
int coap_set_payload(void *packet, const void *payload, size_t length);
#endif /* COAP_03_H_ */

View File

@ -537,6 +537,8 @@ const struct rest_implementation coap_rest_implementation = {
coap_get_header_content_type,
coap_set_header_content_type,
coap_get_header_accept,
NULL,
NULL,
coap_get_header_max_age,
coap_set_header_max_age,
coap_set_header_etag,
@ -564,14 +566,17 @@ const struct rest_implementation coap_rest_implementation = {
CHANGED_2_04,
DELETED_2_02,
VALID_2_03,
BAD_REQUEST_4_00,
UNAUTHORIZED_4_01,
BAD_OPTION_4_02,
FORBIDDEN_4_03,
NOT_FOUND_4_04,
METHOD_NOT_ALLOWED_4_05,
NOT_ACCEPTABLE_4_06,
REQUEST_ENTITY_TOO_LARGE_4_13,
UNSUPPORTED_MADIA_TYPE_4_15,
UNSUPPORTED_MEDIA_TYPE_4_15,
INTERNAL_SERVER_ERROR_5_00,
NOT_IMPLEMENTED_5_01,
BAD_GATEWAY_5_02,

View File

@ -1177,7 +1177,7 @@ coap_set_header_block1(void *packet, uint32_t num, uint8_t more, uint16_t size)
/*- PAYLOAD -------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
int
coap_get_payload(void *packet, uint8_t **payload)
coap_get_payload(void *packet, const uint8_t **payload)
{
coap_packet_t *const coap_pkt = (coap_packet_t *) packet;

View File

@ -129,9 +129,10 @@ typedef enum {
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
UNSUPPORTED_MADIA_TYPE_4_15 = 143, /* UNSUPPORTED_MADIA_TYPE */
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
@ -315,7 +316,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, uint8_t **payload);
int coap_get_payload(void *packet, const uint8_t **payload);
int coap_set_payload(void *packet, const void *payload, size_t length);
#endif /* COAP_07_H_ */

View File

@ -440,7 +440,7 @@ separate_finalize_handler()
coap_packet_t response[1]; /* This way the packet can be treated as pointer as usual. */
/* Restore the request information for the response. */
coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05);
coap_separate_resume(response, &separate_store->request_metadata, REST.status.OK);
coap_set_payload(response, separate_store->buffer, strlen(separate_store->buffer));
@ -505,7 +505,7 @@ pushing_periodic_handler(resource_t *r)
/* Build notification. */
coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */
coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0 );
coap_init_message(notification, COAP_TYPE_NON, REST.status.OK, 0 );
coap_set_payload(notification, content, snprintf(content, sizeof(content), "TICK %u", obs_counter));
/* Notify the registered observers with the given message type, observe option, and payload. */
@ -547,7 +547,7 @@ event_event_handler(resource_t *r)
/* Build notification. */
coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */
coap_init_message(notification, COAP_TYPE_CON, CONTENT_2_05, 0 );
coap_init_message(notification, COAP_TYPE_CON, REST.status.OK, 0 );
coap_set_payload(notification, content, snprintf(content, sizeof(content), "EVENT %u", event_counter));
/* Notify the registered observers with the given message type, observe option, and payload. */

View File

@ -627,7 +627,10 @@ path_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_
#if REST_RES_SEPARATE
/* Required to manually (=not by the engine) handle the response transaction. */
#if WITH_COAP == 12
#if WITH_COAP == 7
#include "er-coap-07-separate.h"
#include "er-coap-07-transactions.h"
#elif WITH_COAP == 12
#include "er-coap-12-separate.h"
#include "er-coap-12-transactions.h"
#elif WITH_COAP == 13