#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 <stdint.h>
#include <stdbool.h>
#include "fds.h"
#include "nrf_fstorage.h"




#define     SC16IS750_ADDRESS_AA     (0X90)
#define     SC16IS750_ADDRESS_AB     (0X92)
#define     SC16IS750_ADDRESS_AC     (0X94)
#define     SC16IS750_ADDRESS_AD     (0X96)
#define     SC16IS750_ADDRESS_BA     (0X98)
#define     SC16IS750_ADDRESS_BB     (0X9A)
#define     SC16IS750_ADDRESS_BC     (0X9C)
#define     SC16IS750_ADDRESS_BD     (0X9E)
#define     SC16IS750_ADDRESS_CA     (0XA0)
#define     SC16IS750_ADDRESS_CB     (0XA2)
#define     SC16IS750_ADDRESS_CC     (0XA4)
#define     SC16IS750_ADDRESS_CD     (0XA6)
#define     SC16IS750_ADDRESS_DA     (0XA8)
#define     SC16IS750_ADDRESS_DB     (0XAA)
#define     SC16IS750_ADDRESS_DC     (0XAC)
#define     SC16IS750_ADDRESS_DD     (0XAE)


//General Registers
#define     SC16IS750_REG_RHR        (0x00)
#define     SC16IS750_REG_THR        (0X00)
#define     SC16IS750_REG_IER        (0X01)
#define     SC16IS750_REG_FCR        (0X02)
#define     SC16IS750_REG_IIR        (0X02)
#define     SC16IS750_REG_LCR        (0X03)
#define     SC16IS750_REG_MCR        (0X04)
#define     SC16IS750_REG_LSR        (0X05)
#define     SC16IS750_REG_MSR        (0X06)
#define     SC16IS750_REG_SPR        (0X07)
#define     SC16IS750_REG_TCR        (0X06)
#define     SC16IS750_REG_TLR        (0X07)
#define     SC16IS750_REG_TXLVL      (0X08)
#define     SC16IS750_REG_RXLVL      (0X09)
#define     SC16IS750_REG_IODIR      (0X0A)
#define     SC16IS750_REG_IOSTATE    (0X0B)
#define     SC16IS750_REG_IOINTENA   (0X0C)
#define     SC16IS750_REG_IOCONTROL  (0X0E)
#define     SC16IS750_REG_EFCR       (0X0F)

//Special Registers
#define     SC16IS750_REG_DLL        (0x00)
#define     SC16IS750_REG_DLH        (0X01)

//Enhanced Registers
#define     SC16IS750_REG_EFR        (0X02)
#define     SC16IS750_REG_XON1       (0X04)
#define     SC16IS750_REG_XON2       (0X05)
#define     SC16IS750_REG_XOFF1      (0X06)
#define     SC16IS750_REG_XOFF2      (0X07)

//
#define     SC16IS750_INT_CTS        (0X80)
#define     SC16IS750_INT_RTS        (0X40)
#define     SC16IS750_INT_XOFF       (0X20)
#define     SC16IS750_INT_SLEEP      (0X10)
#define     SC16IS750_INT_MODEM      (0X08)
#define     SC16IS750_INT_LINE       (0X04)
#define     SC16IS750_INT_THR        (0X02)
#define     SC16IS750_INT_RHR        (0X01)

//Application Related 

#define     SC16IS750_CRYSTCAL_FREQ (14745600UL) 
//#define 	SC16IS750_CRYSTCAL_FREQ (1843200UL)	  
//#define     SC16IS750_CRYSTCAL_FREQ (16000000UL)    
//#define     SC16IS750_DEBUG_PRINT   (0)
#define     SC16IS750_PROTOCOL_I2C  (0)
#define     SC16IS750_PROTOCOL_SPI  (1)



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



#if 0
void SC16IS750_ResetDevice(void);
void SC16IS750_WriteRegister(unsigned char reg_addr, unsigned char val);
unsigned char SC16IS750_ReadRegister(unsigned char reg_addr);
void SC16IS750_FIFOEnable(unsigned char fifo_enable);
int16_t SC16IS750_SetBaudrate(uint32_t baudrate);
void SC16IS750_SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length );
void SC16IS750_WriteByte(uint8_t val);
int DBGPrint(const char *fmt, ...);


void ParkSysInit();
#endif



void RegMac(char* str);
void LoadRegMac();

void SetMacAddress(uint8_t* addr);


/* 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 FlashTest();
void RegMacDelteAll();
void delete_all_begin(void);
void DisplayRegMac();
void SaveRegMac(char* mac);
int FindRegMac(char* mac);
void CarEncryptEcb(int mode);
void CarDecryptEcb(char* in, char* out);
void CarEncryptEcbACK(char type);
int AppParkingCheck(char* data);
int AppParkingCheck_IOS(char* data);



extern ble_gap_addr_t old_ble_addr;
extern ble_gap_addr_t new_ble_addr;

extern RegMacPool_t RegMacPool;


#endif
