5735b406
Peter M. Groen
Revert "Setting u...
|
1
2
3
4
5
|
/*
* Copyright © 2010-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
|
500c015a
Peter M. Groen
Setting up workin...
|
6
7
8
|
#include <stdlib.h>
|
5735b406
Peter M. Groen
Revert "Setting u...
|
9
10
11
12
13
|
#ifndef _MSC_VER
# include <stdint.h>
#else
# include "stdint.h"
#endif
|
500c015a
Peter M. Groen
Setting up workin...
|
14
15
16
|
#include <string.h>
#include <assert.h>
|
5735b406
Peter M. Groen
Revert "Setting u...
|
17
18
19
20
21
22
23
|
#if defined(_WIN32)
# include <winsock2.h>
#else
# include <arpa/inet.h>
#endif
|
500c015a
Peter M. Groen
Setting up workin...
|
24
25
26
27
28
29
30
31
|
#include <config.h>
#include "modbus.h"
#if defined(HAVE_BYTESWAP_H)
# include <byteswap.h>
#endif
|
5735b406
Peter M. Groen
Revert "Setting u...
|
32
33
34
35
36
37
38
|
#if defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define bswap_16 OSSwapInt16
# define bswap_32 OSSwapInt32
# define bswap_64 OSSwapInt64
#endif
|
500c015a
Peter M. Groen
Setting up workin...
|
39
40
41
42
43
44
45
46
47
|
#if defined(__GNUC__)
# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10)
# if GCC_VERSION >= 430
// Since GCC >= 4.30, GCC provides __builtin_bswapXX() alternatives so we switch to them
# undef bswap_32
# define bswap_32 __builtin_bswap32
# endif
#endif
|
5735b406
Peter M. Groen
Revert "Setting u...
|
48
49
50
51
52
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
# define bswap_32 _byteswap_ulong
# define bswap_16 _byteswap_ushort
#endif
|
500c015a
Peter M. Groen
Setting up workin...
|
53
|
#if !defined(__CYGWIN__) && !defined(bswap_16)
|
5735b406
Peter M. Groen
Revert "Setting u...
|
54
|
# warning "Fallback on C functions for bswap_16"
|
500c015a
Peter M. Groen
Setting up workin...
|
55
56
57
58
59
60
61
|
static inline uint16_t bswap_16(uint16_t x)
{
return (x >> 8) | (x << 8);
}
#endif
#if !defined(bswap_32)
|
5735b406
Peter M. Groen
Revert "Setting u...
|
62
|
# warning "Fallback on C functions for bswap_32"
|
500c015a
Peter M. Groen
Setting up workin...
|
63
64
65
66
67
68
69
70
|
static inline uint32_t bswap_32(uint32_t x)
{
return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
}
#endif
/* Sets many bits from a single byte value (all 8 bits of the byte value are
set) */
|
5735b406
Peter M. Groen
Revert "Setting u...
|
71
|
void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
|
500c015a
Peter M. Groen
Setting up workin...
|
72
73
74
|
{
int i;
|
5735b406
Peter M. Groen
Revert "Setting u...
|
75
|
for (i=0; i < 8; i++) {
|
500c015a
Peter M. Groen
Setting up workin...
|
76
77
78
79
80
81
|
dest[idx+i] = (value & (1 << i)) ? 1 : 0;
}
}
/* Sets many bits from a table of bytes (only the bits between idx and
idx + nb_bits are set) */
|
5735b406
Peter M. Groen
Revert "Setting u...
|
82
83
|
void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
const uint8_t *tab_byte)
|
500c015a
Peter M. Groen
Setting up workin...
|
84
85
86
87
|
{
unsigned int i;
int shift = 0;
|
5735b406
Peter M. Groen
Revert "Setting u...
|
88
|
for (i = idx; i < idx + nb_bits; i++) {
|
500c015a
Peter M. Groen
Setting up workin...
|
89
90
91
92
93
94
95
96
97
|
dest[i] = tab_byte[(i - idx) / 8] & (1 << shift) ? 1 : 0;
/* gcc doesn't like: shift = (++shift) % 8; */
shift++;
shift %= 8;
}
}
/* Gets the byte value from many bits.
To obtain a full byte, set nb_bits to 8. */
|
5735b406
Peter M. Groen
Revert "Setting u...
|
98
99
|
uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
unsigned int nb_bits)
|
500c015a
Peter M. Groen
Setting up workin...
|
100
101
102
103
|
{
unsigned int i;
uint8_t value = 0;
|
5735b406
Peter M. Groen
Revert "Setting u...
|
104
|
if (nb_bits > 8) {
|
500c015a
Peter M. Groen
Setting up workin...
|
105
106
107
108
109
|
/* Assert is ignored if NDEBUG is set */
assert(nb_bits < 8);
nb_bits = 8;
}
|
5735b406
Peter M. Groen
Revert "Setting u...
|
110
|
for (i=0; i < nb_bits; i++) {
|
500c015a
Peter M. Groen
Setting up workin...
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
value |= (src[idx+i] << i);
}
return value;
}
/* Get a float from 4 bytes (Modbus) without any conversion (ABCD) */
float modbus_get_float_abcd(const uint16_t *src)
{
float f;
uint32_t i;
i = ntohl(((uint32_t)src[0] << 16) + src[1]);
memcpy(&f, &i, sizeof(float));
return f;
}
/* Get a float from 4 bytes (Modbus) in inversed format (DCBA) */
float modbus_get_float_dcba(const uint16_t *src)
{
float f;
uint32_t i;
i = ntohl(bswap_32((((uint32_t)src[0]) << 16) + src[1]));
memcpy(&f, &i, sizeof(float));
return f;
}
/* Get a float from 4 bytes (Modbus) with swapped bytes (BADC) */
float modbus_get_float_badc(const uint16_t *src)
{
float f;
uint32_t i;
i = ntohl((uint32_t)(bswap_16(src[0]) << 16) + bswap_16(src[1]));
memcpy(&f, &i, sizeof(float));
return f;
}
/* Get a float from 4 bytes (Modbus) with swapped words (CDAB) */
float modbus_get_float_cdab(const uint16_t *src)
{
float f;
uint32_t i;
i = ntohl((((uint32_t)src[1]) << 16) + src[0]);
memcpy(&f, &i, sizeof(float));
return f;
}
/* DEPRECATED - Get a float from 4 bytes in sort of Modbus format */
float modbus_get_float(const uint16_t *src)
{
float f;
uint32_t i;
i = (((uint32_t)src[1]) << 16) + src[0];
memcpy(&f, &i, sizeof(float));
return f;
}
/* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
void modbus_set_float_abcd(float f, uint16_t *dest)
{
uint32_t i;
memcpy(&i, &f, sizeof(uint32_t));
i = htonl(i);
dest[0] = (uint16_t)(i >> 16);
dest[1] = (uint16_t)i;
}
/* Set a float to 4 bytes for Modbus with byte and word swap conversion (DCBA) */
void modbus_set_float_dcba(float f, uint16_t *dest)
{
uint32_t i;
memcpy(&i, &f, sizeof(uint32_t));
i = bswap_32(htonl(i));
dest[0] = (uint16_t)(i >> 16);
dest[1] = (uint16_t)i;
}
/* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
void modbus_set_float_badc(float f, uint16_t *dest)
{
uint32_t i;
memcpy(&i, &f, sizeof(uint32_t));
i = htonl(i);
dest[0] = (uint16_t)bswap_16(i >> 16);
dest[1] = (uint16_t)bswap_16(i & 0xFFFF);
}
/* Set a float to 4 bytes for Modbus with word swap conversion (CDAB) */
void modbus_set_float_cdab(float f, uint16_t *dest)
{
uint32_t i;
memcpy(&i, &f, sizeof(uint32_t));
i = htonl(i);
dest[0] = (uint16_t)i;
dest[1] = (uint16_t)(i >> 16);
}
/* DEPRECATED - Set a float to 4 bytes in a sort of Modbus format! */
void modbus_set_float(float f, uint16_t *dest)
{
uint32_t i;
memcpy(&i, &f, sizeof(uint32_t));
dest[0] = (uint16_t)i;
dest[1] = (uint16_t)(i >> 16);
}
|