Commit dd45f19a6efc20d6032b424a067294086a315899

Authored by Stéphane Raimbault
1 parent d5512d9b

Add .clang-format and format code (closes #394)

Thank you @pboettch for initial proposal.
.clang-format 0 → 100644
  1 +---
  2 +BasedOnStyle: LLVM
  3 +AlignArrayOfStructures: Left
  4 +AlignOperands: true
  5 +AlignConsecutiveAssignments: false
  6 +AlignConsecutiveMacros: true
  7 +AlignEscapedNewlines: Left
  8 +AlignTrailingComments: true
  9 +AllowShortBlocksOnASingleLine: false
  10 +AllowShortCaseLabelsOnASingleLine: false
  11 +AllowShortEnumsOnASingleLine: false
  12 +AllowShortFunctionsOnASingleLine: None
  13 +AllowShortIfStatementsOnASingleLine: Never
  14 +BreakBeforeBraces: Custom
  15 +BraceWrapping:
  16 + AfterClass: false
  17 + AfterControlStatement: false
  18 + AfterEnum: false
  19 + AfterFunction: true
  20 + AfterNamespace: true
  21 + AfterObjCDeclaration: true
  22 + AfterStruct: false
  23 + AfterUnion: false
  24 + AfterExternBlock: false
  25 + BeforeCatch: false
  26 + BeforeElse: false
  27 + IndentBraces: false
  28 + SplitEmptyFunction: true
  29 + SplitEmptyRecord: true
  30 + SplitEmptyNamespace: true
  31 +BinPackArguments: false
  32 +BinPackParameters: false
  33 +ColumnLimit: 90
  34 +ConstructorInitializerAllOnOneLineOrOnePerLine: true
  35 +IncludeBlocks: Preserve
  36 +IndentWidth: 4
  37 +ObjCBlockIndentWidth: 4
  38 +PointerAlignment: Right
  39 +ReferenceAlignment: Right
  40 +SpaceAfterCStyleCast: true
  41 +SpaceAfterTemplateKeyword: true
  42 +SpaceBeforeAssignmentOperators: true
  43 +SpaceBeforeCtorInitializerColon: true
  44 +SpaceBeforeInheritanceColon: true
  45 +SpaceBeforeParens: ControlStatementsExceptForEachMacros
  46 +SpaceBeforeRangeBasedForLoopColon: true
  47 +SpaceInEmptyParentheses: false
  48 +SpacesBeforeTrailingComments: 1
  49 +SpacesInAngles: false
  50 +SpacesInContainerLiterals: false
  51 +SpacesInCStyleCastParentheses: false
  52 +SpacesInParentheses: false
  53 +SpacesInSquareBrackets: false
  54 +SeparateDefinitionBlocks: Always
  55 +UseTab: Never
  56 +PPIndentWidth: 2
src/modbus-data.c
@@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
  9 +// clang-format off
9 #ifndef _MSC_VER 10 #ifndef _MSC_VER
10 # include <stdint.h> 11 # include <stdint.h>
11 #else 12 #else
@@ -69,6 +70,7 @@ static inline uint32_t bswap_32(uint32_t x) @@ -69,6 +70,7 @@ static inline uint32_t bswap_32(uint32_t x)
69 return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16)); 70 return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
70 } 71 }
71 #endif 72 #endif
  73 +// clang-format on
72 74
73 /* Sets many bits from a single byte value (all 8 bits of the byte value are 75 /* Sets many bits from a single byte value (all 8 bits of the byte value are
74 set) */ 76 set) */
@@ -76,14 +78,16 @@ void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value) @@ -76,14 +78,16 @@ void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
76 { 78 {
77 int i; 79 int i;
78 80
79 - for (i=0; i < 8; i++) {  
80 - dest[idx+i] = (value & (1 << i)) ? 1 : 0; 81 + for (i = 0; i < 8; i++) {
  82 + dest[idx + i] = (value & (1 << i)) ? 1 : 0;
81 } 83 }
82 } 84 }
83 85
84 /* Sets many bits from a table of bytes (only the bits between idx and 86 /* Sets many bits from a table of bytes (only the bits between idx and
85 idx + nb_bits are set) */ 87 idx + nb_bits are set) */
86 -void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits, 88 +void modbus_set_bits_from_bytes(uint8_t *dest,
  89 + int idx,
  90 + unsigned int nb_bits,
87 const uint8_t *tab_byte) 91 const uint8_t *tab_byte)
88 { 92 {
89 unsigned int i; 93 unsigned int i;
@@ -99,8 +103,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits, @@ -99,8 +103,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
99 103
100 /* Gets the byte value from many bits. 104 /* Gets the byte value from many bits.
101 To obtain a full byte, set nb_bits to 8. */ 105 To obtain a full byte, set nb_bits to 8. */
102 -uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,  
103 - unsigned int nb_bits) 106 +uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits)
104 { 107 {
105 unsigned int i; 108 unsigned int i;
106 uint8_t value = 0; 109 uint8_t value = 0;
@@ -111,8 +114,8 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, @@ -111,8 +114,8 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
111 nb_bits = 8; 114 nb_bits = 8;
112 } 115 }
113 116
114 - for (i=0; i < nb_bits; i++) {  
115 - value |= (src[idx+i] << i); 117 + for (i = 0; i < nb_bits; i++) {
  118 + value |= (src[idx + i] << i);
116 } 119 }
117 120
118 return value; 121 return value;
@@ -130,10 +133,7 @@ float modbus_get_float_abcd(const uint16_t *src) @@ -130,10 +133,7 @@ float modbus_get_float_abcd(const uint16_t *src)
130 c = (src[1] >> 8) & 0xFF; 133 c = (src[1] >> 8) & 0xFF;
131 d = (src[1] >> 0) & 0xFF; 134 d = (src[1] >> 0) & 0xFF;
132 135
133 - i = (a << 24) |  
134 - (b << 16) |  
135 - (c << 8) |  
136 - (d << 0); 136 + i = (a << 24) | (b << 16) | (c << 8) | (d << 0);
137 memcpy(&f, &i, 4); 137 memcpy(&f, &i, 4);
138 138
139 return f; 139 return f;
@@ -151,10 +151,7 @@ float modbus_get_float_dcba(const uint16_t *src) @@ -151,10 +151,7 @@ float modbus_get_float_dcba(const uint16_t *src)
151 c = (src[1] >> 8) & 0xFF; 151 c = (src[1] >> 8) & 0xFF;
152 d = (src[1] >> 0) & 0xFF; 152 d = (src[1] >> 0) & 0xFF;
153 153
154 - i = (d << 24) |  
155 - (c << 16) |  
156 - (b << 8) |  
157 - (a << 0); 154 + i = (d << 24) | (c << 16) | (b << 8) | (a << 0);
158 memcpy(&f, &i, 4); 155 memcpy(&f, &i, 4);
159 156
160 return f; 157 return f;
@@ -172,10 +169,7 @@ float modbus_get_float_badc(const uint16_t *src) @@ -172,10 +169,7 @@ float modbus_get_float_badc(const uint16_t *src)
172 c = (src[1] >> 8) & 0xFF; 169 c = (src[1] >> 8) & 0xFF;
173 d = (src[1] >> 0) & 0xFF; 170 d = (src[1] >> 0) & 0xFF;
174 171
175 - i = (b << 24) |  
176 - (a << 16) |  
177 - (d << 8) |  
178 - (c << 0); 172 + i = (b << 24) | (a << 16) | (d << 8) | (c << 0);
179 memcpy(&f, &i, 4); 173 memcpy(&f, &i, 4);
180 174
181 return f; 175 return f;
@@ -193,10 +187,7 @@ float modbus_get_float_cdab(const uint16_t *src) @@ -193,10 +187,7 @@ float modbus_get_float_cdab(const uint16_t *src)
193 c = (src[1] >> 8) & 0xFF; 187 c = (src[1] >> 8) & 0xFF;
194 d = (src[1] >> 0) & 0xFF; 188 d = (src[1] >> 0) & 0xFF;
195 189
196 - i = (c << 24) |  
197 - (d << 16) |  
198 - (a << 8) |  
199 - (b << 0); 190 + i = (c << 24) | (d << 16) | (a << 8) | (b << 0);
200 memcpy(&f, &i, 4); 191 memcpy(&f, &i, 4);
201 192
202 return f; 193 return f;
@@ -208,18 +199,17 @@ float modbus_get_float(const uint16_t *src) @@ -208,18 +199,17 @@ float modbus_get_float(const uint16_t *src)
208 float f; 199 float f;
209 uint32_t i; 200 uint32_t i;
210 201
211 - i = (((uint32_t)src[1]) << 16) + src[0]; 202 + i = (((uint32_t) src[1]) << 16) + src[0];
212 memcpy(&f, &i, sizeof(float)); 203 memcpy(&f, &i, sizeof(float));
213 204
214 return f; 205 return f;
215 -  
216 } 206 }
217 207
218 /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */ 208 /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
219 void modbus_set_float_abcd(float f, uint16_t *dest) 209 void modbus_set_float_abcd(float f, uint16_t *dest)
220 { 210 {
221 uint32_t i; 211 uint32_t i;
222 - uint8_t *out = (uint8_t*) dest; 212 + uint8_t *out = (uint8_t *) dest;
223 uint8_t a, b, c, d; 213 uint8_t a, b, c, d;
224 214
225 memcpy(&i, &f, sizeof(uint32_t)); 215 memcpy(&i, &f, sizeof(uint32_t));
@@ -238,7 +228,7 @@ void modbus_set_float_abcd(float f, uint16_t *dest) @@ -238,7 +228,7 @@ void modbus_set_float_abcd(float f, uint16_t *dest)
238 void modbus_set_float_dcba(float f, uint16_t *dest) 228 void modbus_set_float_dcba(float f, uint16_t *dest)
239 { 229 {
240 uint32_t i; 230 uint32_t i;
241 - uint8_t *out = (uint8_t*) dest; 231 + uint8_t *out = (uint8_t *) dest;
242 uint8_t a, b, c, d; 232 uint8_t a, b, c, d;
243 233
244 memcpy(&i, &f, sizeof(uint32_t)); 234 memcpy(&i, &f, sizeof(uint32_t));
@@ -251,14 +241,13 @@ void modbus_set_float_dcba(float f, uint16_t *dest) @@ -251,14 +241,13 @@ void modbus_set_float_dcba(float f, uint16_t *dest)
251 out[1] = c; 241 out[1] = c;
252 out[2] = b; 242 out[2] = b;
253 out[3] = a; 243 out[3] = a;
254 -  
255 } 244 }
256 245
257 /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */ 246 /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
258 void modbus_set_float_badc(float f, uint16_t *dest) 247 void modbus_set_float_badc(float f, uint16_t *dest)
259 { 248 {
260 uint32_t i; 249 uint32_t i;
261 - uint8_t *out = (uint8_t*) dest; 250 + uint8_t *out = (uint8_t *) dest;
262 uint8_t a, b, c, d; 251 uint8_t a, b, c, d;
263 252
264 memcpy(&i, &f, sizeof(uint32_t)); 253 memcpy(&i, &f, sizeof(uint32_t));
@@ -277,7 +266,7 @@ void modbus_set_float_badc(float f, uint16_t *dest) @@ -277,7 +266,7 @@ void modbus_set_float_badc(float f, uint16_t *dest)
277 void modbus_set_float_cdab(float f, uint16_t *dest) 266 void modbus_set_float_cdab(float f, uint16_t *dest)
278 { 267 {
279 uint32_t i; 268 uint32_t i;
280 - uint8_t *out = (uint8_t*) dest; 269 + uint8_t *out = (uint8_t *) dest;
281 uint8_t a, b, c, d; 270 uint8_t a, b, c, d;
282 271
283 memcpy(&i, &f, sizeof(uint32_t)); 272 memcpy(&i, &f, sizeof(uint32_t));
@@ -298,6 +287,6 @@ void modbus_set_float(float f, uint16_t *dest) @@ -298,6 +287,6 @@ void modbus_set_float(float f, uint16_t *dest)
298 uint32_t i; 287 uint32_t i;
299 288
300 memcpy(&i, &f, sizeof(uint32_t)); 289 memcpy(&i, &f, sizeof(uint32_t));
301 - dest[0] = (uint16_t)i;  
302 - dest[1] = (uint16_t)(i >> 16); 290 + dest[0] = (uint16_t) i;
  291 + dest[1] = (uint16_t) (i >> 16);
303 } 292 }
src/modbus-private.h
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 #ifndef MODBUS_PRIVATE_H 7 #ifndef MODBUS_PRIVATE_H
8 #define MODBUS_PRIVATE_H 8 #define MODBUS_PRIVATE_H
9 9
  10 +// clang-format off
10 #ifndef _MSC_VER 11 #ifndef _MSC_VER
11 # include <stdint.h> 12 # include <stdint.h>
12 # include <sys/time.h> 13 # include <sys/time.h>
@@ -15,8 +16,9 @@ @@ -15,8 +16,9 @@
15 # include <time.h> 16 # include <time.h>
16 typedef int ssize_t; 17 typedef int ssize_t;
17 #endif 18 #endif
18 -#include <sys/types.h> 19 +// clang-format on
19 #include <config.h> 20 #include <config.h>
  21 +#include <sys/types.h>
20 22
21 #include "modbus.h" 23 #include "modbus.h"
22 24
@@ -36,11 +38,11 @@ MODBUS_BEGIN_DECLS @@ -36,11 +38,11 @@ MODBUS_BEGIN_DECLS
36 #define _MODBUS_EXCEPTION_RSP_LENGTH 5 38 #define _MODBUS_EXCEPTION_RSP_LENGTH 5
37 39
38 /* Timeouts in microsecond (0.5 s) */ 40 /* Timeouts in microsecond (0.5 s) */
39 -#define _RESPONSE_TIMEOUT 500000  
40 -#define _BYTE_TIMEOUT 500000 41 +#define _RESPONSE_TIMEOUT 500000
  42 +#define _BYTE_TIMEOUT 500000
41 43
42 typedef enum { 44 typedef enum {
43 - _MODBUS_BACKEND_TYPE_RTU=0, 45 + _MODBUS_BACKEND_TYPE_RTU = 0,
44 _MODBUS_BACKEND_TYPE_TCP 46 _MODBUS_BACKEND_TYPE_TCP
45 } modbus_backend_type_t; 47 } modbus_backend_type_t;
46 48
@@ -69,24 +71,25 @@ typedef struct _modbus_backend { @@ -69,24 +71,25 @@ typedef struct _modbus_backend {
69 unsigned int header_length; 71 unsigned int header_length;
70 unsigned int checksum_length; 72 unsigned int checksum_length;
71 unsigned int max_adu_length; 73 unsigned int max_adu_length;
72 - int (*set_slave) (modbus_t *ctx, int slave);  
73 - int (*build_request_basis) (modbus_t *ctx, int function, int addr,  
74 - int nb, uint8_t *req);  
75 - int (*build_response_basis) (sft_t *sft, uint8_t *rsp);  
76 - int (*prepare_response_tid) (const uint8_t *req, int *req_length);  
77 - int (*send_msg_pre) (uint8_t *req, int req_length);  
78 - ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);  
79 - int (*receive) (modbus_t *ctx, uint8_t *req);  
80 - ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);  
81 - int (*check_integrity) (modbus_t *ctx, uint8_t *msg,  
82 - const int msg_length);  
83 - int (*pre_check_confirmation) (modbus_t *ctx, const uint8_t *req,  
84 - const uint8_t *rsp, int rsp_length);  
85 - int (*connect) (modbus_t *ctx);  
86 - void (*close) (modbus_t *ctx);  
87 - int (*flush) (modbus_t *ctx);  
88 - int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);  
89 - void (*free) (modbus_t *ctx); 74 + int (*set_slave)(modbus_t *ctx, int slave);
  75 + int (*build_request_basis)(
  76 + modbus_t *ctx, int function, int addr, int nb, uint8_t *req);
  77 + int (*build_response_basis)(sft_t *sft, uint8_t *rsp);
  78 + int (*prepare_response_tid)(const uint8_t *req, int *req_length);
  79 + int (*send_msg_pre)(uint8_t *req, int req_length);
  80 + ssize_t (*send)(modbus_t *ctx, const uint8_t *req, int req_length);
  81 + int (*receive)(modbus_t *ctx, uint8_t *req);
  82 + ssize_t (*recv)(modbus_t *ctx, uint8_t *rsp, int rsp_length);
  83 + int (*check_integrity)(modbus_t *ctx, uint8_t *msg, const int msg_length);
  84 + int (*pre_check_confirmation)(modbus_t *ctx,
  85 + const uint8_t *req,
  86 + const uint8_t *rsp,
  87 + int rsp_length);
  88 + int (*connect)(modbus_t *ctx);
  89 + void (*close)(modbus_t *ctx);
  90 + int (*flush)(modbus_t *ctx);
  91 + int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
  92 + void (*free)(modbus_t *ctx);
90 } modbus_backend_t; 93 } modbus_backend_t;
91 94
92 struct _modbus { 95 struct _modbus {
@@ -114,4 +117,4 @@ size_t strlcpy(char *dest, const char *src, size_t dest_size); @@ -114,4 +117,4 @@ size_t strlcpy(char *dest, const char *src, size_t dest_size);
114 117
115 MODBUS_END_DECLS 118 MODBUS_END_DECLS
116 119
117 -#endif /* MODBUS_PRIVATE_H */ 120 +#endif /* MODBUS_PRIVATE_H */
src/modbus-rtu-private.h
@@ -19,11 +19,11 @@ @@ -19,11 +19,11 @@
19 #include <termios.h> 19 #include <termios.h>
20 #endif 20 #endif
21 21
22 -#define _MODBUS_RTU_HEADER_LENGTH 1  
23 -#define _MODBUS_RTU_PRESET_REQ_LENGTH 6  
24 -#define _MODBUS_RTU_PRESET_RSP_LENGTH 2 22 +#define _MODBUS_RTU_HEADER_LENGTH 1
  23 +#define _MODBUS_RTU_PRESET_REQ_LENGTH 6
  24 +#define _MODBUS_RTU_PRESET_RSP_LENGTH 2
25 25
26 -#define _MODBUS_RTU_CHECKSUM_LENGTH 2 26 +#define _MODBUS_RTU_CHECKSUM_LENGTH 2
27 27
28 #if defined(_WIN32) 28 #if defined(_WIN32)
29 #if !defined(ENOTSUP) 29 #if !defined(ENOTSUP)
@@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
32 32
33 /* WIN32: struct containing serial handle and a receive buffer */ 33 /* WIN32: struct containing serial handle and a receive buffer */
34 #define PY_BUF_SIZE 512 34 #define PY_BUF_SIZE 512
  35 +
35 struct win32_ser { 36 struct win32_ser {
36 /* File handle */ 37 /* File handle */
37 HANDLE fd; 38 HANDLE fd;
@@ -67,7 +68,7 @@ typedef struct _modbus_rtu { @@ -67,7 +68,7 @@ typedef struct _modbus_rtu {
67 int rts; 68 int rts;
68 int rts_delay; 69 int rts_delay;
69 int onebyte_time; 70 int onebyte_time;
70 - void (*set_rts) (modbus_t *ctx, int on); 71 + void (*set_rts)(modbus_t *ctx, int on);
71 #endif 72 #endif
72 /* To handle many slaves on the same link */ 73 /* To handle many slaves on the same link */
73 int confirmation_to_ignore; 74 int confirmation_to_ignore;
src/modbus-rtu.c
@@ -4,10 +4,10 @@ @@ -4,10 +4,10 @@
4 * SPDX-License-Identifier: LGPL-2.1-or-later 4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */ 5 */
6 6
7 -#include <stdio.h>  
8 -#include <stdlib.h>  
9 #include <errno.h> 7 #include <errno.h>
10 #include <fcntl.h> 8 #include <fcntl.h>
  9 +#include <stdio.h>
  10 +#include <stdlib.h>
11 #include <string.h> 11 #include <string.h>
12 #ifndef _MSC_VER 12 #ifndef _MSC_VER
13 #include <unistd.h> 13 #include <unistd.h>
@@ -16,8 +16,8 @@ @@ -16,8 +16,8 @@
16 16
17 #include "modbus-private.h" 17 #include "modbus-private.h"
18 18
19 -#include "modbus-rtu.h"  
20 #include "modbus-rtu-private.h" 19 #include "modbus-rtu-private.h"
  20 +#include "modbus-rtu.h"
21 21
22 #if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS 22 #if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
23 #include <sys/ioctl.h> 23 #include <sys/ioctl.h>
@@ -29,63 +29,47 @@ @@ -29,63 +29,47 @@
29 29
30 /* Table of CRC values for high-order byte */ 30 /* Table of CRC values for high-order byte */
31 static const uint8_t table_crc_hi[] = { 31 static const uint8_t table_crc_hi[] = {
32 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
33 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
34 - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
35 - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
36 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
37 - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,  
38 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
39 - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
40 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
41 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
42 - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
43 - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
44 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
45 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
46 - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
47 - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
48 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
49 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
50 - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
51 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
52 - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
53 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
54 - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
55 - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
56 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
57 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40  
58 -}; 32 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  33 + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  34 + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  35 + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  36 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  37 + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  38 + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
  39 + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  40 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  41 + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  42 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  43 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  44 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  45 + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  46 + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  47 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  48 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  49 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  50 + 0x00, 0xC1, 0x81, 0x40};
59 51
60 /* Table of CRC values for low-order byte */ 52 /* Table of CRC values for low-order byte */
61 static const uint8_t table_crc_lo[] = { 53 static const uint8_t table_crc_lo[] = {
62 - 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,  
63 - 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,  
64 - 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,  
65 - 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,  
66 - 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,  
67 - 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,  
68 - 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,  
69 - 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,  
70 - 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,  
71 - 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,  
72 - 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,  
73 - 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,  
74 - 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,  
75 - 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,  
76 - 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,  
77 - 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,  
78 - 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,  
79 - 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,  
80 - 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,  
81 - 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,  
82 - 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,  
83 - 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,  
84 - 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,  
85 - 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,  
86 - 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,  
87 - 0x43, 0x83, 0x41, 0x81, 0x80, 0x40  
88 -}; 54 + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5,
  55 + 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B,
  56 + 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE,
  57 + 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6,
  58 + 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
  59 + 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  60 + 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8,
  61 + 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
  62 + 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21,
  63 + 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
  64 + 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A,
  65 + 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  66 + 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7,
  67 + 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51,
  68 + 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
  69 + 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
  70 + 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D,
  71 + 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  72 + 0x41, 0x81, 0x80, 0x40};
89 73
90 /* Define the slave ID of the remote device to talk in master mode or set the 74 /* Define the slave ID of the remote device to talk in master mode or set the
91 * internal slave ID in slave mode */ 75 * internal slave ID in slave mode */
@@ -105,9 +89,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave) @@ -105,9 +89,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave)
105 } 89 }
106 90
107 /* Builds a RTU request header */ 91 /* Builds a RTU request header */
108 -static int _modbus_rtu_build_request_basis(modbus_t *ctx, int function,  
109 - int addr, int nb,  
110 - uint8_t *req) 92 +static int _modbus_rtu_build_request_basis(
  93 + modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
111 { 94 {
112 assert(ctx->slave != -1); 95 assert(ctx->slave != -1);
113 req[0] = ctx->slave; 96 req[0] = ctx->slave;
@@ -135,7 +118,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length) @@ -135,7 +118,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
135 { 118 {
136 uint8_t crc_hi = 0xFF; /* high CRC byte initialized */ 119 uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
137 uint8_t crc_lo = 0xFF; /* low CRC byte initialized */ 120 uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
138 - unsigned int i; /* will index into CRC lookup */ 121 + unsigned int i; /* will index into CRC lookup */
139 122
140 /* pass through message buffer */ 123 /* pass through message buffer */
141 while (buffer_length--) { 124 while (buffer_length--) {
@@ -186,8 +169,7 @@ static void win32_ser_init(struct win32_ser *ws) @@ -186,8 +169,7 @@ static void win32_ser_init(struct win32_ser *ws)
186 } 169 }
187 170
188 /* FIXME Try to remove length_to_read -> max_len argument, only used by win32 */ 171 /* FIXME Try to remove length_to_read -> max_len argument, only used by win32 */
189 -static int win32_ser_select(struct win32_ser *ws, int max_len,  
190 - const struct timeval *tv) 172 +static int win32_ser_select(struct win32_ser *ws, int max_len, const struct timeval *tv)
191 { 173 {
192 COMMTIMEOUTS comm_to; 174 COMMTIMEOUTS comm_to;
193 unsigned int msec = 0; 175 unsigned int msec = 0;
@@ -237,8 +219,7 @@ static int win32_ser_select(struct win32_ser *ws, int max_len, @@ -237,8 +219,7 @@ static int win32_ser_select(struct win32_ser *ws, int max_len,
237 } 219 }
238 } 220 }
239 221
240 -static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg,  
241 - unsigned int max_len) 222 +static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max_len)
242 { 223 {
243 unsigned int n = ws->n_bytes; 224 unsigned int n = ws->n_bytes;
244 225
@@ -277,7 +258,9 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt @@ -277,7 +258,9 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt
277 #if defined(_WIN32) 258 #if defined(_WIN32)
278 modbus_rtu_t *ctx_rtu = ctx->backend_data; 259 modbus_rtu_t *ctx_rtu = ctx->backend_data;
279 DWORD n_bytes = 0; 260 DWORD n_bytes = 0;
280 - return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? (ssize_t)n_bytes : -1; 261 + return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL))
  262 + ? (ssize_t) n_bytes
  263 + : -1;
281 #else 264 #else
282 #if HAVE_DECL_TIOCM_RTS 265 #if HAVE_DECL_TIOCM_RTS
283 modbus_rtu_t *ctx_rtu = ctx->backend_data; 266 modbus_rtu_t *ctx_rtu = ctx->backend_data;
@@ -332,7 +315,7 @@ static int _modbus_rtu_receive(modbus_t *ctx, uint8_t *req) @@ -332,7 +315,7 @@ static int _modbus_rtu_receive(modbus_t *ctx, uint8_t *req)
332 static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) 315 static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
333 { 316 {
334 #if defined(_WIN32) 317 #if defined(_WIN32)
335 - return win32_ser_read(&((modbus_rtu_t *)ctx->backend_data)->w_ser, rsp, rsp_length); 318 + return win32_ser_read(&((modbus_rtu_t *) ctx->backend_data)->w_ser, rsp, rsp_length);
336 #else 319 #else
337 return read(ctx->s, rsp, rsp_length); 320 return read(ctx->s, rsp, rsp_length);
338 #endif 321 #endif
@@ -340,8 +323,10 @@ static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) @@ -340,8 +323,10 @@ static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
340 323
341 static int _modbus_rtu_flush(modbus_t *); 324 static int _modbus_rtu_flush(modbus_t *);
342 325
343 -static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,  
344 - const uint8_t *rsp, int rsp_length) 326 +static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx,
  327 + const uint8_t *req,
  328 + const uint8_t *rsp,
  329 + int rsp_length)
345 { 330 {
346 /* Check responding slave is the slave we requested (except for broacast 331 /* Check responding slave is the slave we requested (except for broacast
347 * request) */ 332 * request) */
@@ -349,7 +334,8 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req, @@ -349,7 +334,8 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
349 if (ctx->debug) { 334 if (ctx->debug) {
350 fprintf(stderr, 335 fprintf(stderr,
351 "The responding slave %d isn't the requested slave %d\n", 336 "The responding slave %d isn't the requested slave %d\n",
352 - rsp[0], req[0]); 337 + rsp[0],
  338 + req[0]);
353 } 339 }
354 errno = EMBBADSLAVE; 340 errno = EMBBADSLAVE;
355 return -1; 341 return -1;
@@ -361,8 +347,7 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req, @@ -361,8 +347,7 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
361 /* The check_crc16 function shall return 0 if the message is ignored and the 347 /* The check_crc16 function shall return 0 if the message is ignored and the
362 message length if the CRC is valid. Otherwise it shall return -1 and set 348 message length if the CRC is valid. Otherwise it shall return -1 and set
363 errno to EMBBADCRC. */ 349 errno to EMBBADCRC. */
364 -static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,  
365 - const int msg_length) 350 +static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
366 { 351 {
367 uint16_t crc_calculated; 352 uint16_t crc_calculated;
368 uint16_t crc_received; 353 uint16_t crc_received;
@@ -386,8 +371,10 @@ static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg, @@ -386,8 +371,10 @@ static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,
386 return msg_length; 371 return msg_length;
387 } else { 372 } else {
388 if (ctx->debug) { 373 if (ctx->debug) {
389 - fprintf(stderr, "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n",  
390 - crc_received, crc_calculated); 374 + fprintf(stderr,
  375 + "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n",
  376 + crc_received,
  377 + crc_calculated);
391 } 378 }
392 379
393 if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_PROTOCOL) { 380 if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_PROTOCOL) {
@@ -412,8 +399,11 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -412,8 +399,11 @@ static int _modbus_rtu_connect(modbus_t *ctx)
412 399
413 if (ctx->debug) { 400 if (ctx->debug) {
414 printf("Opening %s at %d bauds (%c, %d, %d)\n", 401 printf("Opening %s at %d bauds (%c, %d, %d)\n",
415 - ctx_rtu->device, ctx_rtu->baud, ctx_rtu->parity,  
416 - ctx_rtu->data_bit, ctx_rtu->stop_bit); 402 + ctx_rtu->device,
  403 + ctx_rtu->baud,
  404 + ctx_rtu->parity,
  405 + ctx_rtu->data_bit,
  406 + ctx_rtu->stop_bit);
417 } 407 }
418 408
419 #if defined(_WIN32) 409 #if defined(_WIN32)
@@ -424,19 +414,16 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -424,19 +414,16 @@ static int _modbus_rtu_connect(modbus_t *ctx)
424 414
425 /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal 415 /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal
426 * number */ 416 * number */
427 - ctx_rtu->w_ser.fd = CreateFileA(ctx_rtu->device,  
428 - GENERIC_READ | GENERIC_WRITE,  
429 - 0,  
430 - NULL,  
431 - OPEN_EXISTING,  
432 - 0,  
433 - NULL); 417 + ctx_rtu->w_ser.fd = CreateFileA(
  418 + ctx_rtu->device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
434 419
435 /* Error checking */ 420 /* Error checking */
436 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) { 421 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
437 if (ctx->debug) { 422 if (ctx->debug) {
438 - fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",  
439 - ctx_rtu->device, (int)GetLastError()); 423 + fprintf(stderr,
  424 + "ERROR Can't open the device %s (LastError %d)\n",
  425 + ctx_rtu->device,
  426 + (int) GetLastError());
440 } 427 }
441 return -1; 428 return -1;
442 } 429 }
@@ -445,8 +432,9 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -445,8 +432,9 @@ static int _modbus_rtu_connect(modbus_t *ctx)
445 ctx_rtu->old_dcb.DCBlength = sizeof(DCB); 432 ctx_rtu->old_dcb.DCBlength = sizeof(DCB);
446 if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) { 433 if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
447 if (ctx->debug) { 434 if (ctx->debug) {
448 - fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",  
449 - (int)GetLastError()); 435 + fprintf(stderr,
  436 + "ERROR Error getting configuration (LastError %d)\n",
  437 + (int) GetLastError());
450 } 438 }
451 CloseHandle(ctx_rtu->w_ser.fd); 439 CloseHandle(ctx_rtu->w_ser.fd);
452 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; 440 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
@@ -519,8 +507,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -519,8 +507,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
519 default: 507 default:
520 dcb.BaudRate = CBR_9600; 508 dcb.BaudRate = CBR_9600;
521 if (ctx->debug) { 509 if (ctx->debug) {
522 - fprintf(stderr, "WARNING Unknown baud rate %d for %s (B9600 used)\n",  
523 - ctx_rtu->baud, ctx_rtu->device); 510 + fprintf(stderr,
  511 + "WARNING Unknown baud rate %d for %s (B9600 used)\n",
  512 + ctx_rtu->baud,
  513 + ctx_rtu->device);
524 } 514 }
525 } 515 }
526 516
@@ -576,8 +566,9 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -576,8 +566,9 @@ static int _modbus_rtu_connect(modbus_t *ctx)
576 /* Setup port */ 566 /* Setup port */
577 if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) { 567 if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
578 if (ctx->debug) { 568 if (ctx->debug) {
579 - fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",  
580 - (int)GetLastError()); 569 + fprintf(stderr,
  570 + "ERROR Error setting new configuration (LastError %d)\n",
  571 + (int) GetLastError());
581 } 572 }
582 CloseHandle(ctx_rtu->w_ser.fd); 573 CloseHandle(ctx_rtu->w_ser.fd);
583 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE; 574 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
@@ -599,8 +590,10 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -599,8 +590,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
599 ctx->s = open(ctx_rtu->device, flags); 590 ctx->s = open(ctx_rtu->device, flags);
600 if (ctx->s < 0) { 591 if (ctx->s < 0) {
601 if (ctx->debug) { 592 if (ctx->debug) {
602 - fprintf(stderr, "ERROR Can't open the device %s (%s)\n",  
603 - ctx_rtu->device, strerror(errno)); 593 + fprintf(stderr,
  594 + "ERROR Can't open the device %s (%s)\n",
  595 + ctx_rtu->device,
  596 + strerror(errno));
604 } 597 }
605 return -1; 598 return -1;
606 } 599 }
@@ -682,7 +675,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -682,7 +675,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
682 break; 675 break;
683 #endif 676 #endif
684 #ifdef B1152000 677 #ifdef B1152000
685 - case 1152000: 678 + case 1152000:
686 speed = B1152000; 679 speed = B1152000;
687 break; 680 break;
688 #endif 681 #endif
@@ -716,13 +709,13 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -716,13 +709,13 @@ static int _modbus_rtu_connect(modbus_t *ctx)
716 if (ctx->debug) { 709 if (ctx->debug) {
717 fprintf(stderr, 710 fprintf(stderr,
718 "WARNING Unknown baud rate %d for %s (B9600 used)\n", 711 "WARNING Unknown baud rate %d for %s (B9600 used)\n",
719 - ctx_rtu->baud, ctx_rtu->device); 712 + ctx_rtu->baud,
  713 + ctx_rtu->device);
720 } 714 }
721 } 715 }
722 716
723 /* Set the baud rate */ 717 /* Set the baud rate */
724 - if ((cfsetispeed(&tios, speed) < 0) ||  
725 - (cfsetospeed(&tios, speed) < 0)) { 718 + if ((cfsetispeed(&tios, speed) < 0) || (cfsetospeed(&tios, speed) < 0)) {
726 close(ctx->s); 719 close(ctx->s);
727 ctx->s = -1; 720 ctx->s = -1;
728 return -1; 721 return -1;
@@ -757,7 +750,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -757,7 +750,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
757 750
758 /* Stop bit (1 or 2) */ 751 /* Stop bit (1 or 2) */
759 if (ctx_rtu->stop_bit == 1) 752 if (ctx_rtu->stop_bit == 1)
760 - tios.c_cflag &=~ CSTOPB; 753 + tios.c_cflag &= ~CSTOPB;
761 else /* 2 */ 754 else /* 2 */
762 tios.c_cflag |= CSTOPB; 755 tios.c_cflag |= CSTOPB;
763 756
@@ -765,11 +758,11 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -765,11 +758,11 @@ static int _modbus_rtu_connect(modbus_t *ctx)
765 PARODD Use odd parity instead of even */ 758 PARODD Use odd parity instead of even */
766 if (ctx_rtu->parity == 'N') { 759 if (ctx_rtu->parity == 'N') {
767 /* None */ 760 /* None */
768 - tios.c_cflag &=~ PARENB; 761 + tios.c_cflag &= ~PARENB;
769 } else if (ctx_rtu->parity == 'E') { 762 } else if (ctx_rtu->parity == 'E') {
770 /* Even */ 763 /* Even */
771 tios.c_cflag |= PARENB; 764 tios.c_cflag |= PARENB;
772 - tios.c_cflag &=~ PARODD; 765 + tios.c_cflag &= ~PARODD;
773 } else { 766 } else {
774 /* Odd */ 767 /* Odd */
775 tios.c_cflag |= PARENB; 768 tios.c_cflag |= PARENB;
@@ -851,7 +844,7 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -851,7 +844,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
851 */ 844 */
852 845
853 /* Raw output */ 846 /* Raw output */
854 - tios.c_oflag &=~ OPOST; 847 + tios.c_oflag &= ~OPOST;
855 848
856 /* C_CC Control characters 849 /* C_CC Control characters
857 VMIN Minimum number of characters to read 850 VMIN Minimum number of characters to read
@@ -1044,7 +1037,7 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode) @@ -1044,7 +1037,7 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
1044 return -1; 1037 return -1;
1045 } 1038 }
1046 1039
1047 -int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)) 1040 +int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts)(modbus_t *ctx, int on))
1048 { 1041 {
1049 if (ctx == NULL) { 1042 if (ctx == NULL) {
1050 errno = EINVAL; 1043 errno = EINVAL;
@@ -1079,7 +1072,7 @@ int modbus_rtu_get_rts_delay(modbus_t *ctx) @@ -1079,7 +1072,7 @@ int modbus_rtu_get_rts_delay(modbus_t *ctx)
1079 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { 1072 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
1080 #if HAVE_DECL_TIOCM_RTS 1073 #if HAVE_DECL_TIOCM_RTS
1081 modbus_rtu_t *ctx_rtu; 1074 modbus_rtu_t *ctx_rtu;
1082 - ctx_rtu = (modbus_rtu_t *)ctx->backend_data; 1075 + ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
1083 return ctx_rtu->rts_delay; 1076 return ctx_rtu->rts_delay;
1084 #else 1077 #else
1085 if (ctx->debug) { 1078 if (ctx->debug) {
@@ -1104,7 +1097,7 @@ int modbus_rtu_set_rts_delay(modbus_t *ctx, int us) @@ -1104,7 +1097,7 @@ int modbus_rtu_set_rts_delay(modbus_t *ctx, int us)
1104 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { 1097 if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
1105 #if HAVE_DECL_TIOCM_RTS 1098 #if HAVE_DECL_TIOCM_RTS
1106 modbus_rtu_t *ctx_rtu; 1099 modbus_rtu_t *ctx_rtu;
1107 - ctx_rtu = (modbus_rtu_t *)ctx->backend_data; 1100 + ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
1108 ctx_rtu->rts_delay = us; 1101 ctx_rtu->rts_delay = us;
1109 return 0; 1102 return 0;
1110 #else 1103 #else
@@ -1128,13 +1121,15 @@ static void _modbus_rtu_close(modbus_t *ctx) @@ -1128,13 +1121,15 @@ static void _modbus_rtu_close(modbus_t *ctx)
1128 #if defined(_WIN32) 1121 #if defined(_WIN32)
1129 /* Revert settings */ 1122 /* Revert settings */
1130 if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) { 1123 if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) {
1131 - fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n",  
1132 - (int)GetLastError()); 1124 + fprintf(stderr,
  1125 + "ERROR Couldn't revert to configuration (LastError %d)\n",
  1126 + (int) GetLastError());
1133 } 1127 }
1134 1128
1135 if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) { 1129 if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) {
1136 - fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n",  
1137 - (int)GetLastError()); 1130 + fprintf(stderr,
  1131 + "ERROR Error while closing handle (LastError %d)\n",
  1132 + (int) GetLastError());
1138 } 1133 }
1139 #else 1134 #else
1140 if (ctx->s >= 0) { 1135 if (ctx->s >= 0) {
@@ -1156,13 +1151,13 @@ static int _modbus_rtu_flush(modbus_t *ctx) @@ -1156,13 +1151,13 @@ static int _modbus_rtu_flush(modbus_t *ctx)
1156 #endif 1151 #endif
1157 } 1152 }
1158 1153
1159 -static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,  
1160 - struct timeval *tv, int length_to_read) 1154 +static int
  1155 +_modbus_rtu_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
1161 { 1156 {
1162 int s_rc; 1157 int s_rc;
1163 #if defined(_WIN32) 1158 #if defined(_WIN32)
1164 - s_rc = win32_ser_select(&((modbus_rtu_t *)ctx->backend_data)->w_ser,  
1165 - length_to_read, tv); 1159 + s_rc = win32_ser_select(
  1160 + &((modbus_rtu_t *) ctx->backend_data)->w_ser, length_to_read, tv);
1166 if (s_rc == 0) { 1161 if (s_rc == 0) {
1167 errno = ETIMEDOUT; 1162 errno = ETIMEDOUT;
1168 return -1; 1163 return -1;
@@ -1172,7 +1167,7 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset, @@ -1172,7 +1167,7 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
1172 return -1; 1167 return -1;
1173 } 1168 }
1174 #else 1169 #else
1175 - while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) { 1170 + while ((s_rc = select(ctx->s + 1, rset, NULL, NULL, tv)) == -1) {
1176 if (errno == EINTR) { 1171 if (errno == EINTR) {
1177 if (ctx->debug) { 1172 if (ctx->debug) {
1178 fprintf(stderr, "A non blocked signal was caught\n"); 1173 fprintf(stderr, "A non blocked signal was caught\n");
@@ -1195,15 +1190,17 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset, @@ -1195,15 +1190,17 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
1195 return s_rc; 1190 return s_rc;
1196 } 1191 }
1197 1192
1198 -static void _modbus_rtu_free(modbus_t *ctx) { 1193 +static void _modbus_rtu_free(modbus_t *ctx)
  1194 +{
1199 if (ctx->backend_data) { 1195 if (ctx->backend_data) {
1200 - free(((modbus_rtu_t *)ctx->backend_data)->device); 1196 + free(((modbus_rtu_t *) ctx->backend_data)->device);
1201 free(ctx->backend_data); 1197 free(ctx->backend_data);
1202 } 1198 }
1203 1199
1204 free(ctx); 1200 free(ctx);
1205 } 1201 }
1206 1202
  1203 +// clang-format off
1207 const modbus_backend_t _modbus_rtu_backend = { 1204 const modbus_backend_t _modbus_rtu_backend = {
1208 _MODBUS_BACKEND_TYPE_RTU, 1205 _MODBUS_BACKEND_TYPE_RTU,
1209 _MODBUS_RTU_HEADER_LENGTH, 1206 _MODBUS_RTU_HEADER_LENGTH,
@@ -1226,9 +1223,10 @@ const modbus_backend_t _modbus_rtu_backend = { @@ -1226,9 +1223,10 @@ const modbus_backend_t _modbus_rtu_backend = {
1226 _modbus_rtu_free 1223 _modbus_rtu_free
1227 }; 1224 };
1228 1225
1229 -modbus_t* modbus_new_rtu(const char *device,  
1230 - int baud, char parity, int data_bit,  
1231 - int stop_bit) 1226 +// clang-format on
  1227 +
  1228 +modbus_t *
  1229 +modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit)
1232 { 1230 {
1233 modbus_t *ctx; 1231 modbus_t *ctx;
1234 modbus_rtu_t *ctx_rtu; 1232 modbus_rtu_t *ctx_rtu;
@@ -1247,23 +1245,23 @@ modbus_t* modbus_new_rtu(const char *device, @@ -1247,23 +1245,23 @@ modbus_t* modbus_new_rtu(const char *device,
1247 return NULL; 1245 return NULL;
1248 } 1246 }
1249 1247
1250 - ctx = (modbus_t *)malloc(sizeof(modbus_t)); 1248 + ctx = (modbus_t *) malloc(sizeof(modbus_t));
1251 if (ctx == NULL) { 1249 if (ctx == NULL) {
1252 return NULL; 1250 return NULL;
1253 } 1251 }
1254 1252
1255 _modbus_init_common(ctx); 1253 _modbus_init_common(ctx);
1256 ctx->backend = &_modbus_rtu_backend; 1254 ctx->backend = &_modbus_rtu_backend;
1257 - ctx->backend_data = (modbus_rtu_t *)malloc(sizeof(modbus_rtu_t)); 1255 + ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
1258 if (ctx->backend_data == NULL) { 1256 if (ctx->backend_data == NULL) {
1259 modbus_free(ctx); 1257 modbus_free(ctx);
1260 errno = ENOMEM; 1258 errno = ENOMEM;
1261 return NULL; 1259 return NULL;
1262 } 1260 }
1263 - ctx_rtu = (modbus_rtu_t *)ctx->backend_data; 1261 + ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
1264 1262
1265 /* Device name and \0 */ 1263 /* Device name and \0 */
1266 - ctx_rtu->device = (char *)malloc((strlen(device) + 1) * sizeof(char)); 1264 + ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
1267 if (ctx_rtu->device == NULL) { 1265 if (ctx_rtu->device == NULL) {
1268 modbus_free(ctx); 1266 modbus_free(ctx);
1269 errno = ENOMEM; 1267 errno = ENOMEM;
@@ -1292,7 +1290,8 @@ modbus_t* modbus_new_rtu(const char *device, @@ -1292,7 +1290,8 @@ modbus_t* modbus_new_rtu(const char *device,
1292 ctx_rtu->rts = MODBUS_RTU_RTS_NONE; 1290 ctx_rtu->rts = MODBUS_RTU_RTS_NONE;
1293 1291
1294 /* Calculate estimated time in micro second to send one byte */ 1292 /* Calculate estimated time in micro second to send one byte */
1295 - ctx_rtu->onebyte_time = 1000000 * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud; 1293 + ctx_rtu->onebyte_time =
  1294 + 1000000 * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud;
1296 1295
1297 /* The internal function is used by default to set RTS */ 1296 /* The internal function is used by default to set RTS */
1298 ctx_rtu->set_rts = _modbus_rtu_ioctl_rts; 1297 ctx_rtu->set_rts = _modbus_rtu_ioctl_rts;
src/modbus-rtu.h
@@ -14,10 +14,10 @@ MODBUS_BEGIN_DECLS @@ -14,10 +14,10 @@ MODBUS_BEGIN_DECLS
14 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5 14 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
15 * RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes 15 * RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes
16 */ 16 */
17 -#define MODBUS_RTU_MAX_ADU_LENGTH 256 17 +#define MODBUS_RTU_MAX_ADU_LENGTH 256
18 18
19 -MODBUS_API modbus_t* modbus_new_rtu(const char *device, int baud, char parity,  
20 - int data_bit, int stop_bit); 19 +MODBUS_API modbus_t *
  20 +modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit);
21 21
22 #define MODBUS_RTU_RS232 0 22 #define MODBUS_RTU_RS232 0
23 #define MODBUS_RTU_RS485 1 23 #define MODBUS_RTU_RS485 1
@@ -25,14 +25,15 @@ MODBUS_API modbus_t* modbus_new_rtu(const char *device, int baud, char parity, @@ -25,14 +25,15 @@ MODBUS_API modbus_t* modbus_new_rtu(const char *device, int baud, char parity,
25 MODBUS_API int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode); 25 MODBUS_API int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode);
26 MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx); 26 MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx);
27 27
28 -#define MODBUS_RTU_RTS_NONE 0  
29 -#define MODBUS_RTU_RTS_UP 1  
30 -#define MODBUS_RTU_RTS_DOWN 2 28 +#define MODBUS_RTU_RTS_NONE 0
  29 +#define MODBUS_RTU_RTS_UP 1
  30 +#define MODBUS_RTU_RTS_DOWN 2
31 31
32 MODBUS_API int modbus_rtu_set_rts(modbus_t *ctx, int mode); 32 MODBUS_API int modbus_rtu_set_rts(modbus_t *ctx, int mode);
33 MODBUS_API int modbus_rtu_get_rts(modbus_t *ctx); 33 MODBUS_API int modbus_rtu_get_rts(modbus_t *ctx);
34 34
35 -MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on)); 35 +MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx,
  36 + void (*set_rts)(modbus_t *ctx, int on));
36 37
37 MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us); 38 MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us);
38 MODBUS_API int modbus_rtu_get_rts_delay(modbus_t *ctx); 39 MODBUS_API int modbus_rtu_get_rts_delay(modbus_t *ctx);
src/modbus-tcp-private.h
@@ -7,11 +7,11 @@ @@ -7,11 +7,11 @@
7 #ifndef MODBUS_TCP_PRIVATE_H 7 #ifndef MODBUS_TCP_PRIVATE_H
8 #define MODBUS_TCP_PRIVATE_H 8 #define MODBUS_TCP_PRIVATE_H
9 9
10 -#define _MODBUS_TCP_HEADER_LENGTH 7 10 +#define _MODBUS_TCP_HEADER_LENGTH 7
11 #define _MODBUS_TCP_PRESET_REQ_LENGTH 12 11 #define _MODBUS_TCP_PRESET_REQ_LENGTH 12
12 -#define _MODBUS_TCP_PRESET_RSP_LENGTH 8 12 +#define _MODBUS_TCP_PRESET_RSP_LENGTH 8
13 13
14 -#define _MODBUS_TCP_CHECKSUM_LENGTH 0 14 +#define _MODBUS_TCP_CHECKSUM_LENGTH 0
15 15
16 /* In both structures, the transaction ID must be placed on first position 16 /* In both structures, the transaction ID must be placed on first position
17 to have a quick access not dependent of the TCP backend */ 17 to have a quick access not dependent of the TCP backend */
src/modbus-tcp.c
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 * SPDX-License-Identifier: LGPL-2.1-or-later 4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */ 5 */
6 6
  7 +// clang-format off
7 #if defined(_WIN32) 8 #if defined(_WIN32)
8 # define OS_WIN32 9 # define OS_WIN32
9 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later. 10 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
@@ -52,11 +53,12 @@ @@ -52,11 +53,12 @@
52 #if defined(_AIX) && !defined(MSG_DONTWAIT) 53 #if defined(_AIX) && !defined(MSG_DONTWAIT)
53 #define MSG_DONTWAIT MSG_NONBLOCK 54 #define MSG_DONTWAIT MSG_NONBLOCK
54 #endif 55 #endif
  56 +// clang-format on
55 57
56 #include "modbus-private.h" 58 #include "modbus-private.h"
57 59
58 -#include "modbus-tcp.h"  
59 #include "modbus-tcp-private.h" 60 #include "modbus-tcp-private.h"
  61 +#include "modbus-tcp.h"
60 62
61 #ifdef OS_WIN32 63 #ifdef OS_WIN32
62 static int _modbus_tcp_init_win32(void) 64 static int _modbus_tcp_init_win32(void)
@@ -65,8 +67,9 @@ static int _modbus_tcp_init_win32(void) @@ -65,8 +67,9 @@ static int _modbus_tcp_init_win32(void)
65 WSADATA wsaData; 67 WSADATA wsaData;
66 68
67 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { 69 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
68 - fprintf(stderr, "WSAStartup() returned error code %d\n",  
69 - (unsigned int)GetLastError()); 70 + fprintf(stderr,
  71 + "WSAStartup() returned error code %d\n",
  72 + (unsigned int) GetLastError());
70 errno = EIO; 73 errno = EIO;
71 return -1; 74 return -1;
72 } 75 }
@@ -94,9 +97,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave) @@ -94,9 +97,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave)
94 } 97 }
95 98
96 /* Builds a TCP request header */ 99 /* Builds a TCP request header */
97 -static int _modbus_tcp_build_request_basis(modbus_t *ctx, int function,  
98 - int addr, int nb,  
99 - uint8_t *req) 100 +static int _modbus_tcp_build_request_basis(
  101 + modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
100 { 102 {
101 modbus_tcp_t *ctx_tcp = ctx->backend_data; 103 modbus_tcp_t *ctx_tcp = ctx->backend_data;
102 104
@@ -148,7 +150,6 @@ static int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp) @@ -148,7 +150,6 @@ static int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp)
148 return _MODBUS_TCP_PRESET_RSP_LENGTH; 150 return _MODBUS_TCP_PRESET_RSP_LENGTH;
149 } 151 }
150 152
151 -  
152 static int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length) 153 static int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
153 { 154 {
154 return (req[0] << 8) + req[1]; 155 return (req[0] << 8) + req[1];
@@ -171,15 +172,17 @@ static ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_lengt @@ -171,15 +172,17 @@ static ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_lengt
171 Requests not to send SIGPIPE on errors on stream oriented 172 Requests not to send SIGPIPE on errors on stream oriented
172 sockets when the other end breaks the connection. The EPIPE 173 sockets when the other end breaks the connection. The EPIPE
173 error is still returned. */ 174 error is still returned. */
174 - return send(ctx->s, (const char *)req, req_length, MSG_NOSIGNAL); 175 + return send(ctx->s, (const char *) req, req_length, MSG_NOSIGNAL);
175 } 176 }
176 177
177 -static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req) { 178 +static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req)
  179 +{
178 return _modbus_receive_msg(ctx, req, MSG_INDICATION); 180 return _modbus_receive_msg(ctx, req, MSG_INDICATION);
179 } 181 }
180 182
181 -static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {  
182 - return recv(ctx->s, (char *)rsp, rsp_length, 0); 183 +static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
  184 +{
  185 + return recv(ctx->s, (char *) rsp, rsp_length, 0);
183 } 186 }
184 187
185 static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length) 188 static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
@@ -187,15 +190,19 @@ static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int ms @@ -187,15 +190,19 @@ static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int ms
187 return msg_length; 190 return msg_length;
188 } 191 }
189 192
190 -static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,  
191 - const uint8_t *rsp, int rsp_length) 193 +static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx,
  194 + const uint8_t *req,
  195 + const uint8_t *rsp,
  196 + int rsp_length)
192 { 197 {
193 unsigned int protocol_id; 198 unsigned int protocol_id;
194 /* Check transaction ID */ 199 /* Check transaction ID */
195 if (req[0] != rsp[0] || req[1] != rsp[1]) { 200 if (req[0] != rsp[0] || req[1] != rsp[1]) {
196 if (ctx->debug) { 201 if (ctx->debug) {
197 - fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",  
198 - (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]); 202 + fprintf(stderr,
  203 + "Invalid transaction ID received 0x%X (not 0x%X)\n",
  204 + (rsp[0] << 8) + rsp[1],
  205 + (req[0] << 8) + req[1]);
199 } 206 }
200 errno = EMBBADDATA; 207 errno = EMBBADDATA;
201 return -1; 208 return -1;
@@ -205,8 +212,7 @@ static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req, @@ -205,8 +212,7 @@ static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
205 protocol_id = (rsp[2] << 8) + rsp[3]; 212 protocol_id = (rsp[2] << 8) + rsp[3];
206 if (protocol_id != 0x0) { 213 if (protocol_id != 0x0) {
207 if (ctx->debug) { 214 if (ctx->debug) {
208 - fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",  
209 - protocol_id); 215 + fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n", protocol_id);
210 } 216 }
211 errno = EMBBADDATA; 217 errno = EMBBADDATA;
212 return -1; 218 return -1;
@@ -223,8 +229,7 @@ static int _modbus_tcp_set_ipv4_options(int s) @@ -223,8 +229,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
223 /* Set the TCP no delay flag */ 229 /* Set the TCP no delay flag */
224 /* SOL_TCP = IPPROTO_TCP */ 230 /* SOL_TCP = IPPROTO_TCP */
225 option = 1; 231 option = 1;
226 - rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY,  
227 - (const void *)&option, sizeof(int)); 232 + rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const void *) &option, sizeof(int));
228 if (rc == -1) { 233 if (rc == -1) {
229 return -1; 234 return -1;
230 } 235 }
@@ -252,8 +257,7 @@ static int _modbus_tcp_set_ipv4_options(int s) @@ -252,8 +257,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
252 **/ 257 **/
253 /* Set the IP low delay option */ 258 /* Set the IP low delay option */
254 option = IPTOS_LOWDELAY; 259 option = IPTOS_LOWDELAY;
255 - rc = setsockopt(s, IPPROTO_IP, IP_TOS,  
256 - (const void *)&option, sizeof(int)); 260 + rc = setsockopt(s, IPPROTO_IP, IP_TOS, (const void *) &option, sizeof(int));
257 if (rc == -1) { 261 if (rc == -1) {
258 return -1; 262 return -1;
259 } 263 }
@@ -262,7 +266,9 @@ static int _modbus_tcp_set_ipv4_options(int s) @@ -262,7 +266,9 @@ static int _modbus_tcp_set_ipv4_options(int s)
262 return 0; 266 return 0;
263 } 267 }
264 268
265 -static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, 269 +static int _connect(int sockfd,
  270 + const struct sockaddr *addr,
  271 + socklen_t addrlen,
266 const struct timeval *ro_tv) 272 const struct timeval *ro_tv)
267 { 273 {
268 int rc = connect(sockfd, addr, addrlen); 274 int rc = connect(sockfd, addr, addrlen);
@@ -292,7 +298,7 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, @@ -292,7 +298,7 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
292 } 298 }
293 299
294 /* The connection is established if SO_ERROR and optval are set to 0 */ 300 /* The connection is established if SO_ERROR and optval are set to 0 */
295 - rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen); 301 + rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
296 if (rc == 0 && optval == 0) { 302 if (rc == 0 && optval == 0) {
297 return 0; 303 return 0;
298 } else { 304 } else {
@@ -345,7 +351,8 @@ static int _modbus_tcp_connect(modbus_t *ctx) @@ -345,7 +351,8 @@ static int _modbus_tcp_connect(modbus_t *ctx)
345 addr.sin_family = AF_INET; 351 addr.sin_family = AF_INET;
346 addr.sin_port = htons(ctx_tcp->port); 352 addr.sin_port = htons(ctx_tcp->port);
347 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip); 353 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
348 - rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout); 354 + rc =
  355 + _connect(ctx->s, (struct sockaddr *) &addr, sizeof(addr), &ctx->response_timeout);
349 if (rc == -1) { 356 if (rc == -1) {
350 close(ctx->s); 357 close(ctx->s);
351 ctx->s = -1; 358 ctx->s = -1;
@@ -381,8 +388,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx) @@ -381,8 +388,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
381 ai_hints.ai_next = NULL; 388 ai_hints.ai_next = NULL;
382 389
383 ai_list = NULL; 390 ai_list = NULL;
384 - rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,  
385 - &ai_hints, &ai_list); 391 + rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service, &ai_hints, &ai_list);
386 if (rc != 0) { 392 if (rc != 0) {
387 if (ctx->debug) { 393 if (ctx->debug) {
388 fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc)); 394 fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
@@ -462,7 +468,7 @@ static int _modbus_tcp_flush(modbus_t *ctx) @@ -462,7 +468,7 @@ static int _modbus_tcp_flush(modbus_t *ctx)
462 tv.tv_usec = 0; 468 tv.tv_usec = 0;
463 FD_ZERO(&rset); 469 FD_ZERO(&rset);
464 FD_SET(ctx->s, &rset); 470 FD_SET(ctx->s, &rset);
465 - rc = select(ctx->s+1, &rset, NULL, NULL, &tv); 471 + rc = select(ctx->s + 1, &rset, NULL, NULL, &tv);
466 if (rc == -1) { 472 if (rc == -1) {
467 return -1; 473 return -1;
468 } 474 }
@@ -514,8 +520,8 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) @@ -514,8 +520,8 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
514 } 520 }
515 521
516 enable = 1; 522 enable = 1;
517 - if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR,  
518 - (char *)&enable, sizeof(enable)) == -1) { 523 + if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(enable)) ==
  524 + -1) {
519 close(new_s); 525 close(new_s);
520 return -1; 526 return -1;
521 } 527 }
@@ -531,7 +537,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) @@ -531,7 +537,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
531 /* Listen only specified IP address */ 537 /* Listen only specified IP address */
532 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip); 538 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
533 } 539 }
534 - if (bind(new_s, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 540 + if (bind(new_s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
535 close(new_s); 541 close(new_s);
536 return -1; 542 return -1;
537 } 543 }
@@ -580,7 +586,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) @@ -580,7 +586,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
580 service = ctx_tcp_pi->service; 586 service = ctx_tcp_pi->service;
581 } 587 }
582 588
583 - memset(&ai_hints, 0, sizeof (ai_hints)); 589 + memset(&ai_hints, 0, sizeof(ai_hints));
584 /* If node is not NULL, than the AI_PASSIVE flag is ignored. */ 590 /* If node is not NULL, than the AI_PASSIVE flag is ignored. */
585 ai_hints.ai_flags |= AI_PASSIVE; 591 ai_hints.ai_flags |= AI_PASSIVE;
586 #ifdef AI_ADDRCONFIG 592 #ifdef AI_ADDRCONFIG
@@ -619,8 +625,8 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) @@ -619,8 +625,8 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
619 continue; 625 continue;
620 } else { 626 } else {
621 int enable = 1; 627 int enable = 1;
622 - rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,  
623 - (void *)&enable, sizeof (enable)); 628 + rc =
  629 + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &enable, sizeof(enable));
624 if (rc != 0) { 630 if (rc != 0) {
625 close(s); 631 close(s);
626 if (ctx->debug) { 632 if (ctx->debug) {
@@ -673,9 +679,9 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) @@ -673,9 +679,9 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
673 addrlen = sizeof(addr); 679 addrlen = sizeof(addr);
674 #ifdef HAVE_ACCEPT4 680 #ifdef HAVE_ACCEPT4
675 /* Inherit socket flags and use accept4 call */ 681 /* Inherit socket flags and use accept4 call */
676 - ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC); 682 + ctx->s = accept4(*s, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
677 #else 683 #else
678 - ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); 684 + ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen);
679 #endif 685 #endif
680 686
681 if (ctx->s < 0) { 687 if (ctx->s < 0) {
@@ -683,8 +689,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) @@ -683,8 +689,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
683 } 689 }
684 690
685 if (ctx->debug) { 691 if (ctx->debug) {
686 - printf("The client connection from %s is accepted\n",  
687 - inet_ntoa(addr.sin_addr)); 692 + printf("The client connection from %s is accepted\n", inet_ntoa(addr.sin_addr));
688 } 693 }
689 694
690 return ctx->s; 695 return ctx->s;
@@ -703,9 +708,9 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) @@ -703,9 +708,9 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
703 addrlen = sizeof(addr); 708 addrlen = sizeof(addr);
704 #ifdef HAVE_ACCEPT4 709 #ifdef HAVE_ACCEPT4
705 /* Inherit socket flags and use accept4 call */ 710 /* Inherit socket flags and use accept4 call */
706 - ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC); 711 + ctx->s = accept4(*s, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
707 #else 712 #else
708 - ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); 713 + ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen);
709 #endif 714 #endif
710 715
711 if (ctx->s < 0) { 716 if (ctx->s < 0) {
@@ -719,10 +724,11 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) @@ -719,10 +724,11 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
719 return ctx->s; 724 return ctx->s;
720 } 725 }
721 726
722 -static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read) 727 +static int
  728 +_modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
723 { 729 {
724 int s_rc; 730 int s_rc;
725 - while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) { 731 + while ((s_rc = select(ctx->s + 1, rset, NULL, NULL, tv)) == -1) {
726 if (errno == EINTR) { 732 if (errno == EINTR) {
727 if (ctx->debug) { 733 if (ctx->debug) {
728 fprintf(stderr, "A non blocked signal was caught\n"); 734 fprintf(stderr, "A non blocked signal was caught\n");
@@ -743,14 +749,16 @@ static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, i @@ -743,14 +749,16 @@ static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, i
743 return s_rc; 749 return s_rc;
744 } 750 }
745 751
746 -static void _modbus_tcp_free(modbus_t *ctx) { 752 +static void _modbus_tcp_free(modbus_t *ctx)
  753 +{
747 if (ctx->backend_data) { 754 if (ctx->backend_data) {
748 free(ctx->backend_data); 755 free(ctx->backend_data);
749 } 756 }
750 free(ctx); 757 free(ctx);
751 } 758 }
752 759
753 -static void _modbus_tcp_pi_free(modbus_t *ctx) { 760 +static void _modbus_tcp_pi_free(modbus_t *ctx)
  761 +{
754 if (ctx->backend_data) { 762 if (ctx->backend_data) {
755 modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data; 763 modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
756 free(ctx_tcp_pi->node); 764 free(ctx_tcp_pi->node);
@@ -761,6 +769,7 @@ static void _modbus_tcp_pi_free(modbus_t *ctx) { @@ -761,6 +769,7 @@ static void _modbus_tcp_pi_free(modbus_t *ctx) {
761 free(ctx); 769 free(ctx);
762 } 770 }
763 771
  772 +// clang-format off
764 const modbus_backend_t _modbus_tcp_backend = { 773 const modbus_backend_t _modbus_tcp_backend = {
765 _MODBUS_BACKEND_TYPE_TCP, 774 _MODBUS_BACKEND_TYPE_TCP,
766 _MODBUS_TCP_HEADER_LENGTH, 775 _MODBUS_TCP_HEADER_LENGTH,
@@ -783,7 +792,6 @@ const modbus_backend_t _modbus_tcp_backend = { @@ -783,7 +792,6 @@ const modbus_backend_t _modbus_tcp_backend = {
783 _modbus_tcp_free 792 _modbus_tcp_free
784 }; 793 };
785 794
786 -  
787 const modbus_backend_t _modbus_tcp_pi_backend = { 795 const modbus_backend_t _modbus_tcp_pi_backend = {
788 _MODBUS_BACKEND_TYPE_TCP, 796 _MODBUS_BACKEND_TYPE_TCP,
789 _MODBUS_TCP_HEADER_LENGTH, 797 _MODBUS_TCP_HEADER_LENGTH,
@@ -806,7 +814,9 @@ const modbus_backend_t _modbus_tcp_pi_backend = { @@ -806,7 +814,9 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
806 _modbus_tcp_pi_free 814 _modbus_tcp_pi_free
807 }; 815 };
808 816
809 -modbus_t* modbus_new_tcp(const char *ip, int port) 817 +// clang-format on
  818 +
  819 +modbus_t *modbus_new_tcp(const char *ip, int port)
810 { 820 {
811 modbus_t *ctx; 821 modbus_t *ctx;
812 modbus_tcp_t *ctx_tcp; 822 modbus_tcp_t *ctx_tcp;
@@ -826,7 +836,7 @@ modbus_t* modbus_new_tcp(const char *ip, int port) @@ -826,7 +836,7 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
826 } 836 }
827 #endif 837 #endif
828 838
829 - ctx = (modbus_t *)malloc(sizeof(modbus_t)); 839 + ctx = (modbus_t *) malloc(sizeof(modbus_t));
830 if (ctx == NULL) { 840 if (ctx == NULL) {
831 return NULL; 841 return NULL;
832 } 842 }
@@ -837,13 +847,13 @@ modbus_t* modbus_new_tcp(const char *ip, int port) @@ -837,13 +847,13 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
837 847
838 ctx->backend = &_modbus_tcp_backend; 848 ctx->backend = &_modbus_tcp_backend;
839 849
840 - ctx->backend_data = (modbus_tcp_t *)malloc(sizeof(modbus_tcp_t)); 850 + ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
841 if (ctx->backend_data == NULL) { 851 if (ctx->backend_data == NULL) {
842 modbus_free(ctx); 852 modbus_free(ctx);
843 errno = ENOMEM; 853 errno = ENOMEM;
844 return NULL; 854 return NULL;
845 } 855 }
846 - ctx_tcp = (modbus_tcp_t *)ctx->backend_data; 856 + ctx_tcp = (modbus_tcp_t *) ctx->backend_data;
847 857
848 if (ip != NULL) { 858 if (ip != NULL) {
849 dest_size = sizeof(char) * 16; 859 dest_size = sizeof(char) * 16;
@@ -870,13 +880,12 @@ modbus_t* modbus_new_tcp(const char *ip, int port) @@ -870,13 +880,12 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
870 return ctx; 880 return ctx;
871 } 881 }
872 882
873 -  
874 -modbus_t* modbus_new_tcp_pi(const char *node, const char *service) 883 +modbus_t *modbus_new_tcp_pi(const char *node, const char *service)
875 { 884 {
876 modbus_t *ctx; 885 modbus_t *ctx;
877 modbus_tcp_pi_t *ctx_tcp_pi; 886 modbus_tcp_pi_t *ctx_tcp_pi;
878 887
879 - ctx = (modbus_t *)malloc(sizeof(modbus_t)); 888 + ctx = (modbus_t *) malloc(sizeof(modbus_t));
880 if (ctx == NULL) { 889 if (ctx == NULL) {
881 return NULL; 890 return NULL;
882 } 891 }
@@ -887,13 +896,13 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service) @@ -887,13 +896,13 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
887 896
888 ctx->backend = &_modbus_tcp_pi_backend; 897 ctx->backend = &_modbus_tcp_pi_backend;
889 898
890 - ctx->backend_data = (modbus_tcp_pi_t *)malloc(sizeof(modbus_tcp_pi_t)); 899 + ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
891 if (ctx->backend_data == NULL) { 900 if (ctx->backend_data == NULL) {
892 modbus_free(ctx); 901 modbus_free(ctx);
893 errno = ENOMEM; 902 errno = ENOMEM;
894 return NULL; 903 return NULL;
895 } 904 }
896 - ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data; 905 + ctx_tcp_pi = (modbus_tcp_pi_t *) ctx->backend_data;
897 ctx_tcp_pi->node = NULL; 906 ctx_tcp_pi->node = NULL;
898 ctx_tcp_pi->service = NULL; 907 ctx_tcp_pi->service = NULL;
899 908
src/modbus-tcp.h
@@ -15,35 +15,35 @@ MODBUS_BEGIN_DECLS @@ -15,35 +15,35 @@ MODBUS_BEGIN_DECLS
15 /* Win32 with MinGW, supplement to <errno.h> */ 15 /* Win32 with MinGW, supplement to <errno.h> */
16 #include <winsock2.h> 16 #include <winsock2.h>
17 #if !defined(ECONNRESET) 17 #if !defined(ECONNRESET)
18 -#define ECONNRESET WSAECONNRESET 18 +#define ECONNRESET WSAECONNRESET
19 #endif 19 #endif
20 #if !defined(ECONNREFUSED) 20 #if !defined(ECONNREFUSED)
21 #define ECONNREFUSED WSAECONNREFUSED 21 #define ECONNREFUSED WSAECONNREFUSED
22 #endif 22 #endif
23 #if !defined(ETIMEDOUT) 23 #if !defined(ETIMEDOUT)
24 -#define ETIMEDOUT WSAETIMEDOUT 24 +#define ETIMEDOUT WSAETIMEDOUT
25 #endif 25 #endif
26 #if !defined(ENOPROTOOPT) 26 #if !defined(ENOPROTOOPT)
27 -#define ENOPROTOOPT WSAENOPROTOOPT 27 +#define ENOPROTOOPT WSAENOPROTOOPT
28 #endif 28 #endif
29 #if !defined(EINPROGRESS) 29 #if !defined(EINPROGRESS)
30 -#define EINPROGRESS WSAEINPROGRESS 30 +#define EINPROGRESS WSAEINPROGRESS
31 #endif 31 #endif
32 #endif 32 #endif
33 33
34 -#define MODBUS_TCP_DEFAULT_PORT 502  
35 -#define MODBUS_TCP_SLAVE 0xFF 34 +#define MODBUS_TCP_DEFAULT_PORT 502
  35 +#define MODBUS_TCP_SLAVE 0xFF
36 36
37 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5 37 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
38 * TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes 38 * TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes
39 */ 39 */
40 -#define MODBUS_TCP_MAX_ADU_LENGTH 260 40 +#define MODBUS_TCP_MAX_ADU_LENGTH 260
41 41
42 -MODBUS_API modbus_t* modbus_new_tcp(const char *ip_address, int port); 42 +MODBUS_API modbus_t *modbus_new_tcp(const char *ip_address, int port);
43 MODBUS_API int modbus_tcp_listen(modbus_t *ctx, int nb_connection); 43 MODBUS_API int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
44 MODBUS_API int modbus_tcp_accept(modbus_t *ctx, int *s); 44 MODBUS_API int modbus_tcp_accept(modbus_t *ctx, int *s);
45 45
46 -MODBUS_API modbus_t* modbus_new_tcp_pi(const char *node, const char *service); 46 +MODBUS_API modbus_t *modbus_new_tcp_pi(const char *node, const char *service);
47 MODBUS_API int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection); 47 MODBUS_API int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection);
48 MODBUS_API int modbus_tcp_pi_accept(modbus_t *ctx, int *s); 48 MODBUS_API int modbus_tcp_pi_accept(modbus_t *ctx, int *s);
49 49
src/modbus-version.h.in
@@ -29,25 +29,23 @@ @@ -29,25 +29,23 @@
29 #define LIBMODBUS_VERSION_MICRO (@LIBMODBUS_VERSION_MICRO@) 29 #define LIBMODBUS_VERSION_MICRO (@LIBMODBUS_VERSION_MICRO@)
30 30
31 /* The full version, like 1.2.3 */ 31 /* The full version, like 1.2.3 */
32 -#define LIBMODBUS_VERSION @LIBMODBUS_VERSION@ 32 +#define LIBMODBUS_VERSION @LIBMODBUS_VERSION@
33 33
34 /* The full version, in string form (suited for string concatenation) 34 /* The full version, in string form (suited for string concatenation)
35 */ 35 */
36 #define LIBMODBUS_VERSION_STRING "@LIBMODBUS_VERSION@" 36 #define LIBMODBUS_VERSION_STRING "@LIBMODBUS_VERSION@"
37 37
38 /* Numerically encoded version, eg. v1.2.3 is 0x010203 */ 38 /* Numerically encoded version, eg. v1.2.3 is 0x010203 */
39 -#define LIBMODBUS_VERSION_HEX ((LIBMODBUS_VERSION_MAJOR << 16) | \  
40 - (LIBMODBUS_VERSION_MINOR << 8) | \  
41 - (LIBMODBUS_VERSION_MICRO << 0)) 39 +#define LIBMODBUS_VERSION_HEX \
  40 + ((LIBMODBUS_VERSION_MAJOR << 16) | (LIBMODBUS_VERSION_MINOR << 8) | \
  41 + (LIBMODBUS_VERSION_MICRO << 0))
42 42
43 /* Evaluates to True if the version is greater than @major, @minor and @micro 43 /* Evaluates to True if the version is greater than @major, @minor and @micro
44 */ 44 */
45 -#define LIBMODBUS_VERSION_CHECK(major,minor,micro) \  
46 - (LIBMODBUS_VERSION_MAJOR > (major) || \  
47 - (LIBMODBUS_VERSION_MAJOR == (major) && \  
48 - LIBMODBUS_VERSION_MINOR > (minor)) || \  
49 - (LIBMODBUS_VERSION_MAJOR == (major) && \  
50 - LIBMODBUS_VERSION_MINOR == (minor) && \ 45 +#define LIBMODBUS_VERSION_CHECK(major, minor, micro) \
  46 + (LIBMODBUS_VERSION_MAJOR > (major) || \
  47 + (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR > (minor)) || \
  48 + (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR == (minor) && \
51 LIBMODBUS_VERSION_MICRO >= (micro))) 49 LIBMODBUS_VERSION_MICRO >= (micro)))
52 50
53 #endif /* MODBUS_VERSION_H */ 51 #endif /* MODBUS_VERSION_H */
src/modbus.c
No preview for this file type
src/modbus.h
@@ -7,15 +7,16 @@ @@ -7,15 +7,16 @@
7 #ifndef MODBUS_H 7 #ifndef MODBUS_H
8 #define MODBUS_H 8 #define MODBUS_H
9 9
  10 +// clang-format off
10 /* Add this for macros that defined unix flavor */ 11 /* Add this for macros that defined unix flavor */
11 #if (defined(__unix__) || defined(unix)) && !defined(USG) 12 #if (defined(__unix__) || defined(unix)) && !defined(USG)
12 -#include <sys/param.h> 13 +# include <sys/param.h>
13 #endif 14 #endif
14 15
15 #ifndef _MSC_VER 16 #ifndef _MSC_VER
16 -#include <stdint.h> 17 +# include <stdint.h>
17 #else 18 #else
18 -#include "stdint.h" 19 +# include "stdint.h"
19 #endif 20 #endif
20 21
21 #include "modbus-version.h" 22 #include "modbus-version.h"
@@ -38,6 +39,7 @@ @@ -38,6 +39,7 @@
38 # define MODBUS_BEGIN_DECLS 39 # define MODBUS_BEGIN_DECLS
39 # define MODBUS_END_DECLS 40 # define MODBUS_END_DECLS
40 #endif 41 #endif
  42 +// clang-format on
41 43
42 MODBUS_BEGIN_DECLS 44 MODBUS_BEGIN_DECLS
43 45
@@ -58,28 +60,28 @@ MODBUS_BEGIN_DECLS @@ -58,28 +60,28 @@ MODBUS_BEGIN_DECLS
58 #endif 60 #endif
59 61
60 /* Modbus function codes */ 62 /* Modbus function codes */
61 -#define MODBUS_FC_READ_COILS 0x01  
62 -#define MODBUS_FC_READ_DISCRETE_INPUTS 0x02  
63 -#define MODBUS_FC_READ_HOLDING_REGISTERS 0x03  
64 -#define MODBUS_FC_READ_INPUT_REGISTERS 0x04  
65 -#define MODBUS_FC_WRITE_SINGLE_COIL 0x05  
66 -#define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06  
67 -#define MODBUS_FC_READ_EXCEPTION_STATUS 0x07  
68 -#define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F  
69 -#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10  
70 -#define MODBUS_FC_REPORT_SLAVE_ID 0x11  
71 -#define MODBUS_FC_MASK_WRITE_REGISTER 0x16  
72 -#define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17  
73 -  
74 -#define MODBUS_BROADCAST_ADDRESS 0 63 +#define MODBUS_FC_READ_COILS 0x01
  64 +#define MODBUS_FC_READ_DISCRETE_INPUTS 0x02
  65 +#define MODBUS_FC_READ_HOLDING_REGISTERS 0x03
  66 +#define MODBUS_FC_READ_INPUT_REGISTERS 0x04
  67 +#define MODBUS_FC_WRITE_SINGLE_COIL 0x05
  68 +#define MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
  69 +#define MODBUS_FC_READ_EXCEPTION_STATUS 0x07
  70 +#define MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
  71 +#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
  72 +#define MODBUS_FC_REPORT_SLAVE_ID 0x11
  73 +#define MODBUS_FC_MASK_WRITE_REGISTER 0x16
  74 +#define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
  75 +
  76 +#define MODBUS_BROADCAST_ADDRESS 0
75 77
76 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) 78 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
77 * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0) 79 * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
78 * (chapter 6 section 11 page 29) 80 * (chapter 6 section 11 page 29)
79 * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0) 81 * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
80 */ 82 */
81 -#define MODBUS_MAX_READ_BITS 2000  
82 -#define MODBUS_MAX_WRITE_BITS 1968 83 +#define MODBUS_MAX_READ_BITS 2000
  84 +#define MODBUS_MAX_WRITE_BITS 1968
83 85
84 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) 86 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
85 * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D) 87 * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
@@ -88,17 +90,17 @@ MODBUS_BEGIN_DECLS @@ -88,17 +90,17 @@ MODBUS_BEGIN_DECLS
88 * (chapter 6 section 17 page 38) 90 * (chapter 6 section 17 page 38)
89 * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79) 91 * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
90 */ 92 */
91 -#define MODBUS_MAX_READ_REGISTERS 125  
92 -#define MODBUS_MAX_WRITE_REGISTERS 123  
93 -#define MODBUS_MAX_WR_WRITE_REGISTERS 121  
94 -#define MODBUS_MAX_WR_READ_REGISTERS 125 93 +#define MODBUS_MAX_READ_REGISTERS 125
  94 +#define MODBUS_MAX_WRITE_REGISTERS 123
  95 +#define MODBUS_MAX_WR_WRITE_REGISTERS 121
  96 +#define MODBUS_MAX_WR_READ_REGISTERS 125
95 97
96 /* The size of the MODBUS PDU is limited by the size constraint inherited from 98 /* The size of the MODBUS PDU is limited by the size constraint inherited from
97 * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 99 * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
98 * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server 100 * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
99 * address (1 byte) - CRC (2 bytes) = 253 bytes. 101 * address (1 byte) - CRC (2 bytes) = 253 bytes.
100 */ 102 */
101 -#define MODBUS_MAX_PDU_LENGTH 253 103 +#define MODBUS_MAX_PDU_LENGTH 253
102 104
103 /* Consequently: 105 /* Consequently:
104 * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 106 * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256
@@ -108,7 +110,7 @@ MODBUS_BEGIN_DECLS @@ -108,7 +110,7 @@ MODBUS_BEGIN_DECLS
108 * an array of bytes to store responses and it will be compatible with the two 110 * an array of bytes to store responses and it will be compatible with the two
109 * backends. 111 * backends.
110 */ 112 */
111 -#define MODBUS_MAX_ADU_LENGTH 260 113 +#define MODBUS_MAX_ADU_LENGTH 260
112 114
113 /* Random number to avoid errno conflicts */ 115 /* Random number to avoid errno conflicts */
114 #define MODBUS_ENOBASE 112345678 116 #define MODBUS_ENOBASE 112345678
@@ -141,11 +143,11 @@ enum { @@ -141,11 +143,11 @@ enum {
141 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET) 143 #define EMBXGTAR (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
142 144
143 /* Native libmodbus error codes */ 145 /* Native libmodbus error codes */
144 -#define EMBBADCRC (EMBXGTAR + 1)  
145 -#define EMBBADDATA (EMBXGTAR + 2)  
146 -#define EMBBADEXC (EMBXGTAR + 3)  
147 -#define EMBUNKEXC (EMBXGTAR + 4)  
148 -#define EMBMDATA (EMBXGTAR + 5) 146 +#define EMBBADCRC (EMBXGTAR + 1)
  147 +#define EMBBADDATA (EMBXGTAR + 2)
  148 +#define EMBBADEXC (EMBXGTAR + 3)
  149 +#define EMBUNKEXC (EMBXGTAR + 4)
  150 +#define EMBMDATA (EMBXGTAR + 5)
149 #define EMBBADSLAVE (EMBXGTAR + 6) 151 #define EMBBADSLAVE (EMBXGTAR + 6)
150 152
151 extern const unsigned int libmodbus_version_major; 153 extern const unsigned int libmodbus_version_major;
@@ -169,35 +171,39 @@ typedef struct _modbus_mapping_t { @@ -169,35 +171,39 @@ typedef struct _modbus_mapping_t {
169 uint16_t *tab_registers; 171 uint16_t *tab_registers;
170 } modbus_mapping_t; 172 } modbus_mapping_t;
171 173
172 -typedef enum  
173 -{  
174 - MODBUS_ERROR_RECOVERY_NONE = 0,  
175 - MODBUS_ERROR_RECOVERY_LINK = (1<<1),  
176 - MODBUS_ERROR_RECOVERY_PROTOCOL = (1<<2) 174 +typedef enum {
  175 + MODBUS_ERROR_RECOVERY_NONE = 0,
  176 + MODBUS_ERROR_RECOVERY_LINK = (1 << 1),
  177 + MODBUS_ERROR_RECOVERY_PROTOCOL = (1 << 2)
177 } modbus_error_recovery_mode; 178 } modbus_error_recovery_mode;
178 179
179 -typedef enum  
180 -{  
181 - MODBUS_QUIRK_NONE = 0,  
182 - MODBUS_QUIRK_MAX_SLAVE = (1<<1),  
183 - MODBUS_QUIRK_REPLY_TO_BROADCAST = (1<<2),  
184 - MODBUS_QUIRK_ALL = 0xFF 180 +typedef enum {
  181 + MODBUS_QUIRK_NONE = 0,
  182 + MODBUS_QUIRK_MAX_SLAVE = (1 << 1),
  183 + MODBUS_QUIRK_REPLY_TO_BROADCAST = (1 << 2),
  184 + MODBUS_QUIRK_ALL = 0xFF
185 } modbus_quirks; 185 } modbus_quirks;
186 186
187 -MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave);  
188 -MODBUS_API int modbus_get_slave(modbus_t* ctx);  
189 -MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery); 187 +MODBUS_API int modbus_set_slave(modbus_t *ctx, int slave);
  188 +MODBUS_API int modbus_get_slave(modbus_t *ctx);
  189 +MODBUS_API int modbus_set_error_recovery(modbus_t *ctx,
  190 + modbus_error_recovery_mode error_recovery);
190 MODBUS_API int modbus_set_socket(modbus_t *ctx, int s); 191 MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
191 MODBUS_API int modbus_get_socket(modbus_t *ctx); 192 MODBUS_API int modbus_get_socket(modbus_t *ctx);
192 193
193 -MODBUS_API int modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);  
194 -MODBUS_API int modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec); 194 +MODBUS_API int
  195 +modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  196 +MODBUS_API int
  197 +modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
195 198
196 -MODBUS_API int modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec); 199 +MODBUS_API int
  200 +modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
197 MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec); 201 MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
198 202
199 -MODBUS_API int modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);  
200 -MODBUS_API int modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec); 203 +MODBUS_API int
  204 +modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
  205 +MODBUS_API int
  206 +modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
201 207
202 MODBUS_API int modbus_get_header_length(modbus_t *ctx); 208 MODBUS_API int modbus_get_header_length(modbus_t *ctx);
203 209
@@ -214,37 +220,53 @@ MODBUS_API const char *modbus_strerror(int errnum); @@ -214,37 +220,53 @@ MODBUS_API const char *modbus_strerror(int errnum);
214 MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest); 220 MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
215 MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest); 221 MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
216 MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest); 222 MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
217 -MODBUS_API int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest); 223 +MODBUS_API int
  224 +modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
218 MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status); 225 MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
219 MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value); 226 MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);
220 MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data); 227 MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
221 -MODBUS_API int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);  
222 -MODBUS_API int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);  
223 -MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,  
224 - const uint16_t *src, int read_addr, int read_nb, 228 +MODBUS_API int
  229 +modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
  230 +MODBUS_API int
  231 +modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
  232 +MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx,
  233 + int write_addr,
  234 + int write_nb,
  235 + const uint16_t *src,
  236 + int read_addr,
  237 + int read_nb,
225 uint16_t *dest); 238 uint16_t *dest);
226 MODBUS_API int modbus_report_slave_id(modbus_t *ctx, int max_dest, uint8_t *dest); 239 MODBUS_API int modbus_report_slave_id(modbus_t *ctx, int max_dest, uint8_t *dest);
227 240
228 -MODBUS_API modbus_mapping_t* modbus_mapping_new_start_address(  
229 - unsigned int start_bits, unsigned int nb_bits,  
230 - unsigned int start_input_bits, unsigned int nb_input_bits,  
231 - unsigned int start_registers, unsigned int nb_registers,  
232 - unsigned int start_input_registers, unsigned int nb_input_registers);  
233 -  
234 -MODBUS_API modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,  
235 - int nb_registers, int nb_input_registers); 241 +MODBUS_API modbus_mapping_t *
  242 +modbus_mapping_new_start_address(unsigned int start_bits,
  243 + unsigned int nb_bits,
  244 + unsigned int start_input_bits,
  245 + unsigned int nb_input_bits,
  246 + unsigned int start_registers,
  247 + unsigned int nb_registers,
  248 + unsigned int start_input_registers,
  249 + unsigned int nb_input_registers);
  250 +
  251 +MODBUS_API modbus_mapping_t *modbus_mapping_new(int nb_bits,
  252 + int nb_input_bits,
  253 + int nb_registers,
  254 + int nb_input_registers);
236 MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping); 255 MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);
237 256
238 -MODBUS_API int modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int raw_req_length); 257 +MODBUS_API int
  258 +modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
239 259
240 MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req); 260 MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req);
241 261
242 MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp); 262 MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
243 263
244 -MODBUS_API int modbus_reply(modbus_t *ctx, const uint8_t *req,  
245 - int req_length, modbus_mapping_t *mb_mapping);  
246 -MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,  
247 - unsigned int exception_code); 264 +MODBUS_API int modbus_reply(modbus_t *ctx,
  265 + const uint8_t *req,
  266 + int req_length,
  267 + modbus_mapping_t *mb_mapping);
  268 +MODBUS_API int
  269 +modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code);
248 MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask); 270 MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask);
249 MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask); 271 MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);
250 272
@@ -253,40 +275,40 @@ MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask); @@ -253,40 +275,40 @@ MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);
253 **/ 275 **/
254 276
255 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) 277 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
256 -#define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)  
257 -#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \  
258 - (((int64_t)tab_int16[(index) ] << 48) | \  
259 - ((int64_t)tab_int16[(index) + 1] << 32) | \  
260 - ((int64_t)tab_int16[(index) + 2] << 16) | \  
261 - (int64_t)tab_int16[(index) + 3]) 278 +#define MODBUS_GET_LOW_BYTE(data) ((data) &0xFF)
  279 +#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
  280 + (((int64_t) tab_int16[(index)] << 48) | ((int64_t) tab_int16[(index) + 1] << 32) | \
  281 + ((int64_t) tab_int16[(index) + 2] << 16) | (int64_t) tab_int16[(index) + 3])
262 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) \ 282 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) \
263 - (((int32_t)tab_int16[(index) ] << 16) | \  
264 - (int32_t)tab_int16[(index) + 1]) 283 + (((int32_t) tab_int16[(index)] << 16) | (int32_t) tab_int16[(index) + 1])
265 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) \ 284 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) \
266 - (((int16_t)tab_int8[(index) ] << 8) | \  
267 - (int16_t)tab_int8[(index) + 1])  
268 -#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \  
269 - do { \  
270 - ((int8_t*)(tab_int8))[(index) ] = (int8_t)((value) >> 8); \  
271 - ((int8_t*)(tab_int8))[(index) + 1] = (int8_t)(value); \  
272 - } while (0)  
273 -#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \  
274 - do { \  
275 - ((int16_t*)(tab_int16))[(index) ] = (int16_t)((value) >> 16); \  
276 - ((int16_t*)(tab_int16))[(index) + 1] = (int16_t)(value); \  
277 - } while (0)  
278 -#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \  
279 - do { \  
280 - ((int16_t*)(tab_int16))[(index) ] = (int16_t)((value) >> 48); \  
281 - ((int16_t*)(tab_int16))[(index) + 1] = (int16_t)((value) >> 32); \  
282 - ((int16_t*)(tab_int16))[(index) + 2] = (int16_t)((value) >> 16); \  
283 - ((int16_t*)(tab_int16))[(index) + 3] = (int16_t)(value); \  
284 - } while (0) 285 + (((int16_t) tab_int8[(index)] << 8) | (int16_t) tab_int8[(index) + 1])
  286 +#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
  287 + do { \
  288 + ((int8_t *) (tab_int8))[(index)] = (int8_t) ((value) >> 8); \
  289 + ((int8_t *) (tab_int8))[(index) + 1] = (int8_t) (value); \
  290 + } while (0)
  291 +#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \
  292 + do { \
  293 + ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 16); \
  294 + ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) (value); \
  295 + } while (0)
  296 +#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \
  297 + do { \
  298 + ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 48); \
  299 + ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) ((value) >> 32); \
  300 + ((int16_t *) (tab_int16))[(index) + 2] = (int16_t) ((value) >> 16); \
  301 + ((int16_t *) (tab_int16))[(index) + 3] = (int16_t) (value); \
  302 + } while (0)
285 303
286 MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value); 304 MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);
287 -MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,  
288 - const uint8_t *tab_byte);  
289 -MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits); 305 +MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest,
  306 + int idx,
  307 + unsigned int nb_bits,
  308 + const uint8_t *tab_byte);
  309 +MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src,
  310 + int idx,
  311 + unsigned int nb_bits);
290 MODBUS_API float modbus_get_float(const uint16_t *src); 312 MODBUS_API float modbus_get_float(const uint16_t *src);
291 MODBUS_API float modbus_get_float_abcd(const uint16_t *src); 313 MODBUS_API float modbus_get_float_abcd(const uint16_t *src);
292 MODBUS_API float modbus_get_float_dcba(const uint16_t *src); 314 MODBUS_API float modbus_get_float_dcba(const uint16_t *src);
@@ -299,9 +321,9 @@ MODBUS_API void modbus_set_float_dcba(float f, uint16_t *dest); @@ -299,9 +321,9 @@ MODBUS_API void modbus_set_float_dcba(float f, uint16_t *dest);
299 MODBUS_API void modbus_set_float_badc(float f, uint16_t *dest); 321 MODBUS_API void modbus_set_float_badc(float f, uint16_t *dest);
300 MODBUS_API void modbus_set_float_cdab(float f, uint16_t *dest); 322 MODBUS_API void modbus_set_float_cdab(float f, uint16_t *dest);
301 323
302 -#include "modbus-tcp.h"  
303 #include "modbus-rtu.h" 324 #include "modbus-rtu.h"
  325 +#include "modbus-tcp.h"
304 326
305 MODBUS_END_DECLS 327 MODBUS_END_DECLS
306 328
307 -#endif /* MODBUS_H */ 329 +#endif /* MODBUS_H */
src/win32/config.h.win32
@@ -121,7 +121,7 @@ @@ -121,7 +121,7 @@
121 /* #undef HAVE_WORKING_VFORK */ 121 /* #undef HAVE_WORKING_VFORK */
122 122
123 /* Define to the sub-directory in which libtool stores uninstalled libraries. 123 /* Define to the sub-directory in which libtool stores uninstalled libraries.
124 - */ 124 + */
125 /* #undef LT_OBJDIR */ 125 /* #undef LT_OBJDIR */
126 126
127 /* Name of package */ 127 /* Name of package */
tests/bandwidth-client.c
@@ -6,13 +6,13 @@ @@ -6,13 +6,13 @@
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #ifndef _MSC_VER 8 #ifndef _MSC_VER
9 -#include <unistd.h>  
10 #include <sys/time.h> 9 #include <sys/time.h>
  10 +#include <unistd.h>
11 #endif 11 #endif
12 -#include <string.h> 12 +#include <errno.h>
13 #include <stdlib.h> 13 #include <stdlib.h>
  14 +#include <string.h>
14 #include <time.h> 15 #include <time.h>
15 -#include <errno.h>  
16 16
17 #include <modbus.h> 17 #include <modbus.h>
18 18
@@ -59,7 +59,8 @@ int main(int argc, char *argv[]) @@ -59,7 +59,8 @@ int main(int argc, char *argv[])
59 use_backend = RTU; 59 use_backend = RTU;
60 n_loop = 100; 60 n_loop = 100;
61 } else { 61 } else {
62 - printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", argv[0]); 62 + printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
  63 + argv[0]);
63 exit(1); 64 exit(1);
64 } 65 }
65 } else { 66 } else {
@@ -75,8 +76,7 @@ int main(int argc, char *argv[]) @@ -75,8 +76,7 @@ int main(int argc, char *argv[])
75 modbus_set_slave(ctx, 1); 76 modbus_set_slave(ctx, 1);
76 } 77 }
77 if (modbus_connect(ctx) == -1) { 78 if (modbus_connect(ctx) == -1) {
78 - fprintf(stderr, "Connection failed: %s\n",  
79 - modbus_strerror(errno)); 79 + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
80 modbus_free(ctx); 80 modbus_free(ctx);
81 return -1; 81 return -1;
82 } 82 }
@@ -93,7 +93,7 @@ int main(int argc, char *argv[]) @@ -93,7 +93,7 @@ int main(int argc, char *argv[])
93 93
94 nb_points = MODBUS_MAX_READ_BITS; 94 nb_points = MODBUS_MAX_READ_BITS;
95 start = gettime_ms(); 95 start = gettime_ms();
96 - for (i=0; i<n_loop; i++) { 96 + for (i = 0; i < n_loop; i++) {
97 rc = modbus_read_bits(ctx, 0, nb_points, tab_bit); 97 rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
98 if (rc == -1) { 98 if (rc == -1) {
99 fprintf(stderr, "%s\n", modbus_strerror(errno)); 99 fprintf(stderr, "%s\n", modbus_strerror(errno));
@@ -130,7 +130,7 @@ int main(int argc, char *argv[]) @@ -130,7 +130,7 @@ int main(int argc, char *argv[])
130 130
131 nb_points = MODBUS_MAX_READ_REGISTERS; 131 nb_points = MODBUS_MAX_READ_REGISTERS;
132 start = gettime_ms(); 132 start = gettime_ms();
133 - for (i=0; i<n_loop; i++) { 133 + for (i = 0; i < n_loop; i++) {
134 rc = modbus_read_registers(ctx, 0, nb_points, tab_reg); 134 rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
135 if (rc == -1) { 135 if (rc == -1) {
136 fprintf(stderr, "%s\n", modbus_strerror(errno)); 136 fprintf(stderr, "%s\n", modbus_strerror(errno));
@@ -167,10 +167,9 @@ int main(int argc, char *argv[]) @@ -167,10 +167,9 @@ int main(int argc, char *argv[])
167 167
168 nb_points = MODBUS_MAX_WR_WRITE_REGISTERS; 168 nb_points = MODBUS_MAX_WR_WRITE_REGISTERS;
169 start = gettime_ms(); 169 start = gettime_ms();
170 - for (i=0; i<n_loop; i++) {  
171 - rc = modbus_write_and_read_registers(ctx,  
172 - 0, nb_points, tab_reg,  
173 - 0, nb_points, tab_reg); 170 + for (i = 0; i < n_loop; i++) {
  171 + rc = modbus_write_and_read_registers(
  172 + ctx, 0, nb_points, tab_reg, 0, nb_points, tab_reg);
174 if (rc == -1) { 173 if (rc == -1) {
175 fprintf(stderr, "%s\n", modbus_strerror(errno)); 174 fprintf(stderr, "%s\n", modbus_strerror(errno));
176 return -1; 175 return -1;
tests/bandwidth-server-many-up.c
@@ -4,25 +4,25 @@ @@ -4,25 +4,25 @@
4 * SPDX-License-Identifier: BSD-3-Clause 4 * SPDX-License-Identifier: BSD-3-Clause
5 */ 5 */
6 6
7 -#include <stdio.h>  
8 -#include <unistd.h>  
9 -#include <string.h>  
10 -#include <stdlib.h>  
11 #include <errno.h> 7 #include <errno.h>
12 #include <signal.h> 8 #include <signal.h>
  9 +#include <stdio.h>
  10 +#include <stdlib.h>
  11 +#include <string.h>
  12 +#include <unistd.h>
13 13
14 #include <modbus.h> 14 #include <modbus.h>
15 15
16 #if defined(_WIN32) 16 #if defined(_WIN32)
17 #include <ws2tcpip.h> 17 #include <ws2tcpip.h>
18 #else 18 #else
  19 +#include <arpa/inet.h>
  20 +#include <netinet/in.h>
19 #include <sys/select.h> 21 #include <sys/select.h>
20 #include <sys/socket.h> 22 #include <sys/socket.h>
21 -#include <netinet/in.h>  
22 -#include <arpa/inet.h>  
23 #endif 23 #endif
24 24
25 -#define NB_CONNECTION 5 25 +#define NB_CONNECTION 5
26 26
27 static modbus_t *ctx = NULL; 27 static modbus_t *ctx = NULL;
28 static modbus_mapping_t *mb_mapping; 28 static modbus_mapping_t *mb_mapping;
@@ -52,11 +52,10 @@ int main(void) @@ -52,11 +52,10 @@ int main(void)
52 52
53 ctx = modbus_new_tcp("127.0.0.1", 1502); 53 ctx = modbus_new_tcp("127.0.0.1", 1502);
54 54
55 - mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,  
56 - MODBUS_MAX_READ_REGISTERS, 0); 55 + mb_mapping =
  56 + modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
57 if (mb_mapping == NULL) { 57 if (mb_mapping == NULL) {
58 - fprintf(stderr, "Failed to allocate the mapping: %s\n",  
59 - modbus_strerror(errno)); 58 + fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
60 modbus_free(ctx); 59 modbus_free(ctx);
61 return -1; 60 return -1;
62 } 61 }
@@ -80,7 +79,7 @@ int main(void) @@ -80,7 +79,7 @@ int main(void)
80 79
81 for (;;) { 80 for (;;) {
82 rdset = refset; 81 rdset = refset;
83 - if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) { 82 + if (select(fdmax + 1, &rdset, NULL, NULL, NULL) == -1) {
84 perror("Server select() failure."); 83 perror("Server select() failure.");
85 close_sigint(1); 84 close_sigint(1);
86 } 85 }
@@ -102,7 +101,7 @@ int main(void) @@ -102,7 +101,7 @@ int main(void)
102 /* Handle new connections */ 101 /* Handle new connections */
103 addrlen = sizeof(clientaddr); 102 addrlen = sizeof(clientaddr);
104 memset(&clientaddr, 0, sizeof(clientaddr)); 103 memset(&clientaddr, 0, sizeof(clientaddr));
105 - newfd = accept(server_socket, (struct sockaddr *)&clientaddr, &addrlen); 104 + newfd = accept(server_socket, (struct sockaddr *) &clientaddr, &addrlen);
106 if (newfd == -1) { 105 if (newfd == -1) {
107 perror("Server accept() error"); 106 perror("Server accept() error");
108 } else { 107 } else {
@@ -113,7 +112,9 @@ int main(void) @@ -113,7 +112,9 @@ int main(void)
113 fdmax = newfd; 112 fdmax = newfd;
114 } 113 }
115 printf("New connection from %s:%d on socket %d\n", 114 printf("New connection from %s:%d on socket %d\n",
116 - inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd); 115 + inet_ntoa(clientaddr.sin_addr),
  116 + clientaddr.sin_port,
  117 + newfd);
117 } 118 }
118 } else { 119 } else {
119 modbus_set_socket(ctx, master_socket); 120 modbus_set_socket(ctx, master_socket);
tests/bandwidth-server-one.c
@@ -8,9 +8,9 @@ @@ -8,9 +8,9 @@
8 #ifndef _MSC_VER 8 #ifndef _MSC_VER
9 #include <unistd.h> 9 #include <unistd.h>
10 #endif 10 #endif
11 -#include <string.h>  
12 -#include <stdlib.h>  
13 #include <errno.h> 11 #include <errno.h>
  12 +#include <stdlib.h>
  13 +#include <string.h>
14 14
15 #include <modbus.h> 15 #include <modbus.h>
16 16
@@ -31,14 +31,15 @@ int main(int argc, char *argv[]) @@ -31,14 +31,15 @@ int main(int argc, char *argv[])
31 int rc; 31 int rc;
32 int use_backend; 32 int use_backend;
33 33
34 - /* TCP */ 34 + /* TCP */
35 if (argc > 1) { 35 if (argc > 1) {
36 if (strcmp(argv[1], "tcp") == 0) { 36 if (strcmp(argv[1], "tcp") == 0) {
37 use_backend = TCP; 37 use_backend = TCP;
38 } else if (strcmp(argv[1], "rtu") == 0) { 38 } else if (strcmp(argv[1], "rtu") == 0) {
39 use_backend = RTU; 39 use_backend = RTU;
40 } else { 40 } else {
41 - printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", argv[0]); 41 + printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
  42 + argv[0]);
42 exit(1); 43 exit(1);
43 } 44 }
44 } else { 45 } else {
@@ -57,22 +58,21 @@ int main(int argc, char *argv[]) @@ -57,22 +58,21 @@ int main(int argc, char *argv[])
57 modbus_connect(ctx); 58 modbus_connect(ctx);
58 } 59 }
59 60
60 - mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,  
61 - MODBUS_MAX_READ_REGISTERS, 0); 61 + mb_mapping =
  62 + modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
62 if (mb_mapping == NULL) { 63 if (mb_mapping == NULL) {
63 - fprintf(stderr, "Failed to allocate the mapping: %s\n",  
64 - modbus_strerror(errno)); 64 + fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
65 modbus_free(ctx); 65 modbus_free(ctx);
66 return -1; 66 return -1;
67 } 67 }
68 68
69 - for(;;) { 69 + for (;;) {
70 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; 70 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
71 71
72 rc = modbus_receive(ctx, query); 72 rc = modbus_receive(ctx, query);
73 if (rc > 0) { 73 if (rc > 0) {
74 modbus_reply(ctx, query, rc, mb_mapping); 74 modbus_reply(ctx, query, rc, mb_mapping);
75 - } else if (rc == -1) { 75 + } else if (rc == -1) {
76 /* Connection closed by the client or error */ 76 /* Connection closed by the client or error */
77 break; 77 break;
78 } 78 }
tests/random-test-client.c
@@ -8,9 +8,9 @@ @@ -8,9 +8,9 @@
8 #ifndef _MSC_VER 8 #ifndef _MSC_VER
9 #include <unistd.h> 9 #include <unistd.h>
10 #endif 10 #endif
11 -#include <string.h>  
12 -#include <stdlib.h>  
13 #include <errno.h> 11 #include <errno.h>
  12 +#include <stdlib.h>
  13 +#include <string.h>
14 14
15 #include <modbus.h> 15 #include <modbus.h>
16 16
@@ -27,10 +27,10 @@ @@ -27,10 +27,10 @@
27 All these functions are called with random values on a address 27 All these functions are called with random values on a address
28 range defined by the following defines. 28 range defined by the following defines.
29 */ 29 */
30 -#define LOOP 1  
31 -#define SERVER_ID 17  
32 -#define ADDRESS_START 0  
33 -#define ADDRESS_END 99 30 +#define LOOP 1
  31 +#define SERVER_ID 17
  32 +#define ADDRESS_START 0
  33 +#define ADDRESS_END 99
34 34
35 /* At each loop, the program works in the range ADDRESS_START to 35 /* At each loop, the program works in the range ADDRESS_START to
36 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on. 36 * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
@@ -50,18 +50,17 @@ int main(void) @@ -50,18 +50,17 @@ int main(void)
50 uint16_t *tab_rp_registers; 50 uint16_t *tab_rp_registers;
51 51
52 /* RTU */ 52 /* RTU */
53 -/*  
54 - ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);  
55 - modbus_set_slave(ctx, SERVER_ID);  
56 -*/ 53 + /*
  54 + ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
  55 + modbus_set_slave(ctx, SERVER_ID);
  56 + */
57 57
58 /* TCP */ 58 /* TCP */
59 ctx = modbus_new_tcp("127.0.0.1", 1502); 59 ctx = modbus_new_tcp("127.0.0.1", 1502);
60 modbus_set_debug(ctx, TRUE); 60 modbus_set_debug(ctx, TRUE);
61 61
62 if (modbus_connect(ctx) == -1) { 62 if (modbus_connect(ctx) == -1) {
63 - fprintf(stderr, "Connection failed: %s\n",  
64 - modbus_strerror(errno)); 63 + fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
65 modbus_free(ctx); 64 modbus_free(ctx);
66 return -1; 65 return -1;
67 } 66 }
@@ -90,8 +89,8 @@ int main(void) @@ -90,8 +89,8 @@ int main(void)
90 int i; 89 int i;
91 90
92 /* Random numbers (short) */ 91 /* Random numbers (short) */
93 - for (i=0; i<nb; i++) {  
94 - tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0)); 92 + for (i = 0; i < nb; i++) {
  93 + tab_rq_registers[i] = (uint16_t) (65535.0 * rand() / (RAND_MAX + 1.0));
95 tab_rw_rq_registers[i] = ~tab_rq_registers[i]; 94 tab_rw_rq_registers[i] = ~tab_rq_registers[i];
96 tab_rq_bits[i] = tab_rq_registers[i] % 2; 95 tab_rq_bits[i] = tab_rq_registers[i] % 2;
97 } 96 }
@@ -125,12 +124,15 @@ int main(void) @@ -125,12 +124,15 @@ int main(void)
125 printf("Address = %d, nb = %d\n", addr, nb); 124 printf("Address = %d, nb = %d\n", addr, nb);
126 nb_fail++; 125 nb_fail++;
127 } else { 126 } else {
128 - for (i=0; i<nb; i++) { 127 + for (i = 0; i < nb; i++) {
129 if (tab_rp_bits[i] != tab_rq_bits[i]) { 128 if (tab_rp_bits[i] != tab_rq_bits[i]) {
130 printf("ERROR modbus_read_bits\n"); 129 printf("ERROR modbus_read_bits\n");
131 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n", 130 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
132 - addr, tab_rq_bits[i], tab_rq_bits[i],  
133 - tab_rp_bits[i], tab_rp_bits[i]); 131 + addr,
  132 + tab_rq_bits[i],
  133 + tab_rq_bits[i],
  134 + tab_rp_bits[i],
  135 + tab_rp_bits[i]);
134 nb_fail++; 136 nb_fail++;
135 } 137 }
136 } 138 }
@@ -142,7 +144,9 @@ int main(void) @@ -142,7 +144,9 @@ int main(void)
142 if (rc != 1) { 144 if (rc != 1) {
143 printf("ERROR modbus_write_register (%d)\n", rc); 145 printf("ERROR modbus_write_register (%d)\n", rc);
144 printf("Address = %d, value = %d (0x%X)\n", 146 printf("Address = %d, value = %d (0x%X)\n",
145 - addr, tab_rq_registers[0], tab_rq_registers[0]); 147 + addr,
  148 + tab_rq_registers[0],
  149 + tab_rq_registers[0]);
146 nb_fail++; 150 nb_fail++;
147 } else { 151 } else {
148 rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers); 152 rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
@@ -154,8 +158,11 @@ int main(void) @@ -154,8 +158,11 @@ int main(void)
154 if (tab_rq_registers[0] != tab_rp_registers[0]) { 158 if (tab_rq_registers[0] != tab_rp_registers[0]) {
155 printf("ERROR modbus_read_registers single\n"); 159 printf("ERROR modbus_read_registers single\n");
156 printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n", 160 printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
157 - addr, tab_rq_registers[0], tab_rq_registers[0],  
158 - tab_rp_registers[0], tab_rp_registers[0]); 161 + addr,
  162 + tab_rq_registers[0],
  163 + tab_rq_registers[0],
  164 + tab_rp_registers[0],
  165 + tab_rp_registers[0]);
159 nb_fail++; 166 nb_fail++;
160 } 167 }
161 } 168 }
@@ -174,32 +181,37 @@ int main(void) @@ -174,32 +181,37 @@ int main(void)
174 printf("Address = %d, nb = %d\n", addr, nb); 181 printf("Address = %d, nb = %d\n", addr, nb);
175 nb_fail++; 182 nb_fail++;
176 } else { 183 } else {
177 - for (i=0; i<nb; i++) { 184 + for (i = 0; i < nb; i++) {
178 if (tab_rq_registers[i] != tab_rp_registers[i]) { 185 if (tab_rq_registers[i] != tab_rp_registers[i]) {
179 printf("ERROR modbus_read_registers\n"); 186 printf("ERROR modbus_read_registers\n");
180 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n", 187 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
181 - addr, tab_rq_registers[i], tab_rq_registers[i],  
182 - tab_rp_registers[i], tab_rp_registers[i]); 188 + addr,
  189 + tab_rq_registers[i],
  190 + tab_rq_registers[i],
  191 + tab_rp_registers[i],
  192 + tab_rp_registers[i]);
183 nb_fail++; 193 nb_fail++;
184 } 194 }
185 } 195 }
186 } 196 }
187 } 197 }
188 /* R/W MULTIPLE REGISTERS */ 198 /* R/W MULTIPLE REGISTERS */
189 - rc = modbus_write_and_read_registers(ctx,  
190 - addr, nb, tab_rw_rq_registers,  
191 - addr, nb, tab_rp_registers); 199 + rc = modbus_write_and_read_registers(
  200 + ctx, addr, nb, tab_rw_rq_registers, addr, nb, tab_rp_registers);
192 if (rc != nb) { 201 if (rc != nb) {
193 printf("ERROR modbus_read_and_write_registers (%d)\n", rc); 202 printf("ERROR modbus_read_and_write_registers (%d)\n", rc);
194 printf("Address = %d, nb = %d\n", addr, nb); 203 printf("Address = %d, nb = %d\n", addr, nb);
195 nb_fail++; 204 nb_fail++;
196 } else { 205 } else {
197 - for (i=0; i<nb; i++) { 206 + for (i = 0; i < nb; i++) {
198 if (tab_rp_registers[i] != tab_rw_rq_registers[i]) { 207 if (tab_rp_registers[i] != tab_rw_rq_registers[i]) {
199 printf("ERROR modbus_read_and_write_registers READ\n"); 208 printf("ERROR modbus_read_and_write_registers READ\n");
200 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n", 209 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
201 - addr, tab_rp_registers[i], tab_rw_rq_registers[i],  
202 - tab_rp_registers[i], tab_rw_rq_registers[i]); 210 + addr,
  211 + tab_rp_registers[i],
  212 + tab_rw_rq_registers[i],
  213 + tab_rp_registers[i],
  214 + tab_rw_rq_registers[i]);
203 nb_fail++; 215 nb_fail++;
204 } 216 }
205 } 217 }
@@ -210,12 +222,15 @@ int main(void) @@ -210,12 +222,15 @@ int main(void)
210 printf("Address = %d, nb = %d\n", addr, nb); 222 printf("Address = %d, nb = %d\n", addr, nb);
211 nb_fail++; 223 nb_fail++;
212 } else { 224 } else {
213 - for (i=0; i<nb; i++) { 225 + for (i = 0; i < nb; i++) {
214 if (tab_rw_rq_registers[i] != tab_rp_registers[i]) { 226 if (tab_rw_rq_registers[i] != tab_rp_registers[i]) {
215 printf("ERROR modbus_read_and_write_registers WRITE\n"); 227 printf("ERROR modbus_read_and_write_registers WRITE\n");
216 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n", 228 printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
217 - addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i],  
218 - tab_rp_registers[i], tab_rp_registers[i]); 229 + addr,
  230 + tab_rw_rq_registers[i],
  231 + tab_rw_rq_registers[i],
  232 + tab_rp_registers[i],
  233 + tab_rp_registers[i]);
219 nb_fail++; 234 nb_fail++;
220 } 235 }
221 } 236 }
tests/random-test-server.c
@@ -8,8 +8,8 @@ @@ -8,8 +8,8 @@
8 #ifndef _MSC_VER 8 #ifndef _MSC_VER
9 #include <unistd.h> 9 #include <unistd.h>
10 #endif 10 #endif
11 -#include <stdlib.h>  
12 #include <errno.h> 11 #include <errno.h>
  12 +#include <stdlib.h>
13 13
14 #include <modbus.h> 14 #include <modbus.h>
15 15
@@ -24,8 +24,7 @@ int main(void) @@ -24,8 +24,7 @@ int main(void)
24 24
25 mb_mapping = modbus_mapping_new(500, 500, 500, 500); 25 mb_mapping = modbus_mapping_new(500, 500, 500, 500);
26 if (mb_mapping == NULL) { 26 if (mb_mapping == NULL) {
27 - fprintf(stderr, "Failed to allocate the mapping: %s\n",  
28 - modbus_strerror(errno)); 27 + fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
29 modbus_free(ctx); 28 modbus_free(ctx);
30 return -1; 29 return -1;
31 } 30 }
tests/unit-test-client.c
@@ -4,12 +4,12 @@ @@ -4,12 +4,12 @@
4 * SPDX-License-Identifier: BSD-3-Clause 4 * SPDX-License-Identifier: BSD-3-Clause
5 */ 5 */
6 6
7 -#include <stdio.h>  
8 -#include <unistd.h>  
9 -#include <string.h>  
10 -#include <stdlib.h>  
11 #include <errno.h> 7 #include <errno.h>
12 #include <modbus.h> 8 #include <modbus.h>
  9 +#include <stdio.h>
  10 +#include <stdlib.h>
  11 +#include <string.h>
  12 +#include <unistd.h>
13 13
14 #include "unit-test.h" 14 #include "unit-test.h"
15 15
@@ -22,31 +22,37 @@ enum { @@ -22,31 +22,37 @@ enum {
22 }; 22 };
23 23
24 int test_server(modbus_t *ctx, int use_backend); 24 int test_server(modbus_t *ctx, int use_backend);
25 -int send_crafted_request(modbus_t *ctx, int function,  
26 - uint8_t *req, int req_size,  
27 - uint16_t max_value, uint16_t bytes,  
28 - int backend_length, int backend_offset); 25 +int send_crafted_request(modbus_t *ctx,
  26 + int function,
  27 + uint8_t *req,
  28 + int req_size,
  29 + uint16_t max_value,
  30 + uint16_t bytes,
  31 + int backend_length,
  32 + int backend_offset);
29 int equal_dword(uint16_t *tab_reg, const uint32_t value); 33 int equal_dword(uint16_t *tab_reg, const uint32_t value);
30 int is_memory_equal(const void *s1, const void *s2, size_t size); 34 int is_memory_equal(const void *s1, const void *s2, size_t size);
31 35
32 -#define BUG_REPORT(_cond, _format, _args ...) \  
33 - printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args) 36 +#define BUG_REPORT(_cond, _format, _args...) \
  37 + printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args)
34 38
35 -#define ASSERT_TRUE(_cond, _format, __args...) { \  
36 - if (_cond) { \  
37 - printf("OK\n"); \  
38 - } else { \  
39 - BUG_REPORT(_cond, _format, ## __args); \  
40 - goto close; \  
41 - } \  
42 -}; 39 +#define ASSERT_TRUE(_cond, _format, __args...) \
  40 + { \
  41 + if (_cond) { \
  42 + printf("OK\n"); \
  43 + } else { \
  44 + BUG_REPORT(_cond, _format, ##__args); \
  45 + goto close; \
  46 + } \
  47 + };
43 48
44 int is_memory_equal(const void *s1, const void *s2, size_t size) 49 int is_memory_equal(const void *s1, const void *s2, size_t size)
45 { 50 {
46 return (memcmp(s1, s2, size) == 0); 51 return (memcmp(s1, s2, size) == 0);
47 } 52 }
48 53
49 -int equal_dword(uint16_t *tab_reg, const uint32_t value) { 54 +int equal_dword(uint16_t *tab_reg, const uint32_t value)
  55 +{
50 return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF))); 56 return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF)));
51 } 57 }
52 58
@@ -80,7 +86,8 @@ int main(int argc, char *argv[]) @@ -80,7 +86,8 @@ int main(int argc, char *argv[])
80 } else if (strcmp(argv[1], "rtu") == 0) { 86 } else if (strcmp(argv[1], "rtu") == 0) {
81 use_backend = RTU; 87 use_backend = RTU;
82 } else { 88 } else {
83 - printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]); 89 + printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n",
  90 + argv[0]);
84 exit(1); 91 exit(1);
85 } 92 }
86 } else { 93 } else {
@@ -100,9 +107,8 @@ int main(int argc, char *argv[]) @@ -100,9 +107,8 @@ int main(int argc, char *argv[])
100 return -1; 107 return -1;
101 } 108 }
102 modbus_set_debug(ctx, TRUE); 109 modbus_set_debug(ctx, TRUE);
103 - modbus_set_error_recovery(ctx,  
104 - MODBUS_ERROR_RECOVERY_LINK |  
105 - MODBUS_ERROR_RECOVERY_PROTOCOL); 110 + modbus_set_error_recovery(
  111 + ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
106 112
107 if (use_backend == RTU) { 113 if (use_backend == RTU) {
108 modbus_set_slave(ctx, SERVER_ID); 114 modbus_set_slave(ctx, SERVER_ID);
@@ -121,8 +127,8 @@ int main(int argc, char *argv[]) @@ -121,8 +127,8 @@ int main(int argc, char *argv[])
121 memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t)); 127 memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
122 128
123 /* Allocate and initialize the memory to store the registers */ 129 /* Allocate and initialize the memory to store the registers */
124 - nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?  
125 - UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB; 130 + nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
  131 + : UT_INPUT_REGISTERS_NB;
126 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); 132 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
127 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); 133 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
128 134
@@ -131,7 +137,8 @@ int main(int argc, char *argv[]) @@ -131,7 +137,8 @@ int main(int argc, char *argv[])
131 printf("1/1 No response timeout modification on connect: "); 137 printf("1/1 No response timeout modification on connect: ");
132 modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec); 138 modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
133 ASSERT_TRUE(old_response_to_sec == new_response_to_sec && 139 ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
134 - old_response_to_usec == new_response_to_usec, ""); 140 + old_response_to_usec == new_response_to_usec,
  141 + "");
135 142
136 printf("\nTEST WRITE/READ:\n"); 143 printf("\nTEST WRITE/READ:\n");
137 144
@@ -145,8 +152,7 @@ int main(int argc, char *argv[]) @@ -145,8 +152,7 @@ int main(int argc, char *argv[])
145 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits); 152 rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
146 printf("2/2 modbus_read_bits: "); 153 printf("2/2 modbus_read_bits: ");
147 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); 154 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
148 - ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n",  
149 - tab_rp_bits[0], ON); 155 + ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
150 156
151 /* End single */ 157 /* End single */
152 158
@@ -169,9 +175,9 @@ int main(int argc, char *argv[]) @@ -169,9 +175,9 @@ int main(int argc, char *argv[])
169 while (nb_points > 0) { 175 while (nb_points > 0) {
170 int nb_bits = (nb_points > 8) ? 8 : nb_points; 176 int nb_bits = (nb_points > 8) ? 8 : nb_points;
171 177
172 - value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);  
173 - ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n",  
174 - value, UT_BITS_TAB[i]); 178 + value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits);
  179 + ASSERT_TRUE(
  180 + value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
175 181
176 nb_points -= nb_bits; 182 nb_points -= nb_bits;
177 i++; 183 i++;
@@ -180,8 +186,8 @@ int main(int argc, char *argv[]) @@ -180,8 +186,8 @@ int main(int argc, char *argv[])
180 /* End of multiple bits */ 186 /* End of multiple bits */
181 187
182 /** DISCRETE INPUTS **/ 188 /** DISCRETE INPUTS **/
183 - rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,  
184 - UT_INPUT_BITS_NB, tab_rp_bits); 189 + rc =
  190 + modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, tab_rp_bits);
185 printf("1/1 modbus_read_input_bits: "); 191 printf("1/1 modbus_read_input_bits: ");
186 ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc); 192 ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
187 193
@@ -189,9 +195,11 @@ int main(int argc, char *argv[]) @@ -189,9 +195,11 @@ int main(int argc, char *argv[])
189 nb_points = UT_INPUT_BITS_NB; 195 nb_points = UT_INPUT_BITS_NB;
190 while (nb_points > 0) { 196 while (nb_points > 0) {
191 int nb_bits = (nb_points > 8) ? 8 : nb_points; 197 int nb_bits = (nb_points > 8) ? 8 : nb_points;
192 - value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);  
193 - ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n",  
194 - value, UT_INPUT_BITS_TAB[i]); 198 + value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits);
  199 + ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i],
  200 + "FAILED (%0X != %0X)\n",
  201 + value,
  202 + UT_INPUT_BITS_TAB[i]);
195 203
196 nb_points -= nb_bits; 204 nb_points -= nb_bits;
197 i++; 205 i++;
@@ -205,39 +213,39 @@ int main(int argc, char *argv[]) @@ -205,39 +213,39 @@ int main(int argc, char *argv[])
205 printf("1/2 modbus_write_register: "); 213 printf("1/2 modbus_write_register: ");
206 ASSERT_TRUE(rc == 1, ""); 214 ASSERT_TRUE(rc == 1, "");
207 215
208 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
209 - 1, tab_rp_registers); 216 + rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
210 printf("2/2 modbus_read_registers: "); 217 printf("2/2 modbus_read_registers: ");
211 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); 218 ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
212 - ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n",  
213 - tab_rp_registers[0], 0x1234); 219 + ASSERT_TRUE(tab_rp_registers[0] == 0x1234,
  220 + "FAILED (%0X != %0X)\n",
  221 + tab_rp_registers[0],
  222 + 0x1234);
214 /* End of single register */ 223 /* End of single register */
215 224
216 /* Many registers */ 225 /* Many registers */
217 - rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,  
218 - UT_REGISTERS_NB, UT_REGISTERS_TAB); 226 + rc = modbus_write_registers(
  227 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, UT_REGISTERS_TAB);
219 printf("1/5 modbus_write_registers: "); 228 printf("1/5 modbus_write_registers: ");
220 ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); 229 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
221 230
222 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
223 - UT_REGISTERS_NB, tab_rp_registers); 231 + rc = modbus_read_registers(
  232 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
224 printf("2/5 modbus_read_registers: "); 233 printf("2/5 modbus_read_registers: ");
225 ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); 234 ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
226 235
227 - for (i=0; i < UT_REGISTERS_NB; i++) { 236 + for (i = 0; i < UT_REGISTERS_NB; i++) {
228 ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i], 237 ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
229 "FAILED (%0X != %0X)\n", 238 "FAILED (%0X != %0X)\n",
230 - tab_rp_registers[i], UT_REGISTERS_TAB[i]); 239 + tab_rp_registers[i],
  240 + UT_REGISTERS_TAB[i]);
231 } 241 }
232 242
233 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
234 - 0, tab_rp_registers); 243 + rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 0, tab_rp_registers);
235 printf("3/5 modbus_read_registers (0): "); 244 printf("3/5 modbus_read_registers (0): ");
236 ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc); 245 ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
237 246
238 - nb_points = (UT_REGISTERS_NB >  
239 - UT_INPUT_REGISTERS_NB) ?  
240 - UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB; 247 + nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
  248 + : UT_INPUT_REGISTERS_NB;
241 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); 249 memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
242 250
243 /* Write registers to zero from tab_rp_registers and store read registers 251 /* Write registers to zero from tab_rp_registers and store read registers
@@ -251,32 +259,32 @@ int main(int argc, char *argv[]) @@ -251,32 +259,32 @@ int main(int argc, char *argv[])
251 UT_REGISTERS_NB, 259 UT_REGISTERS_NB,
252 tab_rp_registers); 260 tab_rp_registers);
253 printf("4/5 modbus_write_and_read_registers: "); 261 printf("4/5 modbus_write_and_read_registers: ");
254 - ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n",  
255 - rc, UT_REGISTERS_NB); 262 + ASSERT_TRUE(
  263 + rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
256 264
257 ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0], 265 ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
258 "FAILED (%0X != %0X)\n", 266 "FAILED (%0X != %0X)\n",
259 - tab_rp_registers[0], UT_REGISTERS_TAB[0]); 267 + tab_rp_registers[0],
  268 + UT_REGISTERS_TAB[0]);
260 269
261 - for (i=1; i < UT_REGISTERS_NB; i++) {  
262 - ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n",  
263 - tab_rp_registers[i], 0); 270 + for (i = 1; i < UT_REGISTERS_NB; i++) {
  271 + ASSERT_TRUE(
  272 + tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers[i], 0);
264 } 273 }
265 274
266 /* End of many registers */ 275 /* End of many registers */
267 276
268 -  
269 /** INPUT REGISTERS **/ 277 /** INPUT REGISTERS **/
270 - rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,  
271 - UT_INPUT_REGISTERS_NB,  
272 - tab_rp_registers); 278 + rc = modbus_read_input_registers(
  279 + ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB, tab_rp_registers);
273 printf("1/1 modbus_read_input_registers: "); 280 printf("1/1 modbus_read_input_registers: ");
274 ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); 281 ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
275 282
276 - for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { 283 + for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
277 ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i], 284 ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
278 "FAILED (%0X != %0X)\n", 285 "FAILED (%0X != %0X)\n",
279 - tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]); 286 + tab_rp_registers[i],
  287 + UT_INPUT_REGISTERS_TAB[i]);
280 } 288 }
281 289
282 /* MASKS */ 290 /* MASKS */
@@ -285,33 +293,36 @@ int main(int argc, char *argv[]) @@ -285,33 +293,36 @@ int main(int argc, char *argv[])
285 rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25); 293 rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
286 ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc); 294 ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
287 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers); 295 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
288 - ASSERT_TRUE(tab_rp_registers[0] == 0x17,  
289 - "FAILED (%0X != %0X)\n",  
290 - tab_rp_registers[0], 0x17); 296 + ASSERT_TRUE(
  297 + tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17);
291 298
292 printf("\nTEST FLOATS\n"); 299 printf("\nTEST FLOATS\n");
293 /** FLOAT **/ 300 /** FLOAT **/
294 printf("1/4 Set/get float ABCD: "); 301 printf("1/4 Set/get float ABCD: ");
295 modbus_set_float_abcd(UT_REAL, tab_rp_registers); 302 modbus_set_float_abcd(UT_REAL, tab_rp_registers);
296 - ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD_SET, 4), "FAILED Set float ABCD"); 303 + ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD_SET, 4),
  304 + "FAILED Set float ABCD");
297 real = modbus_get_float_abcd(UT_IREAL_ABCD_GET); 305 real = modbus_get_float_abcd(UT_IREAL_ABCD_GET);
298 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); 306 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
299 307
300 printf("2/4 Set/get float DCBA: "); 308 printf("2/4 Set/get float DCBA: ");
301 modbus_set_float_dcba(UT_REAL, tab_rp_registers); 309 modbus_set_float_dcba(UT_REAL, tab_rp_registers);
302 - ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA_SET, 4), "FAILED Set float DCBA"); 310 + ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA_SET, 4),
  311 + "FAILED Set float DCBA");
303 real = modbus_get_float_dcba(UT_IREAL_DCBA_GET); 312 real = modbus_get_float_dcba(UT_IREAL_DCBA_GET);
304 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); 313 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
305 314
306 printf("3/4 Set/get float BADC: "); 315 printf("3/4 Set/get float BADC: ");
307 modbus_set_float_badc(UT_REAL, tab_rp_registers); 316 modbus_set_float_badc(UT_REAL, tab_rp_registers);
308 - ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC_SET, 4), "FAILED Set float BADC"); 317 + ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC_SET, 4),
  318 + "FAILED Set float BADC");
309 real = modbus_get_float_badc(UT_IREAL_BADC_GET); 319 real = modbus_get_float_badc(UT_IREAL_BADC_GET);
310 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); 320 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
311 321
312 printf("4/4 Set/get float CDAB: "); 322 printf("4/4 Set/get float CDAB: ");
313 modbus_set_float_cdab(UT_REAL, tab_rp_registers); 323 modbus_set_float_cdab(UT_REAL, tab_rp_registers);
314 - ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB_SET, 4), "FAILED Set float CDAB"); 324 + ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB_SET, 4),
  325 + "FAILED Set float CDAB");
315 real = modbus_get_float_cdab(UT_IREAL_CDAB_GET); 326 real = modbus_get_float_cdab(UT_IREAL_CDAB_GET);
316 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); 327 ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
317 328
@@ -335,8 +346,8 @@ int main(int argc, char *argv[]) @@ -335,8 +346,8 @@ int main(int argc, char *argv[])
335 printf("* modbus_read_input_bits (0): "); 346 printf("* modbus_read_input_bits (0): ");
336 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 347 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
337 348
338 - rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,  
339 - UT_INPUT_BITS_NB + 1, tab_rp_bits); 349 + rc = modbus_read_input_bits(
  350 + ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB + 1, tab_rp_bits);
340 printf("* modbus_read_input_bits (max): "); 351 printf("* modbus_read_input_bits (max): ");
341 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 352 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
342 353
@@ -344,8 +355,8 @@ int main(int argc, char *argv[]) @@ -344,8 +355,8 @@ int main(int argc, char *argv[])
344 printf("* modbus_read_registers (0): "); 355 printf("* modbus_read_registers (0): ");
345 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 356 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
346 357
347 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
348 - UT_REGISTERS_NB_MAX + 1, tab_rp_registers); 358 + rc = modbus_read_registers(
  359 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX + 1, tab_rp_registers);
349 printf("* modbus_read_registers (max): "); 360 printf("* modbus_read_registers (max): ");
350 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 361 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
351 362
@@ -353,9 +364,8 @@ int main(int argc, char *argv[]) @@ -353,9 +364,8 @@ int main(int argc, char *argv[])
353 printf("* modbus_read_input_registers (0): "); 364 printf("* modbus_read_input_registers (0): ");
354 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 365 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
355 366
356 - rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,  
357 - UT_INPUT_REGISTERS_NB + 1,  
358 - tab_rp_registers); 367 + rc = modbus_read_input_registers(
  368 + ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB + 1, tab_rp_registers);
359 printf("* modbus_read_input_registers (max): "); 369 printf("* modbus_read_input_registers (max): ");
360 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 370 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
361 371
@@ -371,8 +381,7 @@ int main(int argc, char *argv[]) @@ -371,8 +381,7 @@ int main(int argc, char *argv[])
371 printf("* modbus_write_coils (0): "); 381 printf("* modbus_write_coils (0): ");
372 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 382 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
373 383
374 - rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,  
375 - UT_BITS_NB, tab_rp_bits); 384 + rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, UT_BITS_NB, tab_rp_bits);
376 printf("* modbus_write_coils (max): "); 385 printf("* modbus_write_coils (max): ");
377 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 386 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
378 387
@@ -380,8 +389,8 @@ int main(int argc, char *argv[]) @@ -380,8 +389,8 @@ int main(int argc, char *argv[])
380 printf("* modbus_write_register (0): "); 389 printf("* modbus_write_register (0): ");
381 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 390 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
382 391
383 - rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,  
384 - tab_rp_registers[0]); 392 + rc = modbus_write_register(
  393 + ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, tab_rp_registers[0]);
385 printf("* modbus_write_register (max): "); 394 printf("* modbus_write_register (max): ");
386 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 395 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
387 396
@@ -389,8 +398,10 @@ int main(int argc, char *argv[]) @@ -389,8 +398,10 @@ int main(int argc, char *argv[])
389 printf("* modbus_write_registers (0): "); 398 printf("* modbus_write_registers (0): ");
390 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 399 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
391 400
392 - rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,  
393 - UT_REGISTERS_NB, tab_rp_registers); 401 + rc = modbus_write_registers(ctx,
  402 + UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
  403 + UT_REGISTERS_NB,
  404 + tab_rp_registers);
394 printf("* modbus_write_registers (max): "); 405 printf("* modbus_write_registers (max): ");
395 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 406 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
396 407
@@ -398,56 +409,54 @@ int main(int argc, char *argv[]) @@ -398,56 +409,54 @@ int main(int argc, char *argv[])
398 printf("* modbus_mask_write_registers (0): "); 409 printf("* modbus_mask_write_registers (0): ");
399 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 410 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
400 411
401 - rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,  
402 - 0xF2, 0x25); 412 + rc = modbus_mask_write_register(
  413 + ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 0xF2, 0x25);
403 printf("* modbus_mask_write_registers (max): "); 414 printf("* modbus_mask_write_registers (max): ");
404 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 415 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
405 416
406 - rc = modbus_write_and_read_registers(ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers); 417 + rc = modbus_write_and_read_registers(
  418 + ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers);
407 printf("* modbus_write_and_read_registers (0): "); 419 printf("* modbus_write_and_read_registers (0): ");
408 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 420 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
409 421
410 rc = modbus_write_and_read_registers(ctx, 422 rc = modbus_write_and_read_registers(ctx,
411 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 423 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
412 - UT_REGISTERS_NB, tab_rp_registers, 424 + UT_REGISTERS_NB,
  425 + tab_rp_registers,
413 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 426 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
414 - UT_REGISTERS_NB, tab_rp_registers); 427 + UT_REGISTERS_NB,
  428 + tab_rp_registers);
415 printf("* modbus_write_and_read_registers (max): "); 429 printf("* modbus_write_and_read_registers (max): ");
416 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); 430 ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
417 431
418 /** TOO MANY DATA **/ 432 /** TOO MANY DATA **/
419 printf("\nTEST TOO MANY DATA ERROR:\n"); 433 printf("\nTEST TOO MANY DATA ERROR:\n");
420 434
421 - rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,  
422 - MODBUS_MAX_READ_BITS + 1, tab_rp_bits); 435 + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
423 printf("* modbus_read_bits: "); 436 printf("* modbus_read_bits: ");
424 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 437 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
425 438
426 - rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,  
427 - MODBUS_MAX_READ_BITS + 1, tab_rp_bits); 439 + rc = modbus_read_input_bits(
  440 + ctx, UT_INPUT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
428 printf("* modbus_read_input_bits: "); 441 printf("* modbus_read_input_bits: ");
429 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 442 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
430 443
431 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
432 - MODBUS_MAX_READ_REGISTERS + 1,  
433 - tab_rp_registers); 444 + rc = modbus_read_registers(
  445 + ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
434 printf("* modbus_read_registers: "); 446 printf("* modbus_read_registers: ");
435 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 447 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
436 448
437 - rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,  
438 - MODBUS_MAX_READ_REGISTERS + 1,  
439 - tab_rp_registers); 449 + rc = modbus_read_input_registers(
  450 + ctx, UT_INPUT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
440 printf("* modbus_read_input_registers: "); 451 printf("* modbus_read_input_registers: ");
441 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 452 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
442 453
443 - rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,  
444 - MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits); 454 + rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
445 printf("* modbus_write_bits: "); 455 printf("* modbus_write_bits: ");
446 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 456 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
447 457
448 - rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,  
449 - MODBUS_MAX_WRITE_REGISTERS + 1,  
450 - tab_rp_registers); 458 + rc = modbus_write_registers(
  459 + ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_WRITE_REGISTERS + 1, tab_rp_registers);
451 printf("* modbus_write_registers: "); 460 printf("* modbus_write_registers: ");
452 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); 461 ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
453 462
@@ -473,15 +482,15 @@ int main(int argc, char *argv[]) @@ -473,15 +482,15 @@ int main(int argc, char *argv[])
473 482
474 printf("\nTEST SLAVE REPLY:\n"); 483 printf("\nTEST SLAVE REPLY:\n");
475 modbus_set_slave(ctx, INVALID_SERVER_ID); 484 modbus_set_slave(ctx, INVALID_SERVER_ID);
476 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
477 - UT_REGISTERS_NB, tab_rp_registers); 485 + rc = modbus_read_registers(
  486 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
478 if (use_backend == RTU) { 487 if (use_backend == RTU) {
479 const int RAW_REQ_LENGTH = 6; 488 const int RAW_REQ_LENGTH = 6;
480 - uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 }; 489 + uint8_t raw_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01};
481 /* Too many points */ 490 /* Too many points */
482 - uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF }; 491 + uint8_t raw_invalid_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF};
483 const int RAW_RSP_LENGTH = 7; 492 const int RAW_RSP_LENGTH = 7;
484 - uint8_t raw_rsp[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 }; 493 + uint8_t raw_rsp[] = {INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0};
485 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH]; 494 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
486 495
487 /* No response in RTU mode */ 496 /* No response in RTU mode */
@@ -491,7 +500,6 @@ int main(int argc, char *argv[]) @@ -491,7 +500,6 @@ int main(int argc, char *argv[])
491 /* The slave raises a timeout on a confirmation to ignore because if an 500 /* The slave raises a timeout on a confirmation to ignore because if an
492 * indication for another slave is received, a confirmation must follow */ 501 * indication for another slave is received, a confirmation must follow */
493 502
494 -  
495 /* Send a pair of indication/confirmation to the slave with a different 503 /* Send a pair of indication/confirmation to the slave with a different
496 * slave ID to simulate a communication on a RS485 bus. At first, the 504 * slave ID to simulate a communication on a RS485 bus. At first, the
497 * slave will see the indication message then the confirmation, and it must 505 * slave will see the indication message then the confirmation, and it must
@@ -515,8 +523,8 @@ int main(int argc, char *argv[]) @@ -515,8 +523,8 @@ int main(int argc, char *argv[])
515 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); 523 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
516 ASSERT_TRUE(rc == 0, "Invalid broadcast address"); 524 ASSERT_TRUE(rc == 0, "Invalid broadcast address");
517 525
518 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
519 - UT_REGISTERS_NB, tab_rp_registers); 526 + rc = modbus_read_registers(
  527 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
520 printf("2/3 No reply after a broadcast query: "); 528 printf("2/3 No reply after a broadcast query: ");
521 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 529 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
522 } else { 530 } else {
@@ -527,8 +535,8 @@ int main(int argc, char *argv[]) @@ -527,8 +535,8 @@ int main(int argc, char *argv[])
527 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); 535 rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
528 ASSERT_TRUE(rc == 0, "Invalid broacast address"); 536 ASSERT_TRUE(rc == 0, "Invalid broacast address");
529 537
530 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
531 - UT_REGISTERS_NB, tab_rp_registers); 538 + rc = modbus_read_registers(
  539 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
532 printf("2/3 Reply after a query with unit id == 0: "); 540 printf("2/3 Reply after a query with unit id == 0: ");
533 ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); 541 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
534 } 542 }
@@ -537,8 +545,8 @@ int main(int argc, char *argv[]) @@ -537,8 +545,8 @@ int main(int argc, char *argv[])
537 modbus_set_slave(ctx, old_slave); 545 modbus_set_slave(ctx, old_slave);
538 546
539 printf("3/3 Response with an invalid TID or slave: "); 547 printf("3/3 Response with an invalid TID or slave: ");
540 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,  
541 - 1, tab_rp_registers); 548 + rc = modbus_read_registers(
  549 + ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 1, tab_rp_registers);
542 ASSERT_TRUE(rc == -1, ""); 550 ASSERT_TRUE(rc == -1, "");
543 551
544 printf("1/2 Report slave ID truncated: \n"); 552 printf("1/2 Report slave ID truncated: \n");
@@ -546,10 +554,11 @@ int main(int argc, char *argv[]) @@ -546,10 +554,11 @@ int main(int argc, char *argv[])
546 tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42; 554 tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
547 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits); 555 rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
548 /* Return the size required (response size) but respects the defined limit */ 556 /* Return the size required (response size) but respects the defined limit */
549 - ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&  
550 - tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42, 557 + ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
551 "Return is rc %d (%d) and marker is %d (42)", 558 "Return is rc %d (%d) and marker is %d (42)",
552 - rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]); 559 + rc,
  560 + NB_REPORT_SLAVE_ID,
  561 + tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
553 562
554 printf("2/2 Report slave ID: \n"); 563 printf("2/2 Report slave ID: \n");
555 /* tab_rp_bits is used to store bytes */ 564 /* tab_rp_bits is used to store bytes */
@@ -565,7 +574,7 @@ int main(int argc, char *argv[]) @@ -565,7 +574,7 @@ int main(int argc, char *argv[])
565 /* Print additional data as string */ 574 /* Print additional data as string */
566 if (rc > 2) { 575 if (rc > 2) {
567 printf("Additional data: "); 576 printf("Additional data: ");
568 - for (i=2; i < rc; i++) { 577 + for (i = 2; i < rc; i++) {
569 printf("%c", tab_rp_bits[i]); 578 printf("%c", tab_rp_bits[i]);
570 } 579 }
571 printf("\n"); 580 printf("\n");
@@ -588,8 +597,8 @@ int main(int argc, char *argv[]) @@ -588,8 +597,8 @@ int main(int argc, char *argv[])
588 ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); 597 ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
589 598
590 modbus_set_response_timeout(ctx, 0, 1); 599 modbus_set_response_timeout(ctx, 0, 1);
591 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
592 - UT_REGISTERS_NB, tab_rp_registers); 600 + rc = modbus_read_registers(
  601 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
593 printf("4/6 1us response timeout: "); 602 printf("4/6 1us response timeout: ");
594 if (rc == -1 && errno == ETIMEDOUT) { 603 if (rc == -1 && errno == ETIMEDOUT) {
595 printf("OK\n"); 604 printf("OK\n");
@@ -607,8 +616,8 @@ int main(int argc, char *argv[]) @@ -607,8 +616,8 @@ int main(int argc, char *argv[])
607 /* Trigger a special behaviour on server to wait for 0.5 second before 616 /* Trigger a special behaviour on server to wait for 0.5 second before
608 * replying whereas allowed timeout is 0.2 second */ 617 * replying whereas allowed timeout is 0.2 second */
609 modbus_set_response_timeout(ctx, 0, 200000); 618 modbus_set_response_timeout(ctx, 0, 200000);
610 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,  
611 - 1, tab_rp_registers); 619 + rc = modbus_read_registers(
  620 + ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
612 printf("5/6 Too short response timeout (0.2s < 0.5s): "); 621 printf("5/6 Too short response timeout (0.2s < 0.5s): ");
613 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 622 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
614 623
@@ -617,22 +626,21 @@ int main(int argc, char *argv[]) @@ -617,22 +626,21 @@ int main(int argc, char *argv[])
617 modbus_flush(ctx); 626 modbus_flush(ctx);
618 627
619 modbus_set_response_timeout(ctx, 0, 600000); 628 modbus_set_response_timeout(ctx, 0, 600000);
620 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,  
621 - 1, tab_rp_registers); 629 + rc = modbus_read_registers(
  630 + ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
622 printf("6/6 Adequate response timeout (0.6s > 0.5s): "); 631 printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
623 ASSERT_TRUE(rc == 1, ""); 632 ASSERT_TRUE(rc == 1, "");
624 633
625 /* Disable the byte timeout. 634 /* Disable the byte timeout.
626 The full response must be available in the 600ms interval */ 635 The full response must be available in the 600ms interval */
627 modbus_set_byte_timeout(ctx, 0, 0); 636 modbus_set_byte_timeout(ctx, 0, 0);
628 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,  
629 - 1, tab_rp_registers); 637 + rc = modbus_read_registers(
  638 + ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
630 printf("7/7 Disable byte timeout: "); 639 printf("7/7 Disable byte timeout: ");
631 ASSERT_TRUE(rc == 1, ""); 640 ASSERT_TRUE(rc == 1, "");
632 641
633 /* Restore original response timeout */ 642 /* Restore original response timeout */
634 - modbus_set_response_timeout(ctx, old_response_to_sec,  
635 - old_response_to_usec); 643 + modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
636 644
637 if (use_backend == TCP) { 645 if (use_backend == TCP) {
638 /* The test server is only able to test byte timeouts with the TCP 646 /* The test server is only able to test byte timeouts with the TCP
@@ -640,8 +648,8 @@ int main(int argc, char *argv[]) @@ -640,8 +648,8 @@ int main(int argc, char *argv[])
640 648
641 /* Timeout of 3ms between bytes */ 649 /* Timeout of 3ms between bytes */
642 modbus_set_byte_timeout(ctx, 0, 3000); 650 modbus_set_byte_timeout(ctx, 0, 3000);
643 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,  
644 - 1, tab_rp_registers); 651 + rc = modbus_read_registers(
  652 + ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
645 printf("1/2 Too small byte timeout (3ms < 5ms): "); 653 printf("1/2 Too small byte timeout (3ms < 5ms): ");
646 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); 654 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
647 655
@@ -651,8 +659,8 @@ int main(int argc, char *argv[]) @@ -651,8 +659,8 @@ int main(int argc, char *argv[])
651 659
652 /* Timeout of 7ms between bytes */ 660 /* Timeout of 7ms between bytes */
653 modbus_set_byte_timeout(ctx, 0, 7000); 661 modbus_set_byte_timeout(ctx, 0, 7000);
654 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,  
655 - 1, tab_rp_registers); 662 + rc = modbus_read_registers(
  663 + ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
656 printf("2/2 Adapted byte timeout (7ms > 5ms): "); 664 printf("2/2 Adapted byte timeout (7ms > 5ms): ");
657 ASSERT_TRUE(rc == 1, ""); 665 ASSERT_TRUE(rc == 1, "");
658 } 666 }
@@ -664,19 +672,19 @@ int main(int argc, char *argv[]) @@ -664,19 +672,19 @@ int main(int argc, char *argv[])
664 printf("\nTEST BAD RESPONSE ERROR:\n"); 672 printf("\nTEST BAD RESPONSE ERROR:\n");
665 673
666 /* Allocate only the required space */ 674 /* Allocate only the required space */
667 - tab_rp_registers_bad = (uint16_t *) malloc(  
668 - UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t)); 675 + tab_rp_registers_bad =
  676 + (uint16_t *) malloc(UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
669 677
670 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,  
671 - UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad); 678 + rc = modbus_read_registers(
  679 + ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
672 printf("* modbus_read_registers: "); 680 printf("* modbus_read_registers: ");
673 ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, ""); 681 ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
674 free(tab_rp_registers_bad); 682 free(tab_rp_registers_bad);
675 683
676 /** MANUAL EXCEPTION **/ 684 /** MANUAL EXCEPTION **/
677 printf("\nTEST MANUAL EXCEPTION:\n"); 685 printf("\nTEST MANUAL EXCEPTION:\n");
678 - rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,  
679 - UT_REGISTERS_NB, tab_rp_registers); 686 + rc = modbus_read_registers(
  687 + ctx, UT_REGISTERS_ADDRESS_SPECIAL, UT_REGISTERS_NB, tab_rp_registers);
680 688
681 printf("* modbus_read_registers at special address: "); 689 printf("* modbus_read_registers at special address: ");
682 ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, ""); 690 ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
@@ -722,63 +730,65 @@ int test_server(modbus_t *ctx, int use_backend) @@ -722,63 +730,65 @@ int test_server(modbus_t *ctx, int use_backend)
722 /* Read requests */ 730 /* Read requests */
723 const int READ_RAW_REQ_LEN = 6; 731 const int READ_RAW_REQ_LEN = 6;
724 const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE; 732 const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE;
725 - uint8_t read_raw_req[] = {  
726 - slave,  
727 - /* function, address, 5 values */  
728 - MODBUS_FC_READ_HOLDING_REGISTERS,  
729 - UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,  
730 - 0x0, 0x05  
731 - }; 733 + uint8_t read_raw_req[] = {slave,
  734 + /* function, address, 5 values */
  735 + MODBUS_FC_READ_HOLDING_REGISTERS,
  736 + UT_REGISTERS_ADDRESS >> 8,
  737 + UT_REGISTERS_ADDRESS & 0xFF,
  738 + 0x0,
  739 + 0x05};
732 /* Write and read registers request */ 740 /* Write and read registers request */
733 const int RW_RAW_REQ_LEN = 13; 741 const int RW_RAW_REQ_LEN = 13;
734 - uint8_t rw_raw_req[] = {  
735 - slave,  
736 - /* function, addr to read, nb to read */  
737 - MODBUS_FC_WRITE_AND_READ_REGISTERS,  
738 - /* Read */  
739 - UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,  
740 - (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,  
741 - (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,  
742 - /* Write */  
743 - 0, 0,  
744 - 0, 1,  
745 - /* Write byte count */  
746 - 1 * 2,  
747 - /* One data to write... */  
748 - 0x12, 0x34  
749 - }; 742 + uint8_t rw_raw_req[] = {slave,
  743 + /* function, addr to read, nb to read */
  744 + MODBUS_FC_WRITE_AND_READ_REGISTERS,
  745 + /* Read */
  746 + UT_REGISTERS_ADDRESS >> 8,
  747 + UT_REGISTERS_ADDRESS & 0xFF,
  748 + (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
  749 + (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
  750 + /* Write */
  751 + 0,
  752 + 0,
  753 + 0,
  754 + 1,
  755 + /* Write byte count */
  756 + 1 * 2,
  757 + /* One data to write... */
  758 + 0x12,
  759 + 0x34};
750 const int WRITE_RAW_REQ_LEN = 13; 760 const int WRITE_RAW_REQ_LEN = 13;
751 - uint8_t write_raw_req[] = {  
752 - slave,  
753 - /* function will be set in the loop */  
754 - MODBUS_FC_WRITE_MULTIPLE_REGISTERS,  
755 - /* Address */  
756 - UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,  
757 - /* 3 values, 6 bytes */  
758 - 0x00, 0x03, 0x06,  
759 - /* Dummy data to write */  
760 - 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64  
761 - }; 761 + uint8_t write_raw_req[] = {slave,
  762 + /* function will be set in the loop */
  763 + MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  764 + /* Address */
  765 + UT_REGISTERS_ADDRESS >> 8,
  766 + UT_REGISTERS_ADDRESS & 0xFF,
  767 + /* 3 values, 6 bytes */
  768 + 0x00,
  769 + 0x03,
  770 + 0x06,
  771 + /* Dummy data to write */
  772 + 0x02,
  773 + 0x2B,
  774 + 0x00,
  775 + 0x01,
  776 + 0x00,
  777 + 0x64};
762 const int INVALID_FC = 0x42; 778 const int INVALID_FC = 0x42;
763 const int INVALID_FC_REQ_LEN = 6; 779 const int INVALID_FC_REQ_LEN = 6;
764 - uint8_t invalid_fc_raw_req[] = {  
765 - slave, 0x42, 0x00, 0x00, 0x00, 0x00  
766 - }; 780 + uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00};
767 781
768 int req_length; 782 int req_length;
769 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; 783 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
770 - int tab_read_function[] = {  
771 - MODBUS_FC_READ_COILS,  
772 - MODBUS_FC_READ_DISCRETE_INPUTS,  
773 - MODBUS_FC_READ_HOLDING_REGISTERS,  
774 - MODBUS_FC_READ_INPUT_REGISTERS  
775 - };  
776 - int tab_read_nb_max[] = {  
777 - MODBUS_MAX_READ_BITS + 1,  
778 - MODBUS_MAX_READ_BITS + 1,  
779 - MODBUS_MAX_READ_REGISTERS + 1,  
780 - MODBUS_MAX_READ_REGISTERS + 1  
781 - }; 784 + int tab_read_function[] = {MODBUS_FC_READ_COILS,
  785 + MODBUS_FC_READ_DISCRETE_INPUTS,
  786 + MODBUS_FC_READ_HOLDING_REGISTERS,
  787 + MODBUS_FC_READ_INPUT_REGISTERS};
  788 + int tab_read_nb_max[] = {MODBUS_MAX_READ_BITS + 1,
  789 + MODBUS_MAX_READ_BITS + 1,
  790 + MODBUS_MAX_READ_REGISTERS + 1,
  791 + MODBUS_MAX_READ_REGISTERS + 1};
782 int backend_length; 792 int backend_length;
783 int backend_offset; 793 int backend_offset;
784 794
@@ -820,58 +830,84 @@ int test_server(modbus_t *ctx, int use_backend) @@ -820,58 +830,84 @@ int test_server(modbus_t *ctx, int use_backend)
820 830
821 /* Try to read more values than a response could hold for all data 831 /* Try to read more values than a response could hold for all data
822 types. */ 832 types. */
823 - for (i=0; i<4; i++) {  
824 - rc = send_crafted_request(ctx, tab_read_function[i],  
825 - read_raw_req, READ_RAW_REQ_LEN,  
826 - tab_read_nb_max[i], 0,  
827 - backend_length, backend_offset); 833 + for (i = 0; i < 4; i++) {
  834 + rc = send_crafted_request(ctx,
  835 + tab_read_function[i],
  836 + read_raw_req,
  837 + READ_RAW_REQ_LEN,
  838 + tab_read_nb_max[i],
  839 + 0,
  840 + backend_length,
  841 + backend_offset);
828 if (rc == -1) 842 if (rc == -1)
829 goto close; 843 goto close;
830 } 844 }
831 845
832 - rc = send_crafted_request(ctx, MODBUS_FC_WRITE_AND_READ_REGISTERS,  
833 - rw_raw_req, RW_RAW_REQ_LEN,  
834 - MODBUS_MAX_WR_READ_REGISTERS + 1, 0,  
835 - backend_length, backend_offset); 846 + rc = send_crafted_request(ctx,
  847 + MODBUS_FC_WRITE_AND_READ_REGISTERS,
  848 + rw_raw_req,
  849 + RW_RAW_REQ_LEN,
  850 + MODBUS_MAX_WR_READ_REGISTERS + 1,
  851 + 0,
  852 + backend_length,
  853 + backend_offset);
836 if (rc == -1) 854 if (rc == -1)
837 goto close; 855 goto close;
838 856
839 - rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,  
840 - write_raw_req, WRITE_RAW_REQ_LEN,  
841 - MODBUS_MAX_WRITE_REGISTERS + 1, 6,  
842 - backend_length, backend_offset); 857 + rc = send_crafted_request(ctx,
  858 + MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  859 + write_raw_req,
  860 + WRITE_RAW_REQ_LEN,
  861 + MODBUS_MAX_WRITE_REGISTERS + 1,
  862 + 6,
  863 + backend_length,
  864 + backend_offset);
843 if (rc == -1) 865 if (rc == -1)
844 goto close; 866 goto close;
845 867
846 - rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,  
847 - write_raw_req, WRITE_RAW_REQ_LEN,  
848 - MODBUS_MAX_WRITE_BITS + 1, 6,  
849 - backend_length, backend_offset); 868 + rc = send_crafted_request(ctx,
  869 + MODBUS_FC_WRITE_MULTIPLE_COILS,
  870 + write_raw_req,
  871 + WRITE_RAW_REQ_LEN,
  872 + MODBUS_MAX_WRITE_BITS + 1,
  873 + 6,
  874 + backend_length,
  875 + backend_offset);
850 if (rc == -1) 876 if (rc == -1)
851 goto close; 877 goto close;
852 878
853 /* Modbus write multiple registers with large number of values but a set a 879 /* Modbus write multiple registers with large number of values but a set a
854 small number of bytes in requests (not nb * 2 as usual). */ 880 small number of bytes in requests (not nb * 2 as usual). */
855 - rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,  
856 - write_raw_req, WRITE_RAW_REQ_LEN,  
857 - MODBUS_MAX_WRITE_REGISTERS, 6,  
858 - backend_length, backend_offset); 881 + rc = send_crafted_request(ctx,
  882 + MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  883 + write_raw_req,
  884 + WRITE_RAW_REQ_LEN,
  885 + MODBUS_MAX_WRITE_REGISTERS,
  886 + 6,
  887 + backend_length,
  888 + backend_offset);
859 if (rc == -1) 889 if (rc == -1)
860 goto close; 890 goto close;
861 891
862 - rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,  
863 - write_raw_req, WRITE_RAW_REQ_LEN,  
864 - MODBUS_MAX_WRITE_BITS, 6,  
865 - backend_length, backend_offset); 892 + rc = send_crafted_request(ctx,
  893 + MODBUS_FC_WRITE_MULTIPLE_COILS,
  894 + write_raw_req,
  895 + WRITE_RAW_REQ_LEN,
  896 + MODBUS_MAX_WRITE_BITS,
  897 + 6,
  898 + backend_length,
  899 + backend_offset);
866 if (rc == -1) 900 if (rc == -1)
867 goto close; 901 goto close;
868 902
869 /* Test invalid function code */ 903 /* Test invalid function code */
870 - modbus_send_raw_request(ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t)); 904 + modbus_send_raw_request(
  905 + ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
871 rc = modbus_receive_confirmation(ctx, rsp); 906 rc = modbus_receive_confirmation(ctx, rsp);
872 printf("Return an exception on unknown function code: "); 907 printf("Return an exception on unknown function code: ");
873 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && 908 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
874 - rsp[backend_offset] == (0x80 + INVALID_FC), "") 909 + rsp[backend_offset] == (0x80 + INVALID_FC),
  910 + "")
875 911
876 modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); 912 modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
877 return 0; 913 return 0;
@@ -880,16 +916,19 @@ close: @@ -880,16 +916,19 @@ close:
880 return -1; 916 return -1;
881 } 917 }
882 918
883 -  
884 -int send_crafted_request(modbus_t *ctx, int function,  
885 - uint8_t *req, int req_len,  
886 - uint16_t max_value, uint16_t bytes,  
887 - int backend_length, int backend_offset) 919 +int send_crafted_request(modbus_t *ctx,
  920 + int function,
  921 + uint8_t *req,
  922 + int req_len,
  923 + uint16_t max_value,
  924 + uint16_t bytes,
  925 + int backend_length,
  926 + int backend_offset)
888 { 927 {
889 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; 928 uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
890 int j; 929 int j;
891 930
892 - for (j=0; j<2; j++) { 931 + for (j = 0; j < 2; j++) {
893 int rc; 932 int rc;
894 933
895 req[1] = function; 934 req[1] = function;
@@ -913,15 +952,19 @@ int send_crafted_request(modbus_t *ctx, int function, @@ -913,15 +952,19 @@ int send_crafted_request(modbus_t *ctx, int function,
913 952
914 modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t)); 953 modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
915 if (j == 0) { 954 if (j == 0) {
916 - printf("* try function 0x%X: %s 0 values: ", function, bytes ? "write": "read"); 955 + printf(
  956 + "* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read");
917 } else { 957 } else {
918 - printf("* try function 0x%X: %s %d values: ", function, bytes ? "write": "read", 958 + printf("* try function 0x%X: %s %d values: ",
  959 + function,
  960 + bytes ? "write" : "read",
919 max_value); 961 max_value);
920 } 962 }
921 rc = modbus_receive_confirmation(ctx, rsp); 963 rc = modbus_receive_confirmation(ctx, rsp);
922 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && 964 ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
923 - rsp[backend_offset] == (0x80 + function) &&  
924 - rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, ""); 965 + rsp[backend_offset] == (0x80 + function) &&
  966 + rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
  967 + "");
925 } 968 }
926 return 0; 969 return 0;
927 close: 970 close:
tests/unit-test-server.c
@@ -4,12 +4,14 @@ @@ -4,12 +4,14 @@
4 * SPDX-License-Identifier: BSD-3-Clause 4 * SPDX-License-Identifier: BSD-3-Clause
5 */ 5 */
6 6
7 -#include <stdio.h>  
8 -#include <unistd.h>  
9 -#include <string.h>  
10 -#include <stdlib.h>  
11 #include <errno.h> 7 #include <errno.h>
12 #include <modbus.h> 8 #include <modbus.h>
  9 +#include <stdio.h>
  10 +#include <stdlib.h>
  11 +#include <string.h>
  12 +#include <unistd.h>
  13 +
  14 +// clang-format off
13 #ifdef _WIN32 15 #ifdef _WIN32
14 # include <winsock2.h> 16 # include <winsock2.h>
15 #else 17 #else
@@ -20,6 +22,7 @@ @@ -20,6 +22,7 @@
20 #ifndef MSG_NOSIGNAL 22 #ifndef MSG_NOSIGNAL
21 # define MSG_NOSIGNAL 0 23 # define MSG_NOSIGNAL 0
22 #endif 24 #endif
  25 +// clang-format on
23 26
24 #include "unit-test.h" 27 #include "unit-test.h"
25 28
@@ -29,7 +32,7 @@ enum { @@ -29,7 +32,7 @@ enum {
29 RTU 32 RTU
30 }; 33 };
31 34
32 -int main(int argc, char*argv[]) 35 +int main(int argc, char *argv[])
33 { 36 {
34 int s = -1; 37 int s = -1;
35 modbus_t *ctx; 38 modbus_t *ctx;
@@ -48,7 +51,8 @@ int main(int argc, char*argv[]) @@ -48,7 +51,8 @@ int main(int argc, char*argv[])
48 } else if (strcmp(argv[1], "rtu") == 0) { 51 } else if (strcmp(argv[1], "rtu") == 0) {
49 use_backend = RTU; 52 use_backend = RTU;
50 } else { 53 } else {
51 - printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]); 54 + printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n",
  55 + argv[0]);
52 return -1; 56 return -1;
53 } 57 }
54 } else { 58 } else {
@@ -71,14 +75,16 @@ int main(int argc, char*argv[]) @@ -71,14 +75,16 @@ int main(int argc, char*argv[])
71 75
72 modbus_set_debug(ctx, TRUE); 76 modbus_set_debug(ctx, TRUE);
73 77
74 - mb_mapping = modbus_mapping_new_start_address(  
75 - UT_BITS_ADDRESS, UT_BITS_NB,  
76 - UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,  
77 - UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX,  
78 - UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB); 78 + mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS,
  79 + UT_BITS_NB,
  80 + UT_INPUT_BITS_ADDRESS,
  81 + UT_INPUT_BITS_NB,
  82 + UT_REGISTERS_ADDRESS,
  83 + UT_REGISTERS_NB_MAX,
  84 + UT_INPUT_REGISTERS_ADDRESS,
  85 + UT_INPUT_REGISTERS_NB);
79 if (mb_mapping == NULL) { 86 if (mb_mapping == NULL) {
80 - fprintf(stderr, "Failed to allocate the mapping: %s\n",  
81 - modbus_strerror(errno)); 87 + fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
82 modbus_free(ctx); 88 modbus_free(ctx);
83 return -1; 89 return -1;
84 } 90 }
@@ -87,11 +93,11 @@ int main(int argc, char*argv[]) @@ -87,11 +93,11 @@ int main(int argc, char*argv[])
87 Only the read-only input values are assigned. */ 93 Only the read-only input values are assigned. */
88 94
89 /* Initialize input values that's can be only done server side. */ 95 /* Initialize input values that's can be only done server side. */
90 - modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB,  
91 - UT_INPUT_BITS_TAB); 96 + modbus_set_bits_from_bytes(
  97 + mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB);
92 98
93 /* Initialize values of INPUT REGISTERS */ 99 /* Initialize values of INPUT REGISTERS */
94 - for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { 100 + for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
95 mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i]; 101 mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];
96 } 102 }
97 103
@@ -127,35 +133,34 @@ int main(int argc, char*argv[]) @@ -127,35 +133,34 @@ int main(int argc, char*argv[])
127 if (query[header_length] == 0x03) { 133 if (query[header_length] == 0x03) {
128 /* Read holding registers */ 134 /* Read holding registers */
129 135
130 - if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)  
131 - == UT_REGISTERS_NB_SPECIAL) { 136 + if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) ==
  137 + UT_REGISTERS_NB_SPECIAL) {
132 printf("Set an incorrect number of values\n"); 138 printf("Set an incorrect number of values\n");
133 - MODBUS_SET_INT16_TO_INT8(query, header_length + 3,  
134 - UT_REGISTERS_NB_SPECIAL - 1);  
135 - } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)  
136 - == UT_REGISTERS_ADDRESS_SPECIAL) { 139 + MODBUS_SET_INT16_TO_INT8(
  140 + query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
  141 + } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  142 + UT_REGISTERS_ADDRESS_SPECIAL) {
137 printf("Reply to this special register address by an exception\n"); 143 printf("Reply to this special register address by an exception\n");
138 - modbus_reply_exception(ctx, query,  
139 - MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY); 144 + modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
140 continue; 145 continue;
141 - } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)  
142 - == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) { 146 + } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  147 + UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
143 const int RAW_REQ_LENGTH = 5; 148 const int RAW_REQ_LENGTH = 5;
144 - uint8_t raw_req[] = {  
145 - (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,  
146 - 0x03,  
147 - 0x02, 0x00, 0x00  
148 - }; 149 + uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  150 + 0x03,
  151 + 0x02,
  152 + 0x00,
  153 + 0x00};
149 154
150 printf("Reply with an invalid TID or slave\n"); 155 printf("Reply with an invalid TID or slave\n");
151 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t)); 156 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
152 continue; 157 continue;
153 - } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)  
154 - == UT_REGISTERS_ADDRESS_SLEEP_500_MS) { 158 + } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  159 + UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
155 printf("Sleep 0.5 s before replying\n"); 160 printf("Sleep 0.5 s before replying\n");
156 usleep(500000); 161 usleep(500000);
157 - } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)  
158 - == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) { 162 + } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  163 + UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
159 /* Test low level only available in TCP mode */ 164 /* Test low level only available in TCP mode */
160 /* Catch the reply and send reply byte a byte */ 165 /* Catch the reply and send reply byte a byte */
161 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00"; 166 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
@@ -168,10 +173,10 @@ int main(int argc, char*argv[]) @@ -168,10 +173,10 @@ int main(int argc, char*argv[])
168 173
169 /* Copy TID */ 174 /* Copy TID */
170 req[1] = query[1]; 175 req[1] = query[1];
171 - for (i=0; i < req_length; i++) { 176 + for (i = 0; i < req_length; i++) {
172 printf("(%.2X)", req[i]); 177 printf("(%.2X)", req[i]);
173 usleep(5000); 178 usleep(5000);
174 - rc = send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL); 179 + rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL);
175 if (rc == -1) { 180 if (rc == -1) {
176 break; 181 break;
177 } 182 }
tests/unit-test.h.in
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 #define HAVE_INTTYPES_H @HAVE_INTTYPES_H@ 11 #define HAVE_INTTYPES_H @HAVE_INTTYPES_H@
12 #define HAVE_STDINT_H @HAVE_STDINT_H@ 12 #define HAVE_STDINT_H @HAVE_STDINT_H@
13 13
  14 +// clang-format off
14 #ifdef HAVE_INTTYPES_H 15 #ifdef HAVE_INTTYPES_H
15 #include <inttypes.h> 16 #include <inttypes.h>
16 #endif 17 #endif
@@ -21,6 +22,7 @@ @@ -21,6 +22,7 @@
21 # include "stdint.h" 22 # include "stdint.h"
22 # endif 23 # endif
23 #endif 24 #endif
  25 +// clang-format on
24 26
25 #define SERVER_ID 17 27 #define SERVER_ID 17
26 #define INVALID_SERVER_ID 18 28 #define INVALID_SERVER_ID 18
tests/version.c
@@ -4,14 +4,18 @@ @@ -4,14 +4,18 @@
4 * SPDX-License-Identifier: BSD-3-Clause 4 * SPDX-License-Identifier: BSD-3-Clause
5 */ 5 */
6 6
7 -#include <stdio.h>  
8 #include <modbus.h> 7 #include <modbus.h>
  8 +#include <stdio.h>
9 9
10 int main(void) 10 int main(void)
11 { 11 {
12 - printf("Compiled with libmodbus version %s (%06X)\n", LIBMODBUS_VERSION_STRING, LIBMODBUS_VERSION_HEX); 12 + printf("Compiled with libmodbus version %s (%06X)\n",
  13 + LIBMODBUS_VERSION_STRING,
  14 + LIBMODBUS_VERSION_HEX);
13 printf("Linked with libmodbus version %d.%d.%d\n", 15 printf("Linked with libmodbus version %d.%d.%d\n",
14 - libmodbus_version_major, libmodbus_version_minor, libmodbus_version_micro); 16 + libmodbus_version_major,
  17 + libmodbus_version_minor,
  18 + libmodbus_version_micro);
15 19
16 if (LIBMODBUS_VERSION_CHECK(2, 1, 0)) { 20 if (LIBMODBUS_VERSION_CHECK(2, 1, 0)) {
17 printf("The functions to read/write float values are available (2.1.0).\n"); 21 printf("The functions to read/write float values are available (2.1.0).\n");