| 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 | // clang-format off |
| 15 | #ifdef _WIN32 |
| 16 | # include <winsock2.h> |
| 17 | #else |
| 18 | # include <sys/socket.h> |
| 19 | #endif |
| 20 | |
| 21 | /* For MinGW */ |
| 22 | #ifndef MSG_NOSIGNAL |
| 23 | # define MSG_NOSIGNAL 0 |
| 24 | #endif |
| 25 | // clang-format on |
| 26 | |
| 27 | #include "unit-test.h" |
| 28 | |
| 29 | enum { |
| 30 | TCP, |
| 31 | TCP_PI, |
| 32 | RTU |
| 33 | }; |
| 34 | |
| 35 | int main(int argc, char *argv[]) |
| 36 | { |
| 37 | int s = -1; |
| 38 | modbus_t *ctx; |
| 39 | modbus_mapping_t *mb_mapping; |
| 40 | int rc; |
| 41 | int i; |
| 42 | int use_backend; |
| 43 | uint8_t *query; |
| 44 | int header_length; |
| 45 | char *ip_or_device; |
| 46 | |
| 47 | if (argc > 1) { |
| 48 | if (strcmp(argv[1], "tcp") == 0) { |
| 49 | use_backend = TCP; |
| 50 | } else if (strcmp(argv[1], "tcppi") == 0) { |
| 51 | use_backend = TCP_PI; |
| 52 | } else if (strcmp(argv[1], "rtu") == 0) { |
| 53 | use_backend = RTU; |
| 54 | } else { |
| 55 | printf("Modbus server for unit testing.\n"); |
| 56 | printf("Usage:\n %s [tcp|tcppi|rtu] [<ip or device>]\n", argv[0]); |
| 57 | printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB0\n\n"); |
| 58 | return -1; |
| 59 | } |
| 60 | } else { |
| 61 | /* By default */ |
| 62 | use_backend = TCP; |
| 63 | } |
| 64 | |
| 65 | if (argc > 2) { |
| 66 | ip_or_device = argv[2]; |
| 67 | } else { |
| 68 | switch (use_backend) { |
| 69 | case TCP: |
| 70 | ip_or_device = "127.0.0.1"; |
| 71 | break; |
| 72 | case TCP_PI: |
| 73 | ip_or_device = "::1"; |
| 74 | break; |
| 75 | case RTU: |
| 76 | ip_or_device = "/dev/ttyUSB0"; |
| 77 | break; |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (use_backend == TCP) { |
| 84 | ctx = modbus_new_tcp(ip_or_device, 1502); |
| 85 | query = malloc(MODBUS_TCP_MAX_ADU_LENGTH); |
| 86 | } else if (use_backend == TCP_PI) { |
| 87 | ctx = modbus_new_tcp_pi(ip_or_device, "1502"); |
| 88 | query = malloc(MODBUS_TCP_MAX_ADU_LENGTH); |
| 89 | } else { |
| 90 | ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1); |
| 91 | modbus_set_slave(ctx, SERVER_ID); |
| 92 | query = malloc(MODBUS_RTU_MAX_ADU_LENGTH); |
| 93 | } |
| 94 | |
| 95 | header_length = modbus_get_header_length(ctx); |
| 96 | |
| 97 | modbus_set_debug(ctx, TRUE); |
| 98 | |
| 99 | mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS, |
| 100 | UT_BITS_NB, |
| 101 | UT_INPUT_BITS_ADDRESS, |
| 102 | UT_INPUT_BITS_NB, |
| 103 | UT_REGISTERS_ADDRESS, |
| 104 | UT_REGISTERS_NB_MAX, |
| 105 | UT_INPUT_REGISTERS_ADDRESS, |
| 106 | UT_INPUT_REGISTERS_NB); |
| 107 | if (mb_mapping == NULL) { |
| 108 | fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno)); |
| 109 | modbus_free(ctx); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | /* Examples from PI_MODBUS_300.pdf. |
| 114 | Only the read-only input values are assigned. */ |
| 115 | |
| 116 | /* Initialize input values that's can be only done server side. */ |
| 117 | modbus_set_bits_from_bytes( |
| 118 | mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB); |
| 119 | |
| 120 | /* Initialize values of INPUT REGISTERS */ |
| 121 | for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) { |
| 122 | mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i]; |
| 123 | } |
| 124 | |
| 125 | if (use_backend == TCP) { |
| 126 | s = modbus_tcp_listen(ctx, 1); |
| 127 | modbus_tcp_accept(ctx, &s); |
| 128 | } else if (use_backend == TCP_PI) { |
| 129 | s = modbus_tcp_pi_listen(ctx, 1); |
| 130 | modbus_tcp_pi_accept(ctx, &s); |
| 131 | } else { |
| 132 | rc = modbus_connect(ctx); |
| 133 | if (rc == -1) { |
| 134 | fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno)); |
| 135 | modbus_free(ctx); |
| 136 | return -1; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | for (;;) { |
| 141 | do { |
| 142 | rc = modbus_receive(ctx, query); |
| 143 | /* Filtered queries return 0 */ |
| 144 | } while (rc == 0); |
| 145 | |
| 146 | /* The connection is not closed on errors which require no reply such as |
| 147 | bad CRC in RTU. */ |
| 148 | if (rc == -1) { |
| 149 | if (errno == EMBBADCRC) { |
| 150 | /* Wait for the next request, the query is unusable */ |
| 151 | continue; |
| 152 | } |
| 153 | /* Quit */ |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | uint8_t function = query[header_length]; |
| 158 | uint16_t address = 0; |
| 159 | |
| 160 | /* Only read address when the request carries one (some function |
| 161 | codes like FC 0x07/0x11 have no address in the PDU). */ |
| 162 | if (rc >= header_length + 3) |
| 163 | address = MODBUS_GET_INT16_FROM_INT8(query, header_length + 1); |
| 164 | |
| 165 | /** Special server behavior to test client **/ |
| 166 | if (function == MODBUS_FC_READ_HOLDING_REGISTERS) { |
| 167 | if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) == |
| 168 | UT_REGISTERS_NB_SPECIAL) { |
| 169 | printf("Set an incorrect number of values\n"); |
| 170 | MODBUS_SET_INT16_TO_INT8( |
| 171 | query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1); |
| 172 | } else if (address == UT_REGISTERS_ADDRESS_SPECIAL) { |
| 173 | printf("Reply to this special register address by an exception\n"); |
| 174 | modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY); |
| 175 | continue; |
| 176 | } else if (address == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) { |
| 177 | const int RAW_REQ_LENGTH = 5; |
| 178 | uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF, |
| 179 | 0x03, |
| 180 | 0x02, |
| 181 | 0x00, |
| 182 | 0x00}; |
| 183 | |
| 184 | printf("Reply with an invalid TID or slave\n"); |
| 185 | modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t)); |
| 186 | continue; |
| 187 | } else if (address == UT_REGISTERS_ADDRESS_SLEEP_500_MS) { |
| 188 | printf("Sleep 0.5 s before replying\n"); |
| 189 | usleep(500000); |
| 190 | } else if (address == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) { |
| 191 | /* Test low level only available in TCP mode */ |
| 192 | /* Catch the reply and send reply byte a byte */ |
| 193 | uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00"; |
| 194 | int req_length = 11; |
| 195 | int w_s = modbus_get_socket(ctx); |
| 196 | if (w_s == -1) { |
| 197 | fprintf(stderr, "Unable to get a valid socket in special test\n"); |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | /* Copy TID */ |
| 202 | req[1] = query[1]; |
| 203 | for (i = 0; i < req_length; i++) { |
| 204 | printf("(%.2X)", req[i]); |
| 205 | usleep(5000); |
| 206 | rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL); |
| 207 | if (rc == -1) { |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | continue; |
| 212 | } |
| 213 | } else if (function == MODBUS_FC_WRITE_SINGLE_COIL) { |
| 214 | if (address == UT_BITS_ADDRESS_INVALID_REQUEST_LENGTH) { |
| 215 | // The valid length is lengths of header + checkum + FC + address + value |
| 216 | // (max 12) |
| 217 | rc = 34; |
| 218 | printf( |
| 219 | "Special modbus_write_bit detected. Inject a wrong length value (%d) " |
| 220 | "in modbus_reply\n", |
| 221 | rc); |
| 222 | } |
| 223 | } else if (function == MODBUS_FC_WRITE_SINGLE_REGISTER) { |
| 224 | if (address == UT_REGISTERS_ADDRESS_SPECIAL) { |
| 225 | rc = 45; |
| 226 | printf("Special modbus_write_register detected. Inject a wrong length " |
| 227 | "value (%d) in modbus_reply\n", |
| 228 | rc); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | rc = modbus_reply(ctx, query, rc, mb_mapping); |
| 233 | if (rc == -1) { |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | printf("Quit the loop: %s\n", modbus_strerror(errno)); |
| 239 | |
| 240 | if (use_backend == TCP) { |
| 241 | if (s != -1) { |
| 242 | close(s); |
| 243 | } |
| 244 | } |
| 245 | modbus_mapping_free(mb_mapping); |
| 246 | free(query); |
| 247 | /* For RTU */ |
| 248 | modbus_close(ctx); |
| 249 | modbus_free(ctx); |
| 250 | |
| 251 | return 0; |
| 252 | } |