Merge pull request #2664 from geky/nsapi-static-config

nsapi - Add optional API for static configuration of network interfaces
pull/2808/head
Sam Grove 2016-09-24 16:27:55 -05:00 committed by GitHub
commit 584384c38f
12 changed files with 475 additions and 86 deletions

View File

@ -19,9 +19,32 @@
/* Interface implementation */
EthernetInterface::EthernetInterface()
: _dhcp(false), _ip_address(), _netmask(), _gateway()
{
}
int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
return 0;
}
int EthernetInterface::set_dhcp(bool dhcp)
{
_dhcp = dhcp;
return 0;
}
int EthernetInterface::connect()
{
return lwip_bringup();
return lwip_bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0);
}
int EthernetInterface::disconnect()
@ -29,14 +52,24 @@ int EthernetInterface::disconnect()
return lwip_bringdown();
}
const char *EthernetInterface::get_mac_address()
{
return lwip_get_mac_address();
}
const char *EthernetInterface::get_ip_address()
{
return lwip_get_ip_address();
}
const char *EthernetInterface::get_mac_address()
const char *EthernetInterface::get_netmask()
{
return lwip_get_mac_address();
return lwip_get_netmask();
}
const char *EthernetInterface::get_gateway()
{
return lwip_get_gateway();
}
NetworkStack *EthernetInterface::get_stack()

View File

@ -31,6 +31,32 @@ class NetworkStack;
class EthernetInterface : public EthInterface
{
public:
/** EthernetInterface lifetime
*/
EthernetInterface();
/** Set a static IP address
*
* Configures this network interface to use a static IP address.
* Implicitly disables DHCP, which can be enabled in set_dhcp.
* Requires that the network is disconnected.
*
* @param address Null-terminated representation of the local IP address
* @param netmask Null-terminated representation of the local network mask
* @param gateway Null-terminated representation of the local gateway
* @return 0 on success, negative error code on failure
*/
virtual int set_network(const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network
*
* Requires that the network is disconnected
*
* @param dhcp False to disable dhcp (defaults to enabled)
* @return 0 on success, negative error code on failure
*/
virtual int set_dhcp(bool dhcp);
/** Start the interface
* @return 0 on success, negative on failure
*/
@ -41,15 +67,37 @@ public:
*/
virtual int disconnect();
/** Get the internally stored IP address
* @return IP address of the interface or null if not yet connected
/** Get the local MAC address
*
* Provided MAC address is intended for info or debug purposes and
* may not be provided if the underlying network interface does not
* provide a MAC address
*
* @return Null-terminated representation of the local MAC address
* or null if no MAC address is available
*/
virtual const char *get_mac_address();
/** Get the local IP address
*
* @return Null-terminated representation of the local IP address
* or null if no IP address has been recieved
*/
virtual const char *get_ip_address();
/** Get the internally stored MAC address
* @return MAC address of the interface
/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual const char *get_mac_address();
virtual const char *get_netmask();
/** Get the local gateway
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual const char *get_gateway();
protected:
/** Provide access to the underlying stack
@ -57,6 +105,11 @@ protected:
* @return The underlying network stack
*/
virtual NetworkStack *get_stack();
bool _dhcp;
char _ip_address[NSAPI_IPv4_SIZE];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
};

View File

@ -89,9 +89,11 @@ static void lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t
/* TCP/IP and Network Interface Initialisation */
static struct netif lwip_netif;
static char lwip_ip_addr[NSAPI_IP_SIZE] = "\0";
static char lwip_mac_addr[NSAPI_MAC_SIZE] = "\0";
static bool lwip_dhcp = false;
static char lwip_mac_address[NSAPI_MAC_SIZE] = "\0";
static char lwip_ip_address[NSAPI_IPv4_SIZE] = "\0";
static char lwip_netmask[NSAPI_IPv4_SIZE] = "\0";
static char lwip_gateway[NSAPI_IPv4_SIZE] = "\0";
static sys_sem_t lwip_tcpip_inited;
static void lwip_tcpip_init_irq(void *eh)
@ -111,7 +113,9 @@ static sys_sem_t lwip_netif_up;
static void lwip_netif_status_irq(struct netif *lwip_netif)
{
if (netif_is_up(lwip_netif)) {
strcpy(lwip_ip_addr, inet_ntoa(lwip_netif->ip_addr));
strcpy(lwip_ip_address, inet_ntoa(lwip_netif->ip_addr));
strcpy(lwip_netmask, inet_ntoa(lwip_netif->netmask));
strcpy(lwip_gateway, inet_ntoa(lwip_netif->gw));
sys_sem_signal(&lwip_netif_up);
}
}
@ -119,13 +123,13 @@ static void lwip_netif_status_irq(struct netif *lwip_netif)
static void lwip_set_mac_address(void)
{
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
snprintf(lwip_mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
snprintf(lwip_mac_address, 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(lwip_mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
#endif
}
@ -134,15 +138,26 @@ static void lwip_set_mac_address(void)
/* LWIP interface implementation */
const char *lwip_get_mac_address(void)
{
return lwip_mac_addr[0] ? lwip_mac_addr : 0;
return lwip_mac_address[0] ? lwip_mac_address : 0;
}
const char *lwip_get_ip_address(void)
{
return lwip_ip_addr[0] ? lwip_ip_addr : 0;
return lwip_ip_address[0] ? lwip_ip_address : 0;
}
int lwip_bringup(void)
const char *lwip_get_netmask(void)
{
return lwip_netmask[0] ? lwip_netmask : 0;
}
const char *lwip_get_gateway(void)
{
return lwip_gateway[0] ? lwip_gateway : 0;
}
int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
{
// Check if we've already connected
if (lwip_get_ip_address()) {
@ -162,6 +177,7 @@ int lwip_bringup(void)
sys_arch_sem_wait(&lwip_tcpip_inited, 0);
memset(&lwip_netif, 0, sizeof lwip_netif);
netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input);
netif_set_default(&lwip_netif);
@ -175,7 +191,26 @@ int lwip_bringup(void)
lwip_arena_init();
// Connect to the network
dhcp_start(&lwip_netif);
lwip_dhcp = dhcp;
if (lwip_dhcp) {
err_t err = dhcp_start(&lwip_netif);
if (err) {
return NSAPI_ERROR_DHCP_FAILURE;
}
} else {
ip_addr_t ip_addr;
ip_addr_t netmask_addr;
ip_addr_t gw_addr;
if (!inet_aton(ip, &ip_addr) ||
!inet_aton(netmask, &netmask_addr) ||
!inet_aton(gw, &gw_addr)) {
return NSAPI_ERROR_PARAMETER;
}
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
netif_set_up(&lwip_netif);
}
// Wait for an IP Address
u32_t ret = sys_arch_sem_wait(&lwip_netif_up, 15000);
@ -194,9 +229,17 @@ int lwip_bringdown(void)
}
// Disconnect from the network
dhcp_release(&lwip_netif);
dhcp_stop(&lwip_netif);
lwip_ip_addr[0] = '\0';
if (lwip_dhcp) {
dhcp_release(&lwip_netif);
dhcp_stop(&lwip_netif);
lwip_dhcp = false;
lwip_ip_address[0] = '\0';
lwip_netmask[0] = '\0';
lwip_gateway[0] = '\0';
} else {
netif_set_down(&lwip_netif);
}
return 0;
}
@ -230,18 +273,6 @@ static int lwip_err_remap(err_t err) {
/* LWIP network stack implementation */
static nsapi_addr_t lwip_getaddr(nsapi_stack_t *stack)
{
if (!lwip_get_ip_address()) {
return (nsapi_addr_t){0};
}
nsapi_addr_t addr;
addr.version = NSAPI_IPv4;
inet_aton(lwip_get_ip_address(), (ip_addr_t *)addr.bytes);
return addr;
}
static int lwip_gethostbyname(nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host)
{
err_t err = netconn_gethostbyname(host, (ip_addr_t *)addr->bytes);
@ -487,7 +518,6 @@ static void lwip_socket_attach(nsapi_stack_t *stack, nsapi_socket_t handle, void
/* LWIP network stack */
const nsapi_stack_api_t lwip_stack_api = {
.get_ip_address = lwip_getaddr,
.gethostbyname = lwip_gethostbyname,
.socket_open = lwip_socket_open,
.socket_close = lwip_socket_close,

View File

@ -25,13 +25,15 @@ extern "C" {
// Access to lwip through the nsapi
int lwip_bringup(void);
int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw);
int lwip_bringdown(void);
extern nsapi_stack_t lwip_stack;
const char *lwip_get_mac_address(void);
const char *lwip_get_ip_address(void);
const char *lwip_get_netmask(void);
const char *lwip_get_gateway(void);
extern nsapi_stack_t lwip_stack;
#ifdef __cplusplus

View File

@ -0,0 +1,77 @@
/* Socket
* 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 "network-socket/CellularInterface.h"
#include <stdlib.h>
#include <string.h>
CellularInterface::CellularInterface()
: _apn(0), _user(0), _pass(0)
{
}
CellularInterface::~CellularInterface()
{
free(_apn);
free(_user);
free(_pass);
}
int CellularInterface::set_credentials(const char *apn, const char *user, const char *pass)
{
free(_apn);
_apn = 0;
free(_user);
_user = 0;
free(_pass);
_pass = 0;
if (apn) {
_apn = (char*)malloc(strlen(apn)+1);
if (!_apn) {
return NSAPI_ERROR_NO_MEMORY;
}
strcpy(_apn, apn);
}
if (user) {
_user = (char*)malloc(strlen(user)+1);
if (!_user) {
return NSAPI_ERROR_NO_MEMORY;
}
strcpy(_user, user);
}
if (pass) {
_pass = (char*)malloc(strlen(pass)+1);
if (!_pass) {
return NSAPI_ERROR_NO_MEMORY;
}
strcpy(_pass, pass);
}
return 0;
}
int CellularInterface::connect()
{
return connect(_apn, _user, _pass);
}

View File

@ -27,6 +27,19 @@
class CellularInterface : public NetworkInterface
{
public:
/** CellularInterface lifetime
*/
CellularInterface();
virtual ~CellularInterface();
/** Set the cellular network APN and credentials
*
* @param apn Optional name of the network to connect to
* @param user Optional username for the APN
* @param pass Optional password fot the APN
*/
virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0);
/** Start the interface
*
* @param apn Optional name of the network to connect to
@ -34,7 +47,15 @@ public:
* @param password Optional password for your APN
* @return 0 on success, negative error code on failure
*/
virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0) = 0;
virtual int connect(const char *apn, const char *username = 0, const char *password = 0) = 0;
/** Start the interface
*
* Attempts to connect to a cellular network based on supplied credentials
*
* @return 0 on success, negative error code on failure
*/
virtual int connect();
/** Stop the interface
*
@ -42,11 +63,10 @@ public:
*/
virtual int disconnect() = 0;
/** Get the local MAC address
*
* @return Null-terminated representation of the local MAC address
*/
virtual const char *get_mac_address() = 0;
private:
char *_apn;
char *_user;
char *_pass;
};

View File

@ -26,24 +26,6 @@
*/
class EthInterface : public NetworkInterface
{
public:
/** Start the interface
*
* @return 0 on success, negative error code on failure
*/
virtual int connect() = 0;
/** Stop the interface
*
* @return 0 on success, negative error code on failure
*/
virtual int disconnect() = 0;
/** Get the local MAC address
*
* @return Null-terminated representation of the local MAC address
*/
virtual const char *get_mac_address() = 0;
};

View File

@ -26,24 +26,6 @@
*/
class MeshInterface : public NetworkInterface
{
public:
/** Start the interface
*
* @return 0 on success, negative on failure
*/
virtual int connect() = 0;
/** Stop the interface
*
* @return 0 on success, negative on failure
*/
virtual int disconnect() = 0;
/** Get the local MAC address
*
* @return Null-terminated representation of the local MAC address
*/
virtual const char *get_mac_address() = 0;
};

View File

@ -0,0 +1,54 @@
/* Socket
* 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 "network-socket/NetworkInterface.h"
#include <string.h>
const char *NetworkInterface::get_mac_address()
{
return 0;
}
const char *NetworkInterface::get_ip_address()
{
return 0;
}
const char *NetworkInterface::get_netmask()
{
return 0;
}
const char *NetworkInterface::get_gateway()
{
return 0;
}
int NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
return NSAPI_ERROR_UNSUPPORTED;
}
int NetworkInterface::set_dhcp(bool dhcp)
{
if (!dhcp) {
return NSAPI_ERROR_UNSUPPORTED;
} else {
return 0;
}
}

View File

@ -17,6 +17,8 @@
#ifndef NETWORK_INTERFACE_H
#define NETWORK_INTERFACE_H
#include "network-socket/nsapi_types.h"
// Predeclared class
class NetworkStack;
@ -29,12 +31,73 @@ class NetworkInterface {
public:
virtual ~NetworkInterface() {};
/** Get the local MAC address
*
* Provided MAC address is intended for info or debug purposes and
* may not be provided if the underlying network interface does not
* provide a MAC address
*
* @return Null-terminated representation of the local MAC address
* or null if no MAC address is available
*/
virtual const char *get_mac_address();
/** Get the local IP address
*
* @return Null-terminated representation of the local IP address
* or null if not yet connected
* or null if no IP address has been recieved
*/
virtual const char *get_ip_address() = 0;
virtual const char *get_ip_address();
/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual const char *get_netmask();
/** Get the local gateway
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual const char *get_gateway();
/** Set a static IP address
*
* Configures this network interface to use a static IP address.
* Implicitly disables DHCP, which can be enabled in set_dhcp.
* Requires that the network is disconnected.
*
* @param address Null-terminated representation of the local IP address
* @param netmask Null-terminated representation of the local network mask
* @param gateway Null-terminated representation of the local gateway
* @return 0 on success, negative error code on failure
*/
virtual int set_network(const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network
*
* Enables DHCP on connecting the network. Defaults to enabled unless
* a static IP address has been assigned. Requires that the network is
* disconnected.
*
* @param dhcp True to enable DHCP
* @return 0 on success, negative error code on failure
*/
virtual int set_dhcp(bool dhcp);
/** Start the interface
*
* @return 0 on success, negative error code on failure
*/
virtual int connect() = 0;
/** Stop the interface
*
* @return 0 on success, negative error code on failure
*/
virtual int disconnect() = 0;
protected:
friend class Socket;

View File

@ -0,0 +1,71 @@
/* Socket
* 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 "network-socket/WiFiInterface.h"
#include <stdlib.h>
#include <string.h>
WiFiInterface::WiFiInterface()
: _ssid(0), _pass(0), _security(NSAPI_SECURITY_NONE)
{
}
WiFiInterface::~WiFiInterface()
{
free(_ssid);
free(_pass);
}
int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
free(_ssid);
_ssid = 0;
free(_pass);
_pass = 0;
if (ssid) {
_ssid = (char*)malloc(strlen(ssid)+1);
if (!_ssid) {
return NSAPI_ERROR_NO_MEMORY;
}
strcpy(_ssid, ssid);
}
if (pass) {
_pass = (char*)malloc(strlen(pass)+1);
if (!_pass) {
return NSAPI_ERROR_NO_MEMORY;
}
strcpy(_pass, pass);
}
_security = security;
return 0;
}
int WiFiInterface::connect()
{
if (!_ssid || !_pass) {
return NSAPI_ERROR_PARAMETER;
}
return connect(_ssid, _pass, _security);
}

View File

@ -41,6 +41,20 @@ enum nsapi_security_t {
class WiFiInterface: public NetworkInterface
{
public:
/** WiFiInterface lifetime
*/
WiFiInterface();
virtual ~WiFiInterface();
/** Set the WiFi network credentials
*
* @param ssid Name of the network to connect to
* @param pass Security passphrase to connect to the network
* @param security Type of encryption for connection
* (defaults to NSAPI_SECURITY_NONE)
*/
virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
/** Start the interface
*
* Attempts to connect to a WiFi network. If passphrase is invalid,
@ -53,17 +67,25 @@ public:
*/
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0;
/** Start the interface
*
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
*
* @return 0 on success, negative error code on failure
*/
virtual int connect();
/** Stop the interface
*
* @return 0 on success, negative error code on failure
*/
virtual int disconnect() = 0;
/** Get the local MAC address
*
* @return Null-terminated representation of the local MAC address
*/
virtual const char *get_mac_address() = 0;
private:
char *_ssid;
char *_pass;
nsapi_security_t _security;
};