/*
 * Copyright (c) 2008 Jiri Svoboda
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup debug
 * @{
 */
/** @file
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bool.h>
#include <udebug.h>
#include <sys/types.h>

#include "main.h"
#include "cons.h"
#include "include/arch.h"
#include "cmd.h"

static void cmd_break(int argc, char *argv[]);
static void cmd_ct(int argc, char *argv[]);
static void cmd_go(int argc, char *argv[]);
void	    cmd_help(int argc, char *argv[]);
static void cmd_pwt(int argc, char *argv[]);
static void cmd_read(int argc, char *argv[]);
static void cmd_step(int argc, char *argv[]);
static void cmd_threads(int argc, char *argv[]);
static void cmd_quit(int argc, char *argv[]);

volatile bool quit = false;

cmd_desc_t cmd_table[] = {
	{ 1,	"break",	cmd_break },
	{ 1,	"ct",		cmd_ct },
	{ 0,	"go",		cmd_go },
	{ 0,	"help",		cmd_help },
	{ 0,	"pwt",		cmd_pwt },
	{ 2,	"read",		cmd_read },
	{ 0,	"threads",	cmd_threads },
	{ 0,	"quit",		cmd_quit },
	{ -1,	NULL,		NULL }
};

static void cmd_break(int argc, char *argv[])
{
	uintptr_t addr;

	(void)argc;
	addr = strtoul(argv[1], NULL, 0);

	cons_printf("You requested a breakpoint at 0x%x\n", addr);
	arch_breakpoint_add(addr);
}

static void cmd_ct(int argc, char *argv[])
{
	int i;
	int tid;

	(void)argc;
	tid = strtoul(argv[1], NULL, 0);

	for (i = 0; i < n_threads; ++i) {
		if (thread_id[i] == tid) break;
	}
	
	if (thread_id[i] == tid) {
		cons_printf("changed working thread to: %d [hash 0x%x]\n",
		    thread_id[cwt], thread_hash[cwt]);
	} else {
		cons_printf("no such thread\n");
	}
}


void cmd_go(int argc, char *argv[])
{
	fcv_broadcast(&go_cv);
}

void cmd_help(int argc, char *argv[])
{
	int i;

	(void)argc; (void)argv;
	i = 0;
	while (cmd_table[i].name != NULL) {
		cons_printf("%s\n", cmd_table[i].name);
		++i;
	}
}

#define BYTES_PER_LINE 16

static void cmd_read(int argc, char *argv[])
{
	uintptr_t addr;
	size_t length;
	uint8_t buf[BYTES_PER_LINE];
	int to_read, i;
	int rc;

	(void)argc;
	addr = strtoul(argv[1], NULL, 0);
	length = strtoul(argv[2], NULL, 0);

	while (length > 0) {
		to_read = length < BYTES_PER_LINE ? length : BYTES_PER_LINE;

		rc = udebug_mem_read(app_phone, buf, addr, to_read);
		if (rc < 0) {
			cons_printf("error %d\n", rc);
			return;
		}

		cons_printf("0x%x:", addr);
		for (i = 0; i < to_read; ++i) {
			cons_printf(" %02x", buf[i]);
		}
		for (i = to_read; i < BYTES_PER_LINE; ++i) {
			cons_printf("   ");
		}

		putchar ('\t');

		for (i = 0; i < to_read; ++i) {
			if (buf[i] >= 32 && buf[i] < 127)
				putchar(buf[i]);
			else
				putchar('.');
		}
		cons_printf("\n");

		addr += to_read;
		length -= to_read;
	}
}

void cmd_step(int argc, char *argv[])
{
	fcv_broadcast(&go_cv);
}

void cmd_pwt(int argc, char *argv[])
{
	(void)argc; (void)argv;
	cons_printf("working thread: %d [hash 0x%x]\n", thread_id[cwt],
		thread_hash[cwt]);
}

void cmd_threads(int argc, char *argv[])
{
	int i;

	(void)argc; (void)argv;
	for (i = 0; i < n_threads; ++i) {
		cons_printf("%d [hash 0x%x]\n", thread_id[i], thread_hash[i]);
	}
}


static void cmd_quit(int argc, char *argv[])
{
	(void)argc; (void)argv;
	quit = true;
}

/** @}
 */
