/*  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	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) fd : result of MxGeneralIoOpen
		(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);

/*
	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);


/*
	refresh the watchdog timer
	
	param:
		(in)int fd : result of MxGeneralIoOpen
			
	Return value:
		file descriptor : Success -1: fail
*/
//int mxsmbus_read(int fd, UINT8 address,UINT8 Offset, UINT8 *Data);
//int mxsmbus_read(UINT32 smbus_base_addr, UINT8 address,UINT8 offset , UINT8 length, UINT8 *Data );
//int mxsmbus_read(UINT32 smbus_base_addr, int port_idx, PSMBUSSETTING psmbus_state, UINT8 *sensor_data, int sensor_data_length);
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);



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