2 * Copyright © 2010-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: LGPL-2.1+
17 # include <winsock2.h>
19 # include <arpa/inet.h>
26 #if defined(HAVE_BYTESWAP_H)
27 # include <byteswap.h>
30 #if defined(__APPLE__)
31 #include <libkern/OSByteOrder.h>
32 #define bswap_16 OSSwapInt16
33 #define bswap_32 OSSwapInt32
34 #define bswap_64 OSSwapInt64
38 # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10)
39 # if GCC_VERSION >= 430
40 // Since GCC >= 4.30, GCC provides __builtin_bswapXX() alternatives so we switch to them
42 # define bswap_32 __builtin_bswap32
45 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
46 # define bswap_32 _byteswap_ulong
47 # define bswap_16 _byteswap_ushort
50 #if !defined(bswap_16)
51 # warning "Fallback on C functions for bswap_16"
52 static inline uint16_t bswap_16(uint16_t x
)
54 return (x
>> 8) | (x
<< 8);
58 #if !defined(bswap_32)
59 # warning "Fallback on C functions for bswap_32"
60 static inline uint32_t bswap_32(uint32_t x
)
62 return (bswap_16(x
& 0xffff) << 16) | (bswap_16(x
>> 16));
66 /* Sets many bits from a single byte value (all 8 bits of the byte value are
68 void modbus_set_bits_from_byte(uint8_t *dest
, int idx
, const uint8_t value
)
72 for (i
=0; i
< 8; i
++) {
73 dest
[idx
+i
] = (value
& (1 << i
)) ? 1 : 0;
77 /* Sets many bits from a table of bytes (only the bits between idx and
78 idx + nb_bits are set) */
79 void modbus_set_bits_from_bytes(uint8_t *dest
, int idx
, unsigned int nb_bits
,
80 const uint8_t *tab_byte
)
85 for (i
= idx
; i
< idx
+ nb_bits
; i
++) {
86 dest
[i
] = tab_byte
[(i
- idx
) / 8] & (1 << shift
) ? 1 : 0;
87 /* gcc doesn't like: shift = (++shift) % 8; */
93 /* Gets the byte value from many bits.
94 To obtain a full byte, set nb_bits to 8. */
95 uint8_t modbus_get_byte_from_bits(const uint8_t *src
, int idx
,
102 /* Assert is ignored if NDEBUG is set */
107 for (i
=0; i
< nb_bits
; i
++) {
108 value
|= (src
[idx
+i
] << i
);
114 /* Get a float from 4 bytes (Modbus) without any conversion (ABCD) */
115 float modbus_get_float_abcd(const uint16_t *src
)
120 i
= ntohl(((uint32_t)src
[0] << 16) + src
[1]);
121 memcpy(&f
, &i
, sizeof(float));
126 /* Get a float from 4 bytes (Modbus) in inversed format (DCBA) */
127 float modbus_get_float_dcba(const uint16_t *src
)
132 i
= ntohl(bswap_32((((uint32_t)src
[0]) << 16) + src
[1]));
133 memcpy(&f
, &i
, sizeof(float));
138 /* Get a float from 4 bytes (Modbus) with swapped bytes (BADC) */
139 float modbus_get_float_badc(const uint16_t *src
)
144 i
= ntohl((uint32_t)(bswap_16(src
[0]) << 16) + bswap_16(src
[1]));
145 memcpy(&f
, &i
, sizeof(float));
150 /* Get a float from 4 bytes (Modbus) with swapped words (CDAB) */
151 float modbus_get_float_cdab(const uint16_t *src
)
156 i
= ntohl((((uint32_t)src
[1]) << 16) + src
[0]);
157 memcpy(&f
, &i
, sizeof(float));
162 /* DEPRECATED - Get a float from 4 bytes in sort of Modbus format */
163 float modbus_get_float(const uint16_t *src
)
168 i
= (((uint32_t)src
[1]) << 16) + src
[0];
169 memcpy(&f
, &i
, sizeof(float));
174 /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
175 void modbus_set_float_abcd(float f
, uint16_t *dest
)
179 memcpy(&i
, &f
, sizeof(uint32_t));
181 dest
[0] = (uint16_t)(i
>> 16);
182 dest
[1] = (uint16_t)i
;
185 /* Set a float to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
186 void modbus_set_float_dcba(float f
, uint16_t *dest
)
190 memcpy(&i
, &f
, sizeof(uint32_t));
191 i
= bswap_32(htonl(i
));
192 dest
[0] = (uint16_t)(i
>> 16);
193 dest
[1] = (uint16_t)i
;
196 /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
197 void modbus_set_float_badc(float f
, uint16_t *dest
)
201 memcpy(&i
, &f
, sizeof(uint32_t));
203 dest
[0] = (uint16_t)bswap_16(i
>> 16);
204 dest
[1] = (uint16_t)bswap_16(i
& 0xFFFF);
207 /* Set a float to 4 bytes for Modbus with word swap conversion (CDAB) */
208 void modbus_set_float_cdab(float f
, uint16_t *dest
)
212 memcpy(&i
, &f
, sizeof(uint32_t));
214 dest
[0] = (uint16_t)i
;
215 dest
[1] = (uint16_t)(i
>> 16);
218 /* DEPRECATED - Set a float to 4 bytes in a sort of Modbus format! */
219 void modbus_set_float(float f
, uint16_t *dest
)
223 memcpy(&i
, &f
, sizeof(uint32_t));
224 dest
[0] = (uint16_t)i
;
225 dest
[1] = (uint16_t)(i
>> 16);