Merge commit 'bee5d601f841949ed8d2e6eecef648d09a3d1984'

* commit 'bee5d601f841949ed8d2e6eecef648d09a3d1984':
  Squashed 'features/nanostack/coap-service/' changes from 1cb994e..cbe656a
pull/7737/head
Deepak Venugopal 2018-08-08 16:56:19 +03:00
commit b82566eeaa
9 changed files with 134 additions and 50 deletions

View File

@ -353,6 +353,19 @@ extern int8_t coap_service_set_duplicate_message_buffer(int8_t service_id, uint8
*/
extern int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert, uint16_t cert_len, const unsigned char *priv_key, uint8_t priv_key_len);
/**
* \brief Set CoAP blockwise payload size
*
* Set CoAP blockwise payload limit. If payload is bigger than configured limit, CoAP message will use blockwise option when sending.
*
* \param service_id Id number of the current service.
* \param size Blockwise size. Valid sizes are 16, 32, 64, 128, 256, 512 and 1024 bytes
*
* \return -1 For failure
*- 0 For success
*/
extern int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size);
#ifdef __cplusplus
}
#endif

View File

@ -175,6 +175,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
}
}
if(!to_be_removed){
tr_err("max session count exceeded");
return NULL;
}
@ -188,6 +189,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
}
}
if(handshakes >= max_handshakes) {
tr_err("ongoing handshakes exceeded");
return NULL;
}
@ -215,6 +217,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
this->sec_handler = coap_security_create(parent->socket, this->timer.id, this, secure_mode,
&secure_session_sendto, &secure_session_recvfrom, &start_timer, &timer_status);
if( !this->sec_handler ){
tr_err("security create failed");
ns_dyn_mem_free(this);
return NULL;
}
@ -401,7 +404,6 @@ static int send_to_real_socket(int8_t socket_id, const ns_address_t *address, co
ns_cmsghdr_t *cmsg;
ns_in6_pktinfo_t *pktinfo;
tr_debug("send from source address %s", trace_array(source_address, 16));
msghdr.msg_control = ancillary_databuffer;
msghdr.msg_controllen = sizeof(ancillary_databuffer);
@ -647,9 +649,11 @@ static void secure_recv_sckt_msg(void *cb_res)
session->last_contact_time = coap_service_get_internal_timer_ticks();
// Start handshake
if (!coap_security_handler_is_started(session->sec_handler)) {
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max);
if(-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max)) {
tr_err("Connection start failed");
secure_session_delete(session);
}
ns_dyn_mem_free(keys._key);
}
} else {
//Continue handshake
@ -742,11 +746,16 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
if (sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->socket, address, port, &keys)) {
session = secure_session_create(sock, address, port, keys.mode);
if (!session) {
tr_err("coap_connection_handler_virtual_recv session creation failed - OOM");
tr_err("coap_connection_handler_virtual_recv session creation failed");
ns_dyn_mem_free(keys._key);
return -1;
}
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max);
if (-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max)) {
tr_err("Connection start failed");
ns_dyn_mem_free(keys._key);
secure_session_delete(session);
return -1;
}
ns_dyn_mem_free(keys._key);
return 0;
} else {

View File

@ -20,6 +20,7 @@
#include "coap_service_api_internal.h"
#include "coap_message_handler.h"
#include "mbed-coap/sn_coap_protocol.h"
#include "source/include/sn_coap_protocol_internal.h"
#include "socket_api.h"
#include "ns_types.h"
#include "ns_list.h"
@ -138,7 +139,6 @@ void transaction_delete(coap_transaction_t *this)
if (!coap_message_handler_transaction_valid(this)) {
return;
}
ns_list_remove(&request_list, this);
transaction_free(this);
@ -163,11 +163,14 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ptr, void *param)
{
coap_transaction_t *this = NULL;
(void)address_ptr;
(void)param;
if (resp_ptr->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_DONE) {
return 0;
}
tr_warn("transaction was not handled %d", resp_ptr->msg_id);
if (!resp_ptr) {
if (!resp_ptr || !address_ptr) {
return -1;
}
if(resp_ptr->token_ptr){
@ -193,7 +196,7 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
}
coap_msg_handler_t *handle;
handle = used_malloc_func_ptr(sizeof(coap_msg_handler_t));
handle = ns_dyn_mem_alloc(sizeof(coap_msg_handler_t));
if (handle == NULL) {
return NULL;
}
@ -207,13 +210,16 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
handle->coap = sn_coap_protocol_init(used_malloc_func_ptr, used_free_func_ptr, used_tx_callback_ptr, &coap_rx_function);
if( !handle->coap ){
used_free_func_ptr(handle);
ns_dyn_mem_free(handle);
return NULL;
}
/* Set default buffer size for CoAP duplicate message detection */
sn_coap_protocol_set_duplicate_buffer_size(handle->coap, DUPLICATE_MESSAGE_BUFFER_SIZE);
/* Set default blockwise message size. */
sn_coap_protocol_set_block_size(handle->coap, DEFAULT_BLOCKWISE_DATA_SIZE);
/* Set default CoAP retransmission paramters */
sn_coap_protocol_set_retransmission_parameters(handle->coap, COAP_RESENDING_COUNT, COAP_RESENDING_INTERVAL);
@ -263,6 +269,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
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) {
return -1;
@ -273,8 +280,19 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
src_addr.type = SN_NSDL_ADDRESS_TYPE_IPV6;
src_addr.port = port;
coap_message = sn_coap_protocol_parse(handle->coap, &src_addr, data_len, data_ptr, NULL);
coap_transaction_t *transaction_ptr = transaction_create();
if (!transaction_ptr) {
return -1;
}
transaction_ptr->service_id = coap_service_id_find_by_socket(socket_id);
transaction_ptr->client_request = false;// this is server transaction
memcpy(transaction_ptr->local_address, *(dst_addr_ptr) == 0xFF ? ns_in6addr_any : dst_addr_ptr, 16);
memcpy(transaction_ptr->remote_address, source_addr_ptr, 16);
transaction_ptr->remote_port = port;
coap_message = sn_coap_protocol_parse(handle->coap, &src_addr, data_len, data_ptr, transaction_ptr);
if (coap_message == NULL) {
transaction_delete(transaction_ptr);
tr_err("CoAP Parsing failed");
return -1;
}
@ -284,36 +302,26 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
/* Check, if coap itself sends response, or block receiving is ongoing... */
if (coap_message->coap_status != COAP_STATUS_OK && coap_message->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
tr_debug("CoAP library responds");
transaction_delete(transaction_ptr);
ret_val = -1;
goto exit;
}
/* Request received */
if (coap_message->msg_code > 0 && coap_message->msg_code < 32) {
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
transaction_ptr->req_msg_type = coap_message->msg_type;
memcpy(transaction_ptr->local_address, *(dst_addr_ptr) == 0xFF ? ns_in6addr_any : dst_addr_ptr, 16);
memcpy(transaction_ptr->remote_address, source_addr_ptr, 16);
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;
}
transaction_ptr->remote_port = port;
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
// negative return value = message ignored -> delete transaction
transaction_delete(transaction_ptr);
}
goto exit;
} else {
ret_val = -1;
}
/* Response received */
} else {
coap_transaction_t *this = NULL;
if (coap_message->token_ptr) {
this = transaction_find_client_by_token(coap_message->token_ptr, coap_message->token_len, source_addr_ptr, port);
}
@ -331,6 +339,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
}
exit:
if (coap_message->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
handle->sn_coap_service_free(coap_message->payload_ptr);
}
sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_message);
return ret_val;
@ -350,7 +362,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
tr_debug("Service %d, send CoAP request payload_len %d", service_id, payload_len);
transaction_ptr = transaction_create();
if (!uri || !transaction_ptr) {
if (!uri || !transaction_ptr || !handle) {
return 0;
}
@ -383,7 +395,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
request.payload_len = payload_len;
request.payload_ptr = (uint8_t *) payload_ptr; // Cast away const and trust that nsdl doesn't modify...
data_len = sn_coap_builder_calc_needed_packet_data_size(&request);
prepare_blockwise_message(handle->coap, &request);
data_len = sn_coap_builder_calc_needed_packet_data_size_2(&request, sn_coap_protocol_get_configured_blockwise_size(handle->coap));
data_ptr = own_alloc(data_len);
if(data_len > 0 && !data_ptr){
transaction_delete(transaction_ptr);
@ -408,6 +423,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
// Free allocated data
own_free(data_ptr);
if(request.options_list_ptr) {
own_free(request.options_list_ptr);
}
if(request_response_cb == NULL){
//No response expected
return 0;
@ -426,8 +445,10 @@ static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handl
dst_addr.addr_len = 16;
dst_addr.type = SN_NSDL_ADDRESS_TYPE_IPV6;
dst_addr.port = transaction_ptr->remote_port;
data_len = sn_coap_builder_calc_needed_packet_data_size(coap_msg_ptr);
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
prepare_blockwise_message(handle->coap, coap_msg_ptr);
#endif
data_len = sn_coap_builder_calc_needed_packet_data_size_2(coap_msg_ptr, sn_coap_protocol_get_configured_blockwise_size(handle->coap));
data_ptr = own_alloc(data_len);
if (data_len > 0 && !data_ptr) {
return -1;

View File

@ -606,3 +606,14 @@ int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert
return 0;
}
int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size)
{
(void) service_id;
if (!coap_service_handle) {
return -1;
}
return sn_coap_protocol_set_block_size(coap_service_handle->coap, size);
}

View File

@ -23,9 +23,13 @@
#include "ns_list.h"
#define TRANSACTION_LIFETIME 180
/* Default value for CoAP duplicate message buffer (0 = disabled) */
#define DUPLICATE_MESSAGE_BUFFER_SIZE 0
/* Default value for CoAP blockwise data size (0 = disabled) */
#define DEFAULT_BLOCKWISE_DATA_SIZE 0
/* Default values for CoAP resendings */
#define COAP_RESENDING_COUNT 3
#define COAP_RESENDING_INTERVAL 10

View File

@ -81,6 +81,7 @@ bool test_coap_message_handler_init()
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));
nsdynmemlib_stub.returnCounter = 1;
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
if( NULL == handle )
return false;
@ -97,6 +98,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));
nsdynmemlib_stub.returnCounter = 1;
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) )
@ -113,6 +115,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));
nsdynmemlib_stub.returnCounter = 1;
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
uint8_t buf[16];
@ -140,26 +143,29 @@ bool test_coap_message_handler_coap_msg_process()
{
uint8_t buf[16];
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))
return false;
goto exit;
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));
nsdynmemlib_stub.returnCounter = 1;
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))
return false;
goto exit;
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;
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))
return false;
goto exit;
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));
@ -167,8 +173,9 @@ bool test_coap_message_handler_coap_msg_process()
sn_coap_protocol_stub.expectedHeader->msg_code = 1;
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))
return false;
goto exit;
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));
@ -177,15 +184,16 @@ bool test_coap_message_handler_coap_msg_process()
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))
return false;
goto exit;
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 = COAP_STATUS_OK;
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))
return false;
goto exit;
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));
@ -200,20 +208,19 @@ bool test_coap_message_handler_coap_msg_process()
sn_coap_builder_stub.expectedUint16 = 1;
nsdynmemlib_stub.returnCounter = 3;
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
return false;
goto exit;
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))
return false;
// free(sn_coap_protocol_stub.expectedHeader->token_ptr);
goto exit;
ret_val = true;
exit:
free(sn_coap_protocol_stub.expectedCoap);
sn_coap_protocol_stub.expectedCoap = NULL;
coap_message_handler_destroy(handle);
return true;
return ret_val;
}
bool test_coap_message_handler_request_send()
@ -221,6 +228,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));
nsdynmemlib_stub.returnCounter = 1;
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
uint8_t buf[16];
@ -293,6 +301,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));
nsdynmemlib_stub.returnCounter = 1;
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
uint8_t buf[16];
@ -329,6 +338,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));
nsdynmemlib_stub.returnCounter = 1;
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));
@ -385,6 +395,7 @@ bool test_coap_message_handler_exec()
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));
nsdynmemlib_stub.returnCounter = 1;
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))

View File

@ -30,7 +30,7 @@ void ns_dyn_mem_init(uint8_t *heap, uint16_t h_size, void (*passed_fptr)(heap_fa
{
}
#else
void ns_dyn_mem_init(void *heap, uint16_t h_size, void (*passed_fptr)(heap_fail_t), mem_stat_t *info_ptr)
void ns_dyn_mem_init(void *heap, ns_mem_heap_size_t h_size, void (*passed_fptr)(heap_fail_t), mem_stat_t *info_ptr)
{
}
#endif

View File

@ -49,6 +49,11 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_ms
return sn_coap_builder_stub.expectedUint16;
}
uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
{
return sn_coap_builder_stub.expectedUint16;
}
int16_t sn_coap_builder_options_build_add_zero_length_option(uint8_t **dst_packet_data_pptr, uint8_t option_length, uint8_t option_exist, sn_coap_option_numbers_e option_number)
{
return sn_coap_builder_stub.expectedInt16;

View File

@ -110,3 +110,13 @@ int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t ms
{
return 0;
}
uint16_t sn_coap_protocol_get_configured_blockwise_size(struct coap_s *handle)
{
return 0;
}
int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_msg_ptr)
{
return 0;
}