/*  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.
*/
/*
    canbus.h

    This is an interface for both user programs and the driver.
*/
#ifndef _CANBUS_H_   
#define _CANBUS_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _CANBST
{
	unsigned int data_overrun_count;
	unsigned int warning_error_count;
	unsigned int bus_error_count;
	unsigned int status_error_count;
	unsigned int error_warning_limit;
	unsigned int tx_errors;
	unsigned int rx_errors;
} CANBST;

typedef struct _CANPRM
{
	unsigned int bus_timing; /* the bus speed in KHz (32-bit integer), default is 1000 KHz */
	unsigned int acceptance_filter_mode; /* non-zero is for single mode, default is dual mode */
	unsigned int acceptance_filter_code; /* default is 0 */
	unsigned int acceptance_filter_mask; /* default is 0xFFFFFFFF */
} CANPRM;

#define REMOTE_FRAME	1
#define EXTENDED_FRAME	(1<<1)

#define MSG_UNIT_SIZE	sizeof(CANMSG)
#define MAX_DATA_SIZE	8

/* define the content of a message that is passed between the user space and the driver 
*/
typedef struct _CANMSG
{
	unsigned int  identifier;
	unsigned int  flags;
	unsigned int  length;			/* the length of data */
	unsigned char data[MAX_DATA_SIZE];
} CANMSG;


#if defined(WIN32) && !defined(_WIN32_WCE)
#define MOXA_CAN_IOCTL              0x900 

/* set timeout in ms (32-bit integer) for reading data, default timeout is infinitive */
#define IOCTL_SET_READ_TIMEOUT				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+1,METHOD_BUFFERED,FILE_ANY_ACCESS)
/* set timeout in ms (32-bit integer) for writing data, default timeout is infinitive */
#define IOCTL_SET_WRITE_TIMEOUT				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+2,METHOD_BUFFERED,FILE_ANY_ACCESS) 	
/* get the number of bytes buffered inward in the driver layer */
#define IOCTL_GET_RX_BYTES					CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+3,METHOD_BUFFERED,FILE_ANY_ACCESS) 	
/* get the number of bytes buffered outward in the driver layer */
#define IOCTL_GET_TX_BYTES					CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+4,METHOD_BUFFERED,FILE_ANY_ACCESS) 		
/* get parameters of a bus using an empty CANPRM structure */
#define IOCTL_GET_PARAMETERS				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+5,METHOD_BUFFERED,FILE_ANY_ACCESS) 		
/* set parameters of a bus using a value-specified CANPRM structure */
#define IOCTL_SET_PARAMETERS				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+6,METHOD_BUFFERED,FILE_ANY_ACCESS) 		
/* purge the inward buffer in the driver layer */
#define IOCTL_PURGE_RX_BUFFER				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+7,METHOD_BUFFERED,FILE_ANY_ACCESS) 	
/* purge the outward buffer in the driver layer */
#define IOCTL_PURGE_TX_BUFFER				CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+8,METHOD_BUFFERED,FILE_ANY_ACCESS) 

#define IOCTL_GET_DEVICE_STATISTIC			CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+98,METHOD_BUFFERED,FILE_ANY_ACCESS) 
#define IOCTL_GET_DEVICE_REGISTERS			CTL_CODE( FILE_DEVICE_UNKNOWN, MOXA_CAN_IOCTL+99,METHOD_BUFFERED,FILE_ANY_ACCESS) 
#else /* CE and Linux */
/* set timeout in ms (32-bit integer) for reading data, default timeout is infinitive */
#define IOCTL_SET_READ_TIMEOUT				1
/* set timeout in ms (32-bit integer) for writing data, default timeout is infinitive */
#define IOCTL_SET_WRITE_TIMEOUT				2
/* get the number of bytes buffered inward in the driver layer */
#define IOCTL_GET_RX_BYTES					3
/* get the number of bytes buffered outward in the driver layer */
#define IOCTL_GET_TX_BYTES					4
/* get parameters of a bus using an empty CANPRM structure */
#define IOCTL_GET_PARAMETERS				5
/* set parameters of a bus using a value-specified CANPRM structure */
#define IOCTL_SET_PARAMETERS				6
/* purge the inward buffer in the driver layer */
#define IOCTL_PURGE_RX_BUFFER				7
/* purge the outward buffer in the driver layer */
#define IOCTL_PURGE_TX_BUFFER				8

#define IOCTL_GET_DEVICE_STATISTIC			98
#define IOCTL_GET_DEVICE_REGISTERS			99
#endif

#ifdef __cplusplus
}
#endif

#endif /* _CANBUS_H_ */
