mirror of https://github.com/ARMmbed/mbed-os.git
Preparing new layout - moving lwip (2)
parent
fa71088cdc
commit
1fd427fc59
|
@ -0,0 +1,206 @@
|
|||
/* LWIP implementation of NetworkInterfaceAPI
|
||||
* Copyright (c) 2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "LWIPInterface.h"
|
||||
|
||||
#include "mbed.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "lwip/tcpip.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "netif/etharp.h"
|
||||
#include "eth_arch.h"
|
||||
|
||||
|
||||
#define LWIP_TIMEOUT 15000
|
||||
|
||||
|
||||
/* TCP/IP and Network Interface Initialisation */
|
||||
static struct netif netif;
|
||||
|
||||
static char ip_addr[NS_IP_SIZE] = "\0";
|
||||
static char mac_addr[NS_MAC_SIZE] = "\0";
|
||||
|
||||
static Semaphore tcpip_inited(0);
|
||||
static Semaphore netif_linked(0);
|
||||
static Semaphore netif_up(0);
|
||||
|
||||
static void tcpip_init_done(void *)
|
||||
{
|
||||
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));
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
// LWIPInterface implementation
|
||||
int32_t LWIPInterface::connect()
|
||||
{
|
||||
// Set up network
|
||||
set_mac_address();
|
||||
init_netif(0, 0, 0);
|
||||
|
||||
// Connect to network
|
||||
eth_arch_enable_interrupts();
|
||||
|
||||
dhcp_start(&netif);
|
||||
|
||||
// Wait for an IP Address
|
||||
// -1: error, 0: timeout
|
||||
if (netif_up.wait(LWIP_TIMEOUT) < 0) {
|
||||
return NS_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t LWIPInterface::disconnect()
|
||||
{
|
||||
dhcp_release(&netif);
|
||||
dhcp_stop(&netif);
|
||||
|
||||
eth_arch_disable_interrupts();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *LWIPInterface::getIPAddress()
|
||||
{
|
||||
return ip_addr;
|
||||
}
|
||||
|
||||
const char *LWIPInterface::getMACAddress()
|
||||
{
|
||||
return mac_addr;
|
||||
}
|
||||
|
||||
SocketInterface *LWIPInterface::createSocket(ns_protocol_t proto)
|
||||
{
|
||||
int type = (proto == NS_UDP) ? SOCK_DGRAM : SOCK_STREAM;
|
||||
int fd = lwip_socket(AF_INET, type, 0);
|
||||
if (fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return new LWIPSocket(fd);
|
||||
}
|
||||
|
||||
void LWIPInterface::destroySocket(SocketInterface *siface)
|
||||
{
|
||||
LWIPSocket *socket = (LWIPSocket *)siface;
|
||||
lwip_close(socket->fd);
|
||||
|
||||
delete socket;
|
||||
}
|
||||
|
||||
|
||||
// TCP SocketInterface implementation
|
||||
int32_t LWIPInterface::LWIPSocket::open(const char *ip, uint16_t port)
|
||||
{
|
||||
struct sockaddr_in host;
|
||||
memset(&host, 0, sizeof host);
|
||||
inet_aton(ip, &host.sin_addr);
|
||||
host.sin_family = AF_INET;
|
||||
host.sin_port = htons(port);
|
||||
|
||||
if (lwip_connect(fd, (const struct sockaddr *)&host, sizeof host) < 0) {
|
||||
return NS_ERROR_NO_CONNECTION;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t LWIPInterface::LWIPSocket::close()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t LWIPInterface::LWIPSocket::send(const void *voiddata, uint32_t size)
|
||||
{
|
||||
uint8_t *data = (uint8_t *)voiddata;
|
||||
uint32_t writtenLen = 0;
|
||||
|
||||
while (writtenLen < size) {
|
||||
int ret = lwip_send(fd, data + writtenLen, size - writtenLen, 0);
|
||||
|
||||
if (ret > 0) {
|
||||
writtenLen += ret;
|
||||
} else if (ret == 0) {
|
||||
return NS_ERROR_NO_CONNECTION;
|
||||
} else {
|
||||
return NS_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return writtenLen;
|
||||
}
|
||||
|
||||
int32_t LWIPInterface::LWIPSocket::recv(void *data, uint32_t size)
|
||||
{
|
||||
int ret = lwip_recv(fd, data, size, MSG_DONTWAIT);
|
||||
|
||||
if (ret > 0) {
|
||||
return ret;
|
||||
} else if (ret == 0) {
|
||||
return NS_ERROR_NO_CONNECTION;
|
||||
} else if (ret == -1) {
|
||||
return NS_ERROR_WOULD_BLOCK;
|
||||
} else {
|
||||
return NS_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/* LWIP implementation of NetworkInterfaceAPI
|
||||
* Copyright (c) 2015 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LWIP_INTERFACE_H
|
||||
#define LWIP_INTERFACE_H
|
||||
|
||||
#include "EthernetInterface.h"
|
||||
#include "rtos.h"
|
||||
#include "lwip/netif.h"
|
||||
|
||||
|
||||
/** LWIPInterface class
|
||||
* Implementation of the NetworkInterface for LWIP
|
||||
*/
|
||||
class LWIPInterface : public EthernetInterface
|
||||
{
|
||||
public:
|
||||
|
||||
// Implementation of EthernetInterface
|
||||
virtual int32_t connect();
|
||||
virtual int32_t disconnect();
|
||||
|
||||
// Implementation of NetworkInterface
|
||||
virtual const char *getIPAddress();
|
||||
virtual const char *getMACAddress();
|
||||
|
||||
virtual SocketInterface *createSocket(ns_protocol_t proto);
|
||||
virtual void destroySocket(SocketInterface *socket);
|
||||
|
||||
private:
|
||||
|
||||
// Implementation of the TCP SocketInterface for LWIP
|
||||
class LWIPSocket : public SocketInterface
|
||||
{
|
||||
public:
|
||||
|
||||
LWIPSocket(int fd) : fd(fd) {}
|
||||
int fd;
|
||||
|
||||
// Implementation of SocketInterface
|
||||
virtual int32_t open(const char *ip, uint16_t port);
|
||||
virtual int32_t close();
|
||||
|
||||
virtual int32_t send(const void *data, uint32_t size);
|
||||
virtual int32_t recv(void *data, uint32_t size);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -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_
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
/* 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 LWIPOPTS_H
|
||||
#define LWIPOPTS_H
|
||||
|
||||
#include "lwipopts_conf.h"
|
||||
|
||||
// Workaround for Linux timeval
|
||||
#if defined (TOOLCHAIN_GCC)
|
||||
#define LWIP_TIMEVAL_PRIVATE 0
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
// Operating System
|
||||
#define NO_SYS 0
|
||||
|
||||
#if NO_SYS == 0
|
||||
#include "cmsis_os.h"
|
||||
|
||||
#define SYS_LIGHTWEIGHT_PROT 1
|
||||
|
||||
#define LWIP_RAW 0
|
||||
|
||||
#define TCPIP_MBOX_SIZE 8
|
||||
#define DEFAULT_TCP_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_UDP_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_RAW_RECVMBOX_SIZE 8
|
||||
#define DEFAULT_ACCEPTMBOX_SIZE 8
|
||||
|
||||
#define TCPIP_THREAD_STACKSIZE 1024
|
||||
#define TCPIP_THREAD_PRIO (osPriorityNormal)
|
||||
|
||||
#define DEFAULT_THREAD_STACKSIZE 512
|
||||
|
||||
#define MEMP_NUM_SYS_TIMEOUT 16
|
||||
#endif
|
||||
|
||||
// 32-bit alignment
|
||||
#define MEM_ALIGNMENT 4
|
||||
|
||||
#define PBUF_POOL_SIZE 5
|
||||
#define MEMP_NUM_TCP_PCB_LISTEN 4
|
||||
#define MEMP_NUM_TCP_PCB 4
|
||||
#define MEMP_NUM_UDP_PCB 4
|
||||
#define MEMP_NUM_PBUF 8
|
||||
#define MEMP_NUM_NETBUF 8
|
||||
|
||||
#define TCP_QUEUE_OOSEQ 0
|
||||
#define TCP_OVERSIZE 0
|
||||
|
||||
#define LWIP_DHCP 1
|
||||
#define LWIP_DNS 1
|
||||
|
||||
// Support Multicast
|
||||
#include "stdlib.h"
|
||||
#define LWIP_IGMP 1
|
||||
#define LWIP_RAND() rand()
|
||||
|
||||
#define LWIP_COMPAT_SOCKETS 0
|
||||
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
|
||||
#define LWIP_SO_RCVTIMEO 1
|
||||
#define LWIP_TCP_KEEPALIVE 1
|
||||
|
||||
// Debug Options
|
||||
// #define LWIP_DEBUG
|
||||
#define UDP_LPC_EMAC LWIP_DBG_OFF
|
||||
#define SYS_DEBUG LWIP_DBG_OFF
|
||||
#define PPP_DEBUG LWIP_DBG_OFF
|
||||
#define IP_DEBUG LWIP_DBG_OFF
|
||||
#define MEM_DEBUG LWIP_DBG_OFF
|
||||
#define MEMP_DEBUG LWIP_DBG_OFF
|
||||
#define PBUF_DEBUG LWIP_DBG_OFF
|
||||
#define API_LIB_DEBUG LWIP_DBG_OFF
|
||||
#define API_MSG_DEBUG LWIP_DBG_OFF
|
||||
#define TCPIP_DEBUG LWIP_DBG_OFF
|
||||
#define SOCKETS_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_FR_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_RTO_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_CWND_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_WND_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_RST_DEBUG LWIP_DBG_OFF
|
||||
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
|
||||
#define ETHARP_DEBUG LWIP_DBG_OFF
|
||||
#define NETIF_DEBUG LWIP_DBG_OFF
|
||||
#define DHCP_DEBUG LWIP_DBG_OFF
|
||||
|
||||
#ifdef LWIP_DEBUG
|
||||
#define MEMP_OVERFLOW_CHECK 1
|
||||
#define MEMP_SANITY_CHECK 1
|
||||
#else
|
||||
#define LWIP_NOASSERT 1
|
||||
#define LWIP_STATS 0
|
||||
#endif
|
||||
|
||||
#define LWIP_PLATFORM_BYTESWAP 1
|
||||
|
||||
#if LWIP_TRANSPORT_ETHERNET
|
||||
|
||||
/* MSS should match the hardware packet size */
|
||||
#define TCP_MSS 1460
|
||||
#define TCP_SND_BUF (2 * TCP_MSS)
|
||||
#define TCP_WND (2 * TCP_MSS)
|
||||
#define TCP_SND_QUEUELEN (2 * TCP_SND_BUF/TCP_MSS)
|
||||
|
||||
// Broadcast
|
||||
#define IP_SOF_BROADCAST 1
|
||||
#define IP_SOF_BROADCAST_RECV 1
|
||||
|
||||
#define LWIP_BROADCAST_PING 1
|
||||
|
||||
#define LWIP_CHECKSUM_ON_COPY 1
|
||||
|
||||
#define LWIP_NETIF_HOSTNAME 1
|
||||
#define LWIP_NETIF_STATUS_CALLBACK 1
|
||||
#define LWIP_NETIF_LINK_CALLBACK 1
|
||||
|
||||
#elif LWIP_TRANSPORT_PPP
|
||||
|
||||
#define TCP_SND_BUF (3 * 536)
|
||||
#define TCP_WND (2 * 536)
|
||||
|
||||
#define LWIP_ARP 0
|
||||
|
||||
#define PPP_SUPPORT 1
|
||||
#define CHAP_SUPPORT 1
|
||||
#define PAP_SUPPORT 1
|
||||
#define PPP_THREAD_STACKSIZE 4*192
|
||||
#define PPP_THREAD_PRIO 0
|
||||
|
||||
#define MAXNAMELEN 64 /* max length of hostname or name for auth */
|
||||
#define MAXSECRETLEN 64
|
||||
|
||||
#else
|
||||
#error A transport mechanism (Ethernet or PPP) must be defined
|
||||
#endif
|
||||
|
||||
#endif /* LWIPOPTS_H_ */
|
Loading…
Reference in New Issue