/***********************************************************************************************//**
  @file         xbee_sx_defs.h
  @date         $Date:$        
  @version      $Revision:$    

  @brief        XBee/XBee-PRO SX Module Definitions

 **************************************************************************************************/

#ifndef __XBEE_SX_DEFS_H
#define __XBEE_SX_DEFS_H

/**************************************************************************************************
 *                                        INCLUDES
 **************************************************************************************************/
#include <stdint.h>
#include "ComTypeDefs.h"

/**************************************************************************************************
 *                                        DEFINES
 **************************************************************************************************/

// This is just a SWAG for now
#define XBEE_MAX_PACKET_SIZE	128

#define START_DELIMITER		0x7E

enum xbee_commands
{
	XBEECMD_LegacyTXRequest 		= 0x00,
	XBEECMD_ATCommand 				= 0x08,
	XBEECMD_ATCommandQueue 			= 0x09,
	XBEECMD_TransmitRequest			= 0x10,
	XBEECMD_ExplicitTXRequest		= 0x11,
	XBEECMD_RemoteCommandRequest	= 0x17
};

enum xbee_responses
{
	XBEERSP_ModemStatus 			= 0x8A,
	XBEERSP_TransmitStatus			= 0x8B,
	XBEERSP_RouteInfoPacket			= 0x8D,
	XBEERSP_AggregateAddrUpdate		= 0x8E,
	XBEERSP_LegacyRXIndicator		= 0x80,
	XBEERSP_ATCommandResponse		= 0x88,
	XBEERSP_LegacyTXStatus			= 0x89,
	XBEERSP_RXIndicator				= 0x90,
	XBEERSP_ExplicitRXIndicator		= 0x91,
	XBEERSP_NodeIDIndicator			= 0x95,
	XBEERSP_RemoteCommandResponse	= 0x97
};

enum at_commands
{
	// Special Commands
	ATCMD_ApplyChanges				= 'A' *256 + 'C',
	ATCMD_SoftwareReset				= 'F' *256 + 'R',
	ATCMD_RestoreDefaults			= 'R' *256 + 'E',
	ATCMD_Write						= 'W' *256 + 'R',

	//MAC/PHY Commands
	ATCMD_NetworkID					= 'I' *256 + 'D',
	ATCMD_TXPowerLevel				= 'P' *256 + 'L',

	ATCMD_AggregatorSupport			= 'A' *256 + 'G',


	// Addressing Commands
	ATCMD_SerialNumberHigh			= 'S' *256 + 'H',
	ATCMD_SerialNumberLow			= 'S' *256 + 'L',
	ATCMD_DestinationAddrsHigh		= 'D' *256 + 'H',
	ATCMD_DestinationAddrsLow		= 'D' *256 + 'L',
	ATCMD_ClusterID					= 'C' *256 + 'I',

	// Firmware Version/Info Commands
	ATCMD_FirmwareVersion			= 'V' *256 + 'R',
	ATCMD_HarwareVersion			= 'H' *256 + 'V',
	ATCMD_MaxPacketPayloadBytes		= 'N' *256 + 'P',
};

#define XBEE_ADRS_SIZE		8
typedef uint8_t XBeeAdrs64[XBEE_ADRS_SIZE];

#pragma pack(push,1)

/**************************************************************************************************
 *                                 XBEE COMMAND MESSAGE STRUCTURES
 **************************************************************************************************/

typedef struct XBeeAPIFrameHeader
{
	uint8_t		start_delim;	// 0x7E
	uint16_t	length;			// data field length
	uint8_t		frame[0];		// Start of Frame data

} XBeeAPIFrameHeader_t;

typedef struct XBeeAPI_LegacyTXRequestFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_LegacyTXRequest
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	XBeeAdrs64	dest_adrs;		// Destination Address
	uint8_t		options;		// 0=Standard, 1=Disable ACK
	uint8_t		data[0];		// Data to be sent to dest device

} XBeeAPI_LegacyTXRequestFrm_t;


typedef struct XBeeAPI_ATCommandFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_ATCommand
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	uint16_t	at_cmd;			// packed two ascii char AT Command
	uint8_t		parm_value;		// parameter value (binary)

} XBeeAPI_ATCommandFrm_t;

typedef struct XBeeAPI_ATCommandQueueFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_ATCommandQueue
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	uint16_t	at_cmd;			// packed two ascii char AT Command
	uint8_t		parm_value;		// parameter value (binary)

} XBeeAPI_ATCommandQueueFrm_t;

typedef struct XBeeAPI_TransmitRequestFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_TransmitRequest
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	XBeeAdrs64	dest_adrs;		// Destination Address
	uint16_t	reserved;		// Set to 0xFFFE
	uint8_t		bcast_radius;	// Sets the maximum number of hops a broadcast transmission can occur. If
								// set to 0, the broadcast radius is set to the maximum hops value
	uint8_t		options;		// Set all other bits to 0.
	uint8_t		data[0];		// Data to be sent to dest device

} XBeeAPI_TransmitRequestFrm_t;

typedef struct XBeeAPI_ExplicitTXRequestFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_ExplicitTXRequest
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	XBeeAdrs64	dest_adrs;		// Destination Address
	uint16_t	reserved;		// Set to 0xFFFE
	uint8_t		source_ep;		// Source Endpoint for the transmission.
	uint8_t		dest_ep;		// Destination Endpoint for the transmission.
	uint16_t	cluster_id;		// The Cluster ID that the host uses in the transmission.
	uint16_t	profile_id;		// The Profile ID that the host uses in the transmission.
	uint8_t		bcast_radius;	// Sets the maximum number of hops a broadcast transmission can occur. If
								// set to 0, the broadcast radius is set to the maximum hops value
	uint8_t		options;		// Set all other bits to 0.
	uint8_t		data[0];		// Data to be sent to dest device

} XBeeAPI_ExplicitTXRequestFrm_t;

typedef struct XBeeAPI_RemoteATCommandFrm
{
	uint8_t		api_id;			// API_ID = XBEECMD_RemoteCommandRequest
	uint8_t		frame_id;		// unique frame identifier (i.e. seq#)
	XBeeAdrs64	dest_adrs;		// Destination Address
	uint16_t	reserved;		// Set to 0xFFFE
	uint8_t		remote_cmd_opt;	// 0x02 = Apply changes on remote. If you do not set this, you must send the
								// AC command for changes to take effect.  Set all other bits to 0.
	uint16_t	at_cmd;			// packed two ascii char AT Command
	uint8_t		parm_value;		// parameter value (binary)

} XBeeAPI_RemoteATCommandFrm_t;


/**************************************************************************************************
 *                                 XBEE RESPONSE MESSAGE STRUCTURES
 **************************************************************************************************/

typedef struct XBeeAPI_ModemStatusFrm
{
	uint8_t		api_id;			// API_ID = XBEERSP_ModemStatus
	uint8_t		status;			// 0x00 = Hardware reset, 0x01 = Watchdog timer reset,
								// 0x0B = Network woke up, 0x0C = Network went to sleep
} XBeeAPI_ModemStatusFrm_t;

typedef struct XBeeAPI_TransmitStatusFrm
{
	uint8_t		api_id;			// API_ID = XBEERSP_TransmitStatus
	uint8_t		frame_id;		// frame identifier of command
	uint16_t	reserved;		// ???
	uint16_t	dest_adrs;		// 16b Destination Address. If not successful, this address is 0xFFFD.
	uint8_t		tx_retry_count;	// The number of application transmission retries that occur.
	uint8_t		delivery_status;// 0x00 = Success, 	0x01 = MAC ACK failure
								// 0x02 = Collision avoidance failure,	0x21 = Network ACK failure
								// 0x25 = Route not found, 0x31 = Internal resource error
								// 0x32 = Internal error, 0x74 = Payload too large
	uint8_t		disc_status;	// 0x00 = No discovery overhead, 0x02 = Route discovery

} XBeeAPI_TransmitStatusFrm_t;

typedef struct XBeeAPI_RouteInformationFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_RouteInfoPacket
 	uint8_t		source_event;	// 0x11 = NACK, 0x12 = Trace route
 	uint8_t		length;			// The number of bytes that follow, excluding the checksum.
 	uint32_t	timestamp;		// System timer value on the node generating the Route Information Packet.
 								// The timestamp is in microseconds.
 	uint8_t		ack_to_count;	// The number MAC ACK timeouts that occur.
	uint16_t	reserved;		//
 	XBeeAdrs64	dest_addrs;		// The address of the final destination node of this network-based transmission.
 	XBeeAdrs64	source_addrs;	// Address of the source node of this network-based transmission.
 	XBeeAdrs64	resp_addrs;		// Address of the node that generates this Route Information packet after it sends
 	XBeeAdrs64	receiver_addrs;	// Address of the node that the device sends (or attempts to send) the data	packet.

} XBeeAPI_RouteInformationFrm_t;


typedef struct XBeeAPI_AggregateAddrsUpdateFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_AggregateAddrUpdate
 	uint8_t		format_id;		// Byte reserved to indicate the format of additional packet information which may be
 								// added in future firmware revisions. In the current firmware revision, this field returns 0x00.
 	XBeeAdrs64	new_addrs;		// Address to which DH and DL are being set.
 	XBeeAdrs64	old_addrs;		// Address to which DH and DL were previously set.

} XBeeAPI_AggregateAddrsUpdateFrm_t;

typedef struct XBeeAPI_LegacyRXIndicatorFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_LegacyRXIndicator
 	XBeeAdrs64	source_addrs;	// The sender's 64-bit address. MSB first, LSB last.
 	uint8_t		rssi;			// Received Signal Strength Indicator of the last hop. The Hexadecimal equivalent
 								// of (-dBm) value.
 	uint8_t		options;
 	uint8_t		rcvd_data[0];	// Received RF data

} XBeeAPI_LegacyRXIndicatorFrm_t;

typedef struct XBeeAPI_ATCommandResponseFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_ATCommandResponse
 	uint8_t		frame_id;		// frame identifier of command
 	uint16_t	at_cmd;			// Command name: two ASCII characters that identify the command.
 	uint8_t		cmd_status;		// 0 = OK, 1 = ERROR, 2 = Invalid command, 3 = Invalid parameter
 	uint8_t		cmd_data[0];	// The register data in binary format. If the host sets the register, the device
 								// does not return this field.
} XBeeAPI_ATCommandResponseFrm_t;

typedef struct XBeeAPI_LegacyTXStatusFrm
{
	uint8_t		api_id;			// API_ID = XBEERSP_LegacyTXStatus
	uint8_t		frame_id;		// frame identifier of command
	uint8_t		status;			// 0x00 = standard, 0x01 = no ACK received

} XBeeAPI_LegacyTXStatusFrm_t;

typedef struct XBeeAPI_RXIndicatorFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_RXIndicator
 	XBeeAdrs64	source_addrs;	// The sender's 64-bit address. MSB first, LSB last.
 	uint16_t	reserved;		//
 	uint8_t		rcv_options;	// 0x01 = Packet acknowledged, 0x02 = Packet was a broadcast packet
 	uint8_t		rcvd_data[0];	// Received RF data

} XBeeAPI_RXIndicatorFrm_t;


typedef struct XBeeAPI_ExplicitRXIndicatorFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_ExplicitRXIndicator
 	XBeeAdrs64	source_addrs;	// The sender's 64-bit address. MSB first, LSB last.
 	uint16_t	reserved;		//
	uint8_t		source_ep;		// Endpoint of the source that initiates transmission.
	uint8_t		dest_ep;		// Endpoint of the destination that the message is addressed to.
	uint16_t	cluster_id;		// The Cluster ID that the frame is addressed to.
	uint16_t	profile_id;		// The Profile ID that the frame is addressed to.
 	uint8_t		rcv_options;	// 0x01 = Packet acknowledged, 0x02 = Packet was a broadcast packet
 	uint8_t		rcvd_data[0];	// Received RF data

} XBeeAPI_ExplicitRXIndicatorFrm_t;

typedef struct XBeeAPI_NodeIDIndicatorFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_NodeIDIndicator
 	XBeeAdrs64	source_addrs;	// The sender's 64-bit address. MSB first, LSB last.
 	uint16_t	reserved;		//
 	uint8_t		rcv_options;	//
 	XBeeAdrs64	remote_adrs;	//
	uint8_t		ni_str[2];		// Node identifier string on the remote device. The NI string is terminated with a NULL byte (0x00).
 	uint16_t	reserved2;		//
 	uint8_t		device_type;	// 0=Coordinator, 1=Normal Mode, 2=End Device
 	uint8_t		source_event;	// 1=Frame sent by node identification pushbutton event.
 	uint16_t	digi_prof_id;	// Set to Digi’s application profile ID.
 	uint16_t	digi_mfg_id;	// Set to Digi’s Manufacturer ID.
 	uint32_t	digi_dd_val;	// Reports the DD value of the responding device.
 	uint8_t		rssi;			// Received signal strength indicator.


} XBeeAPI_NodeIDIndicatorFrm_t;

typedef struct XBeeAPI_RemoteATCommandResponseFrm
{
 	uint8_t		api_id;			// API_ID = XBEERSP_RemoteCommandResponse
 	uint8_t		frame_id;		// frame identifier of command
 	XBeeAdrs64	source_addrs;	// The sender's 64-bit address. MSB first, LSB last.
 	uint16_t	reserved;		//
 	uint16_t	at_cmd;			// Command name: two ASCII characters that identify the command.
 	uint8_t		cmd_status;		// 0 = OK, 1 = ERROR, 2 = Invalid command, 3 = Invalid parameter
 	uint8_t		cmd_data[0];	// The register data in binary format. If the host sets the register, the device
 								// does not return this field.
} XBeeAPI_RemoteATCommandResponseFrm_t;

#pragma pack(pop)


#endif //__XBEE_SX_DEFS_H
