Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 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/board.h>
  53. #include <dev/hd44780_bus.h>
  54. #include <dev/term.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. #include <stdlib.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <io.h>
  64. #include <fcntl.h>
  65.  
  66. #include <arpa/inet.h>
  67. #include <net/if_var.h>
  68. #include <pro/dhcp.h>
  69.  
  70. #define BUFFERSIZE 128
  71. #define TCPPORT 23
  72.  
  73. typedef struct {
  74. FILE *cd_rs232;
  75. FILE *cd_tcpip;
  76. char cd_connected;
  77. } CHANNEL;
  78.  
  79. /*
  80. * Transfer data from input stream to output stream.
  81. */
  82. void StreamCopy(FILE * istream, FILE * ostream, char *cop)
  83. {
  84. int cnt;
  85. char *buff;
  86.  
  87. buff = malloc(BUFFERSIZE);
  88. while (*cop) {
  89. if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0)
  90. break;
  91. if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0)
  92. break;
  93. if (*cop && fflush(ostream))
  94. break;
  95. }
  96. *cop = 0;
  97. free(buff);
  98. }
  99.  
  100. /*
  101. * From RS232 to socket.
  102. */
  103. THREAD(Receiver, arg)
  104. {
  105. CHANNEL *cdp = arg;
  106.  
  107. for (;;) {
  108. if (cdp->cd_connected) {
  109. NutThreadSetPriority(64);
  110. /*
  111. * We are reading from the UART without any timeout. So we
  112. * won't return immediately when disconnected.
  113. */
  114. StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected);
  115. NutThreadSetPriority(128);
  116. }
  117. NutThreadYield();
  118. }
  119. }
  120.  
  121. /*
  122. * Main application routine.
  123. *
  124. * Nut/OS automatically calls this entry after initialization.
  125. */
  126. int main(void)
  127. {
  128. TCPSOCKET *sock;
  129. CHANNEL cd;
  130. uint32_t baud = 9600;
  131.  
  132. /*
  133. * Register our devices.
  134. */
  135. NutRegisterDevice(&DEV_UART, 0, 0);
  136. #ifndef DEV_ETHER
  137. for (;;);
  138. #else
  139. NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
  140.  
  141. /*
  142. * Setup the uart device.
  143. */
  144. cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b");
  145. _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
  146.  
  147. /*
  148. * Setup the ethernet device. Try DHCP first. If this is
  149. * the first time boot with empty EEPROM and no DHCP server
  150. * was found, use hardcoded values.
  151. */
  152. if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
  153. /* No valid EEPROM contents, use hard coded MAC. */
  154. uint8_t my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x02 };
  155.  
  156. if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
  157. /* No DHCP server found, use hard coded IP address. */
  158. uint32_t ip_addr = inet_addr("192.168.192.100");
  159. uint32_t ip_mask = inet_addr("255.255.255.0");
  160.  
  161. NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
  162. /* If not in a local network, we must also call
  163. NutIpRouteAdd() to configure the routing. */
  164. }
  165. }
  166. NutRegisterDevice(&devLcdBus, 0xFF04, 0);
  167. freopen("lcdbus", "w", stdout);
  168. printf(inet_ntoa(confnet.cdn_ip_addr));
  169. /*
  170. * Start a RS232 receiver thread.
  171. */
  172. NutThreadCreate("xmit", Receiver, &cd, 512);
  173.  
  174. /*
  175. * Now loop endless for connections.
  176. */
  177. cd.cd_connected = 0;
  178. for (;;) {
  179. /*
  180. * Create a socket and listen for a client.
  181. */
  182. sock = NutTcpCreateSocket();
  183. NutTcpAccept(sock, TCPPORT);
  184.  
  185. /*
  186. * Open a stdio stream assigned to the connected socket.
  187. */
  188. cd.cd_tcpip = _fdopen((int) sock, "r+b");
  189. cd.cd_connected = 1;
  190.  
  191. /*
  192. * Call RS232 transmit routine. On return we will be
  193. * disconnected again.
  194. */
  195. StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
  196.  
  197. /*
  198. * Close the stream.
  199. */
  200. fclose(cd.cd_tcpip);
  201.  
  202. /*
  203. * Close our socket.
  204. */
  205. NutTcpCloseSocket(sock);
  206. }
  207. #endif
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement