/* ********************************************************************************************************* * CHECK DHCP STATE * * Description: This function checks the state of DHCP. * Arguments : None. * Returns : None. * Note : ********************************************************************************************************* */ void check_DHCP_state(uint8_t s, wiz_NetInfo *pWIZNETINFO) { uint16_t len, i; uint8_t type, flag; uint8_t d_addr[4]; type = 0; if ((len = getSn_RX_RSR(s)) > 0) { type = parseDHCPMSG(s, len, pWIZNETINFO); } switch ( dhcp_state ) { case STATE_DHCP_DISCOVER : if (type == DHCP_OFFER) { jsiConsolePrintf("> Receive DHCP_OFFER\r\n"); for (i = 0; i < 4; i++) d_addr[i] = 0xff; send_DHCP_REQUEST(s, Cip, d_addr, pWIZNETINFO); dhcp_state = STATE_DHCP_REQUEST; } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_REQUEST : if (type == DHCP_ACK) { jsiConsolePrintf("> Receive DHCP_ACK\r\n"); if (check_leasedIP(s, pWIZNETINFO)) { //iinchip_init() - WIZnet chip reset & Delay (10ms); //Set_network(); ctlwizchip(CW_RESET_WIZCHIP, 0); ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); /* for (i = 0; i < 12; i++) { //EEP_Write(EEP_LIP+i, *(uint8_t *)((*pWIZNETINFO).ip+i)); } */ next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_LEASED; } else { next_time = jshGetSystemTime() + DHCP_WAIT_TIME1; retry_count = 0; //dhcp_state = STATE_DHCP_DISCOVER; dhcp_state = STATE_DHCP_LEASED; jsiConsolePrintf("> => Recceived IP is invalid\r\n"); //iinchip_init(); //Set_Default(); //Set_network(); //ctlwizchip(CW_RESET_WIZCHIP, 0); //set_default_netinfo(); //ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); } } else if (type == DHCP_NAK) { jsiConsolePrintf("> Receive DHCP_NACK\r\n"); next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_DISCOVER; } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_LEASED : if ((lease_time.lVal != 0xffffffff) && ((lease_time.lVal/2) < jshGetSystemTime())) { jsiConsolePrintf("> Renewal IP address \r\n"); type = 0; for (i = 0; i < 4; i++) OLD_SIP[i] = (*pWIZNETINFO).ip[i]; for (i = 0; i < 4; i++) d_addr[i] = DHCP_SIP[i]; DHCP_XID++; send_DHCP_REQUEST(s, (*pWIZNETINFO).ip, d_addr, pWIZNETINFO); dhcp_state = STATE_DHCP_REREQUEST; next_time = jshGetSystemTime() + DHCP_WAIT_TIME; } break; case STATE_DHCP_REREQUEST : if (type == DHCP_ACK) { retry_count = 0; flag = 0; for (i = 0; i < 4; i++) { if (OLD_SIP[i] != (*pWIZNETINFO).ip[i]) { flag = 1; break; } } // change to new IP address if (flag) { //iinchip_init(); //Set_network(); ctlwizchip(CW_RESET_WIZCHIP, 0); ctlnetwork(CN_SET_NETINFO, (void*)&(*pWIZNETINFO)); } next_time = jshGetSystemTime() + DHCP_WAIT_TIME; dhcp_state = STATE_DHCP_LEASED; } else if (type == DHCP_NAK) { next_time = jshGetSystemTime() + DHCP_WAIT_TIME; retry_count = 0; dhcp_state = STATE_DHCP_DISCOVER; // jsiConsolePrintf("state : STATE_DHCP_DISCOVER\r\n"); } else check_Timeout(s, pWIZNETINFO); break; case STATE_DHCP_RELEASE : break; default : break; } }
/* ********************************************************************************************************* * CHECK DHCP STATE * * Description: This function checks the state of DHCP. * Arguments : None. * Returns : None. * Note : ********************************************************************************************************* */ uint8 check_DHCP_state(SOCKET s) { uint16 len; uint8 type; type = 0; if(getSn_SR(s)!=SOCK_CLOSED) { if ((len = getSn_RX_RSR(s)) > 0) { type = parseDHCPMSG(len); } } else { if(dhcp_state == STATE_DHCP_READY) { //init_dhcp_client(); #ifdef DHCP_DEBUG printf("state : STATE_DHCP_READY\r\n"); #endif } if(!socket(SOCK_DHCP,Sn_MR_UDP,DHCP_CLIENT_PORT,0x00)) { #ifdef DHCP_DEBUG printf("Fail to create the DHCPC_SOCK(%u)\r\n",SOCK_DHCP); #endif return DHCP_RET_ERR; } } switch ( dhcp_state ) { case STATE_DHCP_READY: DHCP_timeout = 0; reset_DHCP_time(); send_DHCP_DISCOVER(); DHCP_timer = 0;//set_timer0(DHCP_timer_handler); dhcp_state = STATE_DHCP_DISCOVER; break; case STATE_DHCP_DISCOVER : if (type == DHCP_OFFER) { reset_DHCP_time(); send_DHCP_REQUEST(); dhcp_state = STATE_DHCP_REQUEST; #ifdef DHCP_DEBUG printf("state : STATE_DHCP_REQUEST\r\n"); #endif } else check_DHCP_Timeout(); break; case STATE_DHCP_REQUEST : if (type == DHCP_ACK) { reset_DHCP_time(); if (check_leasedIP()) { #ifdef DHCP_DEBUG printf("state : STATE_DHCP_LEASED\r\n"); #endif dhcp_state = STATE_DHCP_LEASED; return DHCP_RET_UPDATE; } else { #ifdef DHCP_DEBUG printf("state : STATE_DHCP_DISCOVER\r\n"); #endif dhcp_state = STATE_DHCP_DISCOVER; return DHCP_RET_CONFLICT; } } else if (type == DHCP_NAK) { reset_DHCP_time(); dhcp_state = STATE_DHCP_DISCOVER; #ifdef DHCP_DEBUG printf("state : STATE_DHCP_DISCOVER\r\n"); #endif } else check_DHCP_Timeout(); break; case STATE_DHCP_LEASED : if ((dhcp_lease_time != 0xffffffff) && (dhcp_time>(dhcp_lease_time/2))) { type = 0; memcpy(OLD_SIP,GET_SIP,4); DHCP_XID++; send_DHCP_REQUEST(); dhcp_state = STATE_DHCP_REREQUEST; #ifdef DHCP_DEBUG printf("state : STATE_DHCP_REREQUEST\r\n"); #endif reset_DHCP_time(); } break; case STATE_DHCP_REREQUEST : if (type == DHCP_ACK) { if(memcmp(OLD_SIP,GET_SIP,4)!=0) { #ifdef DHCP_DEBUG printf("The IP address from the DHCP server is updated.\r\n"); printf("OLD_SIP=%u.%u.%u.%u",OLD_SIP[0],OLD_SIP[1],OLD_SIP[2],OLD_SIP[3]); printf("GET_SIP=%u.%u.%u.%u\r\n",GET_SIP[0],GET_SIP[1],GET_SIP[2],GET_SIP[3]); #endif return DHCP_RET_UPDATE; } #ifdef DHCP_DEBUG else { printf("state : STATE_DHCP_LEASED : same IP\r\n"); } #endif reset_DHCP_time(); dhcp_state = STATE_DHCP_LEASED; } else if (type == DHCP_NAK) { reset_DHCP_time(); dhcp_state = STATE_DHCP_DISCOVER; #ifdef DHCP_DEBUG printf("state : STATE_DHCP_DISCOVER\r\n"); #endif } else check_DHCP_Timeout(); break; default : dhcp_state = STATE_DHCP_READY; break; } if (DHCP_timeout == 1) { //kill_timer0(DHCP_timer); dhcp_state = STATE_DHCP_READY; return DHCP_RET_TIMEOUT; } return DHCP_RET_NONE; }