rs232d.c

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

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/