#ifndef _MGC__H
#define _MGC__H

#include "../../../public/src/include/pub_sdp.h"
#include "../../../mgcp/src/include/mgcp.h"
#include "../../../8ecp/src/include/8ecp.h"

#define MGC_MAX_NUM_OF_SAP          4
#define MGC_MAX_USER_NAME_LEN       8
#define MGC_MAX_DOMAIN_LEN          64
#define MGC_MAX_PHY_PORT_PER_MG     32

#define MGC_USER_TYPE_MGC           0
#define MGC_USER_TYPE_MG            1

#define MGC_MAX_NUM_OF_MG           1024
#define MGC_MAX_NUM_OF_PHY_PORT     1024
#define MGC_MAX_NUM_OF_CHNL         9424
#define MGC_MAX_NUM_OF_PORT         8192

#define MGC_MAX_NUM_OF_AAS_MGC      4

#define MGC_MG_TYPE_INTERNAL        0
#define MGC_MG_TYPE_AudioCoder      1
#define MGC_MG_TYPE_LGC_MG          2
#define MGC_MG_TYPE_SSW_CSS         3
#define MGC_MG_TYPE_IPBSS           4
#define MGC_MG_TYPE_ANALOG          11 
#define MGC_MG_TYPE_ANN             21

#define MGC_PHY_PORT_TYPE_E1        1
#define MGC_PHY_PORT_TYPE_T1        2
#define MGC_PHY_PORT_TYPE_ANALOG    3

#define MGC_MG_CTRL_TYPE_MGCP       0
#define MGC_MG_CTRL_TYPE_8ECP       1

#define MGC_MEDIA_ATTR_EC_FLAG      0x01
#define MGC_MEDIA_ATTR_SS_FLAG      0x02
#define MGC_MEDIA_ATTR_VT_FLAG      0x04

#define MGC_OPER_ID_CONNECT_CHNL    1
#define MGC_OPER_ID_CONNECT_DTMF    2
#define MGC_OPER_ID_CONNECT_TONE    3
#define MGC_OPER_ID_CRCX            4
#define MGC_OPER_ID_MDCX            5
#define MGC_OPER_ID_DLCX            6
#define MGC_OPER_ID_RQNT            7
#define MGC_OPER_ID_AUEP            8
#define MGC_OPER_ID_CONNECT_AAS     9

#define MGC_OPER_FAILURE            0
#define MGC_OPER_SUCCESS            1

#define MGC_VCTYPE_MAX              13
#define MGC_VCTYPE_MAX_1            12

#define MGC_VCTYPE_PCMA             0
#define MGC_VCTYPE_PCMU             1
#define MGC_VCTYPE_GSM              2
#define MGC_VCTYPE_GSM_EFR          3
#define MGC_VCTYPE_AMR_12_2         4
#define MGC_VCTYPE_G729B            5
#define MGC_VCTYPE_AMR_10_2         6
#define MGC_VCTYPE_AMR_7_95         7
#define MGC_VCTYPE_AMR_7_4          8
#define MGC_VCTYPE_AMR_6_7          9
#define MGC_VCTYPE_AMR_5_15         10
#define MGC_VCTYPE_AMR_5_9          11
#define MGC_VCTYPE_AMR_4_75         12



#define MGC_MEDIA_PARA_FLAG_EC      0x0001
#define MGC_MEDIA_PARA_FLAG_SS      0x0002
#define MGC_MEDIA_PARA_FLAG_VT      0x0004
#define MGC_MEDIA_PARA_FLAG_CM      0x0008
#define MGC_MEDIA_PARA_FLAG_PT      0x0010

#define MGC_MAX_NUM_OF_CODEC        16

#define MGC_HOLD_MUSIC              7
#define MGC_TONE_RING               8
#define MGC_TONE_BUSY               9
#define MGC_TONE_WARN               10

#define MGC_CONNECT_SILENT_TONE     0x01

#define MGC_MG_NETWORK_PORT         4967
#define MGC_MGC_NETWORK_PORT        4966


#define MGC_CONNECT_STATUS_IDLE		   0
#define MGC_CONNECT_STATUS_CREATING	   1
#define MGC_CONNECT_STATUS_CREATED     2
#define MGC_CONNECT_STATUS_TONE 3

typedef enum{
    MGC_DTMF_1 = 1,
	MGC_DTMF_2 = 2,	
    MGC_DTMF_3 = 3,
	MGC_DTMF_4 = 4,
    MGC_DTMF_5 = 5,
	MGC_DTMF_6 = 6,
    MGC_DTMF_7 = 7,
	MGC_DTMF_8 = 8,
    MGC_DTMF_9 = 9,
	MGC_DTMF_0 = 10,
    MGC_DTMF_START = 11,
	MGC_DTMF_POUND = 12	
}MGC_DTMF_DEF;

typedef enum{
    MGC_PKG_L = 0,
	MGC_PKG_D = 1,
	MGC_PKG_G = 2
}MGC_PKG_TYPE;

typedef struct _OPER_REC
{
    BYTE operaId;
    BYTE tone;
	WORD duration;
	WORD times;
}OPER_REC;

typedef struct _CHNL
{
    WORD    mgNo;       // mg No.
    BYTE    portNo;     // local physical port No. of that mg
    WORD    chlNo;      // local channel No. of that trunk
    WORD    connectNo;
}CHNL;

typedef struct _MGC_CODEC_LIST
{
    BYTE    num;                            // num of codecs
    WORD    priority;                       // priority level, used when comparing different codec list
    BYTE    codec[MGC_MAX_NUM_OF_CODEC];    // codec ID number, high priority codec first
}MGC_CODEC_LIST;

typedef struct _MEDIA_PARA
{
    WORD    flag;           // which media attribute is affected
    BYTE    ecDisable;      // echo canceller disable or enable
    BYTE    ssDisable;      // silence suppression disable or enable;
    BYTE    conMode;        // connect mode
    BYTE    ptime;          // packetization period
    MGC_CODEC_LIST  vocoderType;    // vocoder type
}MEDIA_PARA;

typedef struct _MG_ATTR
{
    BYTE    ptime;      // ptime, in ms
    BYTE    mgType;     // non-virtual/ann
    BYTE    ctrlType;   // 8ecp or mgcp
    BYTE    epDes;      // endpoint description method: structured or unstructured
    BYTE    toneCap;    // Tone generation capability
    BYTE    maxPorts;   // maximum physical ports of this MG
    WORD    maxChnls;   // maximum channels of this MG, only applicable to virtual MG.
    DWORD   ip;         // IP address of the MG
    char    domain[MGC_MAX_DOMAIN_LEN];     // domain name of the mg
    BYTE    e1cardNo;
    DWORD   auditTimer;
    
    MGC_CODEC_LIST  codecList;              // list of codecs that the MG supports
}MG_ATTR;

typedef struct _MEDIA_ATTR
{
    BYTE    ecDisable;      // echo canceller disable or enable
    BYTE    ssDisable;      // silence suppression disable or enable
    BYTE    vocoderType;    // vocoder type
    BYTE    conMode;        // connect mode
    char    callId[33];     // call Id
    char    conId[33];      // connection id
    BYTE    ptime;          // packetization period
    WORD    pkgEvent;       // package + event
    WORD    pkgSignal;      // package + signal
    WORD    pkgReq;         //package+req 
    WORD    restMode;       // restart mode
    WORD    restDelay;      // restart delay
    
    PUB_SDP_MSG sdp;        // media info of this channel
}MEDIA_ATTR;

typedef struct _EVENT
{
    BYTE    pkg;        // package name
    BYTE    event;      // event name
}EVENT;

typedef struct _SIGNAL
{
    BYTE    pkg;        // package name
    BYTE    signal;     // signal name
}SIGNAL;

typedef struct _MGC_SAP
{
    char    usrName[MGC_MAX_USER_NAME_LEN]; // user name
    BYTE    usrType;    // user acts as a mg or mgc
    
    // callback function registration - indication
    int (*mgc_ind)(CHNL chnl, BYTE cmd, WORD *usrPort, WORD mgcPort, MEDIA_ATTR *mediaAttr);
    
    // callback function registration - confirmation
    int (*mgc_cnf)(WORD usrPort, WORD mgcPort, BYTE operId, BYTE success, WORD cause);
}MGC_SAP;

int mgc_bind(MGC_SAP *sap);
int mgc_unbind(MGC_SAP *sap, BYTE sapIndex);

int mgc_crcx_chnl(WORD usrPort, CHNL chnl);
int mgc_dlcx_chnl(WORD usrPort, CHNL chnl);
int mgc_mdcx_chnl(WORD usrPort, CHNL chnl, MEDIA_PARA *mediaPara, BYTE sdFlag);
int mgc_recvonly_chnl(WORD usrPort, CHNL orgChnl, CHNL destChnl);
int mgc_rqnt_chnl(WORD usrPort, CHNL chnl, EVENT *event, SIGNAL *signal);
int mgc_auep_chnl(WORD usrPort, CHNL chnl);

int mgc_connect_chnl(WORD usrPort, CHNL orgChnl, CHNL destChnl, BYTE flag);
int mgc_connect_dtmf(WORD usrPort, CHNL orgChnl, CHNL dstChnl, BYTE dtmfNo);
int mgc_connect_tone(WORD usrPort, CHNL orgChnl, CHNL dstChnl, BYTE toneNo, WORD duration);
int mgc_connect_AAS(WORD usrPort, CHNL orgChnl, CHNL dstChnl, BYTE toneNo, WORD duration, WORD times);

int mgc_create_MG(BYTE sapIndex, MG_ATTR *mgAttr);
int mgc_delete_MG(BYTE sapIndex, WORD mgNo);
int mgc_modify_MG(BYTE sapIndex, WORD mgNo, MG_ATTR *mgAttr);

int mgc_add_port(BYTE sapIndex, WORD mgNo, BYTE portNo, BYTE portType);
int mgc_delete_port(BYTE sapIndex, WORD mgNo, BYTE portNo);
int mgc_modify_port(BYTE sapIndex, WORD mgNo, BYTE portNo, BYTE portType);

int mgc_query_mg_status(WORD mgNo);
int mgc_query_port_status(WORD mgNo, BYTE portNo);
int mgc_query_channel_status(CHNL chnl);

int mgc_create_virtual_chnl(WORD port, CHNL *chnl);
int mgc_release_virtual_chnl(WORD port, CHNL chnl);

int mgc_query_chnl_media(WORD port, CHNL chnl, PUB_SDP_MSG **mediaInfo, BYTE *mgPayloads, BYTE *mgPlNum);
int mgc_update_chnl_media(WORD port, CHNL chnl, PUB_SDP_MSG *mediaInfo);

int mgc_license_ctrl(WORD maxMgNum, WORD maxChnlNum);

int mgc_rsp(BYTE cmd, WORD usrPort, WORD port, WORD retCode, MEDIA_ATTR *mediaAttr);

int mgc_chnl_codec_update(CHNL chnl , MGC_CODEC_LIST codecList);
int mgc_query_chnl_status(CHNL chnl);
OPER_REC *mgc_query_chnl_oper_rec(CHNL chnl);    
#endif
