Skip to content

Commit

Permalink
Send 4.04 if URI is not found (#34)
Browse files Browse the repository at this point in the history
If coap service library receives confirmable request to URI that is not
registered, it must send response COAP_MSG_CODE_RESPONSE_NOT_FOUND.
  • Loading branch information
Tero Heinonen authored Oct 4, 2016
1 parent 301458a commit 0e4af1a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 5 additions & 5 deletions source/coap_message_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <string.h>
#include "nsdynmemLIB.h"
#include "coap_service_api_internal.h"
#include "coap_message_handler.h"
#include "sn_coap_protocol.h"
#include "ns_types.h"
Expand Down Expand Up @@ -202,13 +203,11 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_message);
return -1;
}
/* Request received */
if (coap_message->msg_code > 0 && coap_message->msg_code < 32) {

//TODO Sorry

coap_transaction_t *transaction_ptr = transaction_create();
if (transaction_ptr) {

transaction_ptr->service_id = coap_service_id_find_by_socket(socket_id);
transaction_ptr->msg_id = coap_message->msg_id;
transaction_ptr->client_request = false;// this is server transaction
memcpy(transaction_ptr->remote_address, source_addr_ptr, 16);
Expand All @@ -224,8 +223,8 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
}else{
//TODO: handle error case
}
/* Response received */
} else {
//response find by MSG id
coap_transaction_t *this = NULL;
if( coap_message->token_ptr ){
this = transaction_find_client_by_token(coap_message->token_ptr);
Expand All @@ -242,6 +241,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_message);
transaction_delete(this);
}

return 0;

}
Expand Down
20 changes: 17 additions & 3 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "coap_connection_handler.h"
#include "net_interface.h"
#include "coap_service_api_internal.h"
#include "coap_message_handler.h"

static int16_t coap_service_coap_msg_process(int8_t socket_id, uint8_t source_addr_ptr[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len);
static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr);
Expand Down Expand Up @@ -130,7 +131,7 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
return -1;
}

tr_debug("Service %d, CoAP TX Function", transaction_ptr->service_id);
tr_debug("Service %d, CoAP TX Function - mid: %d", transaction_ptr->service_id, common_read_16_bit(data_ptr + 2));

this = service_find(transaction_ptr->service_id);
if (!this) {
Expand Down Expand Up @@ -176,10 +177,16 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
if( !coap_message ){
return -1;
}
// Message is request find correct handle

// Message is request, find correct handle
this = service_find_by_uri(socket_id, coap_message->uri_path_ptr, coap_message->uri_path_len);
if (!this) {
tr_warn("not registered uri %.*s", coap_message->uri_path_len, coap_message->uri_path_ptr);
tr_debug("not registered uri %.*s", coap_message->uri_path_len, coap_message->uri_path_ptr);
if (coap_message->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
coap_message_handler_response_send(coap_service_handle, transaction_ptr->service_id, COAP_SERVICE_OPTIONS_NONE, coap_message,
COAP_MSG_CODE_RESPONSE_NOT_FOUND, COAP_CT_NONE, NULL, 0);
return 0;
}
return -1;
}

Expand Down Expand Up @@ -471,3 +478,10 @@ uint32_t coap_service_get_internal_timer_ticks(void)
{
return coap_ticks;
}

uint16_t coap_service_id_find_by_socket(int8_t socket_id)
{
coap_service_t *this = service_find_by_socket(socket_id);

return this ? this->service_id:0;
}
2 changes: 2 additions & 0 deletions source/include/coap_service_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@

uint32_t coap_service_get_internal_timer_ticks(void);

uint16_t coap_service_id_find_by_socket(int8_t socket_id);

#endif

0 comments on commit 0e4af1a

Please sign in to comment.