unit-test-client.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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_write_register(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  291. tab_rp_registers[0]);
  292. printf("* modbus_write_register: ");
  293. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  294. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  295. UT_REGISTERS_NB, tab_rp_registers);
  296. printf("* modbus_write_registers: ");
  297. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  298. /** TOO MANY DATA **/
  299. printf("\nTEST TOO MANY DATA ERROR:\n");
  300. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  301. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  302. printf("* modbus_read_bits: ");
  303. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  304. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  305. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  306. printf("* modbus_read_input_bits: ");
  307. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  308. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  309. MODBUS_MAX_READ_REGISTERS + 1,
  310. tab_rp_registers);
  311. printf("* modbus_read_registers: ");
  312. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  313. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  314. MODBUS_MAX_READ_REGISTERS + 1,
  315. tab_rp_registers);
  316. printf("* modbus_read_input_registers: ");
  317. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  318. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  319. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  320. printf("* modbus_write_bits: ");
  321. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  322. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  323. MODBUS_MAX_WRITE_REGISTERS + 1,
  324. tab_rp_registers);
  325. printf("* modbus_write_registers: ");
  326. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  327. /** SLAVE REPLY **/
  328. printf("\nTEST SLAVE REPLY:\n");
  329. modbus_set_slave(ctx, INVALID_SERVER_ID);
  330. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  331. UT_REGISTERS_NB, tab_rp_registers);
  332. if (use_backend == RTU) {
  333. const int RAW_REQ_LENGTH = 6;
  334. uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 };
  335. /* Too many points */
  336. uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
  337. const int RAW_REP_LENGTH = 7;
  338. uint8_t raw_rep[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 };
  339. uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
  340. /* No response in RTU mode */
  341. printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
  342. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  343. /* The slave raises a timeout on a confirmation to ignore because if an
  344. * indication for another slave is received, a confirmation must follow */
  345. /* Send a pair of indication/confirmation to the slave with a different
  346. * slave ID to simulate a communication on a RS485 bus. At first, the
  347. * slave will see the indication message then the confirmation, and it must
  348. * ignore both. */
  349. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  350. modbus_send_raw_request(ctx, raw_rep, RAW_REP_LENGTH * sizeof(uint8_t));
  351. rc = modbus_receive_confirmation(ctx, rsp);
  352. printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
  353. INVALID_SERVER_ID);
  354. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  355. /* Send an INVALID request for another slave */
  356. modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  357. rc = modbus_receive_confirmation(ctx, rsp);
  358. printf("1-C/3 No response from slave %d with invalid request: ",
  359. INVALID_SERVER_ID);
  360. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  361. } else {
  362. /* Response in TCP mode */
  363. printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
  364. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  365. }
  366. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  367. ASSERT_TRUE(rc != -1, "Invalid broacast address");
  368. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  369. UT_REGISTERS_NB, tab_rp_registers);
  370. printf("2/3 No reply after a broadcast query: ");
  371. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  372. /* Restore slave */
  373. if (use_backend == RTU) {
  374. modbus_set_slave(ctx, SERVER_ID);
  375. } else {
  376. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  377. }
  378. printf("3/3 Response with an invalid TID or slave: ");
  379. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
  380. 1, tab_rp_registers);
  381. ASSERT_TRUE(rc == -1, "");
  382. printf("1/2 Report slave ID truncated: \n");
  383. /* Set a marker to ensure limit is respected */
  384. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
  385. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
  386. /* Return the size required (response size) but respects the defined limit */
  387. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&
  388. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
  389. "Return is rc %d (%d) and marker is %d (42)",
  390. rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
  391. printf("2/2 Report slave ID: \n");
  392. /* tab_rp_bits is used to store bytes */
  393. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
  394. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, "");
  395. /* Slave ID is an arbitraty number for libmodbus */
  396. ASSERT_TRUE(rc > 0, "");
  397. /* Run status indicator is ON */
  398. ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, "");
  399. /* Print additional data as string */
  400. if (rc > 2) {
  401. printf("Additional data: ");
  402. for (i=2; i < rc; i++) {
  403. printf("%c", tab_rp_bits[i]);
  404. }
  405. printf("\n");
  406. }
  407. /* Save original timeout */
  408. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  409. modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec);
  410. rc = modbus_set_response_timeout(ctx, 0, 0);
  411. printf("1/6 Invalid response timeout (zero): ");
  412. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  413. rc = modbus_set_response_timeout(ctx, 0, 1000000);
  414. printf("2/6 Invalid response timeout (too large us): ");
  415. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  416. rc = modbus_set_byte_timeout(ctx, 0, 1000000);
  417. printf("3/6 Invalid byte timeout (too large us): ");
  418. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  419. modbus_set_response_timeout(ctx, 0, 1);
  420. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  421. UT_REGISTERS_NB, tab_rp_registers);
  422. printf("4/6 1us response timeout: ");
  423. if (rc == -1 && errno == ETIMEDOUT) {
  424. printf("OK\n");
  425. } else {
  426. printf("FAILED (can fail on some platforms)\n");
  427. }
  428. /* A wait and flush operation is done by the error recovery code of
  429. * libmodbus but after a sleep of current response timeout
  430. * so 0 can be too short!
  431. */
  432. usleep(old_response_to_sec * 1000000 + old_response_to_usec);
  433. modbus_flush(ctx);
  434. /* Trigger a special behaviour on server to wait for 0.5 second before
  435. * replying whereas allowed timeout is 0.2 second */
  436. modbus_set_response_timeout(ctx, 0, 200000);
  437. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  438. 1, tab_rp_registers);
  439. printf("5/6 Too short response timeout (0.2s < 0.5s): ");
  440. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  441. /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
  442. usleep(400000);
  443. modbus_flush(ctx);
  444. modbus_set_response_timeout(ctx, 0, 600000);
  445. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  446. 1, tab_rp_registers);
  447. printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
  448. ASSERT_TRUE(rc == 1, "");
  449. /* Disable the byte timeout.
  450. The full response must be available in the 600ms interval */
  451. modbus_set_byte_timeout(ctx, 0, 0);
  452. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  453. 1, tab_rp_registers);
  454. printf("7/7 Disable byte timeout: ");
  455. ASSERT_TRUE(rc == 1, "");
  456. /* Restore original response timeout */
  457. modbus_set_response_timeout(ctx, old_response_to_sec,
  458. old_response_to_usec);
  459. if (use_backend == TCP) {
  460. /* The test server is only able to test byte timeouts with the TCP
  461. * backend */
  462. /* Timeout of 3ms between bytes */
  463. modbus_set_byte_timeout(ctx, 0, 3000);
  464. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
  465. 1, tab_rp_registers);
  466. printf("1/2 Too small byte timeout (3ms < 5ms): ");
  467. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  468. /* Wait remaing bytes before flushing */
  469. usleep(11 * 5000);
  470. modbus_flush(ctx);
  471. /* Timeout of 7ms between bytes */
  472. modbus_set_byte_timeout(ctx, 0, 7000);
  473. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS,
  474. 1, tab_rp_registers);
  475. printf("2/2 Adapted byte timeout (7ms > 5ms): ");
  476. ASSERT_TRUE(rc == 1, "");
  477. }
  478. /* Restore original byte timeout */
  479. modbus_set_byte_timeout(ctx, old_byte_to_sec, old_byte_to_usec);
  480. /** BAD RESPONSE **/
  481. printf("\nTEST BAD RESPONSE ERROR:\n");
  482. /* Allocate only the required space */
  483. tab_rp_registers_bad = (uint16_t *) malloc(
  484. UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t));
  485. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  486. UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad);
  487. printf("* modbus_read_registers: ");
  488. ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "");
  489. free(tab_rp_registers_bad);
  490. /** MANUAL EXCEPTION **/
  491. printf("\nTEST MANUAL EXCEPTION:\n");
  492. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SPECIAL,
  493. UT_REGISTERS_NB, tab_rp_registers);
  494. printf("* modbus_read_registers at special address: ");
  495. ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, "");
  496. /** SERVER **/
  497. if (test_server(ctx, use_backend) == -1) {
  498. goto close;
  499. }
  500. /* Test init functions */
  501. printf("\nTEST INVALID INITIALIZATION:\n");
  502. ctx = modbus_new_rtu(NULL, 1, 'A', 0, 0);
  503. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  504. ctx = modbus_new_rtu("/dev/dummy", 0, 'A', 0, 0);
  505. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  506. ctx = modbus_new_tcp_pi(NULL, NULL);
  507. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  508. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  509. success = TRUE;
  510. close:
  511. /* Free the memory */
  512. free(tab_rp_bits);
  513. free(tab_rp_registers);
  514. /* Close the connection */
  515. modbus_close(ctx);
  516. modbus_free(ctx);
  517. return (success) ? 0 : -1;
  518. }
  519. /* Send crafted requests to test server resilience
  520. and ensure proper exceptions are returned. */
  521. int test_server(modbus_t *ctx, int use_backend)
  522. {
  523. int rc;
  524. int i;
  525. /* Read requests */
  526. const int READ_RAW_REQ_LEN = 6;
  527. uint8_t read_raw_req[] = {
  528. /* slave */
  529. (use_backend == RTU) ? SERVER_ID : 0xFF,
  530. /* function, addr 1, 5 values */
  531. MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05
  532. };
  533. /* Write and read registers request */
  534. const int RW_RAW_REQ_LEN = 13;
  535. uint8_t rw_raw_req[] = {
  536. /* slave */
  537. (use_backend == RTU) ? SERVER_ID : 0xFF,
  538. /* function, addr to read, nb to read */
  539. MODBUS_FC_WRITE_AND_READ_REGISTERS,
  540. /* Read */
  541. 0, 0,
  542. (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
  543. (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
  544. /* Write */
  545. 0, 0,
  546. 0, 1,
  547. /* Write byte count */
  548. 1 * 2,
  549. /* One data to write... */
  550. 0x12, 0x34
  551. };
  552. const int WRITE_RAW_REQ_LEN = 13;
  553. uint8_t write_raw_req[] = {
  554. /* slave */
  555. (use_backend == RTU) ? SERVER_ID : 0xFF,
  556. /* function will be set in the loop */
  557. MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  558. /* Address */
  559. UT_REGISTERS_ADDRESS >> 8,
  560. UT_REGISTERS_ADDRESS & 0xFF,
  561. /* 3 values, 6 bytes */
  562. 0x00, 0x03, 0x06,
  563. /* Dummy data to write */
  564. 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
  565. };
  566. int req_length;
  567. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  568. int tab_read_function[] = {
  569. MODBUS_FC_READ_COILS,
  570. MODBUS_FC_READ_DISCRETE_INPUTS,
  571. MODBUS_FC_READ_HOLDING_REGISTERS,
  572. MODBUS_FC_READ_INPUT_REGISTERS
  573. };
  574. int tab_read_nb_max[] = {
  575. MODBUS_MAX_READ_BITS + 1,
  576. MODBUS_MAX_READ_BITS + 1,
  577. MODBUS_MAX_READ_REGISTERS + 1,
  578. MODBUS_MAX_READ_REGISTERS + 1
  579. };
  580. int backend_length;
  581. int backend_offset;
  582. if (use_backend == RTU) {
  583. backend_length = 3;
  584. backend_offset = 1;
  585. } else {
  586. backend_length = 7;
  587. backend_offset = 7;
  588. }
  589. printf("\nTEST RAW REQUESTS:\n");
  590. req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
  591. printf("* modbus_send_raw_request: ");
  592. ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
  593. printf("* modbus_receive_confirmation: ");
  594. rc = modbus_receive_confirmation(ctx, rsp);
  595. ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc);
  596. /* Try to read more values than a response could hold for all data
  597. * types.
  598. */
  599. for (i=0; i<4; i++) {
  600. rc = send_crafted_request(ctx, tab_read_function[i],
  601. read_raw_req, READ_RAW_REQ_LEN,
  602. tab_read_nb_max[i], 0,
  603. backend_length, backend_offset);
  604. if (rc == -1)
  605. goto close;
  606. }
  607. /* Modbus write and read multiple registers */
  608. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_AND_READ_REGISTERS,
  609. rw_raw_req, RW_RAW_REQ_LEN,
  610. MODBUS_MAX_WR_READ_REGISTERS + 1, 0,
  611. backend_length, backend_offset);
  612. if (rc == -1)
  613. goto close;
  614. /* Modbus write multiple registers with large number of values but a set a
  615. small number of bytes in requests (not nb * 2 as usual). */
  616. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  617. write_raw_req, WRITE_RAW_REQ_LEN,
  618. MODBUS_MAX_WRITE_REGISTERS + 1, 6,
  619. backend_length, backend_offset);
  620. if (rc == -1)
  621. goto close;
  622. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,
  623. write_raw_req, WRITE_RAW_REQ_LEN,
  624. MODBUS_MAX_WRITE_BITS + 1, 6,
  625. backend_length, backend_offset);
  626. if (rc == -1)
  627. goto close;
  628. return 0;
  629. close:
  630. return -1;
  631. }
  632. int send_crafted_request(modbus_t *ctx, int function,
  633. uint8_t *req, int req_len,
  634. uint16_t max_value, uint16_t bytes,
  635. int backend_length, int backend_offset)
  636. {
  637. const int EXCEPTION_RC = 2;
  638. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  639. int j;
  640. uint32_t old_response_to_sec;
  641. uint32_t old_response_to_usec;
  642. /* This requests can generate flushes server side so we need a higher
  643. * response timeout than the server. The server uses the defined response
  644. * timeout to sleep before flushing.
  645. * The old timeouts are restored at the end.
  646. */
  647. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  648. modbus_set_response_timeout(ctx, 0, 600000);
  649. for (j=0; j<2; j++) {
  650. int rc;
  651. req[1] = function;
  652. if (j == 0) {
  653. /* Try to read or write zero values on first iteration */
  654. req[4] = 0x00;
  655. req[5] = 0x00;
  656. if (bytes) {
  657. /* Write query */
  658. req[6] = 0x00;
  659. }
  660. } else {
  661. /* Try to read or write max values + 1 on second iteration */
  662. req[4] = (max_value >> 8) & 0xFF;
  663. req[5] = max_value & 0xFF;
  664. if (bytes) {
  665. /* Write query (nb values * 2 to convert in bytes for registers) */
  666. req[6] = bytes;
  667. }
  668. }
  669. modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
  670. if (j == 0) {
  671. printf("* try function 0x%X: %s 0 values: ", function, bytes ? "write": "read");
  672. } else {
  673. printf("* try function 0x%X: %s %d values: ", function, bytes ? "write": "read",
  674. max_value);
  675. }
  676. rc = modbus_receive_confirmation(ctx, rsp);
  677. ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
  678. rsp[backend_offset] == (0x80 + function) &&
  679. rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
  680. }
  681. modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
  682. return 0;
  683. close:
  684. modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
  685. return -1;
  686. }