Get DHCP working ...

This commit is contained in:
Nick Moore 2018-10-09 16:28:30 +11:00
parent 9b36d33df1
commit a60700b1c5
5 changed files with 42 additions and 28 deletions

View File

@ -6,6 +6,7 @@
//! \version 1.0.3
//! \date 2013/10/21
//! \par Revision history
//! <2018/10/09> Nick Moore fixes for CircuitPython
//! <2014/05/01> V1.0.3. Refer to M20140501
//! 1. Implicit type casting -> Explicit type casting.
//! 2. replace 0x01 with PACK_REMAINED in recvfrom()
@ -393,15 +394,7 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
CHECK_SOCKDATA();
//M20140501 : For avoiding fatal error on memory align mismatched
//if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
{
uint32_t taddr;
taddr = ((uint32_t)addr[0]) & 0x000000FF;
taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
if (taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID;
}
//
if ((addr[0] | addr[1] | addr[2] | addr[3]) == 0) return SOCKERR_IPINVALID;
if(port == 0) return SOCKERR_PORTZERO;
tmp = getSn_SR(sn);
if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS;

View File

@ -6,6 +6,7 @@
//! \version 1.1.0
//! \date 2013/11/18
//! \par Revision history
//! <2018/10/09> Modified by Nick Moore for CircuitPython
//! <2013/11/18> 1st Release
//! <2012/12/20> V1.1.0
//! 1. Optimize code
@ -51,7 +52,7 @@
//#include "Ethernet/socket.h"
//#include "Internet/DHCP/dhcp.h"
#include "../../Ethernet/socket.h"
#include "../../ethernet/socket.h"
#include "dhcp.h"
/* If you want to display debug & processing message, Define _DHCP_DEBUG_ in dhcp.h */
@ -408,7 +409,7 @@ void send_DHCP_DISCOVER(void)
printf("> Send DHCP_DISCOVER\r\n");
#endif
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
}
/* SEND DHCP REQUEST */
@ -503,7 +504,7 @@ void send_DHCP_REQUEST(void)
printf("> Send DHCP_REQUEST\r\n");
#endif
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
}
@ -564,7 +565,7 @@ void send_DHCP_DECLINE(void)
printf("\r\n> Send DHCP_DECLINE\r\n");
#endif
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
}
/* PARSE REPLY pDHCPMSG */
@ -581,13 +582,13 @@ int8_t parseDHCPMSG(void)
if((len = getSn_RX_RSR(DHCP_SOCKET)) > 0)
{
len = recvfrom(DHCP_SOCKET, (uint8_t *)pDHCPMSG, len, svr_addr, &svr_port);
len = WIZCHIP_EXPORT(recvfrom)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, len, svr_addr, &svr_port);
#ifdef _DHCP_DEBUG_
printf("DHCP message : %d.%d.%d.%d(%d) %d received. \r\n",svr_addr[0],svr_addr[1],svr_addr[2], svr_addr[3],svr_port, len);
#endif
}
else return 0;
if (svr_port == DHCP_SERVER_PORT) {
if (svr_port == DHCP_SERVER_PORT) {
// compare mac address
if ( (pDHCPMSG->chaddr[0] != DHCP_CHADDR[0]) || (pDHCPMSG->chaddr[1] != DHCP_CHADDR[1]) ||
(pDHCPMSG->chaddr[2] != DHCP_CHADDR[2]) || (pDHCPMSG->chaddr[3] != DHCP_CHADDR[3]) ||
@ -677,7 +678,7 @@ uint8_t DHCP_run(void)
if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED;
if(getSn_SR(DHCP_SOCKET) != SOCK_UDP)
socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
WIZCHIP_EXPORT(socket)(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
ret = DHCP_RUNNING;
type = parseDHCPMSG();
@ -801,7 +802,7 @@ uint8_t DHCP_run(void)
void DHCP_stop(void)
{
close(DHCP_SOCKET);
WIZCHIP_EXPORT(close)(DHCP_SOCKET);
dhcp_state = STATE_DHCP_STOP;
}
@ -869,7 +870,7 @@ int8_t check_DHCP_leasedIP(void)
// IP conflict detection : ARP request - ARP reply
// Broadcasting ARP Request for check the IP conflict using UDP sendto() function
ret = sendto(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
ret = WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
// RCR value restore
setRCR(tmp);
@ -893,7 +894,7 @@ int8_t check_DHCP_leasedIP(void)
}
}
void DHCP_init(uint8_t s, uint8_t * buf)
void DHCP_init(uint8_t s, DHCP_INIT_BUFFER_TYPE* buf)
{
uint8_t zeroip[4] = {0,0,0,0};
getSHAR(DHCP_CHADDR);

View File

@ -55,7 +55,7 @@
/* Retry to processing DHCP */
#define MAX_DHCP_RETRY 2 ///< Maximum retry count
#define DHCP_WAIT_TIME 10 ///< Wait Time 10s
#define DHCP_WAIT_TIME 3 ///< Wait Time 3s (was 10s)
/* UDP port numbers for DHCP */
#define DHCP_SERVER_PORT 67 ///< DHCP server port number
@ -78,12 +78,14 @@ enum
DHCP_STOPPED ///< Stop processing DHCP protocol
};
#define DHCP_INIT_BUFFER_TYPE uint32_t
#define DHCP_INIT_BUFFER_SIZE (137)
/*
* @brief DHCP client initialization (outside of the main loop)
* @param s - socket number
* @param buf - buffer for processing DHCP message
*/
void DHCP_init(uint8_t s, uint8_t * buf);
void DHCP_init(uint8_t s, DHCP_INIT_BUFFER_TYPE* buf);
/*
* @brief DHCP 1s Tick Timer handler

View File

@ -300,6 +300,7 @@ SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\
ethernet/wizchip_conf.c \
ethernet/socket.c \
internet/dns/dns.c \
internet/dhcp/dhcp.c \
)
endif # MICROPY_PY_WIZNET5K

View File

@ -48,6 +48,7 @@
#include "ethernet/wizchip_conf.h"
#include "ethernet/socket.h"
#include "internet/dns/dns.h"
#include "internet/dhcp/dhcp.h"
/// \moduleref network
@ -337,6 +338,24 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
}
#endif
static void wiznet5k_try_dhcp(void) {
DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
// 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);
DHCP_init(0, dhcp_buf);
// try a few times for DHCP ... XXX this should be asynchronous.
for (int i=0; i<10; i++) {
DHCP_time_handler();
int dhcp_state = DHCP_run();
if (dhcp_state == DHCP_IP_LEASED || dhcp_state == DHCP_IP_CHANGED) break;
mp_hal_delay_ms(1000);
}
DHCP_stop();
WIZCHIP_EXPORT(close)(0);
}
/******************************************************************************/
// MicroPython bindings
@ -377,16 +396,12 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write);
uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2k buffer for each socket
// 2k buffer for each socket
uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
ctlwizchip(CW_INIT_WIZCHIP, sn_size);
// set some sensible default values; they are configurable using ifconfig method
wiz_NetInfo netinfo = {
.ip = {192, 168, 0, 18},
.sn = {255, 255, 255, 0},
.gw = {192, 168, 0, 1},
.dns = {8, 8, 8, 8}, // Google public DNS
.dhcp = NETINFO_STATIC,
.dhcp = NETINFO_DHCP,
};
network_module_create_random_mac_address(netinfo.mac);
ctlnetwork(CN_SET_NETINFO, (void*)&netinfo);
@ -394,6 +409,8 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
// seems we need a small delay after init
mp_hal_delay_ms(250);
wiznet5k_try_dhcp();
// register with network module
network_module_register_nic(&wiznet5k_obj);