2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
14 #include "unit-test.h"
16 const int EXCEPTION_RC
= 2;
24 int test_server(modbus_t
*ctx
, int use_backend
);
25 int send_crafted_request(modbus_t
*ctx
,
33 int equal_dword(uint16_t *tab_reg
, const uint32_t value
);
34 int is_memory_equal(const void *s1
, const void *s2
, size_t size
);
36 #define BUG_REPORT(_cond, _format, _args...) \
38 "\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args)
40 #define ASSERT_TRUE(_cond, _format, __args...) \
45 BUG_REPORT(_cond, _format, ##__args); \
50 int is_memory_equal(const void *s1
, const void *s2
, size_t size
)
52 return (memcmp(s1
, s2
, size
) == 0);
55 int equal_dword(uint16_t *tab_reg
, const uint32_t value
)
57 return ((tab_reg
[0] == (value
>> 16)) && (tab_reg
[1] == (value
& 0xFFFF)));
60 int main(int argc
, char *argv
[])
62 /* Length of report slave ID response slave ID + ON/OFF + 'LMB' + version */
63 const int NB_REPORT_SLAVE_ID
= 2 + 3 + strlen(LIBMODBUS_VERSION_STRING
);
64 uint8_t *tab_rp_bits
= NULL
;
65 uint16_t *tab_rp_registers
= NULL
;
66 uint16_t *tab_rp_registers_bad
= NULL
;
73 uint32_t old_response_to_sec
;
74 uint32_t old_response_to_usec
;
75 uint32_t new_response_to_sec
;
76 uint32_t new_response_to_usec
;
77 uint32_t old_byte_to_sec
;
78 uint32_t old_byte_to_usec
;
85 if (strcmp(argv
[1], "tcp") == 0) {
87 } else if (strcmp(argv
[1], "tcppi") == 0) {
89 } else if (strcmp(argv
[1], "rtu") == 0) {
92 printf("Modbus client for unit testing\n");
93 printf("Usage:\n %s [tcp|tcppi|rtu]\n", argv
[0]);
94 printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB1\n\n");
103 ip_or_device
= argv
[2];
105 switch (use_backend
) {
107 ip_or_device
= "127.0.0.1";
110 ip_or_device
= "::1";
113 ip_or_device
= "/dev/ttyUSB1";
120 if (use_backend
== TCP
) {
121 ctx
= modbus_new_tcp(ip_or_device
, 1502);
122 } else if (use_backend
== TCP_PI
) {
123 ctx
= modbus_new_tcp_pi(ip_or_device
, "1502");
125 ctx
= modbus_new_rtu(ip_or_device
, 115200, 'N', 8, 1);
128 fprintf(stderr
, "Unable to allocate libmodbus context\n");
132 modbus_set_debug(ctx
, TRUE
);
133 modbus_set_error_recovery(
134 ctx
, MODBUS_ERROR_RECOVERY_LINK
| MODBUS_ERROR_RECOVERY_PROTOCOL
);
136 if (use_backend
== RTU
) {
137 modbus_set_slave(ctx
, SERVER_ID
);
140 modbus_get_response_timeout(ctx
, &old_response_to_sec
, &old_response_to_usec
);
141 if (modbus_connect(ctx
) == -1) {
142 fprintf(stderr
, "Connection failed: %s\n", modbus_strerror(errno
));
147 /* Allocate and initialize the memory to store the bits */
148 nb_points
= (UT_BITS_NB
> UT_INPUT_BITS_NB
) ? UT_BITS_NB
: UT_INPUT_BITS_NB
;
149 tab_rp_bits
= (uint8_t *) malloc(nb_points
* sizeof(uint8_t));
150 memset(tab_rp_bits
, 0, nb_points
* sizeof(uint8_t));
152 /* Allocate and initialize the memory to store the registers */
153 nb_points
= (UT_REGISTERS_NB
> UT_INPUT_REGISTERS_NB
) ? UT_REGISTERS_NB
154 : UT_INPUT_REGISTERS_NB
;
155 tab_rp_registers
= (uint16_t *) malloc(nb_points
* sizeof(uint16_t));
156 memset(tab_rp_registers
, 0, nb_points
* sizeof(uint16_t));
158 printf("** UNIT TESTING **\n");
160 printf("1/1 No response timeout modification on connect: ");
161 modbus_get_response_timeout(ctx
, &new_response_to_sec
, &new_response_to_usec
);
162 ASSERT_TRUE(old_response_to_sec
== new_response_to_sec
&&
163 old_response_to_usec
== new_response_to_usec
,
166 printf("\nTEST WRITE/READ:\n");
171 rc
= modbus_write_bit(ctx
, UT_BITS_ADDRESS
, ON
);
172 printf("1/2 modbus_write_bit: ");
173 ASSERT_TRUE(rc
== 1, "");
175 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, 1, tab_rp_bits
);
176 printf("2/2 modbus_read_bits: ");
177 ASSERT_TRUE(rc
== 1, "FAILED (nb points %d)\n", rc
);
178 ASSERT_TRUE(tab_rp_bits
[0] == ON
, "FAILED (%0X != %0X)\n", tab_rp_bits
[0], ON
);
184 uint8_t tab_value
[UT_BITS_NB
];
186 modbus_set_bits_from_bytes(tab_value
, 0, UT_BITS_NB
, UT_BITS_TAB
);
187 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
, UT_BITS_NB
, tab_value
);
188 printf("1/2 modbus_write_bits: ");
189 ASSERT_TRUE(rc
== UT_BITS_NB
, "");
192 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, UT_BITS_NB
, tab_rp_bits
);
193 printf("2/2 modbus_read_bits: ");
194 ASSERT_TRUE(rc
== UT_BITS_NB
, "FAILED (nb points %d)\n", rc
);
197 nb_points
= UT_BITS_NB
;
198 while (nb_points
> 0) {
199 int nb_bits
= (nb_points
> 8) ? 8 : nb_points
;
201 value
= modbus_get_byte_from_bits(tab_rp_bits
, i
* 8, nb_bits
);
203 value
== UT_BITS_TAB
[i
], "FAILED (%0X != %0X)\n", value
, UT_BITS_TAB
[i
]);
205 nb_points
-= nb_bits
;
209 /* End of multiple bits */
211 /** DISCRETE INPUTS **/
213 modbus_read_input_bits(ctx
, UT_INPUT_BITS_ADDRESS
, UT_INPUT_BITS_NB
, tab_rp_bits
);
214 printf("1/1 modbus_read_input_bits: ");
215 ASSERT_TRUE(rc
== UT_INPUT_BITS_NB
, "FAILED (nb points %d)\n", rc
);
218 nb_points
= UT_INPUT_BITS_NB
;
219 while (nb_points
> 0) {
220 int nb_bits
= (nb_points
> 8) ? 8 : nb_points
;
221 value
= modbus_get_byte_from_bits(tab_rp_bits
, i
* 8, nb_bits
);
222 ASSERT_TRUE(value
== UT_INPUT_BITS_TAB
[i
],
223 "FAILED (%0X != %0X)\n",
225 UT_INPUT_BITS_TAB
[i
]);
227 nb_points
-= nb_bits
;
232 /** HOLDING REGISTERS **/
234 /* Single register */
235 rc
= modbus_write_register(ctx
, UT_REGISTERS_ADDRESS
, 0x1234);
236 printf("1/2 modbus_write_register: ");
237 ASSERT_TRUE(rc
== 1, "");
239 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
, 1, tab_rp_registers
);
240 printf("2/2 modbus_read_registers: ");
241 ASSERT_TRUE(rc
== 1, "FAILED (nb points %d)\n", rc
);
242 ASSERT_TRUE(tab_rp_registers
[0] == 0x1234,
243 "FAILED (%0X != %0X)\n",
246 /* End of single register */
249 rc
= modbus_write_registers(
250 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, UT_REGISTERS_TAB
);
251 printf("1/5 modbus_write_registers: ");
252 ASSERT_TRUE(rc
== UT_REGISTERS_NB
, "");
254 rc
= modbus_read_registers(
255 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, tab_rp_registers
);
256 printf("2/5 modbus_read_registers: ");
257 ASSERT_TRUE(rc
== UT_REGISTERS_NB
, "FAILED (nb points %d)\n", rc
);
259 for (i
= 0; i
< UT_REGISTERS_NB
; i
++) {
260 ASSERT_TRUE(tab_rp_registers
[i
] == UT_REGISTERS_TAB
[i
],
261 "FAILED (%0X != %0X)\n",
263 UT_REGISTERS_TAB
[i
]);
266 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
, 0, tab_rp_registers
);
267 printf("3/5 modbus_read_registers (0): ");
268 ASSERT_TRUE(rc
== -1, "FAILED (nb_points %d)\n", rc
);
270 nb_points
= (UT_REGISTERS_NB
> UT_INPUT_REGISTERS_NB
) ? UT_REGISTERS_NB
271 : UT_INPUT_REGISTERS_NB
;
272 memset(tab_rp_registers
, 0, nb_points
* sizeof(uint16_t));
274 /* Write registers to zero from tab_rp_registers and store read registers
275 into tab_rp_registers. So the read registers must set to 0, except the
276 first one because there is an offset of 1 register on write. */
277 rc
= modbus_write_and_read_registers(ctx
,
278 UT_REGISTERS_ADDRESS
+ 1,
281 UT_REGISTERS_ADDRESS
,
284 printf("4/5 modbus_write_and_read_registers: ");
286 rc
== UT_REGISTERS_NB
, "FAILED (nb points %d != %d)\n", rc
, UT_REGISTERS_NB
);
288 ASSERT_TRUE(tab_rp_registers
[0] == UT_REGISTERS_TAB
[0],
289 "FAILED (%0X != %0X)\n",
291 UT_REGISTERS_TAB
[0]);
293 for (i
= 1; i
< UT_REGISTERS_NB
; i
++) {
295 tab_rp_registers
[i
] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers
[i
], 0);
298 /* End of many registers */
300 /** INPUT REGISTERS **/
301 rc
= modbus_read_input_registers(
302 ctx
, UT_INPUT_REGISTERS_ADDRESS
, UT_INPUT_REGISTERS_NB
, tab_rp_registers
);
303 printf("1/1 modbus_read_input_registers: ");
304 ASSERT_TRUE(rc
== UT_INPUT_REGISTERS_NB
, "FAILED (nb points %d)\n", rc
);
306 for (i
= 0; i
< UT_INPUT_REGISTERS_NB
; i
++) {
307 ASSERT_TRUE(tab_rp_registers
[i
] == UT_INPUT_REGISTERS_TAB
[i
],
308 "FAILED (%0X != %0X)\n",
310 UT_INPUT_REGISTERS_TAB
[i
]);
314 printf("1/1 Write mask: ");
315 rc
= modbus_write_register(ctx
, UT_REGISTERS_ADDRESS
, 0x12);
316 rc
= modbus_mask_write_register(ctx
, UT_REGISTERS_ADDRESS
, 0xF2, 0x25);
317 ASSERT_TRUE(rc
!= -1, "FAILED (%x == -1)\n", rc
);
318 rc
= modbus_read_registers(ctx
, UT_REGISTERS_ADDRESS
, 1, tab_rp_registers
);
320 tab_rp_registers
[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers
[0], 0x17);
322 printf("\nTEST FLOATS\n");
324 printf("1/4 Set/get float ABCD: ");
325 modbus_set_float_abcd(UT_REAL
, tab_rp_registers
);
326 ASSERT_TRUE(is_memory_equal(tab_rp_registers
, UT_IREAL_ABCD_SET
, 4),
327 "FAILED Set float ABCD");
328 real
= modbus_get_float_abcd(UT_IREAL_ABCD_GET
);
329 ASSERT_TRUE(real
== UT_REAL
, "FAILED (%f != %f)\n", real
, UT_REAL
);
331 printf("2/4 Set/get float DCBA: ");
332 modbus_set_float_dcba(UT_REAL
, tab_rp_registers
);
333 ASSERT_TRUE(is_memory_equal(tab_rp_registers
, UT_IREAL_DCBA_SET
, 4),
334 "FAILED Set float DCBA");
335 real
= modbus_get_float_dcba(UT_IREAL_DCBA_GET
);
336 ASSERT_TRUE(real
== UT_REAL
, "FAILED (%f != %f)\n", real
, UT_REAL
);
338 printf("3/4 Set/get float BADC: ");
339 modbus_set_float_badc(UT_REAL
, tab_rp_registers
);
340 ASSERT_TRUE(is_memory_equal(tab_rp_registers
, UT_IREAL_BADC_SET
, 4),
341 "FAILED Set float BADC");
342 real
= modbus_get_float_badc(UT_IREAL_BADC_GET
);
343 ASSERT_TRUE(real
== UT_REAL
, "FAILED (%f != %f)\n", real
, UT_REAL
);
345 printf("4/4 Set/get float CDAB: ");
346 modbus_set_float_cdab(UT_REAL
, tab_rp_registers
);
347 ASSERT_TRUE(is_memory_equal(tab_rp_registers
, UT_IREAL_CDAB_SET
, 4),
348 "FAILED Set float CDAB");
349 real
= modbus_get_float_cdab(UT_IREAL_CDAB_GET
);
350 ASSERT_TRUE(real
== UT_REAL
, "FAILED (%f != %f)\n", real
, UT_REAL
);
352 printf("\nAt this point, error messages doesn't mean the test has failed\n");
354 /** ILLEGAL DATA ADDRESS **/
355 printf("\nTEST ILLEGAL DATA ADDRESS:\n");
357 /* The mapping begins at the defined addresses and ends at address +
358 * nb_points so these addresses are not valid. */
360 rc
= modbus_read_bits(ctx
, 0, 1, tab_rp_bits
);
361 printf("* modbus_read_bits (0): ");
362 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
364 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, UT_BITS_NB
+ 1, tab_rp_bits
);
365 printf("* modbus_read_bits (max): ");
366 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
368 rc
= modbus_read_input_bits(ctx
, 0, 1, tab_rp_bits
);
369 printf("* modbus_read_input_bits (0): ");
370 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
372 rc
= modbus_read_input_bits(
373 ctx
, UT_INPUT_BITS_ADDRESS
, UT_INPUT_BITS_NB
+ 1, tab_rp_bits
);
374 printf("* modbus_read_input_bits (max): ");
375 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
377 rc
= modbus_read_registers(ctx
, 0, 1, tab_rp_registers
);
378 printf("* modbus_read_registers (0): ");
379 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
381 rc
= modbus_read_registers(
382 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB_MAX
+ 1, tab_rp_registers
);
383 printf("* modbus_read_registers (max): ");
384 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
386 rc
= modbus_read_input_registers(ctx
, 0, 1, tab_rp_registers
);
387 printf("* modbus_read_input_registers (0): ");
388 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
390 rc
= modbus_read_input_registers(
391 ctx
, UT_INPUT_REGISTERS_ADDRESS
, UT_INPUT_REGISTERS_NB
+ 1, tab_rp_registers
);
392 printf("* modbus_read_input_registers (max): ");
393 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
395 rc
= modbus_write_bit(ctx
, 0, ON
);
396 printf("* modbus_write_bit (0): ");
397 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
399 rc
= modbus_write_bit(ctx
, UT_BITS_ADDRESS
+ UT_BITS_NB
, ON
);
400 printf("* modbus_write_bit (max): ");
401 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
403 rc
= modbus_write_bits(ctx
, 0, 1, tab_rp_bits
);
404 printf("* modbus_write_coils (0): ");
405 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
407 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
+ UT_BITS_NB
, UT_BITS_NB
, tab_rp_bits
);
408 printf("* modbus_write_coils (max): ");
409 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
411 rc
= modbus_write_register(ctx
, 0, tab_rp_registers
[0]);
412 printf("* modbus_write_register (0): ");
413 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
415 rc
= modbus_write_register(
416 ctx
, UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB_MAX
, tab_rp_registers
[0]);
417 printf("* modbus_write_register (max): ");
418 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
420 rc
= modbus_write_registers(ctx
, 0, 1, tab_rp_registers
);
421 printf("* modbus_write_registers (0): ");
422 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
424 rc
= modbus_write_registers(ctx
,
425 UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB_MAX
,
428 printf("* modbus_write_registers (max): ");
429 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
431 rc
= modbus_mask_write_register(ctx
, 0, 0xF2, 0x25);
432 printf("* modbus_mask_write_registers (0): ");
433 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
435 rc
= modbus_mask_write_register(
436 ctx
, UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB_MAX
, 0xF2, 0x25);
437 printf("* modbus_mask_write_registers (max): ");
438 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
440 rc
= modbus_write_and_read_registers(
441 ctx
, 0, 1, tab_rp_registers
, 0, 1, tab_rp_registers
);
442 printf("* modbus_write_and_read_registers (0): ");
443 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
445 rc
= modbus_write_and_read_registers(ctx
,
446 UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB_MAX
,
449 UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB_MAX
,
452 printf("* modbus_write_and_read_registers (max): ");
453 ASSERT_TRUE(rc
== -1 && errno
== EMBXILADD
, "");
455 /** TOO MANY DATA **/
456 printf("\nTEST TOO MANY DATA ERROR:\n");
458 rc
= modbus_read_bits(ctx
, UT_BITS_ADDRESS
, MODBUS_MAX_READ_BITS
+ 1, tab_rp_bits
);
459 printf("* modbus_read_bits: ");
460 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
462 rc
= modbus_read_input_bits(
463 ctx
, UT_INPUT_BITS_ADDRESS
, MODBUS_MAX_READ_BITS
+ 1, tab_rp_bits
);
464 printf("* modbus_read_input_bits: ");
465 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
467 rc
= modbus_read_registers(
468 ctx
, UT_REGISTERS_ADDRESS
, MODBUS_MAX_READ_REGISTERS
+ 1, tab_rp_registers
);
469 printf("* modbus_read_registers: ");
470 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
472 rc
= modbus_read_input_registers(
473 ctx
, UT_INPUT_REGISTERS_ADDRESS
, MODBUS_MAX_READ_REGISTERS
+ 1, tab_rp_registers
);
474 printf("* modbus_read_input_registers: ");
475 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
477 rc
= modbus_write_bits(ctx
, UT_BITS_ADDRESS
, MODBUS_MAX_WRITE_BITS
+ 1, tab_rp_bits
);
478 printf("* modbus_write_bits: ");
479 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
481 rc
= modbus_write_registers(
482 ctx
, UT_REGISTERS_ADDRESS
, MODBUS_MAX_WRITE_REGISTERS
+ 1, tab_rp_registers
);
483 printf("* modbus_write_registers: ");
484 ASSERT_TRUE(rc
== -1 && errno
== EMBMDATA
, "");
486 /** SLAVE ADDRESS **/
487 old_slave
= modbus_get_slave(ctx
);
489 printf("\nTEST SLAVE ADDRESS:\n");
491 printf("1/2 Not compliant slave address is refused: ");
492 rc
= modbus_set_slave(ctx
, 248);
493 ASSERT_TRUE(rc
== -1, "Slave address of 248 shouldn't be allowed");
495 printf("2/2 Not compliant slave address is allowed: ");
496 modbus_enable_quirks(ctx
, MODBUS_QUIRK_MAX_SLAVE
);
497 rc
= modbus_set_slave(ctx
, 248);
498 ASSERT_TRUE(rc
== 0, "Not compliant slave address should have been accepted");
500 modbus_disable_quirks(ctx
, MODBUS_QUIRK_MAX_SLAVE
);
501 rc
= modbus_set_slave(ctx
, old_slave
);
502 ASSERT_TRUE(rc
== 0, "Uanble to restore slave value")
506 printf("\nTEST SLAVE REPLY:\n");
507 modbus_set_slave(ctx
, INVALID_SERVER_ID
);
508 rc
= modbus_read_registers(
509 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, tab_rp_registers
);
510 if (use_backend
== RTU
) {
511 const int RAW_REQ_LENGTH
= 6;
512 uint8_t raw_req
[] = {INVALID_SERVER_ID
, 0x03, 0x00, 0x01, 0x01, 0x01};
513 /* Too many points */
514 uint8_t raw_invalid_req
[] = {INVALID_SERVER_ID
, 0x03, 0x00, 0x01, 0xFF, 0xFF};
515 const int RAW_RSP_LENGTH
= 7;
516 uint8_t raw_rsp
[] = {INVALID_SERVER_ID
, 0x03, 0x04, 0, 0, 0, 0};
517 uint8_t rsp
[MODBUS_RTU_MAX_ADU_LENGTH
];
519 /* No response in RTU mode */
520 printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID
);
521 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
523 /* The slave raises a timeout on a confirmation to ignore because if an
524 * indication for another slave is received, a confirmation must follow */
526 /* Send a pair of indication/confirmation to the slave with a different
527 * slave ID to simulate a communication on a RS485 bus. At first, the
528 * slave will see the indication message then the confirmation, and it must
530 modbus_send_raw_request(ctx
, raw_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
531 modbus_send_raw_request(ctx
, raw_rsp
, RAW_RSP_LENGTH
* sizeof(uint8_t));
532 rc
= modbus_receive_confirmation(ctx
, rsp
);
534 printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
536 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
538 /* Send an INVALID request for another slave */
539 modbus_send_raw_request(ctx
, raw_invalid_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
540 rc
= modbus_receive_confirmation(ctx
, rsp
);
542 printf("1-C/3 No response from slave %d with invalid request: ",
544 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
546 rc
= modbus_set_slave(ctx
, MODBUS_BROADCAST_ADDRESS
);
547 ASSERT_TRUE(rc
== 0, "Invalid broadcast address");
549 rc
= modbus_read_registers(
550 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, tab_rp_registers
);
551 printf("2/3 No reply after a broadcast query: ");
552 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
554 /* Response in TCP mode */
555 printf("1/3 Response from slave %d: ", INVALID_SERVER_ID
);
556 ASSERT_TRUE(rc
== UT_REGISTERS_NB
, "");
558 rc
= modbus_set_slave(ctx
, MODBUS_BROADCAST_ADDRESS
);
559 ASSERT_TRUE(rc
== 0, "Invalid broacast address");
561 rc
= modbus_read_registers(
562 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, tab_rp_registers
);
563 printf("2/3 Reply after a query with unit id == 0: ");
564 ASSERT_TRUE(rc
== UT_REGISTERS_NB
, "");
568 modbus_set_slave(ctx
, old_slave
);
570 printf("3/3 Response with an invalid TID or slave: ");
571 rc
= modbus_read_registers(
572 ctx
, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE
, 1, tab_rp_registers
);
573 ASSERT_TRUE(rc
== -1, "");
575 printf("1/2 Report slave ID truncated: \n");
576 /* Set a marker to ensure limit is respected */
577 tab_rp_bits
[NB_REPORT_SLAVE_ID
- 1] = 42;
578 rc
= modbus_report_slave_id(ctx
, NB_REPORT_SLAVE_ID
- 1, tab_rp_bits
);
579 /* Return the size required (response size) but respects the defined limit */
580 ASSERT_TRUE(rc
== NB_REPORT_SLAVE_ID
&& tab_rp_bits
[NB_REPORT_SLAVE_ID
- 1] == 42,
581 "Return is rc %d (%d) and marker is %d (42)",
584 tab_rp_bits
[NB_REPORT_SLAVE_ID
- 1]);
586 printf("2/2 Report slave ID: \n");
587 /* tab_rp_bits is used to store bytes */
588 rc
= modbus_report_slave_id(ctx
, NB_REPORT_SLAVE_ID
, tab_rp_bits
);
589 ASSERT_TRUE(rc
== NB_REPORT_SLAVE_ID
, "");
591 /* Slave ID is an arbitrary number for libmodbus */
592 ASSERT_TRUE(rc
> 0, "");
594 /* Run status indicator is ON */
595 ASSERT_TRUE(rc
> 1 && tab_rp_bits
[1] == 0xFF, "");
597 /* Print additional data as string */
599 printf("Additional data: ");
600 for (i
= 2; i
< rc
; i
++) {
601 printf("%c", tab_rp_bits
[i
]);
606 /* Save original timeout */
607 modbus_get_response_timeout(ctx
, &old_response_to_sec
, &old_response_to_usec
);
608 modbus_get_byte_timeout(ctx
, &old_byte_to_sec
, &old_byte_to_usec
);
610 rc
= modbus_set_response_timeout(ctx
, 0, 0);
611 printf("1/8 Invalid response timeout (zero): ");
612 ASSERT_TRUE(rc
== -1 && errno
== EINVAL
, "");
614 rc
= modbus_set_response_timeout(ctx
, 0, 1000000);
615 printf("2/8 Invalid response timeout (too large us): ");
616 ASSERT_TRUE(rc
== -1 && errno
== EINVAL
, "");
618 rc
= modbus_set_byte_timeout(ctx
, 0, 1000000);
619 printf("3/8 Invalid byte timeout (too large us): ");
620 ASSERT_TRUE(rc
== -1 && errno
== EINVAL
, "");
622 modbus_set_response_timeout(ctx
, 0, 1);
623 rc
= modbus_read_registers(
624 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB
, tab_rp_registers
);
625 printf("4/8 1us response timeout: ");
626 if (rc
== -1 && errno
== ETIMEDOUT
) {
629 printf("FAILED (can fail on some platforms)\n");
632 /* A wait and flush operation is done by the error recovery code of
633 * libmodbus but after a sleep of current response timeout
634 * so 0 can be too short!
636 usleep(old_response_to_sec
* 1000000 + old_response_to_usec
);
639 /* Trigger a special behaviour on server to wait for 0.5 second before
640 * replying whereas allowed timeout is 0.2 second */
641 modbus_set_response_timeout(ctx
, 0, 200000);
642 rc
= modbus_read_registers(
643 ctx
, UT_REGISTERS_ADDRESS_SLEEP_500_MS
, 1, tab_rp_registers
);
644 printf("5/8 Too short response timeout (0.2s < 0.5s): ");
645 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
647 /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
651 modbus_set_response_timeout(ctx
, 0, 600000);
652 rc
= modbus_read_registers(
653 ctx
, UT_REGISTERS_ADDRESS_SLEEP_500_MS
, 1, tab_rp_registers
);
654 printf("6/8 Adequate response timeout (0.6s > 0.5s): ");
655 ASSERT_TRUE(rc
== 1, "");
657 /* Disable the byte timeout.
658 The full response must be available in the 600ms interval */
659 modbus_set_byte_timeout(ctx
, 0, 0);
660 rc
= modbus_read_registers(
661 ctx
, UT_REGISTERS_ADDRESS_SLEEP_500_MS
, 1, tab_rp_registers
);
662 printf("7/8 Disable byte timeout: ");
663 ASSERT_TRUE(rc
== 1, "");
665 // Invalid in TCP or RTU mode...
666 modbus_t
*invalid_ctx
= modbus_new_tcp("1.2.3.4", 1502);
667 modbus_set_response_timeout(ctx
, 0, 1);
668 rc
= modbus_connect(invalid_ctx
);
669 printf("8/8 Connection timeout: ");
670 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
671 modbus_free(invalid_ctx
);
673 /* Restore original response timeout */
674 modbus_set_response_timeout(ctx
, old_response_to_sec
, old_response_to_usec
);
676 if (use_backend
== TCP
) {
677 /* The test server is only able to test byte timeouts with the TCP
680 /* Timeout of 3ms between bytes */
681 modbus_set_byte_timeout(ctx
, 0, 3000);
682 rc
= modbus_read_registers(
683 ctx
, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS
, 1, tab_rp_registers
);
684 printf("1/2 Too small byte timeout (3ms < 5ms): ");
685 ASSERT_TRUE(rc
== -1 && errno
== ETIMEDOUT
, "");
687 /* Wait remaining bytes before flushing */
691 /* Timeout of 7ms between bytes */
692 modbus_set_byte_timeout(ctx
, 0, 7000);
693 rc
= modbus_read_registers(
694 ctx
, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS
, 1, tab_rp_registers
);
695 printf("2/2 Adapted byte timeout (7ms > 5ms): ");
696 ASSERT_TRUE(rc
== 1, "");
699 /* Restore original byte timeout */
700 modbus_set_byte_timeout(ctx
, old_byte_to_sec
, old_byte_to_usec
);
703 printf("\nTEST BAD RESPONSE ERROR:\n");
705 /* Allocate only the required space */
706 tab_rp_registers_bad
=
707 (uint16_t *) malloc(UT_REGISTERS_NB_SPECIAL
* sizeof(uint16_t));
709 rc
= modbus_read_registers(
710 ctx
, UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB_SPECIAL
, tab_rp_registers_bad
);
711 printf("* modbus_read_registers: ");
712 ASSERT_TRUE(rc
== -1 && errno
== EMBBADDATA
, "");
713 free(tab_rp_registers_bad
);
715 /** MANUAL EXCEPTION **/
716 printf("\nTEST MANUAL EXCEPTION:\n");
717 rc
= modbus_read_registers(
718 ctx
, UT_REGISTERS_ADDRESS_SPECIAL
, UT_REGISTERS_NB
, tab_rp_registers
);
720 printf("* modbus_read_registers at special address: ");
721 ASSERT_TRUE(rc
== -1 && errno
== EMBXSBUSY
, "");
723 /** Run a few tests to challenge the server code **/
724 if (test_server(ctx
, use_backend
) == -1) {
732 /* Test init functions */
733 printf("\nTEST INVALID INITIALIZATION:\n");
734 ctx
= modbus_new_rtu(NULL
, 1, 'A', 0, 0);
735 ASSERT_TRUE(ctx
== NULL
&& errno
== EINVAL
, "");
737 ctx
= modbus_new_rtu("/dev/dummy", 0, 'A', 0, 0);
738 ASSERT_TRUE(ctx
== NULL
&& errno
== EINVAL
, "");
740 printf("\nALL TESTS PASS WITH SUCCESS.\n");
744 /* Free the memory */
746 free(tab_rp_registers
);
748 /* Close the connection */
752 return (success
) ? 0 : -1;
755 /* Send crafted requests to test server resilience
756 and ensure proper exceptions are returned. */
757 int test_server(modbus_t
*ctx
, int use_backend
)
762 const int READ_RAW_REQ_LEN
= 6;
763 const int slave
= (use_backend
== RTU
) ? SERVER_ID
: MODBUS_TCP_SLAVE
;
764 uint8_t read_raw_req
[] = {slave
,
765 /* function, address, 5 values */
766 MODBUS_FC_READ_HOLDING_REGISTERS
,
767 UT_REGISTERS_ADDRESS
>> 8,
768 UT_REGISTERS_ADDRESS
& 0xFF,
771 /* Write and read registers request */
772 const int RW_RAW_REQ_LEN
= 13;
773 uint8_t rw_raw_req
[] = {slave
,
774 /* function, addr to read, nb to read */
775 MODBUS_FC_WRITE_AND_READ_REGISTERS
,
777 UT_REGISTERS_ADDRESS
>> 8,
778 UT_REGISTERS_ADDRESS
& 0xFF,
779 (MODBUS_MAX_WR_READ_REGISTERS
+ 1) >> 8,
780 (MODBUS_MAX_WR_READ_REGISTERS
+ 1) & 0xFF,
786 /* Write byte count */
788 /* One data to write... */
791 const int WRITE_RAW_REQ_LEN
= 13;
792 uint8_t write_raw_req
[] = {slave
,
793 /* function will be set in the loop */
794 MODBUS_FC_WRITE_MULTIPLE_REGISTERS
,
796 UT_REGISTERS_ADDRESS
>> 8,
797 UT_REGISTERS_ADDRESS
& 0xFF,
798 /* 3 values, 6 bytes */
802 /* Dummy data to write */
809 const int INVALID_FC
= 0x42;
810 const int INVALID_FC_REQ_LEN
= 6;
811 uint8_t invalid_fc_raw_req
[] = {slave
, 0x42, 0x00, 0x00, 0x00, 0x00};
814 uint8_t rsp
[MODBUS_TCP_MAX_ADU_LENGTH
];
815 int tab_read_function
[] = {MODBUS_FC_READ_COILS
,
816 MODBUS_FC_READ_DISCRETE_INPUTS
,
817 MODBUS_FC_READ_HOLDING_REGISTERS
,
818 MODBUS_FC_READ_INPUT_REGISTERS
};
819 int tab_read_nb_max
[] = {MODBUS_MAX_READ_BITS
+ 1,
820 MODBUS_MAX_READ_BITS
+ 1,
821 MODBUS_MAX_READ_REGISTERS
+ 1,
822 MODBUS_MAX_READ_REGISTERS
+ 1};
826 if (use_backend
== RTU
) {
834 printf("\nTEST RAW REQUESTS:\n");
836 uint32_t old_response_to_sec
;
837 uint32_t old_response_to_usec
;
839 /* This requests can generate flushes server side so we need a higher
840 * response timeout than the server. The server uses the defined response
841 * timeout to sleep before flushing.
842 * The old timeouts are restored at the end.
844 modbus_get_response_timeout(ctx
, &old_response_to_sec
, &old_response_to_usec
);
845 modbus_set_response_timeout(ctx
, 0, 600000);
847 int old_s
= modbus_get_socket(ctx
);
848 modbus_set_socket(ctx
, -1);
849 rc
= modbus_receive(ctx
, rsp
);
850 modbus_set_socket(ctx
, old_s
);
851 printf("* modbus_receive with invalid socket: ");
852 ASSERT_TRUE(rc
== -1, "FAILED (%d)\n", rc
);
854 req_length
= modbus_send_raw_request(ctx
, read_raw_req
, READ_RAW_REQ_LEN
);
855 printf("* modbus_send_raw_request: ");
856 ASSERT_TRUE(req_length
== (backend_length
+ 5), "FAILED (%d)\n", req_length
);
858 printf("* modbus_receive_confirmation: ");
859 rc
= modbus_receive_confirmation(ctx
, rsp
);
860 ASSERT_TRUE(rc
== (backend_length
+ 12), "FAILED (%d)\n", rc
);
862 /* Try to read more values than a response could hold for all data
864 for (i
= 0; i
< 4; i
++) {
865 rc
= send_crafted_request(ctx
,
866 tab_read_function
[i
],
877 rc
= send_crafted_request(ctx
,
878 MODBUS_FC_WRITE_AND_READ_REGISTERS
,
881 MODBUS_MAX_WR_READ_REGISTERS
+ 1,
888 rc
= send_crafted_request(ctx
,
889 MODBUS_FC_WRITE_MULTIPLE_REGISTERS
,
892 MODBUS_MAX_WRITE_REGISTERS
+ 1,
899 rc
= send_crafted_request(ctx
,
900 MODBUS_FC_WRITE_MULTIPLE_COILS
,
903 MODBUS_MAX_WRITE_BITS
+ 1,
910 /* Modbus write multiple registers with large number of values but a set a
911 small number of bytes in requests (not nb * 2 as usual). */
912 rc
= send_crafted_request(ctx
,
913 MODBUS_FC_WRITE_MULTIPLE_REGISTERS
,
916 MODBUS_MAX_WRITE_REGISTERS
,
923 rc
= send_crafted_request(ctx
,
924 MODBUS_FC_WRITE_MULTIPLE_COILS
,
927 MODBUS_MAX_WRITE_BITS
,
934 /* Test invalid function code */
935 modbus_send_raw_request(
936 ctx
, invalid_fc_raw_req
, INVALID_FC_REQ_LEN
* sizeof(uint8_t));
937 rc
= modbus_receive_confirmation(ctx
, rsp
);
938 printf("Return an exception on unknown function code: ");
939 ASSERT_TRUE(rc
== (backend_length
+ EXCEPTION_RC
) &&
940 rsp
[backend_offset
] == (0x80 + INVALID_FC
),
943 modbus_set_response_timeout(ctx
, old_response_to_sec
, old_response_to_usec
);
946 modbus_set_response_timeout(ctx
, old_response_to_sec
, old_response_to_usec
);
950 int send_crafted_request(modbus_t
*ctx
,
959 uint8_t rsp
[MODBUS_TCP_MAX_ADU_LENGTH
];
962 for (j
= 0; j
< 2; j
++) {
967 /* Try to read or write zero values on first iteration */
975 /* Try to read or write max values + 1 on second iteration */
976 req
[4] = (max_value
>> 8) & 0xFF;
977 req
[5] = max_value
& 0xFF;
979 /* Write query (nb values * 2 to convert in bytes for registers) */
984 modbus_send_raw_request(ctx
, req
, req_len
* sizeof(uint8_t));
987 "* try function 0x%X: %s 0 values: ", function
, bytes
? "write" : "read");
989 printf("* try function 0x%X: %s %d values: ",
991 bytes
? "write" : "read",
994 rc
= modbus_receive_confirmation(ctx
, rsp
);
995 ASSERT_TRUE(rc
== (backend_length
+ EXCEPTION_RC
) &&
996 rsp
[backend_offset
] == (0x80 + function
) &&
997 rsp
[backend_offset
+ 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
,