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