]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp
1 /* EthernetInterface.cpp */
2 /* Copyright (C) 2012 mbed.org, MIT License
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:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
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.
19 #include "EthernetInterface.h"
21 #include "lwip/inet.h"
22 #include "lwip/netif.h"
23 #include "netif/etharp.h"
24 #include "lwip/dhcp.h"
26 #include "lwip/tcpip.h"
30 /* TCP/IP and Network Interface Initialisation */
31 static struct netif netif
;
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;
39 static Semaphore
tcpip_inited(0);
40 static Semaphore
netif_linked(0);
41 static Semaphore
netif_up(0);
43 static void tcpip_init_done(void *arg
) {
44 tcpip_inited
.release();
47 static void netif_link_callback(struct netif
*netif
) {
48 if (netif_is_link_up(netif
)) {
49 netif_linked
.release();
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
));
62 static void init_netif(ip_addr_t
*ipaddr
, ip_addr_t
*netmask
, ip_addr_t
*gw
) {
63 tcpip_init(tcpip_init_done
, NULL
);
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
);
70 netif_set_link_callback (&netif
, netif_link_callback
);
71 netif_set_status_callback(&netif
, netif_status_callback
);
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
);
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]);
85 int EthernetInterface::init() {
88 init_netif(NULL
, NULL
, NULL
);
92 int EthernetInterface::init(const char* ip
, const char* mask
, const char* gateway
) {
98 ip_addr_t ip_n
, mask_n
, gateway_n
;
100 inet_aton(mask
, &mask_n
);
101 inet_aton(gateway
, &gateway_n
);
102 init_netif(&ip_n
, &mask_n
, &gateway_n
);
107 int EthernetInterface::connect(unsigned int timeout_ms
) {
108 eth_arch_enable_interrupts();
114 // Wait for an IP Address
115 // -1: error, 0: timeout
116 inited
= netif_up
.wait(timeout_ms
);
118 netif_set_up(&netif
);
120 // Wait for the link up
121 inited
= netif_linked
.wait(timeout_ms
);
124 return (inited
> 0) ? (0) : (-1);
127 int EthernetInterface::disconnect() {
129 dhcp_release(&netif
);
132 netif_set_down(&netif
);
135 eth_arch_disable_interrupts();
140 char* EthernetInterface::getMACAddress() {
144 char* EthernetInterface::getIPAddress() {
148 char* EthernetInterface::getGateway() {
152 char* EthernetInterface::getNetworkMask() {