
/*
 *               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.
 * 
 *  "$Id: esmon.c,v 1.4 2008/06/25 18:13:25 cnguyen Exp $";
 *
 */


#include <ios.h>
#include <iostypes.h>
#include <ioslibc.h>
#include <apputils.h>
#include <es.h>
#include <es_int.h>


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

extern u8           *__esBuf;


extern int  statfs(char *args, char *d);
extern int  cd(char *args, char *d);
extern int  ls(char *args, char *d);
extern int  cp(char *args, char *d);
extern int  mv(char *args, char *d);
extern int  mkdir(char *args, char *d);
extern int  rm(char *args, char *d);
extern int  stat(char *args, char *d);
extern int  attr(char *args, char *d);
extern int  format(char *args, char *d);

extern int  titleImport(char *args, char *d);
extern int  titleListAll(char *args, char *d);
extern int  titleDelete(char *args, char *d);
extern int  titleView(char *args, char *d);

extern int  isfsCmdInit();


static Command commands[] = {
    {"help",    help,
        "help                   - Print this message", commands},
    {"source", source, 
        "source <file>          - Execute commands from file", commands},
    {"statfs",  statfs,
        "statfs                 - Get file system statistics", 0},
    {"cd",  cd,
        "cd                     - Change to a directory", 0},
    {"ls",  ls,
        "ls                     - List the names of files and directories", 0},
    {"cp",  cp,
        "cp src_file dst_file   - Copy a file", 0},
    {"mv",  mv,
        "mv src_file dst_file   - Move a file", 0},
    {"mkdir",  mkdir,
        "mkdir dirname          - Create a new directory", 0},
    {"rm", rm,
        "rm name                - Remove a file or directory", 0},
    {"stat", stat,
        "stat name              - Get file stats", 0},
    {"attr", attr,
        "attr name              - Get file/dir attributes", 0},
    {"format",  format,
        "format                 - Re-format the NAND flash (requires restart)", 0},
    {"import",  titleImport,
        "import title_name      - Import a title (bwf format) from PC", 0},
    {"list",  titleListAll,
        "list                   - List all titles on NAND", 0},
    {"view",  titleView,
        "view title_index       - View a title in details", 0}, 
    {"del",  titleDelete,
        "del title_index        - Delete a title of given index", 0},

    {0},
};


#define LINE_SIZE 256

IOSError esmCmdInit()
{
    IOSError rc = IOS_ERROR_OK;
    u32 pid, id, num;


    pid = IOS_GetProcessId();
    printf("Current PID = %d\n", pid);
    rc = IOS_SetUid(pid, IOS_ROOT_ID);
    if (rc != IOS_ERROR_OK) {
        printf("Failed to set uid (pid=%d), rc=%d\n", pid, rc);
    }
    else
        printf("Set UID of PID (%d) to %d\n", pid, IOS_ROOT_ID);
    rc = IOS_SetGid(pid, IOS_ROOT_GROUP);
    if (rc != IOS_ERROR_OK) {
        printf("Failed to set gid (pid=%d), rc=%d\n", pid, rc);
    }
    else
        printf("Set GID of PID (%d) to %d\n", pid, IOS_ROOT_GROUP);

    /*
     * Allocate a shared buffer for ES
     */
    __esBuf = IOS_Alloc(0, ES_BUF_SIZE);
    if (__esBuf == NULL) {
        printf("Failed to allocate __esBuf\n");
        goto out;
    }

    rc = ES_InitLib();
    if (rc != ES_ERR_OK) {
        printf("Failed to init ES library, rc=%d\n", rc);
        goto out;
    }

    /*
     * Check for the default ID's and fail
     */
    rc = ES_GetDeviceId(&id);
    if (rc != ES_ERR_OK) {
        printf("Failed to read device ID, rc=%d\n", rc);
        goto out;
    }
    printf("Device ID: 0x%08x\n", id);

    rc = ES_ListTitlesOnCard(NULL, &num);
    if (rc != ES_ERR_OK) {
        printf("Failed to get titles, rc=%d\n", rc);
        goto out;
    }
    printf("Number of titles on NAND: %d\n\n", num);

    rc = isfsCmdInit();

out:
    return rc;

}


void
main(void)
{
    char line[LINE_SIZE];

    IOSError rv = IOS_ERROR_OK;

    printf("\nesmon [");
    printf("built: " BUILD_TIME "]\n\n");

    rv = esmCmdInit();
    if (rv != IOS_ERROR_OK) {
        printf("Failed to initialize ES, rv=%d\n", rv);
        return;
    }

    while (1) {
        printf("[ESMon] Enter command: ");
        gets(line);
        if (parseCommand(line, commands) < 0) {
            help(line, (void *)commands);
        }
    }
}


