diff --git a/source/coap_connection_handler.c b/source/coap_connection_handler.c index fcb622d52b2..f2c39f1ae25 100644 --- a/source/coap_connection_handler.c +++ b/source/coap_connection_handler.c @@ -537,7 +537,7 @@ static int timer_status(int8_t timer_id) return TIMER_STATE_CANCELLED; } -static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16]) +static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16], int8_t *interface) { sock->data_len = 0; if (sckt_data->event_type == SOCKET_DATA && sckt_data->d_len > 0) { @@ -584,6 +584,7 @@ static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_a } if (pkt) { memcpy(dst_address, pkt->ipi6_addr, 16); + *interface = pkt->ipi6_ifindex; } else { goto return_failure; } @@ -613,8 +614,9 @@ static void secure_recv_sckt_msg(void *cb_res) ns_address_t src_address; uint8_t dst_address[16] = {0}; memset(&src_address, 0, sizeof(ns_address_t)); + int8_t interface_id = -1; - if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) { + if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) { /* If received from multicast address, reject */ if (*(dst_address) == 0xFF) { return; @@ -683,7 +685,7 @@ static void secure_recv_sckt_msg(void *cb_res) ns_dyn_mem_free(data); } else { if (sock->parent->_recv_cb) { - sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, data, len); + sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, data, len); } ns_dyn_mem_free(data); } @@ -699,10 +701,11 @@ static void recv_sckt_msg(void *cb_res) internal_socket_t *sock = int_socket_find_by_socket_id(sckt_data->socket_id); ns_address_t src_address; uint8_t dst_address[16]; + int8_t interface_id = -1; - if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 0) { + if (sock && read_data(sckt_data, sock, &src_address, dst_address, &interface_id) == 0) { if (sock->parent && sock->parent->_recv_cb) { - sock->parent->_recv_cb(sock->socket, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len); + sock->parent->_recv_cb(sock->socket, interface_id, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len); } ns_dyn_mem_free(sock->data); sock->data = NULL; @@ -711,6 +714,8 @@ static void recv_sckt_msg(void *cb_res) int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t address[static 16], uint16_t port, uint8_t *data_ptr, uint16_t data_len) { + int8_t interface_id = -1; + if(!handler || !handler->socket) { return -1; } @@ -787,7 +792,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a return 0; } else { if (sock->parent->_recv_cb) { - sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, data, len); + sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, data, len); } ns_dyn_mem_free(data); data = NULL; @@ -798,7 +803,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a } else { /* unsecure*/ if (sock->parent->_recv_cb) { - sock->parent->_recv_cb(sock->socket, address, port, ns_in6addr_any, sock->data, sock->data_len); + sock->parent->_recv_cb(sock->socket, interface_id, address, port, ns_in6addr_any, sock->data, sock->data_len); } if (sock->data) { ns_dyn_mem_free(sock->data); diff --git a/source/coap_message_handler.c b/source/coap_message_handler.c index 1a280374556..9203a42cd1e 100644 --- a/source/coap_message_handler.c +++ b/source/coap_message_handler.c @@ -97,13 +97,28 @@ static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uin return this; } +/* retransmission valid time is calculated to be max. time that CoAP message sending can take: */ +/* Number of retransmisisons, each retransmission is 2 * previous retransmisison time */ +/* + random factor (max. 1.5) */ +static uint32_t transaction_valid_time_calculate(void) +{ + int i; + uint32_t time_valid = 0; + + for (i = 0; i <= COAP_RESENDING_COUNT; i++) { + time_valid += (COAP_RESENDING_INTERVAL << i) * 1.5; + } + + return time_valid + coap_service_get_internal_timer_ticks(); +} + static coap_transaction_t *transaction_create(void) { coap_transaction_t *this = ns_dyn_mem_alloc(sizeof(coap_transaction_t)); if (this) { memset(this, 0, sizeof(coap_transaction_t)); this->client_request = true;// default to client initiated method - this->create_time = coap_service_get_internal_timer_ticks(); + this->valid_until = transaction_valid_time_calculate(); ns_list_add_to_start(&request_list, this); } @@ -199,6 +214,9 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint /* Set default buffer size for CoAP duplicate message detection */ sn_coap_protocol_set_duplicate_buffer_size(handle->coap, DUPLICATE_MESSAGE_BUFFER_SIZE); + /* Set default CoAP retransmission paramters */ + sn_coap_protocol_set_retransmission_parameters(handle->coap, COAP_RESENDING_COUNT, COAP_RESENDING_INTERVAL); + return handle; } @@ -239,8 +257,8 @@ coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, return transaction_find_by_address( address_ptr, port ); } -int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) +int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t interface_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *)) { sn_nsdl_addr_s src_addr; sn_coap_hdr_s *coap_message; @@ -285,7 +303,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t transaction_ptr->token_len = coap_message->token_len; } transaction_ptr->remote_port = port; - if (cb(socket_id, coap_message, transaction_ptr) < 0) { + if (cb(socket_id, interface_id, coap_message, transaction_ptr) < 0) { // negative return value = message ignored -> delete transaction transaction_delete(transaction_ptr); } @@ -381,7 +399,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se //No response expected return 0; } - return transaction_ptr->msg_id; + return request.msg_id; } static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handle, sn_coap_hdr_s *coap_msg_ptr, coap_transaction_t *transaction_ptr) @@ -518,8 +536,13 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti // Remove outdated transactions from queue ns_list_foreach_safe(coap_transaction_t, transaction, &request_list) { - if ((transaction->create_time + TRANSACTION_LIFETIME) < current_time) { - transaction_delete(transaction); + if (transaction->valid_until < current_time) { + tr_debug("transaction %d timed out", transaction->msg_id); + ns_list_remove(&request_list, transaction); + if (transaction->resp_cb) { + transaction->resp_cb(transaction->service_id, transaction->remote_address, transaction->remote_port, NULL); + } + transaction_free(transaction); } } diff --git a/source/coap_service_api.c b/source/coap_service_api.c index 9adcee212a4..8c136083a02 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -36,7 +36,7 @@ #include "coap_message_handler.h" #include "mbed-coap/sn_coap_protocol.h" -static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr); +static int16_t coap_msg_process_callback(int8_t socket_id, int8_t interface_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr); typedef struct uri_registration { char *uri_ptr; @@ -210,7 +210,7 @@ static void service_event_handler(arm_event_s *event) eventOS_event_timer_request((uint8_t)COAP_TICK_TIMER, ARM_LIB_SYSTEM_TIMER_EVENT, tasklet_id, 1000); } -static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr) +static int16_t coap_msg_process_callback(int8_t socket_id, int8_t interface_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr) { coap_service_t *this; if (!coap_message || !transaction_ptr) { @@ -229,6 +229,11 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m return -1; } + if ((interface_id != -1) && (this->interface_id != interface_id)) { + tr_debug("uri %.*s not registered to interface %d", coap_message->uri_path_len, coap_message->uri_path_ptr, interface_id); + return 0; + } + uri_registration_t *uri_reg_ptr = uri_registration_find(this, coap_message->uri_path_ptr, coap_message->uri_path_len); if (uri_reg_ptr && uri_reg_ptr->request_recv_cb) { tr_debug("Service %d, call request recv cb uri %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr); @@ -244,7 +249,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m return -1; } -static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len) +static int recv_cb(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len) { uint8_t *data_ptr = NULL; uint16_t data_len = 0; @@ -263,7 +268,7 @@ static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t po tr_debug("service recv socket data len %d ", data_len); //parse coap message what CoAP to use - int ret = coap_message_handler_coap_msg_process(coap_service_handle, socket_id, src_address, port, dst_address, data_ptr, data_len, &coap_msg_process_callback); + int ret = coap_message_handler_coap_msg_process(coap_service_handle, socket_id, interface_id, src_address, port, dst_address, data_ptr, data_len, &coap_msg_process_callback); own_free(data_ptr); return ret; } diff --git a/source/include/coap_connection_handler.h b/source/include/coap_connection_handler.h index 23339527940..215876da317 100644 --- a/source/include/coap_connection_handler.h +++ b/source/include/coap_connection_handler.h @@ -35,7 +35,7 @@ struct internal_socket_s; typedef int send_to_socket_cb(int8_t socket_id, const uint8_t address[static 16], uint16_t port, const void *, int); -typedef int receive_from_socket_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int); +typedef int receive_from_socket_cb(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int); typedef int get_pw_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr); typedef void security_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]); diff --git a/source/include/coap_message_handler.h b/source/include/coap_message_handler.h index 35c6af129bc..2f7cc4bf425 100644 --- a/source/include/coap_message_handler.h +++ b/source/include/coap_message_handler.h @@ -26,6 +26,10 @@ /* Default value for CoAP duplicate message buffer (0 = disabled) */ #define DUPLICATE_MESSAGE_BUFFER_SIZE 0 +/* Default values for CoAP resendings */ +#define COAP_RESENDING_COUNT 3 +#define COAP_RESENDING_INTERVAL 10 + /** * \brief Service message response receive callback. * @@ -51,7 +55,7 @@ typedef struct coap_transaction { uint8_t remote_address[16]; uint8_t local_address[16]; uint8_t token[8]; - uint32_t create_time; + uint32_t valid_until; uint8_t *data_ptr; coap_message_handler_response_recv *resp_cb; uint16_t remote_port; @@ -76,8 +80,8 @@ extern coap_transaction_t *coap_message_handler_transaction_valid(coap_transacti extern coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, uint16_t port); -extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)); +extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t interface_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *)); extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type, diff --git a/test/coap-service/unittest/coap_message_handler/Makefile b/test/coap-service/unittest/coap_message_handler/Makefile index 1b02dfb31a6..94558a8080c 100644 --- a/test/coap-service/unittest/coap_message_handler/Makefile +++ b/test/coap-service/unittest/coap_message_handler/Makefile @@ -37,5 +37,5 @@ TEST_SRC_FILES = \ include ../MakefileWorker.mk -CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT +CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT -I ../../../../source/ diff --git a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c index cc1e01e7c2f..2527f5d6470 100644 --- a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c +++ b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c @@ -22,11 +22,13 @@ #include "sn_coap_builder_stub.h" #include "sn_coap_parser_stub.h" #include "socket_api.h" +#include "coap_message_handler.c" int retCounter = 0; int retValue = 0; +int transaction_cb = 0; -static void *own_alloc(uint16_t size) +static void *test_own_alloc(uint16_t size) { if( retCounter > 0 ){ retCounter--; @@ -35,7 +37,7 @@ static void *own_alloc(uint16_t size) return NULL; } -static void own_free(void *ptr) +static void test_own_free(void *ptr) { if (ptr) { free(ptr); @@ -56,24 +58,30 @@ int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c) return retValue; } +static int transaction_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr) +{ + transaction_cb = 1; + return 1; +} + bool test_coap_message_handler_init() { if( NULL != coap_message_handler_init(NULL, NULL, NULL) ) return false; - if( NULL != coap_message_handler_init(&own_alloc, NULL, NULL) ) + if( NULL != coap_message_handler_init(&test_own_alloc, NULL, NULL) ) return false; - if( NULL != coap_message_handler_init(&own_alloc, &own_free, NULL) ) + if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, NULL) ) return false; - if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) + if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) return false; retCounter = 1; sn_coap_protocol_stub.expectedCoap = NULL; - if( NULL != coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function) ) + if( NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function) ) return false; retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); if( NULL == handle ) return false; free(sn_coap_protocol_stub.expectedCoap); @@ -89,7 +97,7 @@ bool test_coap_message_handler_destroy() retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); if( 0 != coap_message_handler_destroy(handle) ) return false; @@ -105,7 +113,7 @@ bool test_coap_message_handler_find_transaction() retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); uint8_t buf[16]; memset(&buf, 1, 16); @@ -133,24 +141,24 @@ bool test_coap_message_handler_coap_msg_process() uint8_t buf[16]; memset(&buf, 1, 16); /*Handler is null*/ - if( -1 != coap_message_handler_coap_msg_process(NULL, 0, buf, 22, ns_in6addr_any, NULL, 0, NULL)) + if( -1 != coap_message_handler_coap_msg_process(NULL, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, NULL)) return false; retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); sn_coap_protocol_stub.expectedHeader = NULL; /* Coap parse returns null */ - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( -1 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s)); sn_coap_protocol_stub.expectedHeader->coap_status = 66; /* Coap library responds */ - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( -1 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); @@ -159,7 +167,7 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->msg_code = 1; retValue = 0; /* request received */ - if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( 0 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); @@ -168,7 +176,7 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->msg_code = 1; nsdynmemlib_stub.returnCounter = 1; retValue = -1; - if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( 0 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); @@ -176,7 +184,7 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK; sn_coap_protocol_stub.expectedHeader->msg_code = 333; - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( -1 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); @@ -197,7 +205,7 @@ bool test_coap_message_handler_coap_msg_process() sn_coap_protocol_stub.expectedHeader->msg_id = 2; // sn_coap_protocol_stub.expectedHeader->token_ptr = (uint8_t*)malloc(4); // memset(sn_coap_protocol_stub.expectedHeader->token_ptr, 1, 4); - if( -1 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) + if( -1 != coap_message_handler_coap_msg_process(handle, 0, -1, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) return false; // free(sn_coap_protocol_stub.expectedHeader->token_ptr); @@ -213,7 +221,7 @@ bool test_coap_message_handler_request_send() retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); uint8_t buf[16]; memset(&buf, 1, 16); @@ -255,7 +263,7 @@ bool test_coap_message_handler_request_delete() retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); uint8_t buf[16]; memset(&buf, 1, 16); @@ -291,7 +299,7 @@ bool test_coap_message_handler_response_send() retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); memset(header, 0, sizeof(sn_coap_hdr_s)); @@ -340,15 +348,54 @@ bool test_coap_message_handler_response_send() bool test_coap_message_handler_exec() { + /* Null as a parameter */ if( -1 != coap_message_handler_exec(NULL, 0)) return false; + retCounter = 1; sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s)); memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s)); - coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function); + coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function); + + if( 0 != coap_message_handler_exec(handle, 0)) + return false; + + nsdynmemlib_stub.returnCounter = 1; + coap_transaction_t *transact_ptr = transaction_create(); + + /* Transaction not timed out*/ if( 0 != coap_message_handler_exec(handle, 0)) return false; + if (transaction_cb != 0) + return false; + + /* Timed out, no CB */ + if( 0 != coap_message_handler_exec(handle, 300)) + return false; + + if (transaction_cb != 0) + return false; + + nsdynmemlib_stub.returnCounter = 1; + transact_ptr = transaction_create(); + transact_ptr->resp_cb = transaction_recv_cb; + + /* Transaction not timed out */ + if( 0 != coap_message_handler_exec(handle, 0)) + return false; + + if (transaction_cb != 0) + return false; + + /* Transaction timed out */ + if( 0 != coap_message_handler_exec(handle, 300)) + return false; + + if (transaction_cb == 0) + return false; + + /* Teardown */ free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; coap_message_handler_destroy(handle); diff --git a/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp b/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp index 377f486a61f..413be487bf9 100644 --- a/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp +++ b/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp @@ -117,3 +117,9 @@ TEST(coap_service_api, test_coap_service_handshake_limit_set) { CHECK(test_coap_service_handshake_limit_set()) } + +TEST(coap_service_api, test_coap_service_secure_session_close) +{ + CHECK(test_coap_service_secure_session_close()) +} + diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c index 00714a65b16..254cc903c43 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c @@ -352,14 +352,14 @@ bool test_conn_handler_callbacks() if( thread_conn_handler_stub.receive_from_sock_cb ){ coap_message_handler_stub.int16_value = 2; - if( -1 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, NULL, 0)) + if( -1 != thread_conn_handler_stub.receive_from_sock_cb(1, -1, buf, 12, NULL, NULL, 0)) return false; nsdynmemlib_stub.returnCounter = 1; uint8_t * ptr = ns_dyn_mem_alloc(5); memset(ptr, 3, 5); nsdynmemlib_stub.returnCounter = 1; - if( 2 != thread_conn_handler_stub.receive_from_sock_cb(1, buf, 12, NULL, ptr, 5)) + if( 2 != thread_conn_handler_stub.receive_from_sock_cb(1, -1, buf, 12, NULL, ptr, 5)) return false; ns_dyn_mem_free(ptr); coap_message_handler_stub.int16_value = 0; @@ -367,7 +367,7 @@ bool test_conn_handler_callbacks() //This could be moved to own test function, //but thread_conn_handler_stub.receive_from_sock_cb must be called successfully if( coap_message_handler_stub.cb ){ - if( -1 != coap_message_handler_stub.cb(1, NULL, NULL) ) + if( -1 != coap_message_handler_stub.cb(1, -1, NULL, NULL) ) return false; sn_coap_hdr_s * coap = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s)); @@ -377,7 +377,7 @@ bool test_conn_handler_callbacks() coap->uri_path_ptr = &uri; coap->uri_path_len=2; - if( -1 != coap_message_handler_stub.cb(1, coap, NULL) ) + if( -1 != coap_message_handler_stub.cb(1, -1, coap, NULL) ) return false; thread_conn_handler_stub.bool_value = true; @@ -385,13 +385,13 @@ bool test_conn_handler_callbacks() if( 0 != coap_service_register_uri(1, "as", 1, &request_recv_cb) ) return false; - if( -1 != coap_message_handler_stub.cb(1, coap, NULL) ) + if( -1 != coap_message_handler_stub.cb(1, -1, coap, NULL) ) return false; coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t)); memset(tr, 0, sizeof(coap_transaction_t)); - if( 2 != coap_message_handler_stub.cb(1, coap, tr) ) + if( 2 != coap_message_handler_stub.cb(1, -1, coap, tr) ) return false; free(tr); @@ -589,3 +589,24 @@ bool test_coap_service_handshake_limit_set() return true; } + +bool test_coap_service_secure_session_close() +{ + int service_id; + uint8_t addr_ptr[16] = {0}; + + thread_conn_handler_stub.handler_obj = (coap_conn_handler_t*)malloc(sizeof(coap_conn_handler_t)); + memset(thread_conn_handler_stub.handler_obj, 0, sizeof(coap_conn_handler_t)); + + nsdynmemlib_stub.returnCounter = 1; + service_id = coap_service_initialize(1, 2, 0, NULL, NULL ); + + coap_service_close_secure_connection(0, NULL, 0); + + coap_service_close_secure_connection(service_id, NULL, 0); + + coap_service_close_secure_connection(service_id, addr_ptr, 1234); + + coap_service_delete(service_id); + free(thread_conn_handler_stub.handler_obj); +} diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.h b/test/coap-service/unittest/coap_service_api/test_coap_service_api.h index 9f83ca06a8e..1bf435230db 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.h +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.h @@ -59,6 +59,8 @@ bool test_coap_service_if_find_by_socket(); bool test_coap_service_handshake_limit_set(); +bool test_coap_service_secure_session_close(); + #ifdef __cplusplus } diff --git a/test/coap-service/unittest/stub/coap_connection_handler_stub.c b/test/coap-service/unittest/stub/coap_connection_handler_stub.c index 56da167069e..31c112a28e6 100644 --- a/test/coap-service/unittest/stub/coap_connection_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_connection_handler_stub.c @@ -33,7 +33,7 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a return thread_conn_handler_stub.int_value; } -coap_conn_handler_t *connection_handler_create(int (*recv_cb)(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int), +coap_conn_handler_t *connection_handler_create(int (*recv_cb)(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int), int (*send_cb)(int8_t socket_id, uint8_t const address[static 16], uint16_t port, const void *, int), int (*pw_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr), void(*done_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static KEY_BLOCK_LEN]) ) diff --git a/test/coap-service/unittest/stub/coap_connection_handler_stub.h b/test/coap-service/unittest/stub/coap_connection_handler_stub.h index ea29adcfc47..d92f962228d 100644 --- a/test/coap-service/unittest/stub/coap_connection_handler_stub.h +++ b/test/coap-service/unittest/stub/coap_connection_handler_stub.h @@ -32,7 +32,7 @@ typedef struct { coap_conn_handler_t *handler_obj; int (*send_to_sock_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, const void *, int); - int (*receive_from_sock_cb)(int8_t socket_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len); + int (*receive_from_sock_cb)(int8_t socket_id, int8_t interface_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *data, int len); int (*get_passwd_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, coap_security_keys_t *security_ptr); void (*sec_done_cb)(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40]); diff --git a/test/coap-service/unittest/stub/coap_message_handler_stub.c b/test/coap-service/unittest/stub/coap_message_handler_stub.c index 2116dd34709..ad629e0eca7 100644 --- a/test/coap-service/unittest/stub/coap_message_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_message_handler_stub.c @@ -54,8 +54,8 @@ coap_transaction_t *coap_message_handler_find_transaction(uint8_t *address_ptr, return coap_message_handler_stub.coap_tx_ptr; } -int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], - uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *)) +int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t interface_id, const uint8_t source_addr_ptr[static 16], uint16_t port, const uint8_t dst_addr_ptr[static 16], + uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *)) { coap_message_handler_stub.cb = cb; return coap_message_handler_stub.int16_value; diff --git a/test/coap-service/unittest/stub/coap_message_handler_stub.h b/test/coap-service/unittest/stub/coap_message_handler_stub.h index f0b0bf02910..97b3ac8b5aa 100644 --- a/test/coap-service/unittest/stub/coap_message_handler_stub.h +++ b/test/coap-service/unittest/stub/coap_message_handler_stub.h @@ -31,7 +31,7 @@ typedef struct { uint16_t uint16_value; coap_msg_handler_t *coap_ptr; coap_transaction_t *coap_tx_ptr; - int16_t (*cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *); + int16_t (*cb)(int8_t, int8_t, sn_coap_hdr_s *, coap_transaction_t *); } coap_message_handler_stub_def; extern coap_message_handler_stub_def coap_message_handler_stub;