Skip to content

Commit 209e49a

Browse files
author
Jarkko Paso
authored
Merge pull request ARMmbed#1528 from ARMmbed/IOTTHD-2046
Iotthd 2046
2 parents 44a85e5 + 89a85a8 commit 209e49a

File tree

7 files changed

+558
-0
lines changed

7 files changed

+558
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright (c) 2016-2017, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "nsconfig.h"
18+
19+
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
20+
21+
#define final(a,b,c) \
22+
{ \
23+
c ^= b; c -= rot(b, 14); \
24+
a ^= c; a -= rot(c, 11); \
25+
b ^= a; b -= rot(a, 25); \
26+
c ^= b; c -= rot(b, 16); \
27+
a ^= c; a -= rot(c, 4); \
28+
b ^= a; b -= rot(a, 14); \
29+
c ^= b; c -= rot(b, 24); \
30+
}
31+
32+
#define mix(a,b,c) \
33+
{ \
34+
a -= c; a ^= rot(c, 4); c += b; \
35+
b -= a; b ^= rot(a, 6); a += c; \
36+
c -= b; c ^= rot(b, 8); b += a; \
37+
a -= c; a ^= rot(c, 16); c += b; \
38+
b -= a; b ^= rot(a, 19); a += c; \
39+
c -= b; c ^= rot(b, 4); b += a; \
40+
}
41+
42+
static uint32_t global_seed = 1;
43+
44+
void tr51_seed_rand(uint32_t seed)
45+
{
46+
if (!seed) {
47+
seed = 1;
48+
}
49+
global_seed = seed;
50+
}
51+
52+
int32_t tr51_get_rand(void)
53+
{
54+
uint32_t random_val = ((global_seed * 1103515245) + 12345) & 0x7fffffff;
55+
global_seed = random_val;
56+
return random_val;
57+
}
58+
59+
void tr51_calculate_channel_table(uint16_t number_of_channels, uint16_t nearest_prime, int32_t *channel_table)
60+
{
61+
int32_t i,j,k;
62+
tr51_seed_rand(1);
63+
for (i=0; i < nearest_prime; i++) {
64+
channel_table[i] = -1;
65+
}
66+
for (i=0; i < number_of_channels; i++) {
67+
j = tr51_get_rand() % number_of_channels;
68+
k=0;
69+
while (k <= i) {
70+
if (j == channel_table[k]) {
71+
j = tr51_get_rand() % number_of_channels;
72+
k=0;
73+
} else {
74+
k=k+1;
75+
}
76+
}
77+
channel_table[i] = j;
78+
}
79+
}
80+
81+
void tr51_compute_cfd(uint8_t *mac, uint8_t *first_element, uint8_t *step_size, uint16_t channel_table_length)
82+
{
83+
*first_element = (mac[5] ^ mac[6] ^ mac[7]) % channel_table_length;
84+
*step_size = (mac[7] % (channel_table_length - 1)) + 1;
85+
}
86+
87+
static uint8_t tr51_find_excluded(int32_t channel, uint16_t *excluded_channels, uint16_t number_of_excluded_channels)
88+
{
89+
uint8_t count = 0;
90+
if (excluded_channels != NULL) {
91+
for (count = 0; count < number_of_excluded_channels; count++) {
92+
if (channel == excluded_channels[count]) {
93+
return 1;
94+
}
95+
}
96+
}
97+
return 0;
98+
}
99+
100+
uint16_t tr51_calculate_hopping_sequence(int32_t *channel_table, uint16_t channel_table_length, uint8_t first_element, uint8_t step_size, int32_t *output_table, uint16_t *excluded_channels, uint16_t number_of_excluded_channels)
101+
{
102+
uint16_t cntr = channel_table_length;
103+
uint8_t index = first_element;
104+
uint8_t slot = 0;
105+
while (cntr--) {
106+
if (channel_table[index] != -1) {
107+
if (!tr51_find_excluded(channel_table[index], excluded_channels, number_of_excluded_channels)) {
108+
output_table[slot] = channel_table[index];
109+
slot++;
110+
}
111+
}
112+
index += step_size;
113+
while (index >= channel_table_length) {
114+
index -= channel_table_length;
115+
}
116+
}
117+
return slot;
118+
}
119+
120+
static uint32_t dh1cf_hashword(const uint32_t *key, size_t key_length, uint32_t init_value)
121+
{
122+
uint32_t a,b,c;
123+
a = b = c = 0xdeadbeef + (((uint32_t)key_length) << 2) + init_value;
124+
while (key_length > 3) {
125+
a += key[0];
126+
b += key[1];
127+
c += key[2];
128+
mix(a, b, c);
129+
key_length -= 3;
130+
key += 3;
131+
}
132+
switch(key_length) {
133+
case 3 : c += key[2];
134+
case 2 : b += key[1];
135+
case 1 : a += key[0];
136+
final(a, b, c);
137+
case 0:
138+
break;
139+
}
140+
return c;
141+
}
142+
143+
int32_t dh1cf_get_uc_channel_index(uint16_t slot_number, uint8_t *mac, int16_t number_of_channels)
144+
{
145+
int32_t channel_number;
146+
uint32_t key[3];
147+
key[0] = (uint32_t)slot_number;
148+
key[1] = mac[4] << 24 | mac[5] << 16 | mac[6] << 8 | mac[7];
149+
key[2] = mac[0] << 24 | mac[1] << 16 | mac[2] << 8 | mac[3];
150+
channel_number = dh1cf_hashword(key, 3, 0) % number_of_channels;
151+
return channel_number;
152+
}
153+
154+
int32_t dh1cf_get_bc_channel_index(uint16_t slot_number, uint16_t bsi, int16_t number_of_channels)
155+
{
156+
int32_t channel_number;
157+
uint32_t key[3];
158+
key[0] = (uint32_t)slot_number;
159+
key[1] = bsi << 16;
160+
key[2] = 0;
161+
channel_number = dh1cf_hashword(key, 3, 0) % number_of_channels;
162+
return channel_number;
163+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016-2017, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @brief Calculate channel table based on TR51 channel function.
20+
* @param number_of_channels Number of channels in table.
21+
* @param nearest_prime Nearest prime number. Must be equal to or larger than number_of_channels.
22+
* @param channel_table Output channel table. Has to be at least nearest_prime in length.
23+
*/
24+
void tr51_calculate_channel_table(uint16_t number_of_channels, uint16_t nearest_prime, int32_t *channel_table);
25+
26+
/**
27+
* @brief Calculate hopping sequence for a specific peer.
28+
* @param channel_table Used channel table.
29+
* @param channel_table_length Length of the used channel table.
30+
* @param first_element Start generated by CFD function.
31+
* @param step_size Step size generated by CFD function.
32+
* @param output_table Output hopping sequence table.
33+
* @param excluded_channels List of not used channels.
34+
* @param number_of_excluded_channels Number of not used channels.
35+
* @return Number of channels in sequence.
36+
*/
37+
uint16_t tr51_calculate_hopping_sequence(int32_t *channel_table, uint16_t channel_table_length, uint8_t first_element, uint8_t step_size, int32_t *output_table, uint16_t *excluded_channels, uint16_t number_of_excluded_channels);
38+
39+
/**
40+
* @brief Compute the unicast schedule channel index.
41+
* @param slot_number Current slot number.
42+
* @param mac MAC address of the node for which the index is calculated.
43+
* @param number_of_channels Number of channels.
44+
* @return Channel index.
45+
*/
46+
int32_t dh1cf_get_uc_channel_index(uint16_t slot_number, uint8_t *mac, int16_t number_of_channels);
47+
48+
/**
49+
* @brief Compute the broadcast schedule channel index.
50+
* @param slot_number Current slot number.
51+
* @param bsi Broadcast schedule identifier of the node for which the index is calculated.
52+
* @param number_of_channels Number of channels.
53+
* @return Channel index.
54+
*/
55+
int32_t dh1cf_get_bc_channel_index(uint16_t slot_number, uint16_t bsi, int16_t number_of_channels);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include ../../makefile_defines.txt
2+
3+
COMPONENT_NAME = channel_functions_unit
4+
5+
#This must be changed manually
6+
SRC_FILES = \
7+
../../../../../source/Service_Libs/fhss/channel_functions.c \
8+
9+
10+
TEST_SRC_FILES = \
11+
main.cpp \
12+
channelfunctest.cpp \
13+
test_channel_functions.c \
14+
15+
include ../../MakefileWorker.mk
16+
17+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
18+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2016, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "CppUTest/TestHarness.h"
18+
#include "test_channel_functions.h"
19+
20+
21+
TEST_GROUP(channel_functions)
22+
{
23+
void setup()
24+
{
25+
}
26+
27+
void teardown()
28+
{
29+
}
30+
};
31+
32+
TEST(channel_functions, test_tr51_get_rand)
33+
{
34+
CHECK(test_tr51_get_rand());
35+
}
36+
37+
TEST(channel_functions, test_tr51_calculate_channel_table)
38+
{
39+
CHECK(test_tr51_calculate_channel_table());
40+
}
41+
42+
TEST(channel_functions, test_tr51_compute_cfd)
43+
{
44+
CHECK(test_tr51_compute_cfd());
45+
}
46+
47+
TEST(channel_functions, test_tr51_calculate_hopping_sequence)
48+
{
49+
CHECK(test_tr51_calculate_hopping_sequence());
50+
}
51+
52+
TEST(channel_functions, test_dh1cf_get_uc_channel_index)
53+
{
54+
CHECK(test_dh1cf_get_uc_channel_index());
55+
}
56+
57+
TEST(channel_functions, test_dh1cf_get_bc_channel_index)
58+
{
59+
CHECK(test_dh1cf_get_bc_channel_index());
60+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2016, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "CppUTest/CommandLineTestRunner.h"
19+
#include "CppUTest/TestPlugin.h"
20+
#include "CppUTest/TestRegistry.h"
21+
#include "CppUTestExt/MockSupportPlugin.h"
22+
int main(int ac, char** av)
23+
{
24+
return CommandLineTestRunner::RunAllTests(ac, av);
25+
}
26+
27+
IMPORT_TEST_GROUP(channel_functions);
28+

0 commit comments

Comments
 (0)