@echo off
rem The process of generating DFU files

set  name="yingying"
set  BLE_DFU_ENANBLE_flag=0
set  BleNameHoldOn_ENANBLE_flag=0
set  BleNameHoldOn_ENANBLE_flag=0
set  RTT_DEBUG_EN_flag=0
set  USE_FIFO_FLAG=0

echo=
echo **********************Ready To  generate DFU bootloader zip FILE*****************
echo=

cd ..
rem KEIL directory
set UV=C:\Keil_v5\UV4\UV4.exe
rem Current directory
set Front_PRO_PATH=%cd%
rem Project directory
set UV_PRO_PATH=%Front_PRO_PATH%\pca10040\s132\arm5_no_packs\ble_app_uart_c_pca10040_s132.uvprojx
rem Project output directory
set BUILD_HEX_NAME=%Front_PRO_PATH%\pca10040\s132\arm5_no_packs\_build\nrf52832_xxaa.hex
rem Directory of file names for DFU enabled in the current project
set USR_CONFIG_PACH=%Front_PRO_PATH%\system\usr_config.h
rem Directory to store dfu files and settings
set DFU_CONFIG_FILE_NAME=%cd%\dfu_readme
rem bootloader name
set BOOTLOADER_NAME=%DFU_CONFIG_FILE_NAME%\central_and_peripheral_bootloader.hex
rem Generate the name of the DFU file
set DFU_FILE_NAME=%DFU_CONFIG_FILE_NAME%\bootloader_dfu_package.zip

rem Clear previous DFU files and compiled HEX files
if exist %DFU_FILE_NAME% del %DFU_FILE_NAME%
if exist %BUILD_HEX_NAME% del %BUILD_HEX_NAME% 

findstr /c:"BleNameHoldOn_ENANBLE 1" %USR_CONFIG_PACH%>nul
if %errorlevel% equ 0 set /a BleNameHoldOn_ENANBLE_flag=1

findstr /c:"BLE_DFU_ENANBLE 1" %USR_CONFIG_PACH%>nul
if %errorlevel% equ 0 set /a BLE_DFU_ENANBLE_flag=1

findstr /c:"JS_RTT_DEBUG_EN 1" %USR_CONFIG_PACH%>nul
if %errorlevel% equ 0 set /a RTT_DEBUG_EN_flag=1

findstr /c:"USEFIFO 1" %USR_CONFIG_PACH%>nul
if %errorlevel% equ 0 set /a USE_FIFO_FLAG=1

if %BLE_DFU_ENANBLE_flag% equ 1 (
   if %BleNameHoldOn_ENANBLE_flag% equ 0 (
      echo=
      echo Compile firmware in progress ----------------
      echo=
      %UV% -j0 -b %UV_PRO_PATH% -l %cd%\build_log.txt
      if exist %BUILD_HEX_NAME% (
         nrfutil pkg generate --hw-version 52 --bootloader-version 0x03 --bootloader %BOOTLOADER_NAME% --sd-req 0xcb --key-file .\dfu_readme\private.key %DFU_FILE_NAME% 
         echo=
         echo file generated successfully
         echo=
         echo ================Firmware information============================
         echo | set /p="hardware version "
         findstr "\<HARDWARE_VERSION" %USR_CONFIG_PACH%
         echo | set /p="Software version "
         findstr "\<SOFTWARE_VERSION" %USR_CONFIG_PACH%
         if %RTT_DEBUG_EN_flag% equ 1 (
            echo RTT waveform display is on! ! ! ! ! ! !
         )
         if %USE_FIFO_FLAG% equ 1 (
            echo BLE transmission, using FIFO transmission
         )
         echo | set /p="Standby Power Interval(ms) "
         findstr /c:"#define StandByPower_Interval" %USR_CONFIG_PACH%
         echo | set /p="Heartbeat interval (ms) "
         findstr /c:"#define HeartTime_Interval" %USR_CONFIG_PACH%
         echo | set /p="Low Power Interval(ms) "
         findstr /c:"#define LowPower_Interval" %USR_CONFIG_PACH%
         echo ===================================================
         echo=
         pause
      )
   ) 
) 

   echo !!!!!!!!!Failed to compile the project!!!!!!!!!!!!
   echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   if %BLE_DFU_ENANBLE_flag% equ 0 (
      echo The project does not enable the firmware upgrade function, please confirm
   ) 
   if %BleNameHoldOn_ENANBLE_flag% equ 1 (
      echo The project has enabled the fixed function of the bluetooth name, please confirm
   ) 
   echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo=
pause