Fix address range in random-test-client
[libmodbus.git] / tests / unit-test-server.c
blobebcfdb9c1dbde28ca631aae34865d9d8f4cc7c34
1 /*
2 * Copyright © 2008-2014 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(
75 UT_BITS_ADDRESS + UT_BITS_NB,
76 UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
77 UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
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 /* Unit tests of modbus_mapping_new (tests would not be sufficient if two
87 nb_* were identical) */
88 if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) {
89 printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits);
90 modbus_free(ctx);
91 return -1;
94 if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) {
95 printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB);
96 modbus_free(ctx);
97 return -1;
100 if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) {
101 printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB);
102 modbus_free(ctx);
103 return -1;
106 if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) {
107 printf("Invalid nb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
108 modbus_free(ctx);
109 return -1;
112 /* Examples from PI_MODBUS_300.pdf.
113 Only the read-only input values are assigned. */
115 /** INPUT STATUS **/
116 modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
117 UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
118 UT_INPUT_BITS_TAB);
120 /** INPUT REGISTERS **/
121 for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
122 mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
123 UT_INPUT_REGISTERS_TAB[i];;
126 if (use_backend == TCP) {
127 s = modbus_tcp_listen(ctx, 1);
128 modbus_tcp_accept(ctx, &s);
129 } else if (use_backend == TCP_PI) {
130 s = modbus_tcp_pi_listen(ctx, 1);
131 modbus_tcp_pi_accept(ctx, &s);
132 } else {
133 rc = modbus_connect(ctx);
134 if (rc == -1) {
135 fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
136 modbus_free(ctx);
137 return -1;
141 for (;;) {
142 do {
143 rc = modbus_receive(ctx, query);
144 /* Filtered queries return 0 */
145 } while (rc == 0);
147 /* The connection is not closed on errors which require on reply such as
148 bad CRC in RTU. */
149 if (rc == -1 && errno != EMBBADCRC) {
150 /* Quit */
151 break;
154 /* Special server behavior to test client */
155 if (query[header_length] == 0x03) {
156 /* 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(query, header_length + 3,
162 UT_REGISTERS_NB_SPECIAL - 1);
163 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
164 == UT_REGISTERS_ADDRESS_SPECIAL) {
165 printf("Reply to this special register address by an exception\n");
166 modbus_reply_exception(ctx, query,
167 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
168 continue;
169 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
170 == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
171 const int RAW_REQ_LENGTH = 5;
172 uint8_t raw_req[] = {
173 (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
174 0x03,
175 0x02, 0x00, 0x00
178 printf("Reply with an invalid TID or slave\n");
179 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
180 continue;
181 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
182 == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
183 printf("Sleep 0.5 s before replying\n");
184 usleep(500000);
185 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
186 == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
187 /* Test low level only available in TCP mode */
188 /* Catch the reply and send reply byte a byte */
189 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
190 int req_length = 11;
191 int w_s = modbus_get_socket(ctx);
193 /* Copy TID */
194 req[1] = query[1];
195 for (i=0; i < req_length; i++) {
196 printf("(%.2X)", req[i]);
197 usleep(5000);
198 send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL);
200 continue;
204 rc = modbus_reply(ctx, query, rc, mb_mapping);
205 if (rc == -1) {
206 break;
210 printf("Quit the loop: %s\n", modbus_strerror(errno));
212 if (use_backend == TCP) {
213 if (s != -1) {
214 close(s);
217 modbus_mapping_free(mb_mapping);
218 free(query);
219 /* For RTU */
220 modbus_close(ctx);
221 modbus_free(ctx);
223 return 0;