Updated tests
This commit is contained in:
parent
b6fee48160
commit
7f811f2338
@ -1,45 +1,41 @@
|
|||||||
#define NMBS_CLIENT_DISABLED
|
#define NMBS_CLIENT_DISABLED
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "nanomodbus.h"
|
#include "nanomodbus.h"
|
||||||
|
|
||||||
#define UNUSED_PARAM(x) ((x) = (x))
|
#define UNUSED_PARAM(x) ((x) = (x))
|
||||||
|
|
||||||
|
|
||||||
int read_byte_empty(uint8_t* b, int32_t timeout, void* arg) {
|
int read_empty(uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int write_byte_empty(uint8_t b, int32_t timeout, void* arg) {
|
int write_empty(const uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void platform_sleep(uint32_t milliseconds, void* arg) {
|
|
||||||
UNUSED_PARAM(arg);
|
|
||||||
usleep(milliseconds * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
nmbs_t nmbs;
|
nmbs_t nmbs;
|
||||||
|
|
||||||
nmbs_platform_conf platform_conf_empty = {.transport = NMBS_TRANSPORT_TCP,
|
nmbs_platform_conf platform_conf_empty = {
|
||||||
.read_byte = read_byte_empty,
|
.transport = NMBS_TRANSPORT_TCP,
|
||||||
.write_byte = write_byte_empty,
|
.read = read_empty,
|
||||||
.sleep = platform_sleep};
|
.write = write_empty,
|
||||||
|
};
|
||||||
|
|
||||||
nmbs_callbacks callbacks_empty;
|
nmbs_callbacks callbacks_empty;
|
||||||
|
|
||||||
nmbs_error err = nmbs_server_create(&nmbs, 1, &platform_conf_empty, &callbacks_empty);
|
nmbs_error err = nmbs_server_create(&nmbs, 1, &platform_conf_empty, &callbacks_empty);
|
||||||
if(err != 0)
|
if (err != 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -5,16 +5,18 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
int read_byte_empty(uint8_t* b, int32_t timeout, void* arg) {
|
int read_empty(uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int write_byte_empty(uint8_t b, int32_t timeout, void* arg) {
|
int write_empty(const uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
@ -25,10 +27,12 @@ void test_server_create(nmbs_transport transport) {
|
|||||||
nmbs_t nmbs;
|
nmbs_t nmbs;
|
||||||
nmbs_error err;
|
nmbs_error err;
|
||||||
|
|
||||||
nmbs_platform_conf platform_conf_empty = {.transport = transport,
|
nmbs_platform_conf platform_conf_empty = {
|
||||||
.read_byte = read_byte_empty,
|
.transport = transport,
|
||||||
.write_byte = write_byte_empty,
|
.read = read_empty,
|
||||||
.sleep = platform_sleep};
|
.write = write_empty,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
nmbs_callbacks callbacks_empty;
|
nmbs_callbacks callbacks_empty;
|
||||||
|
|
||||||
@ -57,37 +61,38 @@ void test_server_create(nmbs_transport transport) {
|
|||||||
|
|
||||||
reset(nmbs);
|
reset(nmbs);
|
||||||
p = platform_conf_empty;
|
p = platform_conf_empty;
|
||||||
p.read_byte = NULL;
|
p.read = NULL;
|
||||||
err = nmbs_server_create(&nmbs, 0, &p, &callbacks_empty);
|
err = nmbs_server_create(&nmbs, 0, &p, &callbacks_empty);
|
||||||
expect(err == NMBS_ERROR_INVALID_ARGUMENT);
|
expect(err == NMBS_ERROR_INVALID_ARGUMENT);
|
||||||
|
|
||||||
reset(nmbs);
|
reset(nmbs);
|
||||||
p = platform_conf_empty;
|
p = platform_conf_empty;
|
||||||
p.write_byte = NULL;
|
p.write = NULL;
|
||||||
err = nmbs_server_create(&nmbs, 0, &p, &callbacks_empty);
|
err = nmbs_server_create(&nmbs, 0, &p, &callbacks_empty);
|
||||||
expect(err == NMBS_ERROR_INVALID_ARGUMENT);
|
expect(err == NMBS_ERROR_INVALID_ARGUMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int read_byte_timeout(uint8_t* b, int32_t timeout, void* arg) {
|
int read_timeout(uint8_t* buf, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(buf);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
usleep(timeout * 1000);
|
usleep(timeout * 1000);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Timeouts on the second read
|
||||||
int read_byte_timeout_third(uint8_t* b, int32_t timeout, void* arg) {
|
int read_timeout_second(uint8_t* buf, uint32_t count, int32_t timeout, void* arg) {
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
|
|
||||||
static int stage = 0;
|
static int stage = 0;
|
||||||
switch (stage) {
|
switch (stage) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
*buf = 1;
|
||||||
*b = 1;
|
|
||||||
stage++;
|
stage++;
|
||||||
return 1;
|
return (int) count;
|
||||||
case 2:
|
case 1:
|
||||||
expect(timeout > 0);
|
expect(timeout > 0);
|
||||||
usleep(timeout * 1000 + 100 * 1000);
|
usleep(timeout * 1000 + 100 * 1000);
|
||||||
stage = 0;
|
stage = 0;
|
||||||
@ -100,7 +105,7 @@ int read_byte_timeout_third(uint8_t* b, int32_t timeout, void* arg) {
|
|||||||
|
|
||||||
|
|
||||||
void test_server_receive_base(nmbs_transport transport) {
|
void test_server_receive_base(nmbs_transport transport) {
|
||||||
nmbs_t server, client;
|
nmbs_t server;
|
||||||
nmbs_error err;
|
nmbs_error err;
|
||||||
nmbs_platform_conf platform_conf;
|
nmbs_platform_conf platform_conf;
|
||||||
nmbs_callbacks callbacks_empty;
|
nmbs_callbacks callbacks_empty;
|
||||||
@ -109,9 +114,8 @@ void test_server_receive_base(nmbs_transport transport) {
|
|||||||
should("honor read_timeout and return normally");
|
should("honor read_timeout and return normally");
|
||||||
reset(server);
|
reset(server);
|
||||||
platform_conf.transport = transport;
|
platform_conf.transport = transport;
|
||||||
platform_conf.read_byte = read_byte_timeout;
|
platform_conf.read = read_timeout;
|
||||||
platform_conf.write_byte = write_byte_empty;
|
platform_conf.write = write_empty;
|
||||||
platform_conf.sleep = platform_sleep;
|
|
||||||
|
|
||||||
const int32_t read_timeout_ms = 250;
|
const int32_t read_timeout_ms = 250;
|
||||||
|
|
||||||
@ -136,8 +140,8 @@ void test_server_receive_base(nmbs_transport transport) {
|
|||||||
should("honor byte_timeout and return NMBS_ERROR_TIMEOUT");
|
should("honor byte_timeout and return NMBS_ERROR_TIMEOUT");
|
||||||
reset(server);
|
reset(server);
|
||||||
platform_conf.transport = transport;
|
platform_conf.transport = transport;
|
||||||
platform_conf.read_byte = read_byte_timeout_third;
|
platform_conf.read = read_timeout_second;
|
||||||
platform_conf.write_byte = write_byte_empty;
|
platform_conf.write = write_empty;
|
||||||
|
|
||||||
const int32_t byte_timeout_ms = 250;
|
const int32_t byte_timeout_ms = 250;
|
||||||
|
|
||||||
@ -149,26 +153,6 @@ void test_server_receive_base(nmbs_transport transport) {
|
|||||||
|
|
||||||
err = nmbs_server_poll(&server);
|
err = nmbs_server_poll(&server);
|
||||||
expect(err == NMBS_ERROR_TIMEOUT);
|
expect(err == NMBS_ERROR_TIMEOUT);
|
||||||
|
|
||||||
|
|
||||||
should("honor byte spacing on RTU");
|
|
||||||
if (transport == NMBS_TRANSPORT_RTU) {
|
|
||||||
reset(client);
|
|
||||||
platform_conf.transport = transport;
|
|
||||||
platform_conf.read_byte = read_byte_socket_client;
|
|
||||||
platform_conf.write_byte = write_byte_socket_client;
|
|
||||||
|
|
||||||
reset_sockets();
|
|
||||||
|
|
||||||
check(nmbs_client_create(&client, &platform_conf));
|
|
||||||
|
|
||||||
nmbs_set_byte_spacing(&client, 200);
|
|
||||||
|
|
||||||
uint64_t start = now_ms();
|
|
||||||
check(nmbs_send_raw_pdu(&client, 1, (uint16_t[]){htons(1), htons(1)}, 4));
|
|
||||||
uint64_t diff = now_ms() - start;
|
|
||||||
expect(diff >= 200 * 8);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -64,96 +64,105 @@ void reset_sockets() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int read_byte_fd(int fd, uint8_t* b, int32_t timeout_ms) {
|
int32_t read_fd(int fd, uint8_t* buf, uint32_t count, int32_t timeout_ms) {
|
||||||
fd_set rfds;
|
int32_t total = 0;
|
||||||
FD_ZERO(&rfds);
|
while (total != (int32_t) count) {
|
||||||
FD_SET(fd, &rfds);
|
fd_set rfds;
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(fd, &rfds);
|
||||||
|
|
||||||
struct timeval* tv_p = NULL;
|
struct timeval* tv_p = NULL;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
if (timeout_ms >= 0) {
|
if (timeout_ms >= 0) {
|
||||||
tv_p = &tv;
|
tv_p = &tv;
|
||||||
tv.tv_sec = timeout_ms / 1000;
|
tv.tv_sec = timeout_ms / 1000;
|
||||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||||
}
|
|
||||||
|
|
||||||
int ret = select(fd + 1, &rfds, NULL, NULL, tv_p);
|
|
||||||
if (ret == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if (ret == 1) {
|
|
||||||
ssize_t r = read(fd, b, 1);
|
|
||||||
if (r != 1)
|
|
||||||
return -1;
|
|
||||||
else {
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int ret = select(fd + 1, &rfds, NULL, NULL, tv_p);
|
||||||
int write_byte_fd(int fd, uint8_t b, int32_t timeout_ms) {
|
if (ret == 0) {
|
||||||
fd_set wfds;
|
return total;
|
||||||
FD_ZERO(&wfds);
|
|
||||||
FD_SET(fd, &wfds);
|
|
||||||
|
|
||||||
struct timeval* tv_p = NULL;
|
|
||||||
struct timeval tv;
|
|
||||||
if (timeout_ms >= 0) {
|
|
||||||
tv_p = &tv;
|
|
||||||
tv.tv_sec = timeout_ms / 1000;
|
|
||||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret = select(fd + 1, NULL, &wfds, NULL, tv_p);
|
|
||||||
if (ret == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if (ret == 1) {
|
|
||||||
ssize_t r = write(fd, &b, 1);
|
|
||||||
if (r != 1)
|
|
||||||
return -1;
|
|
||||||
else {
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
else if (ret == 1) {
|
||||||
|
ssize_t r = read(fd, buf + total, 1);
|
||||||
|
if (r <= 0)
|
||||||
|
return -1;
|
||||||
|
else {
|
||||||
|
total += (int32_t) r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
return -1;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int read_byte_socket_server(uint8_t* b, int32_t timeout_ms, void* arg) {
|
int write_fd(int fd, const uint8_t* buf, uint32_t count, int32_t timeout_ms) {
|
||||||
UNUSED_PARAM(arg);
|
int32_t total = 0;
|
||||||
return read_byte_fd(sockets[0], b, timeout_ms);
|
while (total != (int32_t) count) {
|
||||||
|
fd_set wfds;
|
||||||
|
FD_ZERO(&wfds);
|
||||||
|
FD_SET(fd, &wfds);
|
||||||
|
|
||||||
|
struct timeval* tv_p = NULL;
|
||||||
|
struct timeval tv;
|
||||||
|
if (timeout_ms >= 0) {
|
||||||
|
tv_p = &tv;
|
||||||
|
tv.tv_sec = timeout_ms / 1000;
|
||||||
|
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ret = select(fd + 1, NULL, &wfds, NULL, tv_p);
|
||||||
|
if (ret == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if (ret == 1) {
|
||||||
|
ssize_t w = write(fd, buf + total, count);
|
||||||
|
if (w <= 0)
|
||||||
|
return -1;
|
||||||
|
else {
|
||||||
|
total += (int32_t) w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int write_byte_socket_server(uint8_t b, int32_t timeout_ms, void* arg) {
|
int read_socket_server(uint8_t* buf, uint32_t count, int32_t timeout_ms, void* arg) {
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return write_byte_fd(sockets[0], b, timeout_ms);
|
return read_fd(sockets[0], buf, count, timeout_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int read_byte_socket_client(uint8_t* b, int32_t timeout_ms, void* arg) {
|
int write_socket_server(const uint8_t* buf, uint32_t count, int32_t timeout_ms, void* arg) {
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return read_byte_fd(sockets[1], b, timeout_ms);
|
return write_fd(sockets[0], buf, count, timeout_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int write_byte_socket_client(uint8_t b, int32_t timeout_ms, void* arg) {
|
int read_socket_client(uint8_t* buf, uint32_t count, int32_t timeout_ms, void* arg) {
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return write_byte_fd(sockets[1], b, timeout_ms);
|
return read_fd(sockets[1], buf, count, timeout_ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int write_socket_client(const uint8_t* buf, uint32_t count, int32_t timeout_ms, void* arg) {
|
||||||
|
UNUSED_PARAM(arg);
|
||||||
|
return write_fd(sockets[1], buf, count, timeout_ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
nmbs_platform_conf nmbs_platform_conf_server;
|
nmbs_platform_conf nmbs_platform_conf_server;
|
||||||
nmbs_platform_conf* platform_conf_socket_server(nmbs_transport transport) {
|
nmbs_platform_conf* platform_conf_socket_server(nmbs_transport transport) {
|
||||||
nmbs_platform_conf_server.transport = transport;
|
nmbs_platform_conf_server.transport = transport;
|
||||||
nmbs_platform_conf_server.read_byte = read_byte_socket_server;
|
nmbs_platform_conf_server.read = read_socket_server;
|
||||||
nmbs_platform_conf_server.write_byte = write_byte_socket_server;
|
nmbs_platform_conf_server.write = write_socket_server;
|
||||||
nmbs_platform_conf_server.sleep = platform_sleep;
|
|
||||||
return &nmbs_platform_conf_server;
|
return &nmbs_platform_conf_server;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,9 +170,8 @@ nmbs_platform_conf* platform_conf_socket_server(nmbs_transport transport) {
|
|||||||
nmbs_platform_conf nmbs_platform_conf_client;
|
nmbs_platform_conf nmbs_platform_conf_client;
|
||||||
nmbs_platform_conf* platform_conf_socket_client(nmbs_transport transport) {
|
nmbs_platform_conf* platform_conf_socket_client(nmbs_transport transport) {
|
||||||
nmbs_platform_conf_client.transport = transport;
|
nmbs_platform_conf_client.transport = transport;
|
||||||
nmbs_platform_conf_client.read_byte = read_byte_socket_client;
|
nmbs_platform_conf_client.read = read_socket_client;
|
||||||
nmbs_platform_conf_client.write_byte = write_byte_socket_client;
|
nmbs_platform_conf_client.write = write_socket_client;
|
||||||
nmbs_platform_conf_client.sleep = platform_sleep;
|
|
||||||
return &nmbs_platform_conf_client;
|
return &nmbs_platform_conf_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,43 +1,39 @@
|
|||||||
#define NMBS_SERVER_DISABLED
|
#define NMBS_SERVER_DISABLED
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "nanomodbus.h"
|
#include "nanomodbus.h"
|
||||||
|
|
||||||
#define UNUSED_PARAM(x) ((x) = (x))
|
#define UNUSED_PARAM(x) ((x) = (x))
|
||||||
|
|
||||||
|
|
||||||
int read_byte_empty(uint8_t* b, int32_t timeout, void* arg) {
|
int read_empty(uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int write_byte_empty(uint8_t b, int32_t timeout, void* arg) {
|
int write_empty(const uint8_t* b, uint32_t count, int32_t timeout, void* arg) {
|
||||||
UNUSED_PARAM(b);
|
UNUSED_PARAM(b);
|
||||||
|
UNUSED_PARAM(count);
|
||||||
UNUSED_PARAM(timeout);
|
UNUSED_PARAM(timeout);
|
||||||
UNUSED_PARAM(arg);
|
UNUSED_PARAM(arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void platform_sleep(uint32_t milliseconds, void* arg) {
|
|
||||||
UNUSED_PARAM(arg);
|
|
||||||
usleep(milliseconds * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
nmbs_t nmbs;
|
nmbs_t nmbs;
|
||||||
|
|
||||||
nmbs_platform_conf platform_conf_empty = {.transport = NMBS_TRANSPORT_TCP,
|
nmbs_platform_conf platform_conf_empty = {
|
||||||
.read_byte = read_byte_empty,
|
.transport = NMBS_TRANSPORT_TCP,
|
||||||
.write_byte = write_byte_empty,
|
.read = read_empty,
|
||||||
.sleep = platform_sleep};
|
.write = write_empty,
|
||||||
|
};
|
||||||
|
|
||||||
nmbs_error err = nmbs_client_create(&nmbs, &platform_conf_empty);
|
nmbs_error err = nmbs_client_create(&nmbs, &platform_conf_empty);
|
||||||
if(err != 0)
|
if (err != 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user