unit-test-client.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #define BUG_REPORT(_cond, _format, _args ...) \
  24. printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args)
  25. #define ASSERT_TRUE(_cond, _format, __args...) { \
  26. if (_cond) { \
  27. printf("OK\n"); \
  28. } else { \
  29. BUG_REPORT(_cond, _format, ## __args); \
  30. goto close; \
  31. } \
  32. };
  33. int main(int argc, char *argv[])
  34. {
  35. const int NB_REPORT_SLAVE_ID = 10;
  36. uint8_t *tab_rp_bits = NULL;
  37. uint16_t *tab_rp_registers = NULL;
  38. uint16_t *tab_rp_registers_bad = NULL;
  39. modbus_t *ctx = NULL;
  40. int i;
  41. uint8_t value;
  42. int nb_points;
  43. int rc;
  44. float real;
  45. uint32_t ireal;
  46. uint32_t old_response_to_sec;
  47. uint32_t old_response_to_usec;
  48. uint32_t new_response_to_sec;
  49. uint32_t new_response_to_usec;
  50. uint32_t old_byte_to_sec;
  51. uint32_t old_byte_to_usec;
  52. int use_backend;
  53. int success = FALSE;
  54. if (argc > 1) {
  55. if (strcmp(argv[1], "tcp") == 0) {
  56. use_backend = TCP;
  57. } else if (strcmp(argv[1], "tcppi") == 0) {
  58. use_backend = TCP_PI;
  59. } else if (strcmp(argv[1], "rtu") == 0) {
  60. use_backend = RTU;
  61. } else {
  62. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
  63. exit(1);
  64. }
  65. } else {
  66. /* By default */
  67. use_backend = TCP;
  68. }
  69. if (use_backend == TCP) {
  70. ctx = modbus_new_tcp("127.0.0.1", 1502);
  71. } else if (use_backend == TCP_PI) {
  72. ctx = modbus_new_tcp_pi("::1", "1502");
  73. } else {
  74. ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  75. }
  76. if (ctx == NULL) {
  77. fprintf(stderr, "Unable to allocate libmodbus context\n");
  78. return -1;
  79. }
  80. modbus_set_debug(ctx, TRUE);
  81. modbus_set_error_recovery(ctx,
  82. MODBUS_ERROR_RECOVERY_LINK |
  83. MODBUS_ERROR_RECOVERY_PROTOCOL);
  84. if (use_backend == RTU) {
  85. modbus_set_slave(ctx, SERVER_ID);
  86. }
  87. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  88. if (modbus_connect(ctx) == -1) {
  89. fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
  90. modbus_free(ctx);
  91. return -1;
  92. }
  93. modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec);
  94. printf("** UNIT TESTING **\n");
  95. printf("1/1 No response timeout modification on connect: ");
  96. ASSERT_TRUE(old_response_to_sec == new_response_to_sec &&
  97. old_response_to_usec == new_response_to_usec, "");
  98. /* Allocate and initialize the memory to store the bits */
  99. nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB;
  100. tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
  101. memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t));
  102. /* Allocate and initialize the memory to store the registers */
  103. nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ?
  104. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  105. tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
  106. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  107. printf("\nTEST WRITE/READ:\n");
  108. /** COIL BITS **/
  109. /* Single */
  110. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON);
  111. printf("1/2 modbus_write_bit: ");
  112. ASSERT_TRUE(rc == 1, "");
  113. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits);
  114. printf("2/2 modbus_read_bits: ");
  115. ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  116. ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n",
  117. tab_rp_bits[0], ON);
  118. /* End single */
  119. /* Multiple bits */
  120. {
  121. uint8_t tab_value[UT_BITS_NB];
  122. modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB);
  123. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  124. UT_BITS_NB, tab_value);
  125. printf("1/2 modbus_write_bits: ");
  126. ASSERT_TRUE(rc == UT_BITS_NB, "");
  127. }
  128. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits);
  129. printf("2/2 modbus_read_bits: ");
  130. ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc);
  131. i = 0;
  132. nb_points = UT_BITS_NB;
  133. while (nb_points > 0) {
  134. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  135. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  136. ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  137. value, UT_BITS_TAB[i]);
  138. nb_points -= nb_bits;
  139. i++;
  140. }
  141. printf("OK\n");
  142. /* End of multiple bits */
  143. /** DISCRETE INPUTS **/
  144. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  145. UT_INPUT_BITS_NB, tab_rp_bits);
  146. printf("1/1 modbus_read_input_bits: ");
  147. ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc);
  148. i = 0;
  149. nb_points = UT_INPUT_BITS_NB;
  150. while (nb_points > 0) {
  151. int nb_bits = (nb_points > 8) ? 8 : nb_points;
  152. value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits);
  153. ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n",
  154. value, UT_INPUT_BITS_TAB[i]);
  155. nb_points -= nb_bits;
  156. i++;
  157. }
  158. printf("OK\n");
  159. /** HOLDING REGISTERS **/
  160. /* Single register */
  161. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234);
  162. printf("1/2 modbus_write_register: ");
  163. ASSERT_TRUE(rc == 1, "");
  164. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  165. 1, tab_rp_registers);
  166. printf("2/2 modbus_read_registers: ");
  167. ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc);
  168. ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n",
  169. tab_rp_registers[0], 0x1234);
  170. /* End of single register */
  171. /* Many registers */
  172. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  173. UT_REGISTERS_NB, UT_REGISTERS_TAB);
  174. printf("1/5 modbus_write_registers: ");
  175. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  176. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  177. UT_REGISTERS_NB, tab_rp_registers);
  178. printf("2/5 modbus_read_registers: ");
  179. ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
  180. for (i=0; i < UT_REGISTERS_NB; i++) {
  181. ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i],
  182. "FAILED (%0X != %0X)\n",
  183. tab_rp_registers[i], UT_REGISTERS_TAB[i]);
  184. }
  185. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  186. 0, tab_rp_registers);
  187. printf("3/5 modbus_read_registers (0): ");
  188. ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc);
  189. nb_points = (UT_REGISTERS_NB >
  190. UT_INPUT_REGISTERS_NB) ?
  191. UT_REGISTERS_NB : UT_INPUT_REGISTERS_NB;
  192. memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
  193. /* Write registers to zero from tab_rp_registers and store read registers
  194. into tab_rp_registers. So the read registers must set to 0, except the
  195. first one because there is an offset of 1 register on write. */
  196. rc = modbus_write_and_read_registers(ctx,
  197. UT_REGISTERS_ADDRESS + 1,
  198. UT_REGISTERS_NB - 1,
  199. tab_rp_registers,
  200. UT_REGISTERS_ADDRESS,
  201. UT_REGISTERS_NB,
  202. tab_rp_registers);
  203. printf("4/5 modbus_write_and_read_registers: ");
  204. ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n",
  205. rc, UT_REGISTERS_NB);
  206. ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0],
  207. "FAILED (%0X != %0X)\n",
  208. tab_rp_registers[0], UT_REGISTERS_TAB[0]);
  209. for (i=1; i < UT_REGISTERS_NB; i++) {
  210. ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n",
  211. tab_rp_registers[i], 0);
  212. }
  213. /* End of many registers */
  214. /** INPUT REGISTERS **/
  215. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  216. UT_INPUT_REGISTERS_NB,
  217. tab_rp_registers);
  218. printf("1/1 modbus_read_input_registers: ");
  219. ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc);
  220. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  221. ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i],
  222. "FAILED (%0X != %0X)\n",
  223. tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]);
  224. }
  225. printf("\nTEST FLOATS\n");
  226. /** FLOAT **/
  227. printf("1/4 Set float: ");
  228. modbus_set_float(UT_REAL, tab_rp_registers);
  229. if (tab_rp_registers[1] == (UT_IREAL >> 16) &&
  230. tab_rp_registers[0] == (UT_IREAL & 0xFFFF)) {
  231. printf("OK\n");
  232. } else {
  233. /* Avoid *((uint32_t *)tab_rp_registers)
  234. * https://github.com/stephane/libmodbus/pull/104 */
  235. ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF;
  236. ireal |= (uint32_t) tab_rp_registers[1] << 16;
  237. printf("FAILED (%x != %x)\n", ireal, UT_IREAL);
  238. goto close;
  239. }
  240. printf("2/4 Get float: ");
  241. real = modbus_get_float(tab_rp_registers);
  242. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  243. printf("3/4 Set float in DBCA order: ");
  244. modbus_set_float_dcba(UT_REAL, tab_rp_registers);
  245. ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF;
  246. ireal |= (uint32_t) tab_rp_registers[1] << 16;
  247. ASSERT_TRUE(tab_rp_registers[1] == (UT_IREAL_DCBA >> 16) &&
  248. tab_rp_registers[0] == (UT_IREAL_DCBA & 0xFFFF),
  249. "FAILED (%x != %x)\n", ireal, UT_IREAL_DCBA);
  250. printf("4/4 Get float in DCBA order: ");
  251. real = modbus_get_float_dcba(tab_rp_registers);
  252. ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL);
  253. /* MASKS */
  254. printf("1/1 Write mask: ");
  255. rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x12);
  256. rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
  257. ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
  258. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
  259. ASSERT_TRUE(tab_rp_registers[0] == 0x17,
  260. "FAILED (%0X != %0X)\n",
  261. tab_rp_registers[0], 0x17);
  262. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  263. /** ILLEGAL DATA ADDRESS **/
  264. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  265. /* The mapping begins at 0 and ends at address + nb_points so
  266. * the addresses are not valid. */
  267. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits);
  268. printf("* modbus_read_bits: ");
  269. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  270. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  271. UT_INPUT_BITS_NB + 1, tab_rp_bits);
  272. printf("* modbus_read_input_bits: ");
  273. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  274. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  275. UT_REGISTERS_NB + 1, tab_rp_registers);
  276. printf("* modbus_read_registers: ");
  277. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  278. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  279. UT_INPUT_REGISTERS_NB + 1,
  280. tab_rp_registers);
  281. printf("* modbus_read_input_registers: ");
  282. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  283. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  284. printf("* modbus_write_bit: ");
  285. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  286. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  287. UT_BITS_NB, tab_rp_bits);
  288. printf("* modbus_write_coils: ");
  289. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  290. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  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, addr 1, 5 values */
  527. MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05
  528. };
  529. /* Write and read registers request */
  530. const int RW_RAW_REQ_LEN = 13;
  531. uint8_t rw_raw_req[] = {
  532. /* slave */
  533. (use_backend == RTU) ? SERVER_ID : 0xFF,
  534. /* function, addr to read, nb to read */
  535. MODBUS_FC_WRITE_AND_READ_REGISTERS,
  536. /* Read */
  537. 0, 0,
  538. (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
  539. (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
  540. /* Write */
  541. 0, 0,
  542. 0, 1,
  543. /* Write byte count */
  544. 1 * 2,
  545. /* One data to write... */
  546. 0x12, 0x34
  547. };
  548. const int WRITE_RAW_REQ_LEN = 13;
  549. uint8_t write_raw_req[] = {
  550. /* slave */
  551. (use_backend == RTU) ? SERVER_ID : 0xFF,
  552. /* function will be set in the loop */
  553. MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  554. /* Address */
  555. UT_REGISTERS_ADDRESS >> 8,
  556. UT_REGISTERS_ADDRESS & 0xFF,
  557. /* 3 values, 6 bytes */
  558. 0x00, 0x03, 0x06,
  559. /* Dummy data to write */
  560. 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
  561. };
  562. int req_length;
  563. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  564. int tab_read_function[] = {
  565. MODBUS_FC_READ_COILS,
  566. MODBUS_FC_READ_DISCRETE_INPUTS,
  567. MODBUS_FC_READ_HOLDING_REGISTERS,
  568. MODBUS_FC_READ_INPUT_REGISTERS
  569. };
  570. int tab_read_nb_max[] = {
  571. MODBUS_MAX_READ_BITS + 1,
  572. MODBUS_MAX_READ_BITS + 1,
  573. MODBUS_MAX_READ_REGISTERS + 1,
  574. MODBUS_MAX_READ_REGISTERS + 1
  575. };
  576. int backend_length;
  577. int backend_offset;
  578. if (use_backend == RTU) {
  579. backend_length = 3;
  580. backend_offset = 1;
  581. } else {
  582. backend_length = 7;
  583. backend_offset = 7;
  584. }
  585. printf("\nTEST RAW REQUESTS:\n");
  586. req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
  587. printf("* modbus_send_raw_request: ");
  588. ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
  589. printf("* modbus_receive_confirmation: ");
  590. rc = modbus_receive_confirmation(ctx, rsp);
  591. ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc);
  592. /* Try to read more values than a response could hold for all data
  593. * types.
  594. */
  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. }