|
| 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 | +} |
0 commit comments