#ifndef __OSPTASKMSG__
#define __OSPTASKMSG__

#ifdef __cplusplus
extern "C" {
#endif
 

#include "ospTypes.h"

#define OSPTASK_MINID       1
#define OSPTASK_MAXID       31
#define OSP_MAX_TASK        64 //origin: 1024

#define OSP_TASK_STACK      16384
#define OSP_CPU_NUM         8

#define OSP_NO_AFFIINITY    (-1)

#define OSP_PROCESS_MSG     0
#define OSP_UDP_MSG         1
#define OSP_OTHER_MSG       2

//#define OSP_UDP_MSG(x, ipaddr)          (0x1234000000000000 |(x<<32) | inet_addr(ipaddr))


typedef struct tag_OSP_UDP_TASK_DATA                                                                                         
{   
    uint32_t      port;
    uint32_t      IsRx;
    char     ip[32];
}OSP_UDP_TASK_DATA;




typedef struct tag_OSP_TASKMSG_REG                                                                                         
{   
    uint32_t         TaskId;
    uint8_t          TaskName[32];
    uint32_t         TaskPri;
    OSP_FUNCPTR Init;
    OSP_FUNCPTR MainLoop;
    uint32_t         Cpu; 
    uint32_t         MsgType;
    uint64_t         MsgTypeData;
}OSP_TASKMSG_REG;

typedef enum {
    TASK_RUNNING = 1,
    TASK_SUSPEND,

}TaskState;

typedef struct tag_OSP_TASKMSG                                                                                         
{    
    OSP_TASKMSG_REG Osp_TaskMsg_Reg;
    void *sem;
    int32_t   TId; 
    pthread_t PhreadId;
    uint32_t   Active;
    uint32_t   State;
    uint64_t   TaskRunCnt;

    uint32_t   MsgQOffset;
    uint32_t   MsgQId;
    uint32_t   MsgRxCnt;
    uint32_t   MsgTxCnt;
    uint32_t   MsgAllocCnt;
    uint32_t   LogAllocCnt;
    uint32_t   MsgFreeCnt;
    uint32_t   MemAllocCnt;
    uint32_t   MemFreeCnt;

    uint32_t   MsgFullCnt; 
    int32_t   OsTaskPri;
    uint32_t   IsRt;
    OSP_FUNCPTR send_msg_func;
    OSP_FUNCPTR rev_msg_func;
    uint64_t   Msg_Que;
	
	uint32_t   dbg_out_is_enable;
	uint32_t   dbg_out2log_is_enable;
	uint32_t   is_shell;
    
}OSP_TASKCB;

extern OSP_TASKCB *gOspTask;

#define TASK_MSGQ(x) (Osp_MsgQ_Tcb *)(gOspTask[x].MsgQOffset + gOspMsgQue)
#define TASK_MSG_RXCNT(x) (gOspTask[x].MsgRxCnt)
#define TASK_MSG_TXCNT(x) (gOspTask[x].MsgTxCnt)
#define TASK_MSG_FULLCNT(x) (gOspTask[x].MsgFullCnt)
#define TASK_RUNCNT(x) (gOspTask[x].TaskRunCnt)
#define TASK_MSG_ALLOCCNT(x) (gOspTask[x].MsgAllocCnt)
#define TASK_LOGMSG_ALLOCCNT(x) (gOspTask[x].LogAllocCnt)
#define TASK_MSG_FREECNT(x) (gOspTask[x].MsgFreeCnt)

#define TASK_MEM_ALLOCCNT(x) (gOspTask[x].MemAllocCnt)
#define TASK_MEM_FREECNT(x) (gOspTask[x].MemFreeCnt)
#define TASK_ACTIVE(x) (gOspTask[x].Active)
#define TASK_SYSID(x) (gOspTask[x].TId)
#define TASK_THREADID(x) (gOspTask[x].PhreadId)
#define TASK_STATE(x) (gOspTask[x].State)
#define TASK_TXMSG_HANDLE(x) (gOspTask[x].send_msg_func)
#define TASK_MSG_QUE(x) (gOspTask[x].Msg_Que)

#define TASK_RXMSG_HANDLE(x) (gOspTask[x].rev_msg_func)

#define OSP_INVALID_TASKID (-1)



/*0 ~15 task ID belong to OSP*/
typedef enum {
    /*ID0 shoud rev means, ID1 is main_init*/
    OspTestTask = 1,
    OspTestTask1,
    OspTestTask2,
    OspTestTask3,
    OspTestTask4,
    OspTestTask5,
    OspTestTask6,
    SysLogTask,
    DbgLogTask,
    DbgPriTask,
    ShellTask,
    RemShellTask,
    OspTickLess,
    localshellin,
    localshellout,
    remoteshellin,
    remoteshellout,
    remotelocalshellin,
    ospnetshellrx,
#ifdef HEARTBEAT_ENABLE
    OspTmrTask,
#endif
    OspDbgLog,
	CpriGmacTx,
	CpriGmacRx,
}TaskId;
#define NOMSGTASK       0xaa000000
#define MSGTASK         0x55000000
#define MSGMASK         0xff000000

#define RTTASK          0x00aa0000
#define NORMALTASK      0x00550000
#define POLICYMASK      0x00ff0000

#define XXTASK          0x0000aa00
#define XXMASK          0x0000ff00

#define PRIMASK         0x000000ff


#define RT_MSG_PRI(x) (x | MSGTASK | RTTASK)
#define RT_NOMSG_PRI(x) (x | NOMSGTASK | RTTASK)
#define XRT_MSG_PRI(x) (x | MSGTASK | RTTASK | XXTASK)

#define IS_RT_PRI(x) ((x & POLICYMASK) == RTTASK)
#define NOMARL_MSG_PRI(x) (x | MSGTASK | NORMALTASK)
#define NOMARL_NOMSG_PRI(x) (x | NOMSGTASK | NORMALTASK)
 


extern __thread __typeof__(uint32_t) thread_local_TaskId;
#define CURRENT_TASKID thread_local_TaskId

extern OSP_STATUS osp_regtaskAll(void);
void *osp_find_sym(char *pName);
extern int32_t osp_get_task_num(void);
extern OSP_TASKCB *osp_get_taskcb_base(void);
extern OSP_STATUS osp_task_init(void);
extern void osp_show_task_info(void);
extern int32_t osp_task_is_shell(uint32_t taskid);	
extern void osp_start_task_all(void);
extern int32_t osp_set_net_task_msg_que(int32_t taskid, int32_t txudpid, int32_t rxudpid);
extern int32_t osp_local_shell_task_is_reged(void);
extern	OSP_STATUS osp_regtask(OSP_TASKMSG_REG *TaskRegTbl);

#ifdef __cplusplus
}
#endif

#endif  /*__OSPSEMH__ */

