# AvaotaF1 客户端 Makefile
# 交叉编译配置 - 32位 RISC-V

# ===== 工具链配置 =====
# 开发板使用 musl libc 1.2.4，必须用 musl 工具链编译
# 【开发者注意】请将 Tina SDK 放置到以下路径，或修改此变量
SDK_ROOT := /home/rongye/ProgramFiles/TinaSDK/tina-v821-release

# 切换工具链：设置 USE_MUSL=1 使用 musl 工具链（推荐）
USE_MUSL := 1

ifeq ($(USE_MUSL),1)
    # musl 工具链（与开发板兼容）
    # 注意：musl 工具链在 prebuilt 目录，不是 out/toolchain
    TOOLCHAIN_DIR := $(SDK_ROOT)/prebuilt/rootfsbuilt/riscv/nds32le-linux-musl-v5d/bin
    CROSS_COMPILE := riscv32-linux-musl-
    $(info [INFO] Using musl toolchain for board compatibility)
else
    # glibc 工具链（仅用于对比测试，开发板不支持）
    TOOLCHAIN_DIR := $(SDK_ROOT)/out/toolchain/nds32le-linux-glibc-v5d/bin
    CROSS_COMPILE := riscv32-unknown-linux-
    $(warning [WARNING] Using glibc toolchain - will NOT run on board!)
endif

CC  := $(TOOLCHAIN_DIR)/$(CROSS_COMPILE)gcc
CXX := $(TOOLCHAIN_DIR)/$(CROSS_COMPILE)g++
AR  := $(TOOLCHAIN_DIR)/$(CROSS_COMPILE)ar
STRIP := $(TOOLCHAIN_DIR)/$(CROSS_COMPILE)strip

# ===== 目标配置 =====
TARGET := avaota_client
SRC_DIR := .
BUILD_DIR := ../build
OBJ_DIR := $(BUILD_DIR)/obj
BIN_DIR := $(BUILD_DIR)/bin

# ===== 源文件 =====
SRCS := $(SRC_DIR)/main.cpp \
        $(SRC_DIR)/camera/camera.cpp \
        $(SRC_DIR)/audio/audio_capture.cpp \
        $(SRC_DIR)/audio/audio_player.cpp \
        $(SRC_DIR)/imu/icm42688.cpp \
        $(SRC_DIR)/network/ws_client.cpp \
        $(SRC_DIR)/network/udp_sender.cpp \
        $(SRC_DIR)/utils/logger.cpp
# Day 14: 移除 HTTP TTS 相关文件，改用 WebSocket TTS

OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))

# ===== 编译选项 =====
CXXFLAGS := -std=c++14 -O2 -Wall -Wextra
CXXFLAGS += -I$(SRC_DIR)

# 系统头文件
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include

# eyesee-mpp 头文件
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/eyesee-mpp/middleware/include
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/eyesee-mpp/middleware/include/media
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/eyesee-mpp/middleware/include/utils
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/eyesee-mpp/middleware/media/include/component
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/eyesee-mpp/system/public/include

# libisp 头文件
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/include
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/include/device
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/include/V4l2Camera
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/isp_dev
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/isp_tuning
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libisp/isp_manage

# libcedarc 头文件 (视频编解码器 - 添加所有子目录)
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libcedarc
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libcedarc/include
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libcedarc/base
CXXFLAGS += -I$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/include/libcedarc/base/include

# 定义芯片类型
CXXFLAGS += -DAWCHIP=AW_V821

# ===== 库路径与链接 =====
# 使用静态链接（与 test_camera 相同的配置）
LDFLAGS := -static
LDFLAGS += -L$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/lib
LDFLAGS += -L$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/lib/eyesee-mpp

# 开始静态库组 - 顺序很重要！
LDFLAGS += -Wl,--start-group

# 核心 MPP 库
LDFLAGS += -law_mpp -lmedia_utils -lawion -lexpat

# ISP 库 (摄像头图像处理)
LDFLAGS += -lISP -lisp_dev -lisp_ini -liniparser
LDFLAGS += -lisp_ae -lisp_af -lisp_afs -lisp_awb -lisp_base
LDFLAGS += -lisp_gtm -lisp_iso -lisp_math -lisp_md -lisp_pltm -lisp_rolloff

# 视频编码器
LDFLAGS += -lvencoder -lvenc_base -lvenc_common -lvenc_jpeg -lvenc_h264
LDFLAGS += -lMemAdapter -lVE

# 音频处理库
LDFLAGS += -ladecoder -lResample -lAudioVps
LDFLAGS += -lwav -laac
LDFLAGS += -lcedarx_aencoder -laacenc
LDFLAGS += -lAgc -lAec -lAns

# Muxer (文件封装)
LDFLAGS += -lmuxers -lmp4_muxer -lraw_muxer
LDFLAGS += -lmpeg2ts_muxer -laac_muxer -lmp3_muxer -lwav_muxer
LDFLAGS += -lffavutil -lFsWriter -lcedarxstream

# Demuxer (文件解析)
LDFLAGS += -lcedarxdemuxer -lcdx_parser -lcdx_stream -lcdx_file_stream
LDFLAGS += -lcdx_aac_parser -lcdx_id3v2_parser -lcdx_mov_parser
LDFLAGS += -lcdx_mp3_parser -lcdx_ts_parser -lcdx_wav_parser

# 视频解码器 (MJPEG)
LDFLAGS += -lvdecoder -lvideoengine -lawmjpegplus
LDFLAGS += -lcedarx_tencoder

# 配置文件解析
LDFLAGS += -lPluginMpp -lIniParserMpp -lsample_confparser

# Cedar 核心库（包含CDC_LOG_LEVEL_NAME等符号）
LDFLAGS += -lcdc_base -lcdx_base

# 显示库
LDFLAGS += -lcedarxrender -lhwdisplay

# ALSA 音频库（静态库不存在，且音频播放已禁用）
# LDFLAGS += -lasound

# OpenSSL (用于 WebSocket 握手)
LDFLAGS += -lssl -lcrypto

# 结束静态库组
LDFLAGS += -Wl,--end-group

# 系统动态库
# 指定正确的动态链接器路径，避免开发板上需要手动创建 /lib32/ld.so.1 符号链接
LDFLAGS += -Wl,--dynamic-linker=/lib/ld-musl-riscv32.so.1
LDFLAGS += -Wl,-Bdynamic -lasound -lpthread -lm -lrt -ldl -lz -static-libstdc++

# ===== 目标规则 =====
.PHONY: all clean install test_imu test_gpio test_camera

all: $(BIN_DIR)/$(TARGET)

$(BIN_DIR)/$(TARGET): $(OBJS)
	@mkdir -p $(BIN_DIR)
	$(CXX) $(OBJS) $(LDFLAGS) -o $@
	@echo "Build complete: $@"
	@echo "Strip binary..."
	$(STRIP) $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -c $< -o $@

clean:
	rm -rf $(BUILD_DIR)
	@echo "Clean complete"

install:
	@echo "Installing to target device..."
	scp $(BIN_DIR)/$(TARGET) root@192.168.1.100:/usr/bin/
	@echo "Install complete"

# ===== 测试程序 =====

# test_imu 只需要基本库
TEST_IMU_LDFLAGS := -static -lpthread -lm -lstdc++

test_imu: $(OBJ_DIR)/test_imu.o $(OBJ_DIR)/imu/icm42688.o $(OBJ_DIR)/utils/logger.o
	@mkdir -p $(BIN_DIR)
	$(CXX) $^ $(TEST_IMU_LDFLAGS) -o $(BIN_DIR)/$@
	@echo "Build complete: $(BIN_DIR)/$@"
	@echo "Strip binary..."
	$(STRIP) $(BIN_DIR)/$@

# test_gpio 测试 GPIO 输出
test_gpio: $(OBJ_DIR)/test_gpio.o
	@mkdir -p $(BIN_DIR)
	$(CXX) $^ $(TEST_IMU_LDFLAGS) -o $(BIN_DIR)/$@
	@echo "Build complete: $(BIN_DIR)/$@"
	@echo "Strip binary..."
	$(STRIP) $(BIN_DIR)/$@

# test_camera 测试摄像头 (需要 MPP 库)
TEST_CAMERA_LDFLAGS := -static
TEST_CAMERA_LDFLAGS += -L$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/lib
TEST_CAMERA_LDFLAGS += -L$(SDK_ROOT)/out/v821/avaota_f1/openwrt/staging_dir/target/usr/lib/eyesee-mpp

# 静态库组 - 顺序很重要！
TEST_CAMERA_LDFLAGS += -Wl,--start-group

# 核心 MPP 库
TEST_CAMERA_LDFLAGS += -law_mpp -lmedia_utils -lawion -lexpat

# ISP 库 (摄像头图像处理)
TEST_CAMERA_LDFLAGS += -lISP -lisp_dev -lisp_ini -liniparser
TEST_CAMERA_LDFLAGS += -lisp_ae -lisp_af -lisp_afs -lisp_awb -lisp_base
TEST_CAMERA_LDFLAGS += -lisp_gtm -lisp_iso -lisp_math -lisp_md -lisp_pltm -lisp_rolloff

# 视频编码器
TEST_CAMERA_LDFLAGS += -lvencoder -lvenc_base -lvenc_common -lvenc_jpeg -lvenc_h264
TEST_CAMERA_LDFLAGS += -lMemAdapter -lVE

# 音频处理库
TEST_CAMERA_LDFLAGS += -ladecoder -lResample -lAudioVps
TEST_CAMERA_LDFLAGS += -lwav -laac
TEST_CAMERA_LDFLAGS += -lcedarx_aencoder -laacenc
TEST_CAMERA_LDFLAGS += -lAgc -lAec -lAns

# Muxer (文件封装)
TEST_CAMERA_LDFLAGS += -lmuxers -lmp4_muxer -lraw_muxer
TEST_CAMERA_LDFLAGS += -lmpeg2ts_muxer -laac_muxer -lmp3_muxer -lwav_muxer
TEST_CAMERA_LDFLAGS += -lffavutil -lFsWriter -lcedarxstream

# Demuxer (文件解析)
TEST_CAMERA_LDFLAGS += -lcedarxdemuxer -lcdx_parser -lcdx_stream -lcdx_file_stream
TEST_CAMERA_LDFLAGS += -lcdx_aac_parser -lcdx_id3v2_parser -lcdx_mov_parser
TEST_CAMERA_LDFLAGS += -lcdx_mp3_parser -lcdx_ts_parser -lcdx_wav_parser

# 视频解码器 (MJPEG)
TEST_CAMERA_LDFLAGS += -lvdecoder -lvideoengine -lawmjpegplus
TEST_CAMERA_LDFLAGS += -lcedarx_tencoder

# 配置文件解析
TEST_CAMERA_LDFLAGS += -lPluginMpp -lIniParserMpp -lsample_confparser

# Cedar 核心库
TEST_CAMERA_LDFLAGS += -lcdc_base -lcdx_base

# 显示库
TEST_CAMERA_LDFLAGS += -lcedarxrender -lhwdisplay

TEST_CAMERA_LDFLAGS += -Wl,--end-group

# 系统动态库
TEST_CAMERA_LDFLAGS += -Wl,-Bdynamic -lasound -lglog -llog -lpthread -lm -lstdc++ -lrt -ldl -lz

test_camera: $(OBJ_DIR)/test_camera.o $(OBJ_DIR)/camera/camera.o
	@mkdir -p $(BIN_DIR)
	$(CXX) $^ $(TEST_CAMERA_LDFLAGS) -o $(BIN_DIR)/$@
	@echo "Build complete: $(BIN_DIR)/$@"
	@echo "Strip binary..."
	$(STRIP) $(BIN_DIR)/$@

# ===== 帮助 =====
help:
	@echo "AvaotaF1 Client Makefile"
	@echo "------------------------"
	@echo "Targets:"
	@echo "  all         - Build the application (default)"
	@echo "  clean       - Remove build artifacts"
	@echo "  install     - Deploy to device via SCP"
	@echo "  test_imu    - Build IMU test program"
	@echo "  test_gpio   - Build GPIO test program"
	@echo "  test_camera - Build camera test program"
	@echo ""
	@echo "Before build, ensure:"
	@echo "  1. SDK_ROOT points to Tina SDK"
	@echo "  2. Toolchain is available"
	@echo "  3. SDK has been compiled"
