123

Committer:
hudakz
Date:
Tue Sep 03 09:16:55 2019 +0000
Revision:
14:7648334eb41b
UDP socket improved.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 14:7648334eb41b 1 /*
hudakz 14:7648334eb41b 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
hudakz 14:7648334eb41b 3 * SPDX-License-Identifier: Apache-2.0
hudakz 14:7648334eb41b 4 * Licensed under the Apache License, Version 2.0 (the License); you may
hudakz 14:7648334eb41b 5 * not use this file except in compliance with the License.
hudakz 14:7648334eb41b 6 * You may obtain a copy of the License at
hudakz 14:7648334eb41b 7 *
hudakz 14:7648334eb41b 8 * http://www.apache.org/licenses/LICENSE-2.0
hudakz 14:7648334eb41b 9 *
hudakz 14:7648334eb41b 10 * Unless required by applicable law or agreed to in writing, software
hudakz 14:7648334eb41b 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
hudakz 14:7648334eb41b 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hudakz 14:7648334eb41b 13 * See the License for the specific language governing permissions and
hudakz 14:7648334eb41b 14 * limitations under the License.
hudakz 14:7648334eb41b 15 */
hudakz 14:7648334eb41b 16 #ifndef COMMON_FUNCTIONS_H_
hudakz 14:7648334eb41b 17 #define COMMON_FUNCTIONS_H_
hudakz 14:7648334eb41b 18
hudakz 14:7648334eb41b 19 #include "ns_types.h"
hudakz 14:7648334eb41b 20
hudakz 14:7648334eb41b 21 #ifdef __cplusplus
hudakz 14:7648334eb41b 22 extern "C" {
hudakz 14:7648334eb41b 23 #endif
hudakz 14:7648334eb41b 24
hudakz 14:7648334eb41b 25 /*
hudakz 14:7648334eb41b 26 * Common write 64-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 27 *
hudakz 14:7648334eb41b 28 * Write 64 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 29 *
hudakz 14:7648334eb41b 30 * \param value 64-bit variable
hudakz 14:7648334eb41b 31 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 32 *
hudakz 14:7648334eb41b 33 * \return updated pointer
hudakz 14:7648334eb41b 34 */
hudakz 14:7648334eb41b 35 NS_INLINE uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8]);
hudakz 14:7648334eb41b 36
hudakz 14:7648334eb41b 37 /*
hudakz 14:7648334eb41b 38 * Common read 64-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 39 *
hudakz 14:7648334eb41b 40 * Read 64 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 41 *
hudakz 14:7648334eb41b 42 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 43 *
hudakz 14:7648334eb41b 44 * \return 64-bit variable
hudakz 14:7648334eb41b 45 */
hudakz 14:7648334eb41b 46 NS_INLINE uint64_t common_read_64_bit(const uint8_t data_buf[__static 8]);
hudakz 14:7648334eb41b 47
hudakz 14:7648334eb41b 48 /*
hudakz 14:7648334eb41b 49 * Common write 32-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 50 *
hudakz 14:7648334eb41b 51 * Write 32 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 52 *
hudakz 14:7648334eb41b 53 * \param value 32-bit variable
hudakz 14:7648334eb41b 54 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 55 *
hudakz 14:7648334eb41b 56 * \return updated pointer
hudakz 14:7648334eb41b 57 */
hudakz 14:7648334eb41b 58 NS_INLINE uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4]);
hudakz 14:7648334eb41b 59
hudakz 14:7648334eb41b 60 /*
hudakz 14:7648334eb41b 61 * Common read 32-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 62 *
hudakz 14:7648334eb41b 63 * Read 32 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 64 *
hudakz 14:7648334eb41b 65 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 66 *
hudakz 14:7648334eb41b 67 * \return 32-bit variable
hudakz 14:7648334eb41b 68 */
hudakz 14:7648334eb41b 69 NS_INLINE uint32_t common_read_32_bit(const uint8_t data_buf[__static 4]);
hudakz 14:7648334eb41b 70
hudakz 14:7648334eb41b 71 /*
hudakz 14:7648334eb41b 72 * Common write 32-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 73 *
hudakz 14:7648334eb41b 74 * Write 32 bits in little-endian byte order.
hudakz 14:7648334eb41b 75 *
hudakz 14:7648334eb41b 76 * \param value 32-bit variable
hudakz 14:7648334eb41b 77 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 78 *
hudakz 14:7648334eb41b 79 * \return updated pointer
hudakz 14:7648334eb41b 80 */
hudakz 14:7648334eb41b 81 NS_INLINE uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4]);
hudakz 14:7648334eb41b 82
hudakz 14:7648334eb41b 83 /*
hudakz 14:7648334eb41b 84 * Common read 32-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 85 *
hudakz 14:7648334eb41b 86 * Read 32 bits in little-endian byte order.
hudakz 14:7648334eb41b 87 *
hudakz 14:7648334eb41b 88 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 89 *
hudakz 14:7648334eb41b 90 * \return 32-bit variable
hudakz 14:7648334eb41b 91 */
hudakz 14:7648334eb41b 92 NS_INLINE uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4]);
hudakz 14:7648334eb41b 93
hudakz 14:7648334eb41b 94 /*
hudakz 14:7648334eb41b 95 * Common write 24-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 96 *
hudakz 14:7648334eb41b 97 * Write 24 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 98 *
hudakz 14:7648334eb41b 99 * \param value 24-bit variable
hudakz 14:7648334eb41b 100 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 101 *
hudakz 14:7648334eb41b 102 * \return updated pointer
hudakz 14:7648334eb41b 103 */
hudakz 14:7648334eb41b 104 NS_INLINE uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3]);
hudakz 14:7648334eb41b 105
hudakz 14:7648334eb41b 106 /*
hudakz 14:7648334eb41b 107 * Common read 24-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 108 *
hudakz 14:7648334eb41b 109 * Read 24 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 110 *
hudakz 14:7648334eb41b 111 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 112 *
hudakz 14:7648334eb41b 113 * \return 24-bit variable
hudakz 14:7648334eb41b 114 */
hudakz 14:7648334eb41b 115 NS_INLINE uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3]);
hudakz 14:7648334eb41b 116
hudakz 14:7648334eb41b 117 /*
hudakz 14:7648334eb41b 118 * Common write 24-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 119 *
hudakz 14:7648334eb41b 120 * Write 24 bits in little-endian byte order.
hudakz 14:7648334eb41b 121 *
hudakz 14:7648334eb41b 122 * \param value 24-bit variable
hudakz 14:7648334eb41b 123 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 124 *
hudakz 14:7648334eb41b 125 * \return updated pointer
hudakz 14:7648334eb41b 126 */
hudakz 14:7648334eb41b 127 NS_INLINE uint8_t *common_write_24_bit_inverse(uint_fast24_t value, uint8_t ptr[__static 3]);
hudakz 14:7648334eb41b 128
hudakz 14:7648334eb41b 129 /*
hudakz 14:7648334eb41b 130 * Common read 24-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 131 *
hudakz 14:7648334eb41b 132 * Read 24 bits in little-endian byte order.
hudakz 14:7648334eb41b 133 *
hudakz 14:7648334eb41b 134 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 135 *
hudakz 14:7648334eb41b 136 * \return 24-bit variable
hudakz 14:7648334eb41b 137 */
hudakz 14:7648334eb41b 138 NS_INLINE uint_fast24_t common_read_24_bit_inverse(const uint8_t data_buf[__static 3]);
hudakz 14:7648334eb41b 139
hudakz 14:7648334eb41b 140 /*
hudakz 14:7648334eb41b 141 * Common write 16-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 142 *
hudakz 14:7648334eb41b 143 * Write 16 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 144 *
hudakz 14:7648334eb41b 145 * \param value 16-bit variable
hudakz 14:7648334eb41b 146 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 147 *
hudakz 14:7648334eb41b 148 * \return updated pointer
hudakz 14:7648334eb41b 149 */
hudakz 14:7648334eb41b 150 NS_INLINE uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2]);
hudakz 14:7648334eb41b 151
hudakz 14:7648334eb41b 152 /*
hudakz 14:7648334eb41b 153 * Common read 16-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 154 *
hudakz 14:7648334eb41b 155 * Read 16 bits in big-endian (network) byte order.
hudakz 14:7648334eb41b 156 *
hudakz 14:7648334eb41b 157 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 158 *
hudakz 14:7648334eb41b 159 * \return 16-bit variable
hudakz 14:7648334eb41b 160 */
hudakz 14:7648334eb41b 161 NS_INLINE uint16_t common_read_16_bit(const uint8_t data_buf[__static 2]);
hudakz 14:7648334eb41b 162
hudakz 14:7648334eb41b 163 /*
hudakz 14:7648334eb41b 164 * Common write 16-bit variable to 8-bit pointer.
hudakz 14:7648334eb41b 165 *
hudakz 14:7648334eb41b 166 * Write 16 bits in little-endian byte order.
hudakz 14:7648334eb41b 167 *
hudakz 14:7648334eb41b 168 * \param value 16-bit variable
hudakz 14:7648334eb41b 169 * \param ptr pointer where data to be written
hudakz 14:7648334eb41b 170 *
hudakz 14:7648334eb41b 171 * \return updated pointer
hudakz 14:7648334eb41b 172 */
hudakz 14:7648334eb41b 173 NS_INLINE uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2]);
hudakz 14:7648334eb41b 174
hudakz 14:7648334eb41b 175 /*
hudakz 14:7648334eb41b 176 * Common read 16-bit variable from 8-bit pointer.
hudakz 14:7648334eb41b 177 *
hudakz 14:7648334eb41b 178 * Read 16 bits in little-endian byte order.
hudakz 14:7648334eb41b 179 *
hudakz 14:7648334eb41b 180 * \param data_buf pointer where data to be read
hudakz 14:7648334eb41b 181 *
hudakz 14:7648334eb41b 182 * \return 16-bit variable
hudakz 14:7648334eb41b 183 */
hudakz 14:7648334eb41b 184 NS_INLINE uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2]);
hudakz 14:7648334eb41b 185
hudakz 14:7648334eb41b 186 /*
hudakz 14:7648334eb41b 187 * Count bits in a byte
hudakz 14:7648334eb41b 188 *
hudakz 14:7648334eb41b 189 * \param value byte to inspect
hudakz 14:7648334eb41b 190 *
hudakz 14:7648334eb41b 191 * \return number of 1-bits in byte
hudakz 14:7648334eb41b 192 */
hudakz 14:7648334eb41b 193 NS_INLINE uint_fast8_t common_count_bits(uint8_t value);
hudakz 14:7648334eb41b 194
hudakz 14:7648334eb41b 195 /*
hudakz 14:7648334eb41b 196 * Count leading zeros in a byte
hudakz 14:7648334eb41b 197 *
hudakz 14:7648334eb41b 198 * \deprecated Use common_count_leading_zeros_8
hudakz 14:7648334eb41b 199 *
hudakz 14:7648334eb41b 200 * \param value byte to inspect
hudakz 14:7648334eb41b 201 *
hudakz 14:7648334eb41b 202 * \return number of leading zeros in byte (0-8)
hudakz 14:7648334eb41b 203 */
hudakz 14:7648334eb41b 204 NS_INLINE uint_fast8_t common_count_leading_zeros(uint8_t value);
hudakz 14:7648334eb41b 205
hudakz 14:7648334eb41b 206 /*
hudakz 14:7648334eb41b 207 * Count leading zeros in a byte
hudakz 14:7648334eb41b 208 *
hudakz 14:7648334eb41b 209 * \param value byte to inspect
hudakz 14:7648334eb41b 210 *
hudakz 14:7648334eb41b 211 * \return number of leading zeros in byte (0-8)
hudakz 14:7648334eb41b 212 */
hudakz 14:7648334eb41b 213 NS_INLINE uint_fast8_t common_count_leading_zeros_8(uint8_t value);
hudakz 14:7648334eb41b 214
hudakz 14:7648334eb41b 215 /*
hudakz 14:7648334eb41b 216 * Count leading zeros in a 16-bit value
hudakz 14:7648334eb41b 217 *
hudakz 14:7648334eb41b 218 * \param value value to inspect
hudakz 14:7648334eb41b 219 *
hudakz 14:7648334eb41b 220 * \return number of leading zeros in byte (0-16)
hudakz 14:7648334eb41b 221 */
hudakz 14:7648334eb41b 222 NS_INLINE uint_fast8_t common_count_leading_zeros_16(uint16_t value);
hudakz 14:7648334eb41b 223
hudakz 14:7648334eb41b 224 /*
hudakz 14:7648334eb41b 225 * Count leading zeros in a 32-bit value
hudakz 14:7648334eb41b 226 *
hudakz 14:7648334eb41b 227 * \param value value to inspect
hudakz 14:7648334eb41b 228 *
hudakz 14:7648334eb41b 229 * \return number of leading zeros in byte (0-32)
hudakz 14:7648334eb41b 230 */
hudakz 14:7648334eb41b 231 NS_INLINE uint_fast8_t common_count_leading_zeros_32(uint32_t value);
hudakz 14:7648334eb41b 232
hudakz 14:7648334eb41b 233 /*
hudakz 14:7648334eb41b 234 * Compare 8-bit serial numbers
hudakz 14:7648334eb41b 235 *
hudakz 14:7648334eb41b 236 * Compare two 8-bit serial numbers, according to RFC 1982 Serial Number
hudakz 14:7648334eb41b 237 * Arithmetic.
hudakz 14:7648334eb41b 238 *
hudakz 14:7648334eb41b 239 * \param s1 first serial number
hudakz 14:7648334eb41b 240 * \param s2 second serial number
hudakz 14:7648334eb41b 241 *
hudakz 14:7648334eb41b 242 * \return true if s1 > s2
hudakz 14:7648334eb41b 243 * \return false if s1 <= s2, or the comparison is undefined
hudakz 14:7648334eb41b 244 */
hudakz 14:7648334eb41b 245 NS_INLINE bool common_serial_number_greater_8(uint8_t s1, uint8_t s2);
hudakz 14:7648334eb41b 246
hudakz 14:7648334eb41b 247 /*
hudakz 14:7648334eb41b 248 * Compare 16-bit serial numbers
hudakz 14:7648334eb41b 249 *
hudakz 14:7648334eb41b 250 * Compare two 16-bit serial numbers, according to RFC 1982 Serial Number
hudakz 14:7648334eb41b 251 * Arithmetic.
hudakz 14:7648334eb41b 252 *
hudakz 14:7648334eb41b 253 * \param s1 first serial number
hudakz 14:7648334eb41b 254 * \param s2 second serial number
hudakz 14:7648334eb41b 255 *
hudakz 14:7648334eb41b 256 * \return true if s1 > s2
hudakz 14:7648334eb41b 257 * \return false if s1 <= s2, or the comparison is undefined
hudakz 14:7648334eb41b 258 */
hudakz 14:7648334eb41b 259 NS_INLINE bool common_serial_number_greater_16(uint16_t s1, uint16_t s2);
hudakz 14:7648334eb41b 260
hudakz 14:7648334eb41b 261 /*
hudakz 14:7648334eb41b 262 * Compare 32-bit serial numbers
hudakz 14:7648334eb41b 263 *
hudakz 14:7648334eb41b 264 * Compare two 32-bit serial numbers, according to RFC 1982 Serial Number
hudakz 14:7648334eb41b 265 * Arithmetic.
hudakz 14:7648334eb41b 266 *
hudakz 14:7648334eb41b 267 * \param s1 first serial number
hudakz 14:7648334eb41b 268 * \param s2 second serial number
hudakz 14:7648334eb41b 269 *
hudakz 14:7648334eb41b 270 * \return true if s1 > s2
hudakz 14:7648334eb41b 271 * \return false if s1 <= s2, or the comparison is undefined
hudakz 14:7648334eb41b 272 */
hudakz 14:7648334eb41b 273 NS_INLINE bool common_serial_number_greater_32(uint32_t s1, uint32_t s2);
hudakz 14:7648334eb41b 274
hudakz 14:7648334eb41b 275 /*
hudakz 14:7648334eb41b 276 * Test a bit in an bit array.
hudakz 14:7648334eb41b 277 *
hudakz 14:7648334eb41b 278 * Check whether a particular bit is set in a bit string. The bit array
hudakz 14:7648334eb41b 279 * is in big-endian (network) bit order.
hudakz 14:7648334eb41b 280 *
hudakz 14:7648334eb41b 281 * \param bitset pointer to bit array
hudakz 14:7648334eb41b 282 * \param bit index of bit - 0 is the most significant bit of the first byte
hudakz 14:7648334eb41b 283 *
hudakz 14:7648334eb41b 284 * \return true if the bit is set
hudakz 14:7648334eb41b 285 */
hudakz 14:7648334eb41b 286 NS_INLINE bool bit_test(const uint8_t *bitset, uint_fast8_t bit);
hudakz 14:7648334eb41b 287
hudakz 14:7648334eb41b 288 /*
hudakz 14:7648334eb41b 289 * Set a bit in an bit array.
hudakz 14:7648334eb41b 290 *
hudakz 14:7648334eb41b 291 * Set a bit in a bit array. The array is in big-endian (network) bit order.
hudakz 14:7648334eb41b 292 *
hudakz 14:7648334eb41b 293 * \param bitset pointer to bit array
hudakz 14:7648334eb41b 294 * \param bit index of bit - 0 is the most significant bit of the first byte
hudakz 14:7648334eb41b 295 */
hudakz 14:7648334eb41b 296 NS_INLINE void bit_set(uint8_t *bitset, uint_fast8_t bit);
hudakz 14:7648334eb41b 297
hudakz 14:7648334eb41b 298 /*
hudakz 14:7648334eb41b 299 * Clear a bit in an bit array.
hudakz 14:7648334eb41b 300 *
hudakz 14:7648334eb41b 301 * Clear a bit in a bit array. The bit array is in big-endian (network) bit order.
hudakz 14:7648334eb41b 302 *
hudakz 14:7648334eb41b 303 * \param bitset pointer to bit array
hudakz 14:7648334eb41b 304 * \param bit index of bit - 0 is the most significant bit of the first byte
hudakz 14:7648334eb41b 305 */
hudakz 14:7648334eb41b 306 NS_INLINE void bit_clear(uint8_t *bitset, uint_fast8_t bit);
hudakz 14:7648334eb41b 307
hudakz 14:7648334eb41b 308 /*
hudakz 14:7648334eb41b 309 * Compare two bitstrings.
hudakz 14:7648334eb41b 310 *
hudakz 14:7648334eb41b 311 * Compare two bitstrings of specified length. The bit strings are in
hudakz 14:7648334eb41b 312 * big-endian (network) bit order.
hudakz 14:7648334eb41b 313 *
hudakz 14:7648334eb41b 314 * \param a pointer to first string
hudakz 14:7648334eb41b 315 * \param b pointer to second string
hudakz 14:7648334eb41b 316 * \param bits number of bits to compare
hudakz 14:7648334eb41b 317 *
hudakz 14:7648334eb41b 318 * \return true if the strings compare equal
hudakz 14:7648334eb41b 319 */
hudakz 14:7648334eb41b 320 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits);
hudakz 14:7648334eb41b 321
hudakz 14:7648334eb41b 322 /*
hudakz 14:7648334eb41b 323 * Copy a bitstring
hudakz 14:7648334eb41b 324 *
hudakz 14:7648334eb41b 325 * Copy a bitstring of specified length. The bit string is in big-endian
hudakz 14:7648334eb41b 326 * (network) bit order. Bits beyond the bitlength at the destination are not
hudakz 14:7648334eb41b 327 * modified.
hudakz 14:7648334eb41b 328 *
hudakz 14:7648334eb41b 329 * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0],
hudakz 14:7648334eb41b 330 * the lower 4 bits of dst[0] are unmodified.
hudakz 14:7648334eb41b 331 *
hudakz 14:7648334eb41b 332 * \param dst destination pointer
hudakz 14:7648334eb41b 333 * \param src source pointer
hudakz 14:7648334eb41b 334 * \param bits number of bits to copy
hudakz 14:7648334eb41b 335 *
hudakz 14:7648334eb41b 336 * \return the value of dst
hudakz 14:7648334eb41b 337 */
hudakz 14:7648334eb41b 338 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits);
hudakz 14:7648334eb41b 339
hudakz 14:7648334eb41b 340 /*
hudakz 14:7648334eb41b 341 * Copy a bitstring and pad last byte with zeros
hudakz 14:7648334eb41b 342 *
hudakz 14:7648334eb41b 343 * Copy a bitstring of specified length. The bit string is in big-endian
hudakz 14:7648334eb41b 344 * (network) bit order. Bits beyond the bitlength in the last destination byte are
hudakz 14:7648334eb41b 345 * zeroed.
hudakz 14:7648334eb41b 346 *
hudakz 14:7648334eb41b 347 * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0], and
hudakz 14:7648334eb41b 348 * the lower 4 bits of dst[0] are set to 0.
hudakz 14:7648334eb41b 349 *
hudakz 14:7648334eb41b 350 * \param dst destination pointer
hudakz 14:7648334eb41b 351 * \param src source pointer
hudakz 14:7648334eb41b 352 * \param bits number of bits to copy
hudakz 14:7648334eb41b 353 *
hudakz 14:7648334eb41b 354 * \return the value of dst
hudakz 14:7648334eb41b 355 */
hudakz 14:7648334eb41b 356 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits);
hudakz 14:7648334eb41b 357
hudakz 14:7648334eb41b 358 /* Provide definitions, either for inlining, or for common_functions.c */
hudakz 14:7648334eb41b 359 #if defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN
hudakz 14:7648334eb41b 360 #ifndef COMMON_FUNCTIONS_FN
hudakz 14:7648334eb41b 361 #define COMMON_FUNCTIONS_FN NS_INLINE
hudakz 14:7648334eb41b 362 #endif
hudakz 14:7648334eb41b 363
hudakz 14:7648334eb41b 364 COMMON_FUNCTIONS_FN uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8])
hudakz 14:7648334eb41b 365 {
hudakz 14:7648334eb41b 366 *ptr++ = value >> 56;
hudakz 14:7648334eb41b 367 *ptr++ = value >> 48;
hudakz 14:7648334eb41b 368 *ptr++ = value >> 40;
hudakz 14:7648334eb41b 369 *ptr++ = value >> 32;
hudakz 14:7648334eb41b 370 *ptr++ = value >> 24;
hudakz 14:7648334eb41b 371 *ptr++ = value >> 16;
hudakz 14:7648334eb41b 372 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 373 *ptr++ = value;
hudakz 14:7648334eb41b 374 return ptr;
hudakz 14:7648334eb41b 375 }
hudakz 14:7648334eb41b 376
hudakz 14:7648334eb41b 377 COMMON_FUNCTIONS_FN uint64_t common_read_64_bit(const uint8_t data_buf[__static 8])
hudakz 14:7648334eb41b 378 {
hudakz 14:7648334eb41b 379 uint64_t temp_64;
hudakz 14:7648334eb41b 380 temp_64 = (uint64_t)(*data_buf++) << 56;
hudakz 14:7648334eb41b 381 temp_64 += (uint64_t)(*data_buf++) << 48;
hudakz 14:7648334eb41b 382 temp_64 += (uint64_t)(*data_buf++) << 40;
hudakz 14:7648334eb41b 383 temp_64 += (uint64_t)(*data_buf++) << 32;
hudakz 14:7648334eb41b 384 temp_64 += (uint64_t)(*data_buf++) << 24;
hudakz 14:7648334eb41b 385 temp_64 += (uint64_t)(*data_buf++) << 16;
hudakz 14:7648334eb41b 386 temp_64 += (uint64_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 387 temp_64 += *data_buf++;
hudakz 14:7648334eb41b 388 return temp_64;
hudakz 14:7648334eb41b 389 }
hudakz 14:7648334eb41b 390
hudakz 14:7648334eb41b 391 COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4])
hudakz 14:7648334eb41b 392 {
hudakz 14:7648334eb41b 393 *ptr++ = value >> 24;
hudakz 14:7648334eb41b 394 *ptr++ = value >> 16;
hudakz 14:7648334eb41b 395 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 396 *ptr++ = value;
hudakz 14:7648334eb41b 397 return ptr;
hudakz 14:7648334eb41b 398 }
hudakz 14:7648334eb41b 399
hudakz 14:7648334eb41b 400 COMMON_FUNCTIONS_FN uint32_t common_read_32_bit(const uint8_t data_buf[__static 4])
hudakz 14:7648334eb41b 401 {
hudakz 14:7648334eb41b 402 uint32_t temp_32;
hudakz 14:7648334eb41b 403 temp_32 = (uint32_t)(*data_buf++) << 24;
hudakz 14:7648334eb41b 404 temp_32 += (uint32_t)(*data_buf++) << 16;
hudakz 14:7648334eb41b 405 temp_32 += (uint32_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 406 temp_32 += *data_buf++;
hudakz 14:7648334eb41b 407 return temp_32;
hudakz 14:7648334eb41b 408 }
hudakz 14:7648334eb41b 409
hudakz 14:7648334eb41b 410 COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4])
hudakz 14:7648334eb41b 411 {
hudakz 14:7648334eb41b 412 *ptr++ = value;
hudakz 14:7648334eb41b 413 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 414 *ptr++ = value >> 16;
hudakz 14:7648334eb41b 415 *ptr++ = value >> 24;
hudakz 14:7648334eb41b 416 return ptr;
hudakz 14:7648334eb41b 417 }
hudakz 14:7648334eb41b 418
hudakz 14:7648334eb41b 419 COMMON_FUNCTIONS_FN uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4])
hudakz 14:7648334eb41b 420 {
hudakz 14:7648334eb41b 421 uint32_t temp_32;
hudakz 14:7648334eb41b 422 temp_32 = *data_buf++;
hudakz 14:7648334eb41b 423 temp_32 += (uint32_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 424 temp_32 += (uint32_t)(*data_buf++) << 16;
hudakz 14:7648334eb41b 425 temp_32 += (uint32_t)(*data_buf++) << 24;
hudakz 14:7648334eb41b 426 return temp_32;
hudakz 14:7648334eb41b 427 }
hudakz 14:7648334eb41b 428
hudakz 14:7648334eb41b 429 COMMON_FUNCTIONS_FN uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3])
hudakz 14:7648334eb41b 430 {
hudakz 14:7648334eb41b 431 *ptr++ = value >> 16;
hudakz 14:7648334eb41b 432 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 433 *ptr++ = value;
hudakz 14:7648334eb41b 434 return ptr;
hudakz 14:7648334eb41b 435 }
hudakz 14:7648334eb41b 436
hudakz 14:7648334eb41b 437 COMMON_FUNCTIONS_FN uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3])
hudakz 14:7648334eb41b 438 {
hudakz 14:7648334eb41b 439 uint_fast24_t temp_24;
hudakz 14:7648334eb41b 440 temp_24 = (uint_fast24_t)(*data_buf++) << 16;
hudakz 14:7648334eb41b 441 temp_24 += (uint_fast24_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 442 temp_24 += *data_buf++;
hudakz 14:7648334eb41b 443 return temp_24;
hudakz 14:7648334eb41b 444 }
hudakz 14:7648334eb41b 445
hudakz 14:7648334eb41b 446 COMMON_FUNCTIONS_FN uint8_t *common_write_24_bit_inverse(uint_fast24_t value, uint8_t ptr[__static 3])
hudakz 14:7648334eb41b 447 {
hudakz 14:7648334eb41b 448 *ptr++ = value;
hudakz 14:7648334eb41b 449 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 450 *ptr++ = value >> 16;
hudakz 14:7648334eb41b 451 return ptr;
hudakz 14:7648334eb41b 452 }
hudakz 14:7648334eb41b 453
hudakz 14:7648334eb41b 454 COMMON_FUNCTIONS_FN uint_fast24_t common_read_24_bit_inverse(const uint8_t data_buf[__static 3])
hudakz 14:7648334eb41b 455 {
hudakz 14:7648334eb41b 456 uint_fast24_t temp_24;
hudakz 14:7648334eb41b 457 temp_24 = *data_buf++;
hudakz 14:7648334eb41b 458 temp_24 += (uint_fast24_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 459 temp_24 += (uint_fast24_t)(*data_buf++) << 16;
hudakz 14:7648334eb41b 460 return temp_24;
hudakz 14:7648334eb41b 461 }
hudakz 14:7648334eb41b 462
hudakz 14:7648334eb41b 463 COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2])
hudakz 14:7648334eb41b 464 {
hudakz 14:7648334eb41b 465 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 466 *ptr++ = value;
hudakz 14:7648334eb41b 467 return ptr;
hudakz 14:7648334eb41b 468 }
hudakz 14:7648334eb41b 469
hudakz 14:7648334eb41b 470 COMMON_FUNCTIONS_FN uint16_t common_read_16_bit(const uint8_t data_buf[__static 2])
hudakz 14:7648334eb41b 471 {
hudakz 14:7648334eb41b 472 uint16_t temp_16;
hudakz 14:7648334eb41b 473 temp_16 = (uint16_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 474 temp_16 += *data_buf++;
hudakz 14:7648334eb41b 475 return temp_16;
hudakz 14:7648334eb41b 476 }
hudakz 14:7648334eb41b 477
hudakz 14:7648334eb41b 478 COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2])
hudakz 14:7648334eb41b 479 {
hudakz 14:7648334eb41b 480 *ptr++ = value;
hudakz 14:7648334eb41b 481 *ptr++ = value >> 8;
hudakz 14:7648334eb41b 482 return ptr;
hudakz 14:7648334eb41b 483 }
hudakz 14:7648334eb41b 484
hudakz 14:7648334eb41b 485 COMMON_FUNCTIONS_FN uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2])
hudakz 14:7648334eb41b 486 {
hudakz 14:7648334eb41b 487 uint16_t temp_16;
hudakz 14:7648334eb41b 488 temp_16 = *data_buf++;
hudakz 14:7648334eb41b 489 temp_16 += (uint16_t)(*data_buf++) << 8;
hudakz 14:7648334eb41b 490 return temp_16;
hudakz 14:7648334eb41b 491 }
hudakz 14:7648334eb41b 492
hudakz 14:7648334eb41b 493 COMMON_FUNCTIONS_FN uint_fast8_t common_count_bits(uint8_t value)
hudakz 14:7648334eb41b 494 {
hudakz 14:7648334eb41b 495 /* First step sets each bit pair to be count of bits (00,01,10) */
hudakz 14:7648334eb41b 496 /* [00-00 = 00, 01-00 = 01, 10-01 = 01, 11-01 = 10] */
hudakz 14:7648334eb41b 497 uint_fast8_t count = value - ((value >> 1) & 0x55);
hudakz 14:7648334eb41b 498 /* Add bit pairs to make each nibble contain count of bits (0-4) */
hudakz 14:7648334eb41b 499 count = (count & 0x33) + ((count >> 2) & 0x33);
hudakz 14:7648334eb41b 500 /* Final result is sum of nibbles (0-8) */
hudakz 14:7648334eb41b 501 count = (count >> 4) + (count & 0x0F);
hudakz 14:7648334eb41b 502 return count;
hudakz 14:7648334eb41b 503 }
hudakz 14:7648334eb41b 504
hudakz 14:7648334eb41b 505 COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros(uint8_t value)
hudakz 14:7648334eb41b 506 {
hudakz 14:7648334eb41b 507 return common_count_leading_zeros_8(value);
hudakz 14:7648334eb41b 508 }
hudakz 14:7648334eb41b 509
hudakz 14:7648334eb41b 510 COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_8(uint8_t value)
hudakz 14:7648334eb41b 511 {
hudakz 14:7648334eb41b 512 #ifdef __CC_ARM
hudakz 14:7648334eb41b 513 return value ? __clz((unsigned int) value << 24) : 8;
hudakz 14:7648334eb41b 514 #elif defined __GNUC__
hudakz 14:7648334eb41b 515 return value ? __builtin_clz((unsigned int) value << 24) : 8;
hudakz 14:7648334eb41b 516 #else
hudakz 14:7648334eb41b 517 uint_fast8_t cnt = 0;
hudakz 14:7648334eb41b 518 if (value == 0) {
hudakz 14:7648334eb41b 519 return 8;
hudakz 14:7648334eb41b 520 }
hudakz 14:7648334eb41b 521 if ((value & 0xF0) == 0) {
hudakz 14:7648334eb41b 522 value <<= 4;
hudakz 14:7648334eb41b 523 cnt += 4;
hudakz 14:7648334eb41b 524 }
hudakz 14:7648334eb41b 525 if ((value & 0xC0) == 0) {
hudakz 14:7648334eb41b 526 value <<= 2;
hudakz 14:7648334eb41b 527 cnt += 2;
hudakz 14:7648334eb41b 528 }
hudakz 14:7648334eb41b 529 if ((value & 0x80) == 0) {
hudakz 14:7648334eb41b 530 cnt += 1;
hudakz 14:7648334eb41b 531 }
hudakz 14:7648334eb41b 532
hudakz 14:7648334eb41b 533 return cnt;
hudakz 14:7648334eb41b 534 #endif
hudakz 14:7648334eb41b 535 }
hudakz 14:7648334eb41b 536
hudakz 14:7648334eb41b 537 COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_16(uint16_t value)
hudakz 14:7648334eb41b 538 {
hudakz 14:7648334eb41b 539 #ifdef __CC_ARM
hudakz 14:7648334eb41b 540 return value ? __clz((unsigned int) value << 16) : 16;
hudakz 14:7648334eb41b 541 #elif defined __GNUC__
hudakz 14:7648334eb41b 542 return value ? __builtin_clz((unsigned int) value << 16) : 16;
hudakz 14:7648334eb41b 543 #else
hudakz 14:7648334eb41b 544 uint_fast8_t cnt = 0;
hudakz 14:7648334eb41b 545 if (value == 0) {
hudakz 14:7648334eb41b 546 return 16;
hudakz 14:7648334eb41b 547 }
hudakz 14:7648334eb41b 548 if ((value & 0xFF00) == 0) {
hudakz 14:7648334eb41b 549 value <<= 8;
hudakz 14:7648334eb41b 550 cnt += 8;
hudakz 14:7648334eb41b 551 }
hudakz 14:7648334eb41b 552 if ((value & 0xF000) == 0) {
hudakz 14:7648334eb41b 553 value <<= 4;
hudakz 14:7648334eb41b 554 cnt += 4;
hudakz 14:7648334eb41b 555 }
hudakz 14:7648334eb41b 556 if ((value & 0xC000) == 0) {
hudakz 14:7648334eb41b 557 value <<= 2;
hudakz 14:7648334eb41b 558 cnt += 2;
hudakz 14:7648334eb41b 559 }
hudakz 14:7648334eb41b 560 if ((value & 0x8000) == 0) {
hudakz 14:7648334eb41b 561 cnt += 1;
hudakz 14:7648334eb41b 562 }
hudakz 14:7648334eb41b 563
hudakz 14:7648334eb41b 564 return cnt;
hudakz 14:7648334eb41b 565 #endif
hudakz 14:7648334eb41b 566 }
hudakz 14:7648334eb41b 567
hudakz 14:7648334eb41b 568 COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_32(uint32_t value)
hudakz 14:7648334eb41b 569 {
hudakz 14:7648334eb41b 570 #ifdef __CC_ARM
hudakz 14:7648334eb41b 571 return __clz(value);
hudakz 14:7648334eb41b 572 #elif defined __GNUC__
hudakz 14:7648334eb41b 573 return value ? __builtin_clz(value) : 32;
hudakz 14:7648334eb41b 574 #else
hudakz 14:7648334eb41b 575 uint_fast8_t cnt = 0;
hudakz 14:7648334eb41b 576 if (value == 0) {
hudakz 14:7648334eb41b 577 return 32;
hudakz 14:7648334eb41b 578 }
hudakz 14:7648334eb41b 579 if ((value & 0xFFFF0000) == 0) {
hudakz 14:7648334eb41b 580 value <<= 16;
hudakz 14:7648334eb41b 581 cnt += 16;
hudakz 14:7648334eb41b 582 }
hudakz 14:7648334eb41b 583 if ((value & 0xFF000000) == 0) {
hudakz 14:7648334eb41b 584 value <<= 8;
hudakz 14:7648334eb41b 585 cnt += 8;
hudakz 14:7648334eb41b 586 }
hudakz 14:7648334eb41b 587 if ((value & 0xF0000000) == 0) {
hudakz 14:7648334eb41b 588 value <<= 4;
hudakz 14:7648334eb41b 589 cnt += 4;
hudakz 14:7648334eb41b 590 }
hudakz 14:7648334eb41b 591 if ((value & 0xC0000000) == 0) {
hudakz 14:7648334eb41b 592 value <<= 2;
hudakz 14:7648334eb41b 593 cnt += 2;
hudakz 14:7648334eb41b 594 }
hudakz 14:7648334eb41b 595 if ((value & 0x80000000) == 0) {
hudakz 14:7648334eb41b 596 cnt += 1;
hudakz 14:7648334eb41b 597 }
hudakz 14:7648334eb41b 598
hudakz 14:7648334eb41b 599 return cnt;
hudakz 14:7648334eb41b 600 #endif
hudakz 14:7648334eb41b 601 }
hudakz 14:7648334eb41b 602
hudakz 14:7648334eb41b 603 COMMON_FUNCTIONS_FN bool common_serial_number_greater_8(uint8_t s1, uint8_t s2)
hudakz 14:7648334eb41b 604 {
hudakz 14:7648334eb41b 605 return (s1 > s2 && s1 - s2 < UINT8_C(0x80)) || (s1 < s2 && s2 - s1 > UINT8_C(0x80));
hudakz 14:7648334eb41b 606 }
hudakz 14:7648334eb41b 607
hudakz 14:7648334eb41b 608 COMMON_FUNCTIONS_FN bool common_serial_number_greater_16(uint16_t s1, uint16_t s2)
hudakz 14:7648334eb41b 609 {
hudakz 14:7648334eb41b 610 return (s1 > s2 && s1 - s2 < UINT16_C(0x8000)) || (s1 < s2 && s2 - s1 > UINT16_C(0x8000));
hudakz 14:7648334eb41b 611 }
hudakz 14:7648334eb41b 612
hudakz 14:7648334eb41b 613 COMMON_FUNCTIONS_FN bool common_serial_number_greater_32(uint32_t s1, uint32_t s2)
hudakz 14:7648334eb41b 614 {
hudakz 14:7648334eb41b 615 return (s1 > s2 && s1 - s2 < UINT32_C(0x80000000)) || (s1 < s2 && s2 - s1 > UINT32_C(0x80000000));
hudakz 14:7648334eb41b 616 }
hudakz 14:7648334eb41b 617
hudakz 14:7648334eb41b 618 COMMON_FUNCTIONS_FN bool bit_test(const uint8_t *bitset, uint_fast8_t bit)
hudakz 14:7648334eb41b 619 {
hudakz 14:7648334eb41b 620 return bitset[bit >> 3] & (0x80 >> (bit & 7));
hudakz 14:7648334eb41b 621 }
hudakz 14:7648334eb41b 622
hudakz 14:7648334eb41b 623 COMMON_FUNCTIONS_FN void bit_set(uint8_t *bitset, uint_fast8_t bit)
hudakz 14:7648334eb41b 624 {
hudakz 14:7648334eb41b 625 bitset[bit >> 3] |= (0x80 >> (bit & 7));
hudakz 14:7648334eb41b 626 }
hudakz 14:7648334eb41b 627
hudakz 14:7648334eb41b 628 COMMON_FUNCTIONS_FN void bit_clear(uint8_t *bitset, uint_fast8_t bit)
hudakz 14:7648334eb41b 629 {
hudakz 14:7648334eb41b 630 bitset[bit >> 3] &= ~(0x80 >> (bit & 7));
hudakz 14:7648334eb41b 631 }
hudakz 14:7648334eb41b 632
hudakz 14:7648334eb41b 633 #endif /* defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN */
hudakz 14:7648334eb41b 634
hudakz 14:7648334eb41b 635 #ifdef __cplusplus
hudakz 14:7648334eb41b 636 }
hudakz 14:7648334eb41b 637 #endif
hudakz 14:7648334eb41b 638 #endif /*__COMMON_FUNCTIONS_H_*/