Advertisement
kneefer

RNUT-OS lab2 (Ethernet)

Dec 15th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 KB | None | 0 0
  1. /*!
  2.  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3.  *
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. Neither the name of the copyright holders nor the names of
  16.  *    contributors may be used to endorse or promote products derived
  17.  *    from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27.  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29.  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  * For additional information see http://www.ethernut.de/
  33.  */
  34.  
  35. /*!
  36.  * $Id: rs232d.c 3307 2011-02-11 16:12:43Z olereinhardt $
  37.  */
  38.  
  39. /*!
  40.  * \example rs232d/rs232d.c
  41.  *
  42.  * Simple RS232 server. Use a serial cable to connect the RS232 port
  43.  * of the Ethernut Board with a COM port of a PC. Start a terminal
  44.  * program and a telnet client on the PC. Telnet should connect to
  45.  * the Ethernut Board.
  46.  *
  47.  * Characters typed in the telnet window will appear in the terminal
  48.  * program window and vice versa. Baudrate is 9600.
  49.  *
  50.  */
  51.  
  52. #include <dev/hd44780_bus.h>
  53. #include <dev/term.h>
  54. #include <dev/board.h>
  55. #include <sys/heap.h>
  56. #include <sys/thread.h>
  57. #include <sys/timer.h>
  58. #include <sys/socket.h>
  59. #include <sys/confnet.h>
  60.  
  61. #include <stdlib.h>
  62. #include <stdio.h>
  63. #include <string.h>
  64. #include <io.h>
  65. #include <fcntl.h>
  66.  
  67. #include <arpa/inet.h>
  68. #include <net/if_var.h>
  69. #include <pro/dhcp.h>
  70.  
  71. #define BUFFERSIZE  128
  72. #define TCPPORT     23
  73.  
  74. typedef struct {
  75.     FILE *cd_rs232;
  76.     FILE *cd_tcpip;
  77.     char cd_connected;
  78. } CHANNEL;
  79.  
  80. /*
  81.  * Transfer data from input stream to output stream.
  82.  */
  83. void StreamCopy(FILE * istream, FILE * ostream, char *cop)
  84. {
  85.     int cnt;
  86.     char *buff;
  87.  
  88.     buff = malloc(BUFFERSIZE);
  89.     while (*cop) {
  90.         if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0)
  91.             break;
  92.         if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0)
  93.             break;
  94.         if (*cop && fflush(ostream))
  95.             break;
  96.     }
  97.     *cop = 0;
  98.     free(buff);
  99. }
  100.  
  101. /*
  102.  * From RS232 to socket.
  103.  */
  104. THREAD(Receiver, arg)
  105. {
  106.     CHANNEL *cdp = arg;
  107.  
  108.     for (;;) {
  109.         if (cdp->cd_connected) {
  110.             NutThreadSetPriority(64);
  111.             /*
  112.              * We are reading from the UART without any timeout. So we
  113.              * won't return immediately when disconnected.
  114.              */
  115.             StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected);
  116.             NutThreadSetPriority(128);
  117.         }
  118.         NutThreadYield();
  119.     }
  120. }
  121.  
  122. /*
  123.  * Main application routine.
  124.  *
  125.  * Nut/OS automatically calls this entry after initialization.
  126.  */
  127. int main(void)
  128. {
  129.     TCPSOCKET *sock;
  130.     CHANNEL cd;
  131.     uint32_t baud = 9600;
  132.     FILE *lcd;
  133.  
  134.     /*
  135.      * Register our devices.
  136.      */
  137.     NutRegisterDevice(&DEV_UART, 0, 0);
  138.     NutRegisterDevice(&devLcdBus, 0xFF04, 0);
  139. #ifndef DEV_ETHER
  140.     for (;;);
  141. #else
  142.     NutRegisterDevice(&DEV_ETHER, 0xC000, 5);
  143.     lcd = fopen("lcdbus", "w");
  144.  
  145.     /*
  146.      * Setup the uart device.
  147.      */
  148.     cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b");
  149.     _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
  150.     fprintf(lcd, "Searching for ip\r");
  151.     /*
  152.      * Setup the ethernet device. Try DHCP first. If this is
  153.      * the first time boot with empty EEPROM and no DHCP server
  154.      * was found, use hardcoded values.
  155.      */
  156.     if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
  157.         /* No valid EEPROM contents, use hard coded MAC. */
  158.         uint8_t my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 };
  159.  
  160.         if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
  161.             /* No DHCP server found, use hard coded IP address. */
  162.             uint32_t ip_addr = inet_addr("192.168.192.100");
  163.             uint32_t ip_mask = inet_addr("255.255.255.0");
  164.  
  165.             NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
  166.             /* If not in a local network, we must also call
  167.                NutIpRouteAdd() to configure the routing. */
  168.         }
  169.     }
  170.  
  171.     fprintf(lcd, "Ok\r %s", inet_ntoa(confnet.cdn_ip_addr));
  172.     /*
  173.      * Start a RS232 receiver thread.
  174.      */
  175.     NutThreadCreate("xmit", Receiver, &cd, 512);
  176.  
  177.     /*
  178.      * Now loop endless for connections.
  179.      */
  180.     cd.cd_connected = 0;
  181.     for (;;) {
  182.         /*
  183.          * Create a socket and listen for a client.
  184.          */
  185.         sock = NutTcpCreateSocket();
  186.         NutTcpAccept(sock, TCPPORT);
  187.  
  188.         /*
  189.          * Open a stdio stream assigned to the connected socket.
  190.          */
  191.         cd.cd_tcpip = _fdopen((int) sock, "r+b");
  192.         cd.cd_connected = 1;
  193.  
  194.         /*
  195.          * Call RS232 transmit routine. On return we will be
  196.          * disconnected again.
  197.          */
  198.         StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
  199.  
  200.         /*
  201.          * Close the stream.
  202.          */
  203.         fclose(cd.cd_tcpip);
  204.  
  205.         /*
  206.          * Close our socket.
  207.          */
  208.         NutTcpCloseSocket(sock);
  209.     }
  210. #endif
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement