Squashed 'features/nanostack/coap-service/' changes from c45afcd..227cc3d

227cc3d Merge pull request #120 from ARMmbed/sync_with_mbedos_2
88894d1 Follow Mbed OS coding style
f3db9a17 (via Mbed OS) Add mbedtls platform setup and teardown to modules
5feb8dd Merge pull request #119 from ARMmbed/sync_with_MbedOS
10d5054 (via Mbed OS) Add missing mbed_lib.json for frameworks and nanostack
a344676 Remove references to yotta (#118)
610afda Remove excess tracing (#117)
53382d6 Merge pull request #116 from ARMmbed/IOTTHD-1608
15889cb Use Mbed OS coding style
d6eff5c Clarify function signature
587e8de Update message prevalidation API
46f86d4 Add API to set callback for CoAP msg prevalidation (#115)
0eb6630 Merge pull request #114 from ARMmbed/sync_with_mbed_os
f75732b (split) Add unit tests for TLSSocket and TLSSocketWrapper

git-subtree-dir: features/nanostack/coap-service
git-subtree-split: 227cc3da82a8f5cab92c6975aa9623766808489b
pull/9838/head
Arto Kinnunen 2019-02-25 13:38:05 +02:00
parent e6a851f0a7
commit e046a96f8b
27 changed files with 409 additions and 72 deletions

3
.gitignore vendored
View File

@ -3,8 +3,5 @@
output
lcov
coverage
.yotta.json
build/
yotta_modules/
yotta_targets/
upload.tar.gz

View File

@ -40,7 +40,7 @@ test: $(TESTDIRS)
@rm -f lcov/index.xml
@find ./ -name '*.gcno' | xargs cp --backup=numbered -t ./coverage/
@find ./ -name '*.gcda' | xargs cp --backup=numbered -t ./coverage/
@gcovr --object-directory ./coverage --exclude-unreachable-branches -e '.*/builds/.*' -e '.*/test/.*' -e '.*/yotta_modules/.*' -e '.*/stub/.*' -x -o ./lcov/gcovr.xml
@gcovr --object-directory ./coverage --exclude-unreachable-branches -e '.*/builds/.*' -e '.*/test/.*' -e '.*/stub/.*' -x -o ./lcov/gcovr.xml
@lcov -d test/. -c -o $(COVERAGEFILE)
@lcov -q -r $(COVERAGEFILE) "/usr*" -o $(COVERAGEFILE)
@lcov -q -r $(COVERAGEFILE) "/test*" -o $(COVERAGEFILE)

View File

@ -120,6 +120,26 @@ typedef int coap_service_security_start_cb(int8_t service_id, uint8_t address[st
*/
typedef int coap_service_security_done_cb(int8_t service_id, uint8_t address[static 16], uint8_t keyblock[static 40]);
/**
* \brief Message prevalidation callback
*
* Message prevalidation callback function type used in method coap_service_msg_prevalidate_callback_set.
*
* \param local_interface_id Local interface ID, message arrived to this interface.
* \param local_address Local address, message arrived to this address.
* \param local_port Local port, message arrived to this port.
* \param recv_interface_id Interface ID where message was received.
* \param source_address Sender address.
* \param source_port Sender port.
* \param coap_uri CoAP URI, NUL terminated.
*
* \return <0 in case of errors,
* 0 if message is valid to process further,
* >0 if message should be dropped.
*/
typedef int coap_service_msg_prevalidate_cb(int8_t local_interface_id, uint8_t local_address[static 16], uint16_t local_port, int8_t recv_interface_id, uint8_t source_address[static 16], uint16_t source_port, char *coap_uri);
/**
* \brief Initialise server instance.
*
@ -131,7 +151,7 @@ typedef int coap_service_security_done_cb(int8_t service_id, uint8_t address[sta
* \param *start_ptr Callback to inform security handling is started and to fetch device password.
* \param *coap_security_done_cb Callback to inform security handling is done.
*
* \return service_id / -1 for failure
* \return service_id / -1 for failure
*/
extern int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb);
@ -373,6 +393,22 @@ extern int8_t coap_service_certificate_set(int8_t service_id, const unsigned cha
*- 0 For success
*/
extern int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size);
/**
* \brief Set message prevalidation callback function.
*
* Set message prevalidation callback function for the service. Callback will be called for all services using the same listen port.
*
* CoAP service will call this function to allow application prevalidate incoming CoAP message before passing it to application.
*
* \param listen_port Socket port where to set callback.
* \param msg_prevalidate_cb Callback to be called to validate incoming message before processing it. Use NULL to clear callback usage.
*
* \return -1 For failure
* 0 For success
*/
extern int8_t coap_service_msg_prevalidate_callback_set(uint16_t listen_port, coap_service_msg_prevalidate_cb *msg_prevalidate_cb);
#ifdef __cplusplus
}
#endif

3
mbed_lib.json Normal file
View File

@ -0,0 +1,3 @@
{
"name": "coap-service"
}

View File

@ -36,6 +36,7 @@ typedef enum session_state_e {
typedef struct internal_socket_s {
coap_conn_handler_t *parent;
cch_func_cb *cch_function_callback; // callback function
uint32_t timeout_min;
uint32_t timeout_max;
@ -44,6 +45,7 @@ typedef struct internal_socket_s {
int16_t data_len;
uint8_t *data;
int8_t recv_if_id; // interface ID where data is coming from
int8_t socket; //positive value = socket id, negative value virtual socket id
bool real_socket;
@ -548,6 +550,7 @@ static int timer_status(int8_t timer_id)
static int read_data(socket_callback_t *sckt_data, internal_socket_t *sock, ns_address_t *src_address, uint8_t dst_address[static 16])
{
sock->data_len = 0;
sock->recv_if_id = -1;
if (sckt_data->event_type == SOCKET_DATA && sckt_data->d_len > 0) {
uint8_t ancillary_databuffer[NS_CMSG_SPACE(sizeof(ns_in6_pktinfo_t))];
ns_iovec_t msg_iov;
@ -592,6 +595,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);
sock->recv_if_id = pkt->ipi6_ifindex;
} else {
goto return_failure;
}
@ -693,7 +697,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, sock->recv_if_id, src_address.address, src_address.identifier, dst_address, data, len);
}
ns_dyn_mem_free(data);
}
@ -712,7 +716,7 @@ static void recv_sckt_msg(void *cb_res)
if (sock && read_data(sckt_data, sock, &src_address, dst_address) == 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, sock->recv_if_id, src_address.address, src_address.identifier, dst_address, sock->data, sock->data_len);
}
ns_dyn_mem_free(sock->data);
sock->data = NULL;
@ -801,7 +805,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, sock->recv_if_id, address, port, ns_in6addr_any, data, len);
}
ns_dyn_mem_free(data);
data = NULL;
@ -812,7 +816,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, sock->recv_if_id, address, port, ns_in6addr_any, sock->data, sock->data_len);
}
if (sock->data) {
ns_dyn_mem_free(sock->data);
@ -1023,3 +1027,32 @@ void coap_connection_handler_exec(uint32_t time)
}
}
}
int coap_connection_handler_msg_prevalidate_callback_set(coap_conn_handler_t *handler, cch_func_cb *function_callback)
{
if (!handler) {
return -1;
}
handler->socket->cch_function_callback = function_callback;
return 0;
}
coap_conn_handler_t *coap_connection_handler_find_by_socket_port(uint16_t listen_port)
{
ns_list_foreach(internal_socket_t, cur_ptr, &socket_list) {
if (cur_ptr->listen_port == listen_port) {
return cur_ptr->parent;
}
}
return NULL;
}
cch_func_cb *coap_connection_handler_msg_prevalidate_callback_get(coap_conn_handler_t *handler, uint16_t *listen_socket_port)
{
if (!handler || !listen_socket_port) {
return NULL;
}
*listen_socket_port = handler->socket->listen_port;
return handler->socket->cch_function_callback;
}

View File

@ -293,15 +293,15 @@ 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 recv_if_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, coap_msg_process_cb *msg_process_callback)
{
sn_nsdl_addr_s src_addr;
sn_coap_hdr_s *coap_message;
int16_t ret_val = 0;
coap_transaction_t *this = NULL;
if (!cb || !handle) {
if (!msg_process_callback || !handle) {
return -1;
}
@ -337,21 +337,21 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
goto exit;
}
/* Request received */
if (coap_message->msg_code > 0 && coap_message->msg_code < 32) {
/* Request received */
transaction_ptr->msg_id = coap_message->msg_id;
transaction_ptr->req_msg_type = coap_message->msg_type;
if (coap_message->token_len) {
memcpy(transaction_ptr->token, coap_message->token_ptr, coap_message->token_len);
transaction_ptr->token_len = coap_message->token_len;
}
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
if (msg_process_callback(socket_id, recv_if_id, coap_message, transaction_ptr, dst_addr_ptr) < 0) {
// negative return value = message ignored -> delete transaction
transaction_delete(transaction_ptr);
}
goto exit;
/* Response received */
} else {
/* Response received */
transaction_delete(transaction_ptr); // transaction_ptr not needed in response
if (coap_message->token_ptr) {
this = transaction_find_client_by_token(coap_message->token_ptr, coap_message->token_len, source_addr_ptr, port);

View File

@ -102,6 +102,12 @@ static int coap_security_handler_init(coap_security_t *sec)
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_WEAK;
#endif
#if defined(MBEDTLS_PLATFORM_C)
if (mbedtls_platform_setup(NULL) != 0) {
return -1;
}
#endif /* MBEDTLS_PLATFORM_C */
mbedtls_ssl_init(&sec->_ssl);
mbedtls_ssl_config_init(&sec->_conf);
mbedtls_ctr_drbg_init(&sec->_ctr_drbg);
@ -153,6 +159,9 @@ static void coap_security_handler_reset(coap_security_t *sec)
mbedtls_ctr_drbg_free(&sec->_ctr_drbg);
mbedtls_ssl_config_free(&sec->_conf);
mbedtls_ssl_free(&sec->_ssl);
#if defined(MBEDTLS_PLATFORM_C)
mbedtls_platform_teardown(NULL);
#endif /* MBEDTLS_PLATFORM_C */
}

View File

@ -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 recv_if_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr, const uint8_t *local_addr);
typedef struct uri_registration {
char *uri_ptr;
@ -70,6 +70,13 @@ static uint32_t coap_ticks = 1;
#define COAP_TICK_TIMER 0xf1
//#define TRACE_DEEP
#ifdef TRACE_DEEP
#define tr_deep tr_debug
#else
#define tr_deep(...)
#endif
static uri_registration_t *uri_registration_find(coap_service_t *this, const void *uri_ptr, uint16_t uri_len)
{
ns_list_foreach(uri_registration_t, cur_ptr, &this->uri_list) {
@ -165,7 +172,7 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
return 0;
}
tr_debug("Service %d, CoAP TX Function - mid: %d", transaction_ptr->service_id, common_read_16_bit(data_ptr + 2));
tr_debug("Service %d, CoAP TX - mid: %d", transaction_ptr->service_id, common_read_16_bit(data_ptr + 2));
this = service_find(transaction_ptr->service_id);
if (!this) {
@ -211,17 +218,44 @@ 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 recv_if_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr, const uint8_t *local_addr)
{
coap_service_t *this;
coap_service_msg_prevalidate_cb *msg_prevalidate_callback;
uint16_t listen_socket_port;
if (!coap_message || !transaction_ptr) {
return -1;
}
// Message is request, find correct handle
// Message is request, find correct handle based on URI
this = service_find_by_uri(socket_id, coap_message->uri_path_ptr, coap_message->uri_path_len);
if (!this) {
tr_debug("not registered uri %.*s", coap_message->uri_path_len, coap_message->uri_path_ptr);
tr_deep("URI %.*s not registered", coap_message->uri_path_len, coap_message->uri_path_ptr);
// URI is not available, find any service that holds the same shared socket so that we can get msg_prevalidate_callback to validate addresses
this = service_find_by_socket(socket_id);
if (!this) {
return -1;
}
}
msg_prevalidate_callback = (coap_service_msg_prevalidate_cb *)coap_connection_handler_msg_prevalidate_callback_get(this->conn_handler, &listen_socket_port);
if (msg_prevalidate_callback) {
// message prevalidation activated for the port
char request_uri[coap_message->uri_path_len + 1];
memcpy(request_uri, coap_message->uri_path_ptr, coap_message->uri_path_len);
request_uri[coap_message->uri_path_len] = 0;
int msg_prevalidate_status = msg_prevalidate_callback(this->interface_id, (uint8_t *)local_addr, listen_socket_port, recv_if_id, transaction_ptr->remote_address, transaction_ptr->remote_port, request_uri);
if (msg_prevalidate_status >= 1) {
tr_deep("Drop CoAP msg %s from %s to %s", request_uri, trace_ipv6(transaction_ptr->remote_address), trace_ipv6(local_addr));
return -1;
}
}
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 is not available, stop further processing */
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);
@ -230,22 +264,22 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
return -1;
}
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);
if (uri_reg_ptr->request_recv_cb) {
if ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS) { //TODO Add secure bypass option
// Service has secure bypass active TODO this is not defined in interface
// this check can be removed I think
transaction_ptr->options = COAP_REQUEST_OPTIONS_SECURE_BYPASS;
}
transaction_ptr->service_id = this->service_id;
tr_debug("Service %d, recv msg: %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr);
return uri_reg_ptr->request_recv_cb(this->service_id, transaction_ptr->remote_address, transaction_ptr->remote_port, coap_message);
}
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 recv_if_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;
@ -261,10 +295,9 @@ static int recv_cb(int8_t socket_id, uint8_t src_address[static 16], uint16_t po
}
memcpy(data_ptr, data, len);
data_len = len;
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, recv_if_id, src_address, port, dst_address, data_ptr, data_len, &coap_msg_process_callback);
own_free(data_ptr);
return ret;
}
@ -626,3 +659,12 @@ int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size)
return sn_coap_protocol_set_block_size(coap_service_handle->coap, size);
}
int8_t coap_service_msg_prevalidate_callback_set(uint16_t listen_socket, coap_service_msg_prevalidate_cb *msg_prevalidate_cb)
{
coap_conn_handler_t *conn_handler = coap_connection_handler_find_by_socket_port(listen_socket);
if (conn_handler) {
return (int8_t)coap_connection_handler_msg_prevalidate_callback_set(conn_handler, (cch_func_cb *)msg_prevalidate_cb);
}
return -1;
}

View File

@ -35,9 +35,10 @@
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 recv_if_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]);
typedef void cch_func_cb(void);
typedef struct coap_conn_handler_s {
struct internal_socket_s *socket;
@ -81,4 +82,10 @@ int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, ui
void coap_connection_handler_exec(uint32_t time);
coap_conn_handler_t *coap_connection_handler_find_by_socket_port(uint16_t listen_port);
int coap_connection_handler_msg_prevalidate_callback_set(coap_conn_handler_t *handler, cch_func_cb *function_callback);
cch_func_cb *coap_connection_handler_msg_prevalidate_callback_get(coap_conn_handler_t *handler, uint16_t *listen_socket_port);
#endif

View File

@ -49,9 +49,7 @@ typedef int coap_message_handler_response_recv(int8_t service_id, uint8_t source
typedef struct coap_msg_handler_s {
void *(*sn_coap_service_malloc)(uint16_t);
void (*sn_coap_service_free)(void *);
uint8_t (*sn_coap_tx_callback)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *);
struct coap_s *coap;
} coap_msg_handler_t;
@ -74,6 +72,20 @@ typedef struct coap_transaction {
ns_list_link_t link;
} coap_transaction_t;
/**
* \brief Service message processing callback.
*
* Function that processes CoAP service message
*
* \param socket_id Socket that receives the message.
* \param recv_if_id Interface where message is received.
* \param coap_message Actual CoAP message.
* \param transaction_ptr Message transaction.
* \param local_addr Address where message is received.
*
* \return 0 for success / -1 for failure
*/
typedef int16_t coap_msg_process_cb(int8_t socket_id, int8_t recv_if_id, sn_coap_hdr_s *coap_message, coap_transaction_t *transaction_ptr, const uint8_t *local_addr);
extern coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *),
uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *));
@ -84,8 +96,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 recv_if_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, coap_msg_process_cb *msg_process_callback);
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,

View File

@ -71,3 +71,13 @@ TEST(coap_connection_handler, test_security_callbacks)
CHECK(test_security_callbacks());
}
TEST(coap_connection_handler, test_coap_connection_handler_msg_prevalidate_cb_read_and_set)
{
CHECK(test_coap_connection_handler_msg_prevalidate_cb_read_and_set());
}
TEST(coap_connection_handler, test_coap_connection_handler_find_by_socket_port)
{
CHECK(test_coap_connection_handler_find_by_socket_port());
}

View File

@ -511,3 +511,75 @@ bool test_security_callbacks()
sckt_data = NULL;
return true;
}
bool test_coap_connection_handler_msg_prevalidate_cb_read_and_set()
{
coap_security_handler_stub.counter = -1;
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) {
return false;
}
if (-1 != coap_connection_handler_msg_prevalidate_callback_set(NULL, 1000)) {
return false;
}
if (0 != coap_connection_handler_msg_prevalidate_callback_set(handler, 1000)) {
return false;
}
uint16_t listen_socket_port;
if (NULL != coap_connection_handler_msg_prevalidate_callback_get(NULL, &listen_socket_port)) {
return false;
}
if (1000 != coap_connection_handler_msg_prevalidate_callback_get(handler, &listen_socket_port)) {
return false;
}
connection_handler_destroy(handler, false);
free(coap_security_handler_stub.sec_obj);
coap_security_handler_stub.sec_obj = NULL;
return true;
}
bool test_coap_connection_handler_find_by_socket_port()
{
coap_conn_handler_t *handler_ref;
coap_security_handler_stub.counter = -1;
coap_security_handler_stub.sec_obj = coap_security_handler_stub_alloc();
nsdynmemlib_stub.returnCounter = 1;
coap_conn_handler_t *handler = connection_handler_create(&receive_from_sock_cb, &send_to_sock_cb, NULL, NULL);
nsdynmemlib_stub.returnCounter = 2;
if (0 != coap_connection_handler_open_connection(handler, 22, false, true, true, false)) {
return false;
}
handler_ref = coap_connection_handler_find_by_socket_port(1000);
if (NULL != handler_ref) {
return false;
}
handler_ref = coap_connection_handler_find_by_socket_port(22);
if (handler_ref->_recv_cb != receive_from_sock_cb) {
return false;
}
connection_handler_destroy(handler, false);
free(coap_security_handler_stub.sec_obj);
coap_security_handler_stub.sec_obj = NULL;
return true;
}

View File

@ -41,6 +41,10 @@ bool test_socket_api_callbacks();
bool test_security_callbacks();
bool test_coap_connection_handler_msg_prevalidate_cb_read_and_set();
bool test_coap_connection_handler_find_by_socket_port();
#ifdef __cplusplus
}
#endif

View File

@ -157,7 +157,7 @@ bool test_coap_message_handler_coap_msg_process()
memset(&buf, 1, 16);
bool ret_val = false;
/*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, 61131, buf, 22, ns_in6addr_any, NULL, 0, NULL)) {
goto exit;
}
@ -169,7 +169,7 @@ bool test_coap_message_handler_coap_msg_process()
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, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}
@ -178,7 +178,7 @@ bool test_coap_message_handler_coap_msg_process()
sn_coap_protocol_stub.expectedHeader->coap_status = 66;
nsdynmemlib_stub.returnCounter = 1;
/* 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, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}
@ -189,7 +189,7 @@ bool test_coap_message_handler_coap_msg_process()
retValue = 0;
/* request received */
nsdynmemlib_stub.returnCounter = 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, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}
@ -199,7 +199,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, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}
@ -209,7 +209,7 @@ bool test_coap_message_handler_coap_msg_process()
sn_coap_protocol_stub.expectedHeader->msg_code = 333;
nsdynmemlib_stub.returnCounter = 1;
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, buf, 61131, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}
@ -231,7 +231,7 @@ bool test_coap_message_handler_coap_msg_process()
sn_coap_protocol_stub.expectedHeader->msg_id = 2;
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, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
goto exit;
}

View File

@ -33,7 +33,8 @@ TEST_SRC_FILES = \
../stub/coap_connection_handler_stub.c \
../stub/coap_message_handler_stub.c \
../stub/common_functions_stub.c \
../stub/sn_coap_protocol_stub.c
../stub/sn_coap_protocol_stub.c \
../stub/socket_api_stub.c
include ../MakefileWorker.mk

View File

@ -120,3 +120,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_msg_prevalidate_cb_read_and_set)
{
CHECK(test_coap_service_msg_prevalidate_cb_read_and_set())
}

View File

@ -45,6 +45,11 @@ int virtual_sock_send_cb(int8_t service_id, uint8_t destination_addr_ptr[static
return 2;
}
int msg_prevalidate_cb(int8_t interface_id, uint8_t address[static 16])
{
return 1;
}
bool test_coap_service_initialize()
{
if (-1 != coap_service_initialize(1, 2, 0, NULL, NULL)) {
@ -374,6 +379,7 @@ bool test_eventOS_callbacks()
bool test_conn_handler_callbacks()
{
uint8_t buf[16];
uint8_t local_addr[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;
@ -395,7 +401,7 @@ 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, 2, buf, 12, NULL, NULL, 0)) {
return false;
}
@ -403,7 +409,7 @@ bool test_conn_handler_callbacks()
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, 2, buf, 12, NULL, ptr, 5)) {
return false;
}
ns_dyn_mem_free(ptr);
@ -411,8 +417,8 @@ 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 (coap_message_handler_stub.msg_process_cb) {
if (-1 != coap_message_handler_stub.msg_process_cb(1, 1, NULL, NULL, local_addr)) {
return false;
}
@ -423,7 +429,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.msg_process_cb(1, 1, coap, NULL, local_addr)) {
return false;
}
@ -433,14 +439,27 @@ bool test_conn_handler_callbacks()
return false;
}
if (-1 != coap_message_handler_stub.cb(1, coap, NULL)) {
if (-1 != coap_message_handler_stub.msg_process_cb(1, 1, coap, NULL, local_addr)) {
return false;
}
coap_transaction_t *tr = (coap_transaction_t *)malloc(sizeof(coap_transaction_t));
memset(tr, 0, sizeof(coap_transaction_t));
tr->local_address[0] = 2;
if (2 != coap_message_handler_stub.cb(1, coap, tr)) {
if (0 != coap_service_msg_prevalidate_callback_set(2, msg_prevalidate_cb)) {
return false;
}
if (-1 != coap_message_handler_stub.msg_process_cb(1, 1, coap, tr, local_addr)) {
return false;
}
if (0 != coap_service_msg_prevalidate_callback_set(2, NULL)) {
return false;
}
if (2 != coap_message_handler_stub.msg_process_cb(1, 1, coap, tr, local_addr)) {
return false;
}
@ -645,3 +664,36 @@ bool test_coap_service_handshake_limit_set()
return true;
}
bool test_coap_service_msg_prevalidate_cb_read_and_set()
{
/* No valid socket port - return failure */
if (0 == coap_service_msg_prevalidate_callback_set(99, msg_prevalidate_cb)) {
return false;
}
/* Init service */
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;
thread_conn_handler_stub.bool_value = 0;
if (1 != coap_service_initialize(1, 2, 0, NULL, NULL)) {
return false;
}
if (0 != coap_service_msg_prevalidate_callback_set(2, msg_prevalidate_cb)) {
return false;
}
if (0 != coap_service_msg_prevalidate_callback_set(2, NULL)) {
return false;
}
/* Teardown */
coap_service_delete(1);
free(thread_conn_handler_stub.handler_obj);
thread_conn_handler_stub.handler_obj = NULL;
return true;
}

View File

@ -61,6 +61,8 @@ bool test_coap_service_if_find_by_socket();
bool test_coap_service_handshake_limit_set();
bool test_coap_service_msg_prevalidate_cb_read_and_set();
#ifdef __cplusplus
}

View File

@ -24,16 +24,6 @@ INCLUDE_DIRS =\
../stub\
../../../../coap-service/\
../../../../source/include/\
../../../../yotta_modules/mbedtls/ \
../../../../yotta_modules/sal-stack-nanostack/nanostack/ \
../../../../yotta_modules/nanostack-randlib/mbed-client-randlib/ \
../../../../yotta_modules/nanostack-libservice/ \
../../../../yotta_modules/nanostack-libservice/mbed-client-libservice/ \
../../../../yotta_modules/mbed-coap/ \
../../../../yotta_modules/mbed-coap/source/include/ \
../../../../yotta_modules/sal-stack-nanostack-eventloop/nanostack-event-loop/ \
../../../../yotta_modules/sal-stack-nanostack-eventloop/source/ \
../../../../yotta_modules/mbed-trace/ \
../../../../../nanostack/nanostack/\
../../../../../libService/libService/\
../../../../../mbed-coap/\

View File

@ -38,7 +38,6 @@ lcov -q -d ../. -c -o app.info
lcov -q -r app.info "/test*" -o app.info
lcov -q -r app.info "/usr*" -o app.info
lcov -q -r app.info "/libService*" -o app.info
lcov -q -r app.info "/yotta_modules*" -o app.info
genhtml -q --no-branch-coverage app.info
cd ..
echo

View File

@ -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 recv_if_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]))
@ -42,6 +42,7 @@ coap_conn_handler_t *connection_handler_create(int (*recv_cb)(int8_t socket_id,
thread_conn_handler_stub.receive_from_sock_cb = recv_cb;
thread_conn_handler_stub.get_passwd_cb = pw_cb;
thread_conn_handler_stub.sec_done_cb = done_cb;
thread_conn_handler_stub.cch_function_callback = NULL;
return thread_conn_handler_stub.handler_obj;
}
@ -81,5 +82,22 @@ int8_t coap_connection_handler_handshake_limits_set(uint8_t handshakes_limit, ui
void coap_connection_handler_exec(uint32_t time)
{
}
int coap_connection_handler_msg_prevalidate_callback_set(coap_conn_handler_t *handler, cch_func_cb *function_callback)
{
thread_conn_handler_stub.cch_function_callback = function_callback;
return 0;
}
cch_func_cb *coap_connection_handler_msg_prevalidate_callback_get(coap_conn_handler_t *handler, uint16_t *listen_socket_port)
{
*listen_socket_port = 0;
return thread_conn_handler_stub.cch_function_callback;
}
coap_conn_handler_t *coap_connection_handler_find_by_socket_port(uint16_t listen_port)
{
return thread_conn_handler_stub.handler_obj;
}

View File

@ -30,9 +30,10 @@ typedef struct {
int int_value;
bool bool_value;
coap_conn_handler_t *handler_obj;
cch_func_cb *cch_function_callback;
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 recv_if_id, uint8_t src_address[static 16], uint16_t port, const uint8_t dst_address[static 16], unsigned char *, int);
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]);

View File

@ -55,10 +55,10 @@ 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 *))
extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t socket_id, int8_t recv_if_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, coap_msg_process_cb *msg_process_callback)
{
coap_message_handler_stub.cb = cb;
coap_message_handler_stub.msg_process_cb = msg_process_callback;
return coap_message_handler_stub.int16_value;
}

View File

@ -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 *);
coap_msg_process_cb *msg_process_cb;
} coap_message_handler_stub_def;
extern coap_message_handler_stub_def coap_message_handler_stub;

View File

@ -19,13 +19,13 @@
#include <stdarg.h>
#include <stdlib.h>
#ifndef YOTTA_CFG_MBED_TRACE
#define YOTTA_CFG_MBED_TRACE 1
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1
#ifndef MBED_CONF_MBED_TRACE_ENABLE
#define MBED_CONF_MBED_TRACE_ENABLE 1
#define MBED_CONF_MBED_TRACE_FEA_IPV6 1
#endif
#include "mbed-trace/mbed_trace.h"
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
#include "mbed-client-libservice/ip6string.h"
#include "mbed-client-libservice/common_functions.h"
#endif
@ -99,7 +99,7 @@ const char *mbed_trace_last(void)
/* Helping functions */
#define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
char *mbed_trace_ipv6(const void *addr_ptr)
{
return NULL;
@ -109,7 +109,7 @@ char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
{
return NULL;
}
#endif //YOTTA_CFG_MBED_TRACE_FEA_IPV6
#endif //MBED_CONF_MBED_TRACE_FEA_IPV6
char *mbed_trace_array(const uint8_t *buf, uint16_t len)
{

View File

@ -59,6 +59,11 @@ void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor
}
void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
{
}
void mbedtls_ssl_config_init(mbedtls_ssl_config *a)
{
@ -198,6 +203,16 @@ int mbedtls_ssl_write(mbedtls_ssl_context *a, const unsigned char *b, size_t c)
return mbedtls_stub.expected_int;
}
int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
{
return 0;
}
const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
{
return NULL;
}
//From crt_drbg.h
@ -252,6 +267,18 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *a, const unsigned char *b, size_t c
return mbedtls_stub.expected_int;
}
int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
const mbedtls_x509_crt *crt)
{
return 0;
}
int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
uint32_t flags)
{
return 0;
}
//From entropy.h
void mbedtls_entropy_init(mbedtls_entropy_context *a)
{
@ -356,3 +383,19 @@ int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
}
return mbedtls_stub.expected_int;
}
void mbedtls_strerror(int ret, char *buf, size_t buflen)
{
}
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
{
(void)ctx;
return (0);
}
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
{
(void)ctx;
}

View File

@ -28,7 +28,7 @@
#include "mbedtls/sha256.h"
#include "mbedtls/entropy.h"
#include "mbedtls/pk.h"
#include "mbedtls/platform.h"
#define HANDSHAKE_FINISHED_VALUE 8888