/*
 *               Copyright (C) 2006, 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.
 *
 */

/* Client for the sample resource manager in ipcrm.c */
#include <ios.h>
#include <ioslibc.h>
#include "iosresclt.h"
#include "ipc.h"

#include <stdio.h>
#include <string.h>

#define APP_LOG(...)       printf("APP: " __VA_ARGS__)
#define APP_LOG_CONT(...)  printf(__VA_ARGS__)

#ifdef _IOSE_

extern
void 
ipcRm(u32 arg);

extern
IOSMessageQueueId startMq; 

extern
IOSError
IpcCltInit(void);

#define RM_STACK_SIZE 16 * 1024

static
u8 stack[RM_STACK_SIZE];

#   define IPC_INIT                                                           \
        static IOSMessage msgArray[3];                                        \
        u32        id;                                                        \
        IOSMessage msg;                                                       \
                                                                              \
        IpcCltInit();                                                         \
                                                                              \
        if ((startMq = IOS_CreateMessageQueue(msgArray,                       \
                   sizeof(msgArray) / sizeof(msgArray[0]))) < 0)              \
        {                                                                     \
            APP_LOG("IOS_CreateMessageQueue");                                \
            goto out;                                                         \
        }                                                                     \
                                                                              \
        id = IOS_CreateThread(ipcRm, 0, stack,                                \
                sizeof(stack) / sizeof(stack[0]), IOS_GetThreadPriority(0), IOS_THREAD_CREATE_DETACHED);  \
        IOS_StartThread(id);                                                  \

#   define IPC_SYNC_WAIT                                                      \
        if (IOS_ReceiveMessage(startMq, &msg, IOS_MESSAGE_BLOCK)              \
                               != IOS_ERROR_OK)                               \
        {                                                                     \
            APP_LOG("IOS_ReceiveMessage");                                    \
            goto out;                                                         \
        }

#else

#   define IPC_INIT
#   define IPC_SYNC_WAIT

#endif
/*
static void
dump(u8 *buf, u32 len)
{
    APP_LOG("");
    while (len--) 
    {
        APP_LOG_CONT("%02x ", *buf++);
    }
    APP_LOG_CONT("\n\n");
}
*/

#define TEST_BUFF_SIZE 20

#define IPC_DEV0 "/dev/ipc0"
#define IPC_DEV1 "/dev/ipc1"

static
void
bufInit(
    u8   *buf,
    u32   size,
    IOSFd fd)
{
    static char randbuf[] = "random buffer contentent";
    u8 toggle = fd ? 0xaa : 0x55;

    while (size)
    {
        *buf++ = randbuf[size - 1] ^ toggle;
        --size;
    }
}

void 
ipcappsync(void)
{
    IOSError rv;
    IOSFd fd1 = -1, fd2 = -1;
    u8 wbuf[TEST_BUFF_SIZE], rbuf[TEST_BUFF_SIZE];

    APP_LOG("IPC Device driver test application\n");

    IPC_INIT;
    IPC_SYNC_WAIT;

    APP_LOG("Before IOS_Open(%s)\n", IPC_DEV0);
    fd1 = IOS_Open(IPC_DEV0, 0);
    APP_LOG("After IOS_Open(): %d\n\n", fd1);

    APP_LOG("Before IOS_Open(%s)\n", IPC_DEV1);
    fd2 = IOS_Open(IPC_DEV1, 0);
    APP_LOG("After IOS_Open(): %d\n\n", fd2);

    if (fd1 < 0 || fd2 < 0)
    {
        APP_LOG("ERROR fd1: %d fd2: %d\n", fd1, fd2);
        goto out;
    }

    bufInit(wbuf, sizeof(wbuf), fd1);

    APP_LOG("Before IOS_Write() fd: %d\n", fd1);
    rv = IOS_Write(fd1, wbuf, sizeof(wbuf));
    APP_LOG("After IOS_Write(): %d fd: %d\n\n", rv, fd1);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }

    APP_LOG("Before 1st IOS_Read() fd: %d \n", fd1);
    rv = IOS_Read(fd1, rbuf, sizeof(rbuf));
    APP_LOG("After 1st IOS_Read(): %d fd: %d\n", rv, fd1);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }
    if (memcmp(rbuf, wbuf, sizeof(rbuf)))
    {
        APP_LOG("R/W ERROR fd: %d\n", fd1);
        goto out;
    }

    bufInit(wbuf, sizeof(wbuf), fd2);

    APP_LOG("Before IOS_Write() fd: %d\n", fd2);
    rv = IOS_Write(fd2, wbuf, sizeof(wbuf));
    APP_LOG("After IOS_Write(): %d fd: %d\n\n", rv, fd2);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd2);
        goto out;
    }

    APP_LOG("Before 2nd IOS_Read() fd: %d\n", fd2);
    rv = IOS_Read(fd2, rbuf, sizeof(rbuf));
    APP_LOG("After 2nd IOS_Read(): %d fd: %d\n", rv, fd2);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd2);
        goto out;
    }
    if (memcmp(rbuf, wbuf, sizeof(rbuf)))
    {
        APP_LOG("R/W ERROR fd: %d\n", fd2);
        goto out;
    }

    APP_LOG("Before IOS_Seek() fd: %d\n", fd1);
    rv = IOS_Seek(fd1, 0x80, IOS_SEEK_SET);
    APP_LOG("After IOS_Seek(): %d fd: %d\n\n", rv, fd1);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }

    APP_LOG("Before IOS_Seek() fd: %d\n", fd2);
    rv = IOS_Seek(fd2, 0x80, IOS_SEEK_SET);
    APP_LOG("After IOS_Seek(): %d fd: %d\n\n", rv, fd2);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd2);
        goto out;
    }

    APP_LOG("Before IOS_Ioctl() fd: %d\n", fd1);
    rv = IOS_Ioctl(fd1, 0, rbuf, sizeof(rbuf), wbuf, sizeof(wbuf));
    APP_LOG("After IOS_Ioctl(): %d fd: %d\n\n", rv, fd1);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }

    APP_LOG("Before IOS_Ioctl() fd: %d\n", fd2);
    rv = IOS_Ioctl(fd2, 0, rbuf, sizeof(rbuf), wbuf, sizeof(wbuf));
    APP_LOG("After IOS_Ioctl(): %d fd: %d\n\n", rv, fd2);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd2);
        goto out;
    }

    APP_LOG("Before IOS_Close() fd: %d\n", fd1);
    rv = IOS_Close(fd1);
    APP_LOG("After IOS_Close(): %d fd: %d\n\n", rv, fd1);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }

    APP_LOG("Before IOS_CloseAsync() fd: %d\n", fd2);
    rv = IOS_Close(fd2);
    APP_LOG("After closed IOS_Close(): %d fd: %d\n\n", rv, fd2);
    if (rv < 0)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd2);
        goto out;
    }

    APP_LOG("Before closed IOS_Read() fd: %d\n", fd1);
    rv = IOS_Read(fd1, rbuf, sizeof(rbuf));
    APP_LOG("After closed IOS_Read(): %d fd: %d\n", rv, fd1);
    if (rv == IOS_ERROR_OK)
    {
        APP_LOG("Error %d fd: %d\n", rv, fd1);
        goto out;
    }
    APP_LOG("ipcappsync success EXIT\n\n");

out:
    return;
}

