Skip to content

Commit

Permalink
Squashed 'features/nanostack/coap-service/' changes from 1cb994e..cbe…
Browse files Browse the repository at this point in the history
…656a

cbe656a Fix 'unused variable' compiler - warning (ARMmbed#103)
1599c6b CoAP blockwise transfer support (ARMmbed#94)
40abace Update memory allocation and adjust trace (ARMmbed#102)
fc7bec3 Update unit test stub (ARMmbed#101)
4091f1b Check for coap_security_handler_connect_non_blocking return value (ARMmbed#100)

git-subtree-dir: features/nanostack/coap-service
git-subtree-split: cbe656a
  • Loading branch information
deepakvenugopal committed Aug 8, 2018
1 parent 03edf99 commit bee5d60
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 50 deletions.
13 changes: 13 additions & 0 deletions coap-service/coap_service_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
83 changes: 52 additions & 31 deletions source/coap_message_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -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){
Expand All @@ -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;
}
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
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) {
// negative return value = message ignored -> delete transaction
transaction_delete(transaction_ptr);
}
goto exit;
/* 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);
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions source/include/coap_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit bee5d60

Please sign in to comment.