/* VSS $Header: /Viper/VPCmdIF/VPCmdIF/Inc/VPcmdErr.h 11    6/01/20 3:54p Suzanne $
*/
///@file VPcmdErr.h
/// Header file containing VPcmdIF error codes and macros
#pragma once

/// General purpose error codes.
typedef enum
{
	E_VP_SUCCESS = 0              /**< 0, No error occured. */
	, E_VPERR_NAK_RESP            /**< 1, Tracker rejected the command. */
	, E_VPERR_NAKWARN_RESP        /**< 2, Tracker responded with NAK_WARNING. */
	, E_VPERR_MEMORY_ALLOCATION   /**< 3, Memory could not be allocated. */
	, E_VPERR_INVALID_CONTEXT     /**< 4, The specified context is invalid. */
	, E_VPERR_INVALID_ACTION      /**< 5, The specified action is invalid. */
	, E_VPERR_INVALID_SENSOR      /**< 6, The specified sensor is invalid. */
	, E_VPERR_INVALID_SENSOR_ACT  /**< 7, The specified sensor and action combination is invalid.*/
	, E_VPERR_INVALID_PARAMS      /**< 8, An additional specified parameter is invalid. */
	, E_VPERR_INVALID_SIZE        /**< 9, The size field of the received frame is invalid. */
	, E_VPERR_INVALID_CRC         /**< 10,The CRC of the received frame is invalid */
	, E_VPERR_TIMEOUT             /**< 11, A timeout was reached before all the data could be recieved. */
	, E_VPERR_PLATFORM            /**< 12, Indicates a system error in hi bits. */
	, E_VPERR_LIBUSB			  /**< 13, Indicates an error received from libusb library in high bits */
	, E_VPERR_UNKNOWN             /**< 14, The error is unknown. */
	, E_VPERR_NOTFOUND			  /**< 15, Device not found */
	, E_VPERR_SERIO				  /**< 16, Error received from VPserIO library in high bits */
	, E_VPERR_CMDRSP_MISMATCH	  /**< 17, Command-Response mismatch */
	, E_VPERR_BUF_TOO_SMALL		  /**< 18, Supplied buffer is too small */
	, E_VPERR_NOT_IMPLEMENTED     /**< 19, Cmd or operation not implemented */

	, E_VPERR_MAX/**< 15, Not an actual error, marks the end of the error list. */
} eVPErrors;

// Operational Errors generated from within a context.
typedef enum
{
	E_CTXERR_NONE // *
	, E_CTXERR_INVALID_DEVHND = E_VPERR_MAX  // *
	, E_CTXERR_INVALID_CONDITION // *
	, E_CTXERR_MEMALLOC_ERROR // *
	, E_CTXERR_DEV_RECV_QUEUE_EMPTY // *no response
	, E_CTXERR_NO_MORE_DATA // *incoming queue empty (P&O)

	, E_CTXERR_MAX
}eContextErr;

// Errors produced by device io operations
typedef enum
{
	E_DEVIOERR_NONE
	, E_DEVIOERR_NO_CNX = E_CTXERR_MAX//E_VPERR_MAX //
	, E_DEVIOERR_FRAME_HEADER

	, E_DEVIOERR_USB_MIN 
	, E_DEVIOERR_USB_WRITERR = E_DEVIOERR_USB_MIN
	, E_DEVIOERR_USB_READERR
	, E_DEVIOERR_USB_MAX = E_DEVIOERR_USB_READERR

	, E_DEVIOERR_SER_MIN
	, E_DEVIOERR_SER_WRITERR = E_DEVIOERR_SER_MIN
	, E_DEVIOERR_SER_READERR
	, E_DEVIOERR_SER_MAX = E_DEVIOERR_USB_READERR

	, E_DEVIOERR_MAX
}eDevIOErr;


typedef int errorval; ///< Return code for API functions.  All error codes are POSITIVE numbers.

#define VPCMDERR_MAX E_DEVIOERR_MAX

/**
*  Reads the hi bits in `x`. Platform specific errors, such as those generated by `WSAGetLastError()` for Windows, are stored in
*  the hi bits of a returned error code.
*/
#define vp_hierr(x) ((x & 0xffff0000) >> 16)
/**
*  Reads the low bits in `x`.  Library specific errors, those defined in eG4PlusErrors, are stored in the low bits of a returned
*  error code.
*/
#define vp_loerr(x) (x & 0x0000ffff)

#define io_err(x) (x >= E_DEVIOERR_NO_CNX) && (x<E_DEVIOERR_MAX)

#define usb_err(x) (io_err(x) && (x >= E_DEVIOERR_USB_MIN) && (x<=E_DEVIOERR_USB_MAX))
#define ser_err(x) (io_err(x) && (x >= E_DEVIOERR_SER_MIN) && (x<=E_DEVIOERR_SER_MAX))

#define VPSUCCEEDED(e)	(((errorval)(e)) == 0)
#define VPFAILED(e)		( (((errorval)(e)) != 0) && (!(VPWARN(e))) ) 
#define VPWARN(e)		(((errorval)(e)) == E_VPERR_NAKWARN_RESP)
