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

/* Sample resource manager. */
#include <ios.h>
#include <ioslibc.h>

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

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

typedef struct {
    u8 inUse;
    u8 dev;
    u16 pos;
} DDFileDescriptor;

#define DD_NUM_DESCRIPTORS 4
static DDFileDescriptor desc[DD_NUM_DESCRIPTORS];
    
#define DD_DEV0 "/dev/byte0"
#define DD_DEV1 "/dev/byte1"

#define DD_LOG(...) printf("DD: " __VA_ARGS__)
#define DD_ERROR(msg) printf("DD: Error: %s failed\n", msg)

static IOSError
ddOpen(IOSResourceRequest *req)
{
    IOSError rv = IOS_ERROR_OK;
    u32 dev = 0;
    IOSResourceOpen *args = &req->args.open;
    int i;

    DD_LOG("in ddOpen: '%s'\n", args->path);

    if (!strncmp(args->path, DD_DEV0, sizeof(DD_DEV0)-1)) {
        dev = 0;
    } else if (!strncmp(args->path, DD_DEV1, sizeof(DD_DEV1)-1)) {
        dev = 1;
    } else {
        rv = IOS_ERROR_NOEXISTS;
        goto out;
    }

    for (i = 0; i < DD_NUM_DESCRIPTORS; ++i) {
        if (!desc[i].inUse) {
            break;
        }
    }

    if (i == DD_NUM_DESCRIPTORS) {
        rv = IOS_ERROR_MAX;
        goto out;
    }   

    desc[i].inUse = 1;
    desc[i].pos = 0;
    desc[i].dev = dev;
    rv = (IOSError) &desc[i];

    DD_LOG("open: new context handle %08x\n", req->handle);

out:
    return rv;
}

static IOSError
ddClose(IOSResourceRequest *req)
{
    IOSError rv = IOS_ERROR_OK;
    DDFileDescriptor *d = (DDFileDescriptor *) req->handle;

    DD_LOG("in ddClose\n");

    d->inUse = 0;

    return rv;
}

static IOSError
ddRead(IOSResourceRequest *req)
{
    IOSError rv = IOS_ERROR_OK;
    DDFileDescriptor *d = (DDFileDescriptor *) req->handle;

    DD_LOG("in ddRead\n");

    if (!d->inUse) {
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    u8 toggle = d->dev ? 0xff : 0x0;
    u32 len = req->args.read.outLen;
    u8 *p = req->args.read.outPtr;

    while (len) {
        *p++ = (d->pos++ & 0xff) ^ toggle;
        --len;
    }
    rv = req->args.read.outLen;

out:
    return rv;
}

static IOSError
ddSeek(IOSResourceRequest *req)
{
    IOSError rv = IOS_ERROR_OK;
    DDFileDescriptor *d = (DDFileDescriptor *) req->handle;
    IOSResourceSeek *args = (IOSResourceSeek *)&req->args.seek;

    if (!d->inUse) {
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    switch(args->whence) {
    case IOS_SEEK_SET:
        d->pos = args->offset & 0xff;
        break;
    case IOS_SEEK_CURRENT:
        d->pos = (d->pos + args->offset) & 0xff;
        break;
    case IOS_SEEK_END:
        d->pos = (255 + args->offset) & 0xff;
        break;
    default:
        rv = IOS_ERROR_INVALID;
        goto out;
    }

out:
    return rv;
}

int 
dd(void)
{
    IOSError rv;
    IOSMessageQueueId mq;
    IOSMessage msgArray[3];
    IOSMessage msg;
    
    DD_LOG("Simple device driver example\n");

    /* Create a message queue */
    DD_LOG("Create message queue\n");
    if ((mq = IOS_CreateMessageQueue(msgArray, sizeof(msgArray)/sizeof(msgArray[0]))) < 0) {
        rv = mq;
        DD_ERROR("IOS_CreateMessageQueue");
        goto out;
    }

    /* Register the message queue with the kernel */
    if ((rv = IOS_RegisterResourceManager("/dev/byte", mq)) != IOS_ERROR_OK) {
        DD_ERROR("IOS_RegisterResourceManager");
        goto out;
    }
        
    /* Main dispatch loop */
    while (1) {
        /* Receive a command */
        DD_LOG("Waiting for command\n");
        if ((rv = IOS_ReceiveMessage(mq, &msg, IOS_MESSAGE_BLOCK)) != IOS_ERROR_OK) {
            DD_ERROR("IOS_ReceiveMessage");
            goto out;
        }
        DD_LOG("Received resource request: %08x\n", msg);

        /* Dispatch command */
        IOSResourceRequest *req = (IOSResourceRequest *) msg;
        switch (req->cmd) {
        case IOS_OPEN:
            DD_LOG("  IOS_OPEN\n");
            rv = ddOpen(req);
            break;
        case IOS_CLOSE:
            DD_LOG("  IOS_CLOSE\n");
            rv = ddClose(req);
            break;
        case IOS_READ:
            DD_LOG("  IOS_READ\n");
            rv = ddRead(req);
            break;
        case IOS_SEEK:
            DD_LOG("  IOS_SEEK\n");
            rv = ddSeek(req);
            break;
        default:
            DD_LOG("  Unsupported\n");
            rv = IOS_ERROR_INVALID;
            break;
        }
        DD_LOG("Returning = %d\n", rv);
        IOS_ResourceRespond(req, rv);
    }

out:
    return 0;
}
