|
| 1 | +#include "nsconfig.h" |
| 2 | + |
| 3 | +static uint32_t global_seed = 1; |
| 4 | + |
| 5 | +void tr51_seed_rand(uint32_t seed) |
| 6 | +{ |
| 7 | + if (!seed) { |
| 8 | + seed = 1; |
| 9 | + } |
| 10 | + global_seed = seed; |
| 11 | +} |
| 12 | + |
| 13 | +int32_t tr51_get_rand(void) |
| 14 | +{ |
| 15 | + uint32_t random_val = ((global_seed * 1103515245) + 12345) & 0x7fffffff; |
| 16 | + global_seed = random_val; |
| 17 | + return random_val; |
| 18 | +} |
| 19 | + |
| 20 | +void tr51_calculate_channel_table(uint16_t number_of_channels, uint16_t nearest_prime, int32_t *channel_table) |
| 21 | +{ |
| 22 | + int32_t i,j,k; |
| 23 | + tr51_seed_rand(1); |
| 24 | + for (i=0; i < nearest_prime; i++) { |
| 25 | + channel_table[i] = -1; |
| 26 | + } |
| 27 | + for (i=0; i < number_of_channels; i++) { |
| 28 | + j = tr51_get_rand() % number_of_channels; |
| 29 | + k=0; |
| 30 | + while (k <= i) { |
| 31 | + if (j == channel_table[k]) { |
| 32 | + j = tr51_get_rand() % number_of_channels; |
| 33 | + k=0; |
| 34 | + } else { |
| 35 | + k=k+1; |
| 36 | + } |
| 37 | + } |
| 38 | + channel_table[i] = j; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void tr51_compute_cfd(uint8_t *mac, uint8_t *first_element, uint8_t *step_size, uint16_t channel_table_length) |
| 43 | +{ |
| 44 | + *first_element = (mac[5] ^ mac[6] ^ mac[7]) % channel_table_length; |
| 45 | + *step_size = (mac[7] % (channel_table_length - 1)) + 1; |
| 46 | +} |
| 47 | + |
| 48 | +uint8_t tr51_find_excluded(int32_t channel, uint16_t *excluded_channels, uint16_t number_of_excluded_channels) |
| 49 | +{ |
| 50 | + uint8_t count = 0; |
| 51 | + if (excluded_channels != NULL) { |
| 52 | + for (count = 0; count < number_of_excluded_channels; count++) { |
| 53 | + if (channel == excluded_channels[count]) { |
| 54 | + return 1; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +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) |
| 62 | +{ |
| 63 | + uint16_t cntr = channel_table_length; |
| 64 | + uint8_t index = first_element; |
| 65 | + uint8_t slot = 0; |
| 66 | + while (cntr--) { |
| 67 | + if (channel_table[index] != -1) { |
| 68 | + if (!tr51_find_excluded(channel_table[index], excluded_channels, number_of_excluded_channels)) { |
| 69 | + output_table[slot] = channel_table[index]; |
| 70 | + slot++; |
| 71 | + } |
| 72 | + } |
| 73 | + index += step_size; |
| 74 | + while (index >= channel_table_length) { |
| 75 | + index -= channel_table_length; |
| 76 | + } |
| 77 | + } |
| 78 | + return slot; |
| 79 | +} |
0 commit comments