/*
 * Sample_OCPP_Task.h
 *
 *  Created on: 2020�~5��26��
 *      Author: foluswen
 */

#ifndef HEADER_MODULE_OCPPBACKEND_H_
#define HEADER_MODULE_OCPPBACKEND_H_

#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.h>
#include 	<linux/sockios.h>
#include 	<linux/socket.h>
#include 	<arpa/inet.h>
#include 	<netinet/in.h>
#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
#include    <fcntl.h>
#include    <termios.h>
#include 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include 	<pthread.h>
#include 	<mcheck.h>
#include 	<uuid/uuid.h>

#include 	<libwebsockets.h>
#include 	<lws_config.h>
#include	<sqlite3.h>
#include	<json-c/json.h>

#include	"hashmap.h"
#include    "SystemLogMessage.h"
#include    "MessageHandler.h"

#define is_error(ptr) 				((unsigned long)ptr > (unsigned long)-4000L)
#define PASS						1
#define FAIL						-1

#ifndef SPEC_LATEST_SUPPORTED
	#define SPEC_LATEST_SUPPORTED 	13
#endif

// Hash map operation constant
#define HASH_OP_ADD		0
#define HASH_OP_GET		1
#define HASH_OP_REMOVE	2

// OCPP Message type constant
#define MESSAGE_TYPE_CALL			2
#define MESSAGE_TYPE_CALLRESULT		3
#define MESSAGE_TYPE_CALLERROR		4

// Queue operation constant
#define QUEUE_OPERATION_SHOWQUEUE	0
#define QUEUE_OPERATION_SHOWFRONT	1
#define QUEUE_OPERATION_DEL			2
#define QUEUE_OPERATION_SENT		3
#define QUEUE_OPERATION_ADD			4
#define QUEUE_OPERATION_STORE		5

extern void CheckSystemValue(void);
extern int FirstHeartBeatResponse(void);
extern void OCPP_get_TableAuthlocalAllData(void);
extern int TransactionMessageAttemptsGet(void);
extern int TransactionMessageRetryIntervalGet(void);
extern int GetOcppConnStatus(void);
extern void SetOcppConnStatus(uint8_t status);
extern int GetHeartBeatWithNOResponse(void);
extern void SetHeartBeatWithNOResponse(void);

extern int InitShareMemory();
extern int ProcessShareMemory();
extern void CheckSystemValue(void);

extern int showfront(char *uuid, char *data);
extern int addq(char *uuid, char *data) ;
extern int delq();
extern int sentqueue();
extern void CheckTransactionPacket(char *uuid);
extern int queue_operation(int type, char *frontUUID, char *frontData);

extern char *random_uuid( char buf[37]);
extern void work(char s[]);
extern char* strchr(const char *p, int ch);
extern void splitstring(char *src, const char *separator, char **dest,int *num);
extern char* stringtrim( char * s );
extern char* stringtrimspace( char * s );
extern char * strtrim( char * s );


extern struct lws 					*wsi_client;
extern struct lws_context 			*context;
extern unsigned char 				SendBuffer[4096];
extern int 							SendBufLen;
extern char 						OcppPath[160];
extern char 						OcppProtocol[10];
extern char 						OcppHost[50];
extern char 						OcppTempPath[50];
extern int 							OcppPort;
extern unsigned char 				StartTransactionIdTagTemp[20];
extern pthread_mutex_t 				lock_send;
extern uint32_t						startTimeDog;
extern uint32_t						startTimeQueue;
extern uint8_t						isWebsocketSendable;
extern uint8_t						counterLwsRestart;
#endif /* HEADER_MODULE_OCPPBACKEND_H_ */