/*
 *               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.
 *
 *  Description:
 *      This file implements an utility to configure the SC
 *
 */


#include <types.h>
#include <sc/sc.h>
#include <sc/dv_boot.h>
#include <ioslibc.h>
#include <apputils.h>
#include <sc/rtc.h>

/* Maximum command line size including NULL character */
#define LINE_SIZE               256

#define toBCD(i) ((((i)/10) << 4) | ((i) % 10))

int
date(char *args, char *d)
{
    char *tok;
    char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    u32 month;
    RTCDataOrg date;

    if (getNextToken(&tok, &args)) {
        int i;
        for (i = 0; i < 7; ++i) {
            if (!strncmp(tok, days[i], 4)) break;
        }
        if (i == 7) {
            printf("Invalid day of the week: %s\n", tok);
            goto usage;
        }
        date.week = i;

        if (!getNextToken(&tok, &args)) goto usage;

        for (i = 0; i < 12; ++i) {
            if (!strncmp(tok, months[i], 4)) break;
        }
        if (i == 12) {
            printf("Invalid month: %s\n", tok);
            goto usage;
        }
        date.month = toBCD(i+1);
        
        if (!getNextToken(&tok, &args)) goto usage;
        i = asciiToInt(tok);
        if (i > 31) {
            printf("Invalid day: %s\n", tok);
            goto usage;
        }
        date.day = toBCD(i);

        if (!getNextToken(&tok, &args)) goto usage;
        if (strnlen(tok, 9) != 8 || tok[2] != ':' || tok[5] != ':') {
            printf("Invalid time: %s\n", tok);
            goto usage;
        }
        tok[2] = tok[5] = 0;
        i = asciiToInt(tok);
        if (i > 23) {
            printf("Invalid hour: %s\n", tok);
            goto usage;
        }
        date.hour = toBCD(i);
        tok += 3;

        i = asciiToInt(tok);
        if (i > 59) {
            printf("Invalid minutes: %s\n", tok);
            goto usage;
        }
        date.minute = toBCD(i);
        tok += 3;

        i = asciiToInt(tok);
        if (i > 59) {
            printf("Invalid seconds: %s\n", tok);
            goto usage;
        }
        date.second = toBCD(i);

        if (!getNextToken(&tok, &args)) goto usage;
        i = asciiToInt(tok);
        if (i < 2001 || i > 2099) {
            printf("Invalid year: %s\n", tok);
            goto usage;
        }
        date.year = toBCD(i-2000);
        RTCWriteDate(&date);
        memset(&date, 0, sizeof(date));
    }
    RTCReadDate(&date);
    month = (date.month >> 4)*10 + (date.month & 0xf) - 1;
    printf("%s %s %02x %02x:%02x:%02x %x\n", days[date.week], months[month], date.day, date.hour, date.minute, date.second, date.year + 0x2000);
    goto out;

usage:
    printf("Usage: date [Day Mon DD HH:MM:SS YYYY]\n");
out:
    return 0;
}

int
alarm(char *args, char *d)
{
    char *tok;
    int i;
    RTCDataOrg date;

    if (getNextToken(&tok, &args)) {
        if (strnlen(tok, 6) != 5 || tok[2] != ':') {
            printf("Invalid time: %s\n", tok);
            goto usage;
        }
        tok[2] = 0;
        i = asciiToInt(tok);
        if (i > 23) {
            printf("Invalid hour: %s\n", tok);
            goto usage;
        }
        date.alarm_hour = toBCD(i);
        tok += 3;

        i = asciiToInt(tok);
        if (i > 59) {
            printf("Invalid minutes: %s\n", tok);
            goto usage;
        }
        date.alarm_minute = toBCD(i);
        RTCWriteAlarm(&date);
    }
    RTCReadAlarm(&date);
    printf("Alarm: %02x:%02x\n", date.alarm_hour, date.alarm_minute);
    goto out;

usage:
    printf("Usage: alarm [HH:MM]\n");
out:
    return 0;
}

int
stat(char *args, char *d)
{
    RTCDataOrg date;
    char *tok;

    if (getNextToken(&tok, &args)) {
        RTCReadStat(&date);
        date.stat |= 0x4;
        RTCWriteStat(&date);
    }
    RTCReadStat(&date);
    printf("Stat: %02x\n", date.stat);
    return 0;
}

int
quit(char *args, char *d)
{
    printf("Quitting...\n");
    IO_WRITE(SYS_CTRL_REG, SYS_CTRL_SOFT_RST);
    return 0;
}

/* Command blocks */
static Command commands[] = {
    {"alarm", alarm,
        "alarm                - Get/set alarm", 0},
    {"date", date,
        "date                 - Get/set time and date", 0},
    {"stat", stat,
        "stat                 - Read stat register", 0},
    {"quit", quit,
        "quit                 - Return to minimon", 0},
    {"help", help,
        "help                 - Print this message", commands},
    {0}
};


int
cpu0main(void)
{
    int         rc = 0;
    char        line[LINE_SIZE];

    printf("RTC Monitor\n");
    printf("built: " BUILD_TIME "\n\n");

    RTCPowerOnCheck();

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

    return (rc);
}


int cpu1main(void)
{
    int         rc = 0;

    return (rc);
}
