]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / net / eth / EthernetInterface / EthernetInterface.cpp
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
21 #include "lwip/inet.h"
22 #include "lwip/netif.h"
23 #include "netif/etharp.h"
24 #include "lwip/dhcp.h"
25 #include "eth_arch.h"
26 #include "lwip/tcpip.h"
27
28 #include "mbed.h"
29
30 /* TCP/IP and Network Interface Initialisation */
31 static struct netif netif;
32
33 static char mac_addr[19];
34 static char ip_addr[17] = "\0";
35 static char gateway[17] = "\0";
36 static char networkmask[17] = "\0";
37 static bool use_dhcp = false;
38
39 static Semaphore tcpip_inited(0);
40 static Semaphore netif_linked(0);
41 static Semaphore netif_up(0);
42
43 static void tcpip_init_done(void *arg) {
44     tcpip_inited.release();
45 }
46
47 static void netif_link_callback(struct netif *netif) {
48     if (netif_is_link_up(netif)) {
49         netif_linked.release();
50     }
51 }
52
53 static void netif_status_callback(struct netif *netif) {
54     if (netif_is_up(netif)) {
55         strcpy(ip_addr, inet_ntoa(netif->ip_addr));
56         strcpy(gateway, inet_ntoa(netif->gw));
57         strcpy(networkmask, inet_ntoa(netif->netmask));
58         netif_up.release();
59     }
60 }
61
62 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
63     tcpip_init(tcpip_init_done, NULL);
64     tcpip_inited.wait();
65     
66     memset((void*) &netif, 0, sizeof(netif));
67     netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);
68     netif_set_default(&netif);
69     
70     netif_set_link_callback  (&netif, netif_link_callback);
71     netif_set_status_callback(&netif, netif_status_callback);
72 }
73
74 static void set_mac_address(void) {
75 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
76     snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
77              MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
78 #else
79     char mac[6];
80     mbed_mac_address(mac);
81     snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
82 #endif
83 }
84
85 int EthernetInterface::init() {
86     use_dhcp = true;
87     set_mac_address();
88     init_netif(NULL, NULL, NULL);
89     return 0;
90 }
91
92 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
93     use_dhcp = false;
94     
95     set_mac_address();
96     strcpy(ip_addr, ip);
97     
98     ip_addr_t ip_n, mask_n, gateway_n;
99     inet_aton(ip, &ip_n);
100     inet_aton(mask, &mask_n);
101     inet_aton(gateway, &gateway_n);
102     init_netif(&ip_n, &mask_n, &gateway_n);
103     
104     return 0;
105 }
106
107 int EthernetInterface::connect(unsigned int timeout_ms) {
108     eth_arch_enable_interrupts();
109
110     int inited;
111     if (use_dhcp) {
112         dhcp_start(&netif);
113         
114         // Wait for an IP Address
115         // -1: error, 0: timeout
116         inited = netif_up.wait(timeout_ms);
117     } else {
118         netif_set_up(&netif);
119         
120         // Wait for the link up
121         inited = netif_linked.wait(timeout_ms);
122     }
123     
124     return (inited > 0) ? (0) : (-1);
125 }
126
127 int EthernetInterface::disconnect() {
128     if (use_dhcp) {
129         dhcp_release(&netif);
130         dhcp_stop(&netif);
131     } else {
132         netif_set_down(&netif);
133     }
134     
135     eth_arch_disable_interrupts();
136     
137     return 0;
138 }
139
140 char* EthernetInterface::getMACAddress() {
141     return mac_addr;
142 }
143
144 char* EthernetInterface::getIPAddress() {
145     return ip_addr;
146 }
147
148 char* EthernetInterface::getGateway() {
149     return gateway;
150 }
151
152 char* EthernetInterface::getNetworkMask() {
153     return networkmask;
154 }
155
156