aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George2018-06-01 13:33:14 +1000
committerDamien George2018-06-01 13:33:14 +1000
commitd9f1ecece2f86d3c007ec51853ec499f1aebb21b (patch)
tree14c07a43b5f47c65de17f1cbcd07bcdacaa8cbf9
parent7437215ad71dd62897190f20a79344c007b5f875 (diff)
stm32/modnetwork: Provide generic implementation of ifconfig method.
All it needs is a lwIP netif to function.
-rw-r--r--ports/stm32/modnetwork.c57
-rw-r--r--ports/stm32/modnetwork.h2
2 files changed, 59 insertions, 0 deletions
diff --git a/ports/stm32/modnetwork.c b/ports/stm32/modnetwork.c
index 42052e3c7..416a31f6a 100644
--- a/ports/stm32/modnetwork.c
+++ b/ports/stm32/modnetwork.c
@@ -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
diff --git a/ports/stm32/modnetwork.h b/ports/stm32/modnetwork.h
index fa706d869..3224d785e 100644
--- a/ports/stm32/modnetwork.h
+++ b/ports/stm32/modnetwork.h
@@ -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;