OLE Developer Guide  Latest Git
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
utils.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * utils.h
17  * Copyright (C) 2015 Simon Newton
18  */
19 
20 #ifndef FIRMWARE_SRC_UTILS_H_
21 #define FIRMWARE_SRC_UTILS_H_
22 
28 #include <stdint.h>
29 
30 #if HAVE_CONFIG_H
31 // We're in the test environment
32 #include <config.h>
33 #else
34 #include <machine/endian.h>
35 #endif
36 
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
39 #endif
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
50 static inline uint8_t ShortLSB(uint16_t s) {
51  return s & 0xff;
52 }
53 
59 static inline uint8_t ShortMSB(uint16_t s) {
60  return s >> 8;
61 }
62 
69 static inline uint16_t JoinShort(uint8_t upper, uint8_t lower) {
70  return (upper << 8) + lower;
71 }
72 
78 static inline uint8_t UInt32Byte0(uint32_t s) {
79  return s >> 24;
80 }
81 
87 static inline uint8_t UInt32Byte1(uint32_t s) {
88  return s >> 16;
89 }
90 
96 static inline uint8_t UInt32Byte2(uint32_t s) {
97  return s >> 8;
98 }
99 
105 static inline uint8_t UInt32Byte3(uint32_t s) {
106  return s & 0xff;
107 }
108 
114 static inline uint32_t ExtractUInt16(const uint8_t *ptr) {
115  return (ptr[0] << 8) + ptr[1];
116 }
117 
123 static inline uint32_t ExtractUInt32(const uint8_t *ptr) {
124  return (ptr[0] << 24) + (ptr[1] << 16) + (ptr[2] << 8) + ptr[3];
125 }
126 
133 static inline uint8_t* PushUInt16(uint8_t *ptr, uint16_t value) {
134  *ptr++ = (value >> 8);
135  *ptr++ = value & 0xff;
136  return ptr;
137 }
138 
145 static inline uint8_t* PushUInt32(uint8_t *ptr, uint32_t value) {
146  *ptr++ = (value >> 24);
147  *ptr++ = (value >> 16);
148  *ptr++ = (value >> 8);
149  *ptr++ = value & 0xff;
150  return ptr;
151 }
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif // FIRMWARE_SRC_UTILS_H_
static uint8_t * PushUInt16(uint8_t *ptr, uint16_t value)
Copy a 16-bit value to a memory location in network-byte order.
Definition: utils.h:133
static uint8_t UInt32Byte2(uint32_t s)
Return the second-lowest byte from a 32bit value.
Definition: utils.h:96
static uint8_t UInt32Byte3(uint32_t s)
Return the lowest byte from a 32bit value.
Definition: utils.h:105
static uint32_t ExtractUInt32(const uint8_t *ptr)
Extract a uint32_t in network-byte order from a memory location.
Definition: utils.h:123
static uint16_t JoinShort(uint8_t upper, uint8_t lower)
Combine two 8-bit values to a 16-bit value.
Definition: utils.h:69
static uint8_t ShortLSB(uint16_t s)
Return the LSB from a 16bit value.
Definition: utils.h:50
static uint32_t ExtractUInt16(const uint8_t *ptr)
Extract a uint16_t in network-byte order from a memory location.
Definition: utils.h:114
static uint8_t * PushUInt32(uint8_t *ptr, uint32_t value)
Copy a 32-bit value to a memory location in network-byte order.
Definition: utils.h:145
static uint8_t UInt32Byte0(uint32_t s)
Return the MSB from a 32bit value.
Definition: utils.h:78
static uint8_t UInt32Byte1(uint32_t s)
Return the second-highest byte from a 32bit value.
Definition: utils.h:87
static uint8_t ShortMSB(uint16_t s)
Return the MSB from a 16bit value.
Definition: utils.h:59