Skip to content

Commit bee5d60

Browse files
Squashed 'features/nanostack/coap-service/' changes from 1cb994e..cbe656a
cbe656a Fix 'unused variable' compiler - warning (#103) 1599c6b CoAP blockwise transfer support (#94) 40abace Update memory allocation and adjust trace (#102) fc7bec3 Update unit test stub (#101) 4091f1b Check for coap_security_handler_connect_non_blocking return value (#100) git-subtree-dir: features/nanostack/coap-service git-subtree-split: cbe656a
1 parent 03edf99 commit bee5d60

File tree

9 files changed

+134
-50
lines changed

9 files changed

+134
-50
lines changed

coap-service/coap_service_api.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,19 @@ extern int8_t coap_service_set_duplicate_message_buffer(int8_t service_id, uint8
353353
*/
354354

355355
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);
356+
357+
/**
358+
* \brief Set CoAP blockwise payload size
359+
*
360+
* Set CoAP blockwise payload limit. If payload is bigger than configured limit, CoAP message will use blockwise option when sending.
361+
*
362+
* \param service_id Id number of the current service.
363+
* \param size Blockwise size. Valid sizes are 16, 32, 64, 128, 256, 512 and 1024 bytes
364+
*
365+
* \return -1 For failure
366+
*- 0 For success
367+
*/
368+
extern int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size);
356369
#ifdef __cplusplus
357370
}
358371
#endif

source/coap_connection_handler.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
175175
}
176176
}
177177
if(!to_be_removed){
178+
tr_err("max session count exceeded");
178179
return NULL;
179180
}
180181

@@ -188,6 +189,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
188189
}
189190
}
190191
if(handshakes >= max_handshakes) {
192+
tr_err("ongoing handshakes exceeded");
191193
return NULL;
192194
}
193195

@@ -215,6 +217,7 @@ static secure_session_t *secure_session_create(internal_socket_t *parent, const
215217
this->sec_handler = coap_security_create(parent->socket, this->timer.id, this, secure_mode,
216218
&secure_session_sendto, &secure_session_recvfrom, &start_timer, &timer_status);
217219
if( !this->sec_handler ){
220+
tr_err("security create failed");
218221
ns_dyn_mem_free(this);
219222
return NULL;
220223
}
@@ -401,7 +404,6 @@ static int send_to_real_socket(int8_t socket_id, const ns_address_t *address, co
401404
ns_cmsghdr_t *cmsg;
402405
ns_in6_pktinfo_t *pktinfo;
403406

404-
tr_debug("send from source address %s", trace_array(source_address, 16));
405407
msghdr.msg_control = ancillary_databuffer;
406408
msghdr.msg_controllen = sizeof(ancillary_databuffer);
407409

@@ -647,9 +649,11 @@ static void secure_recv_sckt_msg(void *cb_res)
647649
session->last_contact_time = coap_service_get_internal_timer_ticks();
648650
// Start handshake
649651
if (!coap_security_handler_is_started(session->sec_handler)) {
650-
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max);
652+
if(-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, sock->timeout_min, sock->timeout_max)) {
653+
tr_err("Connection start failed");
654+
secure_session_delete(session);
655+
}
651656
ns_dyn_mem_free(keys._key);
652-
653657
}
654658
} else {
655659
//Continue handshake
@@ -742,11 +746,16 @@ int coap_connection_handler_virtual_recv(coap_conn_handler_t *handler, uint8_t a
742746
if (sock->parent->_get_password_cb && 0 == sock->parent->_get_password_cb(sock->socket, address, port, &keys)) {
743747
session = secure_session_create(sock, address, port, keys.mode);
744748
if (!session) {
745-
tr_err("coap_connection_handler_virtual_recv session creation failed - OOM");
749+
tr_err("coap_connection_handler_virtual_recv session creation failed");
746750
ns_dyn_mem_free(keys._key);
747751
return -1;
748752
}
749-
coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max);
753+
if (-1 == coap_security_handler_connect_non_blocking(session->sec_handler, true, DTLS, keys, handler->socket->timeout_min, handler->socket->timeout_max)) {
754+
tr_err("Connection start failed");
755+
ns_dyn_mem_free(keys._key);
756+
secure_session_delete(session);
757+
return -1;
758+
}
750759
ns_dyn_mem_free(keys._key);
751760
return 0;
752761
} else {

source/coap_message_handler.c

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "coap_service_api_internal.h"
2121
#include "coap_message_handler.h"
2222
#include "mbed-coap/sn_coap_protocol.h"
23+
#include "source/include/sn_coap_protocol_internal.h"
2324
#include "socket_api.h"
2425
#include "ns_types.h"
2526
#include "ns_list.h"
@@ -138,7 +139,6 @@ void transaction_delete(coap_transaction_t *this)
138139
if (!coap_message_handler_transaction_valid(this)) {
139140
return;
140141
}
141-
142142
ns_list_remove(&request_list, this);
143143
transaction_free(this);
144144

@@ -163,11 +163,14 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
163163
static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ptr, void *param)
164164
{
165165
coap_transaction_t *this = NULL;
166-
(void)address_ptr;
167166
(void)param;
168167

168+
if (resp_ptr->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_DONE) {
169+
return 0;
170+
}
171+
169172
tr_warn("transaction was not handled %d", resp_ptr->msg_id);
170-
if (!resp_ptr) {
173+
if (!resp_ptr || !address_ptr) {
171174
return -1;
172175
}
173176
if(resp_ptr->token_ptr){
@@ -193,7 +196,7 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
193196
}
194197

195198
coap_msg_handler_t *handle;
196-
handle = used_malloc_func_ptr(sizeof(coap_msg_handler_t));
199+
handle = ns_dyn_mem_alloc(sizeof(coap_msg_handler_t));
197200
if (handle == NULL) {
198201
return NULL;
199202
}
@@ -207,13 +210,16 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
207210

208211
handle->coap = sn_coap_protocol_init(used_malloc_func_ptr, used_free_func_ptr, used_tx_callback_ptr, &coap_rx_function);
209212
if( !handle->coap ){
210-
used_free_func_ptr(handle);
213+
ns_dyn_mem_free(handle);
211214
return NULL;
212215
}
213216

214217
/* Set default buffer size for CoAP duplicate message detection */
215218
sn_coap_protocol_set_duplicate_buffer_size(handle->coap, DUPLICATE_MESSAGE_BUFFER_SIZE);
216219

220+
/* Set default blockwise message size. */
221+
sn_coap_protocol_set_block_size(handle->coap, DEFAULT_BLOCKWISE_DATA_SIZE);
222+
217223
/* Set default CoAP retransmission paramters */
218224
sn_coap_protocol_set_retransmission_parameters(handle->coap, COAP_RESENDING_COUNT, COAP_RESENDING_INTERVAL);
219225

@@ -263,6 +269,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
263269
sn_nsdl_addr_s src_addr;
264270
sn_coap_hdr_s *coap_message;
265271
int16_t ret_val = 0;
272+
coap_transaction_t *this = NULL;
266273

267274
if (!cb || !handle) {
268275
return -1;
@@ -273,8 +280,19 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
273280
src_addr.type = SN_NSDL_ADDRESS_TYPE_IPV6;
274281
src_addr.port = port;
275282

276-
coap_message = sn_coap_protocol_parse(handle->coap, &src_addr, data_len, data_ptr, NULL);
283+
coap_transaction_t *transaction_ptr = transaction_create();
284+
if (!transaction_ptr) {
285+
return -1;
286+
}
287+
transaction_ptr->service_id = coap_service_id_find_by_socket(socket_id);
288+
transaction_ptr->client_request = false;// this is server transaction
289+
memcpy(transaction_ptr->local_address, *(dst_addr_ptr) == 0xFF ? ns_in6addr_any : dst_addr_ptr, 16);
290+
memcpy(transaction_ptr->remote_address, source_addr_ptr, 16);
291+
transaction_ptr->remote_port = port;
292+
293+
coap_message = sn_coap_protocol_parse(handle->coap, &src_addr, data_len, data_ptr, transaction_ptr);
277294
if (coap_message == NULL) {
295+
transaction_delete(transaction_ptr);
278296
tr_err("CoAP Parsing failed");
279297
return -1;
280298
}
@@ -284,36 +302,26 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
284302
/* Check, if coap itself sends response, or block receiving is ongoing... */
285303
if (coap_message->coap_status != COAP_STATUS_OK && coap_message->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
286304
tr_debug("CoAP library responds");
305+
transaction_delete(transaction_ptr);
287306
ret_val = -1;
288307
goto exit;
289308
}
290309

291310
/* Request received */
292311
if (coap_message->msg_code > 0 && coap_message->msg_code < 32) {
293-
coap_transaction_t *transaction_ptr = transaction_create();
294-
if (transaction_ptr) {
295-
transaction_ptr->service_id = coap_service_id_find_by_socket(socket_id);
296-
transaction_ptr->msg_id = coap_message->msg_id;
297-
transaction_ptr->client_request = false;// this is server transaction
298-
transaction_ptr->req_msg_type = coap_message->msg_type;
299-
memcpy(transaction_ptr->local_address, *(dst_addr_ptr) == 0xFF ? ns_in6addr_any : dst_addr_ptr, 16);
300-
memcpy(transaction_ptr->remote_address, source_addr_ptr, 16);
301-
if (coap_message->token_len) {
302-
memcpy(transaction_ptr->token, coap_message->token_ptr, coap_message->token_len);
303-
transaction_ptr->token_len = coap_message->token_len;
304-
}
305-
transaction_ptr->remote_port = port;
306-
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
307-
// negative return value = message ignored -> delete transaction
308-
transaction_delete(transaction_ptr);
309-
}
310-
goto exit;
311-
} else {
312-
ret_val = -1;
312+
transaction_ptr->msg_id = coap_message->msg_id;
313+
transaction_ptr->req_msg_type = coap_message->msg_type;
314+
if (coap_message->token_len) {
315+
memcpy(transaction_ptr->token, coap_message->token_ptr, coap_message->token_len);
316+
transaction_ptr->token_len = coap_message->token_len;
313317
}
318+
if (cb(socket_id, coap_message, transaction_ptr) < 0) {
319+
// negative return value = message ignored -> delete transaction
320+
transaction_delete(transaction_ptr);
321+
}
322+
goto exit;
314323
/* Response received */
315324
} else {
316-
coap_transaction_t *this = NULL;
317325
if (coap_message->token_ptr) {
318326
this = transaction_find_client_by_token(coap_message->token_ptr, coap_message->token_len, source_addr_ptr, port);
319327
}
@@ -331,6 +339,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
331339
}
332340

333341
exit:
342+
if (coap_message->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
343+
handle->sn_coap_service_free(coap_message->payload_ptr);
344+
}
345+
334346
sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_message);
335347

336348
return ret_val;
@@ -350,7 +362,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
350362
tr_debug("Service %d, send CoAP request payload_len %d", service_id, payload_len);
351363
transaction_ptr = transaction_create();
352364

353-
if (!uri || !transaction_ptr) {
365+
if (!uri || !transaction_ptr || !handle) {
354366
return 0;
355367
}
356368

@@ -383,7 +395,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
383395

384396
request.payload_len = payload_len;
385397
request.payload_ptr = (uint8_t *) payload_ptr; // Cast away const and trust that nsdl doesn't modify...
386-
data_len = sn_coap_builder_calc_needed_packet_data_size(&request);
398+
399+
prepare_blockwise_message(handle->coap, &request);
400+
401+
data_len = sn_coap_builder_calc_needed_packet_data_size_2(&request, sn_coap_protocol_get_configured_blockwise_size(handle->coap));
387402
data_ptr = own_alloc(data_len);
388403
if(data_len > 0 && !data_ptr){
389404
transaction_delete(transaction_ptr);
@@ -408,6 +423,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
408423

409424
// Free allocated data
410425
own_free(data_ptr);
426+
if(request.options_list_ptr) {
427+
own_free(request.options_list_ptr);
428+
}
429+
411430
if(request_response_cb == NULL){
412431
//No response expected
413432
return 0;
@@ -426,8 +445,10 @@ static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handl
426445
dst_addr.addr_len = 16;
427446
dst_addr.type = SN_NSDL_ADDRESS_TYPE_IPV6;
428447
dst_addr.port = transaction_ptr->remote_port;
429-
430-
data_len = sn_coap_builder_calc_needed_packet_data_size(coap_msg_ptr);
448+
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
449+
prepare_blockwise_message(handle->coap, coap_msg_ptr);
450+
#endif
451+
data_len = sn_coap_builder_calc_needed_packet_data_size_2(coap_msg_ptr, sn_coap_protocol_get_configured_blockwise_size(handle->coap));
431452
data_ptr = own_alloc(data_len);
432453
if (data_len > 0 && !data_ptr) {
433454
return -1;

source/coap_service_api.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,14 @@ int8_t coap_service_certificate_set(int8_t service_id, const unsigned char *cert
606606

607607
return 0;
608608
}
609+
610+
int8_t coap_service_blockwise_size_set(int8_t service_id, uint16_t size)
611+
{
612+
(void) service_id;
613+
614+
if (!coap_service_handle) {
615+
return -1;
616+
}
617+
618+
return sn_coap_protocol_set_block_size(coap_service_handle->coap, size);
619+
}

source/include/coap_message_handler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
#include "ns_list.h"
2424

2525
#define TRANSACTION_LIFETIME 180
26+
2627
/* Default value for CoAP duplicate message buffer (0 = disabled) */
2728
#define DUPLICATE_MESSAGE_BUFFER_SIZE 0
2829

30+
/* Default value for CoAP blockwise data size (0 = disabled) */
31+
#define DEFAULT_BLOCKWISE_DATA_SIZE 0
32+
2933
/* Default values for CoAP resendings */
3034
#define COAP_RESENDING_COUNT 3
3135
#define COAP_RESENDING_INTERVAL 10

0 commit comments

Comments
 (0)