/*
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.h
 * @brief		Properties
 * @ingroup		cmd
 * @version		$Revision$
 * @since		1.2
 *

<p>The PROPERTY MACRO builds upon the ScatterWeb command engine to provide
a simple way to access global variables at runtime.</p>

<p>Declare a property with the mentioned MACRO to create a
<ul>
<li>a global variable of the given type and name</li>
<li>a terminal command of the same name</li>
	<ul>
		<li>to read the current  value</li>
		<li>to set a new value</li>
	</ul>
</ul>
</p>

<p>The  advantage of using this method are
<ul>
<li>no need to check if a terminal command parameter exists and to parse it</li>
<li>automated value check - must be in given interval</li>
<li>if a function pointer is given, this function gets called if the value of the property variable changes</li>
<li>listing of all properties including their current values, types and intervals at runtime via command "properties"</li>
</ul>
</p>

<ul>
<li>\ref cmd_interior or "How-To write command handlers"</li>
<li>\ref cmd_exterior or "Interfacing the nodes"</li>
<li><a href="group__commands.html">Command Documentation</a></li>
<li><a href="group__cmd.html">Command Engine</a></li>
</ul>
 *
 * $Id: ScatterWeb.Cmd.h 75 2007-09-19 12:24:48Z baar@PCPOOL.MI.FU-BERLIN.DE $
 */

#ifndef ScatterWeb_Properties
#define ScatterWeb_Properties

#include <ScatterWeb.System.h>

typedef struct _property {
	uint8_t type;
	void* value;
	void* min;
	void* max;
	fp_t changed;
} property_t;

typedef enum { type_uint8_t, type_int8_t, type_uint16_t, type_int16_t, type_uint32_t, type_int32_t, type_float } type;

/**
* @brief	Use this MACRO to declare a property
*
* The MACRO declares a terminal function and various additional members to read and write a property.
* PROPERTY([type], [name], [minValue], [maxValue], [initialValue], [callThisFctOnChange])
*
* @since         1.2
*/
#define PROPERTY(_type, _name, _min, _max, _initial, _changed)  \
	static _type _name = _initial; \
	static _type max_##_name = _max; \
	static _type min_##_name = _min; \
	const property_t property_##_name = { (uint8_t) type_##_type, (uint8_t*) &_name, &min_##_name, &max_##_name, (fp_t) _changed }; \
	_type get_##_name() { return _name; } \
	COMMAND_HEADER(_name, CMDFLAG_PROPERTY, cmdargs,  &property_##_name) { \
		_type x; \
		if(cmdargs->arg_size) { \
			bool b = Property_parseArgument(type_##_type, cmdargs, &x); \
			if(b) \
				Property_set((uint8_t*) &cmd_name_##_name, &property_##_name, &x); \
		} \
		Property_print((uint8_t*) &cmd_name_##_name, &property_##_name); \
	}

bool Property_parseArgument(type t, const cmd_args_t* p, void* v);

bool Property_checkInterval(const property_t* p, void* v);

bool Property_set(const uint8_t* name, const property_t* p, void* v);

void Property_print(const uint8_t* name, const property_t* p);

void Property_printAll();

void Property_printDetailed(const uint8_t* name, const property_t* p);

void Property_printValue(type t, void* v);

const property_t* Property_get(uint8_t* name);

#endif
