From ae0579f17378e443a925ded2968530016bae6a35 Mon Sep 17 00:00:00 2001 From: Arto Kinnunen Date: Tue, 28 Jun 2016 10:45:30 +0300 Subject: [PATCH] Update content_type to sn_coap_content_format_e (#27) -Update content_type to be sn_coap_content_format_e instead of uint8_t to avoid type conversion (bugs). -Fix compiler warnings --- coap-service/coap_service_api.h | 8 ++++---- source/coap_connection_handler.c | 1 + source/coap_message_handler.c | 9 +++++---- source/coap_security_handler.c | 6 ++++-- source/coap_service_api.c | 14 +++++++------- source/include/coap_message_handler.h | 4 ++-- .../unittest/stub/coap_message_handler_stub.c | 10 ++++------ .../unittest/stub/coap_service_api_stub.c | 9 +++++---- 8 files changed, 32 insertions(+), 29 deletions(-) diff --git a/coap-service/coap_service_api.h b/coap-service/coap_service_api.h index c1f9063c00..9924d1f311 100644 --- a/coap-service/coap_service_api.h +++ b/coap-service/coap_service_api.h @@ -119,11 +119,11 @@ typedef int coap_service_security_done_cb(int8_t service_id, uint8_t address[sta * \param listen_port Port that Application wants to use for communicate with coap server. * \param service_options Options of the current service. * \param *start_ptr Callback to inform security handling is started and to fetch device password. - * \param *security_done_cb Callback to inform security handling is done. + * \param *coap_security_done_cb Callback to inform security handling is done. * * \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 *security_done_cb); +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); /** * \brief Service delete @@ -245,7 +245,7 @@ extern int8_t coap_service_unregister_uri(int8_t service_id, const char *uri); * \return msg_id Id number of the current message. */ extern uint16_t coap_service_request_send(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, - uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb); + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb); /** * \brief Sends CoAP service response @@ -260,7 +260,7 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co * \return -1 For failure *- 0 For success */ -extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len); +extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len); extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max); #ifdef __cplusplus diff --git a/source/coap_connection_handler.c b/source/coap_connection_handler.c index 03d0b68e5f..4bbe7d0856 100644 --- a/source/coap_connection_handler.c +++ b/source/coap_connection_handler.c @@ -317,6 +317,7 @@ static int send_to_socket(int8_t socket_id, uint8_t *address_ptr, uint16_t port, static int receive_from_socket(int8_t socket_id, unsigned char *buf, size_t len) { + (void)len; internal_socket_t *sock = int_socket_find_by_socket_id(socket_id); if( sock->data && sock->data_len > 0 ){ memcpy( buf, sock->data, sock->data_len ); diff --git a/source/coap_message_handler.c b/source/coap_message_handler.c index bcdb99eb45..613547c8d2 100644 --- a/source/coap_message_handler.c +++ b/source/coap_message_handler.c @@ -248,7 +248,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t 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, - uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) { coap_transaction_t *transaction_ptr; sn_coap_hdr_s request; @@ -280,7 +280,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se request.msg_code = msg_code; request.uri_path_ptr = (uint8_t *)uri; request.uri_path_len = strlen(uri); - coap_service_build_content_format(&request, (sn_coap_content_format_e)cont_type); + coap_service_build_content_format(&request, cont_type); do{ randLIB_get_n_bytes_random(token,4); @@ -313,7 +313,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se } //TODO: refactor this to use nsdl -int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len) +int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { coap_transaction_t *transaction_ptr; sn_coap_hdr_s *response; @@ -321,6 +321,7 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser uint16_t data_len; uint8_t *data_ptr; (void) options; + (void)service_id; tr_debug("Service %d, send CoAP response", service_id); if (!request_ptr || !handle) { @@ -345,7 +346,7 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser } response->payload_len = payload_len; response->payload_ptr = (uint8_t *) payload_ptr; // Cast away const and trust that nsdl doesn't modify... - coap_service_build_content_format(response, (sn_coap_content_format_e)content_type); + coap_service_build_content_format(response, content_type); data_len = sn_coap_builder_calc_needed_packet_data_size(response); data_ptr = own_alloc(data_len); diff --git a/source/coap_security_handler.c b/source/coap_security_handler.c index e239857111..b235bea406 100644 --- a/source/coap_security_handler.c +++ b/source/coap_security_handler.c @@ -26,7 +26,7 @@ const int ECJPAKE_SUITES[] = { }; #endif -const static int PSK_SUITES[] = { +static const int PSK_SUITES[] = { MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256, MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, @@ -204,7 +204,7 @@ static int simple_cookie_check(void *ctx, } /**** Key export function ****/ - +#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) static int export_key_block(void *ctx, const unsigned char *mk, const unsigned char *kb, size_t maclen, size_t keylen, size_t ivlen) @@ -224,6 +224,7 @@ static int export_key_block(void *ctx, memcpy(p_key_block->value, kb /* + (2 * maclen)*/, (2 * keylen) + (2 * ivlen)); return 0; } +#endif int coap_security_handler_configure_keys( coap_security_t *sec, coap_security_keys_t keys ) { @@ -559,6 +560,7 @@ int f_recv(void *ctx, unsigned char *buf, size_t len){ int entropy_poll( void *ctx, unsigned char *output, size_t len, size_t *olen ) { + (void)ctx; //TODO: change to more secure random randLIB_seed_random(); char *c = (char*)ns_dyn_mem_temporary_alloc(len); diff --git a/source/coap_service_api.c b/source/coap_service_api.c index b4d78e6eb6..ed5063ac4e 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -36,7 +36,7 @@ typedef struct uri_registration { typedef NS_LIST_HEAD(uri_registration_t, link) uri_registration_list_t; typedef struct coap_service { - coap_service_security_done_cb *security_done_cb; + coap_service_security_done_cb *coap_security_done_cb; coap_service_security_start_cb *security_start_cb; coap_service_virtual_socket_send_cb *virtual_socket_send_cb; uri_registration_list_t uri_list; @@ -244,8 +244,8 @@ static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t p { //TODO: this is not enough if shared socket. Inform all! coap_service_t *this = service_find_by_socket(socket_id); - if (this && this->security_done_cb) { // secure done callback - this->security_done_cb(this->service_id, address, keyblock); + if (this && this->coap_security_done_cb) { // secure done callback + this->coap_security_done_cb(this->service_id, address, keyblock); } //TODO refactor this away. There should be no transaction_ptr(s) before done_cb has been called @@ -276,7 +276,7 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t } 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 *security_done_cb) + coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) { (void) interface_id; @@ -295,7 +295,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_ this->service_options = service_options; this->security_start_cb = start_ptr; - this->security_done_cb = security_done_cb; + this->coap_security_done_cb = coap_security_done_cb; if (tasklet_id == -1) { tr_debug("service tasklet init"); @@ -447,13 +447,13 @@ int8_t coap_service_unregister_uri(int8_t service_id, const char *uri) } uint16_t coap_service_request_send(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, - uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb){ + sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb){ //TODO: coap_service_response_recv is an ugly cast, this should be refactored away + sn_coap_hdr_s MUST NOT be exposed to users of coap-service! //Callback would be still needed, but where to store callback? return coap_message_handler_request_send(coap_service_handle, service_id, options, destination_addr, destination_port, msg_type, msg_code, uri, cont_type, payload_ptr, payload_len, request_response_cb); } -int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len){ +int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len){ return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len); } diff --git a/source/include/coap_message_handler.h b/source/include/coap_message_handler.h index f12e3f03f7..20a75d6cc7 100644 --- a/source/include/coap_message_handler.h +++ b/source/include/coap_message_handler.h @@ -73,11 +73,11 @@ extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(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, uint8_t cont_type, + 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, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb); extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, - int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len); + sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len); extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time); 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 4be9a3e869..68f2b66912 100644 --- a/test/coap-service/unittest/stub/coap_message_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_message_handler_stub.c @@ -39,15 +39,14 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t return coap_message_handler_stub.int16_value; } -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, uint8_t cont_type, - const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) +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, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb) { return coap_message_handler_stub.uint16_value; } -int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, - int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len) +int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len) { return coap_message_handler_stub.int8_value; } @@ -57,4 +56,3 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti return coap_message_handler_stub.int8_value; } - diff --git a/test/coap-service/unittest/stub/coap_service_api_stub.c b/test/coap-service/unittest/stub/coap_service_api_stub.c index c4a586d647..cdd1d2698d 100644 --- a/test/coap-service/unittest/stub/coap_service_api_stub.c +++ b/test/coap-service/unittest/stub/coap_service_api_stub.c @@ -19,7 +19,7 @@ #include "net_interface.h" 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 *security_done_cb) + coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb) { return 0; } @@ -48,12 +48,13 @@ int8_t coap_service_unregister_uri(int8_t service_id, const char *uri) return 0; } -uint16_t coap_service_request_send(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, - uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb){ +uint16_t coap_service_request_send(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, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb) +{ return 0; } -int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len){ +int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len) +{ return 0; }