Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit c8a9c7c

Browse files
author
Jarkko Paso
committed
FHSS: function to calculate nearest (larger or equal) prime number
1 parent 5e70ec1 commit c8a9c7c

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

source/Service_Libs/fhss/channel_functions.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@
4141

4242
static uint32_t global_seed = 1;
4343

44+
45+
uint16_t tr51_calc_nearest_prime_number(uint16_t start_value)
46+
{
47+
if (start_value < 2) {
48+
return 0;
49+
}
50+
uint16_t divider = start_value - 1;
51+
while (start_value) {
52+
if (start_value % divider--) {
53+
if (divider == 1) {
54+
return start_value;
55+
}
56+
} else {
57+
divider = ++start_value - 1;
58+
}
59+
}
60+
return 0;
61+
}
62+
4463
void tr51_seed_rand(uint32_t seed)
4564
{
4665
if (!seed) {

test/nanostack/unittest/service_libs/channel_functions/channelfunctest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ TEST(channel_functions, test_dh1cf_get_bc_channel_index)
5858
{
5959
CHECK(test_dh1cf_get_bc_channel_index());
6060
}
61+
62+
TEST(channel_functions, test_tr51_calc_nearest_prime_number)
63+
{
64+
CHECK(test_tr51_calc_nearest_prime_number());
65+
}

test/nanostack/unittest/service_libs/channel_functions/test_channel_functions.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const int32_t test_channel_table[131] = { 57,78,25,22,97,90,86,102,
3636
56,71,4,29,52,82,120,113,
3737
8,-1,-1 };
3838

39-
const int32_t test_HopSequenceTable [129] = { 4,93,31,84,55,3,116,123,
39+
const int32_t test_HopSequenceTable[129] = { 4,93,31,84,55,3,116,123,
4040
41,68,105,28,25,71,33,66,
4141
122,20,112,125,118,11,69,89,
4242
128,78,56,42,124,30,64,114,
@@ -54,6 +54,23 @@ const int32_t test_HopSequenceTable [129] = { 4,93,31,84,55,3,116,123,
5454
77,48,100,51,14,21,94,23,
5555
22 };
5656

57+
const uint16_t prime_number_table[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
58+
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
59+
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
60+
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
61+
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
62+
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
63+
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
64+
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
65+
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
66+
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
67+
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
68+
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
69+
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
70+
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
71+
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
72+
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
73+
947, 953, 967, 971, 977, 983, 991, 997, 1009 };
5774

5875
bool test_tr51_get_rand()
5976
{
@@ -194,3 +211,21 @@ bool test_dh1cf_get_bc_channel_index()
194211
}
195212
return true;
196213
}
214+
215+
bool test_tr51_calc_nearest_prime_number()
216+
{
217+
uint16_t prime_number;
218+
int j;
219+
for (int i=2; i<1000; i++) {
220+
prime_number = tr51_calc_nearest_prime_number(i);
221+
for (j=0; j<sizeof(prime_number_table); j++) {
222+
if (prime_number == prime_number_table[j]) {
223+
break;
224+
}
225+
}
226+
if (j >= sizeof(prime_number_table)) {
227+
return false;
228+
}
229+
}
230+
return true;
231+
}

test/nanostack/unittest/service_libs/channel_functions/test_channel_functions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ bool test_tr51_compute_cfd();
2929
bool test_tr51_calculate_hopping_sequence();
3030
bool test_dh1cf_get_uc_channel_index();
3131
bool test_dh1cf_get_bc_channel_index();
32+
bool test_tr51_calc_nearest_prime_number();
3233

3334
#ifdef __cplusplus
3435
}

0 commit comments

Comments
 (0)