unit-test-client.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Copyright © 2008-2014 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the BSD License.
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <modbus.h>
  13. #include "unit-test.h"
  14. enum {
  15. TCP,
  16. TCP_PI,
  17. RTU
  18. };
  19. int test_server(modbus_t *ctx, int use_backend);
  20. int send_crafted_request(modbus_t *ctx, int function,
  21. uint8_t *req, int req_size,
  22. uint16_t max_value, uint16_t bytes,
  23. int backend_length, int backend_offset);
  24. #define BUG_REPORT(_cond, _format, _args ...) \
  25. printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args)
  26. #define ASSERT_TRUE(_cond, _format, __args...) { \
  27. if (_cond) { \
  28. printf("OK\n"); \
  29. } else { \
  30. BUG_REPORT(_cond, _format, ## __args); \
  31. goto close; \
  32. } \
  33. };
  34. int main(int argc, char *argv[])
  35. {
  36. const int NB_REPORT_SLAVE_ID = 10;
  37. uint8_t *tab_rp_bits = NULL;
  38. uint16_t *tab_rp_registers = NULL;
  39. uint16_t *tab_rp_registers_bad = NULL;
  40. modbus_t *ctx = NULL;
  41. int i;
  42. uint8_t value;
  43. int nb_points;
  44. int rc;
  45. float real;
  46. uint32_t ireal;
  47. uint32_t old_response_to_sec;
  48. uint32_t old_response_to_usec;
  49. uint32_t new_response_to_sec;
  50. uint32_t new_response_to_usec;
  51. uint32_t old_byte_to_sec;
  52. uint32_t old_byte_to_usec;
  53. int use_backend;
  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. printf("\nAt this point, error messages doesn't mean the test has failed\n");
  254. /** ILLEGAL DATA ADDRESS **/
  255. printf("\nTEST ILLEGAL DATA ADDRESS:\n");
  256. /* The mapping begins at 0 and ends at address + nb_points so
  257. * the addresses are not valid. */
  258. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits);
  259. printf("* modbus_read_bits: ");
  260. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  261. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  262. UT_INPUT_BITS_NB + 1, tab_rp_bits);
  263. printf("* modbus_read_input_bits: ");
  264. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  265. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  266. UT_REGISTERS_NB + 1, tab_rp_registers);
  267. printf("* modbus_read_registers: ");
  268. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  269. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  270. UT_INPUT_REGISTERS_NB + 1,
  271. tab_rp_registers);
  272. printf("* modbus_read_input_registers: ");
  273. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  274. rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON);
  275. printf("* modbus_write_bit: ");
  276. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  277. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB,
  278. UT_BITS_NB, tab_rp_bits);
  279. printf("* modbus_write_coils: ");
  280. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  281. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  282. UT_REGISTERS_NB, tab_rp_registers);
  283. printf("* modbus_write_registers: ");
  284. ASSERT_TRUE(rc == -1 && errno == EMBXILADD, "");
  285. /** TOO MANY DATA **/
  286. printf("\nTEST TOO MANY DATA ERROR:\n");
  287. rc = modbus_read_bits(ctx, UT_BITS_ADDRESS,
  288. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  289. printf("* modbus_read_bits: ");
  290. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  291. rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
  292. MODBUS_MAX_READ_BITS + 1, tab_rp_bits);
  293. printf("* modbus_read_input_bits: ");
  294. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  295. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  296. MODBUS_MAX_READ_REGISTERS + 1,
  297. tab_rp_registers);
  298. printf("* modbus_read_registers: ");
  299. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  300. rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
  301. MODBUS_MAX_READ_REGISTERS + 1,
  302. tab_rp_registers);
  303. printf("* modbus_read_input_registers: ");
  304. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  305. rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
  306. MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits);
  307. printf("* modbus_write_bits: ");
  308. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  309. rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
  310. MODBUS_MAX_WRITE_REGISTERS + 1,
  311. tab_rp_registers);
  312. printf("* modbus_write_registers: ");
  313. ASSERT_TRUE(rc == -1 && errno == EMBMDATA, "");
  314. /** SLAVE REPLY **/
  315. printf("\nTEST SLAVE REPLY:\n");
  316. modbus_set_slave(ctx, INVALID_SERVER_ID);
  317. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  318. UT_REGISTERS_NB, tab_rp_registers);
  319. if (use_backend == RTU) {
  320. const int RAW_REQ_LENGTH = 6;
  321. uint8_t raw_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01 };
  322. /* Too many points */
  323. uint8_t raw_invalid_req[] = { INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF };
  324. const int RAW_REP_LENGTH = 7;
  325. uint8_t raw_rep[] = { INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0 };
  326. uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
  327. /* No response in RTU mode */
  328. printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
  329. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  330. /* The slave raises a timeout on a confirmation to ignore because if an
  331. * indication for another slave is received, a confirmation must follow */
  332. /* Send a pair of indication/confirmation to the slave with a different
  333. * slave ID to simulate a communication on a RS485 bus. At first, the
  334. * slave will see the indication message then the confirmation, and it must
  335. * ignore both. */
  336. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  337. modbus_send_raw_request(ctx, raw_rep, RAW_REP_LENGTH * sizeof(uint8_t));
  338. rc = modbus_receive_confirmation(ctx, rsp);
  339. printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
  340. INVALID_SERVER_ID);
  341. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  342. /* Send an INVALID request for another slave */
  343. modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  344. rc = modbus_receive_confirmation(ctx, rsp);
  345. printf("1-C/3 No response from slave %d with invalid request: ",
  346. INVALID_SERVER_ID);
  347. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  348. } else {
  349. /* Response in TCP mode */
  350. printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
  351. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  352. }
  353. rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  354. ASSERT_TRUE(rc != -1, "Invalid broacast address");
  355. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  356. UT_REGISTERS_NB, tab_rp_registers);
  357. printf("2/3 Reply after a broadcast query: ");
  358. ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
  359. /* Restore slave */
  360. if (use_backend == RTU) {
  361. modbus_set_slave(ctx, SERVER_ID);
  362. } else {
  363. modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  364. }
  365. printf("3/3 Response with an invalid TID or slave: ");
  366. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
  367. 1, tab_rp_registers);
  368. ASSERT_TRUE(rc == -1, "");
  369. printf("1/2 Report slave ID truncated: \n");
  370. /* Set a marker to ensure limit is respected */
  371. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42;
  372. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits);
  373. /* Return the size required (response size) but respects the defined limit */
  374. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID &&
  375. tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42,
  376. "Return is rc %d (%d) and marker is %d (42)",
  377. rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]);
  378. printf("2/2 Report slave ID: \n");
  379. /* tab_rp_bits is used to store bytes */
  380. rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits);
  381. ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, "");
  382. /* Slave ID is an arbitraty number for libmodbus */
  383. ASSERT_TRUE(rc > 0, "");
  384. /* Run status indicator is ON */
  385. ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, "");
  386. /* Print additional data as string */
  387. if (rc > 2) {
  388. printf("Additional data: ");
  389. for (i=2; i < rc; i++) {
  390. printf("%c", tab_rp_bits[i]);
  391. }
  392. printf("\n");
  393. }
  394. /* Save original timeout */
  395. modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
  396. modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec);
  397. rc = modbus_set_response_timeout(ctx, 0, 0);
  398. printf("1/6 Invalid response timeout (zero): ");
  399. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  400. rc = modbus_set_response_timeout(ctx, 0, 1000000);
  401. printf("2/6 Invalid response timeout (too large us): ");
  402. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  403. rc = modbus_set_byte_timeout(ctx, 0, 1000000);
  404. printf("3/6 Invalid byte timeout (too large us): ");
  405. ASSERT_TRUE(rc == -1 && errno == EINVAL, "");
  406. modbus_set_response_timeout(ctx, 0, 1);
  407. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
  408. UT_REGISTERS_NB, tab_rp_registers);
  409. printf("4/6 1us response timeout: ");
  410. if (rc == -1 && errno == ETIMEDOUT) {
  411. printf("OK\n");
  412. } else {
  413. printf("FAILED (can fail on some platforms)\n");
  414. }
  415. /* A wait and flush operation is done by the error recovery code of
  416. * libmodbus but after a sleep of current response timeout
  417. * so 0 can be too short!
  418. */
  419. usleep(old_response_to_sec * 1000000 + old_response_to_usec);
  420. modbus_flush(ctx);
  421. /* Trigger a special behaviour on server to wait for 0.5 second before
  422. * replying whereas allowed timeout is 0.2 second */
  423. modbus_set_response_timeout(ctx, 0, 200000);
  424. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  425. 1, tab_rp_registers);
  426. printf("5/6 Too short response timeout (0.2s < 0.5s): ");
  427. ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
  428. /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */
  429. usleep(400000);
  430. modbus_flush(ctx);
  431. modbus_set_response_timeout(ctx, 0, 600000);
  432. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  433. 1, tab_rp_registers);
  434. printf("6/6 Adequate response timeout (0.6s > 0.5s): ");
  435. ASSERT_TRUE(rc == 1, "");
  436. /* Disable the byte timeout.
  437. The full response must be available in the 600ms interval */
  438. modbus_set_byte_timeout(ctx, 0, 0);
  439. rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS,
  440. 1, tab_rp_registers);
  441. printf("7/7 Disable byte timeout: ");
  442. ASSERT_TRUE(rc == 1, "");
  443. /* Restore original response timeout */
  444. modbus_set_response_timeout(ctx, old_response_to_sec,
  445. old_response_to_usec);
  446. if (use_backend == TCP) {
  447. /* Test server is only able to test byte timeout with the TCP 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 10ms 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, 0, 'A', 0, 0);
  489. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  490. ctx = modbus_new_tcp_pi(NULL, NULL);
  491. ASSERT_TRUE(ctx == NULL && errno == EINVAL, "");
  492. printf("\nALL TESTS PASS WITH SUCCESS.\n");
  493. close:
  494. /* Free the memory */
  495. free(tab_rp_bits);
  496. free(tab_rp_registers);
  497. /* Close the connection */
  498. modbus_close(ctx);
  499. modbus_free(ctx);
  500. return 0;
  501. }
  502. /* Send crafted requests to test server resilience
  503. and ensure proper exceptions are returned. */
  504. int test_server(modbus_t *ctx, int use_backend)
  505. {
  506. int rc;
  507. int i;
  508. /* Read requests */
  509. const int READ_RAW_REQ_LEN = 6;
  510. uint8_t read_raw_req[] = {
  511. /* slave */
  512. (use_backend == RTU) ? SERVER_ID : 0xFF,
  513. /* function, addr 1, 5 values */
  514. MODBUS_FC_READ_HOLDING_REGISTERS, 0x00, 0x01, 0x0, 0x05
  515. };
  516. /* Write and read registers request */
  517. const int RW_RAW_REQ_LEN = 13;
  518. uint8_t rw_raw_req[] = {
  519. /* slave */
  520. (use_backend == RTU) ? SERVER_ID : 0xFF,
  521. /* function, addr to read, nb to read */
  522. MODBUS_FC_WRITE_AND_READ_REGISTERS,
  523. /* Read */
  524. 0, 0,
  525. (MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8,
  526. (MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF,
  527. /* Write */
  528. 0, 0,
  529. 0, 1,
  530. /* Write byte count */
  531. 1 * 2,
  532. /* One data to write... */
  533. 0x12, 0x34
  534. };
  535. const int WRITE_RAW_REQ_LEN = 13;
  536. uint8_t write_raw_req[] = {
  537. /* slave */
  538. (use_backend == RTU) ? SERVER_ID : 0xFF,
  539. /* function will be set in the loop */
  540. MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  541. /* Address */
  542. UT_REGISTERS_ADDRESS >> 8,
  543. UT_REGISTERS_ADDRESS & 0xFF,
  544. /* 3 values, 6 bytes */
  545. 0x00, 0x03, 0x06,
  546. /* Dummy data to write */
  547. 0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
  548. };
  549. int req_length;
  550. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  551. int tab_read_function[] = {
  552. MODBUS_FC_READ_COILS,
  553. MODBUS_FC_READ_DISCRETE_INPUTS,
  554. MODBUS_FC_READ_HOLDING_REGISTERS,
  555. MODBUS_FC_READ_INPUT_REGISTERS
  556. };
  557. int tab_read_nb_max[] = {
  558. MODBUS_MAX_READ_BITS + 1,
  559. MODBUS_MAX_READ_BITS + 1,
  560. MODBUS_MAX_READ_REGISTERS + 1,
  561. MODBUS_MAX_READ_REGISTERS + 1
  562. };
  563. int backend_length;
  564. int backend_offset;
  565. if (use_backend == RTU) {
  566. backend_length = 3;
  567. backend_offset = 1;
  568. } else {
  569. backend_length = 7;
  570. backend_offset = 7;
  571. }
  572. printf("\nTEST RAW REQUESTS:\n");
  573. req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
  574. printf("* modbus_send_raw_request: ");
  575. ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
  576. printf("* modbus_receive_confirmation: ");
  577. rc = modbus_receive_confirmation(ctx, rsp);
  578. ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc);
  579. /* Try to read more values than a response could hold for all data
  580. * types.
  581. */
  582. for (i=0; i<4; i++) {
  583. rc = send_crafted_request(ctx, tab_read_function[i],
  584. read_raw_req, READ_RAW_REQ_LEN,
  585. tab_read_nb_max[i], 0,
  586. backend_length, backend_offset);
  587. if (rc == -1)
  588. goto close;
  589. }
  590. /* Modbus write and read multiple registers */
  591. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_AND_READ_REGISTERS,
  592. rw_raw_req, RW_RAW_REQ_LEN,
  593. MODBUS_MAX_WR_READ_REGISTERS + 1, 0,
  594. backend_length, backend_offset);
  595. if (rc == -1)
  596. goto close;
  597. /* Modbus write multiple registers with large number of values but a set a
  598. small number of bytes in requests (not nb * 2 as usual). */
  599. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
  600. write_raw_req, WRITE_RAW_REQ_LEN,
  601. MODBUS_MAX_WRITE_REGISTERS + 1, 6,
  602. backend_length, backend_offset);
  603. if (rc == -1)
  604. goto close;
  605. rc = send_crafted_request(ctx, MODBUS_FC_WRITE_MULTIPLE_COILS,
  606. write_raw_req, WRITE_RAW_REQ_LEN,
  607. MODBUS_MAX_WRITE_BITS + 1, 6,
  608. backend_length, backend_offset);
  609. if (rc == -1)
  610. goto close;
  611. return 0;
  612. close:
  613. return -1;
  614. }
  615. int send_crafted_request(modbus_t *ctx, int function,
  616. uint8_t *req, int req_len,
  617. uint16_t max_value, uint16_t bytes,
  618. int backend_length, int backend_offset)
  619. {
  620. const int EXCEPTION_RC = 2;
  621. uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
  622. for (int j=0; j<2; j++) {
  623. int rc;
  624. req[1] = function;
  625. if (j == 0) {
  626. /* Try to read or write zero values on first iteration */
  627. req[4] = 0x00;
  628. req[5] = 0x00;
  629. if (bytes) {
  630. /* Write query */
  631. req[6] = 0x00;
  632. }
  633. } else {
  634. /* Try to read or write max values + 1 on second iteration */
  635. req[4] = (max_value >> 8) & 0xFF;
  636. req[5] = max_value & 0xFF;
  637. if (bytes) {
  638. /* Write query (nb values * 2 to convert in bytes for registers) */
  639. req[6] = bytes;
  640. }
  641. }
  642. modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t));
  643. if (j == 0) {
  644. printf("* try function 0x%X: %s 0 values: ", function, bytes ? "write": "read");
  645. } else {
  646. printf("* try function 0x%X: %s %d values: ", function, bytes ? "write": "read",
  647. max_value);
  648. }
  649. rc = modbus_receive_confirmation(ctx, rsp);
  650. ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
  651. rsp[backend_offset] == (0x80 + function) &&
  652. rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
  653. }
  654. return 0;
  655. close:
  656. return -1;
  657. }