Browse Source

Rename modbus_[listen|accept] to modbus_tcp_[listen|accept]

These functions have no meaning in RTU so it's better to specialize
the names and remove them from the backend.

- remove the functions from the backend
- update tests to handle RTU mode (master and slave)
- add command line options to tests (rtu or tcp)
Stéphane Raimbault 14 years ago
parent
commit
b8c0558c19

+ 0 - 2
src/modbus-private.h

@@ -87,8 +87,6 @@ typedef struct _modbus_backend {
     int (*connect) (modbus_t *ctx);
     void (*close) (modbus_t *ctx);
     int (*flush) (modbus_t *ctx);
-    int (*listen) (modbus_t *ctx, int nb_connection);
-    int (*accept) (modbus_t *ctx, int *socket);
     int (*select) (modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length);
     int (*filter_request) (modbus_t *ctx, int slave);
 } modbus_backend_t;

+ 1 - 31
src/modbus-rtu.c

@@ -707,40 +707,12 @@ int _modbus_rtu_flush(modbus_t *ctx)
 #if defined(_WIN32)
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
     ctx_rtu->w_ser.n_bytes = 0;
-    return ( FlushFileBuffers(ctx_rtu->w_ser.fd) == FALSE );
+    return (FlushFileBuffers(ctx_rtu->w_ser.fd) == FALSE);
 #else
     return tcflush(ctx->s, TCIOFLUSH);
 #endif
 }
 
-int _modbus_rtu_listen(modbus_t *ctx, int nb_connection)
-{
-    if (ctx->debug) {
-        fprintf(stderr, "Not implemented");
-    }
-
-    if (ctx->slave == -1) {
-        if (ctx->debug) {
-            fprintf(stderr, "The slave ID is not set (you must call modbus_set_slave() first)\n");
-        }
-        errno = EINVAL;
-        return -1;
-    }
-
-    errno = EINVAL;
-    return -1;
-}
-
-int _modbus_rtu_accept(modbus_t *ctx, int *socket)
-{
-    if (ctx->debug) {
-        fprintf(stderr, "Not implemented");
-    }
-
-    errno = EINVAL;
-    return -1;
-}
-
 int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length)
 {
     int s_rc;
@@ -835,8 +807,6 @@ const modbus_backend_t _modbus_rtu_backend = {
     _modbus_rtu_connect,
     _modbus_rtu_close,
     _modbus_rtu_flush,
-    _modbus_rtu_listen,
-    _modbus_rtu_accept,
     _modbus_rtu_select,
     _modbus_rtu_filter_request
 };

+ 2 - 4
src/modbus-tcp.c

@@ -284,7 +284,7 @@ int _modbus_tcp_flush(modbus_t *ctx)
 }
 
 /* Listens for any request from one or many modbus masters in TCP */
-int _modbus_tcp_listen(modbus_t *ctx, int nb_connection)
+int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
 {
     int new_socket;
     int yes;
@@ -330,7 +330,7 @@ int _modbus_tcp_listen(modbus_t *ctx, int nb_connection)
 /* On success, the function return a non-negative integer that is a descriptor
    for the accepted socket. On error, -1 is returned, and errno is set
    appropriately. */
-int _modbus_tcp_accept(modbus_t *ctx, int *socket)
+int modbus_tcp_accept(modbus_t *ctx, int *socket)
 {
     struct sockaddr_in addr;
     socklen_t addrlen;
@@ -414,8 +414,6 @@ const modbus_backend_t _modbus_tcp_backend = {
     _modbus_tcp_connect,
     _modbus_tcp_close,
     _modbus_tcp_flush,
-    _modbus_tcp_listen,
-    _modbus_tcp_accept,
     _modbus_tcp_select,
     _modbus_tcp_filter_request
 };

+ 2 - 0
src/modbus-tcp.h

@@ -38,5 +38,7 @@
 #define MODBUS_TCP_MAX_ADU_LENGTH  260
 
 modbus_t* modbus_new_tcp(const char *ip_address, int port);
+int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
+int modbus_tcp_accept(modbus_t *ctx, int *socket);
 
 #endif /* _MODBUS_TCP_H_ */

+ 0 - 10
src/modbus.c

@@ -1416,16 +1416,6 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping)
     free(mb_mapping);
 }
 
-int modbus_listen(modbus_t *ctx, int nb_connection)
-{
-    return ctx->backend->listen(ctx, nb_connection);
-}
-
-int modbus_accept(modbus_t *ctx, int *socket)
-{
-    return ctx->backend->accept(ctx, socket);
-}
-
 #ifndef HAVE_STRLCPY
 /*
 /* Function strlcpy was originally developed by

+ 0 - 2
src/modbus.h

@@ -168,8 +168,6 @@ modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status,
                                      int nb_holding_registers, int nb_input_registers);
 void modbus_mapping_free(modbus_mapping_t *mb_mapping);
 
-int modbus_listen(modbus_t *ctx, int nb_connection);
-int modbus_accept(modbus_t *ctx, int *socket);
 int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req);
 int modbus_reply(modbus_t *ctx, const uint8_t *req,
                  int req_length, modbus_mapping_t *mb_mapping);

+ 50 - 24
tests/bandwidth-client.c

@@ -25,9 +25,6 @@
 
 #include <modbus.h>
 
-/* Tests based on PI-MBUS-300 documentation */
-#define NB_LOOPS  100000
-
 #define G_MSEC_PER_SEC 1000
 
 uint32_t gettime_ms(void)
@@ -38,7 +35,13 @@ uint32_t gettime_ms(void)
     return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 
-int main(void)
+enum {
+    TCP,
+    RTU
+};
+
+/* Tests based on PI-MBUS-300 documentation */
+int main(int argc, char *argv[])
 {
     uint8_t *tab_bit;
     uint16_t *tab_reg;
@@ -51,9 +54,32 @@ int main(void)
     uint32_t bytes;
     uint32_t rate;
     int rc;
+    int n_loop;
+    int use_backend;
+
+    if (argc > 1) {
+        if (strcmp(argv[1], "tcp") == 0) {
+            use_backend = TCP;
+            n_loop = 100000;
+        } else if (strcmp(argv[1], "rtu") == 0) {
+            use_backend = RTU;
+            n_loop = 100;
+        } else {
+            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwith\n\n");
+            exit(1);
+        }
+    } else {
+        /* By default */
+        use_backend = TCP;
+        n_loop = 100000;
+    }
 
-    /* TCP */
-    ctx = modbus_new_tcp("127.0.0.1", 1502);
+    if (use_backend == TCP) {
+        ctx = modbus_new_tcp("127.0.0.1", 1502);
+    } else {
+        ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
+        modbus_set_slave(ctx, 1);
+    }
     if (modbus_connect(ctx) == -1) {
         fprintf(stderr, "Connexion failed: %s\n",
                 modbus_strerror(errno));
@@ -73,7 +99,7 @@ int main(void)
 
     nb_points = MODBUS_MAX_READ_BITS;
     start = gettime_ms();
-    for (i=0; i<NB_LOOPS; 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));
@@ -83,15 +109,15 @@ int main(void)
     end = gettime_ms();
     elapsed = end - start;
 
-    rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
+    rate = (n_loop * 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);
+    bytes = n_loop * (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("* %d x %d values\n", n_loop, nb_points);
     printf("* %.3f ms for %d bytes\n", elapsed, bytes);
     printf("* %d KiB/s\n", rate);
     printf("\n");
@@ -99,8 +125,8 @@ int main(void)
     /* 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;
+    printf("* %d x %d bytes\n", n_loop, bytes);
+    bytes = n_loop * 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);
@@ -110,7 +136,7 @@ int main(void)
 
     nb_points = MODBUS_MAX_READ_REGISTERS;
     start = gettime_ms();
-    for (i=0; i<NB_LOOPS; 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));
@@ -120,15 +146,15 @@ int main(void)
     end = gettime_ms();
     elapsed = end - start;
 
-    rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
+    rate = (n_loop * 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);
+    bytes = n_loop * 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("* %d x %d values\n", n_loop, nb_points);
     printf("* %.3f ms for %d bytes\n", elapsed, bytes);
     printf("* %d KiB/s\n", rate);
     printf("\n");
@@ -136,8 +162,8 @@ int main(void)
     /* 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;
+    printf("* %d x %d bytes\n", n_loop, bytes);
+    bytes = n_loop * 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);
@@ -147,7 +173,7 @@ int main(void)
 
     nb_points = MODBUS_MAX_RW_WRITE_REGISTERS;
     start = gettime_ms();
-    for (i=0; i<NB_LOOPS; i++) {
+    for (i=0; i<n_loop; i++) {
         rc = modbus_read_and_write_registers(ctx,
                                              0, nb_points, tab_reg,
                                              0, nb_points, tab_reg);
@@ -159,15 +185,15 @@ int main(void)
     end = gettime_ms();
     elapsed = end - start;
 
-    rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
+    rate = (n_loop * 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);
+    bytes = n_loop * 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("* %d x %d values\n", n_loop, nb_points);
     printf("* %.3f ms for %d bytes\n", elapsed, bytes);
     printf("* %d KiB/s\n", rate);
     printf("\n");
@@ -175,8 +201,8 @@ int main(void)
     /* 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;
+    printf("* %d x %d bytes\n", n_loop, bytes);
+    bytes = n_loop * 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);

+ 1 - 1
tests/bandwidth-server-many-up.c

@@ -66,7 +66,7 @@ int main(void)
         return -1;
     }
 
-    server_socket = modbus_listen(ctx, NB_CONNECTION);
+    server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
 
     signal(SIGINT, close_sigint);
 

+ 32 - 5
tests/bandwidth-server-one.c

@@ -23,14 +23,44 @@
 
 #include <modbus.h>
 
-int main(void)
+enum {
+    TCP,
+    RTU
+};
+
+int main(int argc, char *argv[])
 {
     int socket;
     modbus_t *ctx;
     modbus_mapping_t *mb_mapping;
     int rc;
+    int use_backend;
+
+     /* 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 bandwith\n\n");
+            exit(1);
+        }
+    } else {
+        /* By default */
+        use_backend = TCP;
+    }
+
+    if (use_backend == TCP) {
+        ctx = modbus_new_tcp("127.0.0.1", 1502);
+        socket = modbus_tcp_listen(ctx, 1);
+        modbus_tcp_accept(ctx, &socket);
 
-    ctx = modbus_new_tcp("127.0.0.1", 1502);
+    } else {
+        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
+        modbus_set_slave(ctx, 1);
+        modbus_connect(ctx);
+    }
 
     mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
                                     MODBUS_MAX_READ_REGISTERS, 0);
@@ -41,9 +71,6 @@ int main(void)
         return -1;
     }
 
-    socket = modbus_listen(ctx, 1);
-    modbus_accept(ctx, &socket);
-
     for(;;) {
         uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
 

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

@@ -39,8 +39,8 @@ int main(void)
         return -1;
     }
 
-    socket = modbus_listen(ctx, 1);
-    modbus_accept(ctx, &socket);
+    socket = modbus_tcp_listen(ctx, 1);
+    modbus_tcp_accept(ctx, &socket);
 
     for (;;) {
         uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];

+ 43 - 15
tests/unit-test-client.c

@@ -24,13 +24,17 @@
 
 #include "unit-test.h"
 
-int main(void)
+enum {
+    TCP,
+    RTU
+};
+
+int main(int argc, char *argv[])
 {
     uint8_t *tab_rp_bits;
     uint16_t *tab_rp_registers;
     uint16_t *tab_rp_registers_bad;
     modbus_t *ctx;
-    int is_mode_rtu = FALSE;
     int i;
     uint8_t value;
     int address;
@@ -39,21 +43,37 @@ int main(void)
     float real;
     struct timeval timeout_begin_old;
     struct timeval timeout_begin_new;
+    int use_backend;
 
-    /*
-      ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
-      modbus_set_slave(ctx, SERVER_ID);
-      is_mode_rtu = TRUE;
-    */
+    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 for unit testing\n\n");
+            exit(1);
+        }
+    } else {
+        /* By default */
+        use_backend = TCP;
+    }
 
-    /* TCP */
-    ctx = modbus_new_tcp("127.0.0.1", 1502);
+    if (use_backend == TCP) {
+        ctx = modbus_new_tcp("127.0.0.1", 1502);
+    } else {
+        ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
+    }
     if (ctx == NULL) {
-        fprintf(stderr, "Unable to initialize TCP Modbus\n");
+        fprintf(stderr, "Unable to allocate libmodbus context\n");
         return -1;
     }
     modbus_set_debug(ctx, TRUE);
 
+    if (use_backend == RTU) {
+          modbus_set_slave(ctx, SERVER_ID);
+    }
+
     if (modbus_connect(ctx) == -1) {
         fprintf(stderr, "Connection failed: %s\n",
                 modbus_strerror(errno));
@@ -496,9 +516,10 @@ int main(void)
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
                                UT_REGISTERS_NB_POINTS,
                                tab_rp_registers);
-    printf("1/4 No or response from slave %d: ", 18);
-    if (is_mode_rtu) {
+    if (use_backend == RTU) {
         /* No response in RTU mode */
+        printf("1/4 No response from slave %d: ", 18);
+
         if (rc == -1 && errno == ETIMEDOUT) {
             printf("OK\n");
         } else {
@@ -507,6 +528,8 @@ int main(void)
         }
     } else {
         /* Response in TCP mode */
+        printf("1/4 Response from slave %d: ", 18);
+
         if (rc == UT_REGISTERS_NB_POINTS) {
             printf("OK\n");
         } else {
@@ -515,7 +538,12 @@ int main(void)
         }
     }
 
-    modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
+    rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
+    if (rc == -1) {
+        printf("Invalid broacast address\n");
+        goto close;
+    }
+
     rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
                                UT_REGISTERS_NB_POINTS,
                                tab_rp_registers);
@@ -528,7 +556,7 @@ int main(void)
     }
 
     /* Restore slave */
-    if (is_mode_rtu) {
+    if (use_backend == RTU) {
         modbus_set_slave(ctx, SERVER_ID);
     } else {
         modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
@@ -542,7 +570,7 @@ int main(void)
         goto close;
     }
 
-    if ((is_mode_rtu && tab_rp_bits[0] == SERVER_ID)
+    if (((use_backend == RTU) && (tab_rp_bits[0] == SERVER_ID))
         || tab_rp_bits[0] == 0xFF) {
         printf("OK\n");
     } else {

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

@@ -27,15 +27,41 @@
 /* Copied from modbus-private.h */
 #define HEADER_LENGTH_TCP 7
 
-int main(void)
+enum {
+    TCP,
+    RTU
+};
+
+int main(int argc, char*argv[])
 {
     int socket;
     modbus_t *ctx;
     modbus_mapping_t *mb_mapping;
     int rc;
     int i;
+    int use_backend;
+
+    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 server for unit testing\n\n");
+            return -1;
+        }
+    } else {
+        /* By default */
+        use_backend = TCP;
+    }
+
+    if (use_backend == TCP) {
+        ctx = modbus_new_tcp("127.0.0.1", 1502);
+    } else {
+        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
+        modbus_set_slave(ctx, SERVER_ID);
+    }
 
-    ctx = modbus_new_tcp("127.0.0.1", 1502);
     modbus_set_debug(ctx, TRUE);
     modbus_set_error_recovery(ctx, TRUE);
 
@@ -65,8 +91,17 @@ int main(void)
             UT_INPUT_REGISTERS_TAB[i];;
     }
 
-    socket = modbus_listen(ctx, 1);
-    modbus_accept(ctx, &socket);
+    if (use_backend == TCP) {
+        socket = modbus_tcp_listen(ctx, 1);
+        modbus_tcp_accept(ctx, &socket);
+    } else {
+        rc = modbus_connect(ctx);
+        if (rc == -1) {
+            fprintf(stderr, "Unable to connect\n", modbus_strerror(errno));
+            modbus_free(ctx);
+            return -1;
+        }
+    }
 
     for (;;) {
         uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
@@ -94,7 +129,9 @@ int main(void)
 
     printf("Quit the loop: %s\n", modbus_strerror(errno));
 
-    close(socket);
+    if (use_backend == TCP) {
+        close(socket);
+    }
     modbus_mapping_free(mb_mapping);
     modbus_free(ctx);