/*  Copyright (C) MOXA Inc. All rights reserved.

    This software is distributed under the terms of the
    MOXA License.  See the file COPYING-MOXA for details.
*/


typedef unsigned char UINT8;

#define GSENSOR_NUMBER	2


typedef struct _GSENSORDATA
{
	UINT16 x_axis;	//x-axis value
	UINT16 y_axis;	//y-axis value
	UINT16 z_axis;	//z-axis value
	
}GSENSORDATA, *PGSENSORDATA;

typedef struct _SMBUSSETTING {
	UINT8	device_id;			//g-sensor status(0 : normal, -1:fail)
	UINT8	address;		//g-sensor address
	UINT	bw_rate;		//baudrate
	UINT	power_control;	//power control
	UINT	data_format;	//data resolution (full resolution/ +-16g)
} SMBUSSETTING, *PSMBUSSETTING;


typedef struct _SHAREDMEMDATA
{
	GSENSORDATA		gsensor_data[2];
	SMBUSSETTING	smbus_setting[2];

}SHAREDMEMDATA, *PSHAREDMEMDATA;

#ifdef __cplusplus    // If used by C++ code, 
extern "C" {          // we need to export the C interface
#endif

/* DLL share memory funcitons*/
VOID GetSharedMem(PSHAREDMEMDATA lpstatus); //extern "C" VOID __cdecl GetSharedMem(PSTATUS lpstatus, DWORD cchSize) ;
VOID SetSharedMem(SHAREDMEMDATA status);    //extern "C" VOID __cdecl SetSharedMem(STATUS status) ;

/*
	Initialize Uart
	
	param:
		port  :	 port number	
				
	Return value:
		unsigned int :			return UART handle
*/
HANDLE mxsp_open();

/*
	Get UART Mode
	
	param:
		(in)int index :	Port index (0,1)		
				
	Return value:
		int:		0 : RS232 Mode
					1 : RS485-2W Mode
					2 : RS422 Mode
					3 : RS485-4W Mode
*/
int mxsp_get_interface(HANDLE fd, int port_index);

/*
	Set UART Mode
	
	param:
		(in)int index  :	Port index		
		(in)int mode   :	Port mode		
				
	Return value:
		int:			Current Mode. 
*/
int mxsp_set_interface(HANDLE fd, int port_index, int port_interface);

/*
	close UART	
	param:
		(in)unsigned int fd  :	Handle of the open port		
				
	Return value:
		none
*/
void mxsp_close();


/*
	Initialize Digitial I/O
	
	param:
		None  :		
				
	Return value:
		HANDLE:			return digital I/O handle
*/
HANDLE mxdgio_open();

/*
	get digital input
	
	param:
		(in)HANDLE hDIO  :	Handle of digital I/O		
				
	Return value:
		int:			return digital input data 
*/
int mxdgio_get_input_signal(HANDLE fd, int port);

/*
	set digital output
	
	param:
		(in)HANDLE fd  :	Handle of digital I/O		
		(in)int port	 :	index of digital output bit		
	
	Return value:
		BOOL:			TRUE indicates success, FALSE indicates failure.
*/
int mxdgio_set_output_signal_high(HANDLE fd, unsigned int port);

/*
	set digital output
	
	param:
		(in)HANDLE fd  :	Handle of digital I/O		
		(in)int port	 :	index of digital output bit		
		
	Return value:
		BOOL:			TRUE indicates success, FALSE indicates failure.
*/
int mxdgio_set_output_signal_low(HANDLE fd, unsigned int port);

/*
	get digital output
	
	param:
		(in)HANDLE hDIO  :	Handle of digital I/O		
		(in)int port	 :	index of digital output			
	Return value:
		int:			return digital output data 
*/
int mxdgio_get_output_signal(HANDLE fd, int port);

/*
	close digital I/O
	
	param:
		(in)HANDLE hDIO  :	Handle of digital I/O		
				
	Return value:
		BOOL:			TRUE indicates success, FALSE indicates failure. 
*/
void mxdgio_close(HANDLE fd);


/*	open a GPIO handle for later uses
	Inputs:
		none
	Returns:
		pointer to a GPIO handle. Return 0 on failure.
*/
HANDLE mxgpio_open(void);

/*	close a GPIO handle
	Inputs:
		<fd> the handle
	Returns:
		none
*/
void mxgpio_close(HANDLE fd);

/*	get the value (HIGH or LOW) of GPIO port
	Inputs:
		<fd>	the GPIO handle
		<port_no> the port number (started from 0)
		<data>    1:HIGH, 0:LOW
	Returns:
		0 on success, otherwise the function fails
*/
int mxgpio_set_data(HANDLE fd, unsigned int port_no, int data);


/*	Get the value (HIGH or LOW) of GPIO port
	Inputs:
		<fd>	the GPIO handle
		<port_no> the port number (started from 0)
	Returns:
		1 indicates HIGH, 0 indicates LOW, otherwise the function fails
*/
int mxgpio_get_data(HANDLE fd, unsigned int port_no);

/*
	Start the watchdog timer
	
	param:
		(in)unsigned long time  :	time period in milliseconds
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxwdg_open(unsigned long time);

/*
	refresh the watchdog timer
	
	param:
		(in)int fd : file descriptor
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxwdg_refresh(int fd);

/*
	Stop the watchdog timer
	
	param:
		(in)int fd : file descriptor
			
	Return value:
		none
*/
void mxwdg_close(int fd);

/*	Open the MxGeneralIo handle for later uses
	Inputs:
		none
	Returns:
		Result of MxGeneralIoOpen. Return 0 on failure.
*/
UINT32 mxsmbus_open();

/*
	set smbus state
	
	param:
		(in) smbus_base_addr : SMBUS address
		(in) psmbus_state : initial parameter to be set
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxsmbus_set_state(UINT32 smbus_base_addr, PSMBUSSETTING psmbus_state);

/*
	clear GSensor offset
	
	param:
		(in) smbus_base_addr : smbus base address
		(in) psmbus_state : initial parameter to be set
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxsmbus_clear_offset(UINT32 smbus_base_addr, PSMBUSSETTING psmbus_state);

/*
	Calibrate GSensor's offset
	
	param:
		(in) smbus_base_addr : SMBUS address
		(in) psmbus_state : initial parameter to be set
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxsmbus_calibrate(UINT32 smbus_base_addr, PSMBUSSETTING psmbus_state);

/*
	get smbus state
	
	param:
		(in) fd : result of MxGeneralIoOpen
		(in) psmbus_state : initial parameter to be set
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxsmbus_get_state(UINT32 smbus_base_addr, PSMBUSSETTING psmbus_state);


/*
	get current smbus register value
	
	param:
		(in)smbus_base_addr : smbus base address
		(in)psmbus_state : smbus settings
		(out)pgsensor_data : returned smbus data
		(out)sensor_data_length) : returned smbus data length
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxsmbus_read(UINT32 smbus_base_addr, PSMBUSSETTING psmbus_state, PGSENSORDATA pgsensor_data, int sensor_data_length);

int mxsmbus_write(UINT32 smbus_base_addr, UINT8 address,UINT8 offset , UINT8 length, UINT8 *Data );

/*	close a GPIO handle
	Inputs:
		<fd> the return value from mxwdg_open
	Returns:
		none
*/
void mxsmbus_close(UINT32 smbus_base_addr);

/*	Open the MxGeneralIo handle for later uses
	Inputs:
		none
	Returns:
		Result of MxGeneralIoOpen. Return 0 on failure.
*/
int mxbuzzer_open();

/*
	set the buzzer
	
	param:
		(in)int fd : result of MxGeneralIoOpen
			
	Return value:
		file descriptor : Success -1: fail
*/
int mxbuzzer_beep(int fd, int time, int freq);

/*	close the MxGeneralIo
	Inputs:
		<fd> the return value from mxwdg_open
	Returns:
		none
*/
void mxbuzzer_close(int fd);

/*	Open the Sensor handle for request the temperature sensor
	Inputs:
		none
	Returns:
		Result of mxsensor_open Return 0 on failure.
*/
UINT32 mxsensor_open();


/*	close the Sensor handle
	Inputs:
		<fd> the return value from mxsensor_close
	Returns:
		none
*/
void mxsensor_close();


/*	Get current temperature of CPU
	Outputs:
		<dCPUTemp> current CPU temperature
	Returns:
		none
*/
BOOL mxsensor_get_cpu_temp(double *dCPUTemp);

/*	Get current temperature of SATA expansion module 1
	Outputs:
		<dSATA1Temp> current temperature of SATA expansion module 1
	Returns:
		none
*/
BOOL mxsensor_get_sata1_temp(double *dSATA1Temp);

/*	Get current temperature of SATA expansion module 2
	Outputs:
		<dSATA1Temp> current temperature of SATA expansion module 2
	Returns:
		none
*/
BOOL mxsensor_get_sata2_temp(double *dSATA2Temp);



/*	Get current gravity value of x-axis
	Outputs:
		<device_index> index of G-Sensor device 0 : G-Sensor1 1 : G-Sensor2
	Returns:
		none
*/
int GetXAxis(int device_index);

/*	Get current gravity value of y-axis
	Outputs:
		<device_index> index of G-Sensor device 0 : G-Sensor1 1 : G-Sensor2
	Returns:
		none
*/
int GetYAxis(int device_index);

/*	Get current gravity value of z-axis
	Outputs:
		<device_index> index of G-Sensor device 0 : G-Sensor1 1 : G-Sensor2
	Returns:
		none
*/
int GetZAxis(int device_index);


#ifdef __cplusplus    // If used by C++ code, 
}
#endif