summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface')
-rw-r--r--tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp156
-rw-r--r--tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.h90
-rw-r--r--tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/eth_arch.h41
3 files changed, 287 insertions, 0 deletions
diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp
new file mode 100644
index 0000000000..73ce896bb8
--- /dev/null
+++ b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.cpp
@@ -0,0 +1,156 @@
+/* EthernetInterface.cpp */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "EthernetInterface.h"
+
+#include "lwip/inet.h"
+#include "lwip/netif.h"
+#include "netif/etharp.h"
+#include "lwip/dhcp.h"
+#include "eth_arch.h"
+#include "lwip/tcpip.h"
+
+#include "mbed.h"
+
+/* TCP/IP and Network Interface Initialisation */
+static struct netif netif;
+
+static char mac_addr[19];
+static char ip_addr[17] = "\0";
+static char gateway[17] = "\0";
+static char networkmask[17] = "\0";
+static bool use_dhcp = false;
+
+static Semaphore tcpip_inited(0);
+static Semaphore netif_linked(0);
+static Semaphore netif_up(0);
+
+static void tcpip_init_done(void *arg) {
+ tcpip_inited.release();
+}
+
+static void netif_link_callback(struct netif *netif) {
+ if (netif_is_link_up(netif)) {
+ netif_linked.release();
+ }
+}
+
+static void netif_status_callback(struct netif *netif) {
+ if (netif_is_up(netif)) {
+ strcpy(ip_addr, inet_ntoa(netif->ip_addr));
+ strcpy(gateway, inet_ntoa(netif->gw));
+ strcpy(networkmask, inet_ntoa(netif->netmask));
+ netif_up.release();
+ }
+}
+
+static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
+ tcpip_init(tcpip_init_done, NULL);
+ tcpip_inited.wait();
+
+ memset((void*) &netif, 0, sizeof(netif));
+ netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);
+ netif_set_default(&netif);
+
+ netif_set_link_callback (&netif, netif_link_callback);
+ netif_set_status_callback(&netif, netif_status_callback);
+}
+
+static void set_mac_address(void) {
+#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
+ snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
+ MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
+#else
+ char mac[6];
+ mbed_mac_address(mac);
+ snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+#endif
+}
+
+int EthernetInterface::init() {
+ use_dhcp = true;
+ set_mac_address();
+ init_netif(NULL, NULL, NULL);
+ return 0;
+}
+
+int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
+ use_dhcp = false;
+
+ set_mac_address();
+ strcpy(ip_addr, ip);
+
+ ip_addr_t ip_n, mask_n, gateway_n;
+ inet_aton(ip, &ip_n);
+ inet_aton(mask, &mask_n);
+ inet_aton(gateway, &gateway_n);
+ init_netif(&ip_n, &mask_n, &gateway_n);
+
+ return 0;
+}
+
+int EthernetInterface::connect(unsigned int timeout_ms) {
+ eth_arch_enable_interrupts();
+
+ int inited;
+ if (use_dhcp) {
+ dhcp_start(&netif);
+
+ // Wait for an IP Address
+ // -1: error, 0: timeout
+ inited = netif_up.wait(timeout_ms);
+ } else {
+ netif_set_up(&netif);
+
+ // Wait for the link up
+ inited = netif_linked.wait(timeout_ms);
+ }
+
+ return (inited > 0) ? (0) : (-1);
+}
+
+int EthernetInterface::disconnect() {
+ if (use_dhcp) {
+ dhcp_release(&netif);
+ dhcp_stop(&netif);
+ } else {
+ netif_set_down(&netif);
+ }
+
+ eth_arch_disable_interrupts();
+
+ return 0;
+}
+
+char* EthernetInterface::getMACAddress() {
+ return mac_addr;
+}
+
+char* EthernetInterface::getIPAddress() {
+ return ip_addr;
+}
+
+char* EthernetInterface::getGateway() {
+ return gateway;
+}
+
+char* EthernetInterface::getNetworkMask() {
+ return networkmask;
+}
+
+
diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.h b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.h
new file mode 100644
index 0000000000..a8ccbedaa9
--- /dev/null
+++ b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/EthernetInterface.h
@@ -0,0 +1,90 @@
+/* EthernetInterface.h */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef ETHERNETINTERFACE_H_
+#define ETHERNETINTERFACE_H_
+
+#if !defined(TARGET_LPC1768) && !defined(TARGET_LPC4088) && !defined(TARGET_LPC4088_DM) && !defined(TARGET_K64F) && !defined(TARGET_RZ_A1H) && !defined(TARGET_STM32F4)
+#error The Ethernet Interface library is not supported on this target
+#endif
+
+#include "rtos.h"
+#include "lwip/netif.h"
+
+ /** Interface using Ethernet to connect to an IP-based network
+ *
+ */
+class EthernetInterface {
+public:
+ /** Initialize the interface with DHCP.
+ * Initialize the interface and configure it to use DHCP (no connection at this point).
+ * \return 0 on success, a negative number on failure
+ */
+ static int init(); //With DHCP
+
+ /** Initialize the interface with a static IP address.
+ * Initialize the interface and configure it with the following static configuration (no connection at this point).
+ * \param ip the IP address to use
+ * \param mask the IP address mask
+ * \param gateway the gateway to use
+ * \return 0 on success, a negative number on failure
+ */
+ static int init(const char* ip, const char* mask, const char* gateway);
+
+ /** Connect
+ * Bring the interface up, start DHCP if needed.
+ * \param timeout_ms timeout in ms (default: (15)s).
+ * \return 0 on success, a negative number on failure
+ */
+ static int connect(unsigned int timeout_ms=15000);
+
+ /** Disconnect
+ * Bring the interface down
+ * \return 0 on success, a negative number on failure
+ */
+ static int disconnect();
+
+ /** Get the MAC address of your Ethernet interface
+ * \return a pointer to a string containing the MAC address
+ */
+ static char* getMACAddress();
+
+ /** Get the IP address of your Ethernet interface
+ * \return a pointer to a string containing the IP address
+ */
+ static char* getIPAddress();
+
+ /** Get the Gateway address of your Ethernet interface
+ * \return a pointer to a string containing the Gateway address
+ */
+ static char* getGateway();
+
+ /** Get the Network mask of your Ethernet interface
+ * \return a pointer to a string containing the Network mask
+ */
+ static char* getNetworkMask();
+};
+
+#include "TCPSocketConnection.h"
+#include "TCPSocketServer.h"
+
+#include "Endpoint.h"
+#include "UDPSocket.h"
+
+#endif /* ETHERNETINTERFACE_H_ */
diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/eth_arch.h b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/eth_arch.h
new file mode 100644
index 0000000000..1ff54b1d38
--- /dev/null
+++ b/tmk_core/tool/mbed/mbed-sdk/libraries/net/eth/EthernetInterface/eth_arch.h
@@ -0,0 +1,41 @@
+/* EthernetInterface.h */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+// Architecture specific Ethernet interface
+// Must be implemented by each target
+
+#ifndef ETHARCH_H_
+#define ETHARCH_H_
+
+#include "lwip/netif.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void eth_arch_enable_interrupts(void);
+void eth_arch_disable_interrupts(void);
+err_t eth_arch_enetif_init(struct netif *netif);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // #ifndef ETHARCHINTERFACE_H_
+