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