Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #ifndef BCMOLT_BUF_H_ |
| 31 | #define BCMOLT_BUF_H_ |
| 32 | |
| 33 | #include "bcmos_system.h" |
| 34 | |
| 35 | /** Generic memory stream object */ |
| 36 | typedef struct bcmolt_buf bcmolt_buf; |
| 37 | |
| 38 | struct bcmolt_buf |
| 39 | { |
| 40 | uint8_t *start; /**< Pointer to the start of the buffer */ |
| 41 | uint8_t *curr; /**< Pointer to the current position in the buffer */ |
| 42 | uint32_t len; /**< Total Length of buffer */ |
| 43 | /* The following 2 fields enable foreign buffer encapsulation */ |
| 44 | void (*free)(void *bh); /**< Foreign buffer release callback */ |
| 45 | void *bh; /**< Foreign buffer handle */ |
| 46 | }; |
| 47 | |
| 48 | /* Serialization buffer endianness */ |
| 49 | #define BCMOLT_BUF_ENDIAN_FIXED BCMOS_ENDIAN_BIG |
| 50 | |
| 51 | #if BCMOLT_BUF_ENDIAN_FIXED == BCMOS_ENDIAN_BIG |
| 52 | #define BCMOLT_BUF_ENDIAN_BUF_TO_CPU(size, n) BCMOS_ENDIAN_BIG_TO_CPU_##size(n) |
| 53 | #define BCMOLT_BUF_ENDIAN_CPU_TO_BUF(size, n) BCMOS_ENDIAN_CPU_TO_BIG_##size(n) |
| 54 | #else |
| 55 | #define BCMOLT_BUF_ENDIAN_BUF_TO_CPU(size, n) BCMOS_ENDIAN_LITTLE_TO_CPU_##size(n) |
| 56 | #define BCMOLT_BUF_ENDIAN_CPU_TO_BUF(size, n) BCMOS_ENDIAN_CPU_TO_LITTLE_##size(n) |
| 57 | #endif |
| 58 | |
| 59 | /** Initalize a bcmolt_buf stream |
| 60 | * |
| 61 | * \param buf |
| 62 | * \param size |
| 63 | * \param start |
| 64 | * \param endian Endianness of numbers in the resulting stream. |
| 65 | * This parameter is only for sanity check. Buffer endianness is |
| 66 | * set at compile time. Application MUST NOT rely on default buffer endianness |
| 67 | * being Big Endian. |
| 68 | * If Big Endian buffer is required, application must use |
| 69 | * access functions with "_be" designator (bcmolt_buf_read_u16_be(), etc.) |
| 70 | */ |
| 71 | void bcmolt_buf_init(bcmolt_buf *buf, uint32_t size, uint8_t *start, bcmos_endian endian); |
| 72 | |
| 73 | /** Allocate data buffer and initialize bcmolt_buf stream |
| 74 | * |
| 75 | * \param buf |
| 76 | * \param size |
| 77 | * \param endian Endianness of numbers in the resulting stream. |
| 78 | * See explanation in bcmolt_buf_alloc() |
| 79 | * \return BCM_ERR_OK or BCM_ERR_NOMEM |
| 80 | */ |
| 81 | bcmos_errno bcmolt_buf_alloc(bcmolt_buf *buf, uint32_t size, bcmos_endian endian); |
| 82 | |
| 83 | /** Release data buffer pointed by bcmolt_buf stream |
| 84 | * \param[in,out] buf |
| 85 | */ |
| 86 | void bcmolt_buf_free(bcmolt_buf *buf); |
| 87 | |
| 88 | /** Read from the buffer |
| 89 | * |
| 90 | * \param buf bcmolt_buf instance |
| 91 | * \param to Where to read to |
| 92 | * \param len Number of bytes to copy |
| 93 | * |
| 94 | * \return BCMOS_TRUE if successfully copied |
| 95 | */ |
| 96 | bcmos_bool bcmolt_buf_read(bcmolt_buf *buf, void *to, size_t len); |
| 97 | |
| 98 | /** Transfer bytes from one buf to another |
| 99 | * |
| 100 | * \param *from Source buffer |
| 101 | * \param *to Destination buffer |
| 102 | * \param bytes Number of bytes to transfer |
| 103 | * \return BCMOS_TRUE if successfully transferred |
| 104 | */ |
| 105 | bcmos_bool bcmolt_buf_transfer_bytes(bcmolt_buf *from, bcmolt_buf *to, uint32_t bytes); |
| 106 | |
| 107 | /** Write to the buffer |
| 108 | * |
| 109 | * \param buf bcmolt_buf instance |
| 110 | * \param from Source, to copy from |
| 111 | * \param len Number of bytes to copy |
| 112 | * |
| 113 | * \return BCMOS_TRUE if successfully copied |
| 114 | */ |
| 115 | bcmos_bool bcmolt_buf_write(bcmolt_buf *buf, const void *from, size_t len); |
| 116 | |
| 117 | /** Move the current pointer to a given position in the buffer |
| 118 | * |
| 119 | * \param pos Byte position in the buffer to move the current pointer to |
| 120 | * |
| 121 | * \param *buf Input buffer |
| 122 | * \return BCMOS_FALSE if len takes us past the end of buffer |
| 123 | */ |
| 124 | bcmos_bool bcmolt_buf_set_pos(bcmolt_buf *buf, uint32_t pos); |
| 125 | |
| 126 | /** Move the current pointer ahead by given number of bytes |
| 127 | * |
| 128 | * \param buf bcmolt_buf instance |
| 129 | * \param len Number of bytes to skip |
| 130 | * |
| 131 | * \return BCMOS_FALSE if len takes us past the end of buffer |
| 132 | */ |
| 133 | bcmos_bool bcmolt_buf_skip(bcmolt_buf *buf, uint32_t len); |
| 134 | |
| 135 | /** Move the current pointer back by given number of bytes |
| 136 | * |
| 137 | * \param buf bcmolt_buf instance |
| 138 | * \param len Number of bytes to go back |
| 139 | * |
| 140 | * \return BCMOS_FALSE if len takes us past the start of buffer |
| 141 | */ |
| 142 | bcmos_bool bcmolt_buf_rewind(bcmolt_buf *buf, uint32_t len); |
| 143 | |
| 144 | /** Get the current buffer pointer |
| 145 | * |
| 146 | * \param buf bcmolt_buf instance |
| 147 | * |
| 148 | * \return the current buffer pointer |
| 149 | */ |
| 150 | static inline uint8_t *bcmolt_buf_snap_get(const bcmolt_buf *buf) |
| 151 | { |
| 152 | return buf->curr; |
| 153 | } |
| 154 | |
| 155 | /** Move the current pointer to a snapped location |
| 156 | * |
| 157 | * \param buf bcmolt_buf instance |
| 158 | * \param snap snapped location |
| 159 | */ |
| 160 | static inline void bcmolt_buf_snap_restore(bcmolt_buf *buf, uint8_t *snap) |
| 161 | { |
| 162 | buf->curr = snap; |
| 163 | } |
| 164 | |
| 165 | /** Get the length of unprocessed bytes in given stream |
| 166 | * |
| 167 | * \param buf Input buffer |
| 168 | * |
| 169 | * \return The number of remaining bytes in the buffer |
| 170 | */ |
| 171 | static inline uint32_t bcmolt_buf_get_remaining_size(const bcmolt_buf *buf) |
| 172 | { |
| 173 | return (uint32_t)((buf->start + buf->len) - buf->curr); |
| 174 | } |
| 175 | |
| 176 | /** Get amount of buf that has been read or written so far |
| 177 | * |
| 178 | * \param buf Input buffer |
| 179 | * \return Amount of buffer used |
| 180 | */ |
| 181 | static inline uint32_t bcmolt_buf_get_used(const bcmolt_buf *buf) |
| 182 | { |
| 183 | return (uint32_t)(buf->curr - buf->start); |
| 184 | } |
| 185 | |
| 186 | /** Reads a uint8_t from a buffer |
| 187 | * |
| 188 | * \param buf Buffer to read from |
| 189 | * \param val uint8_t to read |
| 190 | * |
| 191 | * \return BCMOS_TRUE if read successful |
| 192 | */ |
| 193 | static inline bcmos_bool bcmolt_buf_read_u8(bcmolt_buf *buf, uint8_t *val) |
| 194 | { |
| 195 | return bcmolt_buf_read(buf, val, sizeof(*val)); |
| 196 | } |
| 197 | |
| 198 | /** Writes a uint8_t to a buffer |
| 199 | * |
| 200 | * \param buf Buffer to write to |
| 201 | * \param val uint8_t to write |
| 202 | * |
| 203 | * \return BCMOS_TRUE if write successful |
| 204 | */ |
| 205 | static inline bcmos_bool bcmolt_buf_write_u8(bcmolt_buf *buf, uint8_t val) |
| 206 | { |
| 207 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 208 | } |
| 209 | |
| 210 | /** Reads a boolean from a buffer |
| 211 | * |
| 212 | * \param *buf |
| 213 | * \param *val |
| 214 | */ |
| 215 | bcmos_bool bcmolt_buf_read_bool(bcmolt_buf *buf, bcmos_bool *val); |
| 216 | |
| 217 | /** Writes a bcmos_bool to a buffer |
| 218 | * |
| 219 | * \param buf Buffer to write to |
| 220 | * \param val uint8_t to write |
| 221 | * |
| 222 | * \return BCMOS_TRUE if write successful |
| 223 | */ |
| 224 | static inline bcmos_bool bcmolt_buf_write_bool(bcmolt_buf *buf, bcmos_bool val) |
| 225 | { |
| 226 | return bcmolt_buf_write_u8(buf, val ? 1 : 0); |
| 227 | } |
| 228 | |
| 229 | /** Reads a int8_t from a buffer |
| 230 | * |
| 231 | * \param buf Buffer to read from |
| 232 | * \param val int8_t to read |
| 233 | * |
| 234 | * \return BCMOS_TRUE if read successful |
| 235 | */ |
| 236 | static inline bcmos_bool bcmolt_buf_read_s8(bcmolt_buf *buf, int8_t *val) |
| 237 | { |
| 238 | return bcmolt_buf_read_u8(buf, (uint8_t *)val); |
| 239 | } |
| 240 | |
| 241 | /** Writes a int8_t to a buffer |
| 242 | * |
| 243 | * \param buf Buffer to write to |
| 244 | * \param val int8_t to write |
| 245 | * |
| 246 | * \return BCMOS_TRUE if write successful |
| 247 | */ |
| 248 | static inline bcmos_bool bcmolt_buf_write_s8(bcmolt_buf *buf, int8_t val) |
| 249 | { |
| 250 | return bcmolt_buf_write_u8(buf, (uint8_t)val); |
| 251 | } |
| 252 | |
| 253 | /** Reads a uint16_t from a buffer |
| 254 | * |
| 255 | * \param buf Buffer to read from |
| 256 | * \param val uint16_t to read |
| 257 | * |
| 258 | * \return BCMOS_TRUE if read successful |
| 259 | */ |
| 260 | static inline bcmos_bool bcmolt_buf_read_u16(bcmolt_buf *buf, uint16_t *val) |
| 261 | { |
| 262 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 263 | *val = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U16, *val); |
| 264 | return res; |
| 265 | } |
| 266 | |
| 267 | /** Reads a uint16_t from a Big Endian buffer |
| 268 | * |
| 269 | * \param buf Buffer to read from |
| 270 | * \param val uint16_t to read |
| 271 | * |
| 272 | * \return BCMOS_TRUE if read successful |
| 273 | */ |
| 274 | static inline bcmos_bool bcmolt_buf_read_u16_be(bcmolt_buf *buf, uint16_t *val) |
| 275 | { |
| 276 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 277 | *val = BCMOS_ENDIAN_BIG_TO_CPU_U16(*val); |
| 278 | return res; |
| 279 | } |
| 280 | |
| 281 | /** Writes a uint16_t to a buffer |
| 282 | * |
| 283 | * \param buf Buffer to write to |
| 284 | * \param val uint16_t to write |
| 285 | * |
| 286 | * \return BCMOS_TRUE if write successful |
| 287 | */ |
| 288 | static inline bcmos_bool bcmolt_buf_write_u16(bcmolt_buf *buf, uint16_t val) |
| 289 | { |
| 290 | val = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U16, val); |
| 291 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 292 | } |
| 293 | |
| 294 | /** Writes a uint16_t to a Big Endian buffer |
| 295 | * |
| 296 | * \param buf Buffer to write to |
| 297 | * \param val uint16_t to write |
| 298 | * |
| 299 | * \return BCMOS_TRUE if write successful |
| 300 | */ |
| 301 | static inline bcmos_bool bcmolt_buf_write_u16_be(bcmolt_buf *buf, uint16_t val) |
| 302 | { |
| 303 | val = BCMOS_ENDIAN_CPU_TO_BIG_U16(val); |
| 304 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 305 | } |
| 306 | |
| 307 | /** Reads a int16_t from a buffer |
| 308 | * |
| 309 | * \param buf Buffer to read from |
| 310 | * \param val int16_t to read |
| 311 | * |
| 312 | * \return BCMOS_TRUE if read successful |
| 313 | */ |
| 314 | static inline bcmos_bool bcmolt_buf_read_s16(bcmolt_buf *buf, int16_t *val) |
| 315 | { |
| 316 | return bcmolt_buf_read_u16(buf, (uint16_t *)val); |
| 317 | } |
| 318 | |
| 319 | /** Reads a int16_t from a Big Endian buffer |
| 320 | * |
| 321 | * \param buf Buffer to read from |
| 322 | * \param val int16_t to read |
| 323 | * |
| 324 | * \return BCMOS_TRUE if read successful |
| 325 | */ |
| 326 | static inline bcmos_bool bcmolt_buf_read_s16_be(bcmolt_buf *buf, int16_t *val) |
| 327 | { |
| 328 | return bcmolt_buf_read_u16_be(buf, (uint16_t *)val); |
| 329 | } |
| 330 | |
| 331 | /** Writes int16_t to a buffer |
| 332 | * |
| 333 | * \param buf Buffer to write to |
| 334 | * \param val int16_t to write |
| 335 | * |
| 336 | * \return BCMOS_TRUE if write successful |
| 337 | */ |
| 338 | static inline bcmos_bool bcmolt_buf_write_s16(bcmolt_buf *buf, int16_t val) |
| 339 | { |
| 340 | return bcmolt_buf_write_u16(buf, (uint16_t)val); |
| 341 | } |
| 342 | |
| 343 | /** Writes int16_t to a Big Endian buffer |
| 344 | * |
| 345 | * \param buf Buffer to write to |
| 346 | * \param val int16_t to write |
| 347 | * |
| 348 | * \return BCMOS_TRUE if write successful |
| 349 | */ |
| 350 | static inline bcmos_bool bcmolt_buf_write_s16_be(bcmolt_buf *buf, int16_t val) |
| 351 | { |
| 352 | return bcmolt_buf_write_u16_be(buf, (uint16_t)val); |
| 353 | } |
| 354 | |
| 355 | /** Reads a bcmos_u24 from a buffer |
| 356 | * |
| 357 | * \param buf Buffer to read from |
| 358 | * \param val bcmos_u24 to read |
| 359 | * |
| 360 | * \return BCMOS_TRUE if read successful |
| 361 | */ |
| 362 | static inline bcmos_bool bcmolt_buf_read_u24(bcmolt_buf *buf, uint24_t *val) |
| 363 | { |
| 364 | return bcmolt_buf_read_u8(buf, &(val->low_hi.hi)) && |
| 365 | bcmolt_buf_read_u8(buf, &(val->low_hi.mid)) && |
| 366 | bcmolt_buf_read_u8(buf, &(val->low_hi.low)); |
| 367 | } |
| 368 | |
| 369 | /** Writes a bcmos_u24 to a buffer |
| 370 | * |
| 371 | * \param buf Buffer to write to |
| 372 | * \param val bcmos_u24 to write |
| 373 | * |
| 374 | * \return BCMOS_TRUE if write successful |
| 375 | */ |
| 376 | static inline bcmos_bool bcmolt_buf_write_u24(bcmolt_buf *buf, uint24_t val) |
| 377 | { |
| 378 | return bcmolt_buf_write_u8(buf, val.low_hi.hi) && |
| 379 | bcmolt_buf_write_u8(buf, val.low_hi.mid) && |
| 380 | bcmolt_buf_write_u8(buf, val.low_hi.low); |
| 381 | } |
| 382 | |
| 383 | /** Reads a uint32_t from a buffer |
| 384 | * |
| 385 | * \param buf Buffer to read from |
| 386 | * \param val uint32_t to read |
| 387 | * |
| 388 | * \return BCMOS_TRUE if read successful |
| 389 | */ |
| 390 | static inline bcmos_bool bcmolt_buf_read_u32(bcmolt_buf *buf, uint32_t *val) |
| 391 | { |
| 392 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 393 | *val = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U32, *val); |
| 394 | return res; |
| 395 | } |
| 396 | |
| 397 | /** Reads a uint32_t from a Big Endian buffer |
| 398 | * |
| 399 | * \param buf Buffer to read from |
| 400 | * \param val uint32_t to read |
| 401 | * |
| 402 | * \return BCMOS_TRUE if read successful |
| 403 | */ |
| 404 | static inline bcmos_bool bcmolt_buf_read_u32_be(bcmolt_buf *buf, uint32_t *val) |
| 405 | { |
| 406 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 407 | *val = BCMOS_ENDIAN_BIG_TO_CPU_U32(*val); |
| 408 | return res; |
| 409 | } |
| 410 | |
| 411 | /** Writes a uint32_t to a buffer |
| 412 | * |
| 413 | * \param buf Buffer to write to |
| 414 | * \param val uint32_t to write |
| 415 | * |
| 416 | * \return BCMOS_TRUE if write successful |
| 417 | */ |
| 418 | static inline bcmos_bool bcmolt_buf_write_u32(bcmolt_buf *buf, uint32_t val) |
| 419 | { |
| 420 | val = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U32, val); |
| 421 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 422 | } |
| 423 | |
| 424 | /** Writes a uint32_t to a Big Endian buffer |
| 425 | * |
| 426 | * \param buf Buffer to write to |
| 427 | * \param val uint32_t to write |
| 428 | * |
| 429 | * \return BCMOS_TRUE if write successful |
| 430 | */ |
| 431 | static inline bcmos_bool bcmolt_buf_write_u32_be(bcmolt_buf *buf, uint32_t val) |
| 432 | { |
| 433 | val = BCMOS_ENDIAN_CPU_TO_BIG_U32(val); |
| 434 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 435 | } |
| 436 | |
| 437 | /** Reads a int32_t from a buffer |
| 438 | * |
| 439 | * \param buf Buffer to read from |
| 440 | * \param val int32_t to read |
| 441 | * |
| 442 | * \return BCMOS_TRUE if read successful |
| 443 | */ |
| 444 | static inline bcmos_bool bcmolt_buf_read_s32(bcmolt_buf *buf, int32_t *val) |
| 445 | { |
| 446 | return bcmolt_buf_read_u32(buf, (uint32_t *)val); |
| 447 | } |
| 448 | |
| 449 | /** Reads a int32_t from a big endian buffer |
| 450 | * |
| 451 | * \param buf Buffer to read from |
| 452 | * \param val int32_t to read |
| 453 | * |
| 454 | * \return BCMOS_TRUE if read successful |
| 455 | */ |
| 456 | static inline bcmos_bool bcmolt_buf_read_s32_be(bcmolt_buf *buf, int32_t *val) |
| 457 | { |
| 458 | return bcmolt_buf_read_u32_be(buf, (uint32_t *)val); |
| 459 | } |
| 460 | |
| 461 | /** Writes a int32_t to a buffer |
| 462 | * |
| 463 | * \param buf Buffer to write to |
| 464 | * \param val int32_t to write |
| 465 | * |
| 466 | * \return BCMOS_TRUE if write successful |
| 467 | */ |
| 468 | static inline bcmos_bool bcmolt_buf_write_s32(bcmolt_buf *buf, int32_t val) |
| 469 | { |
| 470 | return bcmolt_buf_write_u32(buf, (uint32_t)val); |
| 471 | } |
| 472 | |
| 473 | /** Writes a int32_t to a Big Endian buffer |
| 474 | * |
| 475 | * \param buf Buffer to write to |
| 476 | * \param val int32_t to write |
| 477 | * |
| 478 | * \return BCMOS_TRUE if write successful |
| 479 | */ |
| 480 | static inline bcmos_bool bcmolt_buf_write_s32_be(bcmolt_buf *buf, int32_t val) |
| 481 | { |
| 482 | return bcmolt_buf_write_u32_be(buf, (uint32_t)val); |
| 483 | } |
| 484 | |
| 485 | /** Reads a uint64_t from a buffer |
| 486 | * |
| 487 | * \param buf Buffer to read from |
| 488 | * \param val uint64_t to read |
| 489 | * |
| 490 | * \return BCMOS_TRUE if read successful |
| 491 | */ |
| 492 | static inline bcmos_bool bcmolt_buf_read_u64(bcmolt_buf *buf, uint64_t *val) |
| 493 | { |
| 494 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 495 | *val = BCMOLT_BUF_ENDIAN_BUF_TO_CPU(U64, *val); |
| 496 | return res; |
| 497 | } |
| 498 | |
| 499 | /** Reads a uint64_t from a Big Endian buffer |
| 500 | * |
| 501 | * \param buf Buffer to read from |
| 502 | * \param val uint64_t to read |
| 503 | * |
| 504 | * \return BCMOS_TRUE if read successful |
| 505 | */ |
| 506 | static inline bcmos_bool bcmolt_buf_read_u64_be(bcmolt_buf *buf, uint64_t *val) |
| 507 | { |
| 508 | bcmos_bool res = bcmolt_buf_read(buf, val, sizeof(*val)); |
| 509 | *val = BCMOS_ENDIAN_BIG_TO_CPU_U64(*val); |
| 510 | return res; |
| 511 | } |
| 512 | |
| 513 | /** Writes a uint64_t to a buffer |
| 514 | * |
| 515 | * \param buf Buffer to write to |
| 516 | * \param val uint64_t to write |
| 517 | * |
| 518 | * \return BCMOS_TRUE if write successful |
| 519 | */ |
| 520 | static inline bcmos_bool bcmolt_buf_write_u64(bcmolt_buf *buf, uint64_t val) |
| 521 | { |
| 522 | val = BCMOLT_BUF_ENDIAN_CPU_TO_BUF(U64, val); |
| 523 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 524 | } |
| 525 | |
| 526 | /** Writes a uint64_t to a Big Endian buffer |
| 527 | * |
| 528 | * \param buf Buffer to write to |
| 529 | * \param val uint64_t to write |
| 530 | * |
| 531 | * \return BCMOS_TRUE if write successful |
| 532 | */ |
| 533 | static inline bcmos_bool bcmolt_buf_write_u64_be(bcmolt_buf *buf, uint64_t val) |
| 534 | { |
| 535 | val = BCMOS_ENDIAN_CPU_TO_BIG_U64(val); |
| 536 | return bcmolt_buf_write(buf, &val, sizeof(val)); |
| 537 | } |
| 538 | |
| 539 | /** Reads a int64_t from a buffer |
| 540 | * |
| 541 | * \param buf Buffer to read from |
| 542 | * \param val int64_t to read |
| 543 | * |
| 544 | * \return BCMOS_TRUE if read successful |
| 545 | */ |
| 546 | static inline bcmos_bool bcmolt_buf_read_s64(bcmolt_buf *buf, int64_t *val) |
| 547 | { |
| 548 | return bcmolt_buf_read_u64(buf, (uint64_t *)val); |
| 549 | } |
| 550 | |
| 551 | /** Reads a int64_t from a Big Endian buffer |
| 552 | * |
| 553 | * \param buf Buffer to read from |
| 554 | * \param val int64_t to read |
| 555 | * |
| 556 | * \return BCMOS_TRUE if read successful |
| 557 | */ |
| 558 | static inline bcmos_bool bcmolt_buf_read_s64_be(bcmolt_buf *buf, int64_t *val) |
| 559 | { |
| 560 | return bcmolt_buf_read_u64_be(buf, (uint64_t *)val); |
| 561 | } |
| 562 | |
| 563 | /** Writes a int64_t to a buffer |
| 564 | * |
| 565 | * \param buf Buffer to write to |
| 566 | * \param val int64_t to write |
| 567 | * |
| 568 | * \return BCMOS_TRUE if write successful |
| 569 | */ |
| 570 | static inline bcmos_bool bcmolt_buf_write_s64(bcmolt_buf *buf, int64_t val) |
| 571 | { |
| 572 | return bcmolt_buf_write_u64(buf, (uint64_t)val); |
| 573 | } |
| 574 | |
| 575 | /** Writes a int64_t to a Big Endian buffer |
| 576 | * |
| 577 | * \param buf Buffer to write to |
| 578 | * \param val int64_t to write |
| 579 | * |
| 580 | * \return BCMOS_TRUE if write successful |
| 581 | */ |
| 582 | static inline bcmos_bool bcmolt_buf_write_s64_be(bcmolt_buf *buf, int64_t val) |
| 583 | { |
| 584 | return bcmolt_buf_write_u64_be(buf, (uint64_t)val); |
| 585 | } |
| 586 | |
| 587 | /** Reads a float from a buffer |
| 588 | * |
| 589 | * \param buf Buffer to read from |
| 590 | * \param val float to read |
| 591 | * |
| 592 | * \return BCMOS_TRUE if read successful |
| 593 | */ |
| 594 | static inline bcmos_bool bcmolt_buf_read_float(bcmolt_buf *buf, float *val) |
| 595 | { |
| 596 | return bcmolt_buf_read_u32(buf, (uint32_t *)val); |
| 597 | } |
| 598 | |
| 599 | /** Writes a float to a buffer |
| 600 | * |
| 601 | * \param buf Buffer to write to |
| 602 | * \param val float to write |
| 603 | * |
| 604 | * \return BCMOS_TRUE if write successful |
| 605 | */ |
| 606 | static inline bcmos_bool bcmolt_buf_write_float(bcmolt_buf *buf, float val) |
| 607 | { |
| 608 | uint32_t *num = (uint32_t *)&val; |
| 609 | return bcmolt_buf_write_u32(buf, *num); |
| 610 | } |
| 611 | |
| 612 | /** Reads a double from a buffer |
| 613 | * |
| 614 | * \param buf Buffer to read from |
| 615 | * \param val double to read |
| 616 | * |
| 617 | * \return BCMOS_TRUE if read successful |
| 618 | */ |
| 619 | static inline bcmos_bool bcmolt_buf_read_double(bcmolt_buf *buf, double *val) |
| 620 | { |
| 621 | return bcmolt_buf_read_u64(buf, (uint64_t *)val); |
| 622 | } |
| 623 | |
| 624 | /** Writes a double to a buffer |
| 625 | * |
| 626 | * \param buf Buffer to write to |
| 627 | * \param val double to write |
| 628 | * |
| 629 | * \return BCMOS_TRUE if write successful |
| 630 | */ |
| 631 | static inline bcmos_bool bcmolt_buf_write_double(bcmolt_buf *buf, double val) |
| 632 | { |
| 633 | uint64_t *num = (uint64_t *)&val; |
| 634 | return bcmolt_buf_write_u64(buf, *num); |
| 635 | } |
| 636 | |
| 637 | /** Reads a bcmos_vlan_tag from a buffer |
| 638 | * |
| 639 | * \param buf Buffer to read from |
| 640 | * \param val bcmos_vlan_tag to read |
| 641 | * |
| 642 | * \return BCMOS_TRUE if read successful |
| 643 | */ |
| 644 | static inline bcmos_bool bcmolt_buf_read_vlan_tag(bcmolt_buf *buf, bcmos_vlan_tag *val) |
| 645 | { |
| 646 | return bcmolt_buf_read_u16(buf, (uint16_t *)val); |
| 647 | } |
| 648 | |
| 649 | /** Writes a bcmos_vlan_tag to a buffer |
| 650 | * |
| 651 | * \param buf Buffer to write to |
| 652 | * \param val bcmos_vlan_tag to write |
| 653 | * |
| 654 | * \return BCMOS_TRUE if write successful |
| 655 | */ |
| 656 | static inline bcmos_bool bcmolt_buf_write_vlan_tag(bcmolt_buf *buf, bcmos_vlan_tag val) |
| 657 | { |
| 658 | return bcmolt_buf_write_u16(buf, (uint16_t)val); |
| 659 | } |
| 660 | |
| 661 | /** Reads a bcmos_mac_address from a buffer |
| 662 | * |
| 663 | * \param buf Buffer to read from |
| 664 | * \param val bcmos_mac_address to read |
| 665 | * |
| 666 | * \return BCMOS_TRUE if read successful |
| 667 | */ |
| 668 | static inline bcmos_bool bcmolt_buf_read_mac_address(bcmolt_buf *buf, bcmos_mac_address *val) |
| 669 | { |
| 670 | return bcmolt_buf_read(buf, val, sizeof(bcmos_mac_address)); |
| 671 | } |
| 672 | |
| 673 | /** Writes a bcmos_mac_address to a buffer |
| 674 | * |
| 675 | * \param buf Buffer to write to |
| 676 | * \param val bcmos_mac_address to write |
| 677 | * |
| 678 | * \return BCMOS_TRUE if write successful |
| 679 | */ |
| 680 | static inline bcmos_bool bcmolt_buf_write_mac_address(bcmolt_buf *buf, bcmos_mac_address val) |
| 681 | { |
| 682 | return bcmolt_buf_write(buf, &val, sizeof(bcmos_mac_address)); |
| 683 | } |
| 684 | |
| 685 | /** Reads a bcmos_ipv4_address from a buffer |
| 686 | * |
| 687 | * \param buf Buffer to read from |
| 688 | * \param val bcmos_ipv4_address to read |
| 689 | * |
| 690 | * \return BCMOS_TRUE if read successful |
| 691 | */ |
| 692 | static inline bcmos_bool bcmolt_buf_read_ipv4_address(bcmolt_buf *buf, bcmos_ipv4_address *val) |
| 693 | { |
| 694 | return bcmolt_buf_read(buf, val->u8, sizeof(bcmos_ipv4_address)); |
| 695 | } |
| 696 | |
| 697 | /** Writes a bcmos_ipv4_address to a buffer |
| 698 | * |
| 699 | * \param buf Buffer to write to |
| 700 | * \param val bcmos_ipv4_address to write |
| 701 | * |
| 702 | * \return BCMOS_TRUE if write successful |
| 703 | */ |
| 704 | static inline bcmos_bool bcmolt_buf_write_ipv4_address(bcmolt_buf *buf, bcmos_ipv4_address val) |
| 705 | { |
| 706 | return bcmolt_buf_write(buf, val.u8, sizeof(bcmos_ipv4_address)); |
| 707 | } |
| 708 | |
| 709 | /** Reads a bcmos_ipv6_address from a buffer |
| 710 | * |
| 711 | * \param buf Buffer to read from |
| 712 | * \param val bcmos_ipv6_address to read |
| 713 | * |
| 714 | * \return BCMOS_TRUE if read successful |
| 715 | */ |
| 716 | static inline bcmos_bool bcmolt_buf_read_ipv6_address(bcmolt_buf *buf, bcmos_ipv6_address *val) |
| 717 | { |
| 718 | return bcmolt_buf_read(buf, *val, sizeof(bcmos_ipv6_address)); |
| 719 | } |
| 720 | |
| 721 | /** Writes a bcmos_ipv6_address to a buffer |
| 722 | * |
| 723 | * \param buf Buffer to write to |
| 724 | * \param val bcmos_ipv6_address to write |
| 725 | * |
| 726 | * \return BCMOS_TRUE if write successful |
| 727 | */ |
| 728 | static inline bcmos_bool bcmolt_buf_write_ipv6_address(bcmolt_buf *buf, bcmos_ipv6_address val) |
| 729 | { |
| 730 | return bcmolt_buf_write(buf, val, sizeof(bcmos_ipv6_address)); |
| 731 | } |
| 732 | |
| 733 | #endif /* BCMOLT_BUF_H_ */ |