/*
 * api-show.c
 *
 *  Created on: Apr 12, 2010
 *      Author: P.Zumbruch@gsi.de
 */

#include <avr/pgmspace.h>
#include <stdio.h>
#include "api_define.h"
#include "api_global.h"
#include "mem-check.h"
#include "api_show.h"



/* max length defined by MAX_LENGTH_PARAMETER */
static const char commandShowKeyword00[] PROGMEM = "free_mem";
static const char commandShowKeyword01[] PROGMEM = "unused_mem";
static const char commandShowKeyword02[] PROGMEM = "unused_mem_start";

//const showCommand_t showCommands[] PROGMEM =
//{
//		{ (int8_t(*)(struct uartStruct)) showFreeMemNow, commandShowKeyword00 },
//		{ (int8_t(*)(struct uartStruct)) showUnusedMemNow, commandShowKeyword01 },
//		{ (int8_t(*)(struct uartStruct)) showUnusedMemStart, commandShowKeyword02 }
//};

PGM_P commandShowKeywords[] = {
		commandShowKeyword00,
		commandShowKeyword01,
		commandShowKeyword02
};

enum commandShowKeyNumber
{
      commandShowKeyNumber_FREE_MEM_NOW = 0,
      commandShowKeyNumber_UNUSED_MEM_NOW = 1,
      commandShowKeyNumber_UNUSED_MEM_START = 2,
      commandShowKeyNumber_MAXIMUM_NUMBER
};


int8_t show(struct uartStruct *ptr_uartStruct)
{
	uint8_t index;
	//int8_t (*func)(struct uartStruct);
    //struct showCommand_t

	switch(ptr_uartStruct->number_of_arguments)
	{
	case 0:
		for (index = 0; index < commandShowKeyNumber_MAXIMUM_NUMBER; index++)
		{
			ptr_uartStruct->number_of_arguments = 1;
			for (uint8_t i = 0; i < MAX_LENGTH_PARAMETER; setParameter[1][i++]='0');

			snprintf_P(setParameter[1],MAX_LENGTH_PARAMETER -1, (PGM_P) pgm_read_word( &(commandShowKeywords[index])));

			if ( eventDebug <= debug && (debugMask >> debugSHOW) & 0x1)
			{
				snprintf_P(tempstr, BUFFER_SIZE - 1, PSTR("DEBUG (%4i, %s) fcn:show: recursive call of show with parameter %s "),
						__LINE__, __FILE__, &setParameter[1][0]);
				UART0_Send_Message_String(tempstr);
			}
			show(ptr_uartStruct);
			ptr_uartStruct->number_of_arguments = 0;
		}
		break;
	case 1:
		index = 0;
		// find matching show keyword
		while (index < commandShowKeyNumber_MAXIMUM_NUMBER)
		{
			if ( 0 == strncmp_P(&setParameter[1][0],
				               (PGM_P) pgm_read_word( &(commandShowKeywords[index])), MAX_LENGTH_PARAMETER) )
			{
				if ( eventDebug <= debug && (debugMask >> debugSHOW) & 0x1)
				{
					snprintf_P(tempstr, BUFFER_SIZE - 1, PSTR("DEBUG (%4i, %s) fcn:show: keyword %s matches"),
							__LINE__, __FILE__, &setParameter[1][0]);
					UART0_Send_Message_String(tempstr);
				}
				break;
			}
			else
			{
				if ( eventDebug <= debug && (debugMask >> debugSHOW) & 0x1)
				{
					snprintf_P(tempstr, BUFFER_SIZE - 1, PSTR("DEBUG (%4i, %s) fcn:show: keyword %s doesn't match"),
							__LINE__, __FILE__, &setParameter[1][0]);
					UART0_Send_Message_String(tempstr);
				}
			}
		}
		switch (index)
		{
			case commandShowKeyNumber_FREE_MEM_NOW:
				showFreeMemNow(ptr_uartStruct);
				break;
			case commandShowKeyNumber_UNUSED_MEM_NOW:
				showUnusedMemNow(ptr_uartStruct);
			break;
			case commandShowKeyNumber_UNUSED_MEM_START:
				showUnusedMemStart(ptr_uartStruct);
			break;
			default:
				Communication_Error("show:wrong argument",0, ERRA);
			break;
		}
		break;
	default:
		ptr_uartStruct->number_of_arguments = 1;
		show(ptr_uartStruct);
		break;
	}
}


void showReceiveHeader(char *string, struct uartStruct * ptr_uartStruct, uint8_t index)
{
	snprintf_P(tempstr, BUFFER_SIZE - 1, PSTR("RECV "));
	strncat_P(tempstr, (PGM_P) pgm_read_word( commandKeywords[ptr_uartStruct->commandKeywordIndex]), MAX_LENGTH_KEYWORD );
	strncat_P(tempstr, PSTR(" "), 1);
	strncat_P(tempstr, (PGM_P) pgm_read_word( &(commandShowKeywords[index])), MAX_LENGTH_PARAMETER );
}

int8_t showFreeMemNow(struct uartStruct * ptr_uartStruct)
{
	unsigned short usedMemory = get_mem_free();
	showReceiveHeader(tempstr, ptr_uartStruct, commandShowKeyNumber_FREE_MEM_NOW);
    sprintf_P(tempstr,PSTR("%s 0x"),tempstr, usedMemory);
	UART0_Send_Message_String(tempstr);
    return 0;
}

int8_t showUnusedMemNow(struct uartStruct * ptr_uartStruct)
{
	unsigned short unusedMemory = get_mem_unused();
	showReceiveHeader(tempstr, ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_NOW);
    sprintf_P(tempstr,PSTR("%s 0x"),tempstr, unusedMemory);
	UART0_Send_Message_String(tempstr);
    return 0;
}

int8_t showUnusedMemStart(struct uartStruct * ptr_uartStruct)
{
	showReceiveHeader(tempstr, ptr_uartStruct, commandShowKeyNumber_UNUSED_MEM_START);
    sprintf_P(tempstr,PSTR("%s 0x"),tempstr, unusedMemoryStart);
	UART0_Send_Message_String(tempstr);
    return 0;
}
