int8_t OnionClient::send(OnionPacket* pkt) {
        int length = pkt->getBufferLength();
        // Send packet
        //int rc = _client->write((uint8_t*)pkt->getBuffer(), length);
        EthClientSend((int8_t*)pkt->getBuffer(),length);
        // Report if packet was sent successfully
        // for now we'll destroy the packet either way, but maybe we should be smarter
        //if (length == rc) {
            delete pkt;
            return 1;
        //}
        delete pkt;
        return 0;
}
Exemple #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;
        }
    }
}