Skip to content

Commit b714f5d

Browse files
committed
Add "dhcp" property to turn DHCP on and off
1 parent 06894be commit b714f5d

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

shared-bindings/wiznet/wiznet5k.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,54 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
6666
return wiznet5k_create(args[0], args[1], args[2]);
6767
}
6868

69+
//| .. attribute:: connected
70+
//|
71+
//| is this device physically connected?
72+
//|
73+
6974
STATIC mp_obj_t wiznet5k_connected_get_value(mp_obj_t self_in) {
7075
(void)self_in;
7176
return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON);
7277
}
7378
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_connected_get_value_obj, wiznet5k_connected_get_value);
7479

75-
//| .. attribute:: connected
76-
//|
77-
//| is this device physically connected?
78-
//|
79-
8080
const mp_obj_property_t wiznet5k_connected_obj = {
8181
.base.type = &mp_type_property,
8282
.proxy = {(mp_obj_t)&wiznet5k_connected_get_value_obj,
8383
(mp_obj_t)&mp_const_none_obj,
8484
(mp_obj_t)&mp_const_none_obj},
8585
};
8686

87+
//| .. attribute:: dhcp
88+
//|
89+
//| is DHCP active on this device? (set to true to activate DHCP, false to turn it off)
90+
//|
91+
92+
STATIC mp_obj_t wiznet5k_dhcp_get_value(mp_obj_t self_in) {
93+
(void)self_in;
94+
return mp_obj_new_bool(wiznet5k_check_dhcp());
95+
}
96+
97+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_value);
98+
99+
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
100+
(void)self_in;
101+
if (mp_obj_is_true(value)) {
102+
wiznet5k_start_dhcp();
103+
} else {
104+
wiznet5k_stop_dhcp();
105+
}
106+
return mp_const_none;
107+
}
108+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(wiznet5k_dhcp_set_value_obj, wiznet5k_dhcp_set_value);
109+
110+
const mp_obj_property_t wiznet5k_dhcp_obj = {
111+
.base.type = &mp_type_property,
112+
.proxy = {(mp_obj_t)&wiznet5k_dhcp_get_value_obj,
113+
(mp_obj_t)&wiznet5k_dhcp_set_value_obj,
114+
(mp_obj_t)&mp_const_none_obj},
115+
};
116+
87117
//| .. method:: ifconfig(...)
88118
//|
89119
//| Called without parameters, returns a tuple of
@@ -121,6 +151,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k
121151
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = {
122152
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) },
123153
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&wiznet5k_connected_obj) },
154+
{ MP_ROM_QSTR(MP_QSTR_dhcp), MP_ROM_PTR(&wiznet5k_dhcp_obj) },
124155
};
125156

126157
STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table);

shared-module/wiznet/wiznet5k.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
324324
}
325325
}
326326

327-
static void wiznet5k_start_dhcp(void) {
327+
void wiznet5k_start_dhcp(void) {
328328
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
329329

330330
if (!wiznet5k_obj.dhcp_active) {
@@ -335,15 +335,17 @@ static void wiznet5k_start_dhcp(void) {
335335
}
336336
}
337337

338-
#if 0
339-
static void wiznet5k_stop_dhcp(void) {
338+
void wiznet5k_stop_dhcp(void) {
340339
if (wiznet5k_obj.dhcp_active) {
341340
wiznet5k_obj.dhcp_active = 0;
342341
DHCP_stop();
343342
WIZCHIP_EXPORT(close)(0);
344343
}
345344
}
346-
#endif
345+
346+
bool wiznet5k_check_dhcp(void) {
347+
return wiznet5k_obj.dhcp_active;
348+
}
347349

348350
/// Create and return a WIZNET5K object.
349351
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {

shared-module/wiznet/wiznet5k.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
6060
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
6161
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
6262

63+
void wiznet5k_start_dhcp(void);
64+
void wiznet5k_stop_dhcp(void);
65+
bool wiznet5k_check_dhcp(void);
66+
6367
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
6468

6569
#endif // MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H

0 commit comments

Comments
 (0)