| 1 |
/* |
| 2 |
* Copyright © Stéphane Raimbault <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <errno.h> |
| 8 |
#include <modbus.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "unit-test.h" |
| 15 |
|
| 16 |
const int EXCEPTION_RC = 2; |
| 17 |
|
| 18 |
enum { |
| 19 |
TCP, |
| 20 |
TCP_PI, |
| 21 |
RTU |
| 22 |
}; |
| 23 |
|
| 24 |
int test_server(modbus_t *ctx, int use_backend); |
| 25 |
int send_crafted_request(modbus_t *ctx, |
| 26 |
int function, |
| 27 |
uint8_t *req, |
| 28 |
int req_size, |
| 29 |
uint16_t max_value, |
| 30 |
uint16_t bytes, |
| 31 |
int backend_length, |
| 32 |
int backend_offset); |
| 33 |
int equal_dword(uint16_t *tab_reg, const uint32_t value); |
| 34 |
int is_memory_equal(const void *s1, const void *s2, size_t size); |
| 35 |
|
| 36 |
#define BUG_REPORT(_cond, _format, _args...) \ |
| 37 |
printf( \ |
| 38 |
"\nLine %d: assertion error for '%s': " _format "\n", __LINE__, #_cond, ##_args) |
| 39 |
|
| 40 |
#define ASSERT_TRUE(_cond, _format, __args...) \ |
| 41 |
{ \ |
| 42 |
if (_cond) { \ |
| 43 |
printf("OK\n"); \ |
| 44 |
} else { \ |
| 45 |
BUG_REPORT(_cond, _format, ##__args); \ |
| 46 |
goto close; \ |
| 47 |
} \ |
| 48 |
}; |
| 49 |
|
| 50 |
int is_memory_equal(const void *s1, const void *s2, size_t size) |
| 51 |
{ |
| 52 |
return (memcmp(s1, s2, size) == 0); |
| 53 |
} |
| 54 |
|
| 55 |
int equal_dword(uint16_t *tab_reg, const uint32_t value) |
| 56 |
{ |
| 57 |
return ((tab_reg[0] == (value >> 16)) && (tab_reg[1] == (value & 0xFFFF))); |
| 58 |
} |
| 59 |
|
| 60 |
int main(int argc, char *argv[]) |
| 61 |
{ |
| 62 |
/* Length of report slave ID response slave ID + ON/OFF + 'LMB' + version */ |
| 63 |
const int NB_REPORT_SLAVE_ID = 2 + 3 + strlen(LIBMODBUS_VERSION_STRING); |
| 64 |
uint8_t *tab_rp_bits = NULL; |
| 65 |
uint16_t *tab_rp_registers = NULL; |
| 66 |
uint16_t *tab_rp_registers_bad = NULL; |
| 67 |
modbus_t *ctx = NULL; |
| 68 |
int i; |
| 69 |
uint8_t value; |
| 70 |
int nb_points; |
| 71 |
int rc; |
| 72 |
float real; |
| 73 |
uint32_t old_response_to_sec; |
| 74 |
uint32_t old_response_to_usec; |
| 75 |
uint32_t new_response_to_sec; |
| 76 |
uint32_t new_response_to_usec; |
| 77 |
uint32_t old_byte_to_sec; |
| 78 |
uint32_t old_byte_to_usec; |
| 79 |
int use_backend; |
| 80 |
int success = FALSE; |
| 81 |
int old_slave; |
| 82 |
char *ip_or_device; |
| 83 |
|
| 84 |
if (argc > 1) { |
| 85 |
if (strcmp(argv[1], "tcp") == 0) { |
| 86 |
use_backend = TCP; |
| 87 |
} else if (strcmp(argv[1], "tcppi") == 0) { |
| 88 |
use_backend = TCP_PI; |
| 89 |
} else if (strcmp(argv[1], "rtu") == 0) { |
| 90 |
use_backend = RTU; |
| 91 |
} else { |
| 92 |
printf("Modbus client for unit testing\n"); |
| 93 |
printf("Usage:\n %s [tcp|tcppi|rtu]\n", argv[0]); |
| 94 |
printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB1\n\n"); |
| 95 |
exit(1); |
| 96 |
} |
| 97 |
} else { |
| 98 |
/* By default */ |
| 99 |
use_backend = TCP; |
| 100 |
} |
| 101 |
|
| 102 |
if (argc > 2) { |
| 103 |
ip_or_device = argv[2]; |
| 104 |
} else { |
| 105 |
switch (use_backend) { |
| 106 |
case TCP: |
| 107 |
ip_or_device = "127.0.0.1"; |
| 108 |
break; |
| 109 |
case TCP_PI: |
| 110 |
ip_or_device = "::1"; |
| 111 |
break; |
| 112 |
case RTU: |
| 113 |
ip_or_device = "/dev/ttyUSB1"; |
| 114 |
break; |
| 115 |
default: |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (use_backend == TCP) { |
| 121 |
ctx = modbus_new_tcp(ip_or_device, 1502); |
| 122 |
} else if (use_backend == TCP_PI) { |
| 123 |
ctx = modbus_new_tcp_pi(ip_or_device, "1502"); |
| 124 |
} else { |
| 125 |
ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1); |
| 126 |
} |
| 127 |
if (ctx == NULL) { |
| 128 |
fprintf(stderr, "Unable to allocate libmodbus context\n"); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
modbus_set_debug(ctx, TRUE); |
| 133 |
modbus_set_error_recovery( |
| 134 |
ctx, MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL); |
| 135 |
|
| 136 |
if (use_backend == RTU) { |
| 137 |
modbus_set_slave(ctx, SERVER_ID); |
| 138 |
} |
| 139 |
|
| 140 |
modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); |
| 141 |
if (modbus_connect(ctx) == -1) { |
| 142 |
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); |
| 143 |
modbus_free(ctx); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
/* Allocate and initialize the memory to store the bits */ |
| 148 |
nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB; |
| 149 |
tab_rp_bits = (uint8_t *) malloc(nb_points * sizeof(uint8_t)); |
| 150 |
memset(tab_rp_bits, 0, nb_points * sizeof(uint8_t)); |
| 151 |
|
| 152 |
/* Allocate and initialize the memory to store the registers */ |
| 153 |
nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB |
| 154 |
: UT_INPUT_REGISTERS_NB; |
| 155 |
tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); |
| 156 |
memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); |
| 157 |
|
| 158 |
printf("** UNIT TESTING **\n"); |
| 159 |
|
| 160 |
printf("1/1 No response timeout modification on connect: "); |
| 161 |
modbus_get_response_timeout(ctx, &new_response_to_sec, &new_response_to_usec); |
| 162 |
ASSERT_TRUE(old_response_to_sec == new_response_to_sec && |
| 163 |
old_response_to_usec == new_response_to_usec, |
| 164 |
""); |
| 165 |
|
| 166 |
printf("\nTEST WRITE/READ:\n"); |
| 167 |
|
| 168 |
/** COIL BITS **/ |
| 169 |
|
| 170 |
/* Single */ |
| 171 |
rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON); |
| 172 |
printf("1/2 modbus_write_bit: "); |
| 173 |
ASSERT_TRUE(rc == 1, ""); |
| 174 |
|
| 175 |
rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits); |
| 176 |
printf("2/2 modbus_read_bits: "); |
| 177 |
ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); |
| 178 |
ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n", tab_rp_bits[0], ON); |
| 179 |
|
| 180 |
/* End single */ |
| 181 |
|
| 182 |
/* Multiple bits */ |
| 183 |
{ |
| 184 |
uint8_t tab_value[UT_BITS_NB]; |
| 185 |
|
| 186 |
modbus_set_bits_from_bytes(tab_value, 0, UT_BITS_NB, UT_BITS_TAB); |
| 187 |
rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_value); |
| 188 |
printf("1/2 modbus_write_bits: "); |
| 189 |
ASSERT_TRUE(rc == UT_BITS_NB, ""); |
| 190 |
} |
| 191 |
|
| 192 |
rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits); |
| 193 |
printf("2/2 modbus_read_bits: "); |
| 194 |
ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc); |
| 195 |
|
| 196 |
i = 0; |
| 197 |
nb_points = UT_BITS_NB; |
| 198 |
while (nb_points > 0) { |
| 199 |
int nb_bits = (nb_points > 8) ? 8 : nb_points; |
| 200 |
|
| 201 |
value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits); |
| 202 |
ASSERT_TRUE( |
| 203 |
value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]); |
| 204 |
|
| 205 |
nb_points -= nb_bits; |
| 206 |
i++; |
| 207 |
} |
| 208 |
printf("OK\n"); |
| 209 |
/* End of multiple bits */ |
| 210 |
|
| 211 |
/** DISCRETE INPUTS **/ |
| 212 |
rc = |
| 213 |
modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, tab_rp_bits); |
| 214 |
printf("1/1 modbus_read_input_bits: "); |
| 215 |
ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc); |
| 216 |
|
| 217 |
i = 0; |
| 218 |
nb_points = UT_INPUT_BITS_NB; |
| 219 |
while (nb_points > 0) { |
| 220 |
int nb_bits = (nb_points > 8) ? 8 : nb_points; |
| 221 |
value = modbus_get_byte_from_bits(tab_rp_bits, i * 8, nb_bits); |
| 222 |
ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], |
| 223 |
"FAILED (%0X != %0X)\n", |
| 224 |
value, |
| 225 |
UT_INPUT_BITS_TAB[i]); |
| 226 |
|
| 227 |
nb_points -= nb_bits; |
| 228 |
i++; |
| 229 |
} |
| 230 |
printf("OK\n"); |
| 231 |
|
| 232 |
/** HOLDING REGISTERS **/ |
| 233 |
|
| 234 |
/* Single register */ |
| 235 |
rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234); |
| 236 |
printf("1/2 modbus_write_register: "); |
| 237 |
ASSERT_TRUE(rc == 1, ""); |
| 238 |
|
| 239 |
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers); |
| 240 |
printf("2/2 modbus_read_registers: "); |
| 241 |
ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); |
| 242 |
ASSERT_TRUE(tab_rp_registers[0] == 0x1234, |
| 243 |
"FAILED (%0X != %0X)\n", |
| 244 |
tab_rp_registers[0], |
| 245 |
0x1234); |
| 246 |
/* End of single register */ |
| 247 |
|
| 248 |
/* Many registers */ |
| 249 |
rc = modbus_write_registers( |
| 250 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, UT_REGISTERS_TAB); |
| 251 |
printf("1/5 modbus_write_registers: "); |
| 252 |
ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 253 |
|
| 254 |
rc = modbus_read_registers( |
| 255 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); |
| 256 |
printf("2/5 modbus_read_registers: "); |
| 257 |
ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); |
| 258 |
|
| 259 |
for (i = 0; i < UT_REGISTERS_NB; i++) { |
| 260 |
ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i], |
| 261 |
"FAILED (%0X != %0X)\n", |
| 262 |
tab_rp_registers[i], |
| 263 |
UT_REGISTERS_TAB[i]); |
| 264 |
} |
| 265 |
|
| 266 |
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 0, tab_rp_registers); |
| 267 |
printf("3/5 modbus_read_registers (0): "); |
| 268 |
ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc); |
| 269 |
|
| 270 |
nb_points = (UT_REGISTERS_NB > UT_INPUT_REGISTERS_NB) ? UT_REGISTERS_NB |
| 271 |
: UT_INPUT_REGISTERS_NB; |
| 272 |
memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t)); |
| 273 |
|
| 274 |
/* Write registers to zero from tab_rp_registers and store read registers |
| 275 |
into tab_rp_registers. So the read registers must set to 0, except the |
| 276 |
first one because there is an offset of 1 register on write. */ |
| 277 |
rc = modbus_write_and_read_registers(ctx, |
| 278 |
UT_REGISTERS_ADDRESS + 1, |
| 279 |
UT_REGISTERS_NB - 1, |
| 280 |
tab_rp_registers, |
| 281 |
UT_REGISTERS_ADDRESS, |
| 282 |
UT_REGISTERS_NB, |
| 283 |
tab_rp_registers); |
| 284 |
printf("4/5 modbus_write_and_read_registers: "); |
| 285 |
ASSERT_TRUE( |
| 286 |
rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB); |
| 287 |
|
| 288 |
ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0], |
| 289 |
"FAILED (%0X != %0X)\n", |
| 290 |
tab_rp_registers[0], |
| 291 |
UT_REGISTERS_TAB[0]); |
| 292 |
|
| 293 |
for (i = 1; i < UT_REGISTERS_NB; i++) { |
| 294 |
ASSERT_TRUE( |
| 295 |
tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", tab_rp_registers[i], 0); |
| 296 |
} |
| 297 |
|
| 298 |
/* End of many registers */ |
| 299 |
|
| 300 |
/** INPUT REGISTERS **/ |
| 301 |
rc = modbus_read_input_registers( |
| 302 |
ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB, tab_rp_registers); |
| 303 |
printf("1/1 modbus_read_input_registers: "); |
| 304 |
ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); |
| 305 |
|
| 306 |
for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) { |
| 307 |
ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i], |
| 308 |
"FAILED (%0X != %0X)\n", |
| 309 |
tab_rp_registers[i], |
| 310 |
UT_INPUT_REGISTERS_TAB[i]); |
| 311 |
} |
| 312 |
|
| 313 |
/* MASKS */ |
| 314 |
printf("1/1 Write mask: "); |
| 315 |
rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x12); |
| 316 |
rc = modbus_mask_write_register(ctx, UT_REGISTERS_ADDRESS, 0xF2, 0x25); |
| 317 |
ASSERT_TRUE(rc != -1, "FAILED (%x == -1)\n", rc); |
| 318 |
rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 1, tab_rp_registers); |
| 319 |
ASSERT_TRUE( |
| 320 |
tab_rp_registers[0] == 0x17, "FAILED (%0X != %0X)\n", tab_rp_registers[0], 0x17); |
| 321 |
|
| 322 |
printf("\nTEST FLOATS\n"); |
| 323 |
/** FLOAT **/ |
| 324 |
printf("1/4 Set/get float ABCD: "); |
| 325 |
modbus_set_float_abcd(UT_REAL, tab_rp_registers); |
| 326 |
ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_ABCD, 4), |
| 327 |
"FAILED Set float ABCD"); |
| 328 |
real = modbus_get_float_abcd(UT_IREAL_ABCD); |
| 329 |
ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 330 |
|
| 331 |
printf("2/4 Set/get float DCBA: "); |
| 332 |
modbus_set_float_dcba(UT_REAL, tab_rp_registers); |
| 333 |
ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_DCBA, 4), |
| 334 |
"FAILED Set float DCBA"); |
| 335 |
real = modbus_get_float_dcba(UT_IREAL_DCBA); |
| 336 |
ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 337 |
|
| 338 |
printf("3/4 Set/get float BADC: "); |
| 339 |
modbus_set_float_badc(UT_REAL, tab_rp_registers); |
| 340 |
ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_BADC, 4), |
| 341 |
"FAILED Set float BADC"); |
| 342 |
real = modbus_get_float_badc(UT_IREAL_BADC); |
| 343 |
ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 344 |
|
| 345 |
printf("4/4 Set/get float CDAB: "); |
| 346 |
modbus_set_float_cdab(UT_REAL, tab_rp_registers); |
| 347 |
ASSERT_TRUE(is_memory_equal(tab_rp_registers, UT_IREAL_CDAB, 4), |
| 348 |
"FAILED Set float CDAB"); |
| 349 |
real = modbus_get_float_cdab(UT_IREAL_CDAB); |
| 350 |
ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 351 |
|
| 352 |
/* Regression: a negative float has its most significant byte >= 0x80. |
| 353 |
On the get side the "a << 24" byte assembly overflows a signed int |
| 354 |
(undefined behavior) unless done in unsigned width; on the set side the |
| 355 |
float must be serialized with memcpy, not a float->uint32_t type-pun |
| 356 |
(strict-aliasing UB). UT_REAL is positive (0x47F12000) and misses both |
| 357 |
paths; -UT_REAL (0xC7F12000) exercises them. Run under |
| 358 |
-fsanitize=undefined to catch the get-side UB. */ |
| 359 |
printf("Set/get negative float ABCD/DCBA/BADC/CDAB: "); |
| 360 |
{ |
| 361 |
const uint16_t neg_abcd[] = {0xC7F1, 0x2000}; |
| 362 |
const uint16_t neg_dcba[] = {0x0020, 0xF1C7}; |
| 363 |
const uint16_t neg_badc[] = {0xF1C7, 0x0020}; |
| 364 |
const uint16_t neg_cdab[] = {0x2000, 0xC7F1}; |
| 365 |
uint16_t out[2]; |
| 366 |
|
| 367 |
modbus_set_float_abcd(-UT_REAL, out); |
| 368 |
ASSERT_TRUE(is_memory_equal(out, neg_abcd, 4) && |
| 369 |
modbus_get_float_abcd(neg_abcd) == -UT_REAL, |
| 370 |
"FAILED negative float ABCD\n"); |
| 371 |
modbus_set_float_dcba(-UT_REAL, out); |
| 372 |
ASSERT_TRUE(is_memory_equal(out, neg_dcba, 4) && |
| 373 |
modbus_get_float_dcba(neg_dcba) == -UT_REAL, |
| 374 |
"FAILED negative float DCBA\n"); |
| 375 |
modbus_set_float_badc(-UT_REAL, out); |
| 376 |
ASSERT_TRUE(is_memory_equal(out, neg_badc, 4) && |
| 377 |
modbus_get_float_badc(neg_badc) == -UT_REAL, |
| 378 |
"FAILED negative float BADC\n"); |
| 379 |
modbus_set_float_cdab(-UT_REAL, out); |
| 380 |
ASSERT_TRUE(is_memory_equal(out, neg_cdab, 4) && |
| 381 |
modbus_get_float_cdab(neg_cdab) == -UT_REAL, |
| 382 |
"FAILED negative float CDAB\n"); |
| 383 |
} |
| 384 |
|
| 385 |
/* Regression: the MODBUS_GET_INT*_FROM_INT* macros must shift in unsigned |
| 386 |
width. A high word/byte with the top bit set makes the old signed shift |
| 387 |
overflow (undefined behavior) and, for INT16, produce a sign-incorrect |
| 388 |
result. */ |
| 389 |
printf("Get signed int from registers INT16/INT32/INT64: "); |
| 390 |
{ |
| 391 |
const uint8_t r8[] = {0xC7, 0xF1}; |
| 392 |
const uint16_t r16[] = {0xC7F1, 0x2000}; |
| 393 |
const uint16_t r64[] = {0xD200, 0x1234, 0x5678, 0x9ABC}; |
| 394 |
|
| 395 |
ASSERT_TRUE(MODBUS_GET_INT16_FROM_INT8(r8, 0) == (int16_t) 0xC7F1 && |
| 396 |
MODBUS_GET_INT32_FROM_INT16(r16, 0) == (int32_t) 0xC7F12000 && |
| 397 |
MODBUS_GET_INT64_FROM_INT16(r64, 0) == |
| 398 |
(int64_t) 0xD200123456789ABCULL, |
| 399 |
"FAILED signed int conversion\n"); |
| 400 |
} |
| 401 |
|
| 402 |
printf("\nAt this point, error messages doesn't mean the test has failed\n"); |
| 403 |
|
| 404 |
/** ILLEGAL DATA ADDRESS **/ |
| 405 |
printf("\nTEST ILLEGAL DATA ADDRESS:\n"); |
| 406 |
|
| 407 |
/* The mapping begins at the defined addresses and ends at address + |
| 408 |
* nb_points so these addresses are not valid. */ |
| 409 |
|
| 410 |
rc = modbus_read_bits(ctx, 0, 1, tab_rp_bits); |
| 411 |
printf("* modbus_read_bits (0): "); |
| 412 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 413 |
|
| 414 |
rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits); |
| 415 |
printf("* modbus_read_bits (max): "); |
| 416 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 417 |
|
| 418 |
rc = modbus_read_input_bits(ctx, 0, 1, tab_rp_bits); |
| 419 |
printf("* modbus_read_input_bits (0): "); |
| 420 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 421 |
|
| 422 |
rc = modbus_read_input_bits( |
| 423 |
ctx, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB + 1, tab_rp_bits); |
| 424 |
printf("* modbus_read_input_bits (max): "); |
| 425 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 426 |
|
| 427 |
rc = modbus_read_registers(ctx, 0, 1, tab_rp_registers); |
| 428 |
printf("* modbus_read_registers (0): "); |
| 429 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 430 |
|
| 431 |
rc = modbus_read_registers( |
| 432 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX + 1, tab_rp_registers); |
| 433 |
printf("* modbus_read_registers (max): "); |
| 434 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 435 |
|
| 436 |
rc = modbus_read_input_registers(ctx, 0, 1, tab_rp_registers); |
| 437 |
printf("* modbus_read_input_registers (0): "); |
| 438 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 439 |
|
| 440 |
rc = modbus_read_input_registers( |
| 441 |
ctx, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB + 1, tab_rp_registers); |
| 442 |
printf("* modbus_read_input_registers (max): "); |
| 443 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 444 |
|
| 445 |
rc = modbus_write_bit(ctx, 0, ON); |
| 446 |
printf("* modbus_write_bit (0): "); |
| 447 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 448 |
|
| 449 |
rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON); |
| 450 |
printf("* modbus_write_bit (max): "); |
| 451 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 452 |
|
| 453 |
rc = modbus_write_bits(ctx, 0, 1, tab_rp_bits); |
| 454 |
printf("* modbus_write_bits (0): "); |
| 455 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 456 |
|
| 457 |
rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, UT_BITS_NB, tab_rp_bits); |
| 458 |
printf("* modbus_write_bits (max): "); |
| 459 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 460 |
|
| 461 |
rc = modbus_write_register(ctx, 0, tab_rp_registers[0]); |
| 462 |
printf("* modbus_write_register (0): "); |
| 463 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 464 |
|
| 465 |
rc = modbus_write_register( |
| 466 |
ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, tab_rp_registers[0]); |
| 467 |
printf("* modbus_write_register (max): "); |
| 468 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 469 |
|
| 470 |
rc = modbus_write_registers(ctx, 0, 1, tab_rp_registers); |
| 471 |
printf("* modbus_write_registers (0): "); |
| 472 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 473 |
|
| 474 |
rc = modbus_write_registers(ctx, |
| 475 |
UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, |
| 476 |
UT_REGISTERS_NB, |
| 477 |
tab_rp_registers); |
| 478 |
printf("* modbus_write_registers (max): "); |
| 479 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 480 |
|
| 481 |
rc = modbus_mask_write_register(ctx, 0, 0xF2, 0x25); |
| 482 |
printf("* modbus_mask_write_registers (0): "); |
| 483 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 484 |
|
| 485 |
rc = modbus_mask_write_register( |
| 486 |
ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, 0xF2, 0x25); |
| 487 |
printf("* modbus_mask_write_registers (max): "); |
| 488 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 489 |
|
| 490 |
rc = modbus_write_and_read_registers( |
| 491 |
ctx, 0, 1, tab_rp_registers, 0, 1, tab_rp_registers); |
| 492 |
printf("* modbus_write_and_read_registers (0): "); |
| 493 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 494 |
|
| 495 |
rc = modbus_write_and_read_registers(ctx, |
| 496 |
UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, |
| 497 |
UT_REGISTERS_NB, |
| 498 |
tab_rp_registers, |
| 499 |
UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_MAX, |
| 500 |
UT_REGISTERS_NB, |
| 501 |
tab_rp_registers); |
| 502 |
printf("* modbus_write_and_read_registers (max): "); |
| 503 |
ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 504 |
|
| 505 |
/** TOO MANY DATA **/ |
| 506 |
printf("\nTEST TOO MANY DATA ERROR:\n"); |
| 507 |
|
| 508 |
rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits); |
| 509 |
printf("* modbus_read_bits: "); |
| 510 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 511 |
|
| 512 |
rc = modbus_read_input_bits( |
| 513 |
ctx, UT_INPUT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1, tab_rp_bits); |
| 514 |
printf("* modbus_read_input_bits: "); |
| 515 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 516 |
|
| 517 |
rc = modbus_read_registers( |
| 518 |
ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers); |
| 519 |
printf("* modbus_read_registers: "); |
| 520 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 521 |
|
| 522 |
rc = modbus_read_input_registers( |
| 523 |
ctx, UT_INPUT_REGISTERS_ADDRESS, MODBUS_MAX_READ_REGISTERS + 1, tab_rp_registers); |
| 524 |
printf("* modbus_read_input_registers: "); |
| 525 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 526 |
|
| 527 |
rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits); |
| 528 |
printf("* modbus_write_bits: "); |
| 529 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 530 |
|
| 531 |
rc = modbus_write_registers( |
| 532 |
ctx, UT_REGISTERS_ADDRESS, MODBUS_MAX_WRITE_REGISTERS + 1, tab_rp_registers); |
| 533 |
printf("* modbus_write_registers: "); |
| 534 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 535 |
|
| 536 |
/** SLAVE ADDRESS **/ |
| 537 |
old_slave = modbus_get_slave(ctx); |
| 538 |
|
| 539 |
printf("\nTEST SLAVE ADDRESS:\n"); |
| 540 |
|
| 541 |
printf("1/2 Not compliant slave address is refused: "); |
| 542 |
rc = modbus_set_slave(ctx, 248); |
| 543 |
ASSERT_TRUE(rc == -1, "Slave address of 248 shouldn't be allowed"); |
| 544 |
|
| 545 |
printf("2/2 Not compliant slave address is allowed: "); |
| 546 |
modbus_enable_quirks(ctx, MODBUS_QUIRK_MAX_SLAVE); |
| 547 |
rc = modbus_set_slave(ctx, 248); |
| 548 |
ASSERT_TRUE(rc == 0, "Not compliant slave address should have been accepted"); |
| 549 |
|
| 550 |
modbus_disable_quirks(ctx, MODBUS_QUIRK_MAX_SLAVE); |
| 551 |
rc = modbus_set_slave(ctx, old_slave); |
| 552 |
ASSERT_TRUE(rc == 0, "Unable to restore slave value") |
| 553 |
|
| 554 |
/** BAD USE OF REPLY FUNCTION **/ |
| 555 |
rc = modbus_write_bit(ctx, UT_BITS_ADDRESS_INVALID_REQUEST_LENGTH, ON); |
| 556 |
printf("* modbus_write_bit (triggers invalid reply): "); |
| 557 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 558 |
|
| 559 |
rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS_SPECIAL, 0x42); |
| 560 |
printf("* modbus_write_register (triggers invalid reply): "); |
| 561 |
ASSERT_TRUE(rc == -1 && errno == EMBXILVAL, ""); |
| 562 |
|
| 563 |
/** SLAVE REPLY **/ |
| 564 |
|
| 565 |
printf("\nTEST SLAVE REPLY:\n"); |
| 566 |
modbus_set_slave(ctx, INVALID_SERVER_ID); |
| 567 |
rc = modbus_read_registers( |
| 568 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); |
| 569 |
if (use_backend == RTU) { |
| 570 |
const int RAW_REQ_LENGTH = 6; |
| 571 |
uint8_t raw_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0x01, 0x01}; |
| 572 |
/* Too many points */ |
| 573 |
uint8_t raw_invalid_req[] = {INVALID_SERVER_ID, 0x03, 0x00, 0x01, 0xFF, 0xFF}; |
| 574 |
const int RAW_RSP_LENGTH = 7; |
| 575 |
uint8_t raw_rsp[] = {INVALID_SERVER_ID, 0x03, 0x04, 0, 0, 0, 0}; |
| 576 |
uint8_t rsp[MODBUS_RTU_MAX_ADU_LENGTH]; |
| 577 |
|
| 578 |
/* No response in RTU mode */ |
| 579 |
printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID); |
| 580 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 581 |
|
| 582 |
/* The slave raises a timeout on a confirmation to ignore because if an |
| 583 |
* indication for another slave is received, a confirmation must follow */ |
| 584 |
|
| 585 |
/* Send a pair of indication/confirmation to the slave with a different |
| 586 |
* slave ID to simulate a communication on a RS485 bus. At first, the |
| 587 |
* slave will see the indication message then the confirmation, and it must |
| 588 |
* ignore both. */ |
| 589 |
modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t)); |
| 590 |
modbus_send_raw_request(ctx, raw_rsp, RAW_RSP_LENGTH * sizeof(uint8_t)); |
| 591 |
rc = modbus_receive_confirmation(ctx, rsp); |
| 592 |
|
| 593 |
printf("1-B/3 No response from slave %d on indication/confirmation messages: ", |
| 594 |
INVALID_SERVER_ID); |
| 595 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 596 |
|
| 597 |
/* Send an INVALID request for another slave */ |
| 598 |
modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t)); |
| 599 |
rc = modbus_receive_confirmation(ctx, rsp); |
| 600 |
|
| 601 |
printf("1-C/3 No response from slave %d with invalid request: ", |
| 602 |
INVALID_SERVER_ID); |
| 603 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 604 |
|
| 605 |
rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); |
| 606 |
ASSERT_TRUE(rc == 0, "Invalid broadcast address"); |
| 607 |
|
| 608 |
rc = modbus_read_registers( |
| 609 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); |
| 610 |
printf("2/3 No reply after a broadcast query: "); |
| 611 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 612 |
} else { |
| 613 |
/* Response in TCP mode */ |
| 614 |
printf("1/3 Response from slave %d: ", INVALID_SERVER_ID); |
| 615 |
ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 616 |
|
| 617 |
rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); |
| 618 |
ASSERT_TRUE(rc == 0, "Invalid broacast address"); |
| 619 |
|
| 620 |
rc = modbus_read_registers( |
| 621 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); |
| 622 |
printf("2/3 Reply after a query with unit id == 0: "); |
| 623 |
ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 624 |
} |
| 625 |
|
| 626 |
/* Restore slave */ |
| 627 |
modbus_set_slave(ctx, old_slave); |
| 628 |
|
| 629 |
printf("3/3 Response with an invalid TID or slave: "); |
| 630 |
rc = modbus_read_registers( |
| 631 |
ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, 1, tab_rp_registers); |
| 632 |
ASSERT_TRUE(rc == -1, ""); |
| 633 |
|
| 634 |
printf("1/2 Report slave ID truncated: \n"); |
| 635 |
/* Set a marker to ensure limit is respected */ |
| 636 |
tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42; |
| 637 |
rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits); |
| 638 |
/* Return the size required (response size) but respects the defined limit */ |
| 639 |
ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42, |
| 640 |
"Return is rc %d (%d) and marker is %d (42)", |
| 641 |
rc, |
| 642 |
NB_REPORT_SLAVE_ID, |
| 643 |
tab_rp_bits[NB_REPORT_SLAVE_ID - 1]); |
| 644 |
|
| 645 |
printf("2/2 Report slave ID: \n"); |
| 646 |
/* tab_rp_bits is used to store bytes */ |
| 647 |
rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits); |
| 648 |
ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, ""); |
| 649 |
|
| 650 |
/* Slave ID is an arbitrary number for libmodbus */ |
| 651 |
ASSERT_TRUE(rc > 0, ""); |
| 652 |
|
| 653 |
/* Run status indicator is ON */ |
| 654 |
ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, ""); |
| 655 |
|
| 656 |
/* Print additional data as string */ |
| 657 |
if (rc > 2) { |
| 658 |
printf("Additional data: "); |
| 659 |
for (i = 2; i < rc; i++) { |
| 660 |
printf("%c", tab_rp_bits[i]); |
| 661 |
} |
| 662 |
printf("\n"); |
| 663 |
} |
| 664 |
|
| 665 |
/* Save original timeout */ |
| 666 |
modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); |
| 667 |
modbus_get_byte_timeout(ctx, &old_byte_to_sec, &old_byte_to_usec); |
| 668 |
|
| 669 |
rc = modbus_set_response_timeout(ctx, 0, 0); |
| 670 |
printf("1/8 Invalid response timeout (zero): "); |
| 671 |
ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 672 |
|
| 673 |
rc = modbus_set_response_timeout(ctx, 0, 1000000); |
| 674 |
printf("2/8 Invalid response timeout (too large us): "); |
| 675 |
ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 676 |
|
| 677 |
rc = modbus_set_byte_timeout(ctx, 0, 1000000); |
| 678 |
printf("3/8 Invalid byte timeout (too large us): "); |
| 679 |
ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 680 |
|
| 681 |
modbus_set_response_timeout(ctx, 0, 1); |
| 682 |
rc = modbus_read_registers( |
| 683 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB, tab_rp_registers); |
| 684 |
printf("4/8 1us response timeout: "); |
| 685 |
if (rc == -1 && errno == ETIMEDOUT) { |
| 686 |
printf("OK\n"); |
| 687 |
} else { |
| 688 |
printf("FAILED (can fail on some platforms)\n"); |
| 689 |
} |
| 690 |
|
| 691 |
/* A wait and flush operation is done by the error recovery code of |
| 692 |
* libmodbus but after a sleep of current response timeout |
| 693 |
* so 0 can be too short! |
| 694 |
*/ |
| 695 |
usleep(old_response_to_sec * 1000000 + old_response_to_usec); |
| 696 |
modbus_flush(ctx); |
| 697 |
|
| 698 |
/* Trigger a special behaviour on server to wait for 0.5 second before |
| 699 |
* replying whereas allowed timeout is 0.2 second */ |
| 700 |
modbus_set_response_timeout(ctx, 0, 200000); |
| 701 |
rc = modbus_read_registers( |
| 702 |
ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers); |
| 703 |
printf("5/8 Too short response timeout (0.2s < 0.5s): "); |
| 704 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 705 |
|
| 706 |
/* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */ |
| 707 |
usleep(400000); |
| 708 |
modbus_flush(ctx); |
| 709 |
|
| 710 |
modbus_set_response_timeout(ctx, 0, 600000); |
| 711 |
rc = modbus_read_registers( |
| 712 |
ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers); |
| 713 |
printf("6/8 Adequate response timeout (0.6s > 0.5s): "); |
| 714 |
ASSERT_TRUE(rc == 1, ""); |
| 715 |
|
| 716 |
/* Disable the byte timeout. |
| 717 |
The full response must be available in the 600ms interval */ |
| 718 |
modbus_set_byte_timeout(ctx, 0, 0); |
| 719 |
rc = modbus_read_registers( |
| 720 |
ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, 1, tab_rp_registers); |
| 721 |
printf("7/8 Disable byte timeout: "); |
| 722 |
ASSERT_TRUE(rc == 1, ""); |
| 723 |
|
| 724 |
// Invalid in TCP or RTU mode... |
| 725 |
modbus_t *invalid_ctx = modbus_new_tcp("1.2.3.4", 1502); |
| 726 |
modbus_set_response_timeout(invalid_ctx, 0, 1); |
| 727 |
rc = modbus_connect(invalid_ctx); |
| 728 |
printf("8/8 Connection timeout: "); |
| •729 |
ASSERT_TRUE(rc == -1 && (errno == ETIMEDOUT || errno == ENETUNREACH), ""); |
| 730 |
modbus_free(invalid_ctx); |
| 731 |
|
| 732 |
/* Restore original response timeout */ |
| 733 |
modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); |
| 734 |
|
| 735 |
if (use_backend == TCP) { |
| 736 |
/* The test server is only able to test byte timeouts with the TCP |
| 737 |
* backend */ |
| 738 |
|
| 739 |
/* Timeout of 3ms between bytes */ |
| 740 |
modbus_set_byte_timeout(ctx, 0, 3000); |
| 741 |
rc = modbus_read_registers( |
| 742 |
ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers); |
| 743 |
printf("1/2 Too small byte timeout (3ms < 5ms): "); |
| 744 |
ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 745 |
|
| 746 |
/* Wait remaining bytes before flushing */ |
| 747 |
usleep(11 * 5000); |
| 748 |
modbus_flush(ctx); |
| 749 |
|
| 750 |
/* Timeout of 7ms between bytes */ |
| 751 |
modbus_set_byte_timeout(ctx, 0, 7000); |
| 752 |
rc = modbus_read_registers( |
| 753 |
ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, 1, tab_rp_registers); |
| 754 |
printf("2/2 Adapted byte timeout (7ms > 5ms): "); |
| 755 |
ASSERT_TRUE(rc == 1, ""); |
| 756 |
} |
| 757 |
|
| 758 |
/* Restore original byte timeout */ |
| 759 |
modbus_set_byte_timeout(ctx, old_byte_to_sec, old_byte_to_usec); |
| 760 |
|
| 761 |
/** BAD RESPONSE **/ |
| 762 |
printf("\nTEST BAD RESPONSE ERROR:\n"); |
| 763 |
|
| 764 |
/* Allocate only the required space */ |
| 765 |
tab_rp_registers_bad = |
| 766 |
(uint16_t *) malloc(UT_REGISTERS_NB_SPECIAL * sizeof(uint16_t)); |
| 767 |
|
| 768 |
rc = modbus_read_registers( |
| 769 |
ctx, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad); |
| 770 |
printf("* modbus_read_registers: "); |
| 771 |
ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, ""); |
| 772 |
free(tab_rp_registers_bad); |
| 773 |
|
| 774 |
/** MANUAL EXCEPTION **/ |
| 775 |
printf("\nTEST MANUAL EXCEPTION:\n"); |
| 776 |
rc = modbus_read_registers( |
| 777 |
ctx, UT_REGISTERS_ADDRESS_SPECIAL, UT_REGISTERS_NB, tab_rp_registers); |
| 778 |
|
| 779 |
printf("* modbus_read_registers at special address: "); |
| 780 |
ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, ""); |
| 781 |
|
| 782 |
/** Run a few tests to challenge the server code **/ |
| 783 |
if (test_server(ctx, use_backend) == -1) { |
| 784 |
goto close; |
| 785 |
} |
| 786 |
|
| 787 |
modbus_close(ctx); |
| 788 |
modbus_free(ctx); |
| 789 |
ctx = NULL; |
| 790 |
|
| 791 |
/* Test init functions */ |
| 792 |
printf("\nTEST INVALID INITIALIZATION:\n"); |
| 793 |
ctx = modbus_new_rtu(NULL, 1, 'A', 0, 0); |
| 794 |
ASSERT_TRUE(ctx == NULL && errno == EINVAL, ""); |
| 795 |
|
| 796 |
ctx = modbus_new_rtu("/dev/dummy", 0, 'A', 0, 0); |
| 797 |
ASSERT_TRUE(ctx == NULL && errno == EINVAL, ""); |
| 798 |
|
| 799 |
printf("\nALL TESTS PASS WITH SUCCESS.\n"); |
| 800 |
success = TRUE; |
| 801 |
|
| 802 |
close: |
| 803 |
/* Free the memory */ |
| 804 |
free(tab_rp_bits); |
| 805 |
free(tab_rp_registers); |
| 806 |
|
| 807 |
/* Close the connection */ |
| 808 |
modbus_close(ctx); |
| 809 |
modbus_free(ctx); |
| 810 |
|
| 811 |
return (success) ? 0 : -1; |
| 812 |
} |
| 813 |
|
| 814 |
/* Send crafted requests to test server resilience |
| 815 |
and ensure proper exceptions are returned. */ |
| 816 |
int test_server(modbus_t *ctx, int use_backend) |
| 817 |
{ |
| 818 |
int rc; |
| 819 |
int i; |
| 820 |
/* Read requests */ |
| 821 |
const int READ_RAW_REQ_LEN = 6; |
| 822 |
const int slave = (use_backend == RTU) ? SERVER_ID : MODBUS_TCP_SLAVE; |
| 823 |
uint8_t read_raw_req[] = {slave, |
| 824 |
/* function, address, 5 values */ |
| 825 |
MODBUS_FC_READ_HOLDING_REGISTERS, |
| 826 |
UT_REGISTERS_ADDRESS >> 8, |
| 827 |
UT_REGISTERS_ADDRESS & 0xFF, |
| 828 |
0x0, |
| 829 |
0x05}; |
| 830 |
/* Write and read registers request */ |
| 831 |
const int RW_RAW_REQ_LEN = 13; |
| 832 |
uint8_t rw_raw_req[] = {slave, |
| 833 |
/* function, addr to read, nb to read */ |
| 834 |
MODBUS_FC_WRITE_AND_READ_REGISTERS, |
| 835 |
/* Read */ |
| 836 |
UT_REGISTERS_ADDRESS >> 8, |
| 837 |
UT_REGISTERS_ADDRESS & 0xFF, |
| 838 |
(MODBUS_MAX_WR_READ_REGISTERS + 1) >> 8, |
| 839 |
(MODBUS_MAX_WR_READ_REGISTERS + 1) & 0xFF, |
| 840 |
/* Write */ |
| 841 |
0, |
| 842 |
0, |
| 843 |
0, |
| 844 |
1, |
| 845 |
/* Write byte count */ |
| 846 |
1 * 2, |
| 847 |
/* One data to write... */ |
| 848 |
0x12, |
| 849 |
0x34}; |
| 850 |
const int WRITE_RAW_REQ_LEN = 13; |
| 851 |
uint8_t write_raw_req[] = {slave, |
| 852 |
/* function will be set in the loop */ |
| 853 |
MODBUS_FC_WRITE_MULTIPLE_REGISTERS, |
| 854 |
/* Address */ |
| 855 |
UT_REGISTERS_ADDRESS >> 8, |
| 856 |
UT_REGISTERS_ADDRESS & 0xFF, |
| 857 |
/* 3 values, 6 bytes */ |
| 858 |
0x00, |
| 859 |
0x03, |
| 860 |
0x06, |
| 861 |
/* Dummy data to write */ |
| 862 |
0x02, |
| 863 |
0x2B, |
| 864 |
0x00, |
| 865 |
0x01, |
| 866 |
0x00, |
| 867 |
0x64}; |
| 868 |
const int INVALID_FC = 0x42; |
| 869 |
const int INVALID_FC_REQ_LEN = 6; |
| 870 |
uint8_t invalid_fc_raw_req[] = {slave, 0x42, 0x00, 0x00, 0x00, 0x00}; |
| 871 |
|
| 872 |
int req_length; |
| 873 |
uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 874 |
int tab_read_function[] = {MODBUS_FC_READ_COILS, |
| 875 |
MODBUS_FC_READ_DISCRETE_INPUTS, |
| 876 |
MODBUS_FC_READ_HOLDING_REGISTERS, |
| 877 |
MODBUS_FC_READ_INPUT_REGISTERS}; |
| 878 |
int tab_read_nb_max[] = {MODBUS_MAX_READ_BITS + 1, |
| 879 |
MODBUS_MAX_READ_BITS + 1, |
| 880 |
MODBUS_MAX_READ_REGISTERS + 1, |
| 881 |
MODBUS_MAX_READ_REGISTERS + 1}; |
| 882 |
int backend_length; |
| 883 |
int backend_offset; |
| 884 |
|
| 885 |
if (use_backend == RTU) { |
| 886 |
backend_length = 3; |
| 887 |
backend_offset = 1; |
| 888 |
} else { |
| 889 |
backend_length = 7; |
| 890 |
backend_offset = 7; |
| 891 |
} |
| 892 |
|
| 893 |
printf("\nTEST RAW REQUESTS:\n"); |
| 894 |
|
| 895 |
uint32_t old_response_to_sec; |
| 896 |
uint32_t old_response_to_usec; |
| 897 |
|
| 898 |
/* This requests can generate flushes server side so we need a higher |
| 899 |
* response timeout than the server. The server uses the defined response |
| 900 |
* timeout to sleep before flushing. |
| 901 |
* The old timeouts are restored at the end. |
| 902 |
*/ |
| 903 |
modbus_get_response_timeout(ctx, &old_response_to_sec, &old_response_to_usec); |
| 904 |
modbus_set_response_timeout(ctx, 0, 600000); |
| 905 |
|
| 906 |
int old_s = modbus_get_socket(ctx); |
| 907 |
modbus_set_socket(ctx, -1); |
| 908 |
rc = modbus_receive(ctx, rsp); |
| 909 |
modbus_set_socket(ctx, old_s); |
| 910 |
printf("* modbus_receive with invalid socket: "); |
| 911 |
ASSERT_TRUE(rc == -1, "FAILED (%d)\n", rc); |
| 912 |
|
| 913 |
req_length = modbus_send_raw_request(ctx, read_raw_req, READ_RAW_REQ_LEN); |
| 914 |
printf("* modbus_send_raw_request: "); |
| 915 |
ASSERT_TRUE(req_length == (backend_length + 5), "FAILED (%d)\n", req_length); |
| 916 |
|
| 917 |
printf("* modbus_receive_confirmation: "); |
| 918 |
rc = modbus_receive_confirmation(ctx, rsp); |
| 919 |
ASSERT_TRUE(rc == (backend_length + 12), "FAILED (%d)\n", rc); |
| 920 |
|
| 921 |
/* Try to read more values than a response could hold for all data |
| 922 |
types. */ |
| 923 |
for (i = 0; i < 4; i++) { |
| 924 |
rc = send_crafted_request(ctx, |
| 925 |
tab_read_function[i], |
| 926 |
read_raw_req, |
| 927 |
READ_RAW_REQ_LEN, |
| 928 |
tab_read_nb_max[i], |
| 929 |
0, |
| 930 |
backend_length, |
| 931 |
backend_offset); |
| 932 |
if (rc == -1) |
| 933 |
goto close; |
| 934 |
} |
| 935 |
|
| 936 |
rc = send_crafted_request(ctx, |
| 937 |
MODBUS_FC_WRITE_AND_READ_REGISTERS, |
| 938 |
rw_raw_req, |
| 939 |
RW_RAW_REQ_LEN, |
| 940 |
MODBUS_MAX_WR_READ_REGISTERS + 1, |
| 941 |
0, |
| 942 |
backend_length, |
| 943 |
backend_offset); |
| 944 |
if (rc == -1) |
| 945 |
goto close; |
| 946 |
|
| 947 |
rc = send_crafted_request(ctx, |
| 948 |
MODBUS_FC_WRITE_MULTIPLE_REGISTERS, |
| 949 |
write_raw_req, |
| 950 |
WRITE_RAW_REQ_LEN, |
| 951 |
MODBUS_MAX_WRITE_REGISTERS + 1, |
| 952 |
6, |
| 953 |
backend_length, |
| 954 |
backend_offset); |
| 955 |
if (rc == -1) |
| 956 |
goto close; |
| 957 |
|
| 958 |
rc = send_crafted_request(ctx, |
| 959 |
MODBUS_FC_WRITE_MULTIPLE_COILS, |
| 960 |
write_raw_req, |
| 961 |
WRITE_RAW_REQ_LEN, |
| 962 |
MODBUS_MAX_WRITE_BITS + 1, |
| 963 |
6, |
| 964 |
backend_length, |
| 965 |
backend_offset); |
| 966 |
if (rc == -1) |
| 967 |
goto close; |
| 968 |
|
| 969 |
/* Modbus write multiple registers with large number of values but a set a |
| 970 |
small number of bytes in requests (not nb * 2 as usual). */ |
| 971 |
rc = send_crafted_request(ctx, |
| 972 |
MODBUS_FC_WRITE_MULTIPLE_REGISTERS, |
| 973 |
write_raw_req, |
| 974 |
WRITE_RAW_REQ_LEN, |
| 975 |
MODBUS_MAX_WRITE_REGISTERS, |
| 976 |
6, |
| 977 |
backend_length, |
| 978 |
backend_offset); |
| 979 |
if (rc == -1) |
| 980 |
goto close; |
| 981 |
|
| 982 |
rc = send_crafted_request(ctx, |
| 983 |
MODBUS_FC_WRITE_MULTIPLE_COILS, |
| 984 |
write_raw_req, |
| 985 |
WRITE_RAW_REQ_LEN, |
| 986 |
MODBUS_MAX_WRITE_BITS, |
| 987 |
6, |
| 988 |
backend_length, |
| 989 |
backend_offset); |
| 990 |
if (rc == -1) |
| 991 |
goto close; |
| 992 |
|
| 993 |
/* Test invalid function code */ |
| 994 |
modbus_send_raw_request( |
| 995 |
ctx, invalid_fc_raw_req, INVALID_FC_REQ_LEN * sizeof(uint8_t)); |
| 996 |
rc = modbus_receive_confirmation(ctx, rsp); |
| 997 |
printf("Return an exception on unknown function code: "); |
| 998 |
ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && |
| 999 |
rsp[backend_offset] == (0x80 + INVALID_FC), |
| 1000 |
"") |
| 1001 |
|
| 1002 |
modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); |
| 1003 |
return 0; |
| 1004 |
close: |
| 1005 |
modbus_set_response_timeout(ctx, old_response_to_sec, old_response_to_usec); |
| 1006 |
return -1; |
| 1007 |
} |
| 1008 |
|
| 1009 |
int send_crafted_request(modbus_t *ctx, |
| 1010 |
int function, |
| 1011 |
uint8_t *req, |
| 1012 |
int req_len, |
| 1013 |
uint16_t max_value, |
| 1014 |
uint16_t bytes, |
| 1015 |
int backend_length, |
| 1016 |
int backend_offset) |
| 1017 |
{ |
| 1018 |
uint8_t rsp[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 1019 |
int j; |
| 1020 |
|
| 1021 |
for (j = 0; j < 2; j++) { |
| 1022 |
int rc; |
| 1023 |
|
| 1024 |
req[1] = function; |
| 1025 |
if (j == 0) { |
| 1026 |
/* Try to read or write zero values on first iteration */ |
| 1027 |
req[4] = 0x00; |
| 1028 |
req[5] = 0x00; |
| 1029 |
if (bytes) { |
| 1030 |
/* Write query */ |
| 1031 |
req[6] = 0x00; |
| 1032 |
} |
| 1033 |
} else { |
| 1034 |
/* Try to read or write max values + 1 on second iteration */ |
| 1035 |
req[4] = (max_value >> 8) & 0xFF; |
| 1036 |
req[5] = max_value & 0xFF; |
| 1037 |
if (bytes) { |
| 1038 |
/* Write query (nb values * 2 to convert in bytes for registers) */ |
| 1039 |
req[6] = bytes; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
modbus_send_raw_request(ctx, req, req_len * sizeof(uint8_t)); |
| 1044 |
if (j == 0) { |
| 1045 |
printf( |
| 1046 |
"* try function 0x%X: %s 0 values: ", function, bytes ? "write" : "read"); |
| 1047 |
} else { |
| 1048 |
printf("* try function 0x%X: %s %d values: ", |
| 1049 |
function, |
| 1050 |
bytes ? "write" : "read", |
| 1051 |
max_value); |
| 1052 |
} |
| 1053 |
rc = modbus_receive_confirmation(ctx, rsp); |
| 1054 |
ASSERT_TRUE(rc == (backend_length + EXCEPTION_RC) && |
| 1055 |
rsp[backend_offset] == (0x80 + function) && |
| 1056 |
rsp[backend_offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, |
| 1057 |
""); |
| 1058 |
} |
| 1059 |
return 0; |
| 1060 |
close: |
| 1061 |
return -1; |
| 1062 |
} |