Skip to content

Commit 584384c

Browse files
authored
Merge pull request #2664 from geky/nsapi-static-config
nsapi - Add optional API for static configuration of network interfaces
2 parents 8e473be + 64ac210 commit 584384c

File tree

12 files changed

+475
-86
lines changed

12 files changed

+475
-86
lines changed

features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,57 @@
1919

2020

2121
/* Interface implementation */
22+
EthernetInterface::EthernetInterface()
23+
: _dhcp(false), _ip_address(), _netmask(), _gateway()
24+
{
25+
}
26+
27+
int EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
28+
{
29+
_dhcp = false;
30+
strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
31+
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
32+
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
33+
return 0;
34+
}
35+
36+
int EthernetInterface::set_dhcp(bool dhcp)
37+
{
38+
_dhcp = dhcp;
39+
return 0;
40+
}
41+
2242
int EthernetInterface::connect()
2343
{
24-
return lwip_bringup();
44+
return lwip_bringup(_dhcp,
45+
_ip_address[0] ? _ip_address : 0,
46+
_netmask[0] ? _netmask : 0,
47+
_gateway[0] ? _gateway : 0);
2548
}
2649

2750
int EthernetInterface::disconnect()
2851
{
2952
return lwip_bringdown();
3053
}
3154

55+
const char *EthernetInterface::get_mac_address()
56+
{
57+
return lwip_get_mac_address();
58+
}
59+
3260
const char *EthernetInterface::get_ip_address()
3361
{
3462
return lwip_get_ip_address();
3563
}
3664

37-
const char *EthernetInterface::get_mac_address()
65+
const char *EthernetInterface::get_netmask()
3866
{
39-
return lwip_get_mac_address();
67+
return lwip_get_netmask();
68+
}
69+
70+
const char *EthernetInterface::get_gateway()
71+
{
72+
return lwip_get_gateway();
4073
}
4174

4275
NetworkStack *EthernetInterface::get_stack()

features/net/FEATURE_IPV4/lwip-interface/EthernetInterface.h

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ class NetworkStack;
3131
class EthernetInterface : public EthInterface
3232
{
3333
public:
34+
/** EthernetInterface lifetime
35+
*/
36+
EthernetInterface();
37+
38+
/** Set a static IP address
39+
*
40+
* Configures this network interface to use a static IP address.
41+
* Implicitly disables DHCP, which can be enabled in set_dhcp.
42+
* Requires that the network is disconnected.
43+
*
44+
* @param address Null-terminated representation of the local IP address
45+
* @param netmask Null-terminated representation of the local network mask
46+
* @param gateway Null-terminated representation of the local gateway
47+
* @return 0 on success, negative error code on failure
48+
*/
49+
virtual int set_network(const char *ip_address, const char *netmask, const char *gateway);
50+
51+
/** Enable or disable DHCP on the network
52+
*
53+
* Requires that the network is disconnected
54+
*
55+
* @param dhcp False to disable dhcp (defaults to enabled)
56+
* @return 0 on success, negative error code on failure
57+
*/
58+
virtual int set_dhcp(bool dhcp);
59+
3460
/** Start the interface
3561
* @return 0 on success, negative on failure
3662
*/
@@ -41,22 +67,49 @@ class EthernetInterface : public EthInterface
4167
*/
4268
virtual int disconnect();
4369

44-
/** Get the internally stored IP address
45-
* @return IP address of the interface or null if not yet connected
70+
/** Get the local MAC address
71+
*
72+
* Provided MAC address is intended for info or debug purposes and
73+
* may not be provided if the underlying network interface does not
74+
* provide a MAC address
75+
*
76+
* @return Null-terminated representation of the local MAC address
77+
* or null if no MAC address is available
78+
*/
79+
virtual const char *get_mac_address();
80+
81+
/** Get the local IP address
82+
*
83+
* @return Null-terminated representation of the local IP address
84+
* or null if no IP address has been recieved
4685
*/
4786
virtual const char *get_ip_address();
4887

49-
/** Get the internally stored MAC address
50-
* @return MAC address of the interface
88+
/** Get the local network mask
89+
*
90+
* @return Null-terminated representation of the local network mask
91+
* or null if no network mask has been recieved
92+
*/
93+
virtual const char *get_netmask();
94+
95+
/** Get the local gateway
96+
*
97+
* @return Null-terminated representation of the local gateway
98+
* or null if no network mask has been recieved
5199
*/
52-
virtual const char *get_mac_address();
100+
virtual const char *get_gateway();
53101

54102
protected:
55103
/** Provide access to the underlying stack
56104
*
57105
* @return The underlying network stack
58106
*/
59107
virtual NetworkStack *get_stack();
108+
109+
bool _dhcp;
110+
char _ip_address[NSAPI_IPv4_SIZE];
111+
char _netmask[NSAPI_IPv4_SIZE];
112+
char _gateway[NSAPI_IPv4_SIZE];
60113
};
61114

62115

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.c

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ static void lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t
8989

9090
/* TCP/IP and Network Interface Initialisation */
9191
static struct netif lwip_netif;
92-
93-
static char lwip_ip_addr[NSAPI_IP_SIZE] = "\0";
94-
static char lwip_mac_addr[NSAPI_MAC_SIZE] = "\0";
92+
static bool lwip_dhcp = false;
93+
static char lwip_mac_address[NSAPI_MAC_SIZE] = "\0";
94+
static char lwip_ip_address[NSAPI_IPv4_SIZE] = "\0";
95+
static char lwip_netmask[NSAPI_IPv4_SIZE] = "\0";
96+
static char lwip_gateway[NSAPI_IPv4_SIZE] = "\0";
9597

9698
static sys_sem_t lwip_tcpip_inited;
9799
static void lwip_tcpip_init_irq(void *eh)
@@ -111,21 +113,23 @@ static sys_sem_t lwip_netif_up;
111113
static void lwip_netif_status_irq(struct netif *lwip_netif)
112114
{
113115
if (netif_is_up(lwip_netif)) {
114-
strcpy(lwip_ip_addr, inet_ntoa(lwip_netif->ip_addr));
116+
strcpy(lwip_ip_address, inet_ntoa(lwip_netif->ip_addr));
117+
strcpy(lwip_netmask, inet_ntoa(lwip_netif->netmask));
118+
strcpy(lwip_gateway, inet_ntoa(lwip_netif->gw));
115119
sys_sem_signal(&lwip_netif_up);
116120
}
117121
}
118122

119123
static void lwip_set_mac_address(void)
120124
{
121125
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
122-
snprintf(lwip_mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
126+
snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
123127
MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
124128
MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
125129
#else
126130
char mac[6];
127131
mbed_mac_address(mac);
128-
snprintf(lwip_mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
132+
snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
129133
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
130134
#endif
131135
}
@@ -134,15 +138,26 @@ static void lwip_set_mac_address(void)
134138
/* LWIP interface implementation */
135139
const char *lwip_get_mac_address(void)
136140
{
137-
return lwip_mac_addr[0] ? lwip_mac_addr : 0;
141+
return lwip_mac_address[0] ? lwip_mac_address : 0;
138142
}
139143

140144
const char *lwip_get_ip_address(void)
141145
{
142-
return lwip_ip_addr[0] ? lwip_ip_addr : 0;
146+
return lwip_ip_address[0] ? lwip_ip_address : 0;
147+
}
148+
149+
const char *lwip_get_netmask(void)
150+
{
151+
return lwip_netmask[0] ? lwip_netmask : 0;
143152
}
144153

145-
int lwip_bringup(void)
154+
const char *lwip_get_gateway(void)
155+
{
156+
return lwip_gateway[0] ? lwip_gateway : 0;
157+
}
158+
159+
160+
int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
146161
{
147162
// Check if we've already connected
148163
if (lwip_get_ip_address()) {
@@ -162,6 +177,7 @@ int lwip_bringup(void)
162177
sys_arch_sem_wait(&lwip_tcpip_inited, 0);
163178

164179
memset(&lwip_netif, 0, sizeof lwip_netif);
180+
165181
netif_add(&lwip_netif, 0, 0, 0, NULL, eth_arch_enetif_init, tcpip_input);
166182
netif_set_default(&lwip_netif);
167183

@@ -175,7 +191,26 @@ int lwip_bringup(void)
175191
lwip_arena_init();
176192

177193
// Connect to the network
178-
dhcp_start(&lwip_netif);
194+
lwip_dhcp = dhcp;
195+
if (lwip_dhcp) {
196+
err_t err = dhcp_start(&lwip_netif);
197+
if (err) {
198+
return NSAPI_ERROR_DHCP_FAILURE;
199+
}
200+
} else {
201+
ip_addr_t ip_addr;
202+
ip_addr_t netmask_addr;
203+
ip_addr_t gw_addr;
204+
205+
if (!inet_aton(ip, &ip_addr) ||
206+
!inet_aton(netmask, &netmask_addr) ||
207+
!inet_aton(gw, &gw_addr)) {
208+
return NSAPI_ERROR_PARAMETER;
209+
}
210+
211+
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
212+
netif_set_up(&lwip_netif);
213+
}
179214

180215
// Wait for an IP Address
181216
u32_t ret = sys_arch_sem_wait(&lwip_netif_up, 15000);
@@ -194,9 +229,17 @@ int lwip_bringdown(void)
194229
}
195230

196231
// Disconnect from the network
197-
dhcp_release(&lwip_netif);
198-
dhcp_stop(&lwip_netif);
199-
lwip_ip_addr[0] = '\0';
232+
if (lwip_dhcp) {
233+
dhcp_release(&lwip_netif);
234+
dhcp_stop(&lwip_netif);
235+
lwip_dhcp = false;
236+
237+
lwip_ip_address[0] = '\0';
238+
lwip_netmask[0] = '\0';
239+
lwip_gateway[0] = '\0';
240+
} else {
241+
netif_set_down(&lwip_netif);
242+
}
200243

201244
return 0;
202245
}
@@ -230,18 +273,6 @@ static int lwip_err_remap(err_t err) {
230273

231274

232275
/* LWIP network stack implementation */
233-
static nsapi_addr_t lwip_getaddr(nsapi_stack_t *stack)
234-
{
235-
if (!lwip_get_ip_address()) {
236-
return (nsapi_addr_t){0};
237-
}
238-
239-
nsapi_addr_t addr;
240-
addr.version = NSAPI_IPv4;
241-
inet_aton(lwip_get_ip_address(), (ip_addr_t *)addr.bytes);
242-
return addr;
243-
}
244-
245276
static int lwip_gethostbyname(nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host)
246277
{
247278
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
487518

488519
/* LWIP network stack */
489520
const nsapi_stack_api_t lwip_stack_api = {
490-
.get_ip_address = lwip_getaddr,
491521
.gethostbyname = lwip_gethostbyname,
492522
.socket_open = lwip_socket_open,
493523
.socket_close = lwip_socket_close,

features/net/FEATURE_IPV4/lwip-interface/lwip_stack.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ extern "C" {
2525

2626

2727
// Access to lwip through the nsapi
28-
int lwip_bringup(void);
28+
int lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw);
2929
int lwip_bringdown(void);
3030

31-
extern nsapi_stack_t lwip_stack;
32-
3331
const char *lwip_get_mac_address(void);
3432
const char *lwip_get_ip_address(void);
33+
const char *lwip_get_netmask(void);
34+
const char *lwip_get_gateway(void);
35+
36+
extern nsapi_stack_t lwip_stack;
3537

3638

3739
#ifdef __cplusplus

0 commit comments

Comments
 (0)