/*---------------------------------------------------------------------------*
  Project:  NitroDWC - nupTest
  File:     main.c

  Copyright 2003-2006 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.

  $NoKeywords: $
 *---------------------------------------------------------------------------*/


#include <es.h>
#include <netconnect.h>
#include <nhttp.h>
#include <nitro.h>
#include <nitro/card.h>
#include <nitro/fs.h>
#include <nitroWiFi.h>
#include <nnsys/fnd.h>
#include <sitedefs.h>
#include <string.h>


/* Routine for screen rendering*/
#include "draw.h"
#include "nup_test.h"


#define NUP_SITEDEF_APCLASS     "WiFi.LAN.1.AP.1"
#define NUP_HEAP_SIZE           (1024 * 1024)


static NNSFndAllocator __allocator;
extern SOCCaInfo nupCaInfo;


/*---------------------------------------------------------------------------*
  Name:         VBlankIntr

  Description:  V-blank callback

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
static void
VBlankIntr(void)
{
    OS_SetIrqCheckFlag(OS_IE_V_BLANK);
}


/*---------------------------------------------------------------------------*
  Name:         InitApp

  Description:  OS etc., application base part initialization

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
static void
InitApp(void)
{
    /* Skipping steps that are already done in NcGlobalInit() */

    //void   *tmp;
    //OSHeapHandle hh;

    /* OS initialization*/
    //OS_Init();
    OS_InitThread();
    //OS_InitTick();

    /* Arena initialization*/
#if 0
    tmp = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
    OS_SetArenaLo(OS_ARENA_MAIN, tmp);
    hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
    if (hh < 0)
        OS_Panic("ARM9: Fail to create heap...\n");
    hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
#endif

    /* Enable interrupts*/
    OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
    (void)OS_EnableIrqMask(OS_IE_V_BLANK);
    (void)OS_EnableIrq();
}

static void 
drawScreen(void)
{
    ClearFrame(0);
    DrawBegin();
    SetTextColor(RGB555_WHITE);

    DrawText((32-sizeof("NUP TEST"))/2*10, 0, "NUP TEST");
    DrawText(10, 100, "Press A to start test");

    DrawEnd();
}


static void *
__alloc(u32 size, int align)
{
    return NNS_FndAllocFromExpHeapEx(__allocator.pHeap, size, align);
}


static void
__free(void *p)
{
    NNS_FndFreeToExpHeap(__allocator.pHeap, p);
}


static NUPError
__runNupTests()
{
    NUPError rv = NUP_ERROR_OK;
    u32 logLevel, caLength, threadPrio, nInitArgs;
    SOCCaInfo *caInfo;
    NUPNameValue initArgs[9];

    logLevel = NUP_LOG_FINE;
    caInfo = &nupCaInfo;
    caLength = 1;
    threadPrio = 18;

    initArgs[0].name = NUP_ALLOC;
    initArgs[0].value = (const char *) __alloc;
    initArgs[1].name = NUP_FREE;
    initArgs[1].value = (const char *) __free;
    initArgs[2].name = NUP_LOG_LEVEL;
    initArgs[2].value = (const char *) &logLevel;
    initArgs[3].name = NUP_CA_INFO;
    initArgs[3].value = (const char *) &caInfo;
    initArgs[4].name = NUP_CA_LENGTH;
    initArgs[4].value = (const char *) &caLength;
    initArgs[5].name = NUP_HOSTNAME;
    initArgs[5].value = "nus.lab1.routefree.com:16990";
    initArgs[6].name = NUP_COUNTRY;
    initArgs[6].value = "JP";
    initArgs[7].name = NUP_REGION;
    initArgs[7].value = "JPN";
    initArgs[8].name = NUP_THREAD_PRIO;
    initArgs[8].value = (const char *) &threadPrio;
    nInitArgs = sizeof(initArgs) / sizeof(initArgs[0]);

    /* TODO: add update partial test */

    /* Update all test */
    rv = nupTest((NUPTestLog) printf, (NUPTestSleep) OS_Sleep, initArgs, nInitArgs, NULL, 0);
    if (rv != NUP_ERROR_OK) {
        goto end;
    }

end:
    return rv;
}


/*---------------------------------------------------------------------------*
  Name:         NitroMain

  Description:  main entry point

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NitroMain(void)
{
    u32 last, r;
    NUPError rv = NUP_ERROR_OK;
    void *heapStart = NULL;
    NNSFndHeapHandle heap = NULL;

    /* Sample application initialization*/
    NcGlobalInit();
    InitApp();
    InitDraw();
    SetClearColor(RGB555_CLEAR);

    CTRDG_Init();
    CTRDG_Enable(TRUE);

    /* Initialize ES */
    rv = ES_InitLib();
    if (rv != ES_ERR_OK) {
        printf("Failed to initialize ES, rv=%d\n", rv);
        rv = NUP_ERROR_INTERNAL;
        goto end;
    }

    /* Start networking */
    NcStart(NUP_SITEDEF_APCLASS);

    heapStart = OS_Alloc(NUP_HEAP_SIZE);
    if (heapStart == NULL) {
        printf("Failed to allocate memory\n");
        rv = NUP_ERROR_INTERNAL;
        goto end;
    }

    heap = NNS_FndCreateExpHeap(heapStart, NUP_HEAP_SIZE);
    if (heap == NULL) {
        printf("Failed to create heap\n");
        rv = NUP_ERROR_INTERNAL;
        goto end;
    }
    NNS_FndInitAllocatorForExpHeap(&__allocator, heap, 0);

    rv = NHTTPStartup(__alloc, __free, 18);
    if (rv != NHTTP_ERROR_NONE) {
        printf("Failed to start NHTTP, rv=%d\n", rv);
        rv = NUP_ERROR_INTERNAL;
        goto end;
    }

    drawScreen();

    /* Screen transition*/
    last = PAD_Read();
    for (;;) {
        r = PAD_Read();
        if ((r & PAD_BUTTON_A) && !(last & PAD_BUTTON_A)) {
            (void) __runNupTests();
        }
        last = r;
    }

end:
    while (1) {
        /* Infinite loop */
    }
}
