/*
 *               Copyright (C) 2008, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  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 BroadOn Communications Corp.
 *
 */
#include <ios.h>
#include <hw.h>
#include <iostypes.h>
#include <ioslibc.h>
#include <priority.h>

/*
 *
 * This program is used to test the IOS API to change priorities
 *
 */

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = IOS_PRIORITY_ES;

/* Thread created by idle process */
u8 testthrStack[1024]; /* 1024 is hardwired as stack size for this thread
                        * when the idle thread starts it. Please change
                        * it if you change this.
                        */

u8 stack2[512];

#define MAX_FORBIDDEN_PIDS  6

/* Test priority lists */
IOSPidPriority plForbidden[MAX_FORBIDDEN_PIDS] = {  /* Test forbidden PIDs */
    {IOS_KERNEL_PROCESS, 50},      /* PID = 0  */
    {IOS_ETICKET_PROCESS, 50},     /* PID = 1  */
    {IOS_FS_PROCESS, 50},          /* PID = 2  */
    {IOS_KD_PROCESS, 50},          /* PID = 12 */
    {IOS_STM_PROCESS, 50},         /* PID = 14 */
    {IOS_PPCBOOT_PROCESS, 50},     /* PID = 15 */
};

IOSPidPriority plInvalidPID[1] = {
    {100,  0}
};

IOSPidPriority plInvalidPriority[1] = {
    {IOS_OH0_PROCESS, IOS_PRIORITY_MAX+1}
};

IOSPidPriority plAllBefore[IOS_PROCESS_MAX] = {
    { 0,  0}, { 1,  0}, { 2,  0}, { 3,  0}, { 4,  0},
    { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
    {10,  0}, {11,  0}, {12,  0}, {13,  0}, {14,  0},
    {15,  0}, {16,  0}, {17,  0}, {18,  0},
};

IOSPidPriority plAllAfter[IOS_PROCESS_MAX] = {
    { 0,  0}, { 1,  0}, { 2,  0}, { 3,  0}, { 4,  0},
    { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
    {10,  0}, {11,  0}, {12,  0}, {13,  0}, {14,  0},
    {15,  0}, {16,  0}, {17,  0}, {18,  0},
};

IOSPidPriority plChangeableBefore[13] = {
    { 3,  0}, { 4, 0}, { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
    {10,  0}, {11, 0}, {13,  0}, {16,  0}, {17,  0}, {18,  0},
};

IOSPidPriority plChangeable[13] = {
    { 3,  0}, { 4, 0}, { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
    {10,  0}, {11, 0}, {13,  0}, {16,  0}, {17,  0}, {18,  0},
};

IOSPidPriority plChangeableAfter[13] = {
    { 3,  0}, { 4, 0}, { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
    {10,  0}, {11, 0}, {13,  0}, {16,  0}, {17,  0}, {18,  0},
};

IOSPidPriority pl[13] = 
    {{IOS_DI_PROCESS,  20},         /* PID = 3 */
     {IOS_OH0_PROCESS, 30},         /* PID = 4 */
     {IOS_OH1_PROCESS, 35},         /* PID = 5 */
     {IOS_EHCI_PROCESS, 40},        /* PID = 6 */
     {IOS_SDI_PROCESS, 50},         /* PID = 7 */
     {IOS_USBETH_PROCESS, 60},      /* PID = 8 */
     {IOS_NET_PROCESS, 70},         /* PID = 9 */
     {IOS_WD_PROCESS, 70},          /* PID = 10 */
     {IOS_WL_PROCESS, 70},          /* PID = 11 */
     {IOS_NCD_PROCESS, 80},         /* PID = 13 */
     {IOS_SSL_PROCESS, 90},         /* PID = 16 */
     {IOS_USB_PROCESS, 100},        /* PID = 17 */
     {IOS_P2P_PROCESS, 120},        /* PID = 18 */
    };

IOSPidPriority pl2[13] = {
      { 0,  0}, { 1,  0}, { 2,  0}, { 3,  0}, { 4,  0},
      { 5,  0}, { 6,  0}, { 7,  0}, { 8,  0}, { 9,  0},
      {10,  0}, {11,  0}, {12,  0},
    };
 
u32 testCnt;

static void
dumpPriorities(IOSPidPriority *pidPriList, u32 listSize)
{
    u32 i;

    printf("     [PID, MaxPriority]:");
    for (i = 0; i < listSize; i++) {
        if ((i % 5)==0) {
            printf("\n     ");
        }
        printf("[%2d,%3d] ", pidPriList[i].pid, pidPriList[i].priority);
    }
    printf("\n");
}


static u32
comparePriorities(IOSPidPriority *pList1, IOSPidPriority *pList2, u32 listSize)
{
    u32 i, errCnt;

    errCnt = 0;
    for (i =0; i < listSize; i++) {
        if ((pList1[i].pid == pList2[i].pid) &&
            (pList1[i].priority == pList2[i].priority)) {
            continue;
        }
        else {
            printf("  -> Mismatch: p1[%2d].(pid=%2d pri=%3d) != "
                    "p2[%2d].(pid=%2d pri=%3d)\n",
                    i, pList1[i].pid, pList1[i].priority, i, pList2[i].pid ,
                    pList2[i].priority);
            errCnt++;
        }
    }
    return errCnt;
}


static IOSError 
testForbiddenPIDs()
{
    u32 i, errCnt;
    IOSError rv;
    IOSPidPriority *p;

    printf("\n*** Test %2d: Using Forbidden PIDs\n", testCnt);

    errCnt = 0;
    for (i = 0; i < 6; i++) {
        p = &plForbidden[i];
        if ((rv = IOS_ChangePriorities(p, 1)) < 0) {
            printf("-> Changing priorities (pid %2d) failed: %d -> OK\n", 
                p->pid, rv);
        }
        else {
            printf("-> Changing priorities (pid %2d) succeeded -> BAD\n", 
                p->pid, rv);
            rv = IOS_ERROR_INVALID;
            errCnt++;
        }
    }
    if (!errCnt)
        rv = IOS_ERROR_OK;

    return rv;

}


static IOSError
testInvalidPID()
{
    IOSError rv;

    printf("\n*** Test %2d: Using Invalid PID\n", testCnt);

    if ((rv = IOS_ChangePriorities(plInvalidPID,
                  sizeof(plInvalidPID)/sizeof(IOSPidPriority))) < 0) {
        if (rv == IOS_ERROR_INVALID) {
            printf("  -> Changing priorities failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> Changing priorities failed: %d -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> Changing priorities succeeded -> BAD\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    if ((rv = IOS_GetMaxPriorities(plInvalidPID,
                  sizeof(plInvalidPID)/sizeof(IOSPidPriority))) < 0) {
        if (rv == IOS_ERROR_INVALID) {
            printf("  -> Getting priorities failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> Getting priorities failed: %d -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> Getting priorities succeeded -> BAD\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

out:
    return rv;
}


static IOSError
testInvalidPriority()
{
    IOSError rv;

    printf("\n*** Test %2d: Using Invalid Priority\n", testCnt);

    if ((rv = IOS_ChangePriorities(plInvalidPriority,
                  sizeof(plInvalidPriority)/sizeof(IOSPidPriority))) < 0) {
        if (rv == IOS_ERROR_INVALID) {
            printf("  -> Changing priorities failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> Changing priorities failed: %d -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> Changing priorities succeeded -> BAD\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

out:
    return rv;
}


static IOSError
testChangeablePIDs()
{
    IOSError rv;
    u32 i, cnt, listSize;

    printf("\n*** Test %2d: Using Changeable PIDs\n", testCnt);

    listSize = sizeof(plChangeableBefore)/sizeof(IOSPidPriority);

    /* First, read out the current priority of 13 processes: DI, OH0, OH1,... */
    printf("Retrieving MaxPriority of %d PIDs: DI, OH0, OH1...\n", listSize);
    rv = IOS_GetMaxPriorities(plChangeableBefore, listSize);
    if (rv != IOS_ERROR_OK) {
        printf("  -> ERROR: IOS_GetMaxPriorities failed %d\n", rv);
        goto out;
    }
    dumpPriorities(plChangeableBefore, listSize);

    /* Now, change the max priority of all changeable PIDs */
    for (i=0; i < listSize; i++) {
        plChangeable[i].priority = plChangeableBefore[i].priority + 5;
    }
    printf("Changing Priority of %d PIDs: DI, OH0, OH1...\n", listSize);
    rv = IOS_ChangePriorities(plChangeable, listSize);
    if (rv != IOS_ERROR_OK) {
        printf("  -> ERROR: IOS_ChangePriorities failed %d\n", rv);
        goto out;
    }

    printf("Retrieving MaxPriority of %d PIDs: DI, OH0, OH1...\n", listSize);
    rv = IOS_GetMaxPriorities(plChangeableAfter, listSize);
    if (rv != IOS_ERROR_OK) {
        printf("  -> ERROR: IOS_GetMaxPriorities failed %d\n", rv);
        goto out;
    }
    dumpPriorities(plChangeableAfter, listSize);

    printf("Comparing lists...\n");
    cnt = comparePriorities(plChangeable, plChangeableAfter, listSize);
    if (cnt == 0) {
        printf(" -> OK\n", rv);
    } else {
        printf(" -> ERROR: Unexpected number of different items: %d\n", cnt);
        rv = IOS_ERROR_INVALID;
    }

out:
    return rv;
}


static IOSError
testRestoreAllPIDs()
{
    IOSError rv;
    u32 i, cnt, listSize;

    printf("\n*** Test %2d: Restoring All Changeable PIDs\n", testCnt);

    listSize = sizeof(plChangeable)/sizeof(IOSPidPriority);

    /* Update the max priority of all changeable PIDs from saved data */
    for (i=0; i < listSize; i++) {
        u32 pid = plChangeable[i].pid;
        plChangeable[i].priority = plAllBefore[pid].priority;
    }
    printf("Changing Priority of %d PIDs...\n", listSize);
    rv = IOS_ChangePriorities(plChangeable, listSize);
    if (rv != IOS_ERROR_OK) {
        printf("  -> ERROR: IOS_ChangePriorities failed %d\n", rv);
        goto out;
    }

    printf("Retrieving MaxPriority of All PIDs...\n");

    rv = IOS_GetMaxPriorities(plAllAfter, IOS_PROCESS_MAX);
    if (rv != IOS_ERROR_OK) {
        printf("  -> ERROR: IOS_GetMaxPriorities failed %d\n", rv);
        goto out;
    }

    printf("Comparing lists...\n");
    cnt = comparePriorities(plAllBefore, plAllAfter, IOS_PROCESS_MAX);
    if (cnt == 0) {
        printf(" -> OK\n", rv);
    } else {
        printf(" -> ERROR: Unexpected number of different items: %d\n", cnt);
        rv = IOS_ERROR_INVALID;
    }
    dumpPriorities(plAllAfter, IOS_PROCESS_MAX);

out:
    return rv;
}


void
main(u32 arg)
{
    IOSError rv;
    u32 errCnt;

    errCnt = testCnt = 0;

    printf("\nRetrieving MaxPriorities of All PIDs...\n");
    if ((rv =IOS_GetMaxPriorities(plAllBefore, IOS_PROCESS_MAX)) < 0) {
        printf("-> ERROR: IOS_GetMaxPriorities -> Failed rv=%d\n", rv);
        goto end;
    } 
    dumpPriorities(plAllBefore, IOS_PROCESS_MAX);

    testCnt++;
    if ((rv = testForbiddenPIDs()) < 0) {
        printf("*** Test %2d: Using Forbidden PIDs -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Forbidden PIDs -> OK\n", testCnt);
    }

    testCnt++;
    if ((rv = testInvalidPID()) < 0) {
        printf("*** Test %2d: Using Invalid PID -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Invalid PID -> OK\n", testCnt);
    }

    testCnt++;
    if ((rv = testInvalidPriority()) < 0) {
        printf("*** Test %2d: Using Invalid Priority -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Invalid Priority -> OK\n", testCnt);
    }

    testCnt++;
    if ((rv = testChangeablePIDs()) < 0) {
        printf("*** Test %2d: Using Changeable PIDs -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Changeable PIDs -> OK\n", testCnt);
    }

    testCnt++;
    if ((rv = testRestoreAllPIDs()) < 0) {
        printf("*** Test %2d: Restoring All Changeable PIDs -> FAIL\n",testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Restoring All Changeable PIDs -> OK\n",testCnt);
    }

end:
    printf("\n>>>> Total Tests = %d, Errors = %d\n\n", testCnt, errCnt);

    return;
}

