@@ -89,9 +89,11 @@ static void lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t
89
89
90
90
/* TCP/IP and Network Interface Initialisation */
91
91
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" ;
95
97
96
98
static sys_sem_t lwip_tcpip_inited ;
97
99
static void lwip_tcpip_init_irq (void * eh )
@@ -111,21 +113,23 @@ static sys_sem_t lwip_netif_up;
111
113
static void lwip_netif_status_irq (struct netif * lwip_netif )
112
114
{
113
115
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 ));
115
119
sys_sem_signal (& lwip_netif_up );
116
120
}
117
121
}
118
122
119
123
static void lwip_set_mac_address (void )
120
124
{
121
125
#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" ,
123
127
MBED_MAC_ADDR_0 , MBED_MAC_ADDR_1 , MBED_MAC_ADDR_2 ,
124
128
MBED_MAC_ADDR_3 , MBED_MAC_ADDR_4 , MBED_MAC_ADDR_5 );
125
129
#else
126
130
char mac [6 ];
127
131
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" ,
129
133
mac [0 ], mac [1 ], mac [2 ], mac [3 ], mac [4 ], mac [5 ]);
130
134
#endif
131
135
}
@@ -134,15 +138,26 @@ static void lwip_set_mac_address(void)
134
138
/* LWIP interface implementation */
135
139
const char * lwip_get_mac_address (void )
136
140
{
137
- return lwip_mac_addr [0 ] ? lwip_mac_addr : 0 ;
141
+ return lwip_mac_address [0 ] ? lwip_mac_address : 0 ;
138
142
}
139
143
140
144
const char * lwip_get_ip_address (void )
141
145
{
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 ;
143
152
}
144
153
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 )
146
161
{
147
162
// Check if we've already connected
148
163
if (lwip_get_ip_address ()) {
@@ -162,6 +177,7 @@ int lwip_bringup(void)
162
177
sys_arch_sem_wait (& lwip_tcpip_inited , 0 );
163
178
164
179
memset (& lwip_netif , 0 , sizeof lwip_netif );
180
+
165
181
netif_add (& lwip_netif , 0 , 0 , 0 , NULL , eth_arch_enetif_init , tcpip_input );
166
182
netif_set_default (& lwip_netif );
167
183
@@ -175,7 +191,26 @@ int lwip_bringup(void)
175
191
lwip_arena_init ();
176
192
177
193
// 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
+ }
179
214
180
215
// Wait for an IP Address
181
216
u32_t ret = sys_arch_sem_wait (& lwip_netif_up , 15000 );
@@ -194,9 +229,17 @@ int lwip_bringdown(void)
194
229
}
195
230
196
231
// 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
+ }
200
243
201
244
return 0 ;
202
245
}
@@ -230,18 +273,6 @@ static int lwip_err_remap(err_t err) {
230
273
231
274
232
275
/* 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
-
245
276
static int lwip_gethostbyname (nsapi_stack_t * stack , nsapi_addr_t * addr , const char * host )
246
277
{
247
278
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
487
518
488
519
/* LWIP network stack */
489
520
const nsapi_stack_api_t lwip_stack_api = {
490
- .get_ip_address = lwip_getaddr ,
491
521
.gethostbyname = lwip_gethostbyname ,
492
522
.socket_open = lwip_socket_open ,
493
523
.socket_close = lwip_socket_close ,
0 commit comments