#ifndef __COMM_H__
#define __COMM_H__

//**************************************************************
//  Copyright %G% %U% By Eaton Corporation Trucking Information
//                    All Rights Reserved
//
//  NO PART OF THIS DOCUMENT MAY BE REPRODUCED WITHOUT
//  THE WRITTEN PERMISSION OF Eaton Corporation Trucking
//  Information Services
//
//
//**************************************************************
//
//  File Name:        %M%
//  Type:             header file
//  Version:          %I%
//  Description:      Standard 'C' COMM Layer              
//                    Wrapper for C++ AppLayer 
//
//**************************************************************
#include <stdio.h>                     // standard 'C' headers   
#include <conio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <io.h>

void cls(void);
void wrt_str(unsigned char*);

// standard ASCII keyboard code definitions 
#define NUL	0		// null 
#define BEL	7		// bell tone 
#define BS	8		// back space 
#define HT	9		// horizontal tab 
#define LF	10		// line feed or newline 
#define CR	13		// carriage return 
#define ESC	27		// escape 
#define SP	32		// space 
#define DEL	127	// delete 

#define TAB	HT		// alternate name for horizontal tab 

#define INBOUND  1
#define OUTBOUND 2

#define LOG_SIZE 1024                         // for event log    
#define SRVR_NAME_SIZE 16
//  
// CommServ statistics structures
//
struct cs_stat {			// real-time out/in msgs/blks/pkts
   int oq_msgs; int oq_blks; int oq_pkts; int oq_strs;	
   int iq_msgs; int iq_blks; int iq_pkts; int iq_strs; 
};		
struct net_hist {			// historical for msgs,pkts,and calls
   int tq_msgs; int ts_msgs; int tf_msgs;	// total msgs queued/sent/failed
   int tiq_pkts; int toq_pkts; int ts_pkts; // total pkts queued(int/out)/sent
   int tr_pkts; int td_pkts;				// total pkts re-queued/duplicates
   int tq_call; int tf_call; int tc_call;	// total calls made/failed/cleared 
   int t_hangup; int t_listen; int t_reset;	// totals in hangups and listens
   int t_misc;								// plan to expand ...
};
enum eNetHist { NH_INIT, NH_SAVE, NH_Q_MSG, NH_S_MSG, NH_F_MSG,  
				NH_IQ_PKT, NH_OQ_PKT, NH_S_PKT, NH_R_PKT, NH_D_PKT,
				NH_T_CALL, NH_F_CALL, NH_C_CALL,
				NH_T_HANGUP, NH_T_LISTEN, NH_T_RESETS };
#define NH_ARRAY 24

//
// function prototypes
//

int CommProc(void);
int CommStat(cs_stat *);
void CommNetHist(int iVar);

int CommInit(void);
int CommClear(void);

int CommQueue(char *szDestName, char *szFileName, int iUrgency, int iType, long lMsgId);
int CommRecv(char *szOrigName, char *szFileName, int* iFileFlag=NULL);
int CommMsgStat(int iDirection, int iMsgId);
int CommDelete(int iDirection, int iMsgId);

int CommTime(time_t *lCurrTime);
#if defined (_PLA) || defined (_ALL)
int CommPos(float *lLatitude, float *lLongitude, long *lFixTime=NULL, int *Age=NULL);
#else
int CommPos(float *lLatitude, float *lLongitude);
#endif
int CommNewUser(char* szUserName);
int CommMapUser(char* szUserName, char* szNodeName, int iClearFlag);
int CommCheckUser(char* szName);
int CommCheckNode(char* szName);
int CommGetNode(char* szAppName, char* szNodeName);
int CommSetTmo(time_t lVal, char *szSrvrName);
int CommGetTmo(time_t *, char *szSrvrName);
int CommNetStat(int* iRSSI=NULL, int* iBatLev=NULL, char* szMAN=NULL, 
                int* iBaseId=NULL, int* iAreaId=NULL);	
int CommGetDsr(char* szPort=NULL);                  // defaults to COM1
void CommSetMode(int iLibMode);
int CommGetMode(void);
const char* CommErrMsg(int iErr);

//
//  Used with diagnostic version of SCM library
//
#if (defined (_SCM) || defined (_ALL)) && defined SCM_TEST
struct SCMInfo {
	char szOpMode[24];
	char szSyncState[8];
	float fSigLevel;
	float fEbNo;
	char szGPSState[8];
	float fLongitude;
	float fLatitude;
	time_t FixTime;
	time_t LastRcvdTime;	  // time rcvd anything from SCM 
	char szGPSPosStatus[6];
	int iGroup;								 
	int iSatelliteId;
	int iBeamId;
};
struct USERInfo {
	char szOrigPrsName[SRVR_NAME_SIZE];		// Vehicle ID
	char szOrigNetName[SRVR_NAME_SIZE];		// FTIN(SCM)
	char szDestNetName[SRVR_NAME_SIZE];		// APP_SEL (SCM)
};
int CommPollScm(int iPollingRatio);
int CommGetScmInfo(SCMInfo *ScmInfo);
int CommGetUserInfo(USERInfo *UserInfo);
#endif // _SCM 

int rd_parms(void);                                 // read comm parms
int rd_line(char* buf, FILE* fp, const int max);    // read one line of a flat file
void comm_log(char *format, ...); 
long ahtol(char* sHexStrg, int iLen);


//Message Status Codes

#define	CS_QUEUED		0 			// ‘Queued’ `	- Transmission has not started yet
#define	CS_INVALID		1			// ‘Invalid’	- Message was addressed to an invalid destination
#define	CS_SENDING		2			// ‘Sending’	- Transmission of messages has started
#define	CS_SENT			3			// ‘Sent’ 		- Message has been successfully sent
#define	CS_FAILED		4			// ‘Failed’ 	- Message transmission has failed
#define	CS_UNREAD		5			// ‘UnRead’ 	- Newly received message
#define	CS_READ			6			// ‘Read’	 	- Previously read inbound message (CommRecv)
#define	CS_PROCERR		7			// ‘Procerr’ 	- Internal I/O error occurred during processing
#define	CS_OUT			8			// ‘Out’ 		- Message has been transmitted 									//				  (awaiting acknowledgement)

//Function Return Codes - General

#define	CS_NOT_READY	-10		    // Service not ready
#define	CS_INV_PARM		-9 		    // Invalid calling parameter
#define	CS_INV_CFG		-8			// Invalid configuration file
#define	CS_CFG_IO_ERR	-7			// File I/O error in reading configuration file
#define	CS_OPEN_ERR		-6			// File I/O error in opening the specified file
#define	CS_DEL_ERR		-5			// File I/O error in deleting requested message
#define	CS_READ_ERR		-4			// Unable to process request - File read error
#define	CS_WRITE_ERR	-3			// Unable to process request - File write error
#define	CS_MEM_ERR		-2			// Unable to process  request - Out of memory
#define	CS_NO_OPP		-1			// Request resulted in no action being taken
#define	CS_SUCCESS		0			// Request completed successfully
 
//Function Return Codes - Comm Port Specific 

#define	CS_PRI_PORT_INIT_ERR	-50	// Unable to initialize primary communications port
#define	CS_SEC_PORT_INIT_ERR	-51	// Unable to initialize secondary communications port
#define	CS_PRI_PORT_FAILURE	    -52	// Failure on primary communications port
#define	CS_SEC_PORT_FAILURE	    -53	// Failure on secondary communications port

//Function Return Codes - Device Specific 

#define	CS_PRI_DEV_INIT_ERR	    -170	// Unable to initialize primary equipent
#define	CS_SEC_DEV_INIT_ERR	    -171	// Unable to initialize secondary equipent
#define	CS_PRI_DEV_NET_ERR	    -172	// Primary device reporting network problems
#define	CS_SEC_DEV_NET_ERR	    -173	// Secondary device reporting network problems
#define	CS_PRI_DEV_HW_ERR		-174	// Primary device reporting hardware problems
#define	CS_SEC_DEV_HW_ERR		-175	// Secondary device reporting hardware problems
#define	CS_NO_VC_FOUND		    -176	// Send failed due to no virtual circuit estab'd

//Function Return Codes - Network Specific

#define	CS_PRI_NET_INIT_ERR	    -290	// Unable to connect to primary network service provider
#define	CS_SEC_NET_INIT_ERR 	-291	// Unable to connect to secondary network service provider
#define	CS_PRI_NET_DEL_ERR   	-292	// Primary network service provider reporting delivery problems
#define	CS_SEC_NET_DEL_ERR 	    -293	// Secondary network service provider reporting delivery problems

//Library Operational Modes

#define CS_LIB_NORMAL			0		// Normal operating mode - send&recv packets
#define CS_OUT_ONLY				1		// Send Only - do not Recv or ACK packets
#define CS_IN_ONLY				2		// Recv Only - do not accept new Msgs to send

#endif
