#ifndef _SYSMGR_H_
#define _SYSMGR_H_

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "ble_gap.h"

#include "fds.h"
#include "nrf_fstorage.h"



struct stSystemTimer__ {
	volatile int MS_1;
        volatile int UART_LAST_RECV_TIMER;
        volatile int SEND_TEST_TIMER;
        volatile int LED_TOGGLE_TIMER;
        volatile int TMR_SYS_OFF;
        volatile int SEND_TIMER;
        volatile int BUZZER_TIMER;
        char EMG_SEND_RUN;
		volatile int TIM_KEY;
		volatile int SLEEP_TIMER;
};
typedef struct stSystemTimer__ stSystemTimer;




struct stSystemManager__ {

  char EmgSendButton;
  char TestSendButton;
  char RxEvent;
  char SendRepeat;
  char SendFlag;
  int  BleConnectTime;
  int  SendRepeatTimerLimit;
  char BuzzerType;
  char BuzzerBusy;
  unsigned char Batt;

  char BleSendReady;
  char BleSendStatus;


  char BleConnected;
  char BleSendRetry;
  char PeerAddr[6];
  char StrPeerAddr[15];
  char RegPeerAddr[6];
  char StrRegPeerAddr[15];



  char SysReady;
  char SleepEnter;

  char FWUpdate;
  
  char GpReg;
  char GoSleep;  
  uint32_t ResetReason;


	char AesEncData[16];


};
typedef struct stSystemManager__ stSystemManager;




#define AES_ENC_CAR		1
#define AES_ENC_EMG		2
#define AES_ENC_PCA		3
#define AES_ENC_WES_EMG	4
#define AES_ENC_WES_ALV	5
#define AES_ENC_WES_TST	6
#define AES_ENC_WES_REG	7


/* A dummy structure to save in flash. */

#define CONFIG_FILE     (0x8010)
#define CONFIG_REC_KEY  (0x7010)


typedef struct
{
    uint32_t boot_count;
    char     device_name[16];
    bool     config1_on;
    bool     config2_on;
} configuration_t;



typedef struct
{
    char Mac[10][15];	
    int Count;
}RegMacPool_t;







void ParkSysInit();
void ParkPowerOff();

void SendEmgData(void);
extern stSystemTimer SystemTimer;
extern stSystemManager SystemManager;







#define KEY_SELECT		0
#define KEY_UP			1
#define KEY_DOWN		3
#define KEY_BACK		2




typedef struct tsHSKey{
	unsigned char onFlag;
	unsigned char softRelease;
	unsigned char pressedSec;
	unsigned int  pressTime;
	unsigned int  longPressTimeOne;
	unsigned int  longPressTimeKeep;
	unsigned int  prevTime;


}HSKey;






void ReadKEY(void);
void KeyInit(void);




void KeySelectLongPressedOne(void);


void m_standby_counter_clerar();
void PortInputCfg(void);


void lfclk_config(void);
void rtc_config(void);

void SaveReceiverMac(char* mac);
void LoadReceiverMac();
void FlashTest();


uint8_t Load_Flash();
void Save_Flash(uint8_t data);



void WesEncryptEcb(uint8_t* in);
void WesDecryptEcb(char* in, char* out);



extern ble_gap_addr_t old_ble_addr;
extern ble_gap_addr_t new_ble_addr;

extern RegMacPool_t RegMacPool;

#endif
