33
33
#include " lwip/dns.h"
34
34
#include " lwip/udp.h"
35
35
#include " lwip/lwip_errno.h"
36
+ #include " lwip-sys/arch/sys_arch.h"
36
37
37
38
#include " LWIPStack.h"
38
39
@@ -47,10 +48,10 @@ void LWIP::socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len)
47
48
return ;
48
49
}
49
50
50
- sys_prot_t prot = sys_arch_protect ();
51
-
52
51
LWIP &lwip = LWIP::get_instance ();
53
52
53
+ lwip.adaptation .lock ();
54
+
54
55
for (int i = 0 ; i < MEMP_NUM_NETCONN; i++) {
55
56
if (lwip.arena [i].in_use
56
57
&& lwip.arena [i].conn == nc
@@ -59,7 +60,7 @@ void LWIP::socket_callback(struct netconn *nc, enum netconn_evt eh, u16_t len)
59
60
}
60
61
}
61
62
62
- sys_arch_unprotect (prot );
63
+ lwip. adaptation . unlock ( );
63
64
}
64
65
65
66
#if !LWIP_IPV4 || !LWIP_IPV6
@@ -149,6 +150,7 @@ void LWIP::tcpip_init_irq(void *eh)
149
150
{
150
151
LWIP *lwip = static_cast <LWIP *>(eh);
151
152
lwip->tcpip_inited .release ();
153
+ sys_tcpip_thread_set ();
152
154
}
153
155
154
156
/* LWIP network stack implementation */
@@ -173,80 +175,84 @@ LWIP::LWIP()
173
175
arena_init ();
174
176
}
175
177
176
- nsapi_error_t LWIP::gethostbyname ( const char *host , SocketAddress *address, nsapi_version_t version )
178
+ nsapi_error_t LWIP::get_dns_server ( int index , SocketAddress *address)
177
179
{
178
- ip_addr_t lwip_addr;
179
-
180
- #if LWIP_IPV4 && LWIP_IPV6
181
- u8_t addr_type;
182
- if (version == NSAPI_UNSPEC) {
183
- const ip_addr_t *ip_addr = NULL ;
184
- if (default_interface) {
185
- ip_addr = get_ip_addr (true , &default_interface->netif );
186
- }
187
- // Prefer IPv6
188
- if (IP_IS_V6 (ip_addr)) {
189
- // If IPv4 is available use it as backup
190
- if (get_ipv4_addr (&default_interface->netif )) {
191
- addr_type = NETCONN_DNS_IPV6_IPV4;
192
- } else {
193
- addr_type = NETCONN_DNS_IPV6;
194
- }
195
- // Prefer IPv4
196
- } else {
197
- // If IPv6 is available use it as backup
198
- if (get_ipv6_addr (&default_interface->netif )) {
199
- addr_type = NETCONN_DNS_IPV4_IPV6;
200
- } else {
201
- addr_type = NETCONN_DNS_IPV4;
180
+ int dns_entries = 0 ;
181
+
182
+ for (int i = 0 ; i < DNS_MAX_SERVERS; i++) {
183
+ const ip_addr_t *ip_addr = dns_getserver (i);
184
+ if (!ip_addr_isany (ip_addr)) {
185
+ if (index == dns_entries) {
186
+ nsapi_addr_t addr;
187
+ convert_lwip_addr_to_mbed (&addr, ip_addr);
188
+ address->set_addr (addr);
189
+ return NSAPI_ERROR_OK;
202
190
}
191
+ dns_entries++;
203
192
}
204
- } else if (version == NSAPI_IPv4) {
205
- addr_type = NETCONN_DNS_IPV4;
206
- } else if (version == NSAPI_IPv6) {
207
- addr_type = NETCONN_DNS_IPV6;
208
- } else {
209
- return NSAPI_ERROR_DNS_FAILURE;
210
193
}
211
- err_t err = netconn_gethostbyname_addrtype (host, &lwip_addr, addr_type);
212
- #elif LWIP_IPV4
213
- if (version != NSAPI_IPv4 && version != NSAPI_UNSPEC) {
214
- return NSAPI_ERROR_DNS_FAILURE;
215
- }
216
- err_t err = netconn_gethostbyname (host, &lwip_addr);
217
- #elif LWIP_IPV6
218
- if (version != NSAPI_IPv6 && version != NSAPI_UNSPEC) {
219
- return NSAPI_ERROR_DNS_FAILURE;
194
+ return NSAPI_ERROR_NO_ADDRESS;
195
+ }
196
+
197
+ void LWIP::tcpip_thread_callback (void *ptr)
198
+ {
199
+ lwip_callback *cb = static_cast <lwip_callback *>(ptr);
200
+
201
+ if (cb->delay ) {
202
+ sys_timeout (cb->delay , LWIP::tcpip_thread_callback, ptr);
203
+ cb->delay = 0 ;
204
+ } else {
205
+ cb->callback ();
206
+ delete cb;
220
207
}
221
- err_t err = netconn_gethostbyname (host, &lwip_addr);
222
- #endif
208
+ }
223
209
224
- if (err != ERR_OK) {
225
- return NSAPI_ERROR_DNS_FAILURE;
210
+ nsapi_error_t LWIP::call (mbed::Callback<void ()> func)
211
+ {
212
+ return call_in (0 , func);
213
+ }
214
+
215
+ nsapi_error_t LWIP::call_in (int delay, mbed::Callback<void ()> func)
216
+ {
217
+ lwip_callback *cb = new lwip_callback;
218
+ if (!cb) {
219
+ return NSAPI_ERROR_NO_MEMORY;
226
220
}
227
221
228
- nsapi_addr_t addr;
229
- convert_lwip_addr_to_mbed (&addr, &lwip_addr);
230
- address->set_addr (addr);
222
+ cb->delay = delay;
223
+ cb->callback = func;
231
224
232
- return 0 ;
225
+ if (tcpip_callback_with_block (LWIP::tcpip_thread_callback, cb, 1 ) != ERR_OK) {
226
+ return NSAPI_ERROR_NO_MEMORY;
227
+ }
228
+
229
+ return NSAPI_ERROR_OK;
233
230
}
234
231
235
- nsapi_error_t LWIP::add_dns_server ( const SocketAddress &address )
232
+ const char * LWIP::get_ip_address ( )
236
233
{
237
- // Shift all dns servers down to give precedence to new server
238
- for (int i = DNS_MAX_SERVERS-1 ; i > 0 ; i--) {
239
- dns_setserver (i, dns_getserver (i-1 ));
234
+ if (!default_interface) {
235
+ return NULL ;
240
236
}
241
237
242
- nsapi_addr_t addr = address.get_addr ();
243
- ip_addr_t ip_addr;
244
- if (!convert_mbed_addr_to_lwip (&ip_addr, &addr)) {
245
- return NSAPI_ERROR_PARAMETER;
246
- }
238
+ const ip_addr_t *addr = get_ip_addr (true , &default_interface->netif );
247
239
248
- dns_setserver (0 , &ip_addr);
249
- return 0 ;
240
+ if (!addr) {
241
+ return NULL ;
242
+ }
243
+ #if LWIP_IPV6
244
+ if (IP_IS_V6 (addr)) {
245
+ return ip6addr_ntoa_r (ip_2_ip6 (addr), ip_address, sizeof (ip_address));
246
+ }
247
+ #endif
248
+ #if LWIP_IPV4
249
+ if (IP_IS_V4 (addr)) {
250
+ return ip4addr_ntoa_r (ip_2_ip4 (addr), ip_address, sizeof (ip_address));
251
+ }
252
+ #endif
253
+ #if LWIP_IPV6 && LWIP_IPV4
254
+ return NULL ;
255
+ #endif
250
256
}
251
257
252
258
nsapi_error_t LWIP::socket_open (nsapi_socket_t *handle, nsapi_protocol_t proto)
@@ -439,6 +445,7 @@ nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAdd
439
445
}
440
446
441
447
struct netbuf *buf = netbuf_new ();
448
+
442
449
err_t err = netbuf_ref (buf, data, (u16_t )size);
443
450
if (err != ERR_OK) {
444
451
netbuf_free (buf);
@@ -588,7 +595,7 @@ nsapi_error_t LWIP::setsockopt(nsapi_socket_t handle, int level, int optname, co
588
595
589
596
member_pair_index = next_free_multicast_member (s, 0 );
590
597
591
- sys_prot_t prot = sys_arch_protect ();
598
+ adaptation. lock ();
592
599
593
600
#if LWIP_IPV4
594
601
if (IP_IS_V4 (&if_addr)) {
@@ -601,7 +608,7 @@ nsapi_error_t LWIP::setsockopt(nsapi_socket_t handle, int level, int optname, co
601
608
}
602
609
#endif
603
610
604
- sys_arch_unprotect (prot );
611
+ adaptation. unlock ( );
605
612
606
613
if (igmp_err == ERR_OK) {
607
614
set_multicast_member_registry_bit (s, member_pair_index);
@@ -616,7 +623,7 @@ nsapi_error_t LWIP::setsockopt(nsapi_socket_t handle, int level, int optname, co
616
623
clear_multicast_member_registry_bit (s, member_pair_index);
617
624
s->multicast_memberships_count --;
618
625
619
- sys_prot_t prot = sys_arch_protect ();
626
+ adaptation. lock ();
620
627
621
628
#if LWIP_IPV4
622
629
if (IP_IS_V4 (&if_addr)) {
@@ -629,7 +636,7 @@ nsapi_error_t LWIP::setsockopt(nsapi_socket_t handle, int level, int optname, co
629
636
}
630
637
#endif
631
638
632
- sys_arch_unprotect (prot );
639
+ adaptation. unlock ( );
633
640
}
634
641
635
642
return err_remap (igmp_err);
0 commit comments