@@ -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 */
9191static 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
9698static sys_sem_t lwip_tcpip_inited ;
9799static void lwip_tcpip_init_irq (void * eh )
@@ -111,21 +113,23 @@ static sys_sem_t lwip_netif_up;
111113static 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
119123static 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 */
135139const 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
140144const 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-
245276static 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 */
489520const 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 ,
0 commit comments