# Host-side tests for obu-firmware. See README.md: needs gcc and make on PATH (MSYS2 UCRT64), and
# asn1tools under `py -3.11` for the replay check.
#
#   make                      build and run everything: chain, replay, fuzz
#   make chain | replay | fuzz | battery   one of them
#   make fuzz FUZZ_ITER=50000000 FUZZ_SEED=7   a longer or different fuzz run
#   make clean                remove build/
#
# The firmware sources are compiled straight from ../../main, never copied.

CC          = gcc
CXX         = g++
PYTHON      = python
PYTHON_ASN1 = py -3.11
FW          = ../../main
BUILD       = build
EXE         = $(if $(filter Windows_NT,$(OS)),.exe,)
RECORDINGS  = $(wildcard ../../../its-g5-receiver-firmware/recordings/*.pcap)
FUZZ_ITER   = 2000000
FUZZ_SEED   = 1

# -Werror: these sources must stay warning-free on the host compiler too.
# UBSan in trap mode needs no runtime library, so it works on MinGW; a trap shows up as a crash.
CFLAGS = -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Werror -I$(FW) -I. \
         -fsanitize=undefined -fsanitize-undefined-trap-on-error

FW_SRCS = $(FW)/geonet.c dot11p.c $(FW)/gn_unwrap.c
DEPS    = test_util.c test_util.h dot11p.h $(FW_SRCS) $(wildcard $(FW)/*.h)

.PHONY: all check chain replay fuzz battery firmware corpus clean

all: check

check: chain replay fuzz battery firmware

$(BUILD):
	mkdir -p $@

$(BUILD)/%$(EXE): %.c $(DEPS) | $(BUILD)
	$(CC) $(CFLAGS) -o $@ $< test_util.c $(FW_SRCS)

# The pcap goes through pcap_gn_tally.py as a second, independent parser of the same frames.
chain: $(BUILD)/test_chain$(EXE)
	$(BUILD)/test_chain$(EXE) $(BUILD)/test_chain.pcap
	$(PYTHON) ../pcap_gn_tally.py $(BUILD)/test_chain.pcap

replay: $(BUILD)/test_replay$(EXE)
ifeq ($(RECORDINGS),)
	@echo "replay: no recordings in ../../../its-g5-receiver-firmware/recordings, skipped"
else
	$(BUILD)/test_replay$(EXE) $(BUILD)/replay.tsv $(RECORDINGS)
	$(PYTHON_ASN1) check_replay.py $(BUILD)/replay.tsv $(RECORDINGS)
endif

fuzz: $(BUILD)/fuzz_gn_unwrap$(EXE)
	$(BUILD)/fuzz_gn_unwrap$(EXE) $(FUZZ_ITER) $(FUZZ_SEED) $(RECORDINGS)

# Battery policy: C++17, so its own flags.
CXXFLAGS = -std=c++17 -O2 -g -Wall -Wextra -Wpedantic -Werror -I$(FW)

$(BUILD)/test_battery$(EXE): test_battery.cpp $(FW)/battery_policy.hpp | $(BUILD)
	$(CXX) $(CXXFLAGS) -o $@ test_battery.cpp

battery: $(BUILD)/test_battery$(EXE)
	$(BUILD)/test_battery$(EXE)

clean:
	rm -rf $(BUILD)

# Unit tests of the firmware logic that needs no ESP32: link codec against the app's bytes, request
# reassembly, V2X_RX, radio / BLE-guard / clock / battery policies. Prints PASS or FAIL per test and a RESULT line.
$(BUILD)/fw_c_%.o: $(FW)/%.c | $(BUILD)
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD)/fw_c_dot11p.o: dot11p.c dot11p.h | $(BUILD)
	$(CC) $(CFLAGS) -c -o $@ $<

FW_CXX_SRCS = $(FW)/link_protocol.cpp $(FW)/tx_policy.cpp $(FW)/its_clock.cpp

$(BUILD)/test_firmware$(EXE): test_firmware.cpp $(FW_CXX_SRCS) $(wildcard $(FW)/*.hpp) $(BUILD)/fw_c_geonet.o $(BUILD)/fw_c_gn_unwrap.o $(BUILD)/fw_c_dot11p.o | $(BUILD)
	$(CXX) $(CXXFLAGS) -std=c++20 -I. -o $@ test_firmware.cpp $(FW_CXX_SRCS) $(BUILD)/fw_c_geonet.o $(BUILD)/fw_c_gn_unwrap.o $(BUILD)/fw_c_dot11p.o

firmware: $(BUILD)/test_firmware$(EXE)
	$(BUILD)/test_firmware$(EXE)

# Corpus for the app's GnUnwrapTest (the Kotlin port of gn_unwrap.c): recorded frames, cut and corrupted copies of
# them, with the verdict and fields of the firmware's own code. Writes ../../../app/src/test/resources/gn_unwrap_corpus.tsv
CORPUS_OUT = ../../../app/src/test/resources/gn_unwrap_corpus.tsv

$(BUILD)/dump_corpus$(EXE): dump_corpus.c test_util.c test_util.h $(FW)/gn_unwrap.c $(FW)/gn_unwrap.h | $(BUILD)
	$(CC) $(CFLAGS) -o $@ dump_corpus.c test_util.c $(FW)/gn_unwrap.c

corpus: $(BUILD)/dump_corpus$(EXE)
	mkdir -p $(dir $(CORPUS_OUT))
	$(BUILD)/dump_corpus$(EXE) $(CORPUS_OUT) $(RECORDINGS)
