Debian Codegraph @

tests/random-test-server.c

raw symbols SCIP
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <stdio.h>
8 #ifndef _MSC_VER
9 #include <unistd.h>
10 #endif
11 #include <errno.h>
12 #include <stdlib.h>
13
14 #include <modbus.h>
15
16 int main(void)
17 {
18 int s = -1;
19 modbus_t *ctx;
20 modbus_mapping_t *mb_mapping;
21
22 ctx = modbus_new_tcp("127.0.0.1", 1502);
23 /* modbus_set_debug(ctx, TRUE); */
24
25 mb_mapping = modbus_mapping_new(500, 500, 500, 500);
26 if (mb_mapping == NULL) {
27 fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
28 modbus_free(ctx);
29 return -1;
30 }
31
32 s = modbus_tcp_listen(ctx, 1);
33 modbus_tcp_accept(ctx, &s);
34
35 for (;;) {
36 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
37 int rc;
38
39 rc = modbus_receive(ctx, query);
40 if (rc > 0) {
41 /* rc is the query size */
42 modbus_reply(ctx, query, rc, mb_mapping);
43 } else if (rc == -1) {
44 /* Connection closed by the client or error */
45 break;
46 }
47 }
48
49 printf("Quit the loop: %s\n", modbus_strerror(errno));
50
51 if (s != -1) {
52 close(s);
53 }
54 modbus_mapping_free(mb_mapping);
55 modbus_close(ctx);
56 modbus_free(ctx);
57
58 return 0;
59 }