예제 #1
0
void EthernetCallback(uint32_t ui32Event, void *pvData, uint32_t ui32Param) {
    uint8_t *pD = (uint8_t *)pvData;

    //
    // Handle events from the Ethernet client layer.
    //
    switch(ui32Event)
    {
        //
        // Ethernet client has received data.
        //
        case ETH_CLIENT_EVENT_RECEIVE:
        {
            // handle packet recieve parsing pD is data ui32Param is length
            memcpy(OnionClient::recvPkt->getBuffer(),pD,ui32Param);
            OnionClient::recvPkt->setPayloadLength(ui32Param-3);
            available = true;
        }

        //
        // Ethernet client has connected to the specified server/host and port.
        //
        case ETH_CLIENT_EVENT_CONNECT:
        {
            // update state of connection
	        OnionClient::socketConnected = true;
            break;
        }
        //
        // Ethernet client has obtained IP address via DHCP.
        //
        case ETH_CLIENT_EVENT_DHCP:
        {
            EthClientDNSResolve();
            break;
        }

        //
        // Ethernet client has received DNS response.
        //
        case ETH_CLIENT_EVENT_DNS:
        {
            if(ui32Param != 0)
            {
                //
                // If DNS resolved successfully, initialize the socket and
                // stack.
                //
                EthClientTCPConnect();
            }
            else
            {
                // Report error / update state (no dns available)
            }

            break;
        }
        //
        // Ethernet client has disconnected from the server/host.
        //
        case ETH_CLIENT_EVENT_DISCONNECT:
        {
            //
            // Close the socket.
            //
	        OnionClient::socketConnected = false;
            EthClientTCPDisconnect();

            break;
        }

        //
        // All other cases are unhandled.
        //
        case ETH_CLIENT_EVENT_SEND:
        {

            break;
        }


        case ETH_CLIENT_EVENT_ERROR:
        {
            
            break;
        }

        default:
        {

            break;
        }

    }
}
예제 #2
0
//*****************************************************************************
//
// Periodic Tick for the Ethernet client
//
// This function is the needed periodic tick for the Ethernet client. It needs
// to be called periodically through the use of a timer or systick.
//
// \return None.
//
//*****************************************************************************
void
EthClientTick(uint32_t ui32TickMS)
{
    uint32_t ui32IPAddr;
    int32_t i32Ret;

    if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DHCP_EN))
    {
        lwIPTimer(ui32TickMS);
    }

    if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN))
    {
        dns_tmr();
    }

    if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_TCP_EN))
    {
        tcp_tmr();
    }

    //
    // Check for loss of link.
    //
    if((g_sEnet.eState != iEthNoConnection) &&
       (lwIPLocalIPAddrGet() == 0xffffffff))
    {
        //
        // Reset the connection due to a loss of link.
        //
        EthClientReset();

        //
        // Signal a disconnect event.
        //
        g_sEnet.pfnEvent(ETH_EVENT_DISCONNECT, 0, 0);
    }
    else if(g_sEnet.eState == iEthNoConnection)
    {
        //
        // Once link is detected start DHCP.
        //
        if(lwIPLocalIPAddrGet() != 0xffffffff)
        {
            EthClientDHCPConnect();

            g_sEnet.eState = iEthDHCPWait;
        }
    }
    else if(g_sEnet.eState == iEthDHCPWait)
    {
        //
        // Get IP address.
        //
        ui32IPAddr = lwIPLocalIPAddrGet();

        //
        // If IP Address has not yet been assigned, update the display
        // accordingly.
        //
        if((ui32IPAddr != 0xffffffff) && (ui32IPAddr != 0))
        {
            //
            // Update the DHCP IP address.
            //
            g_sEnet.sLocalIP.addr = ui32IPAddr;

            g_sEnet.eState = iEthDHCPComplete;

            //
            // Stop DHCP timer since an address has been provided.
            //
            HWREGBITW(&g_sEnet.ui32Flags, FLAG_DHCP_STARTED) = 0;
        }
    }
    else if(g_sEnet.eState == iEthDHCPComplete)
    {
        if(g_sEnet.pcProxyName == 0)
        {
            //
            // Resolve the host by name.
            //
            i32Ret = EthClientDNSResolve("api.openweathermap.org");
        }
        else
        {
            //
            // Resolve the proxy by name.
            //
            i32Ret = EthClientDNSResolve(g_sEnet.pcProxyName);
        }

        if(i32Ret == ERR_OK)
        {
            //
            // If the address was already returned then go to idle.
            //
            g_sEnet.eState = iEthIdle;

            //
            // Notify the main routine of the new Ethernet connection.
            //
            g_sEnet.pfnEvent(ETH_EVENT_CONNECT, &g_sEnet.sLocalIP.addr, 4);
        }
        else if(i32Ret == ERR_INPROGRESS)
        {
            //
            // If the request is pending the go to the iEthDNSWait state.
            //
            g_sEnet.eState = iEthDNSWait;
        }
    }
    else if(g_sEnet.eState == iEthDNSWait)
    {
        //
        // Check if the host name was resolved.
        //
        if(HWREGBITW(&g_sEnet.ui32Flags, FLAG_DNS_ADDRFOUND))
        {
            //
            // Stop calling the DNS timer function.
            //
            HWREGBITW(&g_sEnet.ui32Flags, FLAG_TIMER_DNS_EN) = 0;

            g_sEnet.eState = iEthIdle;

            //
            // Notify the main routine of the new Ethernet connection.
            //
            g_sEnet.pfnEvent(ETH_EVENT_CONNECT, &g_sEnet.sLocalIP.addr, 4);
        }
    }
    else if(g_sEnet.eState == iEthTCPConnectWait)
    {
    }
    else if(g_sEnet.eState == iEthTCPConnectComplete)
    {
        err_t eError;

        g_sEnet.eState = iEthTCPOpen;

        eError = EthClientSend(g_sWeather.pcRequest,
                               g_sWeather.ui32RequestSize);

        if(eError == ERR_OK)
        {
            //
            // Waiting on a query response.
            //
            g_sEnet.eState = iEthQueryWait;
        }
        else
        {
            g_sEnet.eState = iEthIdle;
        }
    }
}