diff --git a/source/coap_message_handler.c b/source/coap_message_handler.c index 19c3981c72..fee2c12834 100644 --- a/source/coap_message_handler.c +++ b/source/coap_message_handler.c @@ -9,6 +9,7 @@ #include "ns_types.h" #include "ns_list.h" #include "ns_trace.h" +#include "randLIB.h" #define TRACE_GROUP "CoSA" @@ -41,6 +42,19 @@ static coap_transaction_t *transaction_find_client(uint16_t msg_id) } return this; } + +static coap_transaction_t *transaction_find_client_by_token(uint8_t token[4]) +{ + coap_transaction_t *this = NULL; + ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) { + if (memcmp(cur_ptr->token,token,4) == 0 && cur_ptr->client_request) { + this = cur_ptr; + break; + } + } + return this; +} + static coap_transaction_t *transaction_find_server(uint16_t msg_id) { coap_transaction_t *this = NULL; @@ -88,15 +102,16 @@ static void transaction_delete(coap_transaction_t *this) static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ptr, void *param) { - coap_transaction_t *this; + coap_transaction_t *this = NULL; (void)address_ptr; (void)param; tr_warn("transaction was not handled"); if (!resp_ptr) { return -1; } - //TODO: IOTCLT-294 & 295 here. - this = transaction_find_client(resp_ptr->msg_id); + if( resp_ptr->token_ptr ){ + this = transaction_find_client_by_token(resp_ptr->token_ptr); + } if (this && this->resp_cb) { this->resp_cb(this->service_id, resp_ptr->msg_id, NULL); } @@ -210,7 +225,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t } } else { //response find by MSG id - coap_transaction_t *this = transaction_find_client(coap_message->msg_id); + coap_transaction_t *this = NULL; + if( coap_message->token_ptr ){ + this = transaction_find_client_by_token(coap_message->token_ptr); + } if (!this) { tr_error("client transaction not found"); sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_message); @@ -234,6 +252,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se coap_transaction_t *transaction_ptr; sn_coap_hdr_s request; sn_nsdl_addr_s dst_addr; + uint8_t token[4]; uint16_t data_len; uint8_t *data_ptr; @@ -261,7 +280,13 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se request.uri_path_ptr = (uint8_t *)uri; request.uri_path_len = strlen(uri); coap_service_build_content_format(&request, cont_type); - //TODO: check validity of request.content_type_ptr + + do{ + randLIB_get_n_bytes_random(token,4); + }while(transaction_find_client_by_token(token)); + memcpy(transaction_ptr->token,token,4); + request.token_ptr = transaction_ptr->token; + request.token_len = 4; request.payload_len = payload_len; request.payload_ptr = (uint8_t *) payload_ptr; // Cast away const and trust that nsdl doesn't modify... @@ -281,7 +306,6 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se own_free(data_ptr); if(request_response_cb == NULL){ //No response expected - transaction_delete(transaction_ptr); return 0; } return transaction_ptr->msg_id; diff --git a/source/include/coap_message_handler.h b/source/include/coap_message_handler.h index a7170a9a15..7ab67f2966 100644 --- a/source/include/coap_message_handler.h +++ b/source/include/coap_message_handler.h @@ -2,8 +2,8 @@ * Copyright (c) 2015 ARM Limited. All rights reserved. */ -#ifndef __COAPPI_SERVICE_API_H__ -#define __COAPPI_SERVICE_API_H__ +#ifndef __COAP_MESSAGE_HANDLER_H__ +#define __COAP_MESSAGE_HANDLER_H__ #include #include "sn_coap_header.h" @@ -34,6 +34,7 @@ typedef struct coap_msg_handler_s { typedef struct coap_transaction { uint8_t remote_address[16]; uint16_t remote_port; + uint8_t token[4]; uint16_t msg_id; int8_t service_id; uint8_t options; diff --git a/test/coap-service/unittest/coap_message_handler/Makefile b/test/coap-service/unittest/coap_message_handler/Makefile index 8ca92aaf85..312b8c7ec7 100644 --- a/test/coap-service/unittest/coap_message_handler/Makefile +++ b/test/coap-service/unittest/coap_message_handler/Makefile @@ -16,6 +16,7 @@ TEST_SRC_FILES = \ ../stub/sn_coap_builder_stub.c \ ../stub/nsdynmemLIB_stub.c \ ../stub/ns_list_stub.c \ + ../stub/randLIB_stub.c \ include ../MakefileWorker.mk diff --git a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c index 126e05aa68..827fdc7b60 100644 --- a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c +++ b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c @@ -172,11 +172,13 @@ bool test_coap_message_handler_coap_msg_process() return false; sn_coap_protocol_stub.expectedHeader->msg_id = 2; - if( 0 != coap_message_handler_coap_msg_process(handle, 0, buf, 22, NULL, 0, process_cb)) +// 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, NULL, 0, process_cb)) return false; -// free(sn_coap_protocol_stub.expectedHeader); -// sn_coap_protocol_stub.expectedHeader = NULL; +// free(sn_coap_protocol_stub.expectedHeader->token_ptr); + free(sn_coap_protocol_stub.expectedCoap); sn_coap_protocol_stub.expectedCoap = NULL; coap_message_handler_destroy(handle); diff --git a/test/coap-service/unittest/stub/randLIB_stub.c b/test/coap-service/unittest/stub/randLIB_stub.c index ac55243027..5d9f15ff25 100644 --- a/test/coap-service/unittest/stub/randLIB_stub.c +++ b/test/coap-service/unittest/stub/randLIB_stub.c @@ -12,6 +12,8 @@ #error "RAND_MAX isn't 2^n-1 :(" #endif +int counter = 1; + void randLIB_seed_random(void) { } @@ -33,6 +35,9 @@ uint32_t randLIB_get_32bit(void) int8_t randLIB_get_n_bytes_random(uint8_t *data_ptr, uint8_t eight_bit_boundary) { + if(data_ptr && eight_bit_boundary > 0){ + data_ptr[0] = counter++%255; + } return 0; }