1 /* 2 * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF) 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD 7 */ 8 /* 9 * Licensed to the Apache Software Foundation (ASF) under one 10 * or more contributor license agreements. See the NOTICE file 11 * distributed with this work for additional information 12 * regarding copyright ownership. The ASF licenses this file 13 * to you under the Apache License, Version 2.0 (the 14 * "License"); you may not use this file except in compliance 15 * with the License. You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, 20 * software distributed under the License is distributed on an 21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 22 * KIND, either express or implied. See the License for the 23 * specific language governing permissions and limitations 24 * under the License. 25 */ 26 27 #ifndef H_ENDIAN_ 28 #define H_ENDIAN_ 29 30 #include <inttypes.h> 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 /* Internal helpers */ 37 #ifndef os_bswap_64 38 #define os_bswap_64(x) ((uint64_t) \ 39 ((((x) & 0xff00000000000000ull) >> 56) | \ 40 (((x) & 0x00ff000000000000ull) >> 40) | \ 41 (((x) & 0x0000ff0000000000ull) >> 24) | \ 42 (((x) & 0x000000ff00000000ull) >> 8) | \ 43 (((x) & 0x00000000ff000000ull) << 8) | \ 44 (((x) & 0x0000000000ff0000ull) << 24) | \ 45 (((x) & 0x000000000000ff00ull) << 40) | \ 46 (((x) & 0x00000000000000ffull) << 56))) 47 #endif 48 49 #ifndef os_bswap_32 50 #define os_bswap_32(x) ((uint32_t) \ 51 ((((x) & 0xff000000) >> 24) | \ 52 (((x) & 0x00ff0000) >> 8) | \ 53 (((x) & 0x0000ff00) << 8) | \ 54 (((x) & 0x000000ff) << 24))) 55 #endif 56 57 #ifndef os_bswap_16 58 #define os_bswap_16(x) ((uint16_t) \ 59 ((((x) & 0xff00) >> 8) | \ 60 (((x) & 0x00ff) << 8))) 61 #endif 62 63 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 64 65 #ifndef ntohll 66 #define ntohll(x) ((uint64_t)(x)) 67 #endif 68 69 #ifndef htonll 70 #define htonll(x) ((uint64_t)(x)) 71 #endif 72 73 #ifndef ntohl 74 #define ntohl(x) ((uint32_t)(x)) 75 #endif 76 77 #ifndef htonl 78 #define htonl(x) ((uint32_t)(x)) 79 #endif 80 81 #ifndef ntohs 82 #define ntohs(x) ((uint16_t)(x)) 83 #endif 84 85 #ifndef htons 86 #define htons(x) ((uint16_t)(x)) 87 #endif 88 89 #ifndef htobe16 90 #define htobe16(x) ((uint16_t)(x)) 91 #endif 92 93 #ifndef htole16 94 #define htole16(x) os_bswap_16 (x) 95 #endif 96 97 #ifndef be16toh 98 #define be16toh(x) ((uint16_t)(x)) 99 #endif 100 101 #ifndef le16toh 102 #define le16toh(x) os_bswap_16 (x) 103 #endif 104 105 #ifndef htobe32 106 #define htobe32(x) ((uint32_t)(x)) 107 #endif 108 109 #ifndef htole32 110 #define htole32(x) os_bswap_32 (x) 111 #endif 112 113 #ifndef be32toh 114 #define be32toh(x) ((uint32_t)(x)) 115 #endif 116 117 #ifndef le32toh 118 #define le32toh(x) os_bswap_32 (x) 119 #endif 120 121 #ifndef htobe64 122 #define htobe64(x) ((uint64_t)(x)) 123 #endif 124 125 #ifndef htole64 126 #define htole64(x) os_bswap_64 (x) 127 #endif 128 129 #ifndef be64toh 130 #define be64toh(x) ((uint64_t)(x)) 131 #endif 132 133 #ifndef le64toh 134 #define le64toh(x) os_bswap_64 (x) 135 #endif 136 137 #else 138 139 #ifndef ntohll 140 #define ntohll(x) os_bswap_64(x) 141 #endif 142 143 #ifndef htonll 144 #define htonll ntohll 145 #endif 146 147 /* These are not used in NimBLE and ESP-IDF uses them from LwIP */ 148 #if 0 149 #ifndef ntohl 150 #define ntohl(x) os_bswap_32(x) 151 #endif 152 153 #ifndef htonl 154 #define htonl ntohl 155 #endif 156 157 #ifndef htons 158 #define htons(x) os_bswap_16(x) 159 #endif 160 161 #ifndef ntohs 162 #define ntohs htons 163 #endif 164 #endif 165 166 #ifndef htobe16 167 #define htobe16(x) os_bswap_16(x) 168 #endif 169 170 #ifndef htole16 171 #define htole16(x) ((uint16_t)(x)) 172 #endif 173 174 #ifndef be16toh 175 #define be16toh(x) os_bswap_16(x) 176 #endif 177 178 #ifndef le16toh 179 #define le16toh(x) ((uint16_t)(x)) 180 #endif 181 182 #ifndef htobe32 183 #define htobe32(x) os_bswap_32(x) 184 #endif 185 186 #ifndef htole32 187 #define htole32(x) ((uint32_t)(x)) 188 #endif 189 190 #ifndef be32toh 191 #define be32toh(x) os_bswap_32(x) 192 #endif 193 194 #ifndef le32toh 195 #define le32toh(x) ((uint32_t)(x)) 196 #endif 197 198 #ifndef htobe64 199 #define htobe64(x) os_bswap_64(x) 200 #endif 201 202 #ifndef htole64 203 #define htole64(x) ((uint64_t)(x)) 204 #endif 205 206 #ifndef be64toh 207 #define be64toh(x) os_bswap_64(x) 208 #endif 209 210 #ifndef le64toh 211 #define le64toh(x) ((uint64_t)(x)) 212 #endif 213 214 #endif 215 216 #if SOC_ESP_NIMBLE_CONTROLLER 217 void r_put_le16(void *buf, uint16_t x); 218 #define put_le16 r_put_le16 219 220 void r_put_le24(void *buf, uint32_t x); 221 #define put_le24 r_put_le24 222 223 void r_put_le32(void *buf, uint32_t x); 224 #define put_le32 r_put_le32 225 226 void r_put_le64(void *buf, uint64_t x); 227 #define put_le64 r_put_le64 228 229 uint16_t r_get_le16(const void *buf); 230 #define get_le16 r_get_le16 231 232 uint32_t r_get_le24(const void *buf); 233 #define get_le24 r_get_le24 234 235 uint32_t r_get_le32(const void *buf); 236 #define get_le32 r_get_le32 237 238 uint64_t r_get_le64(const void *buf); 239 #define get_le64 r_get_le64 240 241 void r_put_be16(void *buf, uint16_t x); 242 #define put_be16 r_put_be16 243 244 void r_put_be24(void *buf, uint32_t x); 245 #define put_be24 r_put_be24 246 247 void r_put_be32(void *buf, uint32_t x); 248 #define put_be32 r_put_be32 249 250 void r_put_be64(void *buf, uint64_t x); 251 #define put_be64 r_put_be64 252 253 uint16_t r_get_be16(const void *buf); 254 #define get_be16 r_get_be16 255 256 uint32_t r_get_be24(const void *buf); 257 #define get_be24 r_get_be24 258 259 uint32_t r_get_be32(const void *buf); 260 #define get_be32 r_get_be32 261 262 uint64_t r_get_be64(const void *buf); 263 #define get_be64 r_get_be64 264 265 void r_swap_in_place(void *buf, int len); 266 #define swap_in_place r_swap_in_place 267 268 void r_swap_buf(uint8_t *dst, const uint8_t *src, int len); 269 #define swap_buf r_swap_buf 270 271 272 #else 273 void put_le16(void *buf, uint16_t x); 274 void put_le24(void *buf, uint32_t x); 275 void put_le32(void *buf, uint32_t x); 276 void put_le64(void *buf, uint64_t x); 277 uint16_t get_le16(const void *buf); 278 uint32_t get_le24(const void *buf); 279 uint32_t get_le32(const void *buf); 280 uint64_t get_le64(const void *buf); 281 void put_be16(void *buf, uint16_t x); 282 void put_be24(void *buf, uint32_t x); 283 void put_be32(void *buf, uint32_t x); 284 void put_be64(void *buf, uint64_t x); 285 uint16_t get_be16(const void *buf); 286 uint32_t get_be24(const void *buf); 287 uint32_t get_be32(const void *buf); 288 uint64_t get_be64(const void *buf); 289 void swap_in_place(void *buf, int len); 290 void swap_buf(uint8_t *dst, const uint8_t *src, int len); 291 #endif 292 #ifdef __cplusplus 293 } 294 #endif 295 296 #endif 297