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 #ifndef DEV_ETHER
00167     for (;;);
00168 #else
00169     NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
00170 
00171     /*
00172      * Setup the uart device.
00173      */
00174     cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b");
00175     _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
00176 
00177     /*
00178      * Setup the ethernet device. Try DHCP first. If this is
00179      * the first time boot with empty EEPROM and no DHCP server
00180      * was found, use hardcoded values.
00181      */
00182     if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
00183         /* No valid EEPROM contents, use hard coded MAC. */
00184         u_char my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 };
00185 
00186         if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
00187             /* No DHCP server found, use hard coded IP address. */
00188             u_long ip_addr = inet_addr("192.168.192.100");
00189             u_long ip_mask = inet_addr("255.255.255.0");
00190 
00191             NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
00192             /* If not in a local network, we must also call 
00193                NutIpRouteAdd() to configure the routing. */
00194         }
00195     }
00196 
00197     /*
00198      * Start a RS232 receiver thread.
00199      */
00200     NutThreadCreate("xmit", Receiver, &cd, 512);
00201 
00202     /*
00203      * Now loop endless for connections.
00204      */
00205     cd.cd_connected = 0;
00206     for (;;) {
00207         /*
00208          * Create a socket and listen for a client.
00209          */
00210         sock = NutTcpCreateSocket();
00211         NutTcpAccept(sock, TCPPORT);
00212 
00213         /*
00214          * Open a stdio stream assigned to the connected socket.
00215          */
00216         cd.cd_tcpip = _fdopen((int) sock, "r+b");
00217         cd.cd_connected = 1;
00218 
00219         /*
00220          * Call RS232 transmit routine. On return we will be
00221          * disconnected again.
00222          */
00223         StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
00224 
00225         /*
00226          * Close the stream.
00227          */
00228         fclose(cd.cd_tcpip);
00229 
00230         /*
00231          * Close our socket.
00232          */
00233         NutTcpCloseSocket(sock);
00234     }
00235 #endif
00236     return 0;
00237 }

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