unit-test-server.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <modbus.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. // clang-format off
  13. #ifdef _WIN32
  14. # include <winsock2.h>
  15. #else
  16. # include <sys/socket.h>
  17. #endif
  18. /* For MinGW */
  19. #ifndef MSG_NOSIGNAL
  20. # define MSG_NOSIGNAL 0
  21. #endif
  22. // clang-format on
  23. #include "unit-test.h"
  24. enum {
  25. TCP,
  26. TCP_PI,
  27. RTU
  28. };
  29. int main(int argc, char *argv[])
  30. {
  31. int s = -1;
  32. modbus_t *ctx;
  33. modbus_mapping_t *mb_mapping;
  34. int rc;
  35. int i;
  36. int use_backend;
  37. uint8_t *query;
  38. int header_length;
  39. if (argc > 1) {
  40. if (strcmp(argv[1], "tcp") == 0) {
  41. use_backend = TCP;
  42. } else if (strcmp(argv[1], "tcppi") == 0) {
  43. use_backend = TCP_PI;
  44. } else if (strcmp(argv[1], "rtu") == 0) {
  45. use_backend = RTU;
  46. } else {
  47. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n",
  48. argv[0]);
  49. return -1;
  50. }
  51. } else {
  52. /* By default */
  53. use_backend = TCP;
  54. }
  55. if (use_backend == TCP) {
  56. ctx = modbus_new_tcp("127.0.0.1", 1502);
  57. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  58. } else if (use_backend == TCP_PI) {
  59. ctx = modbus_new_tcp_pi("::0", "1502");
  60. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  61. } else {
  62. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  63. modbus_set_slave(ctx, SERVER_ID);
  64. query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
  65. }
  66. header_length = modbus_get_header_length(ctx);
  67. modbus_set_debug(ctx, TRUE);
  68. mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS,
  69. UT_BITS_NB,
  70. UT_INPUT_BITS_ADDRESS,
  71. UT_INPUT_BITS_NB,
  72. UT_REGISTERS_ADDRESS,
  73. UT_REGISTERS_NB_MAX,
  74. UT_INPUT_REGISTERS_ADDRESS,
  75. UT_INPUT_REGISTERS_NB);
  76. if (mb_mapping == NULL) {
  77. fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
  78. modbus_free(ctx);
  79. return -1;
  80. }
  81. /* Examples from PI_MODBUS_300.pdf.
  82. Only the read-only input values are assigned. */
  83. /* Initialize input values that's can be only done server side. */
  84. modbus_set_bits_from_bytes(
  85. mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB);
  86. /* Initialize values of INPUT REGISTERS */
  87. for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) {
  88. mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];
  89. }
  90. if (use_backend == TCP) {
  91. s = modbus_tcp_listen(ctx, 1);
  92. modbus_tcp_accept(ctx, &s);
  93. } else if (use_backend == TCP_PI) {
  94. s = modbus_tcp_pi_listen(ctx, 1);
  95. modbus_tcp_pi_accept(ctx, &s);
  96. } else {
  97. rc = modbus_connect(ctx);
  98. if (rc == -1) {
  99. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  100. modbus_free(ctx);
  101. return -1;
  102. }
  103. }
  104. for (;;) {
  105. do {
  106. rc = modbus_receive(ctx, query);
  107. /* Filtered queries return 0 */
  108. } while (rc == 0);
  109. /* The connection is not closed on errors which require on reply such as
  110. bad CRC in RTU. */
  111. if (rc == -1 && errno != EMBBADCRC) {
  112. /* Quit */
  113. break;
  114. }
  115. /* Special server behavior to test client */
  116. if (query[header_length] == 0x03) {
  117. /* Read holding registers */
  118. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) ==
  119. UT_REGISTERS_NB_SPECIAL) {
  120. printf("Set an incorrect number of values\n");
  121. MODBUS_SET_INT16_TO_INT8(
  122. query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);
  123. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  124. UT_REGISTERS_ADDRESS_SPECIAL) {
  125. printf("Reply to this special register address by an exception\n");
  126. modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  127. continue;
  128. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  129. UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
  130. const int RAW_REQ_LENGTH = 5;
  131. uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  132. 0x03,
  133. 0x02,
  134. 0x00,
  135. 0x00};
  136. printf("Reply with an invalid TID or slave\n");
  137. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  138. continue;
  139. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  140. UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
  141. printf("Sleep 0.5 s before replying\n");
  142. usleep(500000);
  143. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) ==
  144. UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
  145. /* Test low level only available in TCP mode */
  146. /* Catch the reply and send reply byte a byte */
  147. uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
  148. int req_length = 11;
  149. int w_s = modbus_get_socket(ctx);
  150. if (w_s == -1) {
  151. fprintf(stderr, "Unable to get a valid socket in special test\n");
  152. continue;
  153. }
  154. /* Copy TID */
  155. req[1] = query[1];
  156. for (i = 0; i < req_length; i++) {
  157. printf("(%.2X)", req[i]);
  158. usleep(5000);
  159. rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL);
  160. if (rc == -1) {
  161. break;
  162. }
  163. }
  164. continue;
  165. }
  166. }
  167. rc = modbus_reply(ctx, query, rc, mb_mapping);
  168. if (rc == -1) {
  169. break;
  170. }
  171. }
  172. printf("Quit the loop: %s\n", modbus_strerror(errno));
  173. if (use_backend == TCP) {
  174. if (s != -1) {
  175. close(s);
  176. }
  177. }
  178. modbus_mapping_free(mb_mapping);
  179. free(query);
  180. /* For RTU */
  181. modbus_close(ctx);
  182. modbus_free(ctx);
  183. return 0;
  184. }