Add jaylink_get_device()
[libjaylink.git] / libjaylink / buffer.c
blob4031a9630a633eb37c6a92a12fa357625ae6451b
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 #include "config.h"
24 #include "libjaylink-internal.h"
26 /**
27 * @file
29 * Buffer helper functions.
32 /**
33 * Write a 16-bit unsigned integer value to a buffer.
35 * The value is stored in the buffer in device byte order.
37 * @param buffer Buffer to write the value into.
38 * @param value Value to write into the buffer in host byte order.
39 * @param offset Offset of the value within the buffer in bytes.
41 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value, size_t offset)
44 * Store the value in the buffer and swap byte order depending on the
45 * host byte order.
47 #ifdef WORDS_BIGENDIAN
48 buffer[offset + 0] = value;
49 buffer[offset + 1] = value >> 8;
50 #else
51 memcpy(buffer + offset, &value, sizeof(value));
52 #endif
55 /**
56 * Read a 16-bit unsigned integer value from a buffer.
58 * The value in the buffer is expected to be stored in device byte order.
60 * @param buffer Buffer to read the value from.
61 * @param offset Offset of the value within the buffer in bytes.
63 * @return The value read from the buffer in host byte order.
65 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset)
67 uint16_t value;
70 * Read the value from the buffer and swap byte order depending on the
71 * host byte order.
73 #ifdef WORDS_BIGENDIAN
74 value = (((uint16_t)buffer[offset + 1])) | \
75 (((uint16_t)buffer[offset + 0]) << 8);
76 #else
77 memcpy(&value, buffer + offset, sizeof(value));
78 #endif
80 return value;
83 /**
84 * Write a 32-bit unsigned integer value to a buffer.
86 * The value is stored in the buffer in device byte order.
88 * @param buffer Buffer to write the value into.
89 * @param value Value to write into the buffer in host byte order.
90 * @param offset Offset of the value within the buffer in bytes.
92 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value, size_t offset)
95 * Store the value in the buffer and swap byte order depending on the
96 * host byte order.
98 #ifdef WORDS_BIGENDIAN
99 buffer[offset + 0] = value;
100 buffer[offset + 1] = value >> 8;
101 buffer[offset + 2] = value >> 16;
102 buffer[offset + 3] = value >> 24;
103 #else
104 memcpy(buffer + offset, &value, sizeof(value));
105 #endif
109 * Read a 32-bit unsigned integer value from a buffer.
111 * The value in the buffer is expected to be stored in device byte order.
113 * @param buffer Buffer to read the value from.
114 * @param offset Offset of the value within the buffer in bytes.
116 * @return The value read from the buffer in host byte order.
118 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset)
120 uint32_t value;
123 * Read the value from the buffer and swap byte order depending on the
124 * host byte order.
126 #ifdef WORDS_BIGENDIAN
127 value = (((uint32_t)buffer[offset + 3])) | \
128 (((uint32_t)buffer[offset + 2]) << 8) | \
129 (((uint32_t)buffer[offset + 1]) << 16) | \
130 (((uint32_t)buffer[offset + 0]) << 24);
131 #else
132 memcpy(&value, buffer + offset, sizeof(value));
133 #endif
135 return value;