/*
 Copyright 2007, Freie Universitaet Berlin. All rights reserved.

 These sources were developed at the Freie Universität Berlin, Computer
 Systems and Telematics group.

 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.

 - Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
 contributors may be used to endorse or promote products derived from
 this software without specific prior written permission.

 This software is provided by FUB and the contributors on an "as is"
 basis, without any representations or warranties of any kind, express
 or implied including, but not limited to, representations or
 warranties of non-infringement, merchantability or fitness for a
 particular purpose. In no event shall FUB or contributors 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.

 This implementation was developed by the CST group at the FUB.

 For documentation and questions please use the web site
 http://scatterweb.mi.fu-berlin.de and the mailinglist
 scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
 Berlin, 2007
 */

/**
 * @file		ScatterWeb.Cmd.Properties.c
 * @brief		Properties
 * @ingroup		cmd
 * @version		$Revision$
 * @since		1.2
 *
 * $Id: ScatterWeb.Cmd.c 75 2007-09-19 12:24:48Z baar@PCPOOL.MI.FU-BERLIN.DE $
 */

#include "ScatterWeb.Cmd.Properties.h"
#include "string.h"

static const uint8_t* typeToString[] = { "uint8_t", "int8_t", "uint16_t", "int16_t", "uint32_t", "int32_t", "float" };

void Property_printValue(type t, void* v) {
	switch(t) {
		case type_uint8_t:
			printf("%u", *((uint8_t*) v));
			break;
		case type_uint16_t:
			printf("%u", *((uint16_t*) v));
			break;
		case type_int8_t:
			printf("%i", *((int8_t*) v));
			break;
		case type_int16_t:
			printf("%i", *((int16_t*) v));
			break;
		case type_uint32_t:
			printf("%lu", *((uint32_t*) v));
			break;
		case type_int32_t:
			printf("%li", *((int32_t*) v));
			break;
		case type_float: {
			double d =  *(float*) v;
			printf("%f",d);
			break; }
		default:
			printf("unknown type");
	}
}

bool Property_parseArgument(type t, const cmd_args_t* p, void* v) {
	int32_t sd = 0;
	uint32_t ud = 0;

	switch(t) {
		case type_uint8_t:
			ud = strtoul (p->args, 0, 0);
			*((uint8_t*) v) = (uint8_t) ud;
			break;
		case type_uint16_t:
			ud = strtoul (p->args, 0, 0);
			*((uint16_t*) v) = (uint16_t) ud;
			break;
		case type_uint32_t:
			ud = strtoul (p->args, 0, 0);
			*((uint32_t*) v) = (uint32_t) ud;
			break;
		case type_int8_t:
			sd = strtol (p->args, 0, 0);
			*((int8_t*) v) = (int8_t) sd;
			break;
		case type_int16_t:
			sd = strtol (p->args, 0, 0);
			*((int16_t*) v) = (int16_t) sd;
			break;
		case type_int32_t:
			sd = strtol (p->args, 0, 0);
			*((int32_t*) v) = (int32_t) sd;
			break;
		case type_float:
			*((float*) v) = 0.0;
			break;
		default:
			LOG_LOW("*** unknown property type ***");
			return false;
	}
	return true;
}

void Property_printDetailed(const uint8_t* name, const property_t* p) {
	printf("%s %s = ", typeToString[p->type], name);
	Property_printValue(p->type, p->value);
	printf(" < [");
	Property_printValue(p->type, p->min);
	printf(", ");
	Property_printValue(p->type, p->max);
	printf("]\r\n");
}

bool Property_checkInterval(const property_t* p, void* v) {
	switch(p->type) {
		case type_uint8_t:
			if(*((uint8_t*) v) < *((uint8_t*) p->min)
			|| *((uint8_t*) v) > *((uint8_t*) p->max))
				return false;
			break;
		case type_uint16_t:
			if(*((uint16_t*) v) < *((uint16_t*) p->min)
			|| *((uint16_t*) v) > *((uint16_t*) p->max))
				return false;
			break;
		case type_int8_t:
			if(*((int8_t*) v) < *((int8_t*) p->min)
			|| *((int8_t*) v) > *((int8_t*) p->max))
				return false;
			break;
		case type_int16_t:
			if(*((int16_t*) v) < *((int16_t*) p->min)
			|| *((int16_t*) v) > *((int16_t*) p->max))
				return false;
			break;
		case type_uint32_t:
			if(*((uint32_t*) v) < *((uint32_t*) p->min)
			|| *((uint32_t*) v) > *((uint32_t*) p->max))
				return false;
			break;
		case type_int32_t:
			if(*((int32_t*) v) < *((int32_t*) p->min)
			|| *((int32_t*) v) > *((int32_t*) p->max))
				return false;
			break;
		case type_float:
			if(*((float*) v) < *((float*) p->min)
			|| *((float*) v) > *((float*) p->max))
				return false;
			break;
		default:
			LOG_LOW("*** unknown property type ***");
			return false;
	}
	return true;
}

bool Property_set(const uint8_t* name, const property_t* p, void* v) { \
	if(!Property_checkInterval(p, v)) {
		LOG_LOW("*** Property violation: %s ***", name);
		return false;
	}
	switch(p->type) {
		case type_uint8_t:
			*((uint8_t*) p->value) = *((uint8_t*) v);
			break;
		case type_uint16_t:
			*((uint16_t*) p->value) = *((uint16_t*) v);
			break;
		case type_int8_t:
			*((int8_t*) p->value) = *((int8_t*) v);
			break;
		case type_int16_t:
			*((int16_t*) p->value) = *((int16_t*) v);
			break;
		case type_uint32_t:
			*((uint32_t*) p->value) = *((uint32_t*) v);
			break;
		case type_int32_t:
			*((int32_t*) p->value) = *((int32_t*) v);
			break;
		case type_float:
			*((float*) p->value) = *((float*) v);
			break;
	}

 	if(p->changed)
 		(p->changed)();
	return true;
}

void Property_printAll() {
	extern uint16_t __term_start;
	extern uint16_t __term_end;
	command_t* p = (command_t*) &__term_start;
	const command_t* end = (command_t*) &__term_end;
	printf("Properties: {\r\n");
	while(p < end) {
		if(p->flags & CMDFLAG_PROPERTY)
			Property_printDetailed(p->id.ascName, (property_t*) p->ptr);
		p++;
	}
	String_writeChar(putchar, '#', 23);
	printf("}\r\n");
}

const property_t* Property_get(uint8_t* name) {
	extern uint16_t __term_start;
	extern uint16_t __term_end;
	command_t* p = (command_t*) &__term_start;
	const command_t* end = (command_t*) &__term_end;
	while(p < end) {
		if(!strcmp(p->id.ascName, name) && p->ptr)
			return (property_t*) p->ptr;
	}
	return 0;
}

void Property_print(const uint8_t* name, const property_t* p) {
	printf("%s = ", name);
	Property_printValue(p->type, p->value);
	printf("\r\n");
}

/**
 * @brief Print properties
 * @ingroup commands
 *
 * Prints all properties, their current values, valid intervals and data type.
 */
COMMAND(properties,  CMDFLAG_SERIAL, cmdargs) {
	Property_printAll();
}
