unit-test-server.c
7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright © 2008-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>
#ifdef _WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
#endif
/* For MinGW */
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
#include "unit-test.h"
enum {
TCP,
TCP_PI,
RTU
};
int main(int argc, char*argv[])
{
int s = -1;
modbus_t *ctx;
modbus_mapping_t *mb_mapping;
int rc;
int i;
int use_backend;
uint8_t *query;
int header_length;
if (argc > 1) {
if (strcmp(argv[1], "tcp") == 0) {
use_backend = TCP;
} else if (strcmp(argv[1], "tcppi") == 0) {
use_backend = TCP_PI;
} else if (strcmp(argv[1], "rtu") == 0) {
use_backend = RTU;
} else {
printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
return -1;
}
} else {
/* By default */
use_backend = TCP;
}
if (use_backend == TCP) {
ctx = modbus_new_tcp("127.0.0.1", 1502);
query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
} else if (use_backend == TCP_PI) {
ctx = modbus_new_tcp_pi("::0", "1502");
query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
} else {
ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
modbus_set_slave(ctx, SERVER_ID);
query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
}
header_length = modbus_get_header_length(ctx);
modbus_set_debug(ctx, TRUE);
mb_mapping = modbus_mapping_new(
UT_BITS_ADDRESS + UT_BITS_NB,
UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
/* Unit tests of modbus_mapping_new (tests would not be sufficient if two nb_* were identical) */
if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) {
printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits);
modbus_free(ctx);
return -1;
}
if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) {
printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB);
modbus_free(ctx);
return -1;
}
if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) {
printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB);
modbus_free(ctx);
return -1;
}
if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) {
printf("Invalid bb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
modbus_free(ctx);
return -1;
}
/* Examples from PI_MODBUS_300.pdf.
Only the read-only input values are assigned. */
/** INPUT STATUS **/
modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
UT_INPUT_BITS_TAB);
/** INPUT REGISTERS **/
for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
UT_INPUT_REGISTERS_TAB[i];;
}
if (use_backend == TCP) {
s = modbus_tcp_listen(ctx, 1);
modbus_tcp_accept(ctx, &s);
} else if (use_backend == TCP_PI) {
s = modbus_tcp_pi_listen(ctx, 1);
modbus_tcp_pi_accept(ctx, &s);
} else {
rc = modbus_connect(ctx);
if (rc == -1) {
fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
}
for (;;) {
do {
rc = modbus_receive(ctx, query);
/* Filtered queries return 0 */
} while (rc == 0);
if (rc == -1) {
/* Connection closed by the client or error */
break;
}
/* Read holding registers */
if (query[header_length] == 0x03) {
if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
== UT_REGISTERS_NB_SPECIAL) {
printf("Set an incorrect number of values\n");
MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
UT_REGISTERS_NB_SPECIAL - 1);
} else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
== UT_REGISTERS_ADDRESS_SPECIAL) {
printf("Reply to this special register address by an exception\n");
modbus_reply_exception(ctx, query,
MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
continue;
} else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
== UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
const int RAW_REQ_LENGTH = 5;
uint8_t raw_req[] = {
(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
0x03,
0x02, 0x00, 0x00
};
printf("Reply with an invalid TID or slave\n");
modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
continue;
} else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
== UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
printf("Sleep 0.5 s before replying\n");
usleep(500000);
} else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
== UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
/* Test low level only available in TCP mode */
/* Catch the reply and send reply byte a byte */
uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
int req_length = 11;
int w_s = modbus_get_socket(ctx);
/* Copy TID */
req[1] = query[1];
for (i=0; i < req_length; i++) {
printf("(%.2X)", req[i]);
usleep(500);
send(w_s, req + i, 1, MSG_NOSIGNAL);
}
continue;
}
}
rc = modbus_reply(ctx, query, rc, mb_mapping);
if (rc == -1) {
break;
}
}
printf("Quit the loop: %s\n", modbus_strerror(errno));
if (use_backend == TCP) {
if (s != -1) {
close(s);
}
}
modbus_mapping_free(mb_mapping);
free(query);
/* For RTU */
modbus_close(ctx);
modbus_free(ctx);
return 0;
}