Debian Codegraph @

tests/unit-test-server.c

raw symbols SCIP
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <errno.h>
8 #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 // clang-format off
15 #ifdef _WIN32
16 # include <winsock2.h>
17 #else
18 # include <sys/socket.h>
19 #endif
20
21 /* For MinGW */
22 #ifndef MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!
23 # define MSG_NOSIGNAL 0
24 #endif
25 // clang-format on
26
27 #include "unit-test.h"cxx . . $ `<file>/tests/unit-test.h`/
28
29 enumcxx . . $ $anonymous_type_337aa4c8df1d59de_0# {
30 TCPcxx . . $ TCP.,
31 TCP_PIcxx . . $ TCP_PI.,
32 RTUcxx . . $ RTU.
33 };
34
35 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
36 {
37 int s = -1;
38 modbus_tcxx . . $ modbus_t# *ctx;
39 modbus_mapping_tcxx . . $ modbus_mapping_t# *mb_mapping;
40 int rc;
41 int i;
42 int use_backend;
43 uint8_tcxx . . $ uint8_t# *query;
44 int header_length;
45 char *ip_or_device;
46
47 if (argc > 1) {
48 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcp") == 0) {
49 use_backend = TCPcxx . . $ TCP.;
50 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcppi") == 0) {
51 use_backend = TCP_PIcxx . . $ TCP_PI.;
52 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "rtu") == 0) {
53 use_backend = RTUcxx . . $ RTU.;
54 } else {
55 printfcxx . . $ printf(581fddd72a8db8fc).("Modbus server for unit testing.\n");
56 printfcxx . . $ printf(581fddd72a8db8fc).("Usage:\n %s [tcp|tcppi|rtu] [<ip or device>]\n", argv[0]);
57 printfcxx . . $ printf(581fddd72a8db8fc).("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB0\n\n");
58 return -1;
59 }
60 } else {
61 /* By default */
62 use_backend = TCPcxx . . $ TCP.;
63 }
64
65 if (argc > 2) {
66 ip_or_device = argv[2];
67 } else {
68 switch (use_backend) {
69 case TCPcxx . . $ TCP.:
70 ip_or_device = "127.0.0.1";
71 break;
72 case TCP_PIcxx . . $ TCP_PI.:
73 ip_or_device = "::1";
74 break;
75 case RTUcxx . . $ RTU.:
76 ip_or_device = "/dev/ttyUSB0";
77 break;
78 default:
79 break;
80 }
81 }
82
83 if (use_backend == TCPcxx . . $ TCP.) {
84 ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).(ip_or_device, 1502);
85 query = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!);
86 } else if (use_backend == TCP_PIcxx . . $ TCP_PI.) {
87 ctx = modbus_new_tcp_picxx . . $ modbus_new_tcp_pi(5f5df5bc2aa40d50).(ip_or_device, "1502");
88 query = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!);
89 } else {
90 ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).(ip_or_device, 115200, 'N', 8, 1);
91 modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, SERVER_IDcxx . . $ `tests/unit-test.h:28:9`!);
92 query = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(MODBUS_RTU_MAX_ADU_LENGTHcxx . . $ `src/modbus-rtu.h:17:9`!);
93 }
94
95 header_length = modbus_get_header_lengthcxx . . $ modbus_get_header_length(c57adc5f48380e39).(ctx);
96
97 modbus_set_debugcxx . . $ modbus_set_debug(2a7e0e283938d456).(ctx, TRUEcxx . . $ `src/modbus.h:51:9`!);
98
99 mb_mapping = modbus_mapping_new_start_addresscxx . . $ modbus_mapping_new_start_address(444dd58f5e23594d).(UT_BITS_ADDRESScxx . . $ UT_BITS_ADDRESS.,
100 UT_BITS_NBcxx . . $ `tests/unit-test.h:31:9`!,
101 UT_INPUT_BITS_ADDRESScxx . . $ UT_INPUT_BITS_ADDRESS.,
102 UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB.,
103 UT_REGISTERS_ADDRESScxx . . $ UT_REGISTERS_ADDRESS.,
104 UT_REGISTERS_NB_MAXcxx . . $ UT_REGISTERS_NB_MAX.,
105 UT_INPUT_REGISTERS_ADDRESScxx . . $ UT_INPUT_REGISTERS_ADDRESS.,
106 UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.);
107 if (mb_mapping == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
108 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Failed to allocate the mapping: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
109 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
110 return -1;
111 }
112
113 /* Examples from PI_MODBUS_300.pdf.
114 Only the read-only input values are assigned. */
115
116 /* Initialize input values that's can be only done server side. */
117 modbus_set_bits_from_bytescxx . . $ modbus_set_bits_from_bytes(a1587c06cb9b3a17).(
118 mb_mapping->tab_input_bitscxx . . $ _modbus_mapping_t#tab_input_bits., 0, UT_INPUT_BITS_NBcxx . . $ UT_INPUT_BITS_NB., UT_INPUT_BITS_TABcxx . . $ UT_INPUT_BITS_TAB.);
119
120 /* Initialize values of INPUT REGISTERS */
121 for (i = 0; i < UT_INPUT_REGISTERS_NBcxx . . $ UT_INPUT_REGISTERS_NB.; i++) {
122 mb_mapping->tab_input_registerscxx . . $ _modbus_mapping_t#tab_input_registers.[i] = UT_INPUT_REGISTERS_TABcxx . . $ UT_INPUT_REGISTERS_TAB.[i];
123 }
124
125 if (use_backend == TCPcxx . . $ TCP.) {
126 s = modbus_tcp_listencxx . . $ modbus_tcp_listen(2a7e0e283938d456).(ctx, 1);
127 modbus_tcp_acceptcxx . . $ modbus_tcp_accept(e415c7f53a3f0b7).(ctx, &s);
128 } else if (use_backend == TCP_PIcxx . . $ TCP_PI.) {
129 s = modbus_tcp_pi_listencxx . . $ modbus_tcp_pi_listen(2a7e0e283938d456).(ctx, 1);
130 modbus_tcp_pi_acceptcxx . . $ modbus_tcp_pi_accept(e415c7f53a3f0b7).(ctx, &s);
131 } else {
132 rc = modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(ctx);
133 if (rc == -1) {
134 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Unable to connect %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
135 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
136 return -1;
137 }
138 }
139
140 for (;;) {
141 do {
142 rc = modbus_receivecxx . . $ modbus_receive(744a031d86f39170).(ctx, query);
143 /* Filtered queries return 0 */
144 } while (rc == 0);
145
146 /* The connection is not closed on errors which require no reply such as
147 bad CRC in RTU. */
148 if (rc == -1) {
149 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EMBBADCRCcxx . . $ `src/modbus.h:146:9`!) {
150 /* Wait for the next request, the query is unusable */
151 continue;
152 }
153 /* Quit */
154 break;
155 }
156
157 uint8_tcxx . . $ uint8_t# function = query[header_length];
158 uint16_tcxx . . $ uint16_t# address = 0;
159
160 /* Only read address when the request carries one (some function
161 codes like FC 0x07/0x11 have no address in the PDU). */
162 if (rc >= header_length + 3)
163 address = MODBUS_GET_INT16_FROM_INT8cxx . . $ `src/modbus.h:304:9`!(query, header_length + 1);
164
165 /** Special server behavior to test client **/
166 if (function == MODBUS_FC_READ_HOLDING_REGISTERScxx . . $ `src/modbus.h:65:9`!) {
167 if (MODBUS_GET_INT16_FROM_INT8cxx . . $ `src/modbus.h:304:9`!(query, header_length + 3) ==
168 UT_REGISTERS_NB_SPECIALcxx . . $ UT_REGISTERS_NB_SPECIAL.) {
169 printfcxx . . $ printf(581fddd72a8db8fc).("Set an incorrect number of values\n");
170 MODBUS_SET_INT16_TO_INT8cxx . . $ `src/modbus.h:307:9`!(
171 query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
172 } else if (address == UT_REGISTERS_ADDRESS_SPECIALcxx . . $ UT_REGISTERS_ADDRESS_SPECIAL.) {
173 printfcxx . . $ printf(581fddd72a8db8fc).("Reply to this special register address by an exception\n");
174 modbus_reply_exceptioncxx . . $ modbus_reply_exception(9fb3248ea093d6a5).(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSYcxx . . $ MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY.);
175 continue;
176 } else if (address == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVEcxx . . $ UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE.) {
177 const int RAW_REQ_LENGTH = 5;
178 uint8_tcxx . . $ uint8_t# raw_req[] = {(use_backend == RTUcxx . . $ RTU.) ? INVALID_SERVER_IDcxx . . $ `tests/unit-test.h:29:9`! : 0xFF,
179 0x03,
180 0x02,
181 0x00,
182 0x00};
183
184 printfcxx . . $ printf(581fddd72a8db8fc).("Reply with an invalid TID or slave\n");
185 modbus_send_raw_requestcxx . . $ modbus_send_raw_request(9fdc62dfbbb43cda).(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_tcxx . . $ uint8_t#));
186 continue;
187 } else if (address == UT_REGISTERS_ADDRESS_SLEEP_500_MScxx . . $ UT_REGISTERS_ADDRESS_SLEEP_500_MS.) {
188 printfcxx . . $ printf(581fddd72a8db8fc).("Sleep 0.5 s before replying\n");
189 usleepcxx . . $ usleep(4c679297bf94b9eb).(500000);
190 } else if (address == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MScxx . . $ UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS.) {
191 /* Test low level only available in TCP mode */
192 /* Catch the reply and send reply byte a byte */
193 uint8_tcxx . . $ uint8_t# req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
194 int req_length = 11;
195 int w_s = modbus_get_socketcxx . . $ modbus_get_socket(c57adc5f48380e39).(ctx);
196 if (w_s == -1) {
197 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Unable to get a valid socket in special test\n");
198 continue;
199 }
200
201 /* Copy TID */
202 req[1] = query[1];
203 for (i = 0; i < req_length; i++) {
204 printfcxx . . $ printf(581fddd72a8db8fc).("(%.2X)", req[i]);
205 usleepcxx . . $ usleep(4c679297bf94b9eb).(5000);
206 rc = sendcxx . . $ send(4e364047dcdcb5ca).(w_s, (const char *) (req + i), 1, MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!);
207 if (rc == -1) {
208 break;
209 }
210 }
211 continue;
212 }
213 } else if (function == MODBUS_FC_WRITE_SINGLE_COILcxx . . $ `src/modbus.h:67:9`!) {
214 if (address == UT_BITS_ADDRESS_INVALID_REQUEST_LENGTHcxx . . $ UT_BITS_ADDRESS_INVALID_REQUEST_LENGTH.) {
215 // The valid length is lengths of header + checkum + FC + address + value
216 // (max 12)
217 rc = 34;
218 printfcxx . . $ printf(581fddd72a8db8fc).(
219 "Special modbus_write_bit detected. Inject a wrong length value (%d) "
220 "in modbus_reply\n",
221 rc);
222 }
223 } else if (function == MODBUS_FC_WRITE_SINGLE_REGISTERcxx . . $ `src/modbus.h:68:9`!) {
224 if (address == UT_REGISTERS_ADDRESS_SPECIALcxx . . $ UT_REGISTERS_ADDRESS_SPECIAL.) {
225 rc = 45;
226 printfcxx . . $ printf(581fddd72a8db8fc).("Special modbus_write_register detected. Inject a wrong length "
227 "value (%d) in modbus_reply\n",
228 rc);
229 }
230 }
231
232 rc = modbus_replycxx . . $ modbus_reply(2bd5663397286dec).(ctx, query, rc, mb_mapping);
233 if (rc == -1) {
234 break;
235 }
236 }
237
238 printfcxx . . $ printf(581fddd72a8db8fc).("Quit the loop: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
239
240 if (use_backend == TCPcxx . . $ TCP.) {
241 if (s != -1) {
242 closecxx . . $ close(7864480464b09eea).(s);
243 }
244 }
245 modbus_mapping_freecxx . . $ modbus_mapping_free(456c0c7ef81b32ba).(mb_mapping);
246 freecxx . . $ free(5d22bdacc48458e8).(query);
247 /* For RTU */
248 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx);
249 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
250
251 return 0;
252 }