Link against socket and nsl libs when building on SunOS
[libmodbus.git] / tests / unit-test-server.c
blob561d64d019aa3fbc7f277f0692cf9929b02c1c75
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 /* Special server behavior to test client */
154 if (query[header_length] == 0x03) {
155 /* Read holding registers */
157 if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) ==
158 UT_REGISTERS_NB_SPECIAL) {
159 printf("Set an incorrect number of values\n");
160 MODBUS_SET_INT16_TO_INT8(
161 query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
162 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
163 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 (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
168 UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
169 const int RAW_REQ_LENGTH = 5;
170 uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
171 0x03,
172 0x02,
173 0x00,
174 0x00};
176 printf("Reply with an invalid TID or slave\n");
177 modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
178 continue;
179 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
180 UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
181 printf("Sleep 0.5 s before replying\n");
182 usleep(500000);
183 } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
184 UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
185 /* Test low level only available in TCP mode */
186 /* Catch the reply and send reply byte a byte */
187 uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
188 int req_length = 11;
189 int w_s = modbus_get_socket(ctx);
190 if (w_s == -1) {
191 fprintf(stderr, "Unable to get a valid socket in special test\n");
192 continue;
195 /* Copy TID */
196 req[1] = query[1];
197 for (i = 0; i < req_length; i++) {
198 printf("(%.2X)", req[i]);
199 usleep(5000);
200 rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL);
201 if (rc == -1) {
202 break;
205 continue;
209 rc = modbus_reply(ctx, query, rc, mb_mapping);
210 if (rc == -1) {
211 break;
215 printf("Quit the loop: %s\n", modbus_strerror(errno));
217 if (use_backend == TCP) {
218 if (s != -1) {
219 close(s);
222 modbus_mapping_free(mb_mapping);
223 free(query);
224 /* For RTU */
225 modbus_close(ctx);
226 modbus_free(ctx);
228 return 0;