/*
 *              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 <stdafx.h>
#include "dwusb.h"
#include <sys/stat.h>

#undef SYS_OPEN
#include "sc/semihost.h"
#include "sc/semihost_usb.h"

extern char cmdBuf[16384];
extern char usbBuf[1024];

int semihost()
{
    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(&req, sizeof(req)) < 0) {
            dbgPrintf("<Download PC> Error: recv semihost command error");
            goto out;
        }

         if (req.magic != USB_SEMI_REQ_MAGIC) {
            sprintf(usbBuf, "<Download PC> Error: semihost command magic number is 0x%x", req.magic);
			dbgPrintf(usbBuf);
            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(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(data+pos, len);
                    }
                    fwrite(data, 1, req.len, (FILE *) req.pcFd);
                    free(data);
                    data = NULL;
                } else {
                    sprintf(usbBuf, "<Download PC> Cannot alloc buffer to hold %d NC data", req.len);
					dbgPrintf(usbBuf);
				}
            } 
            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:
            dbgPrintf("<Download PC> Unsupported semihost command");
            break;
        }

        if (req.req == USB_SEMIHOST_END) break;
        if (req.req == USB_SEMIHOST_START) continue;

        hostSend(&reply, sizeof(reply));

        if (reply.len > 0) { 
            for (pos = 0; pos < reply.len; pos += VN_MAX_SIZE) {
                hostRecv(&req, sizeof(req));
                len = (pos + VN_MAX_SIZE) > reply.len ? reply.len - pos : VN_MAX_SIZE;
                hostSend(data+pos, len);
            }
			if (data) {
				free(data);
				data =  NULL;
			}
        }
    }

out:
    return rv;	
}

