#ifndef __AL_BLUETOOTH_UPGRADER_H__
#define __AL_BLUETOOTH_UPGRADER_H__

#include "al_basictypes.h"
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/file.h>
#include <termios.h>
#include <time.h>
#include <sys/ioctl.h>
#include "types.h"
#include "al_bluetooth_ctrl.h"
#include "al_console.h"

//#define RDA58XX_DEBUG

#define CHAR_CR             (0x0D)  // carriage return
#define CHAR_LF             (0x0A)  // line feed

#define MUPLAY_PROMPT       "AT+MUPLAY="
#define TX_DATA_PROMPT      "AT+MURAWDATA="
#define RUSTART_PROMPT      "AT+RUSTART="

#define MUSTOP_CMD          "AT+MUSTOP="
#define MUSTOP_CMD2         "AT+MUSTOP\r\n"
#define RUSTOP_CMD          "AT+RUSTOP\r\n"
#define VOLADD_CMD          "AT+CVOLADD\r\n"
#define VOLSUB_CMD          "AT+CVOLSUB\r\n"
#define SET_VOL_CMD         "AT+CVOLUME="
#define MUPAUSE_CMD         "AT+MUPAUSE\r\n"
#define MURESUME_CMD        "AT+MURESUME\r\n"
#define SET_MIC_GAIN_CMD    "AT+MUSETMICGAIN="
#define FT_CMD              "AT+FTCODEC="
#define GET_STATUS_CMD      "AT+MUGETSTATUS\r\n"
#define GET_VERSION_CMD     "AT+CVERSION\r\n"
#define UART_MODE_CMD       "AT+CMODE=UART_PLAY\r\n"
#define BT_MODE_CMD         "AT+CMODE=BT\r\n"
#define BT_BR_MODE_CMD      "AT+BTSWITCH=0\r\n"
#define BT_LE_MODE_CMD      "AT+BTSWITCH=1\r\n"
#define GET_BT_RSSI_CMD     "AT+BTGETRSSI\r\n"
#define LEGETCONFIGSTATE_CMD "AT+LEGETCONFIGSTATE\r\n"
#define LESWIFISCS_CMD      "AT+LESWIFISCS\r\n"
#define LEGETCONFIGINFO_CMD "AT+LEGETCONFIGINFO\r\n"
#define LESETINDICATION_CMD "AT+LESETINDICATION=\r\n"
#define LESETFEATURE_CMD    "AT+LESETFEATURE=\r\n" 
#define LEGETINDSTATE_CMD   "AT+LEGETINDSTATE\r\n" 

#define RX_UNVALID_RSP      "\r\nNG\r\n"
#define RX_UNVALID_RSP2     "NG\r\n"
#define RX_VALID_RSP        "\r\nOK\r\n"
#define RX_VALID_RSP2       "OK\r\n"
#define AT_READY_RSP        "AT Ready\r\n"
#define AST_POWERON_RSP     "AST_POWERON\r\n"
#define CODEC_READY_RSP     "CODEC_READY\r\n"
#define UPDATER_RUNNING     "UPDATER_RUNNING\r\n"

#define TIMEOUT_MS 5000

typedef enum {
    D_ERROR_LEVEL = 0,
    D_WARNING_LEVEL = 1,
    D_NOTICE_LEVEL = 2,
    D_INFO_LEVEL = 3,
    D_DEBUG_LEVEL = 4,
    D_TRACE_LEVEL = 5,
}BTD_t;

typedef enum {
    UNREADY = -1,
    UPGRADE_STOP = 0,
    PLAY,
    PAUSE,
    RECORDING,
    STOP_RECORDING,
    MODE_SWITCHING
} rda58xx_status;

typedef enum {
    EMPTY = 0,
    FULL
} rda58xx_buffer_status;

typedef enum {
    NACK = 0, //No ACK
    IACK,     //Invalid ACK
    VACK,      //Valid ACK
	NEEDRESEND
} rda58xx_at_status;

typedef enum {
    UART_MODE = 0,
    BT_MODE
} rda58xx_mode;

typedef enum {
    BT_MODULE_RESET = 1,
    BT_UPDATE_PROGRESS = 2,
    BT_SYSTEM_AUDIO_CONTROL,
	BT_UPGRADE_START,
	BT_UPGRADE_DONE,
	BT_UPGRADE_FAIL,
} BTUpgradeEvent;

typedef enum {
    DISABLE_AUDIO = 1,
    ENABLE_AUDIO,
} eaudioControl;


#define codec_dbg_level D_WARNING_LEVEL

#define CODEC_LOG(level, fmt, ...)  do {\
        int dbg_level = D_##level##_LEVEL;\
        if((dbg_level <= codec_dbg_level)){\
            printf(fmt, ##__VA_ARGS__);\
        }\
    } while (0)
    
typedef struct BT_UpgradeMsg_t_ {
	al_uint32 dMessage;
	al_uint32 dParam;
} BT_UpgradeMsg_t;  
		
typedef int (*BT_Upgrade_pf) (BT_UpgradeMsg_t message);

typedef struct BT_UpgradeCfg_t_ {
	BT_Upgrade_pf pfCallBack;
} BT_UpgradeCfg_t;
	
/*******************************************************************************
 * Constant
 ******************************************************************************/
al_uint8 Al_Get_BTUpgradeFlag(void);
void rda58xx_rx_handler(al_int8 * buf,volatile unsigned int buflen);
int al_bluetooth_upgrade_start(char *pPath);
void RegisterBTUpgradeCallback(BT_UpgradeCfg_t * callback);
void BTUpgradeSendMessageToApp(al_uint32 event,al_uint32 parm);

#endif
