Characters typed in the telnet window will appear in the terminal program window and vice versa. Baudrate is 9600.
00001 00080 #include <dev/board.h> 00081 00082 #include <sys/heap.h> 00083 #include <sys/thread.h> 00084 #include <sys/timer.h> 00085 #include <sys/socket.h> 00086 00087 #include <stdlib.h> 00088 #include <stdio.h> 00089 #include <string.h> 00090 #include <io.h> 00091 #include <fcntl.h> 00092 00093 #include <arpa/inet.h> 00094 00095 #include <pro/dhcp.h> 00096 00097 #define BUFFERSIZE 128 00098 #define TCPPORT 23 00099 00100 typedef struct { 00101 FILE *cd_rs232; 00102 FILE *cd_tcpip; 00103 char cd_connected; 00104 } CHANNEL; 00105 00106 /* 00107 * Transfer data from input stream to output stream. 00108 */ 00109 void StreamCopy(FILE * istream, FILE * ostream, char *cop) 00110 { 00111 int cnt; 00112 char *buff; 00113 00114 buff = malloc(BUFFERSIZE); 00115 while (*cop) { 00116 if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0) 00117 break; 00118 if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0) 00119 break; 00120 if (*cop && fflush(ostream)) 00121 break; 00122 } 00123 *cop = 0; 00124 free(buff); 00125 } 00126 00127 /* 00128 * From RS232 to socket. 00129 */ 00130 THREAD(Receiver, arg) 00131 { 00132 CHANNEL *cdp = arg; 00133 00134 for (;;) { 00135 if (cdp->cd_connected) { 00136 NutThreadSetPriority(64); 00137 /* 00138 * We are reading from the UART without any timeout. So we 00139 * won't return immediately when disconnected. 00140 */ 00141 StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected); 00142 NutThreadSetPriority(128); 00143 } 00144 NutThreadYield(); 00145 } 00146 } 00147 00148 /* 00149 * Main application routine. 00150 * 00151 * Nut/OS automatically calls this entry after initialization. 00152 */ 00153 int main(void) 00154 { 00155 TCPSOCKET *sock; 00156 CHANNEL cd; 00157 u_long baud = 9600; 00158 00159 /* 00160 * Register our devices. 00161 */ 00162 NutRegisterDevice(&DEV_UART, 0, 0); 00163 NutRegisterDevice(&DEV_ETHER, 0x8300, 5); 00164 00165 /* 00166 * Setup the uart device. 00167 */ 00168 cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b"); 00169 _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud); 00170 00171 /* 00172 * Setup the ethernet device. Try DHCP first. If this is 00173 * the first time boot with empty EEPROM and no DHCP server 00174 * was found, use hardcoded values. 00175 */ 00176 if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) { 00177 /* No valid EEPROM contents, use hard coded MAC. */ 00178 u_char my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 }; 00179 00180 if (NutDhcpIfConfig("eth0", my_mac, 60000)) { 00181 /* No DHCP server found, use hard coded IP address. */ 00182 u_long ip_addr = inet_addr("192.168.192.100"); 00183 u_long ip_mask = inet_addr("255.255.255.0"); 00184 00185 NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask); 00186 /* If not in a local network, we must also call 00187 NutIpRouteAdd() to configure the routing. */ 00188 } 00189 } 00190 00191 /* 00192 * Start a RS232 receiver thread. 00193 */ 00194 NutThreadCreate("xmit", Receiver, &cd, 512); 00195 00196 /* 00197 * Now loop endless for connections. 00198 */ 00199 cd.cd_connected = 0; 00200 for (;;) { 00201 /* 00202 * Create a socket and listen for a client. 00203 */ 00204 sock = NutTcpCreateSocket(); 00205 NutTcpAccept(sock, TCPPORT); 00206 00207 /* 00208 * Open a stdio stream assigned to the connected socket. 00209 */ 00210 cd.cd_tcpip = _fdopen((int) sock, "r+b"); 00211 cd.cd_connected = 1; 00212 00213 /* 00214 * Call RS232 transmit routine. On return we will be 00215 * disconnected again. 00216 */ 00217 StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected); 00218 00219 /* 00220 * Close the stream. 00221 */ 00222 fclose(cd.cd_tcpip); 00223 00224 /* 00225 * Close our socket. 00226 */ 00227 NutTcpCloseSocket(sock); 00228 } 00229 }