/*
 *              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 <sc/sc.h>
#include <sc/ios.h>
#include <ioslibc.h>
#include <fs.h>
#include <stdarg.h>
#include <sc/usb.h>
#include <sc/shell.h>
#include <flash.h>
#include <sc/rtc.h>
#include "cmd.h"
#include "shell.h"
#include "diag.h"
#include "diag/gbaelf.h"

IOSError
shellDiagAud(IOSFd fd)
{   
    IOSError rv = IOS_ERROR_OK;    
    IOSFd afd;
 
    /* Wait for PC command to start play */
    rv = reliableRead(fd, usbBuf0, 8);
    if (usbBuf0[0] != 0)
        return usbBuf0[0]; 

    audTone();
    IOS_Write(fd, usbBuf0, 8); 

    rv = reliableRead(fd, usbBuf0, 8);
    if (usbBuf0[0] != 0)
        return usbBuf0[0];

    audBuf = NULL;
    afd = audRecordCfg();
    if (afd < 0) {
        rv = afd;
        goto out;
    } else {
        rv = recordBuf(afd, (u8 *) audBuf, audBufSize);
        printf("Record buf 0x%x size=%d \n", audBuf, audBufSize);
    }

    if (rv < 0) {
        printf("Record buf return %d \n", rv);
        goto out;
    }
    *usbBuf0 = 0;
    printf("Record buf=0x%x sz = %d \n", audBuf, audBufSize);
    printf("Record end \n");
    IOS_Write(fd, usbBuf0, 8); 

    rv = reliableRead(fd, usbBuf0, 8);
    if (usbBuf0[0] != 0) {
        rv = usbBuf0[0];
        goto out;
    }
   
    printf("Start play back ...\n"); 
    rv = playbackBuf(afd, (u8 *) audBuf, audBufSize);
    *usbBuf0 = 0;
    IOS_Write(fd, usbBuf0, 8); 
    printf("Play back done...\n"); 

out:
    if (audBuf) IOS_Free(0, audBuf);
    if (afd >= 0) IOS_Close(afd);
    return rv;
}

IOSError
shellDiagRtc(IOSFd fd)
{   
    IOSMessage m;
    IOSTimerId t;
    IOSError rv;    
    IOSDate *rtc;
    u8 sec;

    rtc = (IOSDate *) usbBuf0;

    rv = IOS_GetDate(rtc);
    if (rv < 0) {
        printf("Get data failed \n");
        return rv;
    } 
    printf("%02d/%02d/%02d %02d:%02d:%02d\n", rtc->year, rtc->month, rtc->day, rtc->hour, rtc->minute, rtc->second);
    sec = (rtc->second + 5) % 60;
    t = IOS_CreateTimer(2600000, 0, usbMq, 0);
    if ((rv == IOS_ReceiveMessage(usbMq, &m, IOS_MESSAGE_BLOCK)) < 0)
        return rv;
    IOS_DestroyTimer(t);

    printf("GetDate: ");
    rv = IOS_GetDate(rtc);
    if (rv < 0) {
        printf("Get data failed \n");
        return rv;
    } 
    printf("%02d/%02d/%02d %02d:%02d:%02d\n", rtc->year, rtc->month, rtc->day, rtc->hour, rtc->minute, rtc->second);

    
    if ((rtc->second != sec) && (rtc->second != (sec+1))) {
        printf("RTC timer is not accurate\n");
        return IOS_ERROR_INVALID;
    }

    return IOS_ERROR_OK;
}

#define DIAG_TMP_FILE   "/diag.tmp"
IOSError
shellDiagFlash (IOSFd fd)
{   
    IOSError rv;
    IOSFd  fsFd=-1;
    int i;

    printf("Flash Test start \n");
    ISFS_Delete(DIAG_TMP_FILE);    
    rv = ISFS_CreateFile(DIAG_TMP_FILE, 0, ISFS_RW_ACCESS, 
                         ISFS_RW_ACCESS, ISFS_RW_ACCESS);
    if (rv < 0)  {
        printf("Cannot create file\n");
        goto out;
    }
        
    rv = fsFd = ISFS_Open(DIAG_TMP_FILE, ISFS_WRITE_ACCESS);
    if (fsFd < 0) {
        printf("cannot open file for write\n");
        goto out;
    }

    rv = ISFS_Write(fsFd, usbBuf0, USB_BUF_SIZE );
    if (rv < 0) {
        printf("File write failed \n");
        goto out;
    }
    
    ISFS_Close(fsFd);
    rv = fsFd = ISFS_Open(DIAG_TMP_FILE, ISFS_READ_ACCESS);
    if (fsFd < 0) {
        printf("cannot open file for read\n");
        goto out;
    }

    rv = ISFS_Read(fsFd, usbBuf1, USB_BUF_SIZE);
    if (rv < 0) {
        printf("File read failed \n");
        goto out;
    }
    rv = IOS_ERROR_OK;
   
    for (i=0; i<USB_BUF_SIZE; i++) {
        if (usbBuf0[i] != usbBuf1[i]) {
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }

out:
    if (fsFd>=0) ISFS_Close(fsFd);
    ISFS_Delete(DIAG_TMP_FILE);
    return rv;
}

IOSError
shellDiagGba (IOSFd fd)
{   
    IOSError rv;
    IOSFd  fsFd=-1;
    int i, len, wlen;

    printf("Gba Test start \n");
    ISFS_Delete(DIAG_TMP_FILE);    
    rv = ISFS_CreateFile(DIAG_TMP_FILE, 0, ISFS_RW_ACCESS, 
                         ISFS_RW_ACCESS, ISFS_RW_ACCESS);
    if (rv < 0)  {
        printf("Cannot create file for gba elf\n");
        goto out;
    }
        
    rv = fsFd = ISFS_Open(DIAG_TMP_FILE, ISFS_WRITE_ACCESS);
    if (fsFd < 0) {
        printf("cannot open file for write\n");
        goto out;
    }

    len = sizeof(gbaElf);
    printf("Write file size = %d \n", len);
    for (i=0; i<len; i+=USB_BUF_SIZE) {
        wlen = (i+USB_BUF_SIZE) > len ? len - i : USB_BUF_SIZE;
        memcpy(usbBuf0, gbaElf+i, wlen);
        rv = ISFS_Write(fsFd, usbBuf0, wlen);
    
        if (rv < 0) {
            printf("File write failed \n");
            ISFS_Close(fsFd);
            goto out;
        }
    }
    
    rv = IOS_ERROR_OK;
    ISFS_Close(fsFd);
    rv = IOS_LaunchElf(DIAG_TMP_FILE);                                                                  
    printf("launch returned %d\n", rv);

out:
    ISFS_Delete(DIAG_TMP_FILE);
    return rv;
}

IOSError 
shellDiag(IOSFd fd, char type, int param)
{
    IOSError rv = IOS_ERROR_OK;

    switch (type) {
    case SHELL_DIAG_AUD:
        rv = shellDiagAud(fd); 
        break;
    case SHELL_DIAG_FLASH:
        rv = shellDiagFlash(fd); 
        break;
    case SHELL_DIAG_RTC:
        rv = shellDiagRtc(fd);
        break;
    case SHELL_DIAG_USB:
        break;
    case SHELL_DIAG_GBA:
        shellDiagGba(fd);
        break;
    default:
        printf("Unsupport Diag \n");
        rv = IOS_ERROR_INVALID;
        break;
    }

    return rv;
}


