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

Change indent level from 8 to 4

Stéphane Raimbault 14 жил өмнө
parent
commit
66c59d6f85

+ 1 - 1
.dir-locals.el

@@ -1,4 +1,4 @@
 ((nil . ((indent-tabs-mode . nil)
-         (c-basic-offset . 8)
+         (c-basic-offset . 4)
          (fill-column . 80))))
 

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


+ 64 - 65
src/modbus.h.in

@@ -59,7 +59,7 @@ extern "C" {
  * Maximum between :
  * - HEADER_LENGTH_TCP (7) + function (1) + address (2) + number (2)
  * - HEADER_LENGTH_RTU (1) + function (1) + address (2) + number (2) + CRC (2)
-*/
+ */
 #define MIN_QUERY_LENGTH           12
 
 /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5:
@@ -123,20 +123,20 @@ extern "C" {
 #define MODBUS_ENOBASE 112345678
 
 /* Protocol exceptions */
-enum {
-        MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
-        MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
-        MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
-        MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
-        MODBUS_EXCEPTION_ACKNOWLEDGE,
-        MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
-        MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
-        MODBUS_EXCEPTION_MEMORY_PARITY,
-        MODBUS_EXCEPTION_NOT_DEFINED,
-        MODBUS_EXCEPTION_GATEWAY_PATH,
-        MODBUS_EXCEPTION_GATEWAY_TARGET,
-        MODBUS_EXCEPTION_MAX
-};
+typedef enum {
+    MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 0x01,
+    MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS,
+    MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE,
+    MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE,
+    MODBUS_EXCEPTION_ACKNOWLEDGE,
+    MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY,
+    MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE,
+    MODBUS_EXCEPTION_MEMORY_PARITY,
+    MODBUS_EXCEPTION_NOT_DEFINED,
+    MODBUS_EXCEPTION_GATEWAY_PATH,
+    MODBUS_EXCEPTION_GATEWAY_TARGET,
+    MODBUS_EXCEPTION_MAX
+} modbus_exception_t;
 
 #define EMBXILFUN  (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_FUNCTION)
 #define EMBXILADD  (MODBUS_ENOBASE + MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS)
@@ -177,20 +177,20 @@ enum {
 #define LIBMODBUS_VERSION_STRING "@LIBMODBUS_VERSION@"
 
 /* Numerically encoded version libmb, like 0x010203 */
-#define LIBMODBUS_VERSION_HEX ((LIBMODBUS_MAJOR_VERSION << 24) | \
-                               (LIBMODBUS_MINOR_VERSION << 16) | \
+#define LIBMODBUS_VERSION_HEX ((LIBMODBUS_MAJOR_VERSION << 24) |        \
+                               (LIBMODBUS_MINOR_VERSION << 16) |        \
                                (LIBMODBUS_MICRO_VERSION << 8))
 
 /* Evaluates to True if the version of libmb 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) &&     \
-         LIBMODBUS_VERSION_MICRO >= (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) &&             \
+      LIBMODBUS_VERSION_MICRO >= (micro)))
 
 extern const unsigned int libmodbus_version_major;
 extern const unsigned int libmodbus_version_minor;
@@ -200,54 +200,53 @@ typedef enum { RTU=0, TCP } type_com_t;
 
 /* This structure is byte-aligned */
 typedef struct {
-        /* Slave address */
-        int slave;
-        /* Descriptor (tty or socket) */
-        int fd;
-        /* Communication mode: RTU or TCP */
-        type_com_t type_com;
-        /* Flag debug */
-        int debug;
-        /* TCP port */
-        int port;
-        /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*"
-           on Mac OS X for KeySpan USB<->Serial adapters this string
-           had to be made bigger on OS X as the directory+file name
-           was bigger than 19 bytes. Making it 67 bytes for now, but
-           OS X does support 256 byte file names. May become a problem
-           in the future. */
+    /* Slave address */
+    int slave;
+    /* Descriptor (tty or socket) */
+    int fd;
+    /* Communication mode: RTU or TCP */
+    type_com_t type_com;
+    /* Flag debug */
+    int debug;
+    /* TCP port */
+    int port;
+    /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X for
+       KeySpan USB<->Serial adapters this string had to be made bigger on OS X
+       as the directory+file name was bigger than 19 bytes. Making it 67 bytes
+       for now, but OS X does support 256 byte file names. May become a problem
+       in the future. */
 #ifdef __APPLE_CC__
-        char device[64];
+    char device[64];
 #else
-        char device[16];
+    char device[16];
 #endif
-        /* Bauds: 9600, 19200, 57600, 115200, etc */
-        int baud;
-        /* Data bit */
-        uint8_t data_bit;
-        /* Stop bit */
-        uint8_t stop_bit;
-        /* Parity: 'N', 'O', 'E' */
-        char parity;
-        /* In error_treat with TCP, do a reconnect or just dump the error */
-        uint8_t error_recovery;
+    /* Bauds: 9600, 19200, 57600, 115200, etc */
+    int baud;
+    /* Data bit */
+    uint8_t data_bit;
+    /* Stop bit */
+    uint8_t stop_bit;
+    /* Parity: 'N', 'O', 'E' */
+    char parity;
+    /* In error_treat with TCP, do a reconnect or just dump the error */
+    uint8_t error_recovery;
         /* IP address */
-        char ip[16];
-        /* Save old termios settings */
-        struct termios old_tios;
-        struct timeval timeout_begin;
-        struct timeval timeout_end;
+    char ip[16];
+    /* Save old termios settings */
+    struct termios old_tios;
+    struct timeval timeout_begin;
+    struct timeval timeout_end;
 } modbus_param_t;
 
 typedef struct {
-        int nb_coil_status;
-        int nb_input_status;
-        int nb_input_registers;
-        int nb_holding_registers;
-        uint8_t *tab_coil_status;
-        uint8_t *tab_input_status;
-        uint16_t *tab_input_registers;
-        uint16_t *tab_holding_registers;
+    int nb_coil_status;
+    int nb_input_status;
+    int nb_input_registers;
+    int nb_holding_registers;
+    uint8_t *tab_coil_status;
+    uint8_t *tab_input_status;
+    uint16_t *tab_input_registers;
+    uint16_t *tab_holding_registers;
 } modbus_mapping_t;
 
 int modbus_init_rtu(modbus_param_t *mb_param, const char *device,

+ 111 - 111
tests/bandwidth-master.c

@@ -33,123 +33,123 @@
 
 uint32_t gettime_ms(void)
 {
-        struct timeval tv;
-        gettimeofday (&tv, NULL);
+    struct timeval tv;
+    gettimeofday (&tv, NULL);
 
-        return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 
 int main(void)
 {
-        uint8_t *tab_rp_status;
-        uint16_t *tab_rp_registers;
-        modbus_param_t mb_param;
-        int i;
-        int nb_points;
-        double elapsed;
-        uint32_t start;
-        uint32_t end;
-        uint32_t bytes;
-        uint32_t rate;
-        int rc;
-
-        /* TCP */
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-        rc = modbus_connect(&mb_param);
+    uint8_t *tab_rp_status;
+    uint16_t *tab_rp_registers;
+    modbus_param_t mb_param;
+    int i;
+    int nb_points;
+    double elapsed;
+    uint32_t start;
+    uint32_t end;
+    uint32_t bytes;
+    uint32_t rate;
+    int rc;
+
+    /* TCP */
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    rc = modbus_connect(&mb_param);
+    if (rc == -1) {
+        fprintf(stderr, "Connexion failed: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
+
+    /* Allocate and initialize the memory to store the status */
+    tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t));
+    memset(tab_rp_status, 0, MAX_STATUS * sizeof(uint8_t));
+
+    /* Allocate and initialize the memory to store the registers */
+    tab_rp_registers = (uint16_t *) malloc(MAX_REGISTERS * sizeof(uint16_t));
+    memset(tab_rp_registers, 0, MAX_REGISTERS * sizeof(uint16_t));
+
+    printf("READ COIL STATUS\n\n");
+
+    nb_points = MAX_STATUS;
+    start = gettime_ms();
+    for (i=0; i<NB_LOOPS; i++) {
+        rc = read_coil_status(&mb_param, SERVER_ID, 0, nb_points, tab_rp_status);
         if (rc == -1) {
-                fprintf(stderr, "Connexion failed: %s\n",
-                        modbus_strerror(errno));
-                return -1;
+            fprintf(stderr, "%s\n", modbus_strerror(errno));
+            return -1;
         }
-
-        /* Allocate and initialize the memory to store the status */
-        tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t));
-        memset(tab_rp_status, 0, MAX_STATUS * sizeof(uint8_t));
-
-        /* Allocate and initialize the memory to store the registers */
-        tab_rp_registers = (uint16_t *) malloc(MAX_REGISTERS * sizeof(uint16_t));
-        memset(tab_rp_registers, 0, MAX_REGISTERS * sizeof(uint16_t));
-
-        printf("READ COIL STATUS\n\n");
-
-        nb_points = MAX_STATUS;
-        start = gettime_ms();
-        for (i=0; i<NB_LOOPS; i++) {
-                rc = read_coil_status(&mb_param, SERVER_ID, 0, nb_points, tab_rp_status);
-                if (rc == -1) {
-                        fprintf(stderr, "%s\n", modbus_strerror(errno));
-                        return -1;
-                }
-        }
-        end = gettime_ms();
-        elapsed = end - start;
-
-        rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
-        printf("Transfert rate in points/seconds:\n");
-        printf("* %'d points/s\n", rate);
-        printf("\n");
-
-        bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
-        rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
-        printf("Values:\n");
-        printf("* %d x %d values\n", NB_LOOPS, nb_points);
-        printf("* %.3f ms for %d bytes\n", elapsed, bytes);
-        printf("* %'d KiB/s\n", rate);
-        printf("\n");
-
-        /* TCP: Query and reponse header and values */
-        bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
-        printf("Values and TCP Modbus overhead:\n");
-        printf("* %d x %d bytes\n", NB_LOOPS, bytes);
-        bytes = NB_LOOPS * bytes;
-        rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
-        printf("* %.3f ms for %d bytes\n", elapsed, bytes);
-        printf("* %'d KiB/s\n", rate);
-        printf("\n\n");
-
-        printf("READ HOLDING REGISTERS\n\n");
-
-        nb_points = MAX_REGISTERS;
-        start = gettime_ms();
-        for (i=0; i<NB_LOOPS; i++) {
-                rc = read_holding_registers(&mb_param, SERVER_ID, 0, nb_points, tab_rp_registers);
-                if (rc == -1) {
-                        fprintf(stderr, "%s\n", modbus_strerror(errno));
-                        return -1;
-                }
+    }
+    end = gettime_ms();
+    elapsed = end - start;
+
+    rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
+    printf("Transfert rate in points/seconds:\n");
+    printf("* %'d points/s\n", rate);
+    printf("\n");
+
+    bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
+    rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
+    printf("Values:\n");
+    printf("* %d x %d values\n", NB_LOOPS, nb_points);
+    printf("* %.3f ms for %d bytes\n", elapsed, bytes);
+    printf("* %'d KiB/s\n", rate);
+    printf("\n");
+
+    /* TCP: Query and reponse header and values */
+    bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
+    printf("Values and TCP Modbus overhead:\n");
+    printf("* %d x %d bytes\n", NB_LOOPS, bytes);
+    bytes = NB_LOOPS * bytes;
+    rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
+    printf("* %.3f ms for %d bytes\n", elapsed, bytes);
+    printf("* %'d KiB/s\n", rate);
+    printf("\n\n");
+
+    printf("READ HOLDING REGISTERS\n\n");
+
+    nb_points = MAX_REGISTERS;
+    start = gettime_ms();
+    for (i=0; i<NB_LOOPS; i++) {
+        rc = read_holding_registers(&mb_param, SERVER_ID, 0, nb_points, tab_rp_registers);
+        if (rc == -1) {
+            fprintf(stderr, "%s\n", modbus_strerror(errno));
+            return -1;
         }
-        end = gettime_ms();
-        elapsed = end - start;
-
-        rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
-        printf("Transfert rate in points/seconds:\n");
-        printf("* %'d registers/s\n", rate);
-        printf("\n");
-
-        bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
-        rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
-        printf("Values:\n");
-        printf("* %d x %d values\n", NB_LOOPS, nb_points);
-        printf("* %.3f ms for %d bytes\n", elapsed, bytes);
-        printf("* %'d KiB/s\n", rate);
-        printf("\n");
-
-        /* TCP:Query and reponse header and values */
-        bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
-        printf("Values and TCP Modbus overhead:\n");
-        printf("* %d x %d bytes\n", NB_LOOPS, bytes);
-        bytes = NB_LOOPS * bytes;
-        rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
-        printf("* %.3f ms for %d bytes\n", elapsed, bytes);
-        printf("* %'d KiB/s\n", rate);
-        printf("\n");
-
-        /* Free the memory */
-        free(tab_rp_status);
-        free(tab_rp_registers);
-
-        /* Close the connection */
-        modbus_close(&mb_param);
-
-        return 0;
+    }
+    end = gettime_ms();
+    elapsed = end - start;
+
+    rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
+    printf("Transfert rate in points/seconds:\n");
+    printf("* %'d registers/s\n", rate);
+    printf("\n");
+
+    bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
+    rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
+    printf("Values:\n");
+    printf("* %d x %d values\n", NB_LOOPS, nb_points);
+    printf("* %.3f ms for %d bytes\n", elapsed, bytes);
+    printf("* %'d KiB/s\n", rate);
+    printf("\n");
+
+    /* TCP:Query and reponse header and values */
+    bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
+    printf("Values and TCP Modbus overhead:\n");
+    printf("* %d x %d bytes\n", NB_LOOPS, bytes);
+    bytes = NB_LOOPS * bytes;
+    rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
+    printf("* %.3f ms for %d bytes\n", elapsed, bytes);
+    printf("* %'d KiB/s\n", rate);
+    printf("\n");
+
+    /* Free the memory */
+    free(tab_rp_status);
+    free(tab_rp_registers);
+
+    /* Close the connection */
+    modbus_close(&mb_param);
+
+    return 0;
 }

+ 82 - 82
tests/bandwidth-slave-many-up.c

@@ -31,101 +31,101 @@ modbus_mapping_t mb_mapping;
 
 static void close_sigint(int dummy)
 {
-        modbus_slave_close_tcp(slave_socket);
-        modbus_mapping_free(&mb_mapping);
+    modbus_slave_close_tcp(slave_socket);
+    modbus_mapping_free(&mb_mapping);
 
-        exit(dummy);
+    exit(dummy);
 }
 
 int main(void)
 {
-        int master_socket;
-        modbus_param_t mb_param;
-        int rc;
-        fd_set refset;
-        fd_set rdset;
-
-        /* Maximum file descriptor number */
-        int fdmax;
-
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-
-        rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
-        if (rc == -1) {
-                fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
+    int master_socket;
+    modbus_param_t mb_param;
+    int rc;
+    fd_set refset;
+    fd_set rdset;
 
-        slave_socket = modbus_slave_listen_tcp(&mb_param, NB_CONNECTION);
+    /* Maximum file descriptor number */
+    int fdmax;
 
-        signal(SIGINT, close_sigint);
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
 
-        /* Clear the reference set of socket */
-        FD_ZERO(&refset);
-        /* Add the slave socket */
-        FD_SET(slave_socket, &refset);
+    rc = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0);
+    if (rc == -1) {
+        fprintf(stderr, "Failed to allocate the mapping: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
 
-        /* Keep track of the max file descriptor */
-        fdmax = slave_socket;
+    slave_socket = modbus_slave_listen_tcp(&mb_param, NB_CONNECTION);
 
-        for (;;) {
-                rdset = refset;
-                if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
-                        perror("Slave select() failure.");
-                        close_sigint(1);
-                }
+    signal(SIGINT, close_sigint);
+
+    /* Clear the reference set of socket */
+    FD_ZERO(&refset);
+    /* Add the slave socket */
+    FD_SET(slave_socket, &refset);
+
+    /* Keep track of the max file descriptor */
+    fdmax = slave_socket;
 
-                /* Run through the existing connections looking for data to be
-                 * read */
-                for (master_socket = 0; master_socket <= fdmax; master_socket++) {
-
-                        if (FD_ISSET(master_socket, &rdset)) {
-                                if (master_socket == slave_socket) {
-                                        /* A client is asking a new connection */
-                                        socklen_t addrlen;
-                                        struct sockaddr_in clientaddr;
-                                        int newfd;
-
-                                        /* Handle new connections */
-                                        addrlen = sizeof(clientaddr);
-                                        memset(&clientaddr, 0, sizeof(clientaddr));
-                                        newfd = accept(slave_socket, (struct sockaddr *)&clientaddr, &addrlen);
-                                        if (newfd == -1) {
-                                                perror("Server accept() error");
-                                        } else {
-                                                FD_SET(newfd, &refset);
-
-                                                if (newfd > fdmax) {
-                                                        /* Keep track of the maximum */
-                                                        fdmax = newfd;
-                                                }
-                                                printf("New connection from %s:%d on socket %d\n",
-                                                       inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
-                                        }
-                                } else {
-                                        /* An already connected master has sent a new query */
-                                        uint8_t query[MAX_MESSAGE_LENGTH];
-
-                                        rc = modbus_slave_receive(&mb_param, master_socket, query);
-                                        if (rc != -1) {
-                                                modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
-                                        } else {
-                                                /* Connection closed by the client, end of server */
-                                                printf("Connection closed on socket %d\n", master_socket);
-                                                modbus_slave_close_tcp(master_socket);
-
-                                                /* Remove from reference set */
-                                                FD_CLR(master_socket, &refset);
-
-                                                if (master_socket == fdmax) {
-                                                        fdmax--;
-                                                }
-                                        }
-                                }
+    for (;;) {
+        rdset = refset;
+        if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
+            perror("Slave select() failure.");
+            close_sigint(1);
+        }
+
+        /* Run through the existing connections looking for data to be
+         * read */
+        for (master_socket = 0; master_socket <= fdmax; master_socket++) {
+
+            if (FD_ISSET(master_socket, &rdset)) {
+                if (master_socket == slave_socket) {
+                    /* A client is asking a new connection */
+                    socklen_t addrlen;
+                    struct sockaddr_in clientaddr;
+                    int newfd;
+
+                    /* Handle new connections */
+                    addrlen = sizeof(clientaddr);
+                    memset(&clientaddr, 0, sizeof(clientaddr));
+                    newfd = accept(slave_socket, (struct sockaddr *)&clientaddr, &addrlen);
+                    if (newfd == -1) {
+                        perror("Server accept() error");
+                    } else {
+                        FD_SET(newfd, &refset);
+
+                        if (newfd > fdmax) {
+                            /* Keep track of the maximum */
+                            fdmax = newfd;
+                        }
+                        printf("New connection from %s:%d on socket %d\n",
+                               inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
+                    }
+                } else {
+                    /* An already connected master has sent a new query */
+                    uint8_t query[MAX_MESSAGE_LENGTH];
+
+                    rc = modbus_slave_receive(&mb_param, master_socket, query);
+                    if (rc != -1) {
+                        modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
+                    } else {
+                        /* Connection closed by the client, end of server */
+                        printf("Connection closed on socket %d\n", master_socket);
+                        modbus_slave_close_tcp(master_socket);
+
+                        /* Remove from reference set */
+                        FD_CLR(master_socket, &refset);
+
+                        if (master_socket == fdmax) {
+                            fdmax--;
                         }
+                    }
                 }
+            }
         }
+    }
 
-        return 0;
+    return 0;
 }

+ 27 - 27
tests/bandwidth-slave-one.c

@@ -25,40 +25,40 @@
 
 int main(void)
 {
-        int socket;
-        modbus_param_t mb_param;
-        modbus_mapping_t mb_mapping;
-        int rc;
+    int socket;
+    modbus_param_t mb_param;
+    modbus_mapping_t mb_mapping;
+    int rc;
 
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
 
-        rc = modbus_mapping_new(&mb_mapping,  MAX_STATUS, 0, MAX_REGISTERS, 0);
-        if (rc == -1) {
-                fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
+    rc = modbus_mapping_new(&mb_mapping,  MAX_STATUS, 0, MAX_REGISTERS, 0);
+    if (rc == -1) {
+        fprintf(stderr, "Failed to allocate the mapping: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
 
-        socket = modbus_slave_listen_tcp(&mb_param, 1);
-        modbus_slave_accept_tcp(&mb_param, &socket);
+    socket = modbus_slave_listen_tcp(&mb_param, 1);
+    modbus_slave_accept_tcp(&mb_param, &socket);
 
-        for(;;) {
-                uint8_t query[MAX_MESSAGE_LENGTH];
+    for(;;) {
+        uint8_t query[MAX_MESSAGE_LENGTH];
 
-                rc = modbus_slave_receive(&mb_param, -1, query);
-                if (rc >= 0) {
-                        modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
-                } else {
-                        /* Connection closed by the client or server */
-                        break;
-                }
+        rc = modbus_slave_receive(&mb_param, -1, query);
+        if (rc >= 0) {
+            modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
+        } else {
+            /* Connection closed by the client or server */
+            break;
         }
+    }
 
-        printf("Quit the loop: %s\n", modbus_strerror(errno));
+    printf("Quit the loop: %s\n", modbus_strerror(errno));
 
-        close(socket);
-        modbus_mapping_free(&mb_mapping);
-        modbus_close(&mb_param);
+    close(socket);
+    modbus_mapping_free(&mb_mapping);
+    modbus_close(&mb_param);
 
-        return 0;
+    return 0;
 }

+ 163 - 163
tests/random-test-master.c

@@ -47,175 +47,175 @@
  */
 int main(void)
 {
-        int rc;
-        int nb_fail;
-        int nb_loop;
-        int addr;
-        int nb;
-        uint8_t *tab_rq_status;
-        uint8_t *tab_rp_status;
-        uint16_t *tab_rq_registers;
-        uint16_t *tab_rp_registers;
-        modbus_param_t mb_param;
-
-        /*
-          modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, MY_ID);
-        */
-
-        /* TCP */
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-        modbus_set_debug(&mb_param, TRUE);
-        if (modbus_connect(&mb_param) == -1) {
-                fprintf(stderr, "Connection failed: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
-
-        /* Allocate and initialize the different memory spaces */
-        nb = ADDRESS_END - ADDRESS_START;
-
-        tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
-        memset(tab_rq_status, 0, nb * sizeof(uint8_t));
-
-        tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
-        memset(tab_rp_status, 0, nb * sizeof(uint8_t));
-
-        tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
-        memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
-
-        tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
-        memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
-
-        nb_loop = nb_fail = 0;
-        while (nb_loop++ < LOOP) {
-                for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
-                        int i;
-
-                        /* Random numbers (short) */
-                        for (i=0; i<nb; i++) {
-                                tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
-                                tab_rq_status[i] = tab_rq_registers[i] % 2;
-                        }
-                        nb = ADDRESS_END - addr;
-
-                        /* SINGLE COIL */
-                        rc = force_single_coil(&mb_param, SERVER_ID, addr, tab_rq_status[0]);
-                        if (rc != 1) {
-                                printf("ERROR force_single_coil (%d)\n", rc);
-                                printf("Slave = %d, address = %d, value = %d\n",
-                                       SERVER_ID, addr, tab_rq_status[0]);
-                                nb_fail++;
-                        } else {
-                                rc = read_coil_status(&mb_param, SERVER_ID, addr, 1, tab_rp_status);
-                                if (rc != 1 || tab_rq_status[0] != tab_rp_status[0]) {
-                                        printf("ERROR read_coil_status single (%d)\n", rc);
-                                        printf("Slave = %d, address = %d\n",
-                                               SERVER_ID, addr);
-                                        nb_fail++;
-                                }
-                        }
-
-                        /* MULTIPLE COILS */
-                        rc = force_multiple_coils(&mb_param, SERVER_ID, addr, nb, tab_rq_status);
-                        if (rc != nb) {
-                                printf("ERROR force_multiple_coils (%d)\n", rc);
-                                printf("Slave = %d, address = %d, nb = %d\n",
-                                       SERVER_ID, addr, nb);
-                                nb_fail++;
-                        } else {
-                                rc = read_coil_status(&mb_param, SERVER_ID, addr, nb, tab_rp_status);
-                                if (rc != nb) {
-                                        printf("ERROR read_coil_status\n");
-                                        printf("Slave = %d, address = %d, nb = %d\n",
-                                               SERVER_ID, addr, nb);
-                                        nb_fail++;
-                                } else {
-                                        for (i=0; i<nb; i++) {
-                                                if (tab_rp_status[i] != tab_rq_status[i]) {
-                                                        printf("ERROR read_coil_status\n");
-                                                        printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
-                                                               SERVER_ID, addr,
-                                                               tab_rq_status[i], tab_rq_status[i],
-                                                               tab_rp_status[i], tab_rp_status[i]);
-                                                        nb_fail++;
-                                                }
-                                        }
-                                }
-                        }
-
-                        /* SINGLE REGISTER */
-                        rc = preset_single_register(&mb_param, SERVER_ID, addr, tab_rq_registers[0]);
-                        if (rc != 1) {
-                                printf("ERROR preset_single_register (%d)\n", rc);
-                                printf("Slave = %d, address = %d, value = %d (0x%X)\n",
-                                       SERVER_ID, addr, tab_rq_registers[0], tab_rq_registers[0]);
-                                nb_fail++;
-                        } else {
-                                rc = read_holding_registers(&mb_param, SERVER_ID, addr, 1, tab_rp_registers);
-                                if (rc != 1) {
-                                        printf("ERROR read_holding_registers single (%d)\n", rc);
-                                        printf("Slave = %d, address = %d\n",
-                                               SERVER_ID, addr);
-                                        nb_fail++;
-                                } else {
-                                        if (tab_rq_registers[0] != tab_rp_registers[0]) {
-                                                printf("ERROR read_holding_registers single\n");
-                                                printf("Slave = %d, address = %d, value = %d (0x%X) != %d (0x%X)\n",
-                                                       SERVER_ID, addr,
-                                                       tab_rq_registers[0], tab_rq_registers[0],
-                                                       tab_rp_registers[0], tab_rp_registers[0]);
-                                                nb_fail++;
-                                        }
-                                }
+    int rc;
+    int nb_fail;
+    int nb_loop;
+    int addr;
+    int nb;
+    uint8_t *tab_rq_status;
+    uint8_t *tab_rp_status;
+    uint16_t *tab_rq_registers;
+    uint16_t *tab_rp_registers;
+    modbus_param_t mb_param;
+
+    /*
+      modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, MY_ID);
+    */
+
+    /* TCP */
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    modbus_set_debug(&mb_param, TRUE);
+    if (modbus_connect(&mb_param) == -1) {
+        fprintf(stderr, "Connection failed: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
+
+    /* Allocate and initialize the different memory spaces */
+    nb = ADDRESS_END - ADDRESS_START;
+
+    tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
+    memset(tab_rq_status, 0, nb * sizeof(uint8_t));
+
+    tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
+    memset(tab_rp_status, 0, nb * sizeof(uint8_t));
+
+    tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
+    memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
+
+    tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
+    memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
+
+    nb_loop = nb_fail = 0;
+    while (nb_loop++ < LOOP) {
+        for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
+            int i;
+
+            /* Random numbers (short) */
+            for (i=0; i<nb; i++) {
+                tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
+                tab_rq_status[i] = tab_rq_registers[i] % 2;
+            }
+            nb = ADDRESS_END - addr;
+
+            /* SINGLE COIL */
+            rc = force_single_coil(&mb_param, SERVER_ID, addr, tab_rq_status[0]);
+            if (rc != 1) {
+                printf("ERROR force_single_coil (%d)\n", rc);
+                printf("Slave = %d, address = %d, value = %d\n",
+                       SERVER_ID, addr, tab_rq_status[0]);
+                nb_fail++;
+            } else {
+                rc = read_coil_status(&mb_param, SERVER_ID, addr, 1, tab_rp_status);
+                if (rc != 1 || tab_rq_status[0] != tab_rp_status[0]) {
+                    printf("ERROR read_coil_status single (%d)\n", rc);
+                    printf("Slave = %d, address = %d\n",
+                           SERVER_ID, addr);
+                    nb_fail++;
+                }
+            }
+
+            /* MULTIPLE COILS */
+            rc = force_multiple_coils(&mb_param, SERVER_ID, addr, nb, tab_rq_status);
+            if (rc != nb) {
+                printf("ERROR force_multiple_coils (%d)\n", rc);
+                printf("Slave = %d, address = %d, nb = %d\n",
+                       SERVER_ID, addr, nb);
+                nb_fail++;
+            } else {
+                rc = read_coil_status(&mb_param, SERVER_ID, addr, nb, tab_rp_status);
+                if (rc != nb) {
+                    printf("ERROR read_coil_status\n");
+                    printf("Slave = %d, address = %d, nb = %d\n",
+                           SERVER_ID, addr, nb);
+                    nb_fail++;
+                } else {
+                    for (i=0; i<nb; i++) {
+                        if (tab_rp_status[i] != tab_rq_status[i]) {
+                            printf("ERROR read_coil_status\n");
+                            printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
+                                   SERVER_ID, addr,
+                                   tab_rq_status[i], tab_rq_status[i],
+                                   tab_rp_status[i], tab_rp_status[i]);
+                            nb_fail++;
                         }
-
-                        /* MULTIPLE REGISTERS */
-                        rc = preset_multiple_registers(&mb_param, SERVER_ID, addr, nb,
-                                                        tab_rq_registers);
-                        if (rc != nb) {
-                                printf("ERROR preset_multiple_registers (%d)\n", rc);
-                                printf("Slave = %d, address = %d, nb = %d\n",
-                                               SERVER_ID, addr, nb);
-                                nb_fail++;
-                        } else {
-                                rc = read_holding_registers(&mb_param, SERVER_ID, addr, nb,
-                                                             tab_rp_registers);
-                                if (rc != nb) {
-                                        printf("ERROR read_holding_registers (%d)\n", rc);
-                                        printf("Slave = %d, address = %d, nb = %d\n",
-                                               SERVER_ID, addr, nb);
-                                        nb_fail++;
-                                } else {
-                                        for (i=0; i<nb; i++) {
-                                                if (tab_rq_registers[i] != tab_rp_registers[i]) {
-                                                        printf("ERROR read_holding_registers\n");
-                                                        printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
-                                                               SERVER_ID, addr,
-                                                               tab_rq_registers[i], tab_rq_registers[i],
-                                                               tab_rp_registers[i], tab_rp_registers[i]);
-                                                        nb_fail++;
-                                                }
-                                        }
-                                }
+                    }
+                }
+            }
+
+            /* SINGLE REGISTER */
+            rc = preset_single_register(&mb_param, SERVER_ID, addr, tab_rq_registers[0]);
+            if (rc != 1) {
+                printf("ERROR preset_single_register (%d)\n", rc);
+                printf("Slave = %d, address = %d, value = %d (0x%X)\n",
+                       SERVER_ID, addr, tab_rq_registers[0], tab_rq_registers[0]);
+                nb_fail++;
+            } else {
+                rc = read_holding_registers(&mb_param, SERVER_ID, addr, 1, tab_rp_registers);
+                if (rc != 1) {
+                    printf("ERROR read_holding_registers single (%d)\n", rc);
+                    printf("Slave = %d, address = %d\n",
+                           SERVER_ID, addr);
+                    nb_fail++;
+                } else {
+                    if (tab_rq_registers[0] != tab_rp_registers[0]) {
+                        printf("ERROR read_holding_registers single\n");
+                        printf("Slave = %d, address = %d, value = %d (0x%X) != %d (0x%X)\n",
+                               SERVER_ID, addr,
+                               tab_rq_registers[0], tab_rq_registers[0],
+                               tab_rp_registers[0], tab_rp_registers[0]);
+                        nb_fail++;
+                    }
+                }
+            }
+
+            /* MULTIPLE REGISTERS */
+            rc = preset_multiple_registers(&mb_param, SERVER_ID, addr, nb,
+                                           tab_rq_registers);
+            if (rc != nb) {
+                printf("ERROR preset_multiple_registers (%d)\n", rc);
+                printf("Slave = %d, address = %d, nb = %d\n",
+                       SERVER_ID, addr, nb);
+                nb_fail++;
+            } else {
+                rc = read_holding_registers(&mb_param, SERVER_ID, addr, nb,
+                                            tab_rp_registers);
+                if (rc != nb) {
+                    printf("ERROR read_holding_registers (%d)\n", rc);
+                    printf("Slave = %d, address = %d, nb = %d\n",
+                           SERVER_ID, addr, nb);
+                    nb_fail++;
+                } else {
+                    for (i=0; i<nb; i++) {
+                        if (tab_rq_registers[i] != tab_rp_registers[i]) {
+                            printf("ERROR read_holding_registers\n");
+                            printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
+                                   SERVER_ID, addr,
+                                   tab_rq_registers[i], tab_rq_registers[i],
+                                   tab_rp_registers[i], tab_rp_registers[i]);
+                            nb_fail++;
                         }
-
+                    }
                 }
+            }
 
-                printf("Test: ");
-                if (nb_fail)
-                        printf("%d FAILS\n", nb_fail);
-                else
-                        printf("SUCCESS\n");
         }
 
-        /* Free the memory */
-        free(tab_rq_status);
-        free(tab_rp_status);
-        free(tab_rq_registers);
-        free(tab_rp_registers);
+        printf("Test: ");
+        if (nb_fail)
+            printf("%d FAILS\n", nb_fail);
+        else
+            printf("SUCCESS\n");
+    }
+
+    /* Free the memory */
+    free(tab_rq_status);
+    free(tab_rp_status);
+    free(tab_rq_registers);
+    free(tab_rp_registers);
 
-        /* Close the connection */
-        modbus_close(&mb_param);
+    /* Close the connection */
+    modbus_close(&mb_param);
 
-        return 0;
+    return 0;
 }

+ 30 - 30
tests/random-test-slave.c

@@ -24,43 +24,43 @@
 
 int main(void)
 {
-        int socket;
-        modbus_param_t mb_param;
-        modbus_mapping_t mb_mapping;
-        int rc;
+    int socket;
+    modbus_param_t mb_param;
+    modbus_mapping_t mb_mapping;
+    int rc;
 
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-        /* modbus_set_debug(&mb_param, TRUE); */
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    /* modbus_set_debug(&mb_param, TRUE); */
 
-        rc = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500);
-        if (rc == -1) {
-                fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
+    rc = modbus_mapping_new(&mb_mapping, 500, 500, 500, 500);
+    if (rc == -1) {
+        fprintf(stderr, "Failed to allocate the mapping: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
 
-        socket = modbus_slave_listen_tcp(&mb_param, 1);
-        modbus_slave_accept_tcp(&mb_param, &socket);
+    socket = modbus_slave_listen_tcp(&mb_param, 1);
+    modbus_slave_accept_tcp(&mb_param, &socket);
 
-        for (;;) {
-                uint8_t query[MAX_MESSAGE_LENGTH];
-                int rc;
+    for (;;) {
+        uint8_t query[MAX_MESSAGE_LENGTH];
+        int rc;
 
-                rc = modbus_slave_receive(&mb_param, -1, query);
-                if (rc != -1) {
-                        /* rc is the query size */
-                        modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
-                } else {
-                        /* Connection closed by the client or error */
-                        break;
-                }
+        rc = modbus_slave_receive(&mb_param, -1, query);
+        if (rc != -1) {
+            /* rc is the query size */
+            modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
+        } else {
+            /* Connection closed by the client or error */
+            break;
         }
+    }
 
-        printf("Quit the loop: %s\n", modbus_strerror(errno));
+    printf("Quit the loop: %s\n", modbus_strerror(errno));
 
-        close(socket);
-        modbus_mapping_free(&mb_mapping);
-        modbus_close(&mb_param);
+    close(socket);
+    modbus_mapping_free(&mb_mapping);
+    modbus_close(&mb_param);
 
-        return 0;
+    return 0;
 }

+ 482 - 482
tests/unit-test-master.c

@@ -26,516 +26,516 @@
 
 int main(void)
 {
-        uint8_t *tab_rp_status;
-        uint16_t *tab_rp_registers;
-        uint16_t *tab_rp_registers_bad;
-        modbus_param_t mb_param;
-        int i;
-        uint8_t value;
-        int address;
-        int nb_points;
-        int rc;
-        float real;
-        struct timeval timeout_begin_old;
-        struct timeval timeout_begin_new;
-
-        /*
-        modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1,
-                        CLIENT_ID);
-        */
-
-        /* TCP */
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-        modbus_set_debug(&mb_param, TRUE);
-
-        if (modbus_connect(&mb_param) == -1) {
-                fprintf(stderr, "Connection failed: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
-
-        /* Allocate and initialize the memory to store the status */
-        nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
-                UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
-        tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
-        memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
-
-        /* Allocate and initialize the memory to store the registers */
-        nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
-                     UT_INPUT_REGISTERS_NB_POINTS) ?
-                UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
-        tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
-        memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
-
-        printf("** UNIT TESTING **\n");
-
-        printf("\nTEST WRITE/READ:\n");
-
-        /** COIL STATUS **/
-
-        /* Single */
-        rc = force_single_coil(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, ON);
-        printf("1/2 force_single_coil: ");
-        if (rc == 1) {
-                printf("OK\n");
+    uint8_t *tab_rp_status;
+    uint16_t *tab_rp_registers;
+    uint16_t *tab_rp_registers_bad;
+    modbus_param_t mb_param;
+    int i;
+    uint8_t value;
+    int address;
+    int nb_points;
+    int rc;
+    float real;
+    struct timeval timeout_begin_old;
+    struct timeval timeout_begin_new;
+
+    /*
+      modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1,
+      CLIENT_ID);
+    */
+
+    /* TCP */
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    modbus_set_debug(&mb_param, TRUE);
+
+    if (modbus_connect(&mb_param) == -1) {
+        fprintf(stderr, "Connection failed: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
+
+    /* Allocate and initialize the memory to store the status */
+    nb_points = (UT_COIL_STATUS_NB_POINTS > UT_INPUT_STATUS_NB_POINTS) ?
+        UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
+    tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
+    memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
+
+    /* Allocate and initialize the memory to store the registers */
+    nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
+                 UT_INPUT_REGISTERS_NB_POINTS) ?
+        UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
+    tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
+    memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
+
+    printf("** UNIT TESTING **\n");
+
+    printf("\nTEST WRITE/READ:\n");
+
+    /** COIL STATUS **/
+
+    /* Single */
+    rc = force_single_coil(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, ON);
+    printf("1/2 force_single_coil: ");
+    if (rc == 1) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, 1,
+                          tab_rp_status);
+    printf("2/2 read_coil_status: ");
+    if (rc != 1) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    if (tab_rp_status[0] != ON) {
+        printf("FAILED (%0X = != %0X)\n", tab_rp_status[0], ON);
+        goto close;
+    }
+    printf("OK\n");
+    /* End single */
+
+    /* Multiple coils */
+    {
+        uint8_t tab_value[UT_COIL_STATUS_NB_POINTS];
+
+        set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS,
+                            UT_COIL_STATUS_TAB);
+        rc = force_multiple_coils(&mb_param, SERVER_ID,
+                                  UT_COIL_STATUS_ADDRESS,
+                                  UT_COIL_STATUS_NB_POINTS,
+                                  tab_value);
+        printf("1/2 force_multiple_coils: ");
+        if (rc == UT_COIL_STATUS_NB_POINTS) {
+            printf("OK\n");
         } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS, 1,
-                              tab_rp_status);
-        printf("2/2 read_coil_status: ");
-        if (rc != 1) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
-
-        if (tab_rp_status[0] != ON) {
-                printf("FAILED (%0X = != %0X)\n", tab_rp_status[0], ON);
-                goto close;
-        }
+            printf("FAILED\n");
+            goto close;
+        }
+    }
+
+    rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS,
+                          UT_COIL_STATUS_NB_POINTS, tab_rp_status);
+    printf("2/2 read_coil_status: ");
+    if (rc != UT_COIL_STATUS_NB_POINTS) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    i = 0;
+    address = UT_COIL_STATUS_ADDRESS;
+    nb_points = UT_COIL_STATUS_NB_POINTS;
+    while (nb_points > 0) {
+        int nb_bits = (nb_points > 8) ? 8 : nb_points;
+
+        value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
+        if (value != UT_COIL_STATUS_TAB[i]) {
+            printf("FAILED (%0X != %0X)\n",
+                   value, UT_COIL_STATUS_TAB[i]);
+            goto close;
+        }
+
+        nb_points -= nb_bits;
+        i++;
+    }
+    printf("OK\n");
+    /* End of multiple coils */
+
+    /** INPUT STATUS **/
+    rc = read_input_status(&mb_param, SERVER_ID, UT_INPUT_STATUS_ADDRESS,
+                           UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
+    printf("1/1 read_input_status: ");
+
+    if (rc != UT_INPUT_STATUS_NB_POINTS) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    i = 0;
+    address = UT_INPUT_STATUS_ADDRESS;
+    nb_points = UT_INPUT_STATUS_NB_POINTS;
+    while (nb_points > 0) {
+        int nb_bits = (nb_points > 8) ? 8 : nb_points;
+
+        value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
+        if (value != UT_INPUT_STATUS_TAB[i]) {
+            printf("FAILED (%0X != %0X)\n",
+                   value, UT_INPUT_STATUS_TAB[i]);
+            goto close;
+        }
+
+        nb_points -= nb_bits;
+        i++;
+    }
+    printf("OK\n");
+
+    /** HOLDING REGISTERS **/
+
+    /* Single register */
+    rc = preset_single_register(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS, 0x1234);
+    printf("1/2 preset_single_register: ");
+    if (rc == 1) {
         printf("OK\n");
-        /* End single */
-
-        /* Multiple coils */
-        {
-                uint8_t tab_value[UT_COIL_STATUS_NB_POINTS];
-
-                set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS,
-                                    UT_COIL_STATUS_TAB);
-                rc = force_multiple_coils(&mb_param, SERVER_ID,
-                                          UT_COIL_STATUS_ADDRESS,
-                                          UT_COIL_STATUS_NB_POINTS,
-                                          tab_value);
-                printf("1/2 force_multiple_coils: ");
-                if (rc == UT_COIL_STATUS_NB_POINTS) {
-                        printf("OK\n");
-                } else {
-                        printf("FAILED\n");
-                        goto close;
-                }
-        }
-
-        rc = read_coil_status(&mb_param, SERVER_ID, UT_COIL_STATUS_ADDRESS,
-                              UT_COIL_STATUS_NB_POINTS, tab_rp_status);
-        printf("2/2 read_coil_status: ");
-        if (rc != UT_COIL_STATUS_NB_POINTS) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
-
-        i = 0;
-        address = UT_COIL_STATUS_ADDRESS;
-        nb_points = UT_COIL_STATUS_NB_POINTS;
-        while (nb_points > 0) {
-                int nb_bits = (nb_points > 8) ? 8 : nb_points;
-
-                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
-                if (value != UT_COIL_STATUS_TAB[i]) {
-                        printf("FAILED (%0X != %0X)\n",
-                               value, UT_COIL_STATUS_TAB[i]);
-                        goto close;
-                }
-
-                nb_points -= nb_bits;
-                i++;
-        }
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                1, tab_rp_registers);
+    printf("2/2 read_holding_registers: ");
+    if (rc != 1) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    if (tab_rp_registers[0] != 0x1234) {
+        printf("FAILED (%0X != %0X)\n",
+               tab_rp_registers[0], 0x1234);
+        goto close;
+    }
+    printf("OK\n");
+    /* End of single register */
+
+    /* Many registers */
+    rc = preset_multiple_registers(&mb_param, SERVER_ID,
+                                   UT_HOLDING_REGISTERS_ADDRESS,
+                                   UT_HOLDING_REGISTERS_NB_POINTS,
+                                   UT_HOLDING_REGISTERS_TAB);
+    printf("1/2 preset_multiple_registers: ");
+    if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
         printf("OK\n");
-        /* End of multiple coils */
-
-        /** INPUT STATUS **/
-        rc = read_input_status(&mb_param, SERVER_ID, UT_INPUT_STATUS_ADDRESS,
-                               UT_INPUT_STATUS_NB_POINTS, tab_rp_status);
-        printf("1/1 read_input_status: ");
-
-        if (rc != UT_INPUT_STATUS_NB_POINTS) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
-
-        i = 0;
-        address = UT_INPUT_STATUS_ADDRESS;
-        nb_points = UT_INPUT_STATUS_NB_POINTS;
-        while (nb_points > 0) {
-                int nb_bits = (nb_points > 8) ? 8 : nb_points;
-
-                value = get_byte_from_bits(tab_rp_status, i*8, nb_bits);
-                if (value != UT_INPUT_STATUS_TAB[i]) {
-                        printf("FAILED (%0X != %0X)\n",
-                               value, UT_INPUT_STATUS_TAB[i]);
-                        goto close;
-                }
-
-                nb_points -= nb_bits;
-                i++;
-        }
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS,
+                                tab_rp_registers);
+    printf("2/2 read_holding_registers: ");
+    if (rc != UT_HOLDING_REGISTERS_NB_POINTS) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
+        if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
+            printf("FAILED (%0X != %0X)\n",
+                   tab_rp_registers[i],
+                   UT_HOLDING_REGISTERS_TAB[i]);
+            goto close;
+        }
+    }
+    printf("OK\n");
+    /* End of many registers */
+
+
+    /** INPUT REGISTERS **/
+    rc = read_input_registers(&mb_param, SERVER_ID,
+                              UT_INPUT_REGISTERS_ADDRESS,
+                              UT_INPUT_REGISTERS_NB_POINTS,
+                              tab_rp_registers);
+    printf("1/1 read_input_registers: ");
+    if (rc != UT_INPUT_REGISTERS_NB_POINTS) {
+        printf("FAILED (nb points %d)\n", rc);
+        goto close;
+    }
+
+    for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
+        if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
+            printf("FAILED (%0X != %0X)\n",
+                   tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
+            goto close;
+        }
+    }
+    printf("OK\n");
+
+
+    printf("\nTEST FLOATS\n");
+    /** FLOAT **/
+    printf("1/2 Write float: ");
+    modbus_write_float(UT_REAL, tab_rp_registers);
+    if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
+        tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
+        printf("OK\n");
+    } else {
+        printf("FAILED (%x != %x)\n",
+               *((uint32_t *)tab_rp_registers), UT_IREAL);
+        goto close;
+    }
+
+    printf("2/2 Read float: ");
+    real = modbus_read_float(tab_rp_registers);
+    if (real == UT_REAL) {
         printf("OK\n");
+    } else {
+        printf("FAILED (%f != %f)\n", real, UT_REAL);
+        goto close;
+    }
 
-        /** HOLDING REGISTERS **/
+    printf("\nAt this point, error messages doesn't mean the test has failed\n");
 
-        /* Single register */
-        rc = preset_single_register(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS, 0x1234);
-        printf("1/2 preset_single_register: ");
-        if (rc == 1) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
+    /** ILLEGAL DATA ADDRESS **/
+    printf("\nTEST ILLEGAL DATA ADDRESS:\n");
 
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    1, tab_rp_registers);
-        printf("2/2 read_holding_registers: ");
-        if (rc != 1) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
+    /* The mapping begins at 0 and ending at address + nb_points so
+     * the addresses below are not valid. */
 
-        if (tab_rp_registers[0] != 0x1234) {
-                printf("FAILED (%0X != %0X)\n",
-                       tab_rp_registers[0], 0x1234);
-                goto close;
-        }
+    rc = read_coil_status(&mb_param, SERVER_ID,
+                          UT_COIL_STATUS_ADDRESS,
+                          UT_COIL_STATUS_NB_POINTS + 1,
+                          tab_rp_status);
+    printf("* read_coil_status: ");
+    if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
-        /* End of single register */
-
-        /* Many registers */
-        rc = preset_multiple_registers(&mb_param, SERVER_ID,
-                                       UT_HOLDING_REGISTERS_ADDRESS,
-                                       UT_HOLDING_REGISTERS_NB_POINTS,
-                                       UT_HOLDING_REGISTERS_TAB);
-        printf("1/2 preset_multiple_registers: ");
-        if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS,
-                                    tab_rp_registers);
-        printf("2/2 read_holding_registers: ");
-        if (rc != UT_HOLDING_REGISTERS_NB_POINTS) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
-
-        for (i=0; i < UT_HOLDING_REGISTERS_NB_POINTS; i++) {
-                if (tab_rp_registers[i] != UT_HOLDING_REGISTERS_TAB[i]) {
-                        printf("FAILED (%0X != %0X)\n",
-                               tab_rp_registers[i],
-                               UT_HOLDING_REGISTERS_TAB[i]);
-                        goto close;
-                }
-        }
+    else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_input_status(&mb_param, SERVER_ID,
+                           UT_INPUT_STATUS_ADDRESS,
+                           UT_INPUT_STATUS_NB_POINTS + 1,
+                           tab_rp_status);
+    printf("* read_input_status: ");
+    if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
-        /* End of many registers */
-
-
-        /** INPUT REGISTERS **/
-        rc = read_input_registers(&mb_param, SERVER_ID,
-                                  UT_INPUT_REGISTERS_ADDRESS,
-                                  UT_INPUT_REGISTERS_NB_POINTS,
-                                  tab_rp_registers);
-        printf("1/1 read_input_registers: ");
-        if (rc != UT_INPUT_REGISTERS_NB_POINTS) {
-                printf("FAILED (nb points %d)\n", rc);
-                goto close;
-        }
-
-        for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
-                if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
-                        printf("FAILED (%0X != %0X)\n",
-                               tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
-                        goto close;
-                }
-        }
+    else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS + 1,
+                                tab_rp_registers);
+    printf("* read_holding_registers: ");
+    if (rc == -1 && errno == EMBXILADD)
         printf("OK\n");
-
-
-        printf("\nTEST FLOATS\n");
-        /** FLOAT **/
-        printf("1/2 Write float: ");
-        modbus_write_float(UT_REAL, tab_rp_registers);
-        if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
-            tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
-                printf("OK\n");
-        } else {
-                printf("FAILED (%x != %x)\n",
-                       *((uint32_t *)tab_rp_registers), UT_IREAL);
-                goto close;
-        }
-
-        printf("2/2 Read float: ");
-        real = modbus_read_float(tab_rp_registers);
-        if (real == UT_REAL) {
-                printf("OK\n");
-        } else {
-                printf("FAILED (%f != %f)\n", real, UT_REAL);
-                goto close;
-        }
-
-        printf("\nAt this point, error messages doesn't mean the test has failed\n");
-
-        /** ILLEGAL DATA ADDRESS **/
-        printf("\nTEST ILLEGAL DATA ADDRESS:\n");
-
-        /* The mapping begins at 0 and ending at address + nb_points so
-         * the addresses below are not valid. */
-
-        rc = read_coil_status(&mb_param, SERVER_ID,
-                              UT_COIL_STATUS_ADDRESS,
-                              UT_COIL_STATUS_NB_POINTS + 1,
+    else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_input_registers(&mb_param, SERVER_ID,
+                              UT_INPUT_REGISTERS_ADDRESS,
+                              UT_INPUT_REGISTERS_NB_POINTS + 1,
+                              tab_rp_registers);
+    printf("* read_input_registers: ");
+    if (rc == -1 && errno == EMBXILADD)
+        printf("OK\n");
+    else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = force_single_coil(&mb_param, SERVER_ID,
+                           UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
+                           ON);
+    printf("* force_single_coil: ");
+    if (rc == -1 && errno == EMBXILADD) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = force_multiple_coils(&mb_param, SERVER_ID,
+                              UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
+                              UT_COIL_STATUS_NB_POINTS,
                               tab_rp_status);
-        printf("* read_coil_status: ");
-        if (rc == -1 && errno == EMBXILADD)
-                printf("OK\n");
-        else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_input_status(&mb_param, SERVER_ID,
-                               UT_INPUT_STATUS_ADDRESS,
-                               UT_INPUT_STATUS_NB_POINTS + 1,
-                               tab_rp_status);
-        printf("* read_input_status: ");
-        if (rc == -1 && errno == EMBXILADD)
-                printf("OK\n");
-        else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS + 1,
-                                    tab_rp_registers);
-        printf("* read_holding_registers: ");
-        if (rc == -1 && errno == EMBXILADD)
-                printf("OK\n");
-        else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_input_registers(&mb_param, SERVER_ID,
-                                  UT_INPUT_REGISTERS_ADDRESS,
-                                  UT_INPUT_REGISTERS_NB_POINTS + 1,
-                                  tab_rp_registers);
-        printf("* read_input_registers: ");
-        if (rc == -1 && errno == EMBXILADD)
-                printf("OK\n");
-        else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = force_single_coil(&mb_param, SERVER_ID,
-                               UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
-                               ON);
-        printf("* force_single_coil: ");
-        if (rc == -1 && errno == EMBXILADD) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = force_multiple_coils(&mb_param, SERVER_ID,
-                                  UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
-                                  UT_COIL_STATUS_NB_POINTS,
-                                  tab_rp_status);
-        printf("* force_multiple_coils: ");
-        if (rc == -1 && errno == EMBXILADD) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
+    printf("* force_multiple_coils: ");
+    if (rc == -1 && errno == EMBXILADD) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = preset_multiple_registers(&mb_param, SERVER_ID,
+                                   UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
+                                   UT_HOLDING_REGISTERS_NB_POINTS,
+                                   tab_rp_registers);
+    printf("* preset_multiple_registers: ");
+    if (rc == -1 && errno == EMBXILADD) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
 
-        rc = preset_multiple_registers(&mb_param, SERVER_ID,
-                                       UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
-                                       UT_HOLDING_REGISTERS_NB_POINTS,
-                                       tab_rp_registers);
-        printf("* preset_multiple_registers: ");
-        if (rc == -1 && errno == EMBXILADD) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
 
+    /** TOO MANY DATA **/
+    printf("\nTEST TOO MANY DATA ERROR:\n");
 
-        /** TOO MANY DATA **/
-        printf("\nTEST TOO MANY DATA ERROR:\n");
+    rc = read_coil_status(&mb_param, SERVER_ID,
+                          UT_COIL_STATUS_ADDRESS,
+                          MAX_STATUS + 1,
+                          tab_rp_status);
+    printf("* read_coil_status: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_input_status(&mb_param, SERVER_ID,
+                           UT_INPUT_STATUS_ADDRESS,
+                           MAX_STATUS + 1,
+                           tab_rp_status);
+    printf("* read_input_status: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                MAX_REGISTERS + 1,
+                                tab_rp_registers);
+    printf("* read_holding_registers: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    rc = read_input_registers(&mb_param, SERVER_ID,
+                              UT_INPUT_REGISTERS_ADDRESS,
+                              MAX_REGISTERS + 1,
+                              tab_rp_registers);
+    printf("* read_input_registers: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
 
-        rc = read_coil_status(&mb_param, SERVER_ID,
+    rc = force_multiple_coils(&mb_param, SERVER_ID,
                               UT_COIL_STATUS_ADDRESS,
                               MAX_STATUS + 1,
                               tab_rp_status);
-        printf("* read_coil_status: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_input_status(&mb_param, SERVER_ID,
-                               UT_INPUT_STATUS_ADDRESS,
-                               MAX_STATUS + 1,
-                               tab_rp_status);
-        printf("* read_input_status: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    MAX_REGISTERS + 1,
-                                    tab_rp_registers);
-        printf("* read_holding_registers: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = read_input_registers(&mb_param, SERVER_ID,
-                                  UT_INPUT_REGISTERS_ADDRESS,
-                                  MAX_REGISTERS + 1,
-                                  tab_rp_registers);
-        printf("* read_input_registers: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        rc = force_multiple_coils(&mb_param, SERVER_ID,
-                                  UT_COIL_STATUS_ADDRESS,
-                                  MAX_STATUS + 1,
-                                  tab_rp_status);
-        printf("* force_multiple_coils: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                goto close;
-                printf("FAILED\n");
-        }
-
-        rc = preset_multiple_registers(&mb_param, SERVER_ID,
-                                       UT_HOLDING_REGISTERS_ADDRESS,
-                                       MAX_REGISTERS + 1,
-                                       tab_rp_registers);
-        printf("* preset_multiple_registers: ");
-        if (rc == -1 && errno == EMBMDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        /** SLAVE REPLY **/
-        printf("\nTEST SLAVE REPLY:\n");
-
-        rc = read_holding_registers(&mb_param, 18,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS,
-                                    tab_rp_registers);
-        printf("1/3 No or response from slave %d: ", 18);
-        if (mb_param.type_com == RTU) {
-                /* No response in RTU mode */
-                if (rc == -1 && errno == ETIMEDOUT) {
-                        printf("OK\n");
-                } else {
-                        printf("FAILED\n");
-                        goto close;
-                }
+    printf("* force_multiple_coils: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        goto close;
+        printf("FAILED\n");
+    }
+
+    rc = preset_multiple_registers(&mb_param, SERVER_ID,
+                                   UT_HOLDING_REGISTERS_ADDRESS,
+                                   MAX_REGISTERS + 1,
+                                   tab_rp_registers);
+    printf("* preset_multiple_registers: ");
+    if (rc == -1 && errno == EMBMDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    /** SLAVE REPLY **/
+    printf("\nTEST SLAVE REPLY:\n");
+
+    rc = read_holding_registers(&mb_param, 18,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS,
+                                tab_rp_registers);
+    printf("1/3 No or response from slave %d: ", 18);
+    if (mb_param.type_com == RTU) {
+        /* No response in RTU mode */
+        if (rc == -1 && errno == ETIMEDOUT) {
+            printf("OK\n");
         } else {
-                /* Response in TCP mode */
-                if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
-                        printf("OK\n");
-                } else {
-                        printf("FAILED\n");
-                        goto close;
-                }
+            printf("FAILED\n");
+            goto close;
         }
-
-        rc = read_holding_registers(&mb_param, MODBUS_BROADCAST_ADDRESS,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS,
-                                    tab_rp_registers);
-        printf("2/3 Reply after a broadcast query: ");
+    } else {
+        /* Response in TCP mode */
         if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-
-        /* Save original timeout */
-        modbus_get_timeout_begin(&mb_param, &timeout_begin_old);
-
-        /* Define a new and too short timeout */
-        timeout_begin_new.tv_sec = 0;
-        timeout_begin_new.tv_usec = 0;
-        modbus_set_timeout_begin(&mb_param, &timeout_begin_new);
-
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS,
-                                    tab_rp_registers);
-        printf("3/3 Too short timeout: ");
-        if (rc == -1 && errno == ETIMEDOUT) {
-                printf("OK\n");
+            printf("OK\n");
         } else {
-                printf("FAILED\n");
-                goto close;
+            printf("FAILED\n");
+            goto close;
         }
+    }
 
-        /* Restore original timeout */
-        modbus_set_timeout_begin(&mb_param, &timeout_begin_old);
-
-        /** BAD RESPONSE **/
-        printf("\nTEST BAD RESPONSE ERROR:\n");
-
-        /* Allocate only the required space */
-        tab_rp_registers_bad = (uint16_t *) malloc(
-                UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
-        rc = read_holding_registers(&mb_param, SERVER_ID,
-                                    UT_HOLDING_REGISTERS_ADDRESS,
-                                    UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL,
-                                    tab_rp_registers_bad);
-        printf("* read_holding_registers: ");
-        if (rc == -1 && errno == EMBBADDATA) {
-                printf("OK\n");
-        } else {
-                printf("FAILED\n");
-                goto close;
-        }
-        free(tab_rp_registers_bad);
+    rc = read_holding_registers(&mb_param, MODBUS_BROADCAST_ADDRESS,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS,
+                                tab_rp_registers);
+    printf("2/3 Reply after a broadcast query: ");
+    if (rc == UT_HOLDING_REGISTERS_NB_POINTS) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    /* Save original timeout */
+    modbus_get_timeout_begin(&mb_param, &timeout_begin_old);
+
+    /* Define a new and too short timeout */
+    timeout_begin_new.tv_sec = 0;
+    timeout_begin_new.tv_usec = 0;
+    modbus_set_timeout_begin(&mb_param, &timeout_begin_new);
+
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS,
+                                tab_rp_registers);
+    printf("3/3 Too short timeout: ");
+    if (rc == -1 && errno == ETIMEDOUT) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+
+    /* Restore original timeout */
+    modbus_set_timeout_begin(&mb_param, &timeout_begin_old);
+
+    /** BAD RESPONSE **/
+    printf("\nTEST BAD RESPONSE ERROR:\n");
+
+    /* Allocate only the required space */
+    tab_rp_registers_bad = (uint16_t *) malloc(
+        UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL * sizeof(uint16_t));
+    rc = read_holding_registers(&mb_param, SERVER_ID,
+                                UT_HOLDING_REGISTERS_ADDRESS,
+                                UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL,
+                                tab_rp_registers_bad);
+    printf("* read_holding_registers: ");
+    if (rc == -1 && errno == EMBBADDATA) {
+        printf("OK\n");
+    } else {
+        printf("FAILED\n");
+        goto close;
+    }
+    free(tab_rp_registers_bad);
 
-        printf("\nALL TESTS PASS WITH SUCCESS.\n");
+    printf("\nALL TESTS PASS WITH SUCCESS.\n");
 
 close:
-        /* Free the memory */
-        free(tab_rp_status);
-        free(tab_rp_registers);
+    /* Free the memory */
+    free(tab_rp_status);
+    free(tab_rp_registers);
 
-        /* Close the connection */
-        modbus_close(&mb_param);
+    /* Close the connection */
+    modbus_close(&mb_param);
 
-        return 0;
+    return 0;
 }

+ 55 - 55
tests/unit-test-slave.c

@@ -26,72 +26,72 @@
 
 int main(void)
 {
-        int socket;
-        modbus_param_t mb_param;
-        modbus_mapping_t mb_mapping;
-        int rc;
-        int i;
+    int socket;
+    modbus_param_t mb_param;
+    modbus_mapping_t mb_mapping;
+    int rc;
+    int i;
 
-        modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
-        modbus_set_debug(&mb_param, TRUE);
-        modbus_set_error_recovery(&mb_param, TRUE);
+    modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
+    modbus_set_debug(&mb_param, TRUE);
+    modbus_set_error_recovery(&mb_param, TRUE);
 
-        rc = modbus_mapping_new(&mb_mapping,
-                                 UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
-                                 UT_INPUT_STATUS_ADDRESS + UT_INPUT_STATUS_NB_POINTS,
-                                 UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
-                                 UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
-        if (rc == -1) {
-                fprintf(stderr, "Failed to allocate the mapping: %s\n",
-                        modbus_strerror(errno));
-                return -1;
-        }
+    rc = modbus_mapping_new(&mb_mapping,
+                            UT_COIL_STATUS_ADDRESS + UT_COIL_STATUS_NB_POINTS,
+                            UT_INPUT_STATUS_ADDRESS + UT_INPUT_STATUS_NB_POINTS,
+                            UT_HOLDING_REGISTERS_ADDRESS + UT_HOLDING_REGISTERS_NB_POINTS,
+                            UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
+    if (rc == -1) {
+        fprintf(stderr, "Failed to allocate the mapping: %s\n",
+                modbus_strerror(errno));
+        return -1;
+    }
 
-        /* Examples from PI_MODBUS_300.pdf.
-           Only the read-only input values are assigned. */
+    /* Examples from PI_MODBUS_300.pdf.
+       Only the read-only input values are assigned. */
 
-        /** INPUT STATUS **/
-        set_bits_from_bytes(mb_mapping.tab_input_status,
-                            UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS,
-                            UT_INPUT_STATUS_TAB);
+    /** INPUT STATUS **/
+    set_bits_from_bytes(mb_mapping.tab_input_status,
+                        UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS,
+                        UT_INPUT_STATUS_TAB);
 
-        /** INPUT REGISTERS **/
-        for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
-                mb_mapping.tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
-                        UT_INPUT_REGISTERS_TAB[i];;
-        }
+    /** INPUT REGISTERS **/
+    for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
+        mb_mapping.tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
+            UT_INPUT_REGISTERS_TAB[i];;
+    }
 
-        socket = modbus_slave_listen_tcp(&mb_param, 1);
-        modbus_slave_accept_tcp(&mb_param, &socket);
+    socket = modbus_slave_listen_tcp(&mb_param, 1);
+    modbus_slave_accept_tcp(&mb_param, &socket);
 
-        for (;;) {
-                uint8_t query[MAX_MESSAGE_LENGTH];
+    for (;;) {
+        uint8_t query[MAX_MESSAGE_LENGTH];
 
-                rc = modbus_slave_receive(&mb_param, -1, query);
-                if (rc > 0) {
-                        if (((query[HEADER_LENGTH_TCP + 3] << 8) + query[HEADER_LENGTH_TCP + 4])
-                            == UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL) {
-                                /* Change the number of values (offset
-                                   TCP = 6) */
-                                query[HEADER_LENGTH_TCP + 3] = 0;
-                                query[HEADER_LENGTH_TCP + 4] = UT_HOLDING_REGISTERS_NB_POINTS;
-                        }
+        rc = modbus_slave_receive(&mb_param, -1, query);
+        if (rc > 0) {
+            if (((query[HEADER_LENGTH_TCP + 3] << 8) + query[HEADER_LENGTH_TCP + 4])
+                == UT_HOLDING_REGISTERS_NB_POINTS_SPECIAL) {
+                /* Change the number of values (offset
+                   TCP = 6) */
+                query[HEADER_LENGTH_TCP + 3] = 0;
+                query[HEADER_LENGTH_TCP + 4] = UT_HOLDING_REGISTERS_NB_POINTS;
+            }
 
-                        rc = modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
-                        if (rc == -1) {
-                                return -1;
-                        }
-                } else {
-                        /* Connection closed by the client or error */
-                        break;
-                }
+            rc = modbus_slave_manage(&mb_param, query, rc, &mb_mapping);
+            if (rc == -1) {
+                return -1;
+            }
+        } else {
+            /* Connection closed by the client or error */
+            break;
         }
+    }
 
-        printf("Quit the loop: %s\n", modbus_strerror(errno));
+    printf("Quit the loop: %s\n", modbus_strerror(errno));
 
-        close(socket);
-        modbus_mapping_free(&mb_mapping);
-        modbus_close(&mb_param);
+    close(socket);
+    modbus_mapping_free(&mb_mapping);
+    modbus_close(&mb_param);
 
-        return 0;
+    return 0;
 }

+ 7 - 7
tests/version.c

@@ -20,12 +20,12 @@
 
 int main(void)
 {
-        printf("Compiled with libmodbus version %s\n", LIBMODBUS_VERSION_STRING);
-        printf("Linked with libmodbus version %d.%d.%d\n",
-               libmodbus_version_major, libmodbus_version_minor, libmodbus_version_micro);
+    printf("Compiled with libmodbus version %s\n", LIBMODBUS_VERSION_STRING);
+    printf("Linked with libmodbus version %d.%d.%d\n",
+           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.\n");
-        }
-        return 0;
+    if (LIBMODBUS_VERSION_CHECK(2, 1, 0)) {
+        printf("The functions to read/write float values are available.\n");
+    }
+    return 0;
 }

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