Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@7:cbb5a2a2a0d2, 2020-02-26 (annotated)
- Committer:
- morgandu
- Date:
- Wed Feb 26 07:55:51 2020 +0000
- Revision:
- 7:cbb5a2a2a0d2
- Parent:
- 6:dc9f344f4bf0
- Child:
- 8:e9248126f512
Remove all deprecated API. No more "xxx is deprecated" warnings.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| morgandu | 0:11bc39d0f367 | 1 | /* |
| morgandu | 0:11bc39d0f367 | 2 | * Copyright (c) 2017 Nuvoton Tecnology Corp. All rights reserved. |
| morgandu | 0:11bc39d0f367 | 3 | * |
| morgandu | 0:11bc39d0f367 | 4 | * The code is forward data from UART port to Ethernet, and vice versa. |
| morgandu | 0:11bc39d0f367 | 5 | * |
| morgandu | 0:11bc39d0f367 | 6 | * |
| morgandu | 0:11bc39d0f367 | 7 | */ |
| morgandu | 0:11bc39d0f367 | 8 | |
| morgandu | 0:11bc39d0f367 | 9 | #include "ste_config.h" |
| morgandu | 0:11bc39d0f367 | 10 | |
| morgandu | 7:cbb5a2a2a0d2 | 11 | |
| morgandu | 7:cbb5a2a2a0d2 | 12 | /* If choose static IP, specify the default IP address here. */ |
| morgandu | 0:11bc39d0f367 | 13 | #if 0 |
| morgandu | 0:11bc39d0f367 | 14 | // private IP address for general purpose |
| morgandu | 0:11bc39d0f367 | 15 | #define IP_ADDRESS "192.168.1.2" |
| morgandu | 0:11bc39d0f367 | 16 | #define NETWORK_MASK "255.255.255.0" |
| morgandu | 0:11bc39d0f367 | 17 | #define GATEWAY_ADDRESS "192.168.1.1" |
| morgandu | 0:11bc39d0f367 | 18 | #else |
| morgandu | 0:11bc39d0f367 | 19 | // private IP address only for test with Windows when DHCP server doesn't exist. |
| morgandu | 2:35f2cc8b05fa | 20 | // Windows set its LAN IP address to 169.254.xxx.xxx / 255.255.0.0 |
| morgandu | 0:11bc39d0f367 | 21 | #define IP_ADDRESS "169.254.108.2" |
| morgandu | 0:11bc39d0f367 | 22 | #define NETWORK_MASK "255.255.0.0" |
| morgandu | 0:11bc39d0f367 | 23 | #define GATEWAY_ADDRESS "169.254.108.1" |
| morgandu | 0:11bc39d0f367 | 24 | #endif |
| morgandu | 0:11bc39d0f367 | 25 | |
| morgandu | 7:cbb5a2a2a0d2 | 26 | /* Default IP configuration for Ethernet network */ |
| morgandu | 0:11bc39d0f367 | 27 | S_NET_CONFIG net_config = {IP_STATIC_MODE, IP_ADDRESS, NETWORK_MASK, GATEWAY_ADDRESS}; |
| morgandu | 0:11bc39d0f367 | 28 | |
| morgandu | 6:dc9f344f4bf0 | 29 | #if defined (TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487) |
| morgandu | 5:7e7ad7c079fe | 30 | BufferedSerial serial_0(PB_3, PB_2, 256, 4); // UART1 |
| morgandu | 1:c53f82eb6b42 | 31 | BufferedSerial serial_1(PA_5, PA_4, 256, 4); // UART5 |
| morgandu | 5:7e7ad7c079fe | 32 | //BufferedSerial serial_2(PB_13, PB_12, 256, 4); // UART0, Debug |
| morgandu | 0:11bc39d0f367 | 33 | |
| morgandu | 0:11bc39d0f367 | 34 | #elif defined (TARGET_NUMAKER_PFM_NUC472) |
| morgandu | 0:11bc39d0f367 | 35 | BufferedSerial serial_0(PH_1, PH_0, 256, 4); // UART4 |
| morgandu | 0:11bc39d0f367 | 36 | BufferedSerial serial_1(PG_2, PG_1, 256, 4); // UART0 |
| morgandu | 0:11bc39d0f367 | 37 | BufferedSerial serial_2(PC_11, PC_10, 256, 4); // UART2 |
| morgandu | 0:11bc39d0f367 | 38 | |
| morgandu | 7:cbb5a2a2a0d2 | 39 | #elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) || defined(TARGET_NUMAKER_PFM_M2351) |
| morgandu | 0:11bc39d0f367 | 40 | #error The board has no Ethernet. |
| morgandu | 0:11bc39d0f367 | 41 | |
| morgandu | 0:11bc39d0f367 | 42 | #else |
| morgandu | 0:11bc39d0f367 | 43 | #error define UART ports for your board. |
| morgandu | 0:11bc39d0f367 | 44 | #endif |
| morgandu | 0:11bc39d0f367 | 45 | |
| morgandu | 0:11bc39d0f367 | 46 | /* Default configuration for network ports and UART ports, etc. */ |
| morgandu | 0:11bc39d0f367 | 47 | S_PORT_CONFIG port_config[MAX_UART_PORTS] = { |
| morgandu | 0:11bc39d0f367 | 48 | |
| morgandu | 0:11bc39d0f367 | 49 | #if MAX_UART_PORTS == 1 |
| morgandu | 0:11bc39d0f367 | 50 | {NET_SERVER_MODE, NET_PORT_BASE, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None} |
| morgandu | 0:11bc39d0f367 | 51 | |
| morgandu | 0:11bc39d0f367 | 52 | #elif MAX_UART_PORTS == 2 |
| morgandu | 0:11bc39d0f367 | 53 | {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}, |
| morgandu | 0:11bc39d0f367 | 54 | {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None} |
| morgandu | 0:11bc39d0f367 | 55 | |
| morgandu | 0:11bc39d0f367 | 56 | #elif MAX_UART_PORTS == 3 |
| morgandu | 0:11bc39d0f367 | 57 | {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}, |
| morgandu | 0:11bc39d0f367 | 58 | {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}, |
| morgandu | 0:11bc39d0f367 | 59 | {NET_SERVER_MODE, NET_PORT_BASE + 2, &serial_2, DEFAULT_UART_BAUD, 8, 1, SerialBase::None} |
| morgandu | 0:11bc39d0f367 | 60 | |
| morgandu | 0:11bc39d0f367 | 61 | #else |
| morgandu | 0:11bc39d0f367 | 62 | #error You have to define ports mapping table. |
| morgandu | 0:11bc39d0f367 | 63 | #endif |
| morgandu | 0:11bc39d0f367 | 64 | }; |
| morgandu | 0:11bc39d0f367 | 65 | |
| morgandu | 0:11bc39d0f367 | 66 | EthernetInterface eth; |
| morgandu | 0:11bc39d0f367 | 67 | |
| morgandu | 7:cbb5a2a2a0d2 | 68 | #if ENABLE_WEB_CONFIG |
| morgandu | 0:11bc39d0f367 | 69 | |
| morgandu | 0:11bc39d0f367 | 70 | /* Declare the SD card as storage */ |
| morgandu | 7:cbb5a2a2a0d2 | 71 | NuSDBlockDevice *bd = new NuSDBlockDevice(); |
| morgandu | 0:11bc39d0f367 | 72 | FATFileSystem fs("fs"); |
| morgandu | 0:11bc39d0f367 | 73 | bool SD_Card_Mounted = FALSE; |
| morgandu | 0:11bc39d0f367 | 74 | |
| morgandu | 0:11bc39d0f367 | 75 | #endif |
| morgandu | 0:11bc39d0f367 | 76 | |
| morgandu | 0:11bc39d0f367 | 77 | /* --- --- */ |
| morgandu | 0:11bc39d0f367 | 78 | |
| morgandu | 0:11bc39d0f367 | 79 | /* |
| morgandu | 0:11bc39d0f367 | 80 | * Forward serial port data to ethernet, and vice versa. |
| morgandu | 0:11bc39d0f367 | 81 | * |
| morgandu | 0:11bc39d0f367 | 82 | */ |
| morgandu | 0:11bc39d0f367 | 83 | void exchange_data(S_PORT_CONFIG *pmap, TCPSocket *psocket) |
| morgandu | 0:11bc39d0f367 | 84 | { |
| morgandu | 0:11bc39d0f367 | 85 | unsigned char n_buf[256]; |
| morgandu | 0:11bc39d0f367 | 86 | unsigned char s_buf[256]; |
| morgandu | 0:11bc39d0f367 | 87 | int n_len = 0, n_index = 0; |
| morgandu | 0:11bc39d0f367 | 88 | int s_len = 0, s_index = 0; |
| morgandu | 1:c53f82eb6b42 | 89 | unsigned int eth_tx_count = 0; |
| morgandu | 0:11bc39d0f367 | 90 | |
| morgandu | 0:11bc39d0f367 | 91 | while(1) |
| morgandu | 0:11bc39d0f367 | 92 | { |
| morgandu | 0:11bc39d0f367 | 93 | /*** Network to Serial ***/ |
| morgandu | 0:11bc39d0f367 | 94 | |
| morgandu | 1:c53f82eb6b42 | 95 | if (n_len == 0) |
| morgandu | 0:11bc39d0f367 | 96 | { |
| morgandu | 0:11bc39d0f367 | 97 | // net buffer is empty, try to get new data from network. |
| morgandu | 0:11bc39d0f367 | 98 | n_len = psocket->recv(n_buf, sizeof(n_buf)); |
| morgandu | 1:c53f82eb6b42 | 99 | if (n_len == 0) |
| morgandu | 0:11bc39d0f367 | 100 | { |
| morgandu | 1:c53f82eb6b42 | 101 | eth_tx_count += 3; |
| morgandu | 1:c53f82eb6b42 | 102 | } |
| morgandu | 1:c53f82eb6b42 | 103 | else if (n_len == NSAPI_ERROR_WOULD_BLOCK) |
| morgandu | 1:c53f82eb6b42 | 104 | { |
| morgandu | 1:c53f82eb6b42 | 105 | n_len = 0; |
| morgandu | 0:11bc39d0f367 | 106 | } |
| morgandu | 0:11bc39d0f367 | 107 | else if (n_len < 0) |
| morgandu | 0:11bc39d0f367 | 108 | { |
| morgandu | 0:11bc39d0f367 | 109 | printf("Socket Recv Err (%d)\r\n", n_len); |
| morgandu | 0:11bc39d0f367 | 110 | break; |
| morgandu | 0:11bc39d0f367 | 111 | } |
| morgandu | 0:11bc39d0f367 | 112 | } |
| morgandu | 0:11bc39d0f367 | 113 | else |
| morgandu | 0:11bc39d0f367 | 114 | { |
| morgandu | 1:c53f82eb6b42 | 115 | n_index += pmap->pserial->write(n_buf+n_index, n_len-n_index); |
| morgandu | 1:c53f82eb6b42 | 116 | if (n_index == n_len) |
| morgandu | 0:11bc39d0f367 | 117 | { |
| morgandu | 1:c53f82eb6b42 | 118 | n_len = 0; |
| morgandu | 1:c53f82eb6b42 | 119 | n_index = 0; |
| morgandu | 0:11bc39d0f367 | 120 | } |
| morgandu | 0:11bc39d0f367 | 121 | } |
| morgandu | 0:11bc39d0f367 | 122 | |
| morgandu | 0:11bc39d0f367 | 123 | /*** Serial to Network ***/ |
| morgandu | 1:c53f82eb6b42 | 124 | |
| morgandu | 1:c53f82eb6b42 | 125 | // try to get more data from serial port |
| morgandu | 1:c53f82eb6b42 | 126 | for(; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++) |
| morgandu | 1:c53f82eb6b42 | 127 | s_buf[s_index] = pmap->pserial->getc(); |
| morgandu | 0:11bc39d0f367 | 128 | |
| morgandu | 1:c53f82eb6b42 | 129 | if (s_index >= 240 || (s_index != 0 && ++eth_tx_count >= 5)) |
| morgandu | 0:11bc39d0f367 | 130 | { |
| morgandu | 1:c53f82eb6b42 | 131 | s_len = psocket->send(s_buf, s_index); |
| morgandu | 1:c53f82eb6b42 | 132 | |
| morgandu | 1:c53f82eb6b42 | 133 | if (s_len == NSAPI_ERROR_WOULD_BLOCK) |
| morgandu | 1:c53f82eb6b42 | 134 | { |
| morgandu | 1:c53f82eb6b42 | 135 | printf("Socket Send no block.\r\n"); |
| morgandu | 1:c53f82eb6b42 | 136 | } |
| morgandu | 1:c53f82eb6b42 | 137 | else if (s_len < 0) |
| morgandu | 0:11bc39d0f367 | 138 | { |
| morgandu | 1:c53f82eb6b42 | 139 | printf("Socket Send Err (%d)\r\n", s_len); |
| morgandu | 1:c53f82eb6b42 | 140 | break; |
| morgandu | 0:11bc39d0f367 | 141 | } |
| morgandu | 1:c53f82eb6b42 | 142 | else // s_len >= s_index |
| morgandu | 1:c53f82eb6b42 | 143 | { |
| morgandu | 1:c53f82eb6b42 | 144 | unsigned int i; |
| morgandu | 1:c53f82eb6b42 | 145 | |
| morgandu | 1:c53f82eb6b42 | 146 | // move remain data if existed. |
| morgandu | 1:c53f82eb6b42 | 147 | for(i=0; s_len < s_index; i++, s_len++) |
| morgandu | 1:c53f82eb6b42 | 148 | s_buf[i] = s_buf[s_len]; |
| morgandu | 1:c53f82eb6b42 | 149 | |
| morgandu | 1:c53f82eb6b42 | 150 | s_index = i; |
| morgandu | 1:c53f82eb6b42 | 151 | eth_tx_count = 0; |
| morgandu | 1:c53f82eb6b42 | 152 | } |
| morgandu | 1:c53f82eb6b42 | 153 | } |
| morgandu | 0:11bc39d0f367 | 154 | } |
| morgandu | 0:11bc39d0f367 | 155 | } |
| morgandu | 1:c53f82eb6b42 | 156 | |
| morgandu | 0:11bc39d0f367 | 157 | void bridge_net_client(S_PORT_CONFIG *pmap) |
| morgandu | 0:11bc39d0f367 | 158 | { |
| morgandu | 0:11bc39d0f367 | 159 | TCPSocket socket; |
| morgandu | 7:cbb5a2a2a0d2 | 160 | SocketAddress server_ipaddr; |
| morgandu | 0:11bc39d0f367 | 161 | nsapi_error_t err; |
| morgandu | 0:11bc39d0f367 | 162 | |
| morgandu | 0:11bc39d0f367 | 163 | printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 164 | |
| morgandu | 0:11bc39d0f367 | 165 | if ((err=socket.open(ð)) < 0) |
| morgandu | 0:11bc39d0f367 | 166 | { |
| morgandu | 0:11bc39d0f367 | 167 | printf("TCP socket can't open (%d)(%x).\r\n", err, (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 168 | return; |
| morgandu | 0:11bc39d0f367 | 169 | } |
| morgandu | 0:11bc39d0f367 | 170 | |
| morgandu | 0:11bc39d0f367 | 171 | printf("Connecting server %s:%d ...\r\n", pmap->server_addr, pmap->server_port); |
| morgandu | 0:11bc39d0f367 | 172 | while(1) |
| morgandu | 0:11bc39d0f367 | 173 | { |
| morgandu | 7:cbb5a2a2a0d2 | 174 | server_ipaddr.set_ip_address(pmap->server_addr); |
| morgandu | 7:cbb5a2a2a0d2 | 175 | server_ipaddr.set_port(pmap->server_port); |
| morgandu | 7:cbb5a2a2a0d2 | 176 | if ((err=socket.connect(server_ipaddr)) >= 0) |
| morgandu | 1:c53f82eb6b42 | 177 | { |
| morgandu | 1:c53f82eb6b42 | 178 | printf("\r\nConnected."); |
| morgandu | 0:11bc39d0f367 | 179 | break; |
| morgandu | 1:c53f82eb6b42 | 180 | } |
| morgandu | 0:11bc39d0f367 | 181 | } |
| morgandu | 0:11bc39d0f367 | 182 | |
| morgandu | 0:11bc39d0f367 | 183 | socket.set_timeout(1); |
| morgandu | 0:11bc39d0f367 | 184 | exchange_data(pmap, &socket); |
| morgandu | 6:dc9f344f4bf0 | 185 | socket.close(); |
| morgandu | 0:11bc39d0f367 | 186 | } |
| morgandu | 0:11bc39d0f367 | 187 | |
| morgandu | 0:11bc39d0f367 | 188 | void bridge_net_server(S_PORT_CONFIG *pmap) |
| morgandu | 0:11bc39d0f367 | 189 | { |
| morgandu | 6:dc9f344f4bf0 | 190 | TCPSocket tcp_server; |
| morgandu | 6:dc9f344f4bf0 | 191 | TCPSocket *client_socket; |
| morgandu | 7:cbb5a2a2a0d2 | 192 | SocketAddress client_ipaddr; |
| morgandu | 0:11bc39d0f367 | 193 | nsapi_error_t err; |
| morgandu | 0:11bc39d0f367 | 194 | |
| morgandu | 0:11bc39d0f367 | 195 | printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 196 | |
| morgandu | 0:11bc39d0f367 | 197 | if ((err=tcp_server.open(ð)) < 0) |
| morgandu | 0:11bc39d0f367 | 198 | { |
| morgandu | 0:11bc39d0f367 | 199 | printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 200 | return; |
| morgandu | 0:11bc39d0f367 | 201 | } |
| morgandu | 7:cbb5a2a2a0d2 | 202 | if ((err=tcp_server.bind(pmap->port)) < 0) |
| morgandu | 0:11bc39d0f367 | 203 | { |
| morgandu | 0:11bc39d0f367 | 204 | printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 205 | return; |
| morgandu | 0:11bc39d0f367 | 206 | } |
| morgandu | 0:11bc39d0f367 | 207 | if ((err=tcp_server.listen(1)) < 0) |
| morgandu | 0:11bc39d0f367 | 208 | { |
| morgandu | 0:11bc39d0f367 | 209 | printf("TCP server can't listen (%d)(%x).\r\n", err, (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 210 | return; |
| morgandu | 0:11bc39d0f367 | 211 | } |
| morgandu | 0:11bc39d0f367 | 212 | |
| morgandu | 0:11bc39d0f367 | 213 | while(1) |
| morgandu | 0:11bc39d0f367 | 214 | { |
| morgandu | 6:dc9f344f4bf0 | 215 | client_socket = tcp_server.accept(&err); |
| morgandu | 6:dc9f344f4bf0 | 216 | if (err < 0) // && err != NSAPI_ERROR_WOULD_BLOCK) |
| morgandu | 0:11bc39d0f367 | 217 | { |
| morgandu | 0:11bc39d0f367 | 218 | printf("TCP server fail to accept connection (%d)(%x).\r\n", err, (unsigned int)pmap); |
| morgandu | 0:11bc39d0f367 | 219 | return; |
| morgandu | 0:11bc39d0f367 | 220 | } |
| morgandu | 6:dc9f344f4bf0 | 221 | else |
| morgandu | 6:dc9f344f4bf0 | 222 | { |
| morgandu | 7:cbb5a2a2a0d2 | 223 | client_socket->getpeername(&client_ipaddr); |
| morgandu | 7:cbb5a2a2a0d2 | 224 | printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_ipaddr.get_ip_address(), client_ipaddr.get_port()); |
| morgandu | 6:dc9f344f4bf0 | 225 | |
| morgandu | 6:dc9f344f4bf0 | 226 | client_socket->set_timeout(1); |
| morgandu | 6:dc9f344f4bf0 | 227 | exchange_data(pmap, client_socket); |
| morgandu | 6:dc9f344f4bf0 | 228 | client_socket->close(); |
| morgandu | 6:dc9f344f4bf0 | 229 | } |
| morgandu | 0:11bc39d0f367 | 230 | } |
| morgandu | 0:11bc39d0f367 | 231 | } |
| morgandu | 0:11bc39d0f367 | 232 | |
| morgandu | 0:11bc39d0f367 | 233 | int main() |
| morgandu | 0:11bc39d0f367 | 234 | { |
| morgandu | 7:cbb5a2a2a0d2 | 235 | SocketAddress ip_addr; |
| morgandu | 7:cbb5a2a2a0d2 | 236 | SocketAddress ip_mask; |
| morgandu | 7:cbb5a2a2a0d2 | 237 | SocketAddress ip_gwaddr; |
| morgandu | 7:cbb5a2a2a0d2 | 238 | |
| morgandu | 0:11bc39d0f367 | 239 | printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION); |
| morgandu | 0:11bc39d0f367 | 240 | printf("Start Serial-to-Ethernet...\r\n"); |
| morgandu | 0:11bc39d0f367 | 241 | |
| morgandu | 7:cbb5a2a2a0d2 | 242 | #if ENABLE_WEB_CONFIG |
| morgandu | 0:11bc39d0f367 | 243 | |
| morgandu | 0:11bc39d0f367 | 244 | /* Restore configuration from SD card */ |
| morgandu | 0:11bc39d0f367 | 245 | |
| morgandu | 7:cbb5a2a2a0d2 | 246 | printf("Mounting SD card...\r\n"); |
| morgandu | 7:cbb5a2a2a0d2 | 247 | SD_Card_Mounted = (fs.mount(bd) == 0); |
| morgandu | 0:11bc39d0f367 | 248 | if (SD_Card_Mounted) |
| morgandu | 0:11bc39d0f367 | 249 | { |
| morgandu | 7:cbb5a2a2a0d2 | 250 | printf("SD card mounted. Read configuration file...\r\n"); |
| morgandu | 0:11bc39d0f367 | 251 | FILE *fd = fopen(SER_CONFIG_FILE, "r"); |
| morgandu | 0:11bc39d0f367 | 252 | if (fd != NULL) |
| morgandu | 0:11bc39d0f367 | 253 | { |
| morgandu | 0:11bc39d0f367 | 254 | char pBuf[sizeof(port_config)+2]; |
| morgandu | 0:11bc39d0f367 | 255 | int len = fread(pBuf, 1, sizeof(port_config)+2, fd); |
| morgandu | 0:11bc39d0f367 | 256 | if (len == (sizeof(port_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T') |
| morgandu | 0:11bc39d0f367 | 257 | { |
| morgandu | 0:11bc39d0f367 | 258 | printf("Set Serial ports from config file in SD card.\r\n"); |
| morgandu | 0:11bc39d0f367 | 259 | memcpy(port_config, pBuf+2, sizeof(port_config)); |
| morgandu | 0:11bc39d0f367 | 260 | } |
| morgandu | 0:11bc39d0f367 | 261 | else |
| morgandu | 0:11bc39d0f367 | 262 | printf("Incorrect serial config file.\r\n"); |
| morgandu | 0:11bc39d0f367 | 263 | |
| morgandu | 0:11bc39d0f367 | 264 | fclose(fd); |
| morgandu | 0:11bc39d0f367 | 265 | } |
| morgandu | 0:11bc39d0f367 | 266 | else |
| morgandu | 0:11bc39d0f367 | 267 | printf("Can't open serial config file.\r\n"); |
| morgandu | 0:11bc39d0f367 | 268 | |
| morgandu | 0:11bc39d0f367 | 269 | fd = fopen(NET_CONFIG_FILE, "r"); |
| morgandu | 0:11bc39d0f367 | 270 | if (fd != NULL) |
| morgandu | 0:11bc39d0f367 | 271 | { |
| morgandu | 0:11bc39d0f367 | 272 | char pBuf[sizeof(net_config)+2]; |
| morgandu | 0:11bc39d0f367 | 273 | int len = fread(pBuf, 1, sizeof(net_config)+2, fd); |
| morgandu | 0:11bc39d0f367 | 274 | if (len == (sizeof(net_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T') |
| morgandu | 0:11bc39d0f367 | 275 | { |
| morgandu | 0:11bc39d0f367 | 276 | printf("Set network from config file in SD card.\r\n"); |
| morgandu | 0:11bc39d0f367 | 277 | memcpy(&net_config, pBuf+2, sizeof(net_config)); |
| morgandu | 0:11bc39d0f367 | 278 | } |
| morgandu | 0:11bc39d0f367 | 279 | else |
| morgandu | 0:11bc39d0f367 | 280 | printf("Incorrect network config file.\r\n"); |
| morgandu | 0:11bc39d0f367 | 281 | |
| morgandu | 0:11bc39d0f367 | 282 | fclose(fd); |
| morgandu | 0:11bc39d0f367 | 283 | } |
| morgandu | 0:11bc39d0f367 | 284 | else |
| morgandu | 0:11bc39d0f367 | 285 | printf("Can't open network config file.\r\n"); |
| morgandu | 0:11bc39d0f367 | 286 | } |
| morgandu | 0:11bc39d0f367 | 287 | else |
| morgandu | 0:11bc39d0f367 | 288 | { |
| morgandu | 0:11bc39d0f367 | 289 | printf("Can't find SD card.\r\n"); |
| morgandu | 0:11bc39d0f367 | 290 | } |
| morgandu | 0:11bc39d0f367 | 291 | |
| morgandu | 0:11bc39d0f367 | 292 | #endif |
| morgandu | 0:11bc39d0f367 | 293 | |
| morgandu | 7:cbb5a2a2a0d2 | 294 | /* Configure serial ports */ |
| morgandu | 0:11bc39d0f367 | 295 | printf("Configure UART ports...\r\n"); |
| morgandu | 0:11bc39d0f367 | 296 | for(int i=0; i<MAX_UART_PORTS; i++) |
| morgandu | 0:11bc39d0f367 | 297 | { |
| morgandu | 0:11bc39d0f367 | 298 | port_config[i].pserial->baud(port_config[i].baud); |
| morgandu | 0:11bc39d0f367 | 299 | port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop); |
| morgandu | 0:11bc39d0f367 | 300 | } |
| morgandu | 0:11bc39d0f367 | 301 | |
| morgandu | 7:cbb5a2a2a0d2 | 302 | /* Configure network IP address */ |
| morgandu | 0:11bc39d0f367 | 303 | if (net_config.mode == IP_STATIC_MODE) |
| morgandu | 0:11bc39d0f367 | 304 | { |
| morgandu | 0:11bc39d0f367 | 305 | printf("Start Ethernet in Static mode.\r\n"); |
| morgandu | 0:11bc39d0f367 | 306 | eth.disconnect(); |
| morgandu | 7:cbb5a2a2a0d2 | 307 | |
| morgandu | 7:cbb5a2a2a0d2 | 308 | ip_addr.set_ip_address(net_config.ip); |
| morgandu | 7:cbb5a2a2a0d2 | 309 | ip_mask.set_ip_address(net_config.mask); |
| morgandu | 7:cbb5a2a2a0d2 | 310 | ip_gwaddr.set_ip_address(net_config.gateway); |
| morgandu | 7:cbb5a2a2a0d2 | 311 | ((NetworkInterface *)ð)->set_network(ip_addr, ip_mask, ip_gwaddr); |
| morgandu | 0:11bc39d0f367 | 312 | } |
| morgandu | 0:11bc39d0f367 | 313 | else |
| morgandu | 0:11bc39d0f367 | 314 | printf("Start Ethernet in DHCP mode.\r\n"); |
| morgandu | 0:11bc39d0f367 | 315 | |
| morgandu | 0:11bc39d0f367 | 316 | eth.connect(); |
| morgandu | 7:cbb5a2a2a0d2 | 317 | eth.get_ip_address(&ip_addr); |
| morgandu | 7:cbb5a2a2a0d2 | 318 | printf("IP Address is %s\r\n", ip_addr.get_ip_address()); |
| morgandu | 5:7e7ad7c079fe | 319 | |
| morgandu | 0:11bc39d0f367 | 320 | Thread thread[MAX_UART_PORTS]; |
| morgandu | 0:11bc39d0f367 | 321 | |
| morgandu | 7:cbb5a2a2a0d2 | 322 | /* Folk thread for each serial port */ |
| morgandu | 0:11bc39d0f367 | 323 | for(int i=0; i<MAX_UART_PORTS; i++) |
| morgandu | 0:11bc39d0f367 | 324 | { |
| morgandu | 0:11bc39d0f367 | 325 | if (port_config[i].mode == NET_SERVER_MODE) |
| morgandu | 0:11bc39d0f367 | 326 | { |
| morgandu | 0:11bc39d0f367 | 327 | thread[i].start(callback(bridge_net_server, &(port_config[i]))); |
| morgandu | 0:11bc39d0f367 | 328 | } |
| morgandu | 0:11bc39d0f367 | 329 | else // if (port_config[i].mode == TCP_CLIENT_MODE) |
| morgandu | 0:11bc39d0f367 | 330 | { |
| morgandu | 0:11bc39d0f367 | 331 | thread[i].start(callback(bridge_net_client, &(port_config[i]))); |
| morgandu | 0:11bc39d0f367 | 332 | } |
| morgandu | 0:11bc39d0f367 | 333 | } |
| morgandu | 0:11bc39d0f367 | 334 | |
| morgandu | 7:cbb5a2a2a0d2 | 335 | #if ENABLE_WEB_CONFIG |
| morgandu | 0:11bc39d0f367 | 336 | |
| morgandu | 0:11bc39d0f367 | 337 | /*** main thread to be a web server for configuration ***/ |
| morgandu | 0:11bc39d0f367 | 338 | start_httpd(); |
| morgandu | 0:11bc39d0f367 | 339 | |
| morgandu | 0:11bc39d0f367 | 340 | #endif |
| morgandu | 0:11bc39d0f367 | 341 | |
| morgandu | 0:11bc39d0f367 | 342 | while(1); |
| morgandu | 0:11bc39d0f367 | 343 | |
| morgandu | 0:11bc39d0f367 | 344 | /* end of main task */ |
| morgandu | 0:11bc39d0f367 | 345 | //eth.disconnect(); |
| morgandu | 0:11bc39d0f367 | 346 | } |