unit-test-client.c 29 KB

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