assign variable socket number to DHCP
This commit is contained in:
parent
09b83e96b3
commit
09d0e99d5b
@ -93,6 +93,16 @@ int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int get_available_socket(wiznet5k_obj_t *wiz) {
|
||||||
|
for (uint8_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
|
||||||
|
if ((wiz->socket_used & (1 << sn)) == 0) {
|
||||||
|
wiz->socket_used |= (1 << sn);
|
||||||
|
return sn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
|
int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
|
||||||
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
|
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
|
||||||
*_errno = MP_EAFNOSUPPORT;
|
*_errno = MP_EAFNOSUPPORT;
|
||||||
@ -106,14 +116,8 @@ int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (socket->u_param.fileno == -1) {
|
if (socket->u_param.fileno == -1) {
|
||||||
// get first unused socket number ... 0 is reserved for DHCP
|
// get first unused socket number
|
||||||
for (mp_uint_t sn = 1; sn < _WIZCHIP_SOCK_NUM_; sn++) {
|
socket->u_param.fileno = get_available_socket(&wiznet5k_obj);
|
||||||
if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) {
|
|
||||||
wiznet5k_obj.socket_used |= (1 << sn);
|
|
||||||
socket->u_param.fileno = sn;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (socket->u_param.fileno == -1) {
|
if (socket->u_param.fileno == -1) {
|
||||||
// too many open sockets
|
// too many open sockets
|
||||||
*_errno = MP_EMFILE;
|
*_errno = MP_EMFILE;
|
||||||
@ -318,33 +322,37 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
|
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
|
||||||
if (wiznet5k_obj.dhcp_active) {
|
if (wiznet5k_obj.dhcp_socket >= 0) {
|
||||||
DHCP_time_handler();
|
DHCP_time_handler();
|
||||||
DHCP_run();
|
DHCP_run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wiznet5k_start_dhcp(void) {
|
void wiznet5k_start_dhcp(void) {
|
||||||
|
// XXX this should throw an error if DHCP fails
|
||||||
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
|
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
|
||||||
|
|
||||||
if (!wiznet5k_obj.dhcp_active) {
|
if (wiznet5k_obj.dhcp_socket < 0) {
|
||||||
// Set up the socket to listen on UDP 68 before calling DHCP_init
|
// Set up the socket to listen on UDP 68 before calling DHCP_init
|
||||||
WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
|
wiznet5k_obj.dhcp_socket = get_available_socket(&wiznet5k_obj);
|
||||||
DHCP_init(0, dhcp_buf);
|
if (wiznet5k_obj.dhcp_socket < 0) return;
|
||||||
wiznet5k_obj.dhcp_active = 1;
|
|
||||||
|
WIZCHIP_EXPORT(socket)(wiznet5k_obj.dhcp_socket, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
|
||||||
|
DHCP_init(wiznet5k_obj.dhcp_socket, dhcp_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wiznet5k_stop_dhcp(void) {
|
void wiznet5k_stop_dhcp(void) {
|
||||||
if (wiznet5k_obj.dhcp_active) {
|
if (wiznet5k_obj.dhcp_socket >= 0) {
|
||||||
wiznet5k_obj.dhcp_active = 0;
|
|
||||||
DHCP_stop();
|
DHCP_stop();
|
||||||
WIZCHIP_EXPORT(close)(0);
|
WIZCHIP_EXPORT(close)(wiznet5k_obj.dhcp_socket);
|
||||||
|
wiznet5k_obj.socket_used &= ~(1 << wiznet5k_obj.dhcp_socket);
|
||||||
|
wiznet5k_obj.dhcp_socket = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wiznet5k_check_dhcp(void) {
|
bool wiznet5k_check_dhcp(void) {
|
||||||
return wiznet5k_obj.dhcp_active;
|
return wiznet5k_obj.dhcp_socket >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create and return a WIZNET5K object.
|
/// Create and return a WIZNET5K object.
|
||||||
@ -357,6 +365,7 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
|
|||||||
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
|
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
|
||||||
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
|
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
|
||||||
wiznet5k_obj.socket_used = 0;
|
wiznet5k_obj.socket_used = 0;
|
||||||
|
wiznet5k_obj.dhcp_socket = -1;
|
||||||
|
|
||||||
/*!< SPI configuration */
|
/*!< SPI configuration */
|
||||||
// XXX probably should check if the provided SPI is already configured, and
|
// XXX probably should check if the provided SPI is already configured, and
|
||||||
@ -394,6 +403,7 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
|
|||||||
// seems we need a small delay after init
|
// seems we need a small delay after init
|
||||||
mp_hal_delay_ms(250);
|
mp_hal_delay_ms(250);
|
||||||
|
|
||||||
|
// dhcp is started by default
|
||||||
wiznet5k_start_dhcp();
|
wiznet5k_start_dhcp();
|
||||||
|
|
||||||
// register with network module
|
// register with network module
|
||||||
|
@ -39,7 +39,7 @@ typedef struct _wiznet5k_obj_t {
|
|||||||
digitalio_digitalinout_obj_t cs;
|
digitalio_digitalinout_obj_t cs;
|
||||||
digitalio_digitalinout_obj_t rst;
|
digitalio_digitalinout_obj_t rst;
|
||||||
uint8_t socket_used;
|
uint8_t socket_used;
|
||||||
bool dhcp_active;
|
int8_t dhcp_socket;
|
||||||
} wiznet5k_obj_t;
|
} wiznet5k_obj_t;
|
||||||
|
|
||||||
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);
|
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);
|
||||||
|
Loading…
Reference in New Issue
Block a user