EthernetInterface.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* EthernetInterface.cpp */
  2. /* Copyright (C) 2012 mbed.org, MIT License
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  5. * and associated documentation files (the "Software"), to deal in the Software without restriction,
  6. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  7. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in all copies or
  11. * substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  14. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. #include "EthernetInterface.h"
  20. #include "lwip/inet.h"
  21. #include "lwip/netif.h"
  22. #include "netif/etharp.h"
  23. #include "lwip/dhcp.h"
  24. #include "eth_arch.h"
  25. #include "lwip/tcpip.h"
  26. #include "mbed.h"
  27. /* TCP/IP and Network Interface Initialisation */
  28. static struct netif netif;
  29. static char mac_addr[19];
  30. static char ip_addr[17] = "\0";
  31. static char gateway[17] = "\0";
  32. static char networkmask[17] = "\0";
  33. static bool use_dhcp = false;
  34. static Semaphore tcpip_inited(0);
  35. static Semaphore netif_linked(0);
  36. static Semaphore netif_up(0);
  37. static void tcpip_init_done(void *arg) {
  38. tcpip_inited.release();
  39. }
  40. static void netif_link_callback(struct netif *netif) {
  41. if (netif_is_link_up(netif)) {
  42. netif_linked.release();
  43. }
  44. }
  45. static void netif_status_callback(struct netif *netif) {
  46. if (netif_is_up(netif)) {
  47. strcpy(ip_addr, inet_ntoa(netif->ip_addr));
  48. strcpy(gateway, inet_ntoa(netif->gw));
  49. strcpy(networkmask, inet_ntoa(netif->netmask));
  50. netif_up.release();
  51. }
  52. }
  53. static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
  54. tcpip_init(tcpip_init_done, NULL);
  55. tcpip_inited.wait();
  56. memset((void*) &netif, 0, sizeof(netif));
  57. netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);
  58. netif_set_default(&netif);
  59. netif_set_link_callback (&netif, netif_link_callback);
  60. netif_set_status_callback(&netif, netif_status_callback);
  61. }
  62. static void set_mac_address(void) {
  63. #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
  64. snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
  65. MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
  66. #else
  67. char mac[6];
  68. mbed_mac_address(mac);
  69. snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  70. #endif
  71. }
  72. int EthernetInterface::init() {
  73. use_dhcp = true;
  74. set_mac_address();
  75. init_netif(NULL, NULL, NULL);
  76. return 0;
  77. }
  78. int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
  79. use_dhcp = false;
  80. set_mac_address();
  81. strcpy(ip_addr, ip);
  82. ip_addr_t ip_n, mask_n, gateway_n;
  83. inet_aton(ip, &ip_n);
  84. inet_aton(mask, &mask_n);
  85. inet_aton(gateway, &gateway_n);
  86. init_netif(&ip_n, &mask_n, &gateway_n);
  87. return 0;
  88. }
  89. int EthernetInterface::connect(unsigned int timeout_ms) {
  90. eth_arch_enable_interrupts();
  91. int inited;
  92. if (use_dhcp) {
  93. dhcp_start(&netif);
  94. // Wait for an IP Address
  95. // -1: error, 0: timeout
  96. inited = netif_up.wait(timeout_ms);
  97. } else {
  98. netif_set_up(&netif);
  99. // Wait for the link up
  100. inited = netif_linked.wait(timeout_ms);
  101. }
  102. return (inited > 0) ? (0) : (-1);
  103. }
  104. int EthernetInterface::disconnect() {
  105. if (use_dhcp) {
  106. dhcp_release(&netif);
  107. dhcp_stop(&netif);
  108. } else {
  109. netif_set_down(&netif);
  110. }
  111. eth_arch_disable_interrupts();
  112. return 0;
  113. }
  114. char* EthernetInterface::getMACAddress() {
  115. return mac_addr;
  116. }
  117. char* EthernetInterface::getIPAddress() {
  118. return ip_addr;
  119. }
  120. char* EthernetInterface::getGateway() {
  121. return gateway;
  122. }
  123. char* EthernetInterface::getNetworkMask() {
  124. return networkmask;
  125. }