Skip to content

Commit 09d0e99

Browse files
committed
assign variable socket number to DHCP
1 parent 09b83e9 commit 09d0e99

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

shared-module/wiznet/wiznet5k.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_
9393
}
9494
}
9595

96+
int get_available_socket(wiznet5k_obj_t *wiz) {
97+
for (uint8_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
98+
if ((wiz->socket_used & (1 << sn)) == 0) {
99+
wiz->socket_used |= (1 << sn);
100+
return sn;
101+
}
102+
}
103+
return -1;
104+
}
105+
96106
int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
97107
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
98108
*_errno = MP_EAFNOSUPPORT;
@@ -106,14 +116,8 @@ int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
106116
}
107117

108118
if (socket->u_param.fileno == -1) {
109-
// get first unused socket number ... 0 is reserved for DHCP
110-
for (mp_uint_t sn = 1; sn < _WIZCHIP_SOCK_NUM_; sn++) {
111-
if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) {
112-
wiznet5k_obj.socket_used |= (1 << sn);
113-
socket->u_param.fileno = sn;
114-
break;
115-
}
116-
}
119+
// get first unused socket number
120+
socket->u_param.fileno = get_available_socket(&wiznet5k_obj);
117121
if (socket->u_param.fileno == -1) {
118122
// too many open sockets
119123
*_errno = MP_EMFILE;
@@ -318,33 +322,37 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
318322
}
319323

320324
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
321-
if (wiznet5k_obj.dhcp_active) {
325+
if (wiznet5k_obj.dhcp_socket >= 0) {
322326
DHCP_time_handler();
323327
DHCP_run();
324328
}
325329
}
326330

327331
void wiznet5k_start_dhcp(void) {
332+
// XXX this should throw an error if DHCP fails
328333
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
329334

330-
if (!wiznet5k_obj.dhcp_active) {
335+
if (wiznet5k_obj.dhcp_socket < 0) {
331336
// Set up the socket to listen on UDP 68 before calling DHCP_init
332-
WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
333-
DHCP_init(0, dhcp_buf);
334-
wiznet5k_obj.dhcp_active = 1;
337+
wiznet5k_obj.dhcp_socket = get_available_socket(&wiznet5k_obj);
338+
if (wiznet5k_obj.dhcp_socket < 0) return;
339+
340+
WIZCHIP_EXPORT(socket)(wiznet5k_obj.dhcp_socket, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
341+
DHCP_init(wiznet5k_obj.dhcp_socket, dhcp_buf);
335342
}
336343
}
337344

338345
void wiznet5k_stop_dhcp(void) {
339-
if (wiznet5k_obj.dhcp_active) {
340-
wiznet5k_obj.dhcp_active = 0;
346+
if (wiznet5k_obj.dhcp_socket >= 0) {
341347
DHCP_stop();
342-
WIZCHIP_EXPORT(close)(0);
348+
WIZCHIP_EXPORT(close)(wiznet5k_obj.dhcp_socket);
349+
wiznet5k_obj.socket_used &= ~(1 << wiznet5k_obj.dhcp_socket);
350+
wiznet5k_obj.dhcp_socket = -1;
343351
}
344352
}
345353

346354
bool wiznet5k_check_dhcp(void) {
347-
return wiznet5k_obj.dhcp_active;
355+
return wiznet5k_obj.dhcp_socket >= 0;
348356
}
349357

350358
/// 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) {
357365
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
358366
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
359367
wiznet5k_obj.socket_used = 0;
368+
wiznet5k_obj.dhcp_socket = -1;
360369

361370
/*!< SPI configuration */
362371
// 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) {
394403
// seems we need a small delay after init
395404
mp_hal_delay_ms(250);
396405

406+
// dhcp is started by default
397407
wiznet5k_start_dhcp();
398408

399409
// register with network module

shared-module/wiznet/wiznet5k.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct _wiznet5k_obj_t {
3939
digitalio_digitalinout_obj_t cs;
4040
digitalio_digitalinout_obj_t rst;
4141
uint8_t socket_used;
42-
bool dhcp_active;
42+
int8_t dhcp_socket;
4343
} wiznet5k_obj_t;
4444

4545
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);

0 commit comments

Comments
 (0)