/*
 *              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.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sc/shell.h>
#ifdef SYS_OPEN
	#undef SYS_OPEN
#endif
#include <sc/semihost.h>
#include <sc/semihost_usb.h>
#include "utils.h"
#include "cmd.h"

extern char cmdBuf[16384];

int 
semihost(int fd)
{
    int rv = -1;
	unsigned int pos, len;
    char *data=NULL;
    struct stat sb;
    USBSemihostRequest req;
    USBSemihostReply   reply;
    static char *fileModes[] = {"r", "rb", "r+", "r+b", "w", "wb", 
                    "w+", "w+b", "a", "ab", "a+", "a+b"};

    reply.magic = USB_SEMI_REPLY_MAGIC;
    while (1) {
        if (hostRecv(fd, &req, sizeof(req)) < 0) {
            printf("Error: recv semihost command error \n");
            goto out;
        }

         if (req.magic != USB_SEMI_REQ_MAGIC) {
            printf("Error: semihost command magic number is 0x%x\n", req.magic);
            goto out;
        }

        reply.len = reply.pcFd = 0;
        switch (req.req) {
        case SYS_OPEN:
            if ((req.len > 0) && req.data[0] < 12 && req.data[0] >= 0) {
                if (hostRecv(fd, cmdBuf, req.len) >= 0) {
					cmdBuf[req.len] = 0;
                    reply.pcFd = (int) fopen(cmdBuf, fileModes[req.data[0]]);
                }
            }
            break;
        case SYS_CLOSE:
            if (req.pcFd) fclose((FILE *) req.pcFd);
            break;
        case SYS_WRITE:
            if (req.pcFd && req.len>0) {
                data = (char *) malloc(req.len);
                if (data != NULL) {
                    for (pos = 0; pos <req.len; pos += VN_MAX_SIZE) {
                        len = (pos + VN_MAX_SIZE) > req.len ? req.len - pos : VN_MAX_SIZE;
                        hostRecv(fd, data+pos, len);
                    }
                    fwrite(data, 1, req.len, (FILE *) req.pcFd);
                    free(data);
                    data = NULL;
                } else
                    printf("Cannot alloc buffer to hold %d NC data\n", req.len);
            } 
            break;
        case SYS_READ:
            if (req.pcFd && req.data[0]>0) {
                data = (char *) malloc(req.data[0]);
                if (data !=  NULL) {
                    fread(data, 1, req.data[0], (FILE *) req.pcFd);
                    reply.len = req.data[0];
                }
            }
            break;
        case SYS_READC:
            reply.data[0] =  getchar();
            break;
        case SYS_SEEK:
            if (req.pcFd) fseek((FILE *) req.pcFd, req.data[0], SEEK_SET);
            break;
        case SYS_FLEN:
            if (req.pcFd) {
                fstat(fileno((FILE *) req.pcFd), &sb);
                reply.data[0] = sb.st_size;
            } else reply.data[0] = -1;
            break;
        case USB_SEMIHOST_END:
            rv = 0;
        case USB_SEMIHOST_START:
            break;
        default:
            printf("Unsupported semihost command \n");
            break;
        }

        if (req.req == USB_SEMIHOST_END) break;
        if (req.req == USB_SEMIHOST_START) continue;

        hostSend(fd, &reply, sizeof(reply));

        if (reply.len > 0) { 
            for (pos = 0; pos < reply.len; pos += VN_MAX_SIZE) {
                hostRecv(fd, &req, sizeof(req));
                len = (pos + VN_MAX_SIZE) > reply.len ? reply.len - pos : VN_MAX_SIZE;
                hostSend(fd, data+pos, len);
            }
			if (data) {
				free(data);
				data =  NULL;
			}
        }
    }

out:
    return rv;	
}

