Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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