/*---------------------------------------------------------------------------*
Project: NitroDWC - demos - sktp
File: main.c

Copyright 2007 Nintendo.  All rights reserved.

These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law.  They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.

$Log: main.c,v $
Revision 1.3  2007/05/12 00:45:16  achan
- added test for socket shutdown

Revision 1.2  2007/05/11 23:54:52  achan
- nhttp, nup: change to use port 16990 instead of 16992 for NUS
- sktp: changed comment

Revision 1.1  2007/05/08 00:02:30  achan
- added socket proxy test cases


$NoKeywords$
 *---------------------------------------------------------------------------*/


#include <nitro.h>
#include <nitroWiFi.h>
#include <string.h>
#include <sitedefs.h>

#include "padkeyboard.h"
#include "output.h"
#include "screen.h"
#include "netconnect.h"

#include <sktp_api.h>


#define SITEDEFS_APCLASS    "WiFi.LAN.1.AP.1"
#define HOSTNAME_LEN        256
#define SERVER_PORT         8000
#define SKTP_BUF_SIZE       4096
#define TOTAL_BYTES          (4 * 1024 * 1024)


static char sktpBuf[SKTP_BUF_SIZE];


static void
VBlankIntr(void)
{
    OS_SetIrqCheckFlag(OS_IE_V_BLANK);
}


void
NitroMain()
{
    SKTPError rv = SKTP_ERROR_OK;
    int skt, numBytes, total;
    char hostname[HOSTNAME_LEN];

    /* Calls the initialization function and the connect function. */
    NcGlobalInit();
    NcStart(SITEDEFS_APCLASS);

    /* Initializes the screen */
    InitScreen();

    FlushScreen();
    FlushDebugOutput();

    /*
     * Empty call for getting key input data (strategy for pressing
     * A button in IPL)
	 */
    (void)PAD_Read();

    /* Initializes the keyboard. */
    InitKeyboardThread();
    KeyboardValid();

    /*
     * Connection to the SKTP server
     * Input the target hostname with the Pad
     */
    DebugOutput("hostname>");

    /* The initial hostname is achan.routefree.com */
    MI_CpuClear8(hostname, HOSTNAME_LEN);
    (void) OS_SNPrintf(hostname, sizeof(hostname), "achan.routefree.com");

    /* Initialize SKTP */
    rv = SKTP_Startup();
    if (rv != SKTP_ERROR_OK) {
        OS_TPrintf("Failed to start SKTP, rv=%d\n", rv);
        goto end;
    }

    /* Reset all sockets */
    rv = SKTP_ResetAll();
    if (rv != SKTP_ERROR_OK) {
        OS_TPrintf("Failed to reset SKTP, rv=%d\n", rv);
        goto end;
    }

    while (1) {
        PadKeySetString(hostname);

        /* Enter the hostname */
        PadKeyGetInputString(hostname, HOSTNAME_LEN);
        OS_TPrintf("hostname: %s\n", hostname);
        if (hostname[0] == '!') {
            break;
        }

        skt = SKTP_Open();
        if (skt < 0) {
            OS_TPrintf("Failed to open socket, rv=%d\n", skt);
            rv = skt;
            goto end;
        }
        OS_TPrintf("Opened socket %d\n", skt);

        rv = SKTP_Connect(skt, hostname, SERVER_PORT);
        if (rv != SKTP_ERROR_OK) {
            OS_TPrintf("Failed to connect to host, rv=%d\n", rv);
            goto end;
        }
        OS_TPrintf("Connected to %s:%d\n", hostname, SERVER_PORT);

        memset(sktpBuf, 'a', SKTP_BUF_SIZE);
        total = 0;
        while (total < TOTAL_BYTES) {
            if ((numBytes = SKTP_Write(skt, sktpBuf, SKTP_BUF_SIZE)) < 0) {
                OS_TPrintf("Failed to write, rv=%d\n", rv);
                goto end;
            }
            total += numBytes;
        }
        OS_TPrintf("Finished writing %d bytes\n", TOTAL_BYTES);

        while (total > 0) {
            if ((numBytes = SKTP_Read(skt, sktpBuf, SKTP_BUF_SIZE)) < 0) {
                OS_TPrintf("Failed to read, rv=%d\n", rv);
                goto end;
            }
            total -= numBytes;

            if (numBytes == 0) {
                continue;
            }

            /* Check a few bytes to verify correctness */
            if (sktpBuf[0] != 'b' || sktpBuf[numBytes/2] != 'b' || sktpBuf[numBytes-1] != 'b') {
                OS_TPrintf("Failed read data check\n");
                rv = SKTP_ERROR_INVALID;
                goto end;
            }
            memset(sktpBuf, 0, SKTP_BUF_SIZE);
        }
        OS_TPrintf("Finished reading %d bytes\n", TOTAL_BYTES);

        rv = SKTP_Shutdown(skt);
        if (rv != SKTP_ERROR_OK) {
            OS_TPrintf("Failed to shut down socket, rv=%d\n", rv);
            goto end;
        }
        OS_TPrintf("Shut down socket %d\n", skt);

        rv = SKTP_Close(skt);
        if (rv != SKTP_ERROR_OK) {
            OS_TPrintf("Failed to close socket, rv=%d\n", rv);
            goto end;
        }
        OS_TPrintf("Closed socket %d\n", skt);
    }

    /* De-initialize SKTP */
    rv = SKTP_Cleanup();
    if (rv != SKTP_ERROR_OK) {
        OS_TPrintf("Failed to clean up SKTP, rv=%d\n", rv);
        goto end;
    }

    OS_TPrintf("Exit test client\n");

end:
    if (rv != SKTP_ERROR_OK) {
        OS_Panic("FAILED");
    }

    NcFinish();
    OS_Terminate();
}
