Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,21 @@ static int check_confirmation(modbus_t *ctx, uint8_t *req, uint8_t *rsp, int rsp
/* 1 Write functions & others */
req_nb_value = rsp_nb_value = 1;
break;
case MODBUS_FC_MASK_WRITE_REGISTER:
/* address in request and response must be equal */
if ((req[offset + 1] != rsp[offset + 1]) ||
(req[offset + 2] != rsp[offset + 2])) {
resp_addr_ok = FALSE;
}
/* AND and OR masks in request and response must be equal */
if ((req[offset + 3] != rsp[offset + 3]) ||
(req[offset + 4] != rsp[offset + 4]) ||
(req[offset + 5] != rsp[offset + 5]) ||
(req[offset + 6] != rsp[offset + 6])) {
resp_data_ok = FALSE;
}
req_nb_value = rsp_nb_value = 1;
break;
default:
/* 1 Write functions & others */
req_nb_value = rsp_nb_value = 1;
Expand Down
7 changes: 6 additions & 1 deletion tests/unit-test-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,19 @@ int main(int argc, char *argv[])
}

/* MASKS */
printf("1/1 Write mask: ");
printf("1/2 Write mask: ");
rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x12);
rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25);
ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc);
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers);
ASSERT_TRUE(
tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17);

printf("2/2 Write mask with invalid echoed address and masks: ");
rc = modbus_mask_write_register(
ctx, UT_REGISTERS_ADDRESS_INVALID_MASK_ECHO, 0xF2, 0x25);
ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, "FAILED (rc %d)\n", rc);

printf("\nTEST FLOATS\n");
/** FLOAT **/
printf("1/4 Set/get float ABCD: ");
Expand Down
23 changes: 23 additions & 0 deletions tests/unit-test-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,29 @@ int main(int argc, char *argv[])
"value (%d) in modbus_reply\n",
rc);
}
} else if (function == MODBUS_FC_MASK_WRITE_REGISTER) {
if (address == UT_REGISTERS_ADDRESS_INVALID_MASK_ECHO) {
const int RAW_RSP_LENGTH = 8;
uint8_t raw_rsp[] = {(use_backend == RTU) ? SERVER_ID : 0xFF,
MODBUS_FC_MASK_WRITE_REGISTER,
/* Another address */
0x00,
0x00,
/* Another AND mask */
0xFF,
0xFF,
/* Another OR mask */
0x00,
0x00};

printf("Reply with invalid echoed address and masks\n");
/* The transaction ID is only used by the TCP backends */
modbus_send_raw_request_tid(ctx,
raw_rsp,
RAW_RSP_LENGTH * sizeof(uint8_t),
MODBUS_GET_INT16_FROM_INT8(query, 0));
continue;
}
}

rc = modbus_reply(ctx, query, rc, mb_mapping);
Expand Down
3 changes: 3 additions & 0 deletions tests/unit-test.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171;
const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172;
/* The server will wait for 5 ms before sending each byte */
const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173;
/* The server replies to a mask write register request on this address with a
confirmation which echoes another address and other masks */
const uint16_t UT_REGISTERS_ADDRESS_INVALID_MASK_ECHO = 0x174;

/* If the following value is used, a bad response is sent.
It's better to test with a lower value than
Expand Down