/* iosim 
 *  cmd function file 
 * :set tabstop=4
 * :set expandtab
 * :set shiftwidth=4
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "utils.h"
#include "cmd.h"
#include "req_cmd.h"
#include "ipc.h"

char *
dvSkipBlank(char *str)
{
    char *tmp = str;

    while (tmp[0] == ' ' || tmp[0] == '\t') {
        tmp++;
    }

    return tmp;
}

char *
dvSkipNonBlank(char *str)
{
    char *tmp = str;

    while (tmp[0] != ' ' && tmp[0] != '\t' && tmp[0]!='\0') {
         tmp++;
    }

    return tmp;
}

unsigned int 
dvGetCmdArgNum(char *str)
{
    char *tmp = dvSkipBlank(str);
    
    if (tmp[0] == '0' && ((tmp[1] == 'x') || (tmp[1] == 'X'))) { 
        return strtoul(tmp, NULL, 16);
    }
    
    return strtoul(tmp, NULL, 10);
}

char *
dvGetCmdArgStr(char *str)
{
    return dvSkipBlank(str);
}

int
dvGetCmdArgConnect(char *str_in)
{
    char *str;

    str = dvSkipBlank(str_in);
    if (strncasecmp(str, "cpu0", 4) == 0) {
        return IPC_SOCK_CPU0;
    } else if (strncasecmp(str, "cpu1", 4) == 0) {
        return IPC_SOCK_CPU1;
    } else if (strncasecmp(str, "gba", 3) == 0) {
        return IPC_SOCK_GBA;
    }

    printf("ERROR: only cpu0 cpu1 and GBA are valid sockets\n");
    
    return 32;    
}

int 
dvCmdTimeAdv(int wh, char *param, int commit, int verbose)
{
    char *s, *sn;
    int type, n;

    if (wh < 0) {
        printf("WARNING: use s/o cmd to open active socket\n"); 
        return 0;
    }

    s = dvSkipBlank(param);
    if (strncasecmp(s, "cpu0", 4) == 0) {
         type=IPC_CMD_TM_ADV_CPU0_NEG;
    } else if (strncasecmp(s, "cpu1", 4) == 0) {
        type=IPC_CMD_TM_ADV_CPU1_NEG;
    } else if (strncasecmp(s, "core", 4) == 0) {
        type=IPC_CMD_TM_ADV_CORE_POS;
    } else if (strncasecmp(s, "gba", 3) == 0) {
        type=IPC_CMD_TM_ADV_GBA_POS;
    } else if (strncasecmp(s, "ns", 2) == 0) {
        type=IPC_CMD_TM_ADV_NS;
    } else{ 
        printf("ERROR: Invalid time advance type\n");
        return 0;
    }
    
    sn = dvSkipNonBlank(s);
    n = dvGetCmdArgNum(sn);

    if (n <= 0) {
        printf("ERROR: invalid time advance %d\n", n);
        return 0;
    }

    return DV_TimeAdvance(wh, type, n, commit, verbose);
} 

int 
dvPrintXzData(char *str)
{
    int i, j;
    unsigned int data = 0;

    if (str == NULL) {
        return -1;
    }
    
    if (strstr(str, "x") || strstr(str, "z")) {
        printf("Binary:");
        for (i=0; i<32; i+=4) { 
            printf(" ");
            for (j=i; j<(i+4); j++) { 
                printf("%c", str[j]);
            }
        }
    } else {
        for (i=0; i<32; i++) {
            data = (data << 1) | ((str[i] - '0') & 1);
        }
        printf("Hex: 0x%8.8x", data);
    }

    printf("\n");

    return 0;
}

int 
dvHandleRecvData(int which, int verbose)
{
    ipc_pkth pkth;
    unsigned int *pdata=NULL;
    int sz;

    if (which >= IPC_SOCK_NUM) {
        printf("ERROR: recv data @ %d invalid value for which\n", which);
        return -1;
    }

    if (verbose) 
        printf("INFO: %s reply data \n", DV_IpcSocketName(which));

    memset(&pkth, 0, sizeof(ipc_pkth));
    DV_IpcRecvPktHd(which, &pkth);

    if (pkth.magic != IPC_PKT_MAGIC) {
        printf("ERROR: invalid packet reply\n");
        return -1;
    }
    printf("Recv packet id=0x%x cmd=0x%x length=%d \n", 
        pkth.cmd_id, pkth.cmd, pkth.length);

    sz = pkth.length - sizeof(ipc_pkth);
    if (sz > 0) {
        pdata = (unsigned int *) malloc(sz);
        if (pdata) {
            DV_IpcRecvPktData(which, (unsigned char *) pdata, sz);
        } else printf("ERROR: memory alloc error\n");
    }

    if (pdata) free(pdata);
    return 0;
}
