
#ifndef PROPERTY_TYPES_H_INCLUDED
#define PROPERTY_TYPES_H_INCLUDED

#include "pftypes.h"



// Property flags that are not exported (see pftypes.h)

#define F_MEMBER   0x01
#define F_ACTION   0x08    // causes an action event


// Forward declaration of camera struct. Never expose to the user.

struct PFDevice;
#define DEVICE struct PFDevice*

#define CAMERA DEVICE


#define INVALID_INDEX 0x0

#define max2(a,b) (a > b ? a : b)

#ifndef WIN32
#define itoa(val, str, base) sprintf(str, "%u", val)
#endif



typedef enum {
	PF_ADDR,                        // value in flat address space
	PF_HANDLER,                     // get/set value by handler
	PF_STATIC                       // static value
} Location;

// Events descriptors are children nodes of a property and are 'fired'
// under certain conditions. When a code 'PROPERTY_EVENT' is returned
// from pfSetProperty(), the property's event nodes must be queried and
// an appropriate action on the event target must be taken

typedef enum {
	PF_ENABLE,                      // Enable property
	PF_DISABLE,                     // Disable property
	PF_UPDATE                       // Update property
} EventType;

// Getter/Setter function pointers
typedef int (*FPGetter)(int port, PFValue *out); 
typedef int (*FPSetter)(int port, PFValue *in); 

typedef unsigned short ADDRESS;  // Address
typedef unsigned int  BITMASK;  // Bitmask
typedef unsigned short BSIZE;    // register or block size

typedef unsigned short PROPINDEX;

#define LAST_BIT 31
#define _1 (BITMASK) (~0)

#define CREATEMASK(from, to)  (( _1 << from ) & ( _1 >> (LAST_BIT - to)))

// Property descriptor
typedef struct _propertydesc {
	char         *name;
	PropertyType  type;
	short         flags;
	Location      where;
	PROPINDEX     *members;          // Member list (static)	
	union {
		// case PF_HANDLER
		struct {
			FPGetter get;
			FPSetter set;
		} func;
		// case PF_ARRAY
		// TODO: size can be a reference to a property
		struct {
			short          banksize; // register bank size
			short          offset;   // register offset;
		} array;

		// case PF_STRUCT
		// case PF_REGISTER
		struct {
			ADDRESS        base;     // Memory or Register address 
			unsigned char  lsb;      // Bitrange LSB
			unsigned char  msb;      // Bitrange MSB
			BSIZE          size;     // register or block size
			long           value;    // default or other cmd value
		} addr;
		// case PF_BUFFER
		unsigned char *buf;          // Buffer pointer     

		// PF_STATIC
		int          s_int;
		float        s_float;
		char        *s_string;

		// PF_EVENT
		struct {
			EventType      s_evttype;  // Event type
			PROPINDEX      target;     // property target
		} event;

	} access;
} PropertyDesc;

typedef struct {
	PropertyDesc *root;
	int major;
	int minor;   
} CameraDesc;

// Internal helper function:
//

PropertyDesc *getProperty_ByToken(TOKEN t);

#endif //PROPERTY_TYPES_H_INCLUDED


