Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit e502b67

Browse files
Changed sn_coap_protocol.c to use randLIB for random message ID. (#91)
* Changed sn_coap_protocol.c to use randLIB for random message ID. randLIB now needed to build the coap library. * Added randLIB dependency to module.json * Added check for message_id==0 when randomizing, as we dont want to change the api for sn_nsdl.c, that uses it for error cases when sending some messages. * Added randLiIB include path to unit tests. * Added randLIB_stub for sn_coap_protocol unit test.
1 parent 4cdc357 commit e502b67

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

module.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
],
1515
"dependencies": {
1616
"nanostack-libservice": "^3.0.0",
17-
"mbed-trace": ">=0.2.0,<2.0.0"
17+
"mbed-trace": ">=0.2.0,<2.0.0",
18+
"nanostack-randlib": "^1.2.0"
1819
},
1920
"targetDependencies": {}
2021
}

source/libCoap/src/sn_coap_protocol.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
#include "sn_coap_protocol.h"
4141
#include "sn_coap_header_internal.h"
4242
#include "sn_coap_protocol_internal.h"
43+
#include "randLIB.h"
4344
#include "mbed-trace/mbed_trace.h"
45+
4446
#define TRACE_GROUP "coap"
4547
/* * * * * * * * * * * * * * * * * * * * */
4648
/* * * * LOCAL FUNCTION PROTOTYPES * * * */
@@ -198,12 +200,12 @@ struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), vo
198200
#endif /* ENABLE_RESENDINGS */
199201

200202
/* Randomize global message ID */
201-
#if defined __linux__ || defined TARGET_LIKE_MBED
202-
srand(rand()^time(NULL));
203-
message_id = rand() % 400 + 100;
204-
#else
205-
message_id = 100;
206-
#endif
203+
randLIB_seed_random();
204+
message_id = randLIB_get_16bit();
205+
if (message_id == 0) {
206+
message_id = 1;
207+
}
208+
tr_debug("Coap random msg ID: %d", message_id);
207209

208210
return handle;
209211
}

test/nsdl-c/unittest/makefile_defines.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ INCLUDE_DIRS =\
1313
../../../../nsdl-c\
1414
../../../../yotta_modules/nanostack-libservice/mbed-client-libservice\
1515
../../../../yotta_modules/mbed-trace\
16+
../../../../yotta_modules/nanostack-randlib/mbed-client-randlib\
1617
../../../../../libService/libService\
1718
../../../../source/libCoap/src/include\
1819
../../../../source/libNsdl/src/include\

test/nsdl-c/unittest/sn_coap_protocol/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST_SRC_FILES = \
1212
../stubs/sn_coap_parser_stub.c \
1313
../stubs/sn_coap_header_check_stub.c \
1414
../stubs/ns_list_stub.c \
15+
../stubs/randLIB_stub.cpp \
1516

1617
include ../MakefileWorker.mk
1718

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2016 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "randLIB_stub.h"
18+
19+
20+
uint8_t randLIB_stub::uint8_value;
21+
uint16_t randLIB_stub::uint16_value;
22+
uint32_t randLIB_stub::uint32_value;
23+
uint64_t randLIB_stub::uint64_value;
24+
void* randLIB_stub::void_value;
25+
26+
extern "C"{
27+
28+
void randLIB_seed_random(void){}
29+
30+
void randLIB_add_seed(uint64_t seed){}
31+
32+
uint8_t randLIB_get_8bit(void)
33+
{
34+
return randLIB_stub::uint8_value;
35+
}
36+
37+
uint16_t randLIB_get_16bit(void)
38+
{
39+
return randLIB_stub::uint16_value;
40+
}
41+
42+
uint32_t randLIB_get_32bit(void)
43+
{
44+
return randLIB_stub::uint32_value;
45+
}
46+
47+
uint64_t randLIB_get_64bit(void)
48+
{
49+
return randLIB_stub::uint64_value;
50+
}
51+
52+
void *randLIB_get_n_bytes_random(void *data_ptr, uint8_t count)
53+
{
54+
return randLIB_stub::void_value;
55+
}
56+
57+
uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max)
58+
{
59+
return randLIB_stub::uint16_value;
60+
}
61+
62+
uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor)
63+
{
64+
return randLIB_stub::uint32_value;
65+
}
66+
67+
}
68+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2016 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef M2M_RANDLIB_STUB_H
17+
#define M2M_RANDLIB_STUB_H
18+
19+
#include <stdint.h>
20+
21+
//some internal test related stuff
22+
namespace randLIB_stub
23+
{
24+
extern uint8_t uint8_value;
25+
extern uint16_t uint16_value;
26+
extern uint32_t uint32_value;
27+
extern uint64_t uint64_value;
28+
extern void* void_value;
29+
}
30+
31+
#endif // M2M_RANDLIB_STUB_H

0 commit comments

Comments
 (0)