Add jaylink_device_get_usb_bus_ports()
[libjaylink.git] / libjaylink / buffer.c
blob527c25ea49a93e35a68686151389eca797841c84
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdint.h>
21 #include <string.h>
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "libjaylink-internal.h"
28 /**
29 * @file
31 * Buffer helper functions.
34 /**
35 * Write a 16-bit unsigned integer value to a buffer.
37 * The value is stored in the buffer in device byte order.
39 * @param[out] buffer Buffer to write the value into.
40 * @param[in] value Value to write into the buffer in host byte order.
41 * @param[in] offset Offset of the value within the buffer in bytes.
43 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
44 size_t offset)
47 * Store the value in the buffer and swap byte order depending on the
48 * host byte order.
50 #ifdef WORDS_BIGENDIAN
51 buffer[offset + 0] = value;
52 buffer[offset + 1] = value >> 8;
53 #else
54 memcpy(buffer + offset, &value, sizeof(value));
55 #endif
58 /**
59 * Read a 16-bit unsigned integer value from a buffer.
61 * The value in the buffer is expected to be stored in device byte order.
63 * @param[in] buffer Buffer to read the value from.
64 * @param[in] offset Offset of the value within the buffer in bytes.
66 * @return The value read from the buffer in host byte order.
68 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset)
70 uint16_t value;
73 * Read the value from the buffer and swap byte order depending on the
74 * host byte order.
76 #ifdef WORDS_BIGENDIAN
77 value = (((uint16_t)buffer[offset + 1])) | \
78 (((uint16_t)buffer[offset + 0]) << 8);
79 #else
80 memcpy(&value, buffer + offset, sizeof(value));
81 #endif
83 return value;
86 /**
87 * Write a 32-bit unsigned integer value to a buffer.
89 * The value is stored in the buffer in device byte order.
91 * @param[out] buffer Buffer to write the value into.
92 * @param[in] value Value to write into the buffer in host byte order.
93 * @param[in] offset Offset of the value within the buffer in bytes.
95 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
96 size_t offset)
99 * Store the value in the buffer and swap byte order depending on the
100 * host byte order.
102 #ifdef WORDS_BIGENDIAN
103 buffer[offset + 0] = value;
104 buffer[offset + 1] = value >> 8;
105 buffer[offset + 2] = value >> 16;
106 buffer[offset + 3] = value >> 24;
107 #else
108 memcpy(buffer + offset, &value, sizeof(value));
109 #endif
113 * Read a 32-bit unsigned integer value from a buffer.
115 * The value in the buffer is expected to be stored in device byte order.
117 * @param[in] buffer Buffer to read the value from.
118 * @param[in] offset Offset of the value within the buffer in bytes.
120 * @return The value read from the buffer in host byte order.
122 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset)
124 uint32_t value;
127 * Read the value from the buffer and swap byte order depending on the
128 * host byte order.
130 #ifdef WORDS_BIGENDIAN
131 value = (((uint32_t)buffer[offset + 3])) | \
132 (((uint32_t)buffer[offset + 2]) << 8) | \
133 (((uint32_t)buffer[offset + 1]) << 16) | \
134 (((uint32_t)buffer[offset + 0]) << 24);
135 #else
136 memcpy(&value, buffer + offset, sizeof(value));
137 #endif
139 return value;