/* 
** 
**
** file name: 
**
** header file include by IP transceiver. 
** defination of shared memory structure.
**
*/

#ifndef IPTRANS__H
#define IPTRANS__H

#include    "../../../public/src/include/includes.h"
#include    "../../../public/src/include/public.h"
#include    "../../../public/src/include/pub_fmt.h"
#include    "errlog.h"
#include    "debug.h"

#ifndef NO_CONFIG_H
#include    "config.h"
#endif

#define IN_LIST_NUM     200                     /* list number of in_buffer that 
                                                can store in the buffer space
                                                */
#define DS_LIST_NUM     IN_LIST_NUM
#define OUT_LIST_NUM    1024                    /* list number of out_buffer that 
                                                  can store in the buffer space
                                                */
#define PORT_IN_NUM     24                      /* number of in port defined */
#define PORT_DS_NUM     1                       /* number of port to database sync process */
#define PORT_OUT_NUM    1                       /* number of out port defined */ 
#define PORT_NUM        PORT_IN_NUM             /* pre-defined port number */

/* Port service */
#define    PORT_BASE_NUM               4950     /* minimum port number */
#define    MAX_CONTENT_LEN             1514

/* IP transceiver shared memory ipc key and permission */
#define    IPTRANS_SHM_KEY        0x60000000 
#define    IPTRANS_SHM_PERM       0777
#define    IPTRANS_DEBUG_SHM      0x61610000
#define    IPTRANS_DEBUG_PERM     0664

/* IP transceiver semaphore ipc key and permission */
#define    RELEASE                1    /* release semaphore */
#define    ACQUIRE                -1   /* acquire semaphore */
#define    MAX_STATISTICS_SEG     96

#define _msg_list  message_list
typedef struct message_list {
    long            msgSrcIP;            /* ip number of source host */
    u_short         msgSrcPort;          /* port of source host */
    long            msgDstIP;            /* ip number of Destination host */
    u_short         msgDstPort;          /* port of Destination host */
    int             msgBroadcast;        /* message sent with broadcast mode,
                                            0(default)/1=no/yes.
                                         */
    u_short         msgLength;           /* message's length */
    BYTE            msgContent[MAX_CONTENT_LEN];     /* message of packet received */
} message_list;

typedef struct ds_msg {
    long            msgSrcIP;            /* ip number of source host */
    u_short         msgSrcPort;          /* port of source host */
    long            msgDstIP;            /* ip number of Destination host */
    u_short         msgDstPort;          /* port of Destination host */
    int             msgBroadcast;        /* message sent with broadcast mode,
                                            0(default)/1=no/yes.
                                         */
    u_short         msgLength;           /* message's length */
    char            msgContent[MAX_CONTENT_LEN];     /* message of packet received */
} db_msg;

typedef struct ds_buffer {
    u_short         msgReadSub;      /* subscript of array msgList for reader */
    u_short         msgWriteSub;     /* subscript of array msgList for writer */
    db_msg          msgList[DS_LIST_NUM];    /* message list buffer */
} ds_buffer;

#define _in_buffer   in_buffer
typedef struct in_buffer {
    u_short         msgReadSub;      /* subscript of array msgList for reader */
    u_short         msgWriteSub;     /* subscript of array msgList for writer */
    message_list    msgList[IN_LIST_NUM];         /* message list buffer */
} in_buffer;

#define _out_buffer   out_buffer
typedef struct out_buffer {
    u_short         msgReadSub;      /* subscript of array msgList for reader */
    u_short         msgWriteSub;     /* subscript of array msgList for writer */
    message_list    msgList[OUT_LIST_NUM];         /* message list buffer */
} out_buffer;

#define _port_stat   port_stat
typedef struct port_stat {
     
    short           portStatus;         /* port status:
                                           0=idle 1=normal -1=abnormal
                                        */
    u_long          inPackets;          /* number of packets received */
    u_long	        inBytes;
    u_long          outPackets;         /* number of packets sent */
    u_long	        outBytes;
    u_long          errorPackets;     /* number of error packets received */
} port_stat;

/*
#define _port_stat  port_stat
typedef struct port_stat{
    port_stat_15m    port_stat_static[PORT_NUM];
}port_stat;
*/

typedef struct debug_info
{
    DWORD	debug_status_id[20];
	BYTE    debug_status;
	DWORD	debug_ascin_id[20];
	DWORD	debug_ascout_id[20];
	DWORD	debug_name_id[20];
	DWORD	debug_page_title[20];
	DWORD	debug_page_line[20];
	BYTE    title0_p[1024];
	BYTE	title1_p[1024];
	BYTE    title2_p[1024];
	BYTE    title3_p[1020];
	char    iptr_asciin_buf[4096];
	char    iptr_asciout_buf[4096];
    BYTE    port_onoff[PORT_NUM];
	BYTE    port_status[PORT_NUM];  //overflow
    BYTE    resettimer[4];
    u_long  logflag;
	u_long  srclog;
	u_long  dstlog;
}debug_info;


typedef struct port_configure
{
   BYTE    port_config[PORT_NUM];
}port_configure;

typedef struct iptrans_cdr_rec
{
   u_long   inPackets;
   u_long   inBytes;
   u_long   outPackets;
   u_long   outBytes;
   u_long   errorPackets;
}iptrans_cdr_rec;

typedef struct iptrans_cdr_period
{
   iptrans_cdr_rec  cdr_record[PORT_NUM];
}iptrans_cdr_period;

typedef struct snmp_info
{
   BYTE   process_conf;
   BYTE   version[3];
   //port_configure port_conf;
   //PORT STATUS   ; debug_info.port_status
   iptrans_cdr_period  iptrans_cdr[96];
}snmp_info;

#define _iptrans_shm iptrans_shm
typedef struct iptrans_shm {              /* type defination of IP transceiver
                                             shared memory 
                                          */
    in_buffer       msgPortIn[PORT_NUM];  /* message in_buffer defination, 
                                           a port buffer attach only a 
                                           application module.
                                          */
    out_buffer      msgPortOut;
	out_buffer	    msgPlatPortOut;
} iptrans_shm;

typedef struct _iptrans_status
{
	port_stat       msgPortStat[PORT_NUM];
	debug_info      debug_msg;
	snmp_info       snmp_data;	
}iptrans_status;

int iptrans_init();
void iptrans_timer();
int iptrMainInit();
int iptrGetMessage(message_list *msgGet, u_short port);
int iptrDirectRecvMsg(message_list *msgGet, u_short port);
int iptrPutMessage(message_list msgPut);
int iptrDirectSendMsg(message_list msgPut);
int iptrPlatPutMessage(message_list msgPut);
int set_iptrans(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD datalen);
int get_iptrans(BYTE oidlen, DWORD *oid, BYTE *pdata, WORD *vartype);
int trap_iptrans(BYTE oidlen, DWORD *oid_ptr, BYTE *msg_ptr, BYTE msg_len);
int CloseTcpConn();
BOOL iptrSemLock(char *prog_name, int sem_id, int sem_num, unsigned nsops, int sem_flg);
BOOL iptrWriteMsg(void *pbuf, void *pmsg, int max_num);
BOOL iptrSemUnlock(char *prog_name, int sem_id, int sem_num, unsigned nsops, int sem_flg);
BOOL iptrReadMsg(void *pbuf, void *pmsg, int max_num);
int InitSingleSocket(int portno);
int mcast_join(int sockfd, struct sockaddr *sa, socklen_t salen, const char *ifname, u_int ifindex);

#endif   /* IPTRANS__H */
