Эх сурвалжийг харах

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

Thank you @pboettch for initial proposal.
Stéphane Raimbault 2 жил өмнө
parent
commit
dd45f19a6e

+ 56 - 0
.clang-format

@@ -0,0 +1,56 @@
+---
+BasedOnStyle: LLVM
+AlignArrayOfStructures: Left
+AlignOperands: true
+AlignConsecutiveAssignments: false
+AlignConsecutiveMacros: true
+AlignEscapedNewlines: Left
+AlignTrailingComments: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortEnumsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: Never
+BreakBeforeBraces: Custom
+BraceWrapping:
+    AfterClass: false
+    AfterControlStatement: false
+    AfterEnum: false
+    AfterFunction: true
+    AfterNamespace: true
+    AfterObjCDeclaration: true
+    AfterStruct: false
+    AfterUnion: false
+    AfterExternBlock: false
+    BeforeCatch: false
+    BeforeElse: false
+    IndentBraces: false
+    SplitEmptyFunction: true
+    SplitEmptyRecord: true
+    SplitEmptyNamespace: true
+BinPackArguments: false
+BinPackParameters: false
+ColumnLimit: 90
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+IncludeBlocks: Preserve
+IndentWidth: 4
+ObjCBlockIndentWidth: 4
+PointerAlignment: Right
+ReferenceAlignment: Right
+SpaceAfterCStyleCast: true
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SeparateDefinitionBlocks: Always
+UseTab: Never
+PPIndentWidth: 2

+ 21 - 32
src/modbus-data.c

@@ -6,6 +6,7 @@
 
 #include <stdlib.h>
 
+// clang-format off
 #ifndef _MSC_VER
 #  include <stdint.h>
 #else
@@ -69,6 +70,7 @@ static inline uint32_t bswap_32(uint32_t x)
     return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16));
 }
 #endif
+// clang-format on
 
 /* Sets many bits from a single byte value (all 8 bits of the byte value are
    set) */
@@ -76,14 +78,16 @@ void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value)
 {
     int i;
 
-    for (i=0; i < 8; i++) {
-        dest[idx+i] = (value & (1 << i)) ? 1 : 0;
+    for (i = 0; i < 8; i++) {
+        dest[idx + i] = (value & (1 << i)) ? 1 : 0;
     }
 }
 
 /* Sets many bits from a table of bytes (only the bits between idx and
    idx + nb_bits are set) */
-void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
+void modbus_set_bits_from_bytes(uint8_t *dest,
+                                int idx,
+                                unsigned int nb_bits,
                                 const uint8_t *tab_byte)
 {
     unsigned int i;
@@ -99,8 +103,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
 
 /* Gets the byte value from many bits.
    To obtain a full byte, set nb_bits to 8. */
-uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
-                                  unsigned int nb_bits)
+uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits)
 {
     unsigned int i;
     uint8_t value = 0;
@@ -111,8 +114,8 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx,
         nb_bits = 8;
     }
 
-    for (i=0; i < nb_bits; i++) {
-        value |= (src[idx+i] << i);
+    for (i = 0; i < nb_bits; i++) {
+        value |= (src[idx + i] << i);
     }
 
     return value;
@@ -130,10 +133,7 @@ float modbus_get_float_abcd(const uint16_t *src)
     c = (src[1] >> 8) & 0xFF;
     d = (src[1] >> 0) & 0xFF;
 
-    i = (a << 24) |
-        (b << 16) |
-        (c << 8) |
-        (d << 0);
+    i = (a << 24) | (b << 16) | (c << 8) | (d << 0);
     memcpy(&f, &i, 4);
 
     return f;
@@ -151,10 +151,7 @@ float modbus_get_float_dcba(const uint16_t *src)
     c = (src[1] >> 8) & 0xFF;
     d = (src[1] >> 0) & 0xFF;
 
-    i = (d << 24) |
-        (c << 16) |
-        (b << 8) |
-        (a << 0);
+    i = (d << 24) | (c << 16) | (b << 8) | (a << 0);
     memcpy(&f, &i, 4);
 
     return f;
@@ -172,10 +169,7 @@ float modbus_get_float_badc(const uint16_t *src)
     c = (src[1] >> 8) & 0xFF;
     d = (src[1] >> 0) & 0xFF;
 
-    i = (b << 24) |
-        (a << 16) |
-        (d << 8) |
-        (c << 0);
+    i = (b << 24) | (a << 16) | (d << 8) | (c << 0);
     memcpy(&f, &i, 4);
 
     return f;
@@ -193,10 +187,7 @@ float modbus_get_float_cdab(const uint16_t *src)
     c = (src[1] >> 8) & 0xFF;
     d = (src[1] >> 0) & 0xFF;
 
-    i = (c << 24) |
-        (d << 16) |
-        (a << 8) |
-        (b << 0);
+    i = (c << 24) | (d << 16) | (a << 8) | (b << 0);
     memcpy(&f, &i, 4);
 
     return f;
@@ -208,18 +199,17 @@ float modbus_get_float(const uint16_t *src)
     float f;
     uint32_t i;
 
-    i = (((uint32_t)src[1]) << 16) + src[0];
+    i = (((uint32_t) src[1]) << 16) + src[0];
     memcpy(&f, &i, sizeof(float));
 
     return f;
-
 }
 
 /* Set a float to 4 bytes for Modbus w/o any conversion (ABCD) */
 void modbus_set_float_abcd(float f, uint16_t *dest)
 {
     uint32_t i;
-    uint8_t *out = (uint8_t*) dest;
+    uint8_t *out = (uint8_t *) dest;
     uint8_t a, b, c, d;
 
     memcpy(&i, &f, sizeof(uint32_t));
@@ -238,7 +228,7 @@ void modbus_set_float_abcd(float f, uint16_t *dest)
 void modbus_set_float_dcba(float f, uint16_t *dest)
 {
     uint32_t i;
-    uint8_t *out = (uint8_t*) dest;
+    uint8_t *out = (uint8_t *) dest;
     uint8_t a, b, c, d;
 
     memcpy(&i, &f, sizeof(uint32_t));
@@ -251,14 +241,13 @@ void modbus_set_float_dcba(float f, uint16_t *dest)
     out[1] = c;
     out[2] = b;
     out[3] = a;
-
 }
 
 /* Set a float to 4 bytes for Modbus with byte swap conversion (BADC) */
 void modbus_set_float_badc(float f, uint16_t *dest)
 {
     uint32_t i;
-    uint8_t *out = (uint8_t*) dest;
+    uint8_t *out = (uint8_t *) dest;
     uint8_t a, b, c, d;
 
     memcpy(&i, &f, sizeof(uint32_t));
@@ -277,7 +266,7 @@ void modbus_set_float_badc(float f, uint16_t *dest)
 void modbus_set_float_cdab(float f, uint16_t *dest)
 {
     uint32_t i;
-    uint8_t *out = (uint8_t*) dest;
+    uint8_t *out = (uint8_t *) dest;
     uint8_t a, b, c, d;
 
     memcpy(&i, &f, sizeof(uint32_t));
@@ -298,6 +287,6 @@ void modbus_set_float(float f, uint16_t *dest)
     uint32_t i;
 
     memcpy(&i, &f, sizeof(uint32_t));
-    dest[0] = (uint16_t)i;
-    dest[1] = (uint16_t)(i >> 16);
+    dest[0] = (uint16_t) i;
+    dest[1] = (uint16_t) (i >> 16);
 }

+ 26 - 23
src/modbus-private.h

@@ -7,6 +7,7 @@
 #ifndef MODBUS_PRIVATE_H
 #define MODBUS_PRIVATE_H
 
+// clang-format off
 #ifndef _MSC_VER
 # include <stdint.h>
 # include <sys/time.h>
@@ -15,8 +16,9 @@
 # include <time.h>
 typedef int ssize_t;
 #endif
-#include <sys/types.h>
+// clang-format on
 #include <config.h>
+#include <sys/types.h>
 
 #include "modbus.h"
 
@@ -36,11 +38,11 @@ MODBUS_BEGIN_DECLS
 #define _MODBUS_EXCEPTION_RSP_LENGTH 5
 
 /* Timeouts in microsecond (0.5 s) */
-#define _RESPONSE_TIMEOUT    500000
-#define _BYTE_TIMEOUT        500000
+#define _RESPONSE_TIMEOUT 500000
+#define _BYTE_TIMEOUT     500000
 
 typedef enum {
-    _MODBUS_BACKEND_TYPE_RTU=0,
+    _MODBUS_BACKEND_TYPE_RTU = 0,
     _MODBUS_BACKEND_TYPE_TCP
 } modbus_backend_type_t;
 
@@ -69,24 +71,25 @@ typedef struct _modbus_backend {
     unsigned int header_length;
     unsigned int checksum_length;
     unsigned int max_adu_length;
-    int (*set_slave) (modbus_t *ctx, int slave);
-    int (*build_request_basis) (modbus_t *ctx, int function, int addr,
-                                int nb, uint8_t *req);
-    int (*build_response_basis) (sft_t *sft, uint8_t *rsp);
-    int (*prepare_response_tid) (const uint8_t *req, int *req_length);
-    int (*send_msg_pre) (uint8_t *req, int req_length);
-    ssize_t (*send) (modbus_t *ctx, const uint8_t *req, int req_length);
-    int (*receive) (modbus_t *ctx, uint8_t *req);
-    ssize_t (*recv) (modbus_t *ctx, uint8_t *rsp, int rsp_length);
-    int (*check_integrity) (modbus_t *ctx, uint8_t *msg,
-                            const int msg_length);
-    int (*pre_check_confirmation) (modbus_t *ctx, const uint8_t *req,
-                                   const uint8_t *rsp, int rsp_length);
-    int (*connect) (modbus_t *ctx);
-    void (*close) (modbus_t *ctx);
-    int (*flush) (modbus_t *ctx);
-    int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
-    void (*free) (modbus_t *ctx);
+    int (*set_slave)(modbus_t *ctx, int slave);
+    int (*build_request_basis)(
+        modbus_t *ctx, int function, int addr, int nb, uint8_t *req);
+    int (*build_response_basis)(sft_t *sft, uint8_t *rsp);
+    int (*prepare_response_tid)(const uint8_t *req, int *req_length);
+    int (*send_msg_pre)(uint8_t *req, int req_length);
+    ssize_t (*send)(modbus_t *ctx, const uint8_t *req, int req_length);
+    int (*receive)(modbus_t *ctx, uint8_t *req);
+    ssize_t (*recv)(modbus_t *ctx, uint8_t *rsp, int rsp_length);
+    int (*check_integrity)(modbus_t *ctx, uint8_t *msg, const int msg_length);
+    int (*pre_check_confirmation)(modbus_t *ctx,
+                                  const uint8_t *req,
+                                  const uint8_t *rsp,
+                                  int rsp_length);
+    int (*connect)(modbus_t *ctx);
+    void (*close)(modbus_t *ctx);
+    int (*flush)(modbus_t *ctx);
+    int (*select)(modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
+    void (*free)(modbus_t *ctx);
 } modbus_backend_t;
 
 struct _modbus {
@@ -114,4 +117,4 @@ size_t strlcpy(char *dest, const char *src, size_t dest_size);
 
 MODBUS_END_DECLS
 
-#endif  /* MODBUS_PRIVATE_H */
+#endif /* MODBUS_PRIVATE_H */

+ 6 - 5
src/modbus-rtu-private.h

@@ -19,11 +19,11 @@
 #include <termios.h>
 #endif
 
-#define _MODBUS_RTU_HEADER_LENGTH      1
-#define _MODBUS_RTU_PRESET_REQ_LENGTH  6
-#define _MODBUS_RTU_PRESET_RSP_LENGTH  2
+#define _MODBUS_RTU_HEADER_LENGTH     1
+#define _MODBUS_RTU_PRESET_REQ_LENGTH 6
+#define _MODBUS_RTU_PRESET_RSP_LENGTH 2
 
-#define _MODBUS_RTU_CHECKSUM_LENGTH    2
+#define _MODBUS_RTU_CHECKSUM_LENGTH 2
 
 #if defined(_WIN32)
 #if !defined(ENOTSUP)
@@ -32,6 +32,7 @@
 
 /* WIN32: struct containing serial handle and a receive buffer */
 #define PY_BUF_SIZE 512
+
 struct win32_ser {
     /* File handle */
     HANDLE fd;
@@ -67,7 +68,7 @@ typedef struct _modbus_rtu {
     int rts;
     int rts_delay;
     int onebyte_time;
-    void (*set_rts) (modbus_t *ctx, int on);
+    void (*set_rts)(modbus_t *ctx, int on);
 #endif
     /* To handle many slaves on the same link */
     int confirmation_to_ignore;

+ 122 - 123
src/modbus-rtu.c

@@ -4,10 +4,10 @@
  * SPDX-License-Identifier: LGPL-2.1-or-later
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #ifndef _MSC_VER
 #include <unistd.h>
@@ -16,8 +16,8 @@
 
 #include "modbus-private.h"
 
-#include "modbus-rtu.h"
 #include "modbus-rtu-private.h"
+#include "modbus-rtu.h"
 
 #if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS
 #include <sys/ioctl.h>
@@ -29,63 +29,47 @@
 
 /* Table of CRC values for high-order byte */
 static const uint8_t table_crc_hi[] = {
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
-    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
-    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
-    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
-    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
-    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
-    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
-    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
-    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
-    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
-    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
-    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
-    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
-    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
-    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
-};
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40};
 
 /* Table of CRC values for low-order byte */
 static const uint8_t table_crc_lo[] = {
-    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
-    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
-    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
-    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
-    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
-    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
-    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
-    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
-    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
-    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
-    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
-    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
-    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
-    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
-    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
-    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
-    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
-    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
-    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
-    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
-    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
-    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
-    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
-    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
-    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
-    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
-};
+    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5,
+    0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B,
+    0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE,
+    0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6,
+    0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
+    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
+    0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8,
+    0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
+    0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21,
+    0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
+    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A,
+    0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
+    0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7,
+    0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51,
+    0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
+    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
+    0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D,
+    0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
+    0x41, 0x81, 0x80, 0x40};
 
 /* Define the slave ID of the remote device to talk in master mode or set the
  * internal slave ID in slave mode */
@@ -105,9 +89,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave)
 }
 
 /* Builds a RTU request header */
-static int _modbus_rtu_build_request_basis(modbus_t *ctx, int function,
-                                           int addr, int nb,
-                                           uint8_t *req)
+static int _modbus_rtu_build_request_basis(
+    modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
 {
     assert(ctx->slave != -1);
     req[0] = ctx->slave;
@@ -135,7 +118,7 @@ static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
 {
     uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
     uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
-    unsigned int i; /* will index into CRC lookup */
+    unsigned int i;        /* will index into CRC lookup */
 
     /* pass through message buffer */
     while (buffer_length--) {
@@ -186,8 +169,7 @@ static void win32_ser_init(struct win32_ser *ws)
 }
 
 /* FIXME Try to remove length_to_read -> max_len argument, only used by win32 */
-static int win32_ser_select(struct win32_ser *ws, int max_len,
-                            const struct timeval *tv)
+static int win32_ser_select(struct win32_ser *ws, int max_len, const struct timeval *tv)
 {
     COMMTIMEOUTS comm_to;
     unsigned int msec = 0;
@@ -237,8 +219,7 @@ static int win32_ser_select(struct win32_ser *ws, int max_len,
     }
 }
 
-static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg,
-                          unsigned int max_len)
+static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max_len)
 {
     unsigned int n = ws->n_bytes;
 
@@ -277,7 +258,9 @@ static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_lengt
 #if defined(_WIN32)
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
     DWORD n_bytes = 0;
-    return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? (ssize_t)n_bytes : -1;
+    return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL))
+               ? (ssize_t) n_bytes
+               : -1;
 #else
 #if HAVE_DECL_TIOCM_RTS
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
@@ -332,7 +315,7 @@ static int _modbus_rtu_receive(modbus_t *ctx, uint8_t *req)
 static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
 {
 #if defined(_WIN32)
-    return win32_ser_read(&((modbus_rtu_t *)ctx->backend_data)->w_ser, rsp, rsp_length);
+    return win32_ser_read(&((modbus_rtu_t *) ctx->backend_data)->w_ser, rsp, rsp_length);
 #else
     return read(ctx->s, rsp, rsp_length);
 #endif
@@ -340,8 +323,10 @@ static ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
 
 static int _modbus_rtu_flush(modbus_t *);
 
-static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
-                                              const uint8_t *rsp, int rsp_length)
+static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx,
+                                              const uint8_t *req,
+                                              const uint8_t *rsp,
+                                              int rsp_length)
 {
     /* Check responding slave is the slave we requested (except for broacast
      * request) */
@@ -349,7 +334,8 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
         if (ctx->debug) {
             fprintf(stderr,
                     "The responding slave %d isn't the requested slave %d\n",
-                    rsp[0], req[0]);
+                    rsp[0],
+                    req[0]);
         }
         errno = EMBBADSLAVE;
         return -1;
@@ -361,8 +347,7 @@ static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
 /* The check_crc16 function shall return 0 if the message is ignored and the
    message length if the CRC is valid. Otherwise it shall return -1 and set
    errno to EMBBADCRC. */
-static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,
-                                       const int msg_length)
+static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
 {
     uint16_t crc_calculated;
     uint16_t crc_received;
@@ -386,8 +371,10 @@ static int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,
         return msg_length;
     } else {
         if (ctx->debug) {
-            fprintf(stderr, "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n",
-                    crc_received, crc_calculated);
+            fprintf(stderr,
+                    "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n",
+                    crc_received,
+                    crc_calculated);
         }
 
         if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_PROTOCOL) {
@@ -412,8 +399,11 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     if (ctx->debug) {
         printf("Opening %s at %d bauds (%c, %d, %d)\n",
-               ctx_rtu->device, ctx_rtu->baud, ctx_rtu->parity,
-               ctx_rtu->data_bit, ctx_rtu->stop_bit);
+               ctx_rtu->device,
+               ctx_rtu->baud,
+               ctx_rtu->parity,
+               ctx_rtu->data_bit,
+               ctx_rtu->stop_bit);
     }
 
 #if defined(_WIN32)
@@ -424,19 +414,16 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal
      * number */
-    ctx_rtu->w_ser.fd = CreateFileA(ctx_rtu->device,
-                                    GENERIC_READ | GENERIC_WRITE,
-                                    0,
-                                    NULL,
-                                    OPEN_EXISTING,
-                                    0,
-                                    NULL);
+    ctx_rtu->w_ser.fd = CreateFileA(
+        ctx_rtu->device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
 
     /* Error checking */
     if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
         if (ctx->debug) {
-            fprintf(stderr, "ERROR Can't open the device %s (LastError %d)\n",
-                    ctx_rtu->device, (int)GetLastError());
+            fprintf(stderr,
+                    "ERROR Can't open the device %s (LastError %d)\n",
+                    ctx_rtu->device,
+                    (int) GetLastError());
         }
         return -1;
     }
@@ -445,8 +432,9 @@ static int _modbus_rtu_connect(modbus_t *ctx)
     ctx_rtu->old_dcb.DCBlength = sizeof(DCB);
     if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
         if (ctx->debug) {
-            fprintf(stderr, "ERROR Error getting configuration (LastError %d)\n",
-                    (int)GetLastError());
+            fprintf(stderr,
+                    "ERROR Error getting configuration (LastError %d)\n",
+                    (int) GetLastError());
         }
         CloseHandle(ctx_rtu->w_ser.fd);
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
@@ -519,8 +507,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
     default:
         dcb.BaudRate = CBR_9600;
         if (ctx->debug) {
-            fprintf(stderr, "WARNING Unknown baud rate %d for %s (B9600 used)\n",
-                    ctx_rtu->baud, ctx_rtu->device);
+            fprintf(stderr,
+                    "WARNING Unknown baud rate %d for %s (B9600 used)\n",
+                    ctx_rtu->baud,
+                    ctx_rtu->device);
         }
     }
 
@@ -576,8 +566,9 @@ static int _modbus_rtu_connect(modbus_t *ctx)
     /* Setup port */
     if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
         if (ctx->debug) {
-            fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
-                    (int)GetLastError());
+            fprintf(stderr,
+                    "ERROR Error setting new configuration (LastError %d)\n",
+                    (int) GetLastError());
         }
         CloseHandle(ctx_rtu->w_ser.fd);
         ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
@@ -599,8 +590,10 @@ static int _modbus_rtu_connect(modbus_t *ctx)
     ctx->s = open(ctx_rtu->device, flags);
     if (ctx->s < 0) {
         if (ctx->debug) {
-            fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
-                    ctx_rtu->device, strerror(errno));
+            fprintf(stderr,
+                    "ERROR Can't open the device %s (%s)\n",
+                    ctx_rtu->device,
+                    strerror(errno));
         }
         return -1;
     }
@@ -682,7 +675,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
         break;
 #endif
 #ifdef B1152000
-   case 1152000:
+    case 1152000:
         speed = B1152000;
         break;
 #endif
@@ -716,13 +709,13 @@ static int _modbus_rtu_connect(modbus_t *ctx)
         if (ctx->debug) {
             fprintf(stderr,
                     "WARNING Unknown baud rate %d for %s (B9600 used)\n",
-                    ctx_rtu->baud, ctx_rtu->device);
+                    ctx_rtu->baud,
+                    ctx_rtu->device);
         }
     }
 
     /* Set the baud rate */
-    if ((cfsetispeed(&tios, speed) < 0) ||
-        (cfsetospeed(&tios, speed) < 0)) {
+    if ((cfsetispeed(&tios, speed) < 0) || (cfsetospeed(&tios, speed) < 0)) {
         close(ctx->s);
         ctx->s = -1;
         return -1;
@@ -757,7 +750,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
 
     /* Stop bit (1 or 2) */
     if (ctx_rtu->stop_bit == 1)
-        tios.c_cflag &=~ CSTOPB;
+        tios.c_cflag &= ~CSTOPB;
     else /* 2 */
         tios.c_cflag |= CSTOPB;
 
@@ -765,11 +758,11 @@ static int _modbus_rtu_connect(modbus_t *ctx)
        PARODD       Use odd parity instead of even */
     if (ctx_rtu->parity == 'N') {
         /* None */
-        tios.c_cflag &=~ PARENB;
+        tios.c_cflag &= ~PARENB;
     } else if (ctx_rtu->parity == 'E') {
         /* Even */
         tios.c_cflag |= PARENB;
-        tios.c_cflag &=~ PARODD;
+        tios.c_cflag &= ~PARODD;
     } else {
         /* Odd */
         tios.c_cflag |= PARENB;
@@ -851,7 +844,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
     */
 
     /* Raw output */
-    tios.c_oflag &=~ OPOST;
+    tios.c_oflag &= ~OPOST;
 
     /* C_CC         Control characters
        VMIN         Minimum number of characters to read
@@ -1044,7 +1037,7 @@ int modbus_rtu_set_rts(modbus_t *ctx, int mode)
     return -1;
 }
 
-int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on))
+int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts)(modbus_t *ctx, int on))
 {
     if (ctx == NULL) {
         errno = EINVAL;
@@ -1079,7 +1072,7 @@ int modbus_rtu_get_rts_delay(modbus_t *ctx)
     if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
 #if HAVE_DECL_TIOCM_RTS
         modbus_rtu_t *ctx_rtu;
-        ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
+        ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
         return ctx_rtu->rts_delay;
 #else
         if (ctx->debug) {
@@ -1104,7 +1097,7 @@ int modbus_rtu_set_rts_delay(modbus_t *ctx, int us)
     if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
 #if HAVE_DECL_TIOCM_RTS
         modbus_rtu_t *ctx_rtu;
-        ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
+        ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
         ctx_rtu->rts_delay = us;
         return 0;
 #else
@@ -1128,13 +1121,15 @@ static void _modbus_rtu_close(modbus_t *ctx)
 #if defined(_WIN32)
     /* Revert settings */
     if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) {
-        fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n",
-                (int)GetLastError());
+        fprintf(stderr,
+                "ERROR Couldn't revert to configuration (LastError %d)\n",
+                (int) GetLastError());
     }
 
     if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) {
-        fprintf(stderr, "ERROR Error while closing handle (LastError %d)\n",
-                (int)GetLastError());
+        fprintf(stderr,
+                "ERROR Error while closing handle (LastError %d)\n",
+                (int) GetLastError());
     }
 #else
     if (ctx->s >= 0) {
@@ -1156,13 +1151,13 @@ static int _modbus_rtu_flush(modbus_t *ctx)
 #endif
 }
 
-static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
-                              struct timeval *tv, int length_to_read)
+static int
+_modbus_rtu_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
 {
     int s_rc;
 #if defined(_WIN32)
-    s_rc = win32_ser_select(&((modbus_rtu_t *)ctx->backend_data)->w_ser,
-                            length_to_read, tv);
+    s_rc = win32_ser_select(
+        &((modbus_rtu_t *) ctx->backend_data)->w_ser, length_to_read, tv);
     if (s_rc == 0) {
         errno = ETIMEDOUT;
         return -1;
@@ -1172,7 +1167,7 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
         return -1;
     }
 #else
-    while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
+    while ((s_rc = select(ctx->s + 1, rset, NULL, NULL, tv)) == -1) {
         if (errno == EINTR) {
             if (ctx->debug) {
                 fprintf(stderr, "A non blocked signal was caught\n");
@@ -1195,15 +1190,17 @@ static int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
     return s_rc;
 }
 
-static void _modbus_rtu_free(modbus_t *ctx) {
+static void _modbus_rtu_free(modbus_t *ctx)
+{
     if (ctx->backend_data) {
-        free(((modbus_rtu_t *)ctx->backend_data)->device);
+        free(((modbus_rtu_t *) ctx->backend_data)->device);
         free(ctx->backend_data);
     }
 
     free(ctx);
 }
 
+// clang-format off
 const modbus_backend_t _modbus_rtu_backend = {
     _MODBUS_BACKEND_TYPE_RTU,
     _MODBUS_RTU_HEADER_LENGTH,
@@ -1226,9 +1223,10 @@ const modbus_backend_t _modbus_rtu_backend = {
     _modbus_rtu_free
 };
 
-modbus_t* modbus_new_rtu(const char *device,
-                         int baud, char parity, int data_bit,
-                         int stop_bit)
+// clang-format on
+
+modbus_t *
+modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit)
 {
     modbus_t *ctx;
     modbus_rtu_t *ctx_rtu;
@@ -1247,23 +1245,23 @@ modbus_t* modbus_new_rtu(const char *device,
         return NULL;
     }
 
-    ctx = (modbus_t *)malloc(sizeof(modbus_t));
+    ctx = (modbus_t *) malloc(sizeof(modbus_t));
     if (ctx == NULL) {
         return NULL;
     }
 
     _modbus_init_common(ctx);
     ctx->backend = &_modbus_rtu_backend;
-    ctx->backend_data = (modbus_rtu_t *)malloc(sizeof(modbus_rtu_t));
+    ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
     if (ctx->backend_data == NULL) {
         modbus_free(ctx);
         errno = ENOMEM;
         return NULL;
     }
-    ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
+    ctx_rtu = (modbus_rtu_t *) ctx->backend_data;
 
     /* Device name and \0 */
-    ctx_rtu->device = (char *)malloc((strlen(device) + 1) * sizeof(char));
+    ctx_rtu->device = (char *) malloc((strlen(device) + 1) * sizeof(char));
     if (ctx_rtu->device == NULL) {
         modbus_free(ctx);
         errno = ENOMEM;
@@ -1292,7 +1290,8 @@ modbus_t* modbus_new_rtu(const char *device,
     ctx_rtu->rts = MODBUS_RTU_RTS_NONE;
 
     /* Calculate estimated time in micro second to send one byte */
-    ctx_rtu->onebyte_time = 1000000 * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud;
+    ctx_rtu->onebyte_time =
+        1000000 * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud;
 
     /* The internal function is used by default to set RTS */
     ctx_rtu->set_rts = _modbus_rtu_ioctl_rts;

+ 8 - 7
src/modbus-rtu.h

@@ -14,10 +14,10 @@ MODBUS_BEGIN_DECLS
 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
  * RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes
  */
-#define MODBUS_RTU_MAX_ADU_LENGTH  256
+#define MODBUS_RTU_MAX_ADU_LENGTH 256
 
-MODBUS_API modbus_t* modbus_new_rtu(const char *device, int baud, char parity,
-                                    int data_bit, int stop_bit);
+MODBUS_API modbus_t *
+modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit);
 
 #define MODBUS_RTU_RS232 0
 #define MODBUS_RTU_RS485 1
@@ -25,14 +25,15 @@ MODBUS_API modbus_t* modbus_new_rtu(const char *device, int baud, char parity,
 MODBUS_API int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode);
 MODBUS_API int modbus_rtu_get_serial_mode(modbus_t *ctx);
 
-#define MODBUS_RTU_RTS_NONE   0
-#define MODBUS_RTU_RTS_UP     1
-#define MODBUS_RTU_RTS_DOWN   2
+#define MODBUS_RTU_RTS_NONE 0
+#define MODBUS_RTU_RTS_UP   1
+#define MODBUS_RTU_RTS_DOWN 2
 
 MODBUS_API int modbus_rtu_set_rts(modbus_t *ctx, int mode);
 MODBUS_API int modbus_rtu_get_rts(modbus_t *ctx);
 
-MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts) (modbus_t *ctx, int on));
+MODBUS_API int modbus_rtu_set_custom_rts(modbus_t *ctx,
+                                         void (*set_rts)(modbus_t *ctx, int on));
 
 MODBUS_API int modbus_rtu_set_rts_delay(modbus_t *ctx, int us);
 MODBUS_API int modbus_rtu_get_rts_delay(modbus_t *ctx);

+ 3 - 3
src/modbus-tcp-private.h

@@ -7,11 +7,11 @@
 #ifndef MODBUS_TCP_PRIVATE_H
 #define MODBUS_TCP_PRIVATE_H
 
-#define _MODBUS_TCP_HEADER_LENGTH      7
+#define _MODBUS_TCP_HEADER_LENGTH     7
 #define _MODBUS_TCP_PRESET_REQ_LENGTH 12
-#define _MODBUS_TCP_PRESET_RSP_LENGTH  8
+#define _MODBUS_TCP_PRESET_RSP_LENGTH 8
 
-#define _MODBUS_TCP_CHECKSUM_LENGTH    0
+#define _MODBUS_TCP_CHECKSUM_LENGTH 0
 
 /* In both structures, the transaction ID must be placed on first position
    to have a quick access not dependent of the TCP backend */

+ 62 - 53
src/modbus-tcp.c

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: LGPL-2.1-or-later
  */
 
+// clang-format off
 #if defined(_WIN32)
 # define OS_WIN32
 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
@@ -52,11 +53,12 @@
 #if defined(_AIX) && !defined(MSG_DONTWAIT)
 #define MSG_DONTWAIT MSG_NONBLOCK
 #endif
+// clang-format on
 
 #include "modbus-private.h"
 
-#include "modbus-tcp.h"
 #include "modbus-tcp-private.h"
+#include "modbus-tcp.h"
 
 #ifdef OS_WIN32
 static int _modbus_tcp_init_win32(void)
@@ -65,8 +67,9 @@ static int _modbus_tcp_init_win32(void)
     WSADATA wsaData;
 
     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
-        fprintf(stderr, "WSAStartup() returned error code %d\n",
-                (unsigned int)GetLastError());
+        fprintf(stderr,
+                "WSAStartup() returned error code %d\n",
+                (unsigned int) GetLastError());
         errno = EIO;
         return -1;
     }
@@ -94,9 +97,8 @@ static int _modbus_set_slave(modbus_t *ctx, int slave)
 }
 
 /* Builds a TCP request header */
-static int _modbus_tcp_build_request_basis(modbus_t *ctx, int function,
-                                           int addr, int nb,
-                                           uint8_t *req)
+static int _modbus_tcp_build_request_basis(
+    modbus_t *ctx, int function, int addr, int nb, uint8_t *req)
 {
     modbus_tcp_t *ctx_tcp = ctx->backend_data;
 
@@ -148,7 +150,6 @@ static int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp)
     return _MODBUS_TCP_PRESET_RSP_LENGTH;
 }
 
-
 static int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
 {
     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
        Requests not to send SIGPIPE on errors on stream oriented
        sockets when the other end breaks the connection.  The EPIPE
        error is still returned. */
-    return send(ctx->s, (const char *)req, req_length, MSG_NOSIGNAL);
+    return send(ctx->s, (const char *) req, req_length, MSG_NOSIGNAL);
 }
 
-static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req) {
+static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req)
+{
     return _modbus_receive_msg(ctx, req, MSG_INDICATION);
 }
 
-static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {
-    return recv(ctx->s, (char *)rsp, rsp_length, 0);
+static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
+{
+    return recv(ctx->s, (char *) rsp, rsp_length, 0);
 }
 
 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
     return msg_length;
 }
 
-static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
-                                              const uint8_t *rsp, int rsp_length)
+static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx,
+                                              const uint8_t *req,
+                                              const uint8_t *rsp,
+                                              int rsp_length)
 {
     unsigned int protocol_id;
     /* Check transaction ID */
     if (req[0] != rsp[0] || req[1] != rsp[1]) {
         if (ctx->debug) {
-            fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",
-                    (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
+            fprintf(stderr,
+                    "Invalid transaction ID received 0x%X (not 0x%X)\n",
+                    (rsp[0] << 8) + rsp[1],
+                    (req[0] << 8) + req[1]);
         }
         errno = EMBBADDATA;
         return -1;
@@ -205,8 +212,7 @@ static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
     protocol_id = (rsp[2] << 8) + rsp[3];
     if (protocol_id != 0x0) {
         if (ctx->debug) {
-            fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",
-                    protocol_id);
+            fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n", protocol_id);
         }
         errno = EMBBADDATA;
         return -1;
@@ -223,8 +229,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
     /* Set the TCP no delay flag */
     /* SOL_TCP = IPPROTO_TCP */
     option = 1;
-    rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
-                    (const void *)&option, sizeof(int));
+    rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const void *) &option, sizeof(int));
     if (rc == -1) {
         return -1;
     }
@@ -252,8 +257,7 @@ static int _modbus_tcp_set_ipv4_options(int s)
      **/
     /* Set the IP low delay option */
     option = IPTOS_LOWDELAY;
-    rc = setsockopt(s, IPPROTO_IP, IP_TOS,
-                    (const void *)&option, sizeof(int));
+    rc = setsockopt(s, IPPROTO_IP, IP_TOS, (const void *) &option, sizeof(int));
     if (rc == -1) {
         return -1;
     }
@@ -262,7 +266,9 @@ static int _modbus_tcp_set_ipv4_options(int s)
     return 0;
 }
 
-static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
+static int _connect(int sockfd,
+                    const struct sockaddr *addr,
+                    socklen_t addrlen,
                     const struct timeval *ro_tv)
 {
     int rc = connect(sockfd, addr, addrlen);
@@ -292,7 +298,7 @@ static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
         }
 
         /* The connection is established if SO_ERROR and optval are set to 0 */
-        rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);
+        rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
         if (rc == 0 && optval == 0) {
             return 0;
         } else {
@@ -345,7 +351,8 @@ static int _modbus_tcp_connect(modbus_t *ctx)
     addr.sin_family = AF_INET;
     addr.sin_port = htons(ctx_tcp->port);
     addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
-    rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout);
+    rc =
+        _connect(ctx->s, (struct sockaddr *) &addr, sizeof(addr), &ctx->response_timeout);
     if (rc == -1) {
         close(ctx->s);
         ctx->s = -1;
@@ -381,8 +388,7 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
     ai_hints.ai_next = NULL;
 
     ai_list = NULL;
-    rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,
-                     &ai_hints, &ai_list);
+    rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service, &ai_hints, &ai_list);
     if (rc != 0) {
         if (ctx->debug) {
             fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
@@ -462,7 +468,7 @@ static int _modbus_tcp_flush(modbus_t *ctx)
         tv.tv_usec = 0;
         FD_ZERO(&rset);
         FD_SET(ctx->s, &rset);
-        rc = select(ctx->s+1, &rset, NULL, NULL, &tv);
+        rc = select(ctx->s + 1, &rset, NULL, NULL, &tv);
         if (rc == -1) {
             return -1;
         }
@@ -514,8 +520,8 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
     }
 
     enable = 1;
-    if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR,
-                   (char *)&enable, sizeof(enable)) == -1) {
+    if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(enable)) ==
+        -1) {
         close(new_s);
         return -1;
     }
@@ -531,7 +537,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
         /* Listen only specified IP address */
         addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
     }
-    if (bind(new_s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+    if (bind(new_s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
         close(new_s);
         return -1;
     }
@@ -580,7 +586,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
         service = ctx_tcp_pi->service;
     }
 
-    memset(&ai_hints, 0, sizeof (ai_hints));
+    memset(&ai_hints, 0, sizeof(ai_hints));
     /* If node is not NULL, than the AI_PASSIVE flag is ignored. */
     ai_hints.ai_flags |= AI_PASSIVE;
 #ifdef AI_ADDRCONFIG
@@ -619,8 +625,8 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
             continue;
         } else {
             int enable = 1;
-            rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
-                            (void *)&enable, sizeof (enable));
+            rc =
+                setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &enable, sizeof(enable));
             if (rc != 0) {
                 close(s);
                 if (ctx->debug) {
@@ -673,9 +679,9 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
     addrlen = sizeof(addr);
 #ifdef HAVE_ACCEPT4
     /* Inherit socket flags and use accept4 call */
-    ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
+    ctx->s = accept4(*s, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
 #else
-    ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
+    ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen);
 #endif
 
     if (ctx->s < 0) {
@@ -683,8 +689,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
     }
 
     if (ctx->debug) {
-        printf("The client connection from %s is accepted\n",
-               inet_ntoa(addr.sin_addr));
+        printf("The client connection from %s is accepted\n", inet_ntoa(addr.sin_addr));
     }
 
     return ctx->s;
@@ -703,9 +708,9 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
     addrlen = sizeof(addr);
 #ifdef HAVE_ACCEPT4
     /* Inherit socket flags and use accept4 call */
-    ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
+    ctx->s = accept4(*s, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
 #else
-    ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
+    ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen);
 #endif
 
     if (ctx->s < 0) {
@@ -719,10 +724,11 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
     return ctx->s;
 }
 
-static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
+static int
+_modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
 {
     int s_rc;
-    while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
+    while ((s_rc = select(ctx->s + 1, rset, NULL, NULL, tv)) == -1) {
         if (errno == EINTR) {
             if (ctx->debug) {
                 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
     return s_rc;
 }
 
-static void _modbus_tcp_free(modbus_t *ctx) {
+static void _modbus_tcp_free(modbus_t *ctx)
+{
     if (ctx->backend_data) {
         free(ctx->backend_data);
     }
     free(ctx);
 }
 
-static void _modbus_tcp_pi_free(modbus_t *ctx) {
+static void _modbus_tcp_pi_free(modbus_t *ctx)
+{
     if (ctx->backend_data) {
         modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
         free(ctx_tcp_pi->node);
@@ -761,6 +769,7 @@ static void _modbus_tcp_pi_free(modbus_t *ctx) {
     free(ctx);
 }
 
+// clang-format off
 const modbus_backend_t _modbus_tcp_backend = {
     _MODBUS_BACKEND_TYPE_TCP,
     _MODBUS_TCP_HEADER_LENGTH,
@@ -783,7 +792,6 @@ const modbus_backend_t _modbus_tcp_backend = {
     _modbus_tcp_free
 };
 
-
 const modbus_backend_t _modbus_tcp_pi_backend = {
     _MODBUS_BACKEND_TYPE_TCP,
     _MODBUS_TCP_HEADER_LENGTH,
@@ -806,7 +814,9 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
     _modbus_tcp_pi_free
 };
 
-modbus_t* modbus_new_tcp(const char *ip, int port)
+// clang-format on
+
+modbus_t *modbus_new_tcp(const char *ip, int port)
 {
     modbus_t *ctx;
     modbus_tcp_t *ctx_tcp;
@@ -826,7 +836,7 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
     }
 #endif
 
-    ctx = (modbus_t *)malloc(sizeof(modbus_t));
+    ctx = (modbus_t *) malloc(sizeof(modbus_t));
     if (ctx == NULL) {
         return NULL;
     }
@@ -837,13 +847,13 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
 
     ctx->backend = &_modbus_tcp_backend;
 
-    ctx->backend_data = (modbus_tcp_t *)malloc(sizeof(modbus_tcp_t));
+    ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
     if (ctx->backend_data == NULL) {
         modbus_free(ctx);
         errno = ENOMEM;
         return NULL;
     }
-    ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
+    ctx_tcp = (modbus_tcp_t *) ctx->backend_data;
 
     if (ip != NULL) {
         dest_size = sizeof(char) * 16;
@@ -870,13 +880,12 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
     return ctx;
 }
 
-
-modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
+modbus_t *modbus_new_tcp_pi(const char *node, const char *service)
 {
     modbus_t *ctx;
     modbus_tcp_pi_t *ctx_tcp_pi;
 
-    ctx = (modbus_t *)malloc(sizeof(modbus_t));
+    ctx = (modbus_t *) malloc(sizeof(modbus_t));
     if (ctx == NULL) {
         return NULL;
     }
@@ -887,13 +896,13 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
 
     ctx->backend = &_modbus_tcp_pi_backend;
 
-    ctx->backend_data = (modbus_tcp_pi_t *)malloc(sizeof(modbus_tcp_pi_t));
+    ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
     if (ctx->backend_data == NULL) {
         modbus_free(ctx);
         errno = ENOMEM;
         return NULL;
     }
-    ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
+    ctx_tcp_pi = (modbus_tcp_pi_t *) ctx->backend_data;
     ctx_tcp_pi->node = NULL;
     ctx_tcp_pi->service = NULL;
 

+ 9 - 9
src/modbus-tcp.h

@@ -15,35 +15,35 @@ MODBUS_BEGIN_DECLS
 /* Win32 with MinGW, supplement to <errno.h> */
 #include <winsock2.h>
 #if !defined(ECONNRESET)
-#define ECONNRESET   WSAECONNRESET
+#define ECONNRESET WSAECONNRESET
 #endif
 #if !defined(ECONNREFUSED)
 #define ECONNREFUSED WSAECONNREFUSED
 #endif
 #if !defined(ETIMEDOUT)
-#define ETIMEDOUT    WSAETIMEDOUT
+#define ETIMEDOUT WSAETIMEDOUT
 #endif
 #if !defined(ENOPROTOOPT)
-#define ENOPROTOOPT  WSAENOPROTOOPT
+#define ENOPROTOOPT WSAENOPROTOOPT
 #endif
 #if !defined(EINPROGRESS)
-#define EINPROGRESS  WSAEINPROGRESS
+#define EINPROGRESS WSAEINPROGRESS
 #endif
 #endif
 
-#define MODBUS_TCP_DEFAULT_PORT   502
-#define MODBUS_TCP_SLAVE         0xFF
+#define MODBUS_TCP_DEFAULT_PORT 502
+#define MODBUS_TCP_SLAVE        0xFF
 
 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
  * TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes
  */
-#define MODBUS_TCP_MAX_ADU_LENGTH  260
+#define MODBUS_TCP_MAX_ADU_LENGTH 260
 
-MODBUS_API modbus_t* modbus_new_tcp(const char *ip_address, int port);
+MODBUS_API modbus_t *modbus_new_tcp(const char *ip_address, int port);
 MODBUS_API int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
 MODBUS_API int modbus_tcp_accept(modbus_t *ctx, int *s);
 
-MODBUS_API modbus_t* modbus_new_tcp_pi(const char *node, const char *service);
+MODBUS_API modbus_t *modbus_new_tcp_pi(const char *node, const char *service);
 MODBUS_API int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection);
 MODBUS_API int modbus_tcp_pi_accept(modbus_t *ctx, int *s);
 

+ 8 - 10
src/modbus-version.h.in

@@ -29,25 +29,23 @@
 #define LIBMODBUS_VERSION_MICRO (@LIBMODBUS_VERSION_MICRO@)
 
 /* The full version, like 1.2.3 */
-#define LIBMODBUS_VERSION        @LIBMODBUS_VERSION@
+#define LIBMODBUS_VERSION @LIBMODBUS_VERSION@
 
 /* The full version, in string form (suited for string concatenation)
  */
 #define LIBMODBUS_VERSION_STRING "@LIBMODBUS_VERSION@"
 
 /* Numerically encoded version, eg. v1.2.3 is 0x010203 */
-#define LIBMODBUS_VERSION_HEX ((LIBMODBUS_VERSION_MAJOR << 16) |  \
-                               (LIBMODBUS_VERSION_MINOR <<  8) |  \
-                               (LIBMODBUS_VERSION_MICRO <<  0))
+#define LIBMODBUS_VERSION_HEX                                                                      \
+    ((LIBMODBUS_VERSION_MAJOR << 16) | (LIBMODBUS_VERSION_MINOR << 8) |                            \
+     (LIBMODBUS_VERSION_MICRO << 0))
 
 /* Evaluates to True if the version is greater than @major, @minor and @micro
  */
-#define LIBMODBUS_VERSION_CHECK(major,minor,micro)      \
-    (LIBMODBUS_VERSION_MAJOR > (major) ||               \
-     (LIBMODBUS_VERSION_MAJOR == (major) &&             \
-      LIBMODBUS_VERSION_MINOR > (minor)) ||             \
-     (LIBMODBUS_VERSION_MAJOR == (major) &&             \
-      LIBMODBUS_VERSION_MINOR == (minor) &&             \
+#define LIBMODBUS_VERSION_CHECK(major, minor, micro)                                               \
+    (LIBMODBUS_VERSION_MAJOR > (major) ||                                                          \
+     (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR > (minor)) ||                  \
+     (LIBMODBUS_VERSION_MAJOR == (major) && LIBMODBUS_VERSION_MINOR == (minor) &&                  \
       LIBMODBUS_VERSION_MICRO >= (micro)))
 
 #endif /* MODBUS_VERSION_H */

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 261 - 198
src/modbus.c


+ 121 - 99
src/modbus.h

@@ -7,15 +7,16 @@
 #ifndef MODBUS_H
 #define MODBUS_H
 
+// clang-format off
 /* Add this for macros that defined unix flavor */
 #if (defined(__unix__) || defined(unix)) && !defined(USG)
-#include <sys/param.h>
+# include <sys/param.h>
 #endif
 
 #ifndef _MSC_VER
-#include <stdint.h>
+# include <stdint.h>
 #else
-#include "stdint.h"
+# include "stdint.h"
 #endif
 
 #include "modbus-version.h"
@@ -38,6 +39,7 @@
 # define MODBUS_BEGIN_DECLS
 # define MODBUS_END_DECLS
 #endif
+// clang-format on
 
 MODBUS_BEGIN_DECLS
 
@@ -58,28 +60,28 @@ MODBUS_BEGIN_DECLS
 #endif
 
 /* Modbus function codes */
-#define MODBUS_FC_READ_COILS                0x01
-#define MODBUS_FC_READ_DISCRETE_INPUTS      0x02
-#define MODBUS_FC_READ_HOLDING_REGISTERS    0x03
-#define MODBUS_FC_READ_INPUT_REGISTERS      0x04
-#define MODBUS_FC_WRITE_SINGLE_COIL         0x05
-#define MODBUS_FC_WRITE_SINGLE_REGISTER     0x06
-#define MODBUS_FC_READ_EXCEPTION_STATUS     0x07
-#define MODBUS_FC_WRITE_MULTIPLE_COILS      0x0F
-#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS  0x10
-#define MODBUS_FC_REPORT_SLAVE_ID           0x11
-#define MODBUS_FC_MASK_WRITE_REGISTER       0x16
-#define MODBUS_FC_WRITE_AND_READ_REGISTERS  0x17
-
-#define MODBUS_BROADCAST_ADDRESS    0
+#define MODBUS_FC_READ_COILS               0x01
+#define MODBUS_FC_READ_DISCRETE_INPUTS     0x02
+#define MODBUS_FC_READ_HOLDING_REGISTERS   0x03
+#define MODBUS_FC_READ_INPUT_REGISTERS     0x04
+#define MODBUS_FC_WRITE_SINGLE_COIL        0x05
+#define MODBUS_FC_WRITE_SINGLE_REGISTER    0x06
+#define MODBUS_FC_READ_EXCEPTION_STATUS    0x07
+#define MODBUS_FC_WRITE_MULTIPLE_COILS     0x0F
+#define MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
+#define MODBUS_FC_REPORT_SLAVE_ID          0x11
+#define MODBUS_FC_MASK_WRITE_REGISTER      0x16
+#define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
+
+#define MODBUS_BROADCAST_ADDRESS 0
 
 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
  * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  * (chapter 6 section 11 page 29)
  * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
  */
-#define MODBUS_MAX_READ_BITS              2000
-#define MODBUS_MAX_WRITE_BITS             1968
+#define MODBUS_MAX_READ_BITS  2000
+#define MODBUS_MAX_WRITE_BITS 1968
 
 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
  * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
@@ -88,17 +90,17 @@ MODBUS_BEGIN_DECLS
  * (chapter 6 section 17 page 38)
  * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
  */
-#define MODBUS_MAX_READ_REGISTERS          125
-#define MODBUS_MAX_WRITE_REGISTERS         123
-#define MODBUS_MAX_WR_WRITE_REGISTERS      121
-#define MODBUS_MAX_WR_READ_REGISTERS       125
+#define MODBUS_MAX_READ_REGISTERS     125
+#define MODBUS_MAX_WRITE_REGISTERS    123
+#define MODBUS_MAX_WR_WRITE_REGISTERS 121
+#define MODBUS_MAX_WR_READ_REGISTERS  125
 
 /* The size of the MODBUS PDU is limited by the size constraint inherited from
  * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
  * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
  * address (1 byte) - CRC (2 bytes) = 253 bytes.
  */
-#define MODBUS_MAX_PDU_LENGTH              253
+#define MODBUS_MAX_PDU_LENGTH 253
 
 /* Consequently:
  * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256
@@ -108,7 +110,7 @@ MODBUS_BEGIN_DECLS
  * an array of bytes to store responses and it will be compatible with the two
  * backends.
  */
-#define MODBUS_MAX_ADU_LENGTH              260
+#define MODBUS_MAX_ADU_LENGTH 260
 
 /* Random number to avoid errno conflicts */
 #define MODBUS_ENOBASE 112345678
@@ -141,11 +143,11 @@ enum {
 #define EMBXGTAR   (MODBUS_ENOBASE + MODBUS_EXCEPTION_GATEWAY_TARGET)
 
 /* Native libmodbus error codes */
-#define EMBBADCRC  (EMBXGTAR + 1)
-#define EMBBADDATA (EMBXGTAR + 2)
-#define EMBBADEXC  (EMBXGTAR + 3)
-#define EMBUNKEXC  (EMBXGTAR + 4)
-#define EMBMDATA   (EMBXGTAR + 5)
+#define EMBBADCRC   (EMBXGTAR + 1)
+#define EMBBADDATA  (EMBXGTAR + 2)
+#define EMBBADEXC   (EMBXGTAR + 3)
+#define EMBUNKEXC   (EMBXGTAR + 4)
+#define EMBMDATA    (EMBXGTAR + 5)
 #define EMBBADSLAVE (EMBXGTAR + 6)
 
 extern const unsigned int libmodbus_version_major;
@@ -169,35 +171,39 @@ typedef struct _modbus_mapping_t {
     uint16_t *tab_registers;
 } modbus_mapping_t;
 
-typedef enum
-{
-    MODBUS_ERROR_RECOVERY_NONE          = 0,
-    MODBUS_ERROR_RECOVERY_LINK          = (1<<1),
-    MODBUS_ERROR_RECOVERY_PROTOCOL      = (1<<2)
+typedef enum {
+    MODBUS_ERROR_RECOVERY_NONE = 0,
+    MODBUS_ERROR_RECOVERY_LINK = (1 << 1),
+    MODBUS_ERROR_RECOVERY_PROTOCOL = (1 << 2)
 } modbus_error_recovery_mode;
 
-typedef enum
-{
-    MODBUS_QUIRK_NONE                   = 0,
-    MODBUS_QUIRK_MAX_SLAVE              = (1<<1),
-    MODBUS_QUIRK_REPLY_TO_BROADCAST     = (1<<2),
-    MODBUS_QUIRK_ALL                    = 0xFF
+typedef enum {
+    MODBUS_QUIRK_NONE = 0,
+    MODBUS_QUIRK_MAX_SLAVE = (1 << 1),
+    MODBUS_QUIRK_REPLY_TO_BROADCAST = (1 << 2),
+    MODBUS_QUIRK_ALL = 0xFF
 } modbus_quirks;
 
-MODBUS_API int modbus_set_slave(modbus_t* ctx, int slave);
-MODBUS_API int modbus_get_slave(modbus_t* ctx);
-MODBUS_API int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery);
+MODBUS_API int modbus_set_slave(modbus_t *ctx, int slave);
+MODBUS_API int modbus_get_slave(modbus_t *ctx);
+MODBUS_API int modbus_set_error_recovery(modbus_t *ctx,
+                                         modbus_error_recovery_mode error_recovery);
 MODBUS_API int modbus_set_socket(modbus_t *ctx, int s);
 MODBUS_API int modbus_get_socket(modbus_t *ctx);
 
-MODBUS_API int modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
-MODBUS_API int modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
+MODBUS_API int
+modbus_get_response_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
+MODBUS_API int
+modbus_set_response_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
 
-MODBUS_API int modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
+MODBUS_API int
+modbus_get_byte_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
 MODBUS_API int modbus_set_byte_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
 
-MODBUS_API int modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
-MODBUS_API int modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
+MODBUS_API int
+modbus_get_indication_timeout(modbus_t *ctx, uint32_t *to_sec, uint32_t *to_usec);
+MODBUS_API int
+modbus_set_indication_timeout(modbus_t *ctx, uint32_t to_sec, uint32_t to_usec);
 
 MODBUS_API int modbus_get_header_length(modbus_t *ctx);
 
@@ -214,37 +220,53 @@ MODBUS_API const char *modbus_strerror(int errnum);
 MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
 MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
 MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
-MODBUS_API int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
+MODBUS_API int
+modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
 MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
 MODBUS_API int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);
 MODBUS_API int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
-MODBUS_API int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
-MODBUS_API int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
-MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb,
-                                               const uint16_t *src, int read_addr, int read_nb,
+MODBUS_API int
+modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data);
+MODBUS_API int
+modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
+MODBUS_API int modbus_write_and_read_registers(modbus_t *ctx,
+                                               int write_addr,
+                                               int write_nb,
+                                               const uint16_t *src,
+                                               int read_addr,
+                                               int read_nb,
                                                uint16_t *dest);
 MODBUS_API int modbus_report_slave_id(modbus_t *ctx, int max_dest, uint8_t *dest);
 
-MODBUS_API modbus_mapping_t* modbus_mapping_new_start_address(
-    unsigned int start_bits, unsigned int nb_bits,
-    unsigned int start_input_bits, unsigned int nb_input_bits,
-    unsigned int start_registers, unsigned int nb_registers,
-    unsigned int start_input_registers, unsigned int nb_input_registers);
-
-MODBUS_API modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits,
-                                                int nb_registers, int nb_input_registers);
+MODBUS_API modbus_mapping_t *
+modbus_mapping_new_start_address(unsigned int start_bits,
+                                 unsigned int nb_bits,
+                                 unsigned int start_input_bits,
+                                 unsigned int nb_input_bits,
+                                 unsigned int start_registers,
+                                 unsigned int nb_registers,
+                                 unsigned int start_input_registers,
+                                 unsigned int nb_input_registers);
+
+MODBUS_API modbus_mapping_t *modbus_mapping_new(int nb_bits,
+                                                int nb_input_bits,
+                                                int nb_registers,
+                                                int nb_input_registers);
 MODBUS_API void modbus_mapping_free(modbus_mapping_t *mb_mapping);
 
-MODBUS_API int modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
+MODBUS_API int
+modbus_send_raw_request(modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
 
 MODBUS_API int modbus_receive(modbus_t *ctx, uint8_t *req);
 
 MODBUS_API int modbus_receive_confirmation(modbus_t *ctx, uint8_t *rsp);
 
-MODBUS_API int modbus_reply(modbus_t *ctx, const uint8_t *req,
-                            int req_length, modbus_mapping_t *mb_mapping);
-MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
-                                      unsigned int exception_code);
+MODBUS_API int modbus_reply(modbus_t *ctx,
+                            const uint8_t *req,
+                            int req_length,
+                            modbus_mapping_t *mb_mapping);
+MODBUS_API int
+modbus_reply_exception(modbus_t *ctx, const uint8_t *req, unsigned int exception_code);
 MODBUS_API int modbus_enable_quirks(modbus_t *ctx, unsigned int quirks_mask);
 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);
  **/
 
 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
-#define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
-#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
-    (((int64_t)tab_int16[(index)    ] << 48) | \
-     ((int64_t)tab_int16[(index) + 1] << 32) | \
-     ((int64_t)tab_int16[(index) + 2] << 16) | \
-      (int64_t)tab_int16[(index) + 3])
+#define MODBUS_GET_LOW_BYTE(data)  ((data) &0xFF)
+#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index)                                \
+  (((int64_t) tab_int16[(index)] << 48) | ((int64_t) tab_int16[(index) + 1] << 32) | \
+   ((int64_t) tab_int16[(index) + 2] << 16) | (int64_t) tab_int16[(index) + 3])
 #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) \
-    (((int32_t)tab_int16[(index)    ] << 16) | \
-      (int32_t)tab_int16[(index) + 1])
+  (((int32_t) tab_int16[(index)] << 16) | (int32_t) tab_int16[(index) + 1])
 #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) \
-    (((int16_t)tab_int8[(index)    ] << 8) | \
-      (int16_t)tab_int8[(index) + 1])
-#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
-    do { \
-        ((int8_t*)(tab_int8))[(index)    ] = (int8_t)((value) >> 8);  \
-        ((int8_t*)(tab_int8))[(index) + 1] = (int8_t)(value); \
-    } while (0)
-#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \
-    do { \
-        ((int16_t*)(tab_int16))[(index)    ] = (int16_t)((value) >> 16); \
-        ((int16_t*)(tab_int16))[(index) + 1] = (int16_t)(value); \
-    } while (0)
-#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \
-    do { \
-        ((int16_t*)(tab_int16))[(index)    ] = (int16_t)((value) >> 48); \
-        ((int16_t*)(tab_int16))[(index) + 1] = (int16_t)((value) >> 32); \
-        ((int16_t*)(tab_int16))[(index) + 2] = (int16_t)((value) >> 16); \
-        ((int16_t*)(tab_int16))[(index) + 3] = (int16_t)(value); \
-    } while (0)
+  (((int16_t) tab_int8[(index)] << 8) | (int16_t) tab_int8[(index) + 1])
+#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value)        \
+  do {                                                          \
+    ((int8_t *) (tab_int8))[(index)] = (int8_t) ((value) >> 8); \
+    ((int8_t *) (tab_int8))[(index) + 1] = (int8_t) (value);    \
+  } while (0)
+#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value)          \
+  do {                                                              \
+    ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 16); \
+    ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) (value);     \
+  } while (0)
+#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value)              \
+  do {                                                                  \
+    ((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 48);     \
+    ((int16_t *) (tab_int16))[(index) + 1] = (int16_t) ((value) >> 32); \
+    ((int16_t *) (tab_int16))[(index) + 2] = (int16_t) ((value) >> 16); \
+    ((int16_t *) (tab_int16))[(index) + 3] = (int16_t) (value);         \
+  } while (0)
 
 MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);
-MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,
-                                       const uint8_t *tab_byte);
-MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src, int idx, unsigned int nb_bits);
+MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest,
+                                           int idx,
+                                           unsigned int nb_bits,
+                                           const uint8_t *tab_byte);
+MODBUS_API uint8_t modbus_get_byte_from_bits(const uint8_t *src,
+                                             int idx,
+                                             unsigned int nb_bits);
 MODBUS_API float modbus_get_float(const uint16_t *src);
 MODBUS_API float modbus_get_float_abcd(const uint16_t *src);
 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);
 MODBUS_API void modbus_set_float_badc(float f, uint16_t *dest);
 MODBUS_API void modbus_set_float_cdab(float f, uint16_t *dest);
 
-#include "modbus-tcp.h"
 #include "modbus-rtu.h"
+#include "modbus-tcp.h"
 
 MODBUS_END_DECLS
 
-#endif  /* MODBUS_H */
+#endif /* MODBUS_H */

+ 1 - 1
src/win32/config.h.win32

@@ -121,7 +121,7 @@
 /* #undef HAVE_WORKING_VFORK */
 
 /* Define to the sub-directory in which libtool stores uninstalled libraries.
-   */
+ */
 /* #undef LT_OBJDIR */
 
 /* Name of package */

+ 11 - 12
tests/bandwidth-client.c

@@ -6,13 +6,13 @@
 
 #include <stdio.h>
 #ifndef _MSC_VER
-#include <unistd.h>
 #include <sys/time.h>
+#include <unistd.h>
 #endif
-#include <string.h>
+#include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
-#include <errno.h>
 
 #include <modbus.h>
 
@@ -59,7 +59,8 @@ int main(int argc, char *argv[])
             use_backend = RTU;
             n_loop = 100;
         } else {
-            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", argv[0]);
+            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
+                   argv[0]);
             exit(1);
         }
     } else {
@@ -75,8 +76,7 @@ int main(int argc, char *argv[])
         modbus_set_slave(ctx, 1);
     }
     if (modbus_connect(ctx) == -1) {
-        fprintf(stderr, "Connection failed: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
@@ -93,7 +93,7 @@ int main(int argc, char *argv[])
 
     nb_points = MODBUS_MAX_READ_BITS;
     start = gettime_ms();
-    for (i=0; i<n_loop; i++) {
+    for (i = 0; i < n_loop; i++) {
         rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
         if (rc == -1) {
             fprintf(stderr, "%s\n", modbus_strerror(errno));
@@ -130,7 +130,7 @@ int main(int argc, char *argv[])
 
     nb_points = MODBUS_MAX_READ_REGISTERS;
     start = gettime_ms();
-    for (i=0; i<n_loop; i++) {
+    for (i = 0; i < n_loop; i++) {
         rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
         if (rc == -1) {
             fprintf(stderr, "%s\n", modbus_strerror(errno));
@@ -167,10 +167,9 @@ int main(int argc, char *argv[])
 
     nb_points = MODBUS_MAX_WR_WRITE_REGISTERS;
     start = gettime_ms();
-    for (i=0; i<n_loop; i++) {
-        rc = modbus_write_and_read_registers(ctx,
-                                             0, nb_points, tab_reg,
-                                             0, nb_points, tab_reg);
+    for (i = 0; i < n_loop; i++) {
+        rc = modbus_write_and_read_registers(
+            ctx, 0, nb_points, tab_reg, 0, nb_points, tab_reg);
         if (rc == -1) {
             fprintf(stderr, "%s\n", modbus_strerror(errno));
             return -1;

+ 15 - 14
tests/bandwidth-server-many-up.c

@@ -4,25 +4,25 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 #include <modbus.h>
 
 #if defined(_WIN32)
 #include <ws2tcpip.h>
 #else
+#include <arpa/inet.h>
+#include <netinet/in.h>
 #include <sys/select.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #endif
 
-#define NB_CONNECTION    5
+#define NB_CONNECTION 5
 
 static modbus_t *ctx = NULL;
 static modbus_mapping_t *mb_mapping;
@@ -52,11 +52,10 @@ int main(void)
 
     ctx = modbus_new_tcp("127.0.0.1", 1502);
 
-    mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
-                                    MODBUS_MAX_READ_REGISTERS, 0);
+    mb_mapping =
+        modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
     if (mb_mapping == NULL) {
-        fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
@@ -80,7 +79,7 @@ int main(void)
 
     for (;;) {
         rdset = refset;
-        if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
+        if (select(fdmax + 1, &rdset, NULL, NULL, NULL) == -1) {
             perror("Server select() failure.");
             close_sigint(1);
         }
@@ -102,7 +101,7 @@ int main(void)
                 /* Handle new connections */
                 addrlen = sizeof(clientaddr);
                 memset(&clientaddr, 0, sizeof(clientaddr));
-                newfd = accept(server_socket, (struct sockaddr *)&clientaddr, &addrlen);
+                newfd = accept(server_socket, (struct sockaddr *) &clientaddr, &addrlen);
                 if (newfd == -1) {
                     perror("Server accept() error");
                 } else {
@@ -113,7 +112,9 @@ int main(void)
                         fdmax = newfd;
                     }
                     printf("New connection from %s:%d on socket %d\n",
-                           inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
+                           inet_ntoa(clientaddr.sin_addr),
+                           clientaddr.sin_port,
+                           newfd);
                 }
             } else {
                 modbus_set_socket(ctx, master_socket);

+ 10 - 10
tests/bandwidth-server-one.c

@@ -8,9 +8,9 @@
 #ifndef _MSC_VER
 #include <unistd.h>
 #endif
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include <modbus.h>
 
@@ -31,14 +31,15 @@ int main(int argc, char *argv[])
     int rc;
     int use_backend;
 
-     /* TCP */
+    /* TCP */
     if (argc > 1) {
         if (strcmp(argv[1], "tcp") == 0) {
             use_backend = TCP;
         } else if (strcmp(argv[1], "rtu") == 0) {
             use_backend = RTU;
         } else {
-            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", argv[0]);
+            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
+                   argv[0]);
             exit(1);
         }
     } else {
@@ -57,22 +58,21 @@ int main(int argc, char *argv[])
         modbus_connect(ctx);
     }
 
-    mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
-                                    MODBUS_MAX_READ_REGISTERS, 0);
+    mb_mapping =
+        modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0);
     if (mb_mapping == NULL) {
-        fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
 
-    for(;;) {
+    for (;;) {
         uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
 
         rc = modbus_receive(ctx, query);
         if (rc > 0) {
             modbus_reply(ctx, query, rc, mb_mapping);
-        } else if (rc  == -1) {
+        } else if (rc == -1) {
             /* Connection closed by the client or error */
             break;
         }

+ 47 - 32
tests/random-test-client.c

@@ -8,9 +8,9 @@
 #ifndef _MSC_VER
 #include <unistd.h>
 #endif
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include <modbus.h>
 
@@ -27,10 +27,10 @@
    All these functions are called with random values on a address
    range defined by the following defines.
 */
-#define LOOP             1
-#define SERVER_ID       17
-#define ADDRESS_START    0
-#define ADDRESS_END     99
+#define LOOP          1
+#define SERVER_ID     17
+#define ADDRESS_START 0
+#define ADDRESS_END   99
 
 /* At each loop, the program works in the range ADDRESS_START to
  * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
@@ -50,18 +50,17 @@ int main(void)
     uint16_t *tab_rp_registers;
 
     /* RTU */
-/*
-    ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
-    modbus_set_slave(ctx, SERVER_ID);
-*/
+    /*
+        ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
+        modbus_set_slave(ctx, SERVER_ID);
+    */
 
     /* TCP */
     ctx = modbus_new_tcp("127.0.0.1", 1502);
     modbus_set_debug(ctx, TRUE);
 
     if (modbus_connect(ctx) == -1) {
-        fprintf(stderr, "Connection failed: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
@@ -90,8 +89,8 @@ int main(void)
             int i;
 
             /* Random numbers (short) */
-            for (i=0; i<nb; i++) {
-                tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
+            for (i = 0; i < nb; i++) {
+                tab_rq_registers[i] = (uint16_t) (65535.0 * rand() / (RAND_MAX + 1.0));
                 tab_rw_rq_registers[i] = ~tab_rq_registers[i];
                 tab_rq_bits[i] = tab_rq_registers[i] % 2;
             }
@@ -125,12 +124,15 @@ int main(void)
                     printf("Address = %d, nb = %d\n", addr, nb);
                     nb_fail++;
                 } else {
-                    for (i=0; i<nb; i++) {
+                    for (i = 0; i < nb; i++) {
                         if (tab_rp_bits[i] != tab_rq_bits[i]) {
                             printf("ERROR modbus_read_bits\n");
                             printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
-                                   addr, tab_rq_bits[i], tab_rq_bits[i],
-                                   tab_rp_bits[i], tab_rp_bits[i]);
+                                   addr,
+                                   tab_rq_bits[i],
+                                   tab_rq_bits[i],
+                                   tab_rp_bits[i],
+                                   tab_rp_bits[i]);
                             nb_fail++;
                         }
                     }
@@ -142,7 +144,9 @@ int main(void)
             if (rc != 1) {
                 printf("ERROR modbus_write_register (%d)\n", rc);
                 printf("Address = %d, value = %d (0x%X)\n",
-                       addr, tab_rq_registers[0], tab_rq_registers[0]);
+                       addr,
+                       tab_rq_registers[0],
+                       tab_rq_registers[0]);
                 nb_fail++;
             } else {
                 rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
@@ -154,8 +158,11 @@ int main(void)
                     if (tab_rq_registers[0] != tab_rp_registers[0]) {
                         printf("ERROR modbus_read_registers single\n");
                         printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
-                               addr, tab_rq_registers[0], tab_rq_registers[0],
-                               tab_rp_registers[0], tab_rp_registers[0]);
+                               addr,
+                               tab_rq_registers[0],
+                               tab_rq_registers[0],
+                               tab_rp_registers[0],
+                               tab_rp_registers[0]);
                         nb_fail++;
                     }
                 }
@@ -174,32 +181,37 @@ int main(void)
                     printf("Address = %d, nb = %d\n", addr, nb);
                     nb_fail++;
                 } else {
-                    for (i=0; i<nb; i++) {
+                    for (i = 0; i < nb; i++) {
                         if (tab_rq_registers[i] != tab_rp_registers[i]) {
                             printf("ERROR modbus_read_registers\n");
                             printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
-                                   addr, tab_rq_registers[i], tab_rq_registers[i],
-                                   tab_rp_registers[i], tab_rp_registers[i]);
+                                   addr,
+                                   tab_rq_registers[i],
+                                   tab_rq_registers[i],
+                                   tab_rp_registers[i],
+                                   tab_rp_registers[i]);
                             nb_fail++;
                         }
                     }
                 }
             }
             /* R/W MULTIPLE REGISTERS */
-            rc = modbus_write_and_read_registers(ctx,
-                                                 addr, nb, tab_rw_rq_registers,
-                                                 addr, nb, tab_rp_registers);
+            rc = modbus_write_and_read_registers(
+                ctx, addr, nb, tab_rw_rq_registers, addr, nb, tab_rp_registers);
             if (rc != nb) {
                 printf("ERROR modbus_read_and_write_registers (%d)\n", rc);
                 printf("Address = %d, nb = %d\n", addr, nb);
                 nb_fail++;
             } else {
-                for (i=0; i<nb; i++) {
+                for (i = 0; i < nb; i++) {
                     if (tab_rp_registers[i] != tab_rw_rq_registers[i]) {
                         printf("ERROR modbus_read_and_write_registers READ\n");
                         printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
-                               addr, tab_rp_registers[i], tab_rw_rq_registers[i],
-                               tab_rp_registers[i], tab_rw_rq_registers[i]);
+                               addr,
+                               tab_rp_registers[i],
+                               tab_rw_rq_registers[i],
+                               tab_rp_registers[i],
+                               tab_rw_rq_registers[i]);
                         nb_fail++;
                     }
                 }
@@ -210,12 +222,15 @@ int main(void)
                     printf("Address = %d, nb = %d\n", addr, nb);
                     nb_fail++;
                 } else {
-                    for (i=0; i<nb; i++) {
+                    for (i = 0; i < nb; i++) {
                         if (tab_rw_rq_registers[i] != tab_rp_registers[i]) {
                             printf("ERROR modbus_read_and_write_registers WRITE\n");
                             printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
-                                   addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i],
-                                   tab_rp_registers[i], tab_rp_registers[i]);
+                                   addr,
+                                   tab_rw_rq_registers[i],
+                                   tab_rw_rq_registers[i],
+                                   tab_rp_registers[i],
+                                   tab_rp_registers[i]);
                             nb_fail++;
                         }
                     }

+ 2 - 3
tests/random-test-server.c

@@ -8,8 +8,8 @@
 #ifndef _MSC_VER
 #include <unistd.h>
 #endif
-#include <stdlib.h>
 #include <errno.h>
+#include <stdlib.h>
 
 #include <modbus.h>
 
@@ -24,8 +24,7 @@ int main(void)
 
     mb_mapping = modbus_mapping_new(500, 500, 500, 500);
     if (mb_mapping == NULL) {
-        fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }

+ 268 - 225
tests/unit-test-client.c

@@ -4,12 +4,12 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <modbus.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "unit-test.h"
 
@@ -22,31 +22,37 @@ enum {
 };
 
 int test_server(modbus_t *ctx, int use_backend);
-int send_crafted_request(modbus_t *ctx, int function,
-                         uint8_t *req, int req_size,
-                         uint16_t max_value, uint16_t bytes,
-                         int backend_length, int backend_offset);
+int send_crafted_request(modbus_t *ctx,
+                         int function,
+                         uint8_t *req,
+                         int req_size,
+                         uint16_t max_value,
+                         uint16_t bytes,
+                         int backend_length,
+                         int backend_offset);
 int equal_dword(uint16_t *tab_reg, const uint32_t value);
 int is_memory_equal(const void *s1, const void *s2, size_t size);
 
-#define BUG_REPORT(_cond, _format, _args ...) \
-    printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args)
+#define BUG_REPORT(_cond, _format, _args...) \
+  printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args)
 
-#define ASSERT_TRUE(_cond, _format, __args...) {  \
-    if (_cond) {                                  \
-        printf("OK\n");                           \
-    } else {                                      \
-        BUG_REPORT(_cond, _format, ## __args);    \
-        goto close;                               \
-    }                                             \
-};
+#define ASSERT_TRUE(_cond, _format, __args...) \
+  {                                            \
+    if (_cond) {                               \
+      printf("OK\n");                          \
+    } else {                                   \
+      BUG_REPORT(_cond, _format, ##__args);    \
+      goto close;                              \
+    }                                          \
+  };
 
 int is_memory_equal(const void *s1, const void *s2, size_t size)
 {
     return (memcmp(s1, s2, size) == 0);
 }
 
-int equal_dword(uint16_t *tab_reg, const uint32_t value) {
+int equal_dword(uint16_t *tab_reg, const uint32_t value)
+{
     return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF)));
 }
 
@@ -80,7 +86,8 @@ int main(int argc, char *argv[])
         } else if (strcmp(argv[1], "rtu") == 0) {
             use_backend = RTU;
         } else {
-            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
+            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n",
+                   argv[0]);
             exit(1);
         }
     } else {
@@ -100,9 +107,8 @@ int main(int argc, char *argv[])
         return -1;
     }
     modbus_set_debug(ctx, TRUE);
-    modbus_set_error_recovery(ctx,
-                              MODBUS_ERROR_RECOVERY_LINK |
-                              MODBUS_ERROR_RECOVERY_PROTOCOL);
+    modbus_set_error_recovery(
+        ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL);
 
     if (use_backend == RTU) {
         modbus_set_slave(ctx, SERVER_ID);
@@ -121,8 +127,8 @@ int main(int argc, char *argv[])
     memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
 
     /* Allocate and initialize the memory to store the registers */
-    nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
-        UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
+    nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
+                                                          : UT_INPUT_REGISTERS_NB;
     tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
     memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
 
@@ -131,7 +137,8 @@ int main(int argc, char *argv[])
     printf("1/1 No response timeout modification on connect: ");
     modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
     ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
-                old_response_to_usec == new_response_to_usec, "");
+                    old_response_to_usec == new_response_to_usec,
+                "");
 
     printf("\nTEST WRITE/READ:\n");
 
@@ -145,8 +152,7 @@ int main(int argc, char *argv[])
     rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
     printf("2/2 modbus_read_bits: ");
     ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
-    ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n",
-                tab_rp_bits[0], ON);
+    ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n", tab_rp_bits[0], ON);
 
     /* End single */
 
@@ -169,9 +175,9 @@ int main(int argc, char *argv[])
     while (nb_points > 0) {
         int nb_bits = (nb_points > 8) ? 8 : nb_points;
 
-        value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
-        ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
-                    value, UT_BITS_TAB[i]);
+        value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits);
+        ASSERT_TRUE(
+            value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]);
 
         nb_points -= nb_bits;
         i++;
@@ -180,8 +186,8 @@ int main(int argc, char *argv[])
     /* End of multiple bits */
 
     /** DISCRETE INPUTS **/
-    rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                UT_INPUT_BITS_NB, tab_rp_bits);
+    rc =
+        modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, tab_rp_bits);
     printf("1/1 modbus_read_input_bits: ");
     ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
 
@@ -189,9 +195,11 @@ int main(int argc, char *argv[])
     nb_points = UT_INPUT_BITS_NB;
     while (nb_points > 0) {
         int nb_bits = (nb_points > 8) ? 8 : nb_points;
-        value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
-        ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
-                    value, UT_INPUT_BITS_TAB[i]);
+        value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits);
+        ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i],
+                    "FAILED (%0X != %0X)\n",
+                    value,
+                    UT_INPUT_BITS_TAB[i]);
 
         nb_points -= nb_bits;
         i++;
@@ -205,39 +213,39 @@ int main(int argc, char *argv[])
     printf("1/2 modbus_write_register: ");
     ASSERT_TRUE(rc == 1, "");
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               1, tab_rp_registers);
+    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
     printf("2/2 modbus_read_registers: ");
     ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
-    ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n",
-                tab_rp_registers[0], 0x1234);
+    ASSERT_TRUE(tab_rp_registers[0] == 0x1234,
+                "FAILED (%0X != %0X)\n",
+                tab_rp_registers[0],
+                0x1234);
     /* End of single register */
 
     /* Many registers */
-    rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
-                                UT_REGISTERS_NB, UT_REGISTERS_TAB);
+    rc = modbus_write_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, UT_REGISTERS_TAB);
     printf("1/5 modbus_write_registers: ");
     ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
     printf("2/5 modbus_read_registers: ");
     ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
 
-    for (i=0; i < UT_REGISTERS_NB; i++) {
+    for (i = 0; i < UT_REGISTERS_NB; i++) {
         ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
                     "FAILED (%0X != %0X)\n",
-                    tab_rp_registers[i], UT_REGISTERS_TAB[i]);
+                    tab_rp_registers[i],
+                    UT_REGISTERS_TAB[i]);
     }
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               0, tab_rp_registers);
+    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 0, tab_rp_registers);
     printf("3/5 modbus_read_registers (0): ");
     ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
 
-    nb_points = (UT_REGISTERS_NB >
-                 UT_INPUT_REGISTERS_NB) ?
-        UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
+    nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB
+                                                          : UT_INPUT_REGISTERS_NB;
     memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
 
     /* Write registers to zero from tab_rp_registers and store read registers
@@ -251,32 +259,32 @@ int main(int argc, char *argv[])
                                          UT_REGISTERS_NB,
                                          tab_rp_registers);
     printf("4/5 modbus_write_and_read_registers: ");
-    ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n",
-                rc, UT_REGISTERS_NB);
+    ASSERT_TRUE(
+        rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB);
 
     ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
                 "FAILED (%0X != %0X)\n",
-                tab_rp_registers[0], UT_REGISTERS_TAB[0]);
+                tab_rp_registers[0],
+                UT_REGISTERS_TAB[0]);
 
-    for (i=1; i < UT_REGISTERS_NB; i++) {
-        ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n",
-                    tab_rp_registers[i], 0);
+    for (i = 1; i < UT_REGISTERS_NB; i++) {
+        ASSERT_TRUE(
+            tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers[i], 0);
     }
 
     /* End of many registers */
 
-
     /** INPUT REGISTERS **/
-    rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
-                                     UT_INPUT_REGISTERS_NB,
-                                     tab_rp_registers);
+    rc = modbus_read_input_registers(
+        ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB, tab_rp_registers);
     printf("1/1 modbus_read_input_registers: ");
     ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
 
-    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
+    for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
         ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
                     "FAILED (%0X != %0X)\n",
-                    tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
+                    tab_rp_registers[i],
+                    UT_INPUT_REGISTERS_TAB[i]);
     }
 
     /* MASKS */
@@ -285,33 +293,36 @@ int main(int argc, char *argv[])
     rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
     ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
-    ASSERT_TRUE(tab_rp_registers[0] == 0x17,
-                "FAILED (%0X != %0X)\n",
-                tab_rp_registers[0], 0x17);
+    ASSERT_TRUE(
+        tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17);
 
     printf("\nTEST FLOATS\n");
     /** FLOAT **/
     printf("1/4 Set/get float ABCD: ");
     modbus_set_float_abcd(UT_REAL, tab_rp_registers);
-    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD_SET, 4), "FAILED Set float ABCD");
+    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD_SET, 4),
+                "FAILED Set float ABCD");
     real = modbus_get_float_abcd(UT_IREAL_ABCD_GET);
     ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
 
     printf("2/4 Set/get float DCBA: ");
     modbus_set_float_dcba(UT_REAL, tab_rp_registers);
-    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA_SET, 4), "FAILED Set float DCBA");
+    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA_SET, 4),
+                "FAILED Set float DCBA");
     real = modbus_get_float_dcba(UT_IREAL_DCBA_GET);
     ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
 
     printf("3/4 Set/get float BADC: ");
     modbus_set_float_badc(UT_REAL, tab_rp_registers);
-    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC_SET, 4), "FAILED Set float BADC");
+    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC_SET, 4),
+                "FAILED Set float BADC");
     real = modbus_get_float_badc(UT_IREAL_BADC_GET);
     ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
 
     printf("4/4 Set/get float CDAB: ");
     modbus_set_float_cdab(UT_REAL, tab_rp_registers);
-    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB_SET, 4), "FAILED Set float CDAB");
+    ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB_SET, 4),
+                "FAILED Set float CDAB");
     real = modbus_get_float_cdab(UT_IREAL_CDAB_GET);
     ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
 
@@ -335,8 +346,8 @@ int main(int argc, char *argv[])
     printf("* modbus_read_input_bits (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                UT_INPUT_BITS_NB + 1, tab_rp_bits);
+    rc = modbus_read_input_bits(
+        ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB + 1, tab_rp_bits);
     printf("* modbus_read_input_bits (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -344,8 +355,8 @@ int main(int argc, char *argv[])
     printf("* modbus_read_registers (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_MAX + 1, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX + 1, tab_rp_registers);
     printf("* modbus_read_registers (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -353,9 +364,8 @@ int main(int argc, char *argv[])
     printf("* modbus_read_input_registers (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
-                                     UT_INPUT_REGISTERS_NB + 1,
-                                     tab_rp_registers);
+    rc = modbus_read_input_registers(
+        ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB + 1, tab_rp_registers);
     printf("* modbus_read_input_registers (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -371,8 +381,7 @@ int main(int argc, char *argv[])
     printf("* modbus_write_coils (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
-                           UT_BITS_NB, tab_rp_bits);
+    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, UT_BITS_NB, tab_rp_bits);
     printf("* modbus_write_coils (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -380,8 +389,8 @@ int main(int argc, char *argv[])
     printf("* modbus_write_register (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
-                                tab_rp_registers[0]);
+    rc = modbus_write_register(
+        ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, tab_rp_registers[0]);
     printf("* modbus_write_register (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -389,8 +398,10 @@ int main(int argc, char *argv[])
     printf("* modbus_write_registers (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
-                                UT_REGISTERS_NB, tab_rp_registers);
+    rc = modbus_write_registers(ctx,
+                                UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
+                                UT_REGISTERS_NB,
+                                tab_rp_registers);
     printf("* modbus_write_registers (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
@@ -398,56 +409,54 @@ int main(int argc, char *argv[])
     printf("* modbus_mask_write_registers (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
-                                    0xF2, 0x25);
+    rc = modbus_mask_write_register(
+        ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 0xF2, 0x25);
     printf("* modbus_mask_write_registers (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
-    rc = modbus_write_and_read_registers(ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers);
+    rc = modbus_write_and_read_registers(
+        ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers);
     printf("* modbus_write_and_read_registers (0): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
     rc = modbus_write_and_read_registers(ctx,
                                          UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
-                                         UT_REGISTERS_NB, tab_rp_registers,
+                                         UT_REGISTERS_NB,
+                                         tab_rp_registers,
                                          UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
-                                         UT_REGISTERS_NB, tab_rp_registers);
+                                         UT_REGISTERS_NB,
+                                         tab_rp_registers);
     printf("* modbus_write_and_read_registers (max): ");
     ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
 
     /** TOO MANY DATA **/
     printf("\nTEST TOO MANY DATA ERROR:\n");
 
-    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
-                          MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
+    rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
     printf("* modbus_read_bits: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
-    rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
-                                MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
+    rc = modbus_read_input_bits(
+        ctx, UT_INPUT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
     printf("* modbus_read_input_bits: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               MODBUS_MAX_READ_REGISTERS + 1,
-                               tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
     printf("* modbus_read_registers: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
-    rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
-                                     MODBUS_MAX_READ_REGISTERS + 1,
-                                     tab_rp_registers);
+    rc = modbus_read_input_registers(
+        ctx, UT_INPUT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers);
     printf("* modbus_read_input_registers: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
-    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
-                           MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
+    rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
     printf("* modbus_write_bits: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
-    rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
-                                MODBUS_MAX_WRITE_REGISTERS + 1,
-                                tab_rp_registers);
+    rc = modbus_write_registers(
+        ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_WRITE_REGISTERS + 1, tab_rp_registers);
     printf("* modbus_write_registers: ");
     ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
 
@@ -473,15 +482,15 @@ int main(int argc, char *argv[])
 
     printf("\nTEST SLAVE REPLY:\n");
     modbus_set_slave(ctx, INVALID_SERVER_ID);
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
     if (use_backend == RTU) {
         const int RAW_REQ_LENGTH = 6;
-        uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 };
+        uint8_t raw_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01};
         /* Too many points */
-        uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
+        uint8_t raw_invalid_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF};
         const int RAW_RSP_LENGTH = 7;
-        uint8_t raw_rsp[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 };
+        uint8_t raw_rsp[] = {INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0};
         uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
 
         /* No response in RTU mode */
@@ -491,7 +500,6 @@ int main(int argc, char *argv[])
         /* The slave raises a timeout on a confirmation to ignore because if an
          * indication for another slave is received, a confirmation must follow */
 
-
         /* Send a pair of indication/confirmation to the slave with a different
          * slave ID to simulate a communication on a RS485 bus. At first, the
          * slave will see the indication message then the confirmation, and it must
@@ -515,8 +523,8 @@ int main(int argc, char *argv[])
         rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
         ASSERT_TRUE(rc == 0, "Invalid broadcast address");
 
-        rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                                   UT_REGISTERS_NB, tab_rp_registers);
+        rc = modbus_read_registers(
+            ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
         printf("2/3 No reply after a broadcast query: ");
         ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
     } else {
@@ -527,8 +535,8 @@ int main(int argc, char *argv[])
         rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
         ASSERT_TRUE(rc == 0, "Invalid broacast address");
 
-        rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                                   UT_REGISTERS_NB, tab_rp_registers);
+        rc = modbus_read_registers(
+            ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
         printf("2/3 Reply after a query with unit id == 0: ");
         ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
     }
@@ -537,8 +545,8 @@ int main(int argc, char *argv[])
     modbus_set_slave(ctx, old_slave);
 
     printf("3/3 Response with an invalid TID or slave: ");
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
-                               1, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 1, tab_rp_registers);
     ASSERT_TRUE(rc == -1, "");
 
     printf("1/2 Report slave ID truncated: \n");
@@ -546,10 +554,11 @@ int main(int argc, char *argv[])
     tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
     rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
     /* Return the size required (response size) but respects the defined limit */
-    ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&
-                tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
+    ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
                 "Return is rc %d (%d) and marker is %d (42)",
-                rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
+                rc,
+                NB_REPORT_SLAVE_ID,
+                tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
 
     printf("2/2 Report slave ID: \n");
     /* tab_rp_bits is used to store bytes */
@@ -565,7 +574,7 @@ int main(int argc, char *argv[])
     /* Print additional data as string */
     if (rc > 2) {
         printf("Additional data: ");
-        for (i=2; i < rc; i++) {
+        for (i = 2; i < rc; i++) {
             printf("%c", tab_rp_bits[i]);
         }
         printf("\n");
@@ -588,8 +597,8 @@ int main(int argc, char *argv[])
     ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
 
     modbus_set_response_timeout(ctx, 0, 1);
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers);
     printf("4/6 1us response timeout: ");
     if (rc == -1 && errno == ETIMEDOUT) {
         printf("OK\n");
@@ -607,8 +616,8 @@ int main(int argc, char *argv[])
     /* Trigger a special behaviour on server to wait for 0.5 second before
      * replying whereas allowed timeout is 0.2 second */
     modbus_set_response_timeout(ctx, 0, 200000);
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
-                               1, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
     printf("5/6 Too short response timeout (0.2s < 0.5s): ");
     ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
 
@@ -617,22 +626,21 @@ int main(int argc, char *argv[])
     modbus_flush(ctx);
 
     modbus_set_response_timeout(ctx, 0, 600000);
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
-                               1, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
     printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
     ASSERT_TRUE(rc == 1, "");
 
     /* Disable the byte timeout.
        The full response must be available in the 600ms interval */
     modbus_set_byte_timeout(ctx, 0, 0);
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
-                               1, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers);
     printf("7/7 Disable byte timeout: ");
     ASSERT_TRUE(rc == 1, "");
 
     /* Restore original response timeout */
-    modbus_set_response_timeout(ctx, old_response_to_sec,
-                                old_response_to_usec);
+    modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
 
     if (use_backend == TCP) {
         /* The test server is only able to test byte timeouts with the TCP
@@ -640,8 +648,8 @@ int main(int argc, char *argv[])
 
         /* Timeout of 3ms between bytes */
         modbus_set_byte_timeout(ctx, 0, 3000);
-        rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
-                                   1, tab_rp_registers);
+        rc = modbus_read_registers(
+            ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
         printf("1/2 Too small byte timeout (3ms < 5ms): ");
         ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
 
@@ -651,8 +659,8 @@ int main(int argc, char *argv[])
 
         /* Timeout of 7ms between bytes */
         modbus_set_byte_timeout(ctx, 0, 7000);
-        rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
-                                   1, tab_rp_registers);
+        rc = modbus_read_registers(
+            ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers);
         printf("2/2 Adapted byte timeout (7ms > 5ms): ");
         ASSERT_TRUE(rc == 1, "");
     }
@@ -664,19 +672,19 @@ int main(int argc, char *argv[])
     printf("\nTEST BAD RESPONSE ERROR:\n");
 
     /* Allocate only the required space */
-    tab_rp_registers_bad = (uint16_t *) malloc(
-        UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
+    tab_rp_registers_bad =
+        (uint16_t *) malloc(UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
 
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
-                               UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
     printf("* modbus_read_registers: ");
     ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
     free(tab_rp_registers_bad);
 
     /** MANUAL EXCEPTION **/
     printf("\nTEST MANUAL EXCEPTION:\n");
-    rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
-                               UT_REGISTERS_NB, tab_rp_registers);
+    rc = modbus_read_registers(
+        ctx, UT_REGISTERS_ADDRESS_SPECIAL, UT_REGISTERS_NB, tab_rp_registers);
 
     printf("* modbus_read_registers at special address: ");
     ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
@@ -722,63 +730,65 @@ int test_server(modbus_t *ctx, int use_backend)
     /* Read requests */
     const int READ_RAW_REQ_LEN = 6;
     const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE;
-    uint8_t read_raw_req[] = {
-        slave,
-        /* function, address, 5 values */
-        MODBUS_FC_READ_HOLDING_REGISTERS,
-        UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
-        0x0, 0x05
-    };
+    uint8_t read_raw_req[] = {slave,
+                              /* function, address, 5 values */
+                              MODBUS_FC_READ_HOLDING_REGISTERS,
+                              UT_REGISTERS_ADDRESS >> 8,
+                              UT_REGISTERS_ADDRESS & 0xFF,
+                              0x0,
+                              0x05};
     /* Write and read registers request */
     const int RW_RAW_REQ_LEN = 13;
-    uint8_t rw_raw_req[] = {
-        slave,
-        /* function, addr to read, nb to read */
-        MODBUS_FC_WRITE_AND_READ_REGISTERS,
-        /* Read */
-        UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
-        (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
-        (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
-        /* Write */
-        0, 0,
-        0, 1,
-        /* Write byte count */
-        1 * 2,
-        /* One data to write... */
-        0x12, 0x34
-    };
+    uint8_t rw_raw_req[] = {slave,
+                            /* function, addr to read, nb to read */
+                            MODBUS_FC_WRITE_AND_READ_REGISTERS,
+                            /* Read */
+                            UT_REGISTERS_ADDRESS >> 8,
+                            UT_REGISTERS_ADDRESS & 0xFF,
+                            (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
+                            (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
+                            /* Write */
+                            0,
+                            0,
+                            0,
+                            1,
+                            /* Write byte count */
+                            1 * 2,
+                            /* One data to write... */
+                            0x12,
+                            0x34};
     const int WRITE_RAW_REQ_LEN = 13;
-    uint8_t write_raw_req[] = {
-        slave,
-        /* function will be set in the loop */
-        MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
-        /* Address */
-        UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
-        /* 3 values, 6 bytes */
-        0x00, 0x03, 0x06,
-        /* Dummy data to write */
-        0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
-    };
+    uint8_t write_raw_req[] = {slave,
+                               /* function will be set in the loop */
+                               MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
+                               /* Address */
+                               UT_REGISTERS_ADDRESS >> 8,
+                               UT_REGISTERS_ADDRESS & 0xFF,
+                               /* 3 values, 6 bytes */
+                               0x00,
+                               0x03,
+                               0x06,
+                               /* Dummy data to write */
+                               0x02,
+                               0x2B,
+                               0x00,
+                               0x01,
+                               0x00,
+                               0x64};
     const int INVALID_FC = 0x42;
     const int INVALID_FC_REQ_LEN = 6;
-    uint8_t invalid_fc_raw_req[] = {
-        slave, 0x42, 0x00, 0x00, 0x00, 0x00
-    };
+    uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00};
 
     int req_length;
     uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
-    int tab_read_function[] = {
-        MODBUS_FC_READ_COILS,
-        MODBUS_FC_READ_DISCRETE_INPUTS,
-        MODBUS_FC_READ_HOLDING_REGISTERS,
-        MODBUS_FC_READ_INPUT_REGISTERS
-    };
-    int tab_read_nb_max[] = {
-        MODBUS_MAX_READ_BITS + 1,
-        MODBUS_MAX_READ_BITS + 1,
-        MODBUS_MAX_READ_REGISTERS + 1,
-        MODBUS_MAX_READ_REGISTERS + 1
-    };
+    int tab_read_function[] = {MODBUS_FC_READ_COILS,
+                               MODBUS_FC_READ_DISCRETE_INPUTS,
+                               MODBUS_FC_READ_HOLDING_REGISTERS,
+                               MODBUS_FC_READ_INPUT_REGISTERS};
+    int tab_read_nb_max[] = {MODBUS_MAX_READ_BITS + 1,
+                             MODBUS_MAX_READ_BITS + 1,
+                             MODBUS_MAX_READ_REGISTERS + 1,
+                             MODBUS_MAX_READ_REGISTERS + 1};
     int backend_length;
     int backend_offset;
 
@@ -820,58 +830,84 @@ int test_server(modbus_t *ctx, int use_backend)
 
     /* Try to read more values than a response could hold for all data
        types. */
-    for (i=0; i<4; i++) {
-        rc = send_crafted_request(ctx, tab_read_function[i],
-                                  read_raw_req, READ_RAW_REQ_LEN,
-                                  tab_read_nb_max[i], 0,
-                                  backend_length, backend_offset);
+    for (i = 0; i < 4; i++) {
+        rc = send_crafted_request(ctx,
+                                  tab_read_function[i],
+                                  read_raw_req,
+                                  READ_RAW_REQ_LEN,
+                                  tab_read_nb_max[i],
+                                  0,
+                                  backend_length,
+                                  backend_offset);
         if (rc == -1)
             goto close;
     }
 
-    rc = send_crafted_request(ctx, MODBUS_FC_WRITE_AND_READ_REGISTERS,
-                              rw_raw_req, RW_RAW_REQ_LEN,
-                              MODBUS_MAX_WR_READ_REGISTERS + 1, 0,
-                              backend_length, backend_offset);
+    rc = send_crafted_request(ctx,
+                              MODBUS_FC_WRITE_AND_READ_REGISTERS,
+                              rw_raw_req,
+                              RW_RAW_REQ_LEN,
+                              MODBUS_MAX_WR_READ_REGISTERS + 1,
+                              0,
+                              backend_length,
+                              backend_offset);
     if (rc == -1)
         goto close;
 
-    rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
-                              write_raw_req, WRITE_RAW_REQ_LEN,
-                              MODBUS_MAX_WRITE_REGISTERS + 1, 6,
-                              backend_length, backend_offset);
+    rc = send_crafted_request(ctx,
+                              MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
+                              write_raw_req,
+                              WRITE_RAW_REQ_LEN,
+                              MODBUS_MAX_WRITE_REGISTERS + 1,
+                              6,
+                              backend_length,
+                              backend_offset);
     if (rc == -1)
         goto close;
 
-    rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,
-                              write_raw_req, WRITE_RAW_REQ_LEN,
-                              MODBUS_MAX_WRITE_BITS + 1, 6,
-                              backend_length, backend_offset);
+    rc = send_crafted_request(ctx,
+                              MODBUS_FC_WRITE_MULTIPLE_COILS,
+                              write_raw_req,
+                              WRITE_RAW_REQ_LEN,
+                              MODBUS_MAX_WRITE_BITS + 1,
+                              6,
+                              backend_length,
+                              backend_offset);
     if (rc == -1)
         goto close;
 
     /* Modbus write multiple registers with large number of values but a set a
        small number of bytes in requests (not nb * 2 as usual). */
-    rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
-                              write_raw_req, WRITE_RAW_REQ_LEN,
-                              MODBUS_MAX_WRITE_REGISTERS, 6,
-                              backend_length, backend_offset);
+    rc = send_crafted_request(ctx,
+                              MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
+                              write_raw_req,
+                              WRITE_RAW_REQ_LEN,
+                              MODBUS_MAX_WRITE_REGISTERS,
+                              6,
+                              backend_length,
+                              backend_offset);
     if (rc == -1)
         goto close;
 
-    rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,
-                              write_raw_req, WRITE_RAW_REQ_LEN,
-                              MODBUS_MAX_WRITE_BITS, 6,
-                              backend_length, backend_offset);
+    rc = send_crafted_request(ctx,
+                              MODBUS_FC_WRITE_MULTIPLE_COILS,
+                              write_raw_req,
+                              WRITE_RAW_REQ_LEN,
+                              MODBUS_MAX_WRITE_BITS,
+                              6,
+                              backend_length,
+                              backend_offset);
     if (rc == -1)
         goto close;
 
     /* Test invalid function code */
-    modbus_send_raw_request(ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
+    modbus_send_raw_request(
+        ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
     rc = modbus_receive_confirmation(ctx, rsp);
     printf("Return an exception on unknown function code: ");
     ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
-                rsp[backend_offset] == (0x80 + INVALID_FC), "")
+                    rsp[backend_offset] == (0x80 + INVALID_FC),
+                "")
 
     modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
     return 0;
@@ -880,16 +916,19 @@ close:
     return -1;
 }
 
-
-int send_crafted_request(modbus_t *ctx, int function,
-                         uint8_t *req, int req_len,
-                         uint16_t max_value, uint16_t bytes,
-                         int backend_length, int backend_offset)
+int send_crafted_request(modbus_t *ctx,
+                         int function,
+                         uint8_t *req,
+                         int req_len,
+                         uint16_t max_value,
+                         uint16_t bytes,
+                         int backend_length,
+                         int backend_offset)
 {
     uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
     int j;
 
-    for (j=0; j<2; j++) {
+    for (j = 0; j < 2; j++) {
         int rc;
 
         req[1] = function;
@@ -913,15 +952,19 @@ int send_crafted_request(modbus_t *ctx, int function,
 
         modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
         if (j == 0) {
-            printf("* try function 0x%X: %s 0 values: ", function, bytes ? "write": "read");
+            printf(
+                "* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read");
         } else {
-            printf("* try function 0x%X: %s %d values: ", function, bytes ? "write": "read",
+            printf("* try function 0x%X: %s %d values: ",
+                   function,
+                   bytes ? "write" : "read",
                    max_value);
         }
         rc = modbus_receive_confirmation(ctx, rsp);
         ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
-                    rsp[backend_offset] == (0x80 + function) &&
-                    rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
+                        rsp[backend_offset] == (0x80 + function) &&
+                        rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
+                    "");
     }
     return 0;
 close:

+ 42 - 37
tests/unit-test-server.c

@@ -4,12 +4,14 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
 #include <modbus.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+// clang-format off
 #ifdef _WIN32
 # include <winsock2.h>
 #else
@@ -20,6 +22,7 @@
 #ifndef MSG_NOSIGNAL
 # define MSG_NOSIGNAL 0
 #endif
+// clang-format on
 
 #include "unit-test.h"
 
@@ -29,7 +32,7 @@ enum {
     RTU
 };
 
-int main(int argc, char*argv[])
+int main(int argc, char *argv[])
 {
     int s = -1;
     modbus_t *ctx;
@@ -48,7 +51,8 @@ int main(int argc, char*argv[])
         } else if (strcmp(argv[1], "rtu") == 0) {
             use_backend = RTU;
         } else {
-            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
+            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n",
+                   argv[0]);
             return -1;
         }
     } else {
@@ -71,14 +75,16 @@ int main(int argc, char*argv[])
 
     modbus_set_debug(ctx, TRUE);
 
-    mb_mapping = modbus_mapping_new_start_address(
-        UT_BITS_ADDRESS, UT_BITS_NB,
-        UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
-        UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX,
-        UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB);
+    mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS,
+                                                  UT_BITS_NB,
+                                                  UT_INPUT_BITS_ADDRESS,
+                                                  UT_INPUT_BITS_NB,
+                                                  UT_REGISTERS_ADDRESS,
+                                                  UT_REGISTERS_NB_MAX,
+                                                  UT_INPUT_REGISTERS_ADDRESS,
+                                                  UT_INPUT_REGISTERS_NB);
     if (mb_mapping == NULL) {
-        fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                modbus_strerror(errno));
+        fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
         modbus_free(ctx);
         return -1;
     }
@@ -87,11 +93,11 @@ int main(int argc, char*argv[])
        Only the read-only input values are assigned. */
 
     /* Initialize input values that's can be only done server side. */
-    modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB,
-                               UT_INPUT_BITS_TAB);
+    modbus_set_bits_from_bytes(
+        mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB);
 
     /* Initialize values of INPUT REGISTERS */
-    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
+    for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
         mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];
     }
 
@@ -127,35 +133,34 @@ int main(int argc, char*argv[])
         if (query[header_length] == 0x03) {
             /* Read holding registers */
 
-            if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
-                == UT_REGISTERS_NB_SPECIAL) {
+            if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) ==
+                UT_REGISTERS_NB_SPECIAL) {
                 printf("Set an incorrect number of values\n");
-                MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
-                                         UT_REGISTERS_NB_SPECIAL - 1);
-            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
-                       == UT_REGISTERS_ADDRESS_SPECIAL) {
+                MODBUS_SET_INT16_TO_INT8(
+                    query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
+            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
+                       UT_REGISTERS_ADDRESS_SPECIAL) {
                 printf("Reply to this special register address by an exception\n");
-                modbus_reply_exception(ctx, query,
-                                       MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
+                modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
                 continue;
-            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
-                       == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
+            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
+                       UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
                 const int RAW_REQ_LENGTH = 5;
-                uint8_t raw_req[] = {
-                    (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
-                    0x03,
-                    0x02, 0x00, 0x00
-                };
+                uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
+                                     0x03,
+                                     0x02,
+                                     0x00,
+                                     0x00};
 
                 printf("Reply with an invalid TID or slave\n");
                 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
                 continue;
-            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
-                       == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
+            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
+                       UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
                 printf("Sleep 0.5 s before replying\n");
                 usleep(500000);
-            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
-                       == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
+            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
+                       UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
                 /* Test low level only available in TCP mode */
                 /* Catch the reply and send reply byte a byte */
                 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
@@ -168,10 +173,10 @@ int main(int argc, char*argv[])
 
                 /* Copy TID */
                 req[1] = query[1];
-                for (i=0; i < req_length; i++) {
+                for (i = 0; i < req_length; i++) {
                     printf("(%.2X)", req[i]);
                     usleep(5000);
-                    rc = send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL);
+                    rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL);
                     if (rc == -1) {
                         break;
                     }

+ 2 - 0
tests/unit-test.h.in

@@ -11,6 +11,7 @@
 #define HAVE_INTTYPES_H @HAVE_INTTYPES_H@
 #define HAVE_STDINT_H @HAVE_STDINT_H@
 
+// clang-format off
 #ifdef HAVE_INTTYPES_H
 #include <inttypes.h>
 #endif
@@ -21,6 +22,7 @@
 # include "stdint.h"
 # endif
 #endif
+// clang-format on
 
 #define SERVER_ID         17
 #define INVALID_SERVER_ID 18

+ 7 - 3
tests/version.c

@@ -4,14 +4,18 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <stdio.h>
 #include <modbus.h>
+#include <stdio.h>
 
 int main(void)
 {
-    printf("Compiled with libmodbus version %s (%06X)\n", LIBMODBUS_VERSION_STRING, LIBMODBUS_VERSION_HEX);
+    printf("Compiled with libmodbus version %s (%06X)\n",
+           LIBMODBUS_VERSION_STRING,
+           LIBMODBUS_VERSION_HEX);
     printf("Linked with libmodbus version %d.%d.%d\n",
-           libmodbus_version_major, libmodbus_version_minor, libmodbus_version_micro);
+           libmodbus_version_major,
+           libmodbus_version_minor,
+           libmodbus_version_micro);
 
     if (LIBMODBUS_VERSION_CHECK(2, 1, 0)) {
         printf("The functions to read/write float values are available (2.1.0).\n");

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно