cmake_minimum_required(VERSION 3.16)

if(NOT IDF_TARGET)
  set(IDF_TARGET "esp32s3" CACHE STRING "ESP-IDF target")
endif()

set(EXTRA_COMPONENT_DIRS
    "${CMAKE_CURRENT_LIST_DIR}/../../components"
    "${CMAKE_CURRENT_LIST_DIR}/../../knx"
    "${CMAKE_CURRENT_LIST_DIR}/../../../dali_cpp"
)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(gateway_app)

# CHIP defaults its BLE endpoint pool to one even when NimBLE accepts more
# connections. The gateway service consumes a GAP connection without creating
# a CHIPoBLE endpoint, but CHIP still counts that connection when deciding
# whether commissioning advertising can remain connectable. Keep both targets
# on the same four-connection capacity so Matter and one gateway client can be
# active together.
if(CONFIG_GATEWAY_MATTER_SUPPORTED AND CONFIG_ENABLE_CHIPOBLE)
  idf_component_get_property(esp_matter_lib espressif__esp_matter COMPONENT_LIB)
  idf_component_get_property(gateway_ble_lib gateway_ble COMPONENT_LIB)
  target_compile_definitions(
      ${esp_matter_lib} PRIVATE
      BLE_LAYER_NUM_BLE_ENDPOINTS=${CONFIG_BT_NIMBLE_MAX_CONNECTIONS})
  target_compile_definitions(
      ${gateway_ble_lib} PRIVATE
      BLE_LAYER_NUM_BLE_ENDPOINTS=${CONFIG_BT_NIMBLE_MAX_CONNECTIONS})
endif()

# CONFIG_ENABLE_ETHERNET_TELEMETRY is ESP-Matter's switch for the CHIP
# Ethernet connectivity implementation. Its component also compiles the
# internal-EMAC commissioning driver unconditionally, even when that driver is
# disabled. ESP32-S3 has no internal EMAC and this gateway already owns its
# W5500, so omit only that unused source from the managed component target.
if(CONFIG_ENABLE_ETHERNET_TELEMETRY AND
   NOT CONFIG_ETHERNET_NETWORK_COMMISSIONING_DRIVER)
  idf_component_get_property(esp_matter_lib espressif__esp_matter COMPONENT_LIB)
  get_target_property(esp_matter_sources ${esp_matter_lib} SOURCES)
  list(FILTER esp_matter_sources EXCLUDE REGEX
       "/NetworkCommissioningDriver_Ethernet\\.cpp$")
  set_property(TARGET ${esp_matter_lib} PROPERTY SOURCES ${esp_matter_sources})
endif()

# ESP-Matter 1.5.1 still compiles this integration unit when
# CONFIG_CUSTOM_NETWORK_CONFIG has removed the Network Commissioning cluster.
# The unused unit then rejects a configuration with no CHIP commissioning
# driver. The gateway commissions Wi-Fi out-of-band via its long-press
# SmartConfig path, so omit the integration together with the cluster.
if(CONFIG_CUSTOM_NETWORK_CONFIG)
  idf_component_get_property(esp_matter_lib espressif__esp_matter COMPONENT_LIB)
  get_target_property(esp_matter_sources ${esp_matter_lib} SOURCES)
  list(FILTER esp_matter_sources EXCLUDE REGEX
       "/network_commissioning_integration\\.cpp$")
  set_property(TARGET ${esp_matter_lib} PROPERTY SOURCES ${esp_matter_sources})
endif()
