/*
**
** function.h    
**
** Description: Header file of function definations.    
*/

#ifndef _PPS_FUNCTION__H
#define _PPS_FUNCTION__H

#define GetAsciiTime  GetASCTime

char *
NstrCrypt(char *sp);

#define LogContent(fp, buf, bcd_buf, nbytes) \
    BcdToAscii(buf, bcd_buf, 2 * nbytes); \
	FormatOutput(buf, 2*nbytes, 2, ' '); \
    LogMsg(fp, "packets contains (%s):\n%s", FUNCTION_MACRO, buf);
	
#define DisplayContent(buf, bcd_buf, nbytes) \
    BcdToAscii(buf, bcd_buf, 2 * nbytes); \
	FormatOutput(buf, 2*nbytes, 2, ' '); \
    log_debug("packets contains (%s):\n%s", FUNCTION_MACRO, buf);
	
#define BzeroContent(content, length) \
	bzero(content+length, MAX_CONTENT_LEN-length);

int InitDaemon(void);         /* Initialize a daemon process */

void 
SetTermSignal(struct termio *ptermio, void (*handler)(int));

void 
SetFaultSignal(void (*handler)(int));

void 
SetRefreshSignal(struct termio *ptermio, void (*handler)(int));

/* Initialize a daemon process */
void DaemonInit(const char *pname, int); 

/* passive a socket handler, from comfunc.c */
int passivesock( const char * transport, u_short portbase, int qlen);

/* passive a UDP socket handler, from comfunc.c */
int passiveUDP(u_short portbase);

/* Fill string 'c' into last area of 'str'.  */
void StringCat(char *str, char *c, int slen);

int ByteLength(BYTE *bcd, int c);

/* Cut string while match character c.  */
void StringCut(char *str, int c);

void Bstr2Hstr(char *cptr);

void bitstring(char *str, long byze, int biz, int strwid);

/* Binary string convert to int.  */
unsigned int Bstr2Int(char *cptr);

/*  Hex string convert to int.  */
unsigned int Hstr2Int(char *cptr);

/* Get current hour */
int gethour(char *hour_buf);
int getweek(char *week_buf);
void getdate(char *date_buf);
int getmon(char *mon_buf);
int getday(char *day_buf);
int getyear(char *year_buf);
int getmin(char *min_buf);
int getsec(char *sec_buf);

/* Get current hour */
int getmin(char * min_buf);

/* Convert data from ASCII form to left-aligned compact BCD form. */
void  AsciiToBcd (BYTE *bcd_buf, const char *ascii_buf, int len);

/* Convert data from left-aligned compact BCD form to ASCII form. */
void  BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len);

/* Convert data from ASCII form to right-aligned compact BCD form. */
void  AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len);

const char *
ppsgetdaytime(char *daytime_buf);

//char *GetASCTime(char *timep);
char *CharTime(char *ptr, const time_t *timep);

void *
ShmInit(int shm_id, char *prog_name, key_t key, int size, int shmflg);

int
SemInit(char *prog_name, key_t key, int sem_num, int sem_flg);

BOOL
SemLock(char *prog_name, int sem_id, int sem_num, 
        unsigned nsops, int sem_flg);

BOOL 
SemUnlock(char *prog_name, int sem_id, int sem_num, 
        unsigned nsops, int sem_flg);

BOOL
ReadQueueMsg(void *dest, const void *src, int max_num);

BOOL 
WriteQueueMsg(void *dest, const void *src, int max_num);

BOOL
ReadMsg(void *dest, void *src, int max_num);

BOOL 
WriteMsg(void *src, void *dest, int max_num);

BOOL 
WriteErrorLog(void *pbuf, void *perr, int max_num);

const char *
Inet_ntoa(const long addr);

const long
ppsInet_pton(const char *sp);

//const long 
//GetLocalIP(void);

char *
FormatOutput(char *sp, size_t, int, int);
	
void
ByteReverse(BYTE *p1, BYTE *p2, size_t size);

void 
SafeFree(void *pmem);

/* n is BYTE length */
void
GetAsciiField(char *dest, BYTE *src, size_t n);

#define SetAsciiField SetBcdField
void
SetBcdField(BYTE *dest, char *src, size_t n);

void
SetIntField(BYTE *dest, int src, size_t n);

int
GetIntField(int *i, BYTE *src, size_t n, int flag);

void 
StringRev(char *sp);

u_char *
ByteRev(u_char *ptr, size_t size,int flag);

time_t
MakeTime(struct tm *timep, char *ptr);

const char *
AsciiTime(char *ptr, time_t);

void
SetIntToNBO(u_char *dest, const long src);

void
SetShortToNBO(u_char *dest, const short src);

const long
GetIntFromNBO(u_char *src);

const short
GetShortFromNBO(u_char *src);

const long
ByteToInt(u_char *src, int n);

u_short
GetHour(time_t);

u_short
GetMinute(time_t);

u_short
GetTmHour();

u_short
GetTmMinute();

u_short
GetTmSecond();

u_short
GetTmDay();

u_short
GetTmMonth();

u_short
GetTmYear();

char *
GetTmDate(char *ptr);

int
AsciiToMsisdn(char *cptr,u_char *bptr,int len);

void
string2ltime(time_t *nowtime, char *timestr);

void
ppsAsciiToBcdR(u_char *bcd_buf, const char *asc_buf, int len);

void
ppsBcdToAsciiR(char *asc_buf, const u_char *bcd_buf, int len);

void
CharToTimeStamp(u_char *btime,char *atime);

void gettime(char *time_buf);
const int getnowtime(int *timebuf);
void GetVersionID(char *cbuf, u_char *bbuf);
unsigned int HstrToInt(char *cptr);

int	CutUnknownPrefix(char *number);
extern int msisdnUnkToInt(char *strIn, char *strOut);
//extern int 	EncryptPIN();
extern int	PutLogFunID();

#endif
