Remove useless configure checks
[libmodbus.git] / tests / unit-test-server.c
blobbb25ba43b03e20418d431400562a0dabefb03442
1 /*
2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
7 #include <errno.h>
8 #include <modbus.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 // clang-format off
15 #ifdef _WIN32
16 # include <winsock2.h>
17 #else
18 # include <sys/socket.h>
19 #endif
21 /* For MinGW */
22 #ifndef MSG_NOSIGNAL
23 # define MSG_NOSIGNAL 0
24 #endif
25 // clang-format on
27 #include "unit-test.h"
29 enum {
30 TCP,
31 TCP_PI,
32 RTU
35 int main(int argc, char *argv[])
37 int s = -1;
38 modbus_t *ctx;
39 modbus_mapping_t *mb_mapping;
40 int rc;
41 int i;
42 int use_backend;
43 uint8_t *query;
44 int header_length;
45 char *ip_or_device;
47 if (argc > 1) {
48 if (strcmp(argv[1], "tcp") == 0) {
49 use_backend = TCP;
50 } else if (strcmp(argv[1], "tcppi") == 0) {
51 use_backend = TCP_PI;
52 } else if (strcmp(argv[1], "rtu") == 0) {
53 use_backend = RTU;
54 } else {
55 printf("Modbus server for unit testing.\n");
56 printf("Usage:\n %s [tcp|tcppi|rtu] [<ip or device>]\n", argv[0]);
57 printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB0\n\n");
58 return -1;
60 } else {
61 /* By default */
62 use_backend = TCP;
65 if (argc > 2) {
66 ip_or_device = argv[2];
67 } else {
68 switch (use_backend) {
69 case TCP:
70 ip_or_device = "127.0.0.1";
71 break;
72 case TCP_PI:
73 ip_or_device = "::1";
74 break;
75 case RTU:
76 ip_or_device = "/dev/ttyUSB0";
77 break;
78 default:
79 break;
83 if (use_backend == TCP) {
84 ctx = modbus_new_tcp(ip_or_device, 1502);
85 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
86 } else if (use_backend == TCP_PI) {
87 ctx = modbus_new_tcp_pi(ip_or_device, "1502");
88 query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
89 } else {
90 ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1);
91 modbus_set_slave(ctx, SERVER_ID);
92 query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
95 header_length = modbus_get_header_length(ctx);
97 modbus_set_debug(ctx, TRUE);
99 mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS,
100 UT_BITS_NB,
101 UT_INPUT_BITS_ADDRESS,
102 UT_INPUT_BITS_NB,
103 UT_REGISTERS_ADDRESS,
104 UT_REGISTERS_NB_MAX,
105 UT_INPUT_REGISTERS_ADDRESS,
106 UT_INPUT_REGISTERS_NB);
107 if (mb_mapping == NULL) {
108 fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
109 modbus_free(ctx);
110 return -1;
113 /* Examples from PI_MODBUS_300.pdf.
114 Only the read-only input values are assigned. */
116 /* Initialize input values that's can be only done server side. */
117 modbus_set_bits_from_bytes(
118 mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB);
120 /* Initialize values of INPUT REGISTERS */
121 for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
122 mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];
125 if (use_backend == TCP) {
126 s = modbus_tcp_listen(ctx, 1);
127 modbus_tcp_accept(ctx, &s);
128 } else if (use_backend == TCP_PI) {
129 s = modbus_tcp_pi_listen(ctx, 1);
130 modbus_tcp_pi_accept(ctx, &s);
131 } else {
132 rc = modbus_connect(ctx);
133 if (rc == -1) {
134 fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
135 modbus_free(ctx);
136 return -1;
140 for (;;) {
141 do {
142 rc = modbus_receive(ctx, query);
143 /* Filtered queries return 0 */
144 } while (rc == 0);
146 /* The connection is not closed on errors which require on reply such as
147 bad CRC in RTU. */
148 if (rc == -1 && errno != EMBBADCRC) {
149 /* Quit */
150 break;
153 uint8_t function = query[header_length];
154 uint16_t address = MODBUS_GET_INT16_FROM_INT8(query, header_length + 1);
156 /** Special server behavior to test client **/
157 if (function == MODBUS_FC_READ_HOLDING_REGISTERS) {
158 if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) ==
159 UT_REGISTERS_NB_SPECIAL) {
160 printf("Set an incorrect number of values\n");
161 MODBUS_SET_INT16_TO_INT8(
162 query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
163 } else if (address == UT_REGISTERS_ADDRESS_SPECIAL) {
164 printf("Reply to this special register address by an exception\n");
165 modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
166 continue;
167 } else if (address == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
168 const int RAW_REQ_LENGTH = 5;
169 uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
170 0x03,
171 0x02,
172 0x00,
173 0x00};
175 printf("Reply with an invalid TID or slave\n");
176 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
177 continue;
178 } else if (address == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
179 printf("Sleep 0.5 s before replying\n");
180 usleep(500000);
181 } else if (address == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
182 /* Test low level only available in TCP mode */
183 /* Catch the reply and send reply byte a byte */
184 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
185 int req_length = 11;
186 int w_s = modbus_get_socket(ctx);
187 if (w_s == -1) {
188 fprintf(stderr, "Unable to get a valid socket in special test\n");
189 continue;
192 /* Copy TID */
193 req[1] = query[1];
194 for (i = 0; i < req_length; i++) {
195 printf("(%.2X)", req[i]);
196 usleep(5000);
197 rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL);
198 if (rc == -1) {
199 break;
202 continue;
204 } else if (function == MODBUS_FC_WRITE_SINGLE_COIL) {
205 if (address == UT_BITS_ADDRESS_INVALID_REQUEST_LENGTH) {
206 // The valid length is lengths of header + checkum + FC + address + value
207 // (max 12)
208 rc = 34;
209 printf(
210 "Special modbus_write_bit detected. Inject a wrong length value (%d) "
211 "in modbus_reply\n",
212 rc);
214 } else if (function == MODBUS_FC_WRITE_SINGLE_REGISTER) {
215 if (address == UT_REGISTERS_ADDRESS_SPECIAL) {
216 rc = 45;
217 printf("Special modbus_write_register detected. Inject a wrong length "
218 "value (%d) in modbus_reply\n",
219 rc);
223 rc = modbus_reply(ctx, query, rc, mb_mapping);
224 if (rc == -1) {
225 break;
229 printf("Quit the loop: %s\n", modbus_strerror(errno));
231 if (use_backend == TCP) {
232 if (s != -1) {
233 close(s);
236 modbus_mapping_free(mb_mapping);
237 free(query);
238 /* For RTU */
239 modbus_close(ctx);
240 modbus_free(ctx);
242 return 0;