TARGET := stm32$(HARDWARE)


ifeq ($(DEBUG), 1)
	CFLAGS += -DDEBUG -g
endif

ifeq ($(MAC),)
	#Print default MAC
	MAC = $(shell awk '/\#define DEVICE_MAC/{print $$3}' ./Modules/settings_api.h )
else
    #Set device MAC
	CFLAGS += -DDEVICE_MAC='"$(MAC)"'
endif

ifeq ($(FLASH_ERASE), 1)
    	CFLAGS += -DFLASH_ERASE
endif

CFLAGS += -DPRINTF_$(shell echo $(PRINTF) | tr a-z A-Z)

CFLAGS += -DHARDWARE_$(shell echo $(HARDWARE) | tr a-z A-Z)

INCLUDES = -I../config
INCLUDES += -I../stm32/stm32f4xx_spl/inc 
INCLUDES += -I../stm32/system
INCLUDES += -I../peripheral_modules/inc
INCLUDES += -I../user
INCLUDES += -I../thirdparty/TinyStdio
INCLUDES += -I../thirdparty/myStrings
INCLUDES += -I.

CSRC = $(wildcard ../stm32/stm32f4xx_spl/src/*.c)
CSRC += $(wildcard ../stm32/system/*.c)
CSRC += $(wildcard ../peripheral_modules/src/*.c)
CSRC += $(wildcard ../user/*.c)
CSRC += $(wildcard ../thirdparty/TinyStdio/*.c)
CSRC += $(wildcard ../thirdparty/myStrings/*.c)
CSRC += $(wildcard *.c)

INCLUDES += -Ileds
INCLUDES += -Ibuttons
INCLUDES += -Ijumper
INCLUDES += -Id_inouts
INCLUDES += -IMegaTec
INCLUDES += -Icommon
INCLUDES += -Imonitor
INCLUDES += -Ilog
INCLUDES += -Itesting
ifeq ($(HARDWARE), bt6710)
INCLUDES += -Iportgw
INCLUDES += -Ids18b20
endif
ifneq (,$(filter $(HARDWARE),bt6709 bt6709_mts bt6711))
INCLUDES += -Iwhitelist
endif
ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt))
INCLUDES += -Iradius
endif
INCLUDES += -Imbedtls_api
CSRC += $(wildcard leds/*.c)
CSRC += $(wildcard buttons/*.c)
CSRC += $(wildcard jumper/*.c)
CSRC += $(wildcard d_inouts/*.c)
CSRC += $(wildcard MegaTec/*.c)
CSRC += $(wildcard common/*.c)
CSRC += $(wildcard monitor/*.c)
CSRC += $(wildcard log/*.c)
CSRC += $(wildcard testing/*.c)
ifeq ($(HARDWARE), bt6710)
CSRC += $(wildcard portgw/*.c)
CSRC += $(wildcard ds18b20/*.c)
endif
ifneq (,$(filter $(HARDWARE),bt6709 bt6709_mts bt6711))     
CSRC += $(wildcard whitelist/*.c)
endif
ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt))
CSRC += $(wildcard radius/*.c)
endif
CSRC += $(wildcard mbedtls_api/*.c)

CFLAGS += -DOS_FREERTOS

#FreeRTOS
LDSCRIPT := startup/stm32_flash.ld

INCLUDES +=  -I../thirdparty/FreeRTOS/include -I../thirdparty/FreeRTOS/portable/GCC/ARM_CM3
CSRC += $(wildcard ../thirdparty/FreeRTOS/*.c ../thirdparty/FreeRTOS/portable/GCC/ARM_CM3/*.c)
ASMSRC = startup/startup_stm32f4xx.s

# MemMang
CSRC += $(wildcard ../thirdparty/FreeRTOS/portable/MemMang/heap_4.c)
	
#cjson
CSRC += $(wildcard ../thirdparty/cjson/*.c)
INCLUDES += -I../thirdparty/cjson

	# LwIP + Web #	
    INCLUDES += -IHTTP_Server
    INCLUDES += -I../thirdparty/lwip_patched/src/include
    INCLUDES += -I../thirdparty/lwip_patched/src/include/lwip
    INCLUDES += -I../thirdparty/lwip_patched/src/include/netif
    INCLUDES += -I../thirdparty/lwip_patched/src/include/lwip/apps
    INCLUDES += -I../thirdparty/lwip_patched/src/apps/snmp/
#    INCLUDES += -I../thirdparty/lwip_patched/src/netif/ppp
    INCLUDES += -I../thirdparty/lwip_patched/port
    INCLUDES += -I../thirdparty/lwip_patched/port/FreeRTOS
    INCLUDES += -I../thirdparty/lwip_patched/system
    INCLUDES += -I../thirdparty/lwip_patched/system/arch
    INCLUDES += -IEthernet
 # INCLUDES += -I../stm32/stm32f4x7_ethernet 
 	INCLUDES += -ISTM32F4x7_ETH_Driver
ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt))	
   CSRC += $(wildcard HTTP_Server/my_ssl_server.c)
endif  
ifeq ($(HARDWARE), bt6710) 	
   CSRC += $(wildcard HTTP_Server/http_server.c)
endif  
ifneq (,$(filter $(HARDWARE),bt6707 bt6709 bt6709_mts bt6711))
	INCLUDES += -Icli
	CSRC += $(wildcard cli/*.c)
	INCLUDES += -ITelnet_Server
	CSRC += $(wildcard HTTP_Server/http_server.c)
	CSRC += $(wildcard Telnet_Server/*.c)
	ifneq (,$(filter $(HARDWARE),bt6709 bt6709_mts))
		# SSH
		CSRC += $(wildcard ../thirdparty/wolfssl/wolfcrypt/src/*.c) ../thirdparty/wolfssl/wolfcrypt/src/port/st/stm32.c
		INCLUDES += -I../thirdparty/wolfssl
		CFLAGS += -DWOLFSSL_LWIP -DWOLFSSL_STM32F4 -DFREERTOS -DFREERTOS_TCP -DNO_WOLFSSL_DIR -DWOLFSSL_USER_SETTINGS -DNO_FILESYSTEM -DWOLFSSL_IAR_ARM -DWOLFSSL_LEANPSK -DINLINE=inline -DNO_WOLFSSL_MEMORY -DHAVE_ECC -DHAVE_STRINGS_H -DWOLF_C99 -DWOLFSSL_NO_PEM -DHAVE_HASHDRBG -DSTM32F4 -DNO_WOLFSSL_CLIENT -DNO_DES3
		#CFLAGS += -DDEBUG_WOLFSSL
		# incompatible with SPL
		CFLAGS += -DNO_STM32_CRYPTO
		CSRC += $(wildcard ../thirdparty/wolfssh/wolfcrypt/src/*.c ../thirdparty/wolfssh/src/*.c)
		INCLUDES += -I../thirdparty/wolfssh
		CFLAGS += -DWOLFSSH_LWIP -DNO_WOLFSSH_DIR -DSINGLE_THREADED -DXMEMSET=memset -DWSTRNCPY=strncpy -DWMEMSET=memset -DWSTRLEN=strlen -DWMEMMOVE=memmove -DHAVE_DH -Dmin=min -DTINYPRINTF_OVERRIDE_LIBC
		#CFLAGS += -DDEBUG_WOLFSSH
		INCLUDES += -ISSH_Server
		CSRC += $(wildcard SSH_Server/*.c)

		# for pre-C99 compilers
		CFLAGS += -DXSTRNCASECMP=lwip_strnicmp
		# pulls strftime that requires the linker to pull lots of useless Linux calls on gcc-4.7.3
		CFLAGS += -DWOLFSSH_NO_TIMESTAMP
	endif
endif

# don't include GNU/Linux definitions of FD_* since they're provided by lwip, as seen in sys/types.h
CFLAGS += -D__USE_W32_SOCKETS

    CSRC += $(wildcard HTTP_Server/trap_params.c)
    CSRC += $(wildcard HTTP_Server/web_params_api.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/api/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/core/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/core/ipv4/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/netif/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/src/apps/snmp/*.c)
#    CSRC += $(wildcard ../thirdparty/lwip_patched/src/netif/ppp/*.c)
    CSRC += $(wildcard ../thirdparty/lwip_patched/port/FreeRTOS/*.c)
    CSRC += $(wildcard Ethernet/*.c)

  #  CSRC += $(wildcard ../stm32/stm32f4x7_ethernet/*.c)
  	CSRC += $(wildcard STM32F4x7_ETH_Driver/*.c)


CSRC += $(wildcard ../stm32/system/syscalls/syscalls.c)
#SSL
#INCLUDES += -I../thirdparty/PolarSSL/include/polarssl/
#INCLUDES += -I../thirdparty/PolarSSL/include/

#CSRC += $(wildcard ../thirdparty/PolarSSL/library/*.c)

INCLUDES += -I../thirdparty/mbedTLS/include/

CSRC += $(wildcard ../thirdparty/mbedTLS/library/*.c)

ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt))
#RADDIUS_SERVER

INCLUDES += -I../thirdparty/FreeRadius/
INCLUDES += -I../thirdparty/FreeRadius/include/
INCLUDES += -I../thirdparty/FreeRadius/lib/

CSRC += $(wildcard ../thirdparty/FreeRadius/lib/*.c)
endif
    
CFLAGS += -DUSE_STDPERIPH_DRIVER -DSTM32F40_41xxx -DLOG_ENABLE -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'

ifeq ($(HARDWARE), bt6703)
BUILDDIR = ../build/bt6703/$(TARGET)
endif

ifeq ($(HARDWARE), bt6703_rt)
BUILDDIR = ../build/bt6703_rt/$(TARGET)
endif

ifeq ($(HARDWARE), bt6707)
BUILDDIR = ../build/bt6707/$(TARGET)
endif

ifeq ($(HARDWARE), bt6709)
BUILDDIR = ../build/bt6709/$(TARGET)
endif

ifeq ($(HARDWARE), bt6709_mts)
BUILDDIR = ../build/bt6709_mts/$(TARGET)
endif

ifeq ($(HARDWARE), bt6710)
BUILDDIR = ../build/bt6710/$(TARGET)
endif

ifeq ($(HARDWARE), bt6711)
BUILDDIR = ../build/bt6711/$(TARGET)
endif

FW_FLASH_START = $(shell awk '/USER_FLASH_FIRST_PAGE_ADDRESS/{print $$3;exit}' ../config/common_config.h )
FW_FLASH_CRC = $(shell awk '/USER_FLASH_CRC_ADDRESS/{print $$3}' ../config/common_config.h )

ifeq ($(HARDWARE), bt6703)
FW_NAME = BT_6703xx
endif

ifeq ($(HARDWARE), bt6703_rt)
FW_NAME = BT_6703xx_RT
endif

ifeq ($(HARDWARE), bt6707)
FW_NAME = BT_6707xx
endif

ifeq ($(HARDWARE), bt6709)
FW_NAME = BT_6709xx
endif

ifeq ($(HARDWARE), bt6709_mts)
FW_NAME = BT_6709_MTSxx
endif

ifeq ($(HARDWARE), bt6710)
FW_NAME = BT_6710xx
endif

ifeq ($(HARDWARE), bt6711)
FW_NAME = BT_6711xx
endif

-include ../Makefile.inc.stm32

#Building Web UI FS
ifeq ($(HARDWARE), bt6710)
	WUI_DIR = ../web_interface/dist/wui-10
	FSDATA_DIR = ./HTTP_Server/bt6710_fs
endif

ifeq ($(HARDWARE), bt6711)
	WUI_DIR = ../web_interface/dist/wui-11
	FSDATA_DIR = ./HTTP_Server/bt6711_fs
endif

ifeq ($(HARDWARE), bt6707)
	WUI_DIR = ../web_interface/dist/wui-7
	FSDATA_DIR = ./HTTP_Server/bt6707_fs
endif

ifeq ($(HARDWARE), bt6709)
	WUI_DIR = ../web_interface/dist/wui-9
	FSDATA_DIR = ./HTTP_Server/bt6709_fs
endif

ifeq ($(HARDWARE), bt6709_mts)
	WUI_DIR = ../web_interface/dist/wui-9-mts
	FSDATA_DIR = ./HTTP_Server/bt6709_mts_fs
endif

ifeq ($(HARDWARE), bt6703_rt)
	WUI_DIR = ../web_interface/dist/wui-3-rt
	FSDATA_DIR = ./HTTP_Server/bt6703_rt_fs
endif

ifeq ($(HARDWARE), bt6703)
	WUI_DIR = ../web_interface/dist/wui-3
	FSDATA_DIR = ./HTTP_Server/bt6703_fs
endif

$(FSDATA_DIR)/fsdata.c: $(WUI_DIR)/*
	mkdir -p $(FSDATA_DIR)
	../docs/makefsdata.pl $(FSDATA_DIR) $(WUI_DIR)

ARTIFACTS_PATH := ~/Releases/$(HARDWARE)
RELEASE_VERSION := $(shell ../projects/gcc/tools/version.sh $(HARDWARE) $(INCLUDES))
RELEASE_PATH := $(ARTIFACTS_PATH)/$(RELEASE_VERSION)

prebuild: $(FSDATA_DIR)/fsdata.c
	@echo "Hardware version: $(HARDWARE)"
	rm -rf ../thirdparty/lwip_patched
	mkdir -p ../thirdparty/lwip_patched
	cp -R ../thirdparty/lwip/* ../thirdparty/lwip_patched
	cd ../thirdparty/lwip_patched/ && patch -p3 < ../../patches/snmp_mib2_system_patch.diff
	
postbuild: $(BUILDDIR)/$(TARGET).bin
	@echo "FW start address: $(FW_FLASH_START)"
	@echo "FW CRC address: $(FW_FLASH_CRC)"
	@echo "Copying to: $(OUTPUTDIR)/$(FW_NAME).bin"
	@cp $(BUILDDIR)/$(TARGET).bin $(OUTPUTDIR)/$(FW_NAME).bin
	$(BUILDDIR)/../../tools/cortex_crc $(OUTPUTDIR)/$(FW_NAME).bin $(FW_FLASH_START) $(FW_FLASH_CRC)
	
release:
	@echo "FW version: $(RELEASE_VERSION)"
	@mkdir -p $(RELEASE_PATH)
	@cp $(OUTPUTDIR)/iap.bin $(RELEASE_PATH)/bl_$(HARDWARE).bin
	@cp $(OUTPUTDIR)/$(FW_NAME).bin $(RELEASE_PATH)/IBP.bin
	@cp ../docs/$(HARDWARE)/*.MIB $(RELEASE_PATH)
	@vim -c "s/^/# $(RELEASE_VERSION)\r### Добавлено\r\r### Изменено\r\r### Исправлено\r\r\r\r" -c "set noswapfile" -c "3" ../docs/$(HARDWARE)/Changelog.md
	@cp ../docs/$(HARDWARE)/Changelog.md $(RELEASE_PATH)
	@echo "Compressing..."
	@cd $(RELEASE_PATH) && zip $(RELEASE_VERSION).zip IBP.bin *.MIB Changelog.md
	@echo "Done: $(RELEASE_PATH)/$(RELEASE_VERSION).zip"	
