Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uIPManagement.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * uIP Management functions. This file contains the functions and globals needed to maintain the uIP
  29. * stack once an RNDIS device has been attached to the system.
  30. */
  31. #define INCLUDE_FROM_UIPMANAGEMENT_C
  32. #include "uIPManagement.h"
  33. /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
  34. static struct timer ConnectionTimer;
  35. /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
  36. static struct timer ARPTimer;
  37. /** MAC address of the RNDIS device, when enumerated. */
  38. struct uip_eth_addr MACAddress;
  39. /** Configures the uIP stack ready for network traffic processing. */
  40. void uIPManagement_Init(void)
  41. {
  42. /* uIP Timing Initialization */
  43. clock_init();
  44. timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
  45. timer_set(&ARPTimer, CLOCK_SECOND * 10);
  46. /* uIP Stack Initialization */
  47. uip_init();
  48. uip_arp_init();
  49. /* DHCP/Server IP Settings Initialization */
  50. if (USB_CurrentMode == USB_MODE_Device)
  51. {
  52. MACAddress.addr[0] = SERVER_MAC_ADDRESS[0];
  53. MACAddress.addr[1] = SERVER_MAC_ADDRESS[1];
  54. MACAddress.addr[2] = SERVER_MAC_ADDRESS[2];
  55. MACAddress.addr[3] = SERVER_MAC_ADDRESS[3];
  56. MACAddress.addr[4] = SERVER_MAC_ADDRESS[4];
  57. MACAddress.addr[5] = SERVER_MAC_ADDRESS[5];
  58. #if defined(ENABLE_DHCP_SERVER)
  59. DHCPServerApp_Init();
  60. #endif
  61. uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
  62. uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
  63. uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
  64. uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
  65. uip_sethostaddr(&IPAddress);
  66. uip_setnetmask(&Netmask);
  67. uip_setdraddr(&GatewayIPAddress);
  68. }
  69. else
  70. {
  71. #if defined(ENABLE_DHCP_CLIENT)
  72. DHCPClientApp_Init();
  73. #else
  74. uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
  75. uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
  76. uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
  77. uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
  78. uip_sethostaddr(&IPAddress);
  79. uip_setnetmask(&Netmask);
  80. uip_setdraddr(&GatewayIPAddress);
  81. #endif
  82. }
  83. /* Virtual Webserver Ethernet Address Configuration */
  84. uip_setethaddr(MACAddress);
  85. /* HTTP Webserver Initialization */
  86. HTTPServerApp_Init();
  87. /* TELNET Server Initialization */
  88. #if defined(ENABLE_TELNET_SERVER)
  89. TELNETServerApp_Init();
  90. #endif
  91. }
  92. /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
  93. * attached to the system.
  94. */
  95. void uIPManagement_ManageNetwork(void)
  96. {
  97. if (((USB_CurrentMode == USB_MODE_Host) && (USB_HostState == HOST_STATE_Configured)) ||
  98. ((USB_CurrentMode == USB_MODE_Device) && (USB_DeviceState == DEVICE_STATE_Configured)))
  99. {
  100. uIPManagement_ProcessIncomingPacket();
  101. uIPManagement_ManageConnections();
  102. }
  103. }
  104. /** uIP TCP/IP network stack callback function for the processing of a given TCP connection. This routine dispatches
  105. * to the appropriate TCP protocol application based on the connection's listen port number.
  106. */
  107. void uIPManagement_TCPCallback(void)
  108. {
  109. /* Call the correct TCP application based on the port number the connection is listening on */
  110. switch (uip_conn->lport)
  111. {
  112. case HTONS(HTTP_SERVER_PORT):
  113. HTTPServerApp_Callback();
  114. break;
  115. #if defined(ENABLE_TELNET_SERVER)
  116. case HTONS(TELNET_SERVER_PORT):
  117. TELNETServerApp_Callback();
  118. break;
  119. #endif
  120. }
  121. }
  122. /** uIP TCP/IP network stack callback function for the processing of a given UDP connection. This routine dispatches
  123. * to the appropriate UDP protocol application based on the connection's listen port number.
  124. */
  125. void uIPManagement_UDPCallback(void)
  126. {
  127. /* Call the correct UDP application based on the port number the connection is listening on */
  128. switch (uip_udp_conn->lport)
  129. {
  130. #if defined(ENABLE_DHCP_CLIENT)
  131. case HTONS(DHCP_CLIENT_PORT):
  132. DHCPClientApp_Callback();
  133. break;
  134. #endif
  135. #if defined(ENABLE_DHCP_SERVER)
  136. case HTONS(DHCP_SERVER_PORT):
  137. DHCPServerApp_Callback();
  138. break;
  139. #endif
  140. }
  141. }
  142. /** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
  143. static void uIPManagement_ProcessIncomingPacket(void)
  144. {
  145. /* Determine which USB mode the system is currently initialized in */
  146. if (USB_CurrentMode == USB_MODE_Device)
  147. {
  148. /* If no packet received, exit processing routine */
  149. if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
  150. return;
  151. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  152. /* Read the Incoming packet straight into the UIP packet buffer */
  153. RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
  154. }
  155. else
  156. {
  157. /* If no packet received, exit processing routine */
  158. if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
  159. return;
  160. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  161. /* Read the Incoming packet straight into the UIP packet buffer */
  162. RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
  163. }
  164. /* If the packet contains an Ethernet frame, process it */
  165. if (uip_len > 0)
  166. {
  167. switch (((struct uip_eth_hdr*)uip_buf)->type)
  168. {
  169. case HTONS(UIP_ETHTYPE_IP):
  170. /* Filter packet by MAC destination */
  171. uip_arp_ipin();
  172. /* Process Incoming packet */
  173. uip_input();
  174. /* If a response was generated, send it */
  175. if (uip_len > 0)
  176. {
  177. /* Add destination MAC to outgoing packet */
  178. uip_arp_out();
  179. uip_split_output();
  180. }
  181. break;
  182. case HTONS(UIP_ETHTYPE_ARP):
  183. /* Process ARP packet */
  184. uip_arp_arpin();
  185. /* If a response was generated, send it */
  186. if (uip_len > 0)
  187. uip_split_output();
  188. break;
  189. }
  190. }
  191. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  192. }
  193. /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
  194. static void uIPManagement_ManageConnections(void)
  195. {
  196. /* Poll TCP connections for more data to send back to the host */
  197. for (uint8_t i = 0; i < UIP_CONNS; i++)
  198. {
  199. uip_poll_conn(&uip_conns[i]);
  200. /* If a response was generated, send it */
  201. if (uip_len > 0)
  202. {
  203. /* Add destination MAC to outgoing packet */
  204. uip_arp_out();
  205. /* Split and send the outgoing packet */
  206. uip_split_output();
  207. }
  208. }
  209. /* Manage open connections for timeouts */
  210. if (timer_expired(&ConnectionTimer))
  211. {
  212. timer_reset(&ConnectionTimer);
  213. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  214. for (uint8_t i = 0; i < UIP_CONNS; i++)
  215. {
  216. /* Run periodic connection management for each TCP connection */
  217. uip_periodic(i);
  218. /* If a response was generated, send it */
  219. if (uip_len > 0)
  220. {
  221. /* Add destination MAC to outgoing packet */
  222. uip_arp_out();
  223. /* Split and send the outgoing packet */
  224. uip_split_output();
  225. }
  226. }
  227. #if defined(ENABLE_DHCP_CLIENT)
  228. for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
  229. {
  230. /* Run periodic connection management for each UDP connection */
  231. uip_udp_periodic(i);
  232. /* If a response was generated, send it */
  233. if (uip_len > 0)
  234. {
  235. /* Add destination MAC to outgoing packet */
  236. uip_arp_out();
  237. /* Split and send the outgoing packet */
  238. uip_split_output();
  239. }
  240. }
  241. #endif
  242. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  243. }
  244. /* Manage ARP cache refreshing */
  245. if (timer_expired(&ARPTimer))
  246. {
  247. timer_reset(&ARPTimer);
  248. uip_arp_timer();
  249. }
  250. }