//return:0 on error, 1 if request is sent and response is received
int DhcpClass::request_DHCP_lease()
{
	uint8_t messageType = 0;

	// Pick an initial transaction ID
	_dhcpTransactionId = random(1UL, 2000UL);
	_dhcpInitialTransactionId = _dhcpTransactionId;

	_dhcpUdpSocket.stop();
	if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0) {
		// Couldn't get a socket
		return 0;
	}

	presend_DHCP();

	int result = 0;

	unsigned long startTime = millis();

	while (_dhcp_state != STATE_DHCP_LEASED) {
		if (_dhcp_state == STATE_DHCP_START) {
			_dhcpTransactionId++;
			send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000));
			_dhcp_state = STATE_DHCP_DISCOVER;
		} else if (_dhcp_state == STATE_DHCP_REREQUEST) {
			_dhcpTransactionId++;
			send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime)/1000));
			_dhcp_state = STATE_DHCP_REQUEST;
		} else if (_dhcp_state == STATE_DHCP_DISCOVER) {
			uint32_t respId;
			messageType = parseDHCPResponse(_responseTimeout, respId);
			if (messageType == DHCP_OFFER) {
				// We'll use the transaction ID that the offer came with,
				// rather than the one we were up to
				_dhcpTransactionId = respId;
				send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime) / 1000));
				_dhcp_state = STATE_DHCP_REQUEST;
			}
		} else if (_dhcp_state == STATE_DHCP_REQUEST) {
			uint32_t respId;
			messageType = parseDHCPResponse(_responseTimeout, respId);
			if (messageType == DHCP_ACK) {
				_dhcp_state = STATE_DHCP_LEASED;
				result = 1;
				//use default lease time if we didn't get it
				if (_dhcpLeaseTime == 0) {
					_dhcpLeaseTime = DEFAULT_LEASE;
				}
				// Calculate T1 & T2 if we didn't get it
				if (_dhcpT1 == 0) {
					// T1 should be 50% of _dhcpLeaseTime
					_dhcpT1 = _dhcpLeaseTime >> 1;
				}
				if (_dhcpT2 == 0) {
					// T2 should be 87.5% (7/8ths) of _dhcpLeaseTime
					_dhcpT2 = _dhcpLeaseTime - (_dhcpLeaseTime >> 3);
				}
Exemple #2
0
//return:0 on error, 1 if request is sent and response is received, 2 if acquiring address
int DhcpClass::request_DHCP_lease_loop(){ //mg
    uint8_t messageType = 0;
    int result = 2;

    if(_dhcp_state != STATE_DHCP_LEASED)
    {
        if(_dhcp_state == STATE_DHCP_START)
        {
            _dhcpTransactionId++;

            send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - _startTimeRequest) / 1000));
            _dhcp_state = STATE_DHCP_DISCOVER;
        }
        else if(_dhcp_state == STATE_DHCP_REREQUEST){
            _dhcpTransactionId++;
            send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - _startTimeRequest)/1000));
            _dhcp_state = STATE_DHCP_REQUEST;
        }
        else if(_dhcp_state == STATE_DHCP_DISCOVER)
        {
            uint32_t respId;
//             messageType = parseDHCPResponse(_responseTimeout, respId);
            messageType = parseDHCPResponse(1, respId); //disable timeout, wait max 50ms
            if(messageType == DHCP_OFFER)
            {
                // We'll use the transaction ID that the offer came with,
                // rather than the one we were up to
                _dhcpTransactionId = respId;
                send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - _startTimeRequest) / 1000));
                _dhcp_state = STATE_DHCP_REQUEST;
            }
        }
        else if(_dhcp_state == STATE_DHCP_REQUEST)
        {
            uint32_t respId;
//             messageType = parseDHCPResponse(_responseTimeout, respId);
            messageType = parseDHCPResponse(1, respId); //disable timeout, wait max 50ms
            if(messageType == DHCP_ACK)
            {
                _dhcp_state = STATE_DHCP_LEASED;
                result = 1;
                //use default lease time if we didn't get it
                if(_dhcpLeaseTime == 0){
                    _dhcpLeaseTime = DEFAULT_LEASE;
                }
                // Calculate T1 & T2 if we didn't get it
                if(_dhcpT1 == 0){
                    // T1 should be 50% of _dhcpLeaseTime
                    _dhcpT1 = _dhcpLeaseTime >> 1;
                }
                if(_dhcpT2 == 0){
                    // T2 should be 87.5% (7/8ths) of _dhcpLeaseTime
                    _dhcpT2 = _dhcpLeaseTime - (_dhcpLeaseTime >> 3);
                }