Add "dhcp" property to turn DHCP on and off

This commit is contained in:
Nick Moore 2018-10-25 11:29:27 +11:00
parent 06894be294
commit b714f5d650
3 changed files with 46 additions and 9 deletions

View File

@ -66,17 +66,17 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
return wiznet5k_create(args[0], args[1], args[2]);
}
//| .. attribute:: connected
//|
//| is this device physically connected?
//|
STATIC mp_obj_t wiznet5k_connected_get_value(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_connected_get_value_obj, wiznet5k_connected_get_value);
//| .. attribute:: connected
//|
//| is this device physically connected?
//|
const mp_obj_property_t wiznet5k_connected_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&wiznet5k_connected_get_value_obj,
@ -84,6 +84,36 @@ const mp_obj_property_t wiznet5k_connected_obj = {
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: dhcp
//|
//| is DHCP active on this device? (set to true to activate DHCP, false to turn it off)
//|
STATIC mp_obj_t wiznet5k_dhcp_get_value(mp_obj_t self_in) {
(void)self_in;
return mp_obj_new_bool(wiznet5k_check_dhcp());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_value);
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
(void)self_in;
if (mp_obj_is_true(value)) {
wiznet5k_start_dhcp();
} else {
wiznet5k_stop_dhcp();
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(wiznet5k_dhcp_set_value_obj, wiznet5k_dhcp_set_value);
const mp_obj_property_t wiznet5k_dhcp_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&wiznet5k_dhcp_get_value_obj,
(mp_obj_t)&wiznet5k_dhcp_set_value_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. method:: ifconfig(...)
//|
//| 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
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&wiznet5k_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_dhcp), MP_ROM_PTR(&wiznet5k_dhcp_obj) },
};
STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table);

View File

@ -324,7 +324,7 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
}
}
static void wiznet5k_start_dhcp(void) {
void wiznet5k_start_dhcp(void) {
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
if (!wiznet5k_obj.dhcp_active) {
@ -335,15 +335,17 @@ static void wiznet5k_start_dhcp(void) {
}
}
#if 0
static void wiznet5k_stop_dhcp(void) {
void wiznet5k_stop_dhcp(void) {
if (wiznet5k_obj.dhcp_active) {
wiznet5k_obj.dhcp_active = 0;
DHCP_stop();
WIZCHIP_EXPORT(close)(0);
}
}
#endif
bool wiznet5k_check_dhcp(void) {
return wiznet5k_obj.dhcp_active;
}
/// Create and return a WIZNET5K object.
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {

View File

@ -60,6 +60,10 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
void wiznet5k_start_dhcp(void);
void wiznet5k_stop_dhcp(void);
bool wiznet5k_check_dhcp(void);
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
#endif // MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H