Commit 0f28f3c303cde0d92091ee571c5f807197b8328f

Authored by Stéphane Raimbault
1 parent 73a88a74

Add debug message on unknown function and new unit test

src/modbus.c
... ... @@ -1027,7 +1027,8 @@ int modbus_reply(modbus_t *ctx, const uint8_t *req,
1027 1027  
1028 1028 default:
1029 1029 rsp_length = response_exception(
1030   - ctx, &sft, MODBUS_EXCEPTION_ILLEGAL_FUNCTION, rsp, FALSE, "");
  1030 + ctx, &sft, MODBUS_EXCEPTION_ILLEGAL_FUNCTION, rsp, FALSE,
  1031 + "Unknown Modbus function code: 0x%0X\n", function);
1031 1032 break;
1032 1033 }
1033 1034  
... ...
tests/unit-test-client.c
... ... @@ -457,7 +457,7 @@ int main(int argc, char *argv[])
457 457 uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
458 458  
459 459 /* No response in RTU mode */
460   - printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
  460 + printf("1-A/4 No response from slave %d: ", INVALID_SERVER_ID);
461 461 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
462 462  
463 463 /* The slave raises a timeout on a confirmation to ignore because if an
... ... @@ -472,7 +472,7 @@ int main(int argc, char *argv[])
472 472 modbus_send_raw_request(ctx, raw_rep, RAW_REP_LENGTH * sizeof(uint8_t));
473 473 rc = modbus_receive_confirmation(ctx, rsp);
474 474  
475   - printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
  475 + printf("1-B/4 No response from slave %d on indication/confirmation messages: ",
476 476 INVALID_SERVER_ID);
477 477 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
478 478  
... ... @@ -480,12 +480,12 @@ int main(int argc, char *argv[])
480 480 modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
481 481 rc = modbus_receive_confirmation(ctx, rsp);
482 482  
483   - printf("1-C/3 No response from slave %d with invalid request: ",
  483 + printf("1-C/4 No response from slave %d with invalid request: ",
484 484 INVALID_SERVER_ID);
485 485 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
486 486 } else {
487 487 /* Response in TCP mode */
488   - printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
  488 + printf("1/4 Response from slave %d: ", INVALID_SERVER_ID);
489 489 ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
490 490 }
491 491  
... ... @@ -494,17 +494,25 @@ int main(int argc, char *argv[])
494 494  
495 495 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
496 496 UT_REGISTERS_NB, tab_rp_registers);
497   - printf("2/3 No reply after a broadcast query: ");
  497 + printf("2/4 No reply after a broadcast query: ");
498 498 ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
499 499  
500 500 /* Restore slave */
501   - if (use_backend == RTU) {
502   - modbus_set_slave(ctx, SERVER_ID);
503   - } else {
504   - modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
  501 + modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE);
  502 +
  503 + {
  504 + const int RAW_REQ_LENGTH = 6;
  505 + uint8_t raw_req[] = { use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE, 0x42, 0x00, 0x00, 0x00, 0x00 };
  506 + uint8_t rsp[MODBUS_MAX_ADU_LENGTH];
  507 +
  508 + rc = modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  509 + ASSERT_TRUE(rc != -1, "Unable to send raw request with invalid function code");
  510 + rc = modbus_receive_confirmation(ctx, rsp);
  511 + printf("3/4 Raise an exception on unknown function code: ");
  512 + ASSERT_TRUE(rc == -1, "");
505 513 }
506 514  
507   - printf("3/3 Response with an invalid TID or slave: ");
  515 + printf("4/4 Response with an invalid TID or slave: ");
508 516 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
509 517 1, tab_rp_registers);
510 518 ASSERT_TRUE(rc == -1, "");
... ...