2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
16 # include <winsock2.h>
18 # include <sys/socket.h>
23 # define MSG_NOSIGNAL 0
27 #include "unit-test.h"
35 int main(int argc
, char *argv
[])
39 modbus_mapping_t
*mb_mapping
;
48 if (strcmp(argv
[1], "tcp") == 0) {
50 } else if (strcmp(argv
[1], "tcppi") == 0) {
52 } else if (strcmp(argv
[1], "rtu") == 0) {
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");
66 ip_or_device
= argv
[2];
68 switch (use_backend
) {
70 ip_or_device
= "127.0.0.1";
76 ip_or_device
= "/dev/ttyUSB0";
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
);
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
,
101 UT_INPUT_BITS_ADDRESS
,
103 UT_REGISTERS_ADDRESS
,
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
));
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
);
132 rc
= modbus_connect(ctx
);
134 fprintf(stderr
, "Unable to connect %s\n", modbus_strerror(errno
));
142 rc
= modbus_receive(ctx
, query
);
143 /* Filtered queries return 0 */
146 /* The connection is not closed on errors which require on reply such as
148 if (rc
== -1 && errno
!= EMBBADCRC
) {
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
);
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,
175 printf("Reply with an invalid TID or slave\n");
176 modbus_send_raw_request(ctx
, raw_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
178 } else if (address
== UT_REGISTERS_ADDRESS_SLEEP_500_MS
) {
179 printf("Sleep 0.5 s before replying\n");
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";
186 int w_s
= modbus_get_socket(ctx
);
188 fprintf(stderr
, "Unable to get a valid socket in special test\n");
194 for (i
= 0; i
< req_length
; i
++) {
195 printf("(%.2X)", req
[i
]);
197 rc
= send(w_s
, (const char *) (req
+ i
), 1, MSG_NOSIGNAL
);
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
210 "Special modbus_write_bit detected. Inject a wrong length value (%d) "
214 } else if (function
== MODBUS_FC_WRITE_SINGLE_REGISTER
) {
215 if (address
== UT_REGISTERS_ADDRESS_SPECIAL
) {
217 printf("Special modbus_write_register detected. Inject a wrong length "
218 "value (%d) in modbus_reply\n",
223 rc
= modbus_reply(ctx
, query
, rc
, mb_mapping
);
229 printf("Quit the loop: %s\n", modbus_strerror(errno
));
231 if (use_backend
== TCP
) {
236 modbus_mapping_free(mb_mapping
);