DHCP basic setup and communication?

Hi,

I will try to describe how to use DHCP application.
If you cannot understand, reply.
I explain based on ioLibrary with CoIDE project at WIZwiki.

Step 1.
Register DHCP timer handler.

[code]void SysTickIntHandler(void)
{
msTicks++; /* increment counter necessary in Delay()*/

////////////////////////////////////////////////////////
// SHOULD BE Added DHCP Timer Handler your 1s tick timer
if(msTicks % 1000 == 0)	DHCP_time_handler();
////////////////////////////////////////////////////////

}[/code]
SysTickIntHandler is MCU timer handler. This handler is dependent on MCU. So you should refer to your MCU vendor datasheet.

msTicks is increased every 1ms and DHCP timer wants to increase every 1sec. So DHCP_time_handler() called every 1sec by if(msTicks % 1000 == 0) DHCP_time_handler();

Step 2.

You can see structure gWIZNETINFO.

wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd}, .ip = {192, 168, 1, 123}, .sn = {255,255,255,0}, .gw = {192, 168, 1, 1}, .dns = {0,0,0,0}, .dhcp = NETINFO_DHCP };
You concern only MAC address, so set MAC address.
And then run setSHAR function.

setSHAR(gWIZNETINFO.mac);

Then MAC address setting is done.

Step 3.
Run DHCP_init function and register callback function.

DHCP_init(SOCK_DHCP, gDATABUF);

(It uses number 0 socket)

reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict);

Step 4.
Run main loop

/* Main Loop */ while(1) { switch(DHCP_run()) { case DHCP_IP_ASSIGN: case DHCP_IP_CHANGED: /* If this block empty, act with default_ip_assign & default_ip_update */ // // This example calls my_ip_assign in the two case. // // Add to ... // break; case DHCP_IP_LEASED: // // TO DO YOUR NETWORK APPs. // break; case DHCP_FAILED: /* ===== Example pseudo code ===== */ // The below code can be replaced your code or omitted. // if omitted, retry to process DHCP my_dhcp_retry++; if(my_dhcp_retry > MY_MAX_DHCP_RETRY) { #ifdef _MAIN_DEBUG_ printf(">> DHCP %d Failed\r\n", my_dhcp_retry); #endif my_dhcp_retry = 0; DHCP_stop(); // if restart, recall DHCP_init() network_init(); // apply the default static network and print out netinfo to serial } break; default: break; }
The main loop has DHCP_run() function. This function get IP, gateway, subnet. And then you concern only return value.
If main loop is success, return value is DHCP_IP_LEASED.
And IP, gateway, subbet is set already so just design your application in case DHCP_IP_LEASED:

Thanks.