/*
 * Module_PowerSharing.h
 *
 *  Created on: 2020/12/07
 *      Author: foluswen
 */

#ifndef MODULE_POWERSHARING_H_
#define MODULE_POWERSHARING_H_

#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/types.h>
#include 	<sys/ioctl.h>
#include 	<sys/socket.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<linux/wireless.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 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>
#include 	<ifaddrs.h>
#include 	<sys/types.h>
#include 	<sys/socket.h>
#include 	<netinet/in.h>
#include 	<netdb.h>
#include 	<error.h>
#include 	<signal.h>
#include	"define.h"
#include 	"main.h"

//#define DEBUG
#define is_error(ptr) 				((unsigned long)ptr > (unsigned long)-4000L)
#define ARRAY_SIZE(A)				(sizeof(A) / sizeof(A[0]))
#define PASS						1
#define FAIL			   			-1
#define ON							1
#define OFF							0
#define YES							1
#define NO							0
#define true			    		1
#define false						0

#define LISTEN_PORT_TCP				118
#define	CONNECTION_LIMIT			5

#define ShmPowerShargingKey			LISTEN_PORT_TCP+8000

enum ROTARY_SWITCH_LIMIT
{
	SWITCH_0_DEBUG=0,
	SWITCH_1_12A,
	SWITCH_2_16A,
	SWITCH_3_20A,
	SWITCH_4_24A,
	SWITCH_5_28A,
	SWITCH_6_32A,
	SWITCH_7_36A,
	SWITCH_8_40A,
	SWITCH_9_48A,
	SWITCH_A_56A,
	SWITCH_B_64A,
	SWITCH_C_72A,
	SWITCH_D_80A,
	SWITCH_E_RESERVE,
	SWITCH_F_SLAVE
};

enum SHARING_COMMAND
{
	SHARING_CMD_GET_STATUS=0x01,
	SHARING_CMD_SET_CAPACITY=0x02,
	SHARING_CMD_CONNECTION_FULL=0xfd,
	SHARING_CMD_CHKSUM_ERROR=0xfe,
	SHARING_CMD_UNKNOWN=0xff
};

struct Message
{
	int			size;
	uint8_t		buffer[2048];
};

struct CONNECTION_INFO
{
	int 		socketFd;					// Socket file description
	uint16_t	availableSharingCurrent;	// Each connection available sharing current, resolution: 1A
	uint16_t	presentOutputCurrent;		// Each connection preset output current, resolution: 1A
	uint8_t		acPhase;					// Each connection ac power phase
	uint16_t	SOC;						// Each connection preset SOC, resolution: 0.1%
	uint16_t	remindTime;					// Each connection remind charging time, resolution: 1min
	time_t		lastHeartBeatTime;			// Each connection latest get heart beat start time
	time_t		lastGetStatusTime;			// Each connection latest get status start time
	time_t		lastSetCapacityTime;		// Each connection latest set capacity start time
	time_t		lastCheckCapacityTime;		// Each connection latest check capacity start time
	uint8_t		isSocketConnected:1;		// Each connection socket connected flag
	uint8_t		isGunConnected:1;			// Each connection gun connected flag
};

struct POWER_SHARING
{
	uint8_t					connectedQty;
	uint16_t				onHandCurrent;
	struct CONNECTION_INFO	Connection_Info[CONNECTION_LIMIT];
	uint8_t					hasNewConn:1;
};

#endif /* MODULE_POWERSHARING_H_ */