unit-test-client.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright © 2008-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <modbus.h>
  12. #include "unit-test.h"
  13. enum {
  14. TCP,
  15. TCP_PI,
  16. RTU
  17. };
  18. int test_server(modbus_t *ctx, int use_backend);
  19. int send_crafted_request(modbus_t *ctx, int function,
  20. uint8_t *req, int req_size,
  21. uint16_t max_value, uint16_t bytes,
  22. int backend_length, int backend_offset);
  23. int equal_dword(uint16_t *tab_reg, const uint32_t value);
  24. #define BUG_REPORT(_cond, _format, _args ...) \
  25. printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args)
  26. #define ASSERT_TRUE(_cond, _format, __args...) { \
  27. if (_cond) { \
  28. printf("OK\n"); \
  29. } else { \
  30. BUG_REPORT(_cond, _format, ## __args); \
  31. goto close; \
  32. } \
  33. };
  34. int equal_dword(uint16_t *tab_reg, const uint32_t value) {
  35. return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF)));
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. const int NB_REPORT_SLAVE_ID = 10;
  40. uint8_t *tab_rp_bits = NULL;
  41. uint16_t *tab_rp_registers = NULL;
  42. uint16_t *tab_rp_registers_bad = NULL;
  43. modbus_t *ctx = NULL;
  44. int i;
  45. uint8_t value;
  46. int nb_points;
  47. int rc;
  48. float real;
  49. uint32_t old_response_to_sec;
  50. uint32_t old_response_to_usec;
  51. uint32_t new_response_to_sec;
  52. uint32_t new_response_to_usec;
  53. uint32_t old_byte_to_sec;
  54. uint32_t old_byte_to_usec;
  55. int use_backend;
  56. int success = FALSE;
  57. if (argc > 1) {
  58. if (strcmp(argv[1], "tcp") == 0) {
  59. use_backend = TCP;
  60. } else if (strcmp(argv[1], "tcppi") == 0) {
  61. use_backend = TCP_PI;
  62. } else if (strcmp(argv[1], "rtu") == 0) {
  63. use_backend = RTU;
  64. } else {
  65. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
  66. exit(1);
  67. }
  68. } else {
  69. /* By default */
  70. use_backend = TCP;
  71. }
  72. if (use_backend == TCP) {
  73. ctx = modbus_new_tcp("127.0.0.1", 1502);
  74. } else if (use_backend == TCP_PI) {
  75. ctx = modbus_new_tcp_pi("::1", "1502");
  76. } else {
  77. ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  78. }
  79. if (ctx == NULL) {
  80. fprintf(stderr, "Unable to allocate libmodbus context\n");
  81. return -1;
  82. }
  83. modbus_set_debug(ctx, TRUE);
  84. modbus_set_error_recovery(ctx,
  85. MODBUS_ERROR_RECOVERY_LINK |
  86. MODBUS_ERROR_RECOVERY_PROTOCOL);
  87. if (use_backend == RTU) {
  88. modbus_set_slave(ctx, SERVER_ID);
  89. }
  90. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  91. if (modbus_connect(ctx) == -1) {
  92. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  93. modbus_free(ctx);
  94. return -1;
  95. }
  96. modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
  97. printf("** UNIT TESTING **\n");
  98. printf("1/1 No response timeout modification on connect: ");
  99. ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
  100. old_response_to_usec == new_response_to_usec, "");
  101. /* Allocate and initialize the memory to store the bits */
  102. nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
  103. tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  104. memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
  105. /* Allocate and initialize the memory to store the registers */
  106. nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
  107. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  108. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  109. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  110. printf("\nTEST WRITE/READ:\n");
  111. /** COIL BITS **/
  112. /* Single */
  113. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
  114. printf("1/2 modbus_write_bit: ");
  115. ASSERT_TRUE(rc == 1, "");
  116. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
  117. printf("2/2 modbus_read_bits: ");
  118. ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  119. ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n",
  120. tab_rp_bits[0], ON);
  121. /* End single */
  122. /* Multiple bits */
  123. {
  124. uint8_t tab_value[UT_BITS_NB];
  125. modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
  126. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_value);
  127. printf("1/2 modbus_write_bits: ");
  128. ASSERT_TRUE(rc == UT_BITS_NB, "");
  129. }
  130. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
  131. printf("2/2 modbus_read_bits: ");
  132. ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc);
  133. i = 0;
  134. nb_points = UT_BITS_NB;
  135. while (nb_points > 0) {
  136. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  137. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  138. ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  139. value, UT_BITS_TAB[i]);
  140. nb_points -= nb_bits;
  141. i++;
  142. }
  143. printf("OK\n");
  144. /* End of multiple bits */
  145. /** DISCRETE INPUTS **/
  146. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  147. UT_INPUT_BITS_NB, tab_rp_bits);
  148. printf("1/1 modbus_read_input_bits: ");
  149. ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
  150. i = 0;
  151. nb_points = UT_INPUT_BITS_NB;
  152. while (nb_points > 0) {
  153. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  154. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  155. ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  156. value, UT_INPUT_BITS_TAB[i]);
  157. nb_points -= nb_bits;
  158. i++;
  159. }
  160. printf("OK\n");
  161. /** HOLDING REGISTERS **/
  162. /* Single register */
  163. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
  164. printf("1/2 modbus_write_register: ");
  165. ASSERT_TRUE(rc == 1, "");
  166. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  167. 1, tab_rp_registers);
  168. printf("2/2 modbus_read_registers: ");
  169. ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  170. ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n",
  171. tab_rp_registers[0], 0x1234);
  172. /* End of single register */
  173. /* Many registers */
  174. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  175. UT_REGISTERS_NB, UT_REGISTERS_TAB);
  176. printf("1/5 modbus_write_registers: ");
  177. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  178. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  179. UT_REGISTERS_NB, tab_rp_registers);
  180. printf("2/5 modbus_read_registers: ");
  181. ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
  182. for (i=0; i < UT_REGISTERS_NB; i++) {
  183. ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
  184. "FAILED (%0X != %0X)\n",
  185. tab_rp_registers[i], UT_REGISTERS_TAB[i]);
  186. }
  187. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  188. 0, tab_rp_registers);
  189. printf("3/5 modbus_read_registers (0): ");
  190. ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
  191. nb_points = (UT_REGISTERS_NB >
  192. UT_INPUT_REGISTERS_NB) ?
  193. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  194. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  195. /* Write registers to zero from tab_rp_registers and store read registers
  196. into tab_rp_registers. So the read registers must set to 0, except the
  197. first one because there is an offset of 1 register on write. */
  198. rc = modbus_write_and_read_registers(ctx,
  199. UT_REGISTERS_ADDRESS + 1,
  200. UT_REGISTERS_NB - 1,
  201. tab_rp_registers,
  202. UT_REGISTERS_ADDRESS,
  203. UT_REGISTERS_NB,
  204. tab_rp_registers);
  205. printf("4/5 modbus_write_and_read_registers: ");
  206. ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n",
  207. rc, UT_REGISTERS_NB);
  208. ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
  209. "FAILED (%0X != %0X)\n",
  210. tab_rp_registers[0], UT_REGISTERS_TAB[0]);
  211. for (i=1; i < UT_REGISTERS_NB; i++) {
  212. ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n",
  213. tab_rp_registers[i], 0);
  214. }
  215. /* End of many registers */
  216. /** INPUT REGISTERS **/
  217. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  218. UT_INPUT_REGISTERS_NB,
  219. tab_rp_registers);
  220. printf("1/1 modbus_read_input_registers: ");
  221. ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
  222. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  223. ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
  224. "FAILED (%0X != %0X)\n",
  225. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  226. }
  227. /* MASKS */
  228. printf("1/1 Write mask: ");
  229. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x12);
  230. rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
  231. ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
  232. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
  233. ASSERT_TRUE(tab_rp_registers[0] == 0x17,
  234. "FAILED (%0X != %0X)\n",
  235. tab_rp_registers[0], 0x17);
  236. printf("\nTEST FLOATS\n");
  237. /** FLOAT **/
  238. printf("1/4 Set/get float ABCD: ");
  239. modbus_set_float_abcd(UT_REAL, tab_rp_registers);
  240. ASSERT_TRUE(equal_dword(tab_rp_registers, UT_IREAL_ABCD), "FAILED Set float ABCD");
  241. real = modbus_get_float_abcd(tab_rp_registers);
  242. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  243. printf("2/4 Set/get float DCBA: ");
  244. modbus_set_float_dcba(UT_REAL, tab_rp_registers);
  245. ASSERT_TRUE(equal_dword(tab_rp_registers, UT_IREAL_DCBA), "FAILED Set float DCBA");
  246. real = modbus_get_float_dcba(tab_rp_registers);
  247. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  248. printf("3/4 Set/get float BADC: ");
  249. modbus_set_float_badc(UT_REAL, tab_rp_registers);
  250. ASSERT_TRUE(equal_dword(tab_rp_registers, UT_IREAL_BADC), "FAILED Set float BADC");
  251. real = modbus_get_float_badc(tab_rp_registers);
  252. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  253. printf("4/4 Set/get float CDAB: ");
  254. modbus_set_float_cdab(UT_REAL, tab_rp_registers);
  255. ASSERT_TRUE(equal_dword(tab_rp_registers, UT_IREAL_CDAB), "FAILED Set float CDAB");
  256. real = modbus_get_float_cdab(tab_rp_registers);
  257. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  258. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  259. /** ILLEGAL DATA ADDRESS **/
  260. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  261. /* The mapping begins at 0 and ends at address + nb_points so
  262. * the addresses are not valid. */
  263. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits);
  264. printf("* modbus_read_bits: ");
  265. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  266. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  267. UT_INPUT_BITS_NB + 1, tab_rp_bits);
  268. printf("* modbus_read_input_bits: ");
  269. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  270. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  271. UT_REGISTERS_NB_MAX + 1, tab_rp_registers);
  272. printf("* modbus_read_registers: ");
  273. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  274. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  275. UT_INPUT_REGISTERS_NB + 1,
  276. tab_rp_registers);
  277. printf("* modbus_read_input_registers: ");
  278. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  279. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  280. printf("* modbus_write_bit: ");
  281. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  282. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  283. UT_BITS_NB, tab_rp_bits);
  284. printf("* modbus_write_coils: ");
  285. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  286. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
  287. tab_rp_registers[0]);
  288. printf("* modbus_write_register: ");
  289. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  290. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX,
  291. UT_REGISTERS_NB, tab_rp_registers);
  292. printf("* modbus_write_registers: ");
  293. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  294. /** TOO MANY DATA **/
  295. printf("\nTEST TOO MANY DATA ERROR:\n");
  296. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  297. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  298. printf("* modbus_read_bits: ");
  299. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  300. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  301. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  302. printf("* modbus_read_input_bits: ");
  303. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  304. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  305. MODBUS_MAX_READ_REGISTERS + 1,
  306. tab_rp_registers);
  307. printf("* modbus_read_registers: ");
  308. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  309. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  310. MODBUS_MAX_READ_REGISTERS + 1,
  311. tab_rp_registers);
  312. printf("* modbus_read_input_registers: ");
  313. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  314. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  315. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  316. printf("* modbus_write_bits: ");
  317. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  318. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  319. MODBUS_MAX_WRITE_REGISTERS + 1,
  320. tab_rp_registers);
  321. printf("* modbus_write_registers: ");
  322. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  323. /** SLAVE REPLY **/
  324. printf("\nTEST SLAVE REPLY:\n");
  325. modbus_set_slave(ctx, INVALID_SERVER_ID);
  326. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  327. UT_REGISTERS_NB, tab_rp_registers);
  328. if (use_backend == RTU) {
  329. const int RAW_REQ_LENGTH = 6;
  330. uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 };
  331. /* Too many points */
  332. uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
  333. const int RAW_REP_LENGTH = 7;
  334. uint8_t raw_rep[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 };
  335. uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
  336. /* No response in RTU mode */
  337. printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
  338. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  339. /* The slave raises a timeout on a confirmation to ignore because if an
  340. * indication for another slave is received, a confirmation must follow */
  341. /* Send a pair of indication/confirmation to the slave with a different
  342. * slave ID to simulate a communication on a RS485 bus. At first, the
  343. * slave will see the indication message then the confirmation, and it must
  344. * ignore both. */
  345. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  346. modbus_send_raw_request(ctx, raw_rep, RAW_REP_LENGTH * sizeof(uint8_t));
  347. rc = modbus_receive_confirmation(ctx, rsp);
  348. printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
  349. INVALID_SERVER_ID);
  350. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  351. /* Send an INVALID request for another slave */
  352. modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  353. rc = modbus_receive_confirmation(ctx, rsp);
  354. printf("1-C/3 No response from slave %d with invalid request: ",
  355. INVALID_SERVER_ID);
  356. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  357. } else {
  358. /* Response in TCP mode */
  359. printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
  360. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  361. }
  362. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  363. ASSERT_TRUE(rc != -1, "Invalid broacast address");
  364. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  365. UT_REGISTERS_NB, tab_rp_registers);
  366. printf("2/3 No reply after a broadcast query: ");
  367. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  368. /* Restore slave */
  369. if (use_backend == RTU) {
  370. modbus_set_slave(ctx, SERVER_ID);
  371. } else {
  372. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  373. }
  374. printf("3/3 Response with an invalid TID or slave: ");
  375. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
  376. 1, tab_rp_registers);
  377. ASSERT_TRUE(rc == -1, "");
  378. printf("1/2 Report slave ID truncated: \n");
  379. /* Set a marker to ensure limit is respected */
  380. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
  381. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
  382. /* Return the size required (response size) but respects the defined limit */
  383. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&
  384. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
  385. "Return is rc %d (%d) and marker is %d (42)",
  386. rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
  387. printf("2/2 Report slave ID: \n");
  388. /* tab_rp_bits is used to store bytes */
  389. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
  390. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, "");
  391. /* Slave ID is an arbitraty number for libmodbus */
  392. ASSERT_TRUE(rc > 0, "");
  393. /* Run status indicator is ON */
  394. ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, "");
  395. /* Print additional data as string */
  396. if (rc > 2) {
  397. printf("Additional data: ");
  398. for (i=2; i < rc; i++) {
  399. printf("%c", tab_rp_bits[i]);
  400. }
  401. printf("\n");
  402. }
  403. /* Save original timeout */
  404. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  405. modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec);
  406. rc = modbus_set_response_timeout(ctx, 0, 0);
  407. printf("1/6 Invalid response timeout (zero): ");
  408. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  409. rc = modbus_set_response_timeout(ctx, 0, 1000000);
  410. printf("2/6 Invalid response timeout (too large us): ");
  411. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  412. rc = modbus_set_byte_timeout(ctx, 0, 1000000);
  413. printf("3/6 Invalid byte timeout (too large us): ");
  414. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  415. modbus_set_response_timeout(ctx, 0, 1);
  416. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  417. UT_REGISTERS_NB, tab_rp_registers);
  418. printf("4/6 1us response timeout: ");
  419. if (rc == -1 && errno == ETIMEDOUT) {
  420. printf("OK\n");
  421. } else {
  422. printf("FAILED (can fail on some platforms)\n");
  423. }
  424. /* A wait and flush operation is done by the error recovery code of
  425. * libmodbus but after a sleep of current response timeout
  426. * so 0 can be too short!
  427. */
  428. usleep(old_response_to_sec * 1000000 + old_response_to_usec);
  429. modbus_flush(ctx);
  430. /* Trigger a special behaviour on server to wait for 0.5 second before
  431. * replying whereas allowed timeout is 0.2 second */
  432. modbus_set_response_timeout(ctx, 0, 200000);
  433. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  434. 1, tab_rp_registers);
  435. printf("5/6 Too short response timeout (0.2s < 0.5s): ");
  436. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  437. /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
  438. usleep(400000);
  439. modbus_flush(ctx);
  440. modbus_set_response_timeout(ctx, 0, 600000);
  441. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  442. 1, tab_rp_registers);
  443. printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
  444. ASSERT_TRUE(rc == 1, "");
  445. /* Disable the byte timeout.
  446. The full response must be available in the 600ms interval */
  447. modbus_set_byte_timeout(ctx, 0, 0);
  448. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  449. 1, tab_rp_registers);
  450. printf("7/7 Disable byte timeout: ");
  451. ASSERT_TRUE(rc == 1, "");
  452. /* Restore original response timeout */
  453. modbus_set_response_timeout(ctx, old_response_to_sec,
  454. old_response_to_usec);
  455. if (use_backend == TCP) {
  456. /* The test server is only able to test byte timeouts with the TCP
  457. * backend */
  458. /* Timeout of 3ms between bytes */
  459. modbus_set_byte_timeout(ctx, 0, 3000);
  460. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
  461. 1, tab_rp_registers);
  462. printf("1/2 Too small byte timeout (3ms < 5ms): ");
  463. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  464. /* Wait remaing bytes before flushing */
  465. usleep(11 * 5000);
  466. modbus_flush(ctx);
  467. /* Timeout of 7ms between bytes */
  468. modbus_set_byte_timeout(ctx, 0, 7000);
  469. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
  470. 1, tab_rp_registers);
  471. printf("2/2 Adapted byte timeout (7ms > 5ms): ");
  472. ASSERT_TRUE(rc == 1, "");
  473. }
  474. /* Restore original byte timeout */
  475. modbus_set_byte_timeout(ctx, old_byte_to_sec, old_byte_to_usec);
  476. /** BAD RESPONSE **/
  477. printf("\nTEST BAD RESPONSE ERROR:\n");
  478. /* Allocate only the required space */
  479. tab_rp_registers_bad = (uint16_t *) malloc(
  480. UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
  481. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  482. UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
  483. printf("* modbus_read_registers: ");
  484. ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
  485. free(tab_rp_registers_bad);
  486. /** MANUAL EXCEPTION **/
  487. printf("\nTEST MANUAL EXCEPTION:\n");
  488. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
  489. UT_REGISTERS_NB, tab_rp_registers);
  490. printf("* modbus_read_registers at special address: ");
  491. ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
  492. /** SERVER **/
  493. if (test_server(ctx, use_backend) == -1) {
  494. goto close;
  495. }
  496. /* Test init functions */
  497. printf("\nTEST INVALID INITIALIZATION:\n");
  498. ctx = modbus_new_rtu(NULL, 1, 'A', 0, 0);
  499. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  500. ctx = modbus_new_rtu("/dev/dummy", 0, 'A', 0, 0);
  501. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  502. ctx = modbus_new_tcp_pi(NULL, NULL);
  503. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  504. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  505. success = TRUE;
  506. close:
  507. /* Free the memory */
  508. free(tab_rp_bits);
  509. free(tab_rp_registers);
  510. /* Close the connection */
  511. modbus_close(ctx);
  512. modbus_free(ctx);
  513. return (success) ? 0 : -1;
  514. }
  515. /* Send crafted requests to test server resilience
  516. and ensure proper exceptions are returned. */
  517. int test_server(modbus_t *ctx, int use_backend)
  518. {
  519. int rc;
  520. int i;
  521. /* Read requests */
  522. const int READ_RAW_REQ_LEN = 6;
  523. uint8_t read_raw_req[] = {
  524. /* slave */
  525. (use_backend == RTU) ? SERVER_ID : 0xFF,
  526. /* function, address, 5 values */
  527. MODBUS_FC_READ_HOLDING_REGISTERS,
  528. UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
  529. 0x0, 0x05
  530. };
  531. /* Write and read registers request */
  532. const int RW_RAW_REQ_LEN = 13;
  533. uint8_t rw_raw_req[] = {
  534. /* slave */
  535. (use_backend == RTU) ? SERVER_ID : 0xFF,
  536. /* function, addr to read, nb to read */
  537. MODBUS_FC_WRITE_AND_READ_REGISTERS,
  538. /* Read */
  539. UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
  540. (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
  541. (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
  542. /* Write */
  543. 0, 0,
  544. 0, 1,
  545. /* Write byte count */
  546. 1 * 2,
  547. /* One data to write... */
  548. 0x12, 0x34
  549. };
  550. const int WRITE_RAW_REQ_LEN = 13;
  551. uint8_t write_raw_req[] = {
  552. /* slave */
  553. (use_backend == RTU) ? SERVER_ID : 0xFF,
  554. /* function will be set in the loop */
  555. MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  556. /* Address */
  557. UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
  558. /* 3 values, 6 bytes */
  559. 0x00, 0x03, 0x06,
  560. /* Dummy data to write */
  561. 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
  562. };
  563. int req_length;
  564. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  565. int tab_read_function[] = {
  566. MODBUS_FC_READ_COILS,
  567. MODBUS_FC_READ_DISCRETE_INPUTS,
  568. MODBUS_FC_READ_HOLDING_REGISTERS,
  569. MODBUS_FC_READ_INPUT_REGISTERS
  570. };
  571. int tab_read_nb_max[] = {
  572. MODBUS_MAX_READ_BITS + 1,
  573. MODBUS_MAX_READ_BITS + 1,
  574. MODBUS_MAX_READ_REGISTERS + 1,
  575. MODBUS_MAX_READ_REGISTERS + 1
  576. };
  577. int backend_length;
  578. int backend_offset;
  579. if (use_backend == RTU) {
  580. backend_length = 3;
  581. backend_offset = 1;
  582. } else {
  583. backend_length = 7;
  584. backend_offset = 7;
  585. }
  586. printf("\nTEST RAW REQUESTS:\n");
  587. req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
  588. printf("* modbus_send_raw_request: ");
  589. ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
  590. printf("* modbus_receive_confirmation: ");
  591. rc = modbus_receive_confirmation(ctx, rsp);
  592. ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc);
  593. /* Try to read more values than a response could hold for all data
  594. types. */
  595. for (i=0; i<4; i++) {
  596. rc = send_crafted_request(ctx, tab_read_function[i],
  597. read_raw_req, READ_RAW_REQ_LEN,
  598. tab_read_nb_max[i], 0,
  599. backend_length, backend_offset);
  600. if (rc == -1)
  601. goto close;
  602. }
  603. /* Modbus write and read multiple registers */
  604. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_AND_READ_REGISTERS,
  605. rw_raw_req, RW_RAW_REQ_LEN,
  606. MODBUS_MAX_WR_READ_REGISTERS + 1, 0,
  607. backend_length, backend_offset);
  608. if (rc == -1)
  609. goto close;
  610. /* Modbus write multiple registers with large number of values but a set a
  611. small number of bytes in requests (not nb * 2 as usual). */
  612. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  613. write_raw_req, WRITE_RAW_REQ_LEN,
  614. MODBUS_MAX_WRITE_REGISTERS + 1, 6,
  615. backend_length, backend_offset);
  616. if (rc == -1)
  617. goto close;
  618. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,
  619. write_raw_req, WRITE_RAW_REQ_LEN,
  620. MODBUS_MAX_WRITE_BITS + 1, 6,
  621. backend_length, backend_offset);
  622. if (rc == -1)
  623. goto close;
  624. return 0;
  625. close:
  626. return -1;
  627. }
  628. int send_crafted_request(modbus_t *ctx, int function,
  629. uint8_t *req, int req_len,
  630. uint16_t max_value, uint16_t bytes,
  631. int backend_length, int backend_offset)
  632. {
  633. const int EXCEPTION_RC = 2;
  634. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  635. int j;
  636. uint32_t old_response_to_sec;
  637. uint32_t old_response_to_usec;
  638. /* This requests can generate flushes server side so we need a higher
  639. * response timeout than the server. The server uses the defined response
  640. * timeout to sleep before flushing.
  641. * The old timeouts are restored at the end.
  642. */
  643. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  644. modbus_set_response_timeout(ctx, 0, 600000);
  645. for (j=0; j<2; j++) {
  646. int rc;
  647. req[1] = function;
  648. if (j == 0) {
  649. /* Try to read or write zero values on first iteration */
  650. req[4] = 0x00;
  651. req[5] = 0x00;
  652. if (bytes) {
  653. /* Write query */
  654. req[6] = 0x00;
  655. }
  656. } else {
  657. /* Try to read or write max values + 1 on second iteration */
  658. req[4] = (max_value >> 8) & 0xFF;
  659. req[5] = max_value & 0xFF;
  660. if (bytes) {
  661. /* Write query (nb values * 2 to convert in bytes for registers) */
  662. req[6] = bytes;
  663. }
  664. }
  665. modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
  666. if (j == 0) {
  667. printf("* try function 0x%X: %s 0 values: ", function, bytes ? "write": "read");
  668. } else {
  669. printf("* try function 0x%X: %s %d values: ", function, bytes ? "write": "read",
  670. max_value);
  671. }
  672. rc = modbus_receive_confirmation(ctx, rsp);
  673. ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
  674. rsp[backend_offset] == (0x80 + function) &&
  675. rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
  676. }
  677. modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
  678. return 0;
  679. close:
  680. modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
  681. return -1;
  682. }