#ifndef __COMMON_DEFINES_H__
#define __COMMON_DEFINES_H__

#include <os_cpu.h>
#if !defined (ALT_INICHE)
  #error The Unimation Server example requires the
  #error NicheStack TCP/IP Stack Software Component. Please see the Nichestack
  #error Tutorial for details on Nichestack TCP/IP Stack - Nios II Edition,
  #error including notes on migrating applications from lwIP to NicheStack.
#endif

#ifndef __ucosii__
  #error This Unimation Server example requires
  #error the MicroC/OS-II Intellectual Property Software Component.
#endif

#if defined (ALT_LWIP)
  #error The Unimation Server example requires the
  #error NicheStack TCP/IP Stack Software Component, and no longer works
  #error with the lwIP networking stack.  Please see the Altera Nichstack
  #error Tutorial for details on Nichestack TCP/IP Stack - Nios II Edition,
  #error including notes on migrating applications from lwIP to NicheStack.
#endif

/*
 *  Task Priorities:
 * 
 *  MicroC/OS-II only allows one task (thread) per priority number.   
 */
#define UNIMATION_CLIENT_TASK_PRIORITY          7
#define UNIMATION_SLAVE_TASK_PRIORITY           16

#define UNIMATION_SERVER_TASK_PRIORITY          4
#define UNIMATION_INITIAL_TASK_PRIORITY         5
#define PUMA_STATE_TASK_PRIORITY               12
/* 
 * The IP, gateway, and subnet mask address below are used as a last resort
 * if no network settings can be found, and DHCP (if enabled) fails. You can
 * edit these as a quick-and-dirty way of changing network settings if desired.
 * 
 * Default IP addresses are set to all zeros so that DHCP server packets will
 * penetrate secure routers. They are NOT intended to be valid static IPs, 
 * these values are only a valid default on networks with DHCP server. 
 * 
 * If DHCP will not be used, select valid static IP addresses here, for example:
 *           IP: 192.168.1.234
 *      Gateway: 192.168.1.1
 *  Subnet Mask: 255.255.255.0
 */
#define IPADDR0   192
#define IPADDR1   168
#define IPADDR2   2
#define IPADDR3   35

#define GWADDR0   192
#define GWADDR1   168
#define GWADDR2   2
#define GWADDR3   1

#define MSKADDR0  255
#define MSKADDR1  255
#define MSKADDR2  255
#define MSKADDR3  0

#define SERVER_IP0   192
#define SERVER_IP1   168
#define SERVER_IP2   2
#define SERVER_IP3   1
/*
 * IP Port(s) for our application(s)
 */
#define UNIMATION_PORT 15030
/*
 * ROBOT state report IP Port(s)
 */
#define PUMA_STATE_PORT 15031

/* Definition of Task Stack size for tasks not using Nichestack */
#define   TASK_STACKSIZE       2048
  
#define MAX_IPC_MESSAGE_SIZE 1024

/*
 * Bit Masks for LED Toggles
 */
#define BIT_0 0x1
#define BIT_1 0x2
#define BIT_2 0x4
#define BIT_3 0x8
#define BIT_4 0x10
#define BIT_5 0x20
#define BIT_6 0x40
#define BIT_7 0x80

/*
 * TX & RX buffer sizes for all socket sends & receives in unimation app
 */
#define UNIMATION_RX_BUF_SIZE  1500
#define UNIMATION_TX_BUF_SIZE  1500

/* Syncronization WORD: 0x5AFAFA5A*/
#define MSG_START_SYNC      0x5AFAFA5A
#define UNIMATION_MODULE_ID  0x00000003
#define UNIMATION_HOST_ID	 0x00000002

#define CMD_FRM_START			0x82
#define CMD_FRM_CR_END			0x0D
#define CMD_FRM_LF_END			0x0A
/*
 * Here we structure to manage sss communication for a single connection
 */

typedef struct Client_fds_tag
{
    INT32S *fd;
	INT32S *id;
    INT32S size;
    INT32S capacity;
} client_fds;


typedef struct UNIMATION_SOCKET
{
  enum { READY, COMPLETE, CLOSE } state;
  INT32S       fd;
  INT32S       close;
  INT32U       start;/*first send packet header*/
  INT32U       socket_error; /* 0: no error, 1: has error , need close*/
  unsigned int u32Seq; /* u32Seq */
  BOOLEAN 		 to_hal; /* command to HAL*/
  INT32U		 q_id; /* command index in command array*/

  client_fds     clients; //client fds

  INT16U     r_rd; /* position we've read up to */
  INT16U     r_wr; /* position we've written up to */
  INT16U     t_rd;
  INT16U     t_wr;
  INT8U      rx_buf[UNIMATION_RX_BUF_SIZE];
  INT8U      tx_buf[UNIMATION_TX_BUF_SIZE];
} UnimationConn;


typedef struct IPC_MESSAGE_HEADER_t
{   INT32U sync;
    INT32U time_sec;
    INT32U time_nsec;
    INT32U sn; //serial number
    INT32U src;  //source module id
    INT32U dst; // dest module id
    INT32S type; //messageType_e
    INT32U size; // Message header  including header ( at least 32 bytes)
} IpcMessageHeader;


/*CMD_Proto */
#define FLAG_SEQUENCE       0x24 /* '$'*/
#define PROTO_TYPE			0x01 /* Protocol version 0.1*/

/*
 * Defined commands for the Unimation server to interperate
 */
#define CMD_QUIT              0x51 /*   'Q' */
#define CMD_QUIT_RESP		  0x71 /*   'q' */
#define CMD_ATTACH_REQ        0x41 /*  'A'  */
#define CMD_ATTACH_RESP       0x61 /*  'a'  */
#define CMD_DETATCH_REQ       0x44 /*  'D'  */
#define CMD_DETATCH_RESP      0x64 /*  'd'  */
#define CMD_SIGNAL_REQ		  0x53 /*  'S'  */
#define CMD_SIGNAL_RESP		  0x73 /* 's'   */
#define CMD_READ_REQ		  0x52 /* 'R'   */
#define CMD_READ_RESP		  0x72 /* 'r'   */
#define CMD_WRITE_REQ		  0x57 /*  'W'  */
#define CMD_WRITE_RESP		  0x77 /* 'w'   */
#define CMD_IOCTL_REQ		  0x49 /*  'I'  */
#define CMD_IOCTL_RESP		  0x69 /* 'i'   */
#define CMD_PROBE_REQ         0x50 /* 'P' */
#define CMD_PROBE_RESP        0x70 /* 'p' */
#define CMD_OPEN_REQ          0x4F /* 'O' */
#define CMD_OPEN_RESP         0x6F /* 'o' */
#define SERVER_CONNECT_IND	  0x6F /*	'o' */
#define CMD_UNKNOWN_RESP      0x7F /* unknown command */

#define IND_RECEIVE_MSG		  0x6d /* 'm'  Receive message from controller*/
/*
 *   Define maximum message size that is forwarded to the unimation controller
 */
#define UNIMATION_MSG_BUF_SIZE 1020
#define UNIMATION_MSG_Q_SIZE  16  /* Message capacity of UnimationCommandQ */



#pragma pack(1)
typedef struct _NetCommandProtocol_tag
{
    INT8U             u8Flag; /*Must be $*/
    INT8U             u8Proto; /* protocol type: version 01 */
    INT8U			  u8Seq;   /* Command Sequence */
    INT8U             u8Pad;  /*command data frame padding byte : valid value 0-3 */
    INT16U            u16Len;   /* Command Length*/
    INT8U             u8Cmd; /* LSB first 00:command*/
    INT8U		      u8CmdExt;
    /* note:Length is command body length, (the last byte of command is CRC ) and followed with
     * terminator 0x0D 0x0A*/
} UNIMATION_CMD_HEADER; /* FIXME change to 4 bytes header*/


struct MESSAGE_Q
{
	INT16U 			u16Length;
	INT8U 			cmd;
	INT8U           cmdExt;
	INT8U 			data[UNIMATION_MSG_BUF_SIZE]; /* P.Shi: 03122018: hard coded here */
};
#pragma pack() /* restore default pack value */

#endif /* end of __COMMON_DEFINES_H__ */
