54#define LOG_MODULE "coap-eng"
55#define LOG_LEVEL LOG_LEVEL_COAP
57static void process_callback(coap_timer_t *t);
63static int invoke_coap_resource_service(coap_message_t *request,
64 coap_message_t *response,
65 uint8_t *buffer, uint16_t buffer_size,
72LIST(coap_resource_services);
73static uint8_t is_initialized = 0;
79coap_add_handler(coap_handler_t *handler)
85coap_remove_handler(coap_handler_t *handler)
91coap_call_handlers(coap_message_t *request, coap_message_t *response,
92 uint8_t *buffer, uint16_t buffer_size, int32_t *offset)
94 coap_handler_status_t status;
96 for(r =
list_head(coap_handlers); r != NULL; r = r->next) {
98 status = r->handler(request, response, buffer, buffer_size, offset);
99 if(status != COAP_HANDLER_STATUS_CONTINUE) {
103 if(request->code == COAP_GET) {
104 coap_observe_handler(NULL, request, response);
111 return COAP_HANDLER_STATUS_CONTINUE;
114static inline coap_handler_status_t
115call_service(coap_message_t *request, coap_message_t *response,
116 uint8_t *buffer, uint16_t buffer_size, int32_t *offset)
118 coap_handler_status_t status;
119 status = coap_call_handlers(request, response, buffer, buffer_size, offset);
120 if(status != COAP_HANDLER_STATUS_CONTINUE) {
123 status = invoke_coap_resource_service(request, response, buffer, buffer_size, offset);
124 if(status != COAP_HANDLER_STATUS_CONTINUE) {
128 coap_set_status_code(response, NOT_FOUND_4_04);
130 return COAP_HANDLER_STATUS_CONTINUE;
137#if COAP_WELL_KNOWN_RESOURCE_ENABLED
139extern coap_resource_t res_well_known_core;
146coap_receive(
const coap_endpoint_t *src,
147 uint8_t *payload, uint16_t payload_length)
150 static coap_message_t message[1];
151 static coap_message_t response[1];
152 coap_transaction_t *transaction = NULL;
153 coap_handler_status_t status;
155 coap_status_code = coap_parse_message(message, payload, payload_length);
156 coap_set_src_endpoint(message, src);
158 if(coap_status_code == NO_ERROR) {
162 LOG_DBG(
" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version,
163 message->type, message->token_len, message->code, message->mid);
165 LOG_DBG_COAP_STRING(message->uri_path, message->uri_path_len);
167 LOG_DBG(
" Payload: ");
168 LOG_DBG_COAP_STRING((
const char *)message->payload, message->payload_len);
172 if(message->code >= COAP_GET && message->code <= COAP_DELETE) {
175 if((transaction = coap_new_transaction(message->mid, src))) {
176 uint32_t block_num = 0;
177 uint16_t block_size = COAP_MAX_BLOCK_SIZE;
178 uint32_t block_offset = 0;
179 int32_t new_offset = 0;
182 if(message->type == COAP_TYPE_CON) {
184 coap_init_message(response, COAP_TYPE_ACK, CONTENT_2_05,
188 coap_init_message(response, COAP_TYPE_NON, CONTENT_2_05,
192 if(message->token_len) {
193 coap_set_token(response, message->token, message->token_len);
196 if(coap_get_header_block2
197 (message, &block_num, NULL, &block_size, &block_offset)) {
198 LOG_DBG(
"Blockwise: block request %"PRIu32
" (%u/%u) @ %"PRIu32
" bytes\n",
199 block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset);
200 block_size = MIN(block_size, COAP_MAX_BLOCK_SIZE);
201 new_offset = block_offset;
205 LOG_DBG(
"Blockwise: block request offset overflow\n");
206 coap_status_code = BAD_OPTION_4_02;
207#if COAP_MESSAGE_ON_ERROR
208 coap_error_message =
"BlockOutOfScope";
210 status = COAP_HANDLER_STATUS_CONTINUE;
213 status = call_service(message, response,
214 transaction->message + COAP_MAX_HEADER_SIZE,
215 block_size, &new_offset);
218 if(status != COAP_HANDLER_STATUS_CONTINUE) {
220 if(coap_status_code == NO_ERROR) {
225 if(coap_is_option(message, COAP_OPTION_BLOCK1)
226 && response->code < BAD_REQUEST_4_00
227 && !coap_is_option(response, COAP_OPTION_BLOCK1)) {
228 LOG_DBG(
"Block1 NOT IMPLEMENTED\n");
230 coap_status_code = NOT_IMPLEMENTED_5_01;
231#if COAP_MESSAGE_ON_ERROR
232 coap_error_message =
"NoBlock1Support";
236 }
else if(coap_is_option(message, COAP_OPTION_BLOCK2)) {
239 if(new_offset == block_offset) {
240 LOG_DBG(
"Blockwise: unaware resource with payload length %u/%u\n",
241 response->payload_len, block_size);
242 if(block_offset >= response->payload_len) {
243 LOG_DBG(
"handle_incoming_data(): block_offset >= response->payload_len\n");
245 response->code = BAD_OPTION_4_02;
246 coap_set_payload(response,
"BlockOutOfScope", 15);
248 coap_set_header_block2(response, block_num,
249 response->payload_len -
250 block_offset > block_size,
252 coap_set_payload(response,
253 response->payload + block_offset,
254 MIN(response->payload_len -
255 block_offset, block_size));
260 LOG_DBG(
"Blockwise: blockwise resource, new offset %"PRId32
"\n",
262 coap_set_header_block2(response, block_num,
264 || response->payload_len >
265 block_size, block_size);
267 if(response->payload_len > block_size) {
268 coap_set_payload(response, response->payload,
274 }
else if(new_offset != 0) {
275 LOG_DBG(
"Blockwise: no block option for blockwise resource, using block size %u\n",
276 COAP_MAX_BLOCK_SIZE);
278 coap_set_header_block2(response, 0, new_offset != -1,
279 COAP_MAX_BLOCK_SIZE);
280 coap_set_payload(response, response->payload,
281 MIN(response->payload_len,
282 COAP_MAX_BLOCK_SIZE));
288 if(coap_status_code == NO_ERROR) {
289 if((transaction->message_len = coap_serialize_message(response,
293 coap_status_code = PACKET_SERIALIZATION_ERROR;
297 coap_status_code = SERVICE_UNAVAILABLE_5_03;
298#if COAP_MESSAGE_ON_ERROR
299 coap_error_message =
"NoFreeTraBuffer";
306 if(message->type == COAP_TYPE_CON && message->code == 0) {
307 LOG_INFO(
"Received Ping\n");
308 coap_status_code = PING_RESPONSE;
309 }
else if(message->type == COAP_TYPE_ACK) {
311 LOG_DBG(
"Received ACK\n");
312 }
else if(message->type == COAP_TYPE_RST) {
313 LOG_INFO(
"Received RST\n");
315 coap_remove_observer_by_mid(src, message->mid);
318 if((transaction = coap_get_transaction_by_mid(message->mid))) {
320 coap_resource_response_handler_t callback = transaction->callback;
321 void *callback_data = transaction->callback_data;
323 coap_clear_transaction(transaction);
327 callback(callback_data, message);
333#if COAP_OBSERVE_CLIENT
335 if((message->type == COAP_TYPE_CON || message->type == COAP_TYPE_NON)
336 && coap_is_option(message, COAP_OPTION_OBSERVE)) {
337 LOG_DBG(
"Observe [%"PRId32
"]\n", message->observe);
338 coap_handle_notification(src, message);
345 if(coap_status_code == NO_ERROR) {
347 coap_send_transaction(transaction);
349 }
else if(coap_status_code == MANUAL_RESPONSE) {
350 LOG_DBG(
"Clearing transaction for manual response");
351 coap_clear_transaction(transaction);
353 coap_message_type_t reply_type = COAP_TYPE_ACK;
355#if COAP_MESSAGE_ON_ERROR
356 LOG_WARN(
"ERROR %u: %s\n", coap_status_code, coap_error_message);
358 LOG_WARN(
"ERROR %u\n", coap_status_code);
360 coap_clear_transaction(transaction);
362 if(coap_status_code == PING_RESPONSE) {
363 coap_status_code = 0;
364 reply_type = COAP_TYPE_RST;
365 }
else if(coap_status_code >= 192) {
367 coap_status_code = INTERNAL_SERVER_ERROR_5_00;
370 coap_init_message(message, reply_type, coap_status_code,
372#if COAP_MESSAGE_ON_ERROR
373 coap_set_payload(message, coap_error_message,
374 strlen(coap_error_message));
376 coap_sendto(src, payload, coap_serialize_message(message, payload));
380 return coap_status_code;
384coap_engine_init(
void)
392 LOG_INFO(
"Starting CoAP engine...\n");
397#if COAP_WELL_KNOWN_RESOURCE_ENABLED
402 coap_init_connection();
415 coap_periodic_resource_t *periodic;
416 resource->url = path;
417 list_add(coap_resource_services, resource);
419 LOG_INFO(
"Activating: %s\n", resource->url);
422 if(resource->flags & IS_PERIODIC && resource->periodic
423 && resource->periodic->periodic_handler
424 && resource->periodic->period) {
425 LOG_DBG(
"Periodic resource: %p (%s)\n", resource->periodic, path);
426 periodic = resource->periodic;
440 return list_head(coap_resource_services);
450invoke_coap_resource_service(coap_message_t *request, coap_message_t *response,
451 uint8_t *buffer, uint16_t buffer_size,
457 coap_resource_t *resource = NULL;
458 const char *url = NULL;
459 int url_len, res_url_len;
461 url_len = coap_get_header_uri_path(request, &url);
462 for(resource =
list_head(coap_resource_services);
463 resource; resource = resource->next) {
466 res_url_len = strlen(resource->url);
467 if((url_len == res_url_len
468 || (url_len > res_url_len
469 && (resource->flags & HAS_SUB_RESOURCES)
470 && url[res_url_len] ==
'/'))
471 && strncmp(resource->url, url, res_url_len) == 0) {
475 LOG_INFO(
"/%s, method %u, resource->flags %u\n", resource->url,
476 (uint16_t)method, resource->flags);
478 if((method & METHOD_GET) && resource->get_handler != NULL) {
480 resource->get_handler(request, response, buffer, buffer_size, offset);
481 }
else if((method & METHOD_POST) && resource->post_handler != NULL) {
483 resource->post_handler(request, response, buffer, buffer_size,
485 }
else if((method & METHOD_PUT) && resource->put_handler != NULL) {
487 resource->put_handler(request, response, buffer, buffer_size, offset);
488 }
else if((method & METHOD_DELETE) && resource->delete_handler != NULL) {
490 resource->delete_handler(request, response, buffer, buffer_size,
494 coap_set_status_code(response, METHOD_NOT_ALLOWED_4_05);
500 coap_set_status_code(response, NOT_FOUND_4_04);
503 if(resource->flags & IS_OBSERVABLE) {
504 coap_observe_handler(resource, request, response);
507 return found & allowed;
512process_callback(coap_timer_t *t)
514 coap_resource_t *resource;
516 if(resource != NULL && (resource->flags & IS_PERIODIC)
517 && resource->periodic != NULL && resource->periodic->period) {
518 LOG_DBG(
"Periodic: timer expired for /%s (period: %"PRIu32
")\n",
519 resource->url, resource->periodic->period);
521 if(!is_initialized) {
523 }
else if(resource->periodic->periodic_handler) {
525 resource->periodic->periodic_handler();
Default definitions of C compiler quirk work-arounds.
CoAP engine implementation.
static void * coap_timer_get_user_data(coap_timer_t *timer)
Get user data that has been attached to a CoAP timer.
static void coap_timer_set_user_data(coap_timer_t *timer, void *data)
Attach user data to a CoAP timer.
void coap_timer_set(coap_timer_t *timer, uint64_t time)
Set a CoAP timer to expire after the specified time.
static void coap_timer_set_callback(coap_timer_t *timer, void(*callback)(coap_timer_t *))
Set a callback function to be called when a CoAP timer expires.
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
Send a message to the specified CoAP endpoint.
void coap_transport_init(void)
Initialize the CoAP transport.
void coap_activate_resource(coap_resource_t *resource, const char *path)
Makes a resource available under the given URI path.
coap_resource_t * coap_get_next_resource(coap_resource_t *resource)
Returns the next registered CoAP resource.
coap_resource_flags_t
Resource flags for allowed methods and special functionalities.
coap_resource_t * coap_get_first_resource(void)
Returns the first of the registered CoAP resources.
static void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
static void * list_item_next(const void *item)
Get the next item following this item.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Linked list manipulation routines.