Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | /** @file * @brief Byte order helpers. */ /* * Copyright (c) 2015-2016, Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ #ifndef ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ #define ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ #include <zephyr/types.h> #include <stddef.h> #include <zephyr/sys/__assert.h> #include <zephyr/toolchain.h> /* Internal helpers only used by the sys_* APIs further below */ #define __bswap_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) #define __bswap_24(x) ((uint32_t) ((((x) >> 16) & 0xff) | \ (((x)) & 0xff00) | \ (((x) & 0xff) << 16))) #define __bswap_32(x) ((uint32_t) ((((x) >> 24) & 0xff) | \ (((x) >> 8) & 0xff00) | \ (((x) & 0xff00) << 8) | \ (((x) & 0xff) << 24))) #define __bswap_48(x) ((uint64_t) ((((x) >> 40) & 0xff) | \ (((x) >> 24) & 0xff00) | \ (((x) >> 8) & 0xff0000) | \ (((x) & 0xff0000) << 8) | \ (((x) & 0xff00) << 24) | \ (((x) & 0xff) << 40))) #define __bswap_64(x) ((uint64_t) ((((x) >> 56) & 0xff) | \ (((x) >> 40) & 0xff00) | \ (((x) >> 24) & 0xff0000) | \ (((x) >> 8) & 0xff000000) | \ (((x) & 0xff000000) << 8) | \ (((x) & 0xff0000) << 24) | \ (((x) & 0xff00) << 40) | \ (((x) & 0xff) << 56))) /** @def sys_le16_to_cpu * @brief Convert 16-bit integer from little-endian to host endianness. * * @param val 16-bit integer in little-endian format. * * @return 16-bit integer in host endianness. */ /** @def sys_cpu_to_le16 * @brief Convert 16-bit integer from host endianness to little-endian. * * @param val 16-bit integer in host endianness. * * @return 16-bit integer in little-endian format. */ /** @def sys_le24_to_cpu * @brief Convert 24-bit integer from little-endian to host endianness. * * @param val 24-bit integer in little-endian format. * * @return 24-bit integer in host endianness. */ /** @def sys_cpu_to_le24 * @brief Convert 24-bit integer from host endianness to little-endian. * * @param val 24-bit integer in host endianness. * * @return 24-bit integer in little-endian format. */ /** @def sys_le32_to_cpu * @brief Convert 32-bit integer from little-endian to host endianness. * * @param val 32-bit integer in little-endian format. * * @return 32-bit integer in host endianness. */ /** @def sys_cpu_to_le32 * @brief Convert 32-bit integer from host endianness to little-endian. * * @param val 32-bit integer in host endianness. * * @return 32-bit integer in little-endian format. */ /** @def sys_le48_to_cpu * @brief Convert 48-bit integer from little-endian to host endianness. * * @param val 48-bit integer in little-endian format. * * @return 48-bit integer in host endianness. */ /** @def sys_cpu_to_le48 * @brief Convert 48-bit integer from host endianness to little-endian. * * @param val 48-bit integer in host endianness. * * @return 48-bit integer in little-endian format. */ /** @def sys_be16_to_cpu * @brief Convert 16-bit integer from big-endian to host endianness. * * @param val 16-bit integer in big-endian format. * * @return 16-bit integer in host endianness. */ /** @def sys_cpu_to_be16 * @brief Convert 16-bit integer from host endianness to big-endian. * * @param val 16-bit integer in host endianness. * * @return 16-bit integer in big-endian format. */ /** @def sys_be24_to_cpu * @brief Convert 24-bit integer from big-endian to host endianness. * * @param val 24-bit integer in big-endian format. * * @return 24-bit integer in host endianness. */ /** @def sys_cpu_to_be24 * @brief Convert 24-bit integer from host endianness to big-endian. * * @param val 24-bit integer in host endianness. * * @return 24-bit integer in big-endian format. */ /** @def sys_be32_to_cpu * @brief Convert 32-bit integer from big-endian to host endianness. * * @param val 32-bit integer in big-endian format. * * @return 32-bit integer in host endianness. */ /** @def sys_cpu_to_be32 * @brief Convert 32-bit integer from host endianness to big-endian. * * @param val 32-bit integer in host endianness. * * @return 32-bit integer in big-endian format. */ /** @def sys_be48_to_cpu * @brief Convert 48-bit integer from big-endian to host endianness. * * @param val 48-bit integer in big-endian format. * * @return 48-bit integer in host endianness. */ /** @def sys_cpu_to_be48 * @brief Convert 48-bit integer from host endianness to big-endian. * * @param val 48-bit integer in host endianness. * * @return 48-bit integer in big-endian format. */ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #define sys_le16_to_cpu(val) (val) #define sys_cpu_to_le16(val) (val) #define sys_le24_to_cpu(val) (val) #define sys_cpu_to_le24(val) (val) #define sys_le32_to_cpu(val) (val) #define sys_cpu_to_le32(val) (val) #define sys_le48_to_cpu(val) (val) #define sys_cpu_to_le48(val) (val) #define sys_le64_to_cpu(val) (val) #define sys_cpu_to_le64(val) (val) #define sys_be16_to_cpu(val) __bswap_16(val) #define sys_cpu_to_be16(val) __bswap_16(val) #define sys_be24_to_cpu(val) __bswap_24(val) #define sys_cpu_to_be24(val) __bswap_24(val) #define sys_be32_to_cpu(val) __bswap_32(val) #define sys_cpu_to_be32(val) __bswap_32(val) #define sys_be48_to_cpu(val) __bswap_48(val) #define sys_cpu_to_be48(val) __bswap_48(val) #define sys_be64_to_cpu(val) __bswap_64(val) #define sys_cpu_to_be64(val) __bswap_64(val) #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define sys_le16_to_cpu(val) __bswap_16(val) #define sys_cpu_to_le16(val) __bswap_16(val) #define sys_le24_to_cpu(val) __bswap_24(val) #define sys_cpu_to_le24(val) __bswap_24(val) #define sys_le32_to_cpu(val) __bswap_32(val) #define sys_cpu_to_le32(val) __bswap_32(val) #define sys_le48_to_cpu(val) __bswap_48(val) #define sys_cpu_to_le48(val) __bswap_48(val) #define sys_le64_to_cpu(val) __bswap_64(val) #define sys_cpu_to_le64(val) __bswap_64(val) #define sys_be16_to_cpu(val) (val) #define sys_cpu_to_be16(val) (val) #define sys_be24_to_cpu(val) (val) #define sys_cpu_to_be24(val) (val) #define sys_be32_to_cpu(val) (val) #define sys_cpu_to_be32(val) (val) #define sys_be48_to_cpu(val) (val) #define sys_cpu_to_be48(val) (val) #define sys_be64_to_cpu(val) (val) #define sys_cpu_to_be64(val) (val) #else #error "Unknown byte order" #endif /** * @brief Put a 16-bit integer as big-endian to arbitrary location. * * Put a 16-bit integer, originally in host endianness, to a * potentially unaligned memory location in big-endian format. * * @param val 16-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_be16(uint16_t val, uint8_t dst[2]) { dst[0] = val >> 8; dst[1] = val; } /** * @brief Put a 24-bit integer as big-endian to arbitrary location. * * Put a 24-bit integer, originally in host endianness, to a * potentially unaligned memory location in big-endian format. * * @param val 24-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_be24(uint32_t val, uint8_t dst[3]) { dst[0] = val >> 16; sys_put_be16(val, &dst[1]); } /** * @brief Put a 32-bit integer as big-endian to arbitrary location. * * Put a 32-bit integer, originally in host endianness, to a * potentially unaligned memory location in big-endian format. * * @param val 32-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_be32(uint32_t val, uint8_t dst[4]) { sys_put_be16(val >> 16, dst); sys_put_be16(val, &dst[2]); } /** * @brief Put a 48-bit integer as big-endian to arbitrary location. * * Put a 48-bit integer, originally in host endianness, to a * potentially unaligned memory location in big-endian format. * * @param val 48-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_be48(uint64_t val, uint8_t dst[6]) { sys_put_be16(val >> 32, dst); sys_put_be32(val, &dst[2]); } /** * @brief Put a 64-bit integer as big-endian to arbitrary location. * * Put a 64-bit integer, originally in host endianness, to a * potentially unaligned memory location in big-endian format. * * @param val 64-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_be64(uint64_t val, uint8_t dst[8]) { sys_put_be32(val >> 32, dst); sys_put_be32(val, &dst[4]); } /** * @brief Put a 16-bit integer as little-endian to arbitrary location. * * Put a 16-bit integer, originally in host endianness, to a * potentially unaligned memory location in little-endian format. * * @param val 16-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_le16(uint16_t val, uint8_t dst[2]) { dst[0] = val; dst[1] = val >> 8; } /** * @brief Put a 24-bit integer as little-endian to arbitrary location. * * Put a 24-bit integer, originally in host endianness, to a * potentially unaligned memory location in little-endian format. * * @param val 24-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_le24(uint32_t val, uint8_t dst[3]) { sys_put_le16(val, dst); dst[2] = val >> 16; } /** * @brief Put a 32-bit integer as little-endian to arbitrary location. * * Put a 32-bit integer, originally in host endianness, to a * potentially unaligned memory location in little-endian format. * * @param val 32-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_le32(uint32_t val, uint8_t dst[4]) { sys_put_le16(val, dst); sys_put_le16(val >> 16, &dst[2]); } /** * @brief Put a 48-bit integer as little-endian to arbitrary location. * * Put a 48-bit integer, originally in host endianness, to a * potentially unaligned memory location in little-endian format. * * @param val 48-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_le48(uint64_t val, uint8_t dst[6]) { sys_put_le32(val, dst); sys_put_le16(val >> 32, &dst[4]); } /** * @brief Put a 64-bit integer as little-endian to arbitrary location. * * Put a 64-bit integer, originally in host endianness, to a * potentially unaligned memory location in little-endian format. * * @param val 64-bit integer in host endianness. * @param dst Destination memory address to store the result. */ static inline void sys_put_le64(uint64_t val, uint8_t dst[8]) { sys_put_le32(val, dst); sys_put_le32(val >> 32, &dst[4]); } /** * @brief Get a 16-bit integer stored in big-endian format. * * Get a 16-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 16-bit integer to get. * * @return 16-bit integer in host endianness. */ static inline uint16_t sys_get_be16(const uint8_t src[2]) { return ((uint16_t)src[0] << 8) | src[1]; } /** * @brief Get a 24-bit integer stored in big-endian format. * * Get a 24-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 24-bit integer to get. * * @return 24-bit integer in host endianness. */ static inline uint32_t sys_get_be24(const uint8_t src[3]) { return ((uint32_t)src[0] << 16) | sys_get_be16(&src[1]); } /** * @brief Get a 32-bit integer stored in big-endian format. * * Get a 32-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 32-bit integer to get. * * @return 32-bit integer in host endianness. */ static inline uint32_t sys_get_be32(const uint8_t src[4]) { return ((uint32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]); } /** * @brief Get a 48-bit integer stored in big-endian format. * * Get a 48-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 48-bit integer to get. * * @return 48-bit integer in host endianness. */ static inline uint64_t sys_get_be48(const uint8_t src[6]) { return ((uint64_t)sys_get_be32(&src[0]) << 16) | sys_get_be16(&src[4]); } /** * @brief Get a 64-bit integer stored in big-endian format. * * Get a 64-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 64-bit integer to get. * * @return 64-bit integer in host endianness. */ static inline uint64_t sys_get_be64(const uint8_t src[8]) { return ((uint64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]); } /** * @brief Get a 16-bit integer stored in little-endian format. * * Get a 16-bit integer, stored in little-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the little-endian 16-bit integer to get. * * @return 16-bit integer in host endianness. */ static inline uint16_t sys_get_le16(const uint8_t src[2]) { return ((uint16_t)src[1] << 8) | src[0]; } /** * @brief Get a 24-bit integer stored in big-endian format. * * Get a 24-bit integer, stored in big-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the big-endian 24-bit integer to get. * * @return 24-bit integer in host endianness. */ static inline uint32_t sys_get_le24(const uint8_t src[3]) { return ((uint32_t)src[2] << 16) | sys_get_le16(&src[0]); } /** * @brief Get a 32-bit integer stored in little-endian format. * * Get a 32-bit integer, stored in little-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the little-endian 32-bit integer to get. * * @return 32-bit integer in host endianness. */ static inline uint32_t sys_get_le32(const uint8_t src[4]) { return ((uint32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]); } /** * @brief Get a 48-bit integer stored in little-endian format. * * Get a 48-bit integer, stored in little-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the little-endian 48-bit integer to get. * * @return 48-bit integer in host endianness. */ static inline uint64_t sys_get_le48(const uint8_t src[6]) { return ((uint64_t)sys_get_le32(&src[2]) << 16) | sys_get_le16(&src[0]); } /** * @brief Get a 64-bit integer stored in little-endian format. * * Get a 64-bit integer, stored in little-endian format in a potentially * unaligned memory location, and convert it to the host endianness. * * @param src Location of the little-endian 64-bit integer to get. * * @return 64-bit integer in host endianness. */ static inline uint64_t sys_get_le64(const uint8_t src[8]) { return ((uint64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]); } /** * @brief Swap one buffer content into another * * Copy the content of src buffer into dst buffer in reversed order, * i.e.: src[n] will be put in dst[end-n] * Where n is an index and 'end' the last index in both arrays. * The 2 memory pointers must be pointing to different areas, and have * a minimum size of given length. * * @param dst A valid pointer on a memory area where to copy the data in * @param src A valid pointer on a memory area where to copy the data from * @param length Size of both dst and src memory areas */ static inline void sys_memcpy_swap(void *dst, const void *src, size_t length) { uint8_t *pdst = (uint8_t *)dst; const uint8_t *psrc = (const uint8_t *)src; __ASSERT(((psrc < pdst && (psrc + length) <= pdst) || (psrc > pdst && (pdst + length) <= psrc)), "Source and destination buffers must not overlap"); psrc += length - 1; for (; length > 0; length--) { *pdst++ = *psrc--; } } /** * @brief Swap buffer content * * In-place memory swap, where final content will be reversed. * I.e.: buf[n] will be put in buf[end-n] * Where n is an index and 'end' the last index of buf. * * @param buf A valid pointer on a memory area to swap * @param length Size of buf memory area */ static inline void sys_mem_swap(void *buf, size_t length) { size_t i; for (i = 0; i < (length/2); i++) { uint8_t tmp = ((uint8_t *)buf)[i]; ((uint8_t *)buf)[i] = ((uint8_t *)buf)[length - 1 - i]; ((uint8_t *)buf)[length - 1 - i] = tmp; } } #endif /* ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ */ |