Avoid negative value in FD_SET call
[libmodbus.git] / tests / unit-test-server.c
blobf6e2ebb3e1e3ee9ce9c55fda85fe23ec9d435afa
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <modbus.h>
13 #ifdef _WIN32
14 # include <winsock2.h>
15 #else
16 # include <sys/socket.h>
17 #endif
19 /* For MinGW */
20 #ifndef MSG_NOSIGNAL
21 # define MSG_NOSIGNAL 0
22 #endif
24 #include "unit-test.h"
26 enum {
27 TCP,
28 TCP_PI,
29 RTU
32 int main(int argc, char*argv[])
34 int s = -1;
35 modbus_t *ctx;
36 modbus_mapping_t *mb_mapping;
37 int rc;
38 int i;
39 int use_backend;
40 uint8_t *query;
41 int header_length;
43 if (argc > 1) {
44 if (strcmp(argv[1], "tcp") == 0) {
45 use_backend = TCP;
46 } else if (strcmp(argv[1], "tcppi") == 0) {
47 use_backend = TCP_PI;
48 } else if (strcmp(argv[1], "rtu") == 0) {
49 use_backend = RTU;
50 } else {
51 printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
52 return -1;
54 } else {
55 /* By default */
56 use_backend = TCP;
59 if (use_backend == TCP) {
60 ctx = modbus_new_tcp("127.0.0.1", 1502);
61 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
62 } else if (use_backend == TCP_PI) {
63 ctx = modbus_new_tcp_pi("::0", "1502");
64 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
65 } else {
66 ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
67 modbus_set_slave(ctx, SERVER_ID);
68 query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
70 header_length = modbus_get_header_length(ctx);
72 modbus_set_debug(ctx, TRUE);
74 mb_mapping = modbus_mapping_new_start_address(
75 UT_BITS_ADDRESS, UT_BITS_NB,
76 UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
77 UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX,
78 UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB);
79 if (mb_mapping == NULL) {
80 fprintf(stderr, "Failed to allocate the mapping: %s\n",
81 modbus_strerror(errno));
82 modbus_free(ctx);
83 return -1;
86 /* Examples from PI_MODBUS_300.pdf.
87 Only the read-only input values are assigned. */
89 /* Initialize input values that's can be only done server side. */
90 modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB,
91 UT_INPUT_BITS_TAB);
93 /* Initialize values of INPUT REGISTERS */
94 for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
95 mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];
98 if (use_backend == TCP) {
99 s = modbus_tcp_listen(ctx, 1);
100 modbus_tcp_accept(ctx, &s);
101 } else if (use_backend == TCP_PI) {
102 s = modbus_tcp_pi_listen(ctx, 1);
103 modbus_tcp_pi_accept(ctx, &s);
104 } else {
105 rc = modbus_connect(ctx);
106 if (rc == -1) {
107 fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
108 modbus_free(ctx);
109 return -1;
113 for (;;) {
114 do {
115 rc = modbus_receive(ctx, query);
116 /* Filtered queries return 0 */
117 } while (rc == 0);
119 /* The connection is not closed on errors which require on reply such as
120 bad CRC in RTU. */
121 if (rc == -1 && errno != EMBBADCRC) {
122 /* Quit */
123 break;
126 /* Special server behavior to test client */
127 if (query[header_length] == 0x03) {
128 /* Read holding registers */
130 if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
131 == UT_REGISTERS_NB_SPECIAL) {
132 printf("Set an incorrect number of values\n");
133 MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
134 UT_REGISTERS_NB_SPECIAL - 1);
135 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
136 == UT_REGISTERS_ADDRESS_SPECIAL) {
137 printf("Reply to this special register address by an exception\n");
138 modbus_reply_exception(ctx, query,
139 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
140 continue;
141 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
142 == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
143 const int RAW_REQ_LENGTH = 5;
144 uint8_t raw_req[] = {
145 (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
146 0x03,
147 0x02, 0x00, 0x00
150 printf("Reply with an invalid TID or slave\n");
151 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
152 continue;
153 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
154 == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
155 printf("Sleep 0.5 s before replying\n");
156 usleep(500000);
157 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
158 == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
159 /* Test low level only available in TCP mode */
160 /* Catch the reply and send reply byte a byte */
161 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
162 int req_length = 11;
163 int w_s = modbus_get_socket(ctx);
164 if (w_s == -1) {
165 fprintf(stderr, "Unable to get a valid socket in special test\n");
166 continue;
169 /* Copy TID */
170 req[1] = query[1];
171 for (i=0; i < req_length; i++) {
172 printf("(%.2X)", req[i]);
173 usleep(5000);
174 rc = send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL);
175 if (rc == -1) {
176 break;
179 continue;
183 rc = modbus_reply(ctx, query, rc, mb_mapping);
184 if (rc == -1) {
185 break;
189 printf("Quit the loop: %s\n", modbus_strerror(errno));
191 if (use_backend == TCP) {
192 if (s != -1) {
193 close(s);
196 modbus_mapping_free(mb_mapping);
197 free(query);
198 /* For RTU */
199 modbus_close(ctx);
200 modbus_free(ctx);
202 return 0;