stm32/modnetwork: Provide generic implementation of ifconfig method.
All it needs is a lwIP netif to function.
This commit is contained in:
parent
7437215ad7
commit
d9f1ecece2
@ -31,6 +31,7 @@
|
||||
#include "py/objlist.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/mphal.h"
|
||||
#include "lib/netutils/netutils.h"
|
||||
#include "modnetwork.h"
|
||||
|
||||
#if MICROPY_PY_NETWORK
|
||||
@ -39,6 +40,8 @@
|
||||
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/timeouts.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/dhcp.h"
|
||||
|
||||
u32_t sys_now(void) {
|
||||
return mp_hal_ticks_ms();
|
||||
@ -117,4 +120,58 @@ const mp_obj_module_t mp_module_network = {
|
||||
.globals = (mp_obj_dict_t*)&mp_module_network_globals,
|
||||
};
|
||||
|
||||
/*******************************************************************************/
|
||||
// Implementations of network methods that can be used by any interface
|
||||
|
||||
#if MICROPY_PY_LWIP
|
||||
|
||||
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args) {
|
||||
if (n_args == 0) {
|
||||
// Get IP addresses
|
||||
const ip_addr_t *dns = dns_getserver(0);
|
||||
mp_obj_t tuple[4] = {
|
||||
netutils_format_ipv4_addr((uint8_t*)&netif->ip_addr, NETUTILS_BIG),
|
||||
netutils_format_ipv4_addr((uint8_t*)&netif->netmask, NETUTILS_BIG),
|
||||
netutils_format_ipv4_addr((uint8_t*)&netif->gw, NETUTILS_BIG),
|
||||
netutils_format_ipv4_addr((uint8_t*)&dns, NETUTILS_BIG),
|
||||
};
|
||||
return mp_obj_new_tuple(4, tuple);
|
||||
} else if (args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_dhcp)) {
|
||||
// Start the DHCP client
|
||||
if (dhcp_supplied_address(netif)) {
|
||||
dhcp_renew(netif);
|
||||
} else {
|
||||
dhcp_stop(netif);
|
||||
dhcp_start(netif);
|
||||
}
|
||||
|
||||
// Wait for DHCP to get IP address
|
||||
uint32_t start = mp_hal_ticks_ms();
|
||||
while (!dhcp_supplied_address(netif)) {
|
||||
if (mp_hal_ticks_ms() - start > 10000) {
|
||||
mp_raise_msg(&mp_type_OSError, "timeout waiting for DHCP to get IP address");
|
||||
}
|
||||
mp_hal_delay_ms(100);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
} else {
|
||||
// Release and stop any existing DHCP
|
||||
dhcp_release(netif);
|
||||
dhcp_stop(netif);
|
||||
// Set static IP addresses
|
||||
mp_obj_t *items;
|
||||
mp_obj_get_array_fixed_n(args[1], 4, &items);
|
||||
netutils_parse_ipv4_addr(items[0], (uint8_t*)&netif->ip_addr, NETUTILS_BIG);
|
||||
netutils_parse_ipv4_addr(items[1], (uint8_t*)&netif->netmask, NETUTILS_BIG);
|
||||
netutils_parse_ipv4_addr(items[2], (uint8_t*)&netif->gw, NETUTILS_BIG);
|
||||
ip_addr_t dns;
|
||||
netutils_parse_ipv4_addr(items[3], (uint8_t*)&dns, NETUTILS_BIG);
|
||||
dns_setserver(0, &dns);
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // MICROPY_PY_NETWORK
|
||||
|
@ -44,6 +44,8 @@ typedef struct _mod_network_nic_type_t {
|
||||
void (*poll_callback)(void *data, struct netif *netif);
|
||||
} mod_network_nic_type_t;
|
||||
|
||||
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args);
|
||||
|
||||
#else
|
||||
|
||||
struct _mod_network_socket_obj_t;
|
||||
|
Loading…
x
Reference in New Issue
Block a user