unit-test-client.c 29 KB

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