|
@@ -13,6 +13,8 @@
|
|
|
|
|
|
#include "unit-test.h"
|
|
|
|
|
|
+const int EXCEPTION_RC = 2;
|
|
|
+
|
|
|
enum {
|
|
|
TCP,
|
|
|
TCP_PI,
|
|
@@ -457,7 +459,7 @@ int main(int argc, char *argv[])
|
|
|
uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH];
|
|
|
|
|
|
|
|
|
- printf("1-A/4 No response from slave %d: ", INVALID_SERVER_ID);
|
|
|
+ printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID);
|
|
|
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
|
|
|
|
|
|
|
|
@@ -472,7 +474,7 @@ int main(int argc, char *argv[])
|
|
|
modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t));
|
|
|
rc = modbus_receive_confirmation(ctx, rsp);
|
|
|
|
|
|
- printf("1-B/4 No response from slave %d on indication/confirmation messages: ",
|
|
|
+ printf("1-B/3 No response from slave %d on indication/confirmation messages: ",
|
|
|
INVALID_SERVER_ID);
|
|
|
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
|
|
|
|
|
@@ -480,12 +482,12 @@ int main(int argc, char *argv[])
|
|
|
modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t));
|
|
|
rc = modbus_receive_confirmation(ctx, rsp);
|
|
|
|
|
|
- printf("1-C/4 No response from slave %d with invalid request: ",
|
|
|
+ printf("1-C/3 No response from slave %d with invalid request: ",
|
|
|
INVALID_SERVER_ID);
|
|
|
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
|
|
|
} else {
|
|
|
|
|
|
- printf("1/4 Response from slave %d: ", INVALID_SERVER_ID);
|
|
|
+ printf("1/3 Response from slave %d: ", INVALID_SERVER_ID);
|
|
|
ASSERT_TRUE(rc == UT_REGISTERS_NB, "");
|
|
|
}
|
|
|
|
|
@@ -494,25 +496,13 @@ int main(int argc, char *argv[])
|
|
|
|
|
|
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
|
|
|
UT_REGISTERS_NB, tab_rp_registers);
|
|
|
- printf("2/4 No reply after a broadcast query: ");
|
|
|
+ printf("2/3 No reply after a broadcast query: ");
|
|
|
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, "");
|
|
|
|
|
|
|
|
|
modbus_set_slave(ctx, use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE);
|
|
|
|
|
|
- {
|
|
|
- const int RAW_REQ_LENGTH = 6;
|
|
|
- uint8_t raw_req[] = { use_backend == RTU ? SERVER_ID : MODBUS_TCP_SLAVE, 0x42, 0x00, 0x00, 0x00, 0x00 };
|
|
|
- uint8_t rsp[MODBUS_MAX_ADU_LENGTH];
|
|
|
-
|
|
|
- rc = modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
|
|
|
- ASSERT_TRUE(rc != -1, "Unable to send raw request with invalid function code");
|
|
|
- rc = modbus_receive_confirmation(ctx, rsp);
|
|
|
- printf("3/4 Raise an exception on unknown function code: ");
|
|
|
- ASSERT_TRUE(rc == -1, "");
|
|
|
- }
|
|
|
-
|
|
|
- printf("4/4 Response with an invalid TID or slave: ");
|
|
|
+ printf("3/3 Response with an invalid TID or slave: ");
|
|
|
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE,
|
|
|
1, tab_rp_registers);
|
|
|
ASSERT_TRUE(rc == -1, "");
|
|
@@ -700,9 +690,9 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
int i;
|
|
|
|
|
|
const int READ_RAW_REQ_LEN = 6;
|
|
|
+ const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE;
|
|
|
uint8_t read_raw_req[] = {
|
|
|
-
|
|
|
- (use_backend == RTU) ? SERVER_ID : 0xFF,
|
|
|
+ slave,
|
|
|
|
|
|
MODBUS_FC_READ_HOLDING_REGISTERS,
|
|
|
UT_REGISTERS_ADDRESS >> 8, UT_REGISTERS_ADDRESS & 0xFF,
|
|
@@ -711,8 +701,7 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
|
|
|
const int RW_RAW_REQ_LEN = 13;
|
|
|
uint8_t rw_raw_req[] = {
|
|
|
-
|
|
|
- (use_backend == RTU) ? SERVER_ID : 0xFF,
|
|
|
+ slave,
|
|
|
|
|
|
MODBUS_FC_WRITE_AND_READ_REGISTERS,
|
|
|
|
|
@@ -729,8 +718,7 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
};
|
|
|
const int WRITE_RAW_REQ_LEN = 13;
|
|
|
uint8_t write_raw_req[] = {
|
|
|
-
|
|
|
- (use_backend == RTU) ? SERVER_ID : 0xFF,
|
|
|
+ slave,
|
|
|
|
|
|
MODBUS_FC_WRITE_MULTIPLE_REGISTERS,
|
|
|
|
|
@@ -740,6 +728,12 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
|
|
|
0x02, 0x2B, 0x00, 0x01, 0x00, 0x64
|
|
|
};
|
|
|
+ const int INVALID_FC = 0x42;
|
|
|
+ const int INVALID_FC_REQ_LEN = 6;
|
|
|
+ uint8_t invalid_fc_raw_req[] = {
|
|
|
+ slave, 0x42, 0x00, 0x00, 0x00, 0x00
|
|
|
+ };
|
|
|
+
|
|
|
int req_length;
|
|
|
uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
|
|
|
int tab_read_function[] = {
|
|
@@ -767,6 +761,17 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
|
|
|
printf("\nTEST RAW REQUESTS:\n");
|
|
|
|
|
|
+ uint32_t old_response_to_sec;
|
|
|
+ uint32_t old_response_to_usec;
|
|
|
+
|
|
|
+
|
|
|
+ * response timeout than the server. The server uses the defined response
|
|
|
+ * timeout to sleep before flushing.
|
|
|
+ * The old timeouts are restored at the end.
|
|
|
+ */
|
|
|
+ modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
|
|
|
+ modbus_set_response_timeout(ctx, 0, 600000);
|
|
|
+
|
|
|
req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN);
|
|
|
printf("* modbus_send_raw_request: ");
|
|
|
ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length);
|
|
@@ -810,8 +815,17 @@ int test_server(modbus_t *ctx, int use_backend)
|
|
|
if (rc == -1)
|
|
|
goto close;
|
|
|
|
|
|
+
|
|
|
+ modbus_send_raw_request(ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t));
|
|
|
+ rc = modbus_receive_confirmation(ctx, rsp);
|
|
|
+ printf("Return an exception on unknown function code: ");
|
|
|
+ ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) &&
|
|
|
+ rsp[backend_offset] == (0x80 + INVALID_FC), "")
|
|
|
+
|
|
|
+ modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
|
|
|
return 0;
|
|
|
close:
|
|
|
+ modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
@@ -821,19 +835,8 @@ int send_crafted_request(modbus_t *ctx, int function,
|
|
|
uint16_t max_value, uint16_t bytes,
|
|
|
int backend_length, int backend_offset)
|
|
|
{
|
|
|
- const int EXCEPTION_RC = 2;
|
|
|
uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH];
|
|
|
int j;
|
|
|
- uint32_t old_response_to_sec;
|
|
|
- uint32_t old_response_to_usec;
|
|
|
-
|
|
|
-
|
|
|
- * response timeout than the server. The server uses the defined response
|
|
|
- * timeout to sleep before flushing.
|
|
|
- * The old timeouts are restored at the end.
|
|
|
- */
|
|
|
- modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec);
|
|
|
- modbus_set_response_timeout(ctx, 0, 600000);
|
|
|
|
|
|
for (j=0; j<2; j++) {
|
|
|
int rc;
|
|
@@ -869,9 +872,7 @@ int send_crafted_request(modbus_t *ctx, int function,
|
|
|
rsp[backend_offset] == (0x80 + function) &&
|
|
|
rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, "");
|
|
|
}
|
|
|
- modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
|
|
|
return 0;
|
|
|
close:
|
|
|
- modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec);
|
|
|
return -1;
|
|
|
}
|