rs232d.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * $Id: rs232d.c 4115 2012-04-12 21:06:13Z olereinhardt $
  36. */
  37. /*!
  38. * \example rs232d/rs232d.c
  39. *
  40. * Simple RS232 server. Use a serial cable to connect the RS232 port
  41. * of the Ethernut Board with a COM port of a PC. Start a terminal
  42. * program and a telnet client on the PC. Telnet should connect to
  43. * the Ethernut Board.
  44. *
  45. * Characters typed in the telnet window will appear in the terminal
  46. * program window and vice versa. Baudrate is 9600.
  47. *
  48. */
  49. #include <dev/board.h>
  50. #include <sys/heap.h>
  51. #include <sys/thread.h>
  52. #include <sys/timer.h>
  53. #include <sys/socket.h>
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <io.h>
  58. #include <fcntl.h>
  59. #include <arpa/inet.h>
  60. #include <net/if_var.h>
  61. #include <pro/dhcp.h>
  62. #define BUFFERSIZE 128
  63. #define TCPPORT 23
  64. typedef struct {
  65. FILE *cd_rs232;
  66. FILE *cd_tcpip;
  67. char cd_connected;
  68. } CHANNEL;
  69. /*
  70. * Transfer data from input stream to output stream.
  71. */
  72. void StreamCopy(FILE * istream, FILE * ostream, char *cop)
  73. {
  74. int cnt;
  75. char *buff;
  76. buff = malloc(BUFFERSIZE);
  77. while (*cop) {
  78. if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0)
  79. break;
  80. if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0)
  81. break;
  82. if (*cop && fflush(ostream))
  83. break;
  84. }
  85. *cop = 0;
  86. free(buff);
  87. }
  88. /*
  89. * From RS232 to socket.
  90. */
  91. THREAD(Receiver, arg)
  92. {
  93. CHANNEL *cdp = arg;
  94. for (;;) {
  95. if (cdp->cd_connected) {
  96. NutThreadSetPriority(64);
  97. /*
  98. * We are reading from the UART without any timeout. So we
  99. * won't return immediately when disconnected.
  100. */
  101. StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected);
  102. NutThreadSetPriority(128);
  103. }
  104. NutThreadYield();
  105. }
  106. }
  107. /*
  108. * Main application routine.
  109. *
  110. * Nut/OS automatically calls this entry after initialization.
  111. */
  112. int main(void)
  113. {
  114. TCPSOCKET *sock;
  115. CHANNEL cd;
  116. uint32_t baud = 9600;
  117. /*
  118. * Register our devices.
  119. */
  120. NutRegisterDevice(&DEV_UART, 0, 0);
  121. #ifndef DEV_ETHER
  122. for (;;);
  123. #else
  124. NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
  125. /*
  126. * Setup the uart device.
  127. */
  128. cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b");
  129. _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
  130. /*
  131. * Setup the ethernet device. Try DHCP first. If this is
  132. * the first time boot with empty EEPROM and no DHCP server
  133. * was found, use hardcoded values.
  134. */
  135. if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
  136. /* No valid EEPROM contents, use hard coded MAC. */
  137. uint8_t my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 };
  138. if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
  139. /* No DHCP server found, use hard coded IP address. */
  140. uint32_t ip_addr = inet_addr("192.168.192.100");
  141. uint32_t ip_mask = inet_addr("255.255.255.0");
  142. NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
  143. /* If not in a local network, we must also call
  144. NutIpRouteAdd() to configure the routing. */
  145. }
  146. }
  147. /*
  148. * Start a RS232 receiver thread.
  149. */
  150. NutThreadCreate("xmit", Receiver, &cd, 512);
  151. /*
  152. * Now loop endless for connections.
  153. */
  154. cd.cd_connected = 0;
  155. for (;;) {
  156. /*
  157. * Create a socket and listen for a client.
  158. */
  159. sock = NutTcpCreateSocket();
  160. NutTcpAccept(sock, TCPPORT);
  161. /*
  162. * Open a stdio stream assigned to the connected socket.
  163. */
  164. cd.cd_tcpip = _fdopen((int) sock, "r+b");
  165. cd.cd_connected = 1;
  166. /*
  167. * Call RS232 transmit routine. On return we will be
  168. * disconnected again.
  169. */
  170. StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
  171. /*
  172. * Close the stream.
  173. */
  174. fclose(cd.cd_tcpip);
  175. /*
  176. * Close our socket.
  177. */
  178. NutTcpCloseSocket(sock);
  179. }
  180. #endif
  181. return 0;
  182. }