# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2026 the OpenRSP authors
#
# This file is part of OpenRSP.
#
# OpenRSP is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# OpenRSP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with OpenRSP. If
# not, see <https://www.gnu.org/licenses/>.

# OpenRSP Net for SoapySDR: the SoapySDR driver "openrsp_net", a module built against the
# SoapySDR installed here (SoapySDR loads only modules of its own ABI version).
#
# The client (protocol, TLS, decoding) is the one the SDR++ source uses: OPENRSP_NET_CLIENT_DIR
# holds its client/ and net/ sources. OPENRSP_DIR is the OpenRSP tree (or the unpacked source zip)
# with the API headers (include/), the IQ codec (third_party/openrsp-iq-codec/) and the RSP LNA state
# table (android/common/rsplna/). TLS is Mbed
# TLS, fetched at a pinned release and linked statically.

cmake_minimum_required(VERSION 3.16)
project(openrsp_net_soapy C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release)
endif ()

set(OPENRSP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." CACHE PATH "OpenRSP tree with include/ and third_party/openrsp-iq-codec/")
set(OPENRSP_NET_CLIENT_DIR "${OPENRSP_DIR}/sdrpp/src" CACHE PATH "Directory holding the OpenRSP Net client's client/ and net/")
if (NOT EXISTS "${OPENRSP_DIR}/include/sdrplay_api.h" OR NOT EXISTS "${OPENRSP_DIR}/third_party/openrsp-iq-codec/openrsp_iq_codec.hpp")
    message(FATAL_ERROR "OPENRSP_DIR (${OPENRSP_DIR}) has no include/sdrplay_api.h and third_party/openrsp-iq-codec/")
endif ()
if (NOT EXISTS "${OPENRSP_DIR}/android/common/rsplna/rsp_lna.h")
    message(FATAL_ERROR "OPENRSP_DIR (${OPENRSP_DIR}) has no android/common/rsplna/rsp_lna.h")
endif ()
if (NOT EXISTS "${OPENRSP_NET_CLIENT_DIR}/client/client.cpp")
    message(FATAL_ERROR "OPENRSP_NET_CLIENT_DIR (${OPENRSP_NET_CLIENT_DIR}) has no client/client.cpp")
endif ()

find_package(SoapySDR CONFIG REQUIRED)

if (NOT MSVC)
    add_compile_options("-ffile-prefix-map=${CMAKE_BINARY_DIR}=build" "-ffile-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=soapy"
                        "-ffile-prefix-map=${OPENRSP_DIR}=openrsp")
endif ()

include(FetchContent)
if (POLICY CMP0169)
    cmake_policy(SET CMP0169 OLD)
endif ()
if (POLICY CMP0135)
    cmake_policy(SET CMP0135 NEW)
endif ()

# --- Mbed TLS -------------------------------------------------------------------
set(ENABLE_PROGRAMS OFF CACHE BOOL "" FORCE)
set(ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(MBEDTLS_FATAL_WARNINGS OFF CACHE BOOL "" FORCE)
set(USE_STATIC_MBEDTLS_LIBRARY ON CACHE BOOL "" FORCE)
set(USE_SHARED_MBEDTLS_LIBRARY OFF CACHE BOOL "" FORCE)
set(DISABLE_PACKAGE_CONFIG_AND_INSTALL ON CACHE BOOL "" FORCE)
set(GEN_FILES OFF CACHE BOOL "" FORCE)
FetchContent_Declare(mbedtls
    URL https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-3.6.7/mbedtls-3.6.7.tar.bz2
    URL_HASH SHA256=a7e8bcbec0e6f761b4af24f25677626b35f762f68eef79c08677a363212d11f6)
FetchContent_GetProperties(mbedtls)
if (NOT mbedtls_POPULATED)
    FetchContent_Populate(mbedtls)
    add_subdirectory(${mbedtls_SOURCE_DIR} ${mbedtls_BINARY_DIR} EXCLUDE_FROM_ALL)
endif ()

# --- the client -----------------------------------------------------------------
add_library(openrsp_net_client STATIC
    ${OPENRSP_NET_CLIENT_DIR}/net/tls_conn.cpp
    ${OPENRSP_NET_CLIENT_DIR}/net/trust.cpp
    ${OPENRSP_NET_CLIENT_DIR}/net/http.cpp
    ${OPENRSP_NET_CLIENT_DIR}/net/websocket.cpp
    ${OPENRSP_NET_CLIENT_DIR}/net/mdns.cpp
    ${OPENRSP_NET_CLIENT_DIR}/client/link.cpp
    ${OPENRSP_NET_CLIENT_DIR}/client/wire.cpp
    ${OPENRSP_NET_CLIENT_DIR}/client/client.cpp)
target_include_directories(openrsp_net_client PUBLIC
    ${OPENRSP_NET_CLIENT_DIR}
    ${OPENRSP_DIR}/include
    ${OPENRSP_DIR}/third_party/openrsp-iq-codec)
target_link_libraries(openrsp_net_client PUBLIC mbedtls mbedx509 mbedcrypto)
if (WIN32)
    target_compile_definitions(openrsp_net_client PUBLIC NOMINMAX WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS)
    if (MSVC)
        target_compile_options(openrsp_net_client PUBLIC /EHsc /utf-8)
    endif ()
    target_link_libraries(openrsp_net_client PUBLIC ws2_32 crypt32 bcrypt iphlpapi)
elseif (APPLE)
    target_link_libraries(openrsp_net_client PUBLIC "-framework Security" "-framework CoreFoundation")
else ()
    target_link_libraries(openrsp_net_client PUBLIC pthread)
endif ()

# --- the SoapySDR module --------------------------------------------------------
SOAPY_SDR_MODULE_UTIL(
    TARGET openrsp_netSupport
    SOURCES src/SoapyOpenRspNet.cpp
    LIBRARIES openrsp_net_client)
target_include_directories(openrsp_netSupport PRIVATE ${OPENRSP_DIR}/android/common/rsplna)
if (NOT WIN32 AND NOT APPLE)
    target_link_options(openrsp_netSupport PRIVATE -Wl,--exclude-libs,ALL)
endif ()
