# SPDX-FileCopyrightText: 2020-2021 Project CHIP Authors
#
# SPDX-License-Identifier: Apache-2.0
#
# SPDX-FileContributor: 2022-2024 Espressif Systems (Shanghai) PTE LTD
#
# Modified the original code to support external platform on ESP32 SoCs.
#
# For original code and license information, please refer to
# https://github.com/project-chip/connectedhomeip

# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/buildconfig_header.gni")
import("${chip_root}/src/lib/core/core.gni")
import("${chip_root}/src/platform/device.gni")

declare_args() {
  # By default use default/example implementation of CommissionableDataProvider,
  # DeviceAttestationCredentialsProvider and DeviceInstanceInfoProvider
  chip_use_transitional_commissionable_data_provider = true
  chip_use_transitional_device_instance_info_provider = true
  chip_use_factory_data_provider = false
  chip_use_device_info_provider = false
  chip_config_software_version_number = 0
  chip_enable_chipoble = false
  chip_enable_ble_controller = false
  chip_bt_nimble_enabled = false
  chip_bt_bluedroid_enabled = false
  chip_max_discovered_ip_addresses = 5
  chip_enable_route_hook = false
  chip_memory_alloc_mode = "default"
}

buildconfig_header("custom_buildconfig") {
  header = "CHIPDeviceBuildConfig.h"
  header_dir = "platform"

  defines = [
    "CHIP_DEVICE_CONFIG_ENABLE_WPA=false",
    "CHIP_ENABLE_OPENTHREAD=${chip_enable_openthread}",
    "CHIP_DEVICE_CONFIG_THREAD_FTD=${chip_openthread_ftd}",
    "OPENTHREAD_CONFIG_ENABLE_TOBLE=false",
    "CHIP_BYPASS_RENDEZVOUS=false",
    "CHIP_STACK_LOCK_TRACKING_ENABLED=true",
    "CHIP_STACK_LOCK_TRACKING_ERROR_FATAL=false",
    "CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING=false",
    "CHIP_DEVICE_CONFIG_RUN_AS_ROOT=false",
    "CHIP_DEVICE_LAYER_TARGET_ESP32=1",
    "CHIP_DEVICE_LAYER_TARGET=ESP32_custom",
    "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=1",
    "CHIP_USE_TRANSITIONAL_DEVICE_INSTANCE_INFO_PROVIDER=1",
    "CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE=${chip_enable_chipoble}",
    "BLE_PLATFORM_CONFIG_INCLUDE=<platform/ESP32_custom/BlePlatformConfig.h>",
    "CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<platform/ESP32_custom/CHIPDevicePlatformConfig.h>",
    "CHIP_PLATFORM_CONFIG_INCLUDE=<platform/ESP32_custom/CHIPPlatformConfig.h>",
    "INET_CONFIG_INCLUDE=<platform/ESP32_custom/InetPlatformConfig.h>",
    "SYSTEM_PLATFORM_CONFIG_INCLUDE=<platform/ESP32_custom/SystemPlatformConfig.h>",
    "EXTERNAL_CONFIGURATIONMANAGERIMPL_HEADER=<platform/ESP32_custom/ConfigurationManagerImpl.h>",
    "EXTERNAL_CHIPDEVICEPLATFORMEVENT_HEADER=<platform/ESP32_custom/CHIPDevicePlatformEvent.h>",
    "EXTERNAL_CONNECTIVITYMANAGERIMPL_HEADER=<platform/ESP32_custom/ConnectivityManagerImpl.h>",
    "EXTERNAL_BLEMANAGERIMPL_HEADER=<platform/ESP32_custom/BLEManagerImpl.h>",
    "EXTERNAL_KEYVALUESTOREMANAGERIMPL_HEADER=<platform/ESP32_custom/KeyValueStoreManagerImpl.h>",
    "EXTERNAL_PLATFORMMANAGERIMPL_HEADER=<platform/ESP32_custom/PlatformManagerImpl.h>",
    "EXTERNAL_THREADSTACKMANAGERIMPL_HEADER=<platform/ESP32_custom/ThreadStackManagerImpl.h>",
    "EXTERNAL_ESP32UTILS_HEADER=<platform/ESP32_custom/ESP32Utils.h>",
    "EXTERNAL_ESP32OTAIMAGEPROCESSORIMPL_HEADER=<platform/ESP32_custom/OTAImageProcessorImpl.h>",
    "EXTERNAL_ESP32DEVICEINFOPROVIDER_HEADER=<platform/ESP32_custom/ESP32DeviceInfoProvider.h>",
    "EXTERNAL_ESP32FACTORYDATAPROVIDER_HEADER=<platform/ESP32_custom/ESP32FactoryDataProvider.h>",
    "EXTERNAL_ESP32SECURECERTDACPROVIDER_HEADER=<platform/ESP32_custom/ESP32SecureCertDACProvider.h>",
    "EXTERNAL_ESP32SECURECERTDATAPROVIDER_HEADER=<platform/ESP32_custom/ESP32SecureCertDataProvider.h>",
    "CHIP_CONFIG_SOFTWARE_VERSION_NUMBER=${chip_config_software_version_number}",
    "CHIP_DEVICE_CONFIG_MAX_DISCOVERED_IP_ADDRESSES=${chip_max_discovered_ip_addresses}",
  ]

  if (chip_enable_ota_requestor) {
    defines += [ "CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR=1" ]
  }
}

group("platform_buildconfig") {
  public_deps = [
    ":custom_buildconfig",
  ]
}

static_library("ESP32_custom") {
  sources = [
    "${chip_root}/src/platform/SingletonConfigurationManager.cpp",
    "CHIPDevicePlatformConfig.h",
    "CHIPDevicePlatformEvent.h",
    "ConfigurationManagerImpl.cpp",
    "ConfigurationManagerImpl.h",
    "ConnectivityManagerImpl.cpp",
    "ConnectivityManagerImpl.h",
    "DiagnosticDataProviderImpl.cpp",
    "DiagnosticDataProviderImpl.h",
    "ESP32Config.cpp",
    "ESP32Config.h",
    "ESP32Utils.cpp",
    "ESP32Utils.h",
    "KeyValueStoreManagerImpl.cpp",
    "KeyValueStoreManagerImpl.h",
    "Logging.cpp",
    "PlatformManagerImpl.cpp",
    "PlatformManagerImpl.h",
    "SystemTimeSupport.cpp",
  ]

  deps = [
    "${chip_root}/src/lib/dnssd:platform_header",
    "${chip_root}/src/platform/logging:headers",
    "${chip_root}/src/setup_payload",
  ]

  public = [
    "${chip_root}/src/credentials/CHIPCert.h",
    "${chip_root}/src/credentials/DeviceAttestationCredsProvider.h",
  ]

  public_deps = [
    "${chip_root}/src/crypto",
    "${chip_root}/src/platform:platform_base",
    ":platform_buildconfig",
  ]

  if (chip_config_memory_management == "platform") {
    if (chip_memory_alloc_mode == "internal") {
      sources += [ "CHIPMem-PlatformInternal.cpp" ]
    } else if (chip_memory_alloc_mode == "external") {
      sources += [ "CHIPMem-PlatformExternal.cpp" ]
    } else {
      sources += [ "CHIPMem-PlatformDefault.cpp" ]
    }
  }

  if (chip_enable_ota_requestor) {
    sources += [
      "OTAImageProcessorImpl.cpp",
      "OTAImageProcessorImpl.h",
    ]
  }

  if (chip_enable_ble) {
    sources += ["BLEManagerImpl.h"]

    if (chip_enable_ble_controller) {
      sources += [ "ChipDeviceScanner.h" ]
    }

    if (chip_bt_nimble_enabled) {
      sources += [ "nimble/BLEManagerImpl.cpp" ]
      if (chip_enable_ble_controller) {
        sources += [
          "nimble/ChipDeviceScanner.cpp",
          "nimble/blecent.h",
          "nimble/misc.c",
          "nimble/peer.c",
        ]
      }
    }

    if (chip_bt_bluedroid_enabled) {
      sources += [ "bluedroid/BLEManagerImpl.cpp" ]
      if (chip_enable_ble_controller) {
        sources += [ "bluedroid/ChipDeviceScanner.cpp" ]
      }
    }
  }


  if (chip_enable_wifi) {
    sources += [
      "ConnectivityManagerImpl_WiFi.cpp",
      "NetworkCommissioningDriver.cpp",
      "NetworkCommissioningDriver.h",
    ]
  }
  if (chip_mdns == "platform") {
    sources += [
      "DnssdImpl.cpp",
    ]
  }

  if (chip_use_factory_data_provider) {
    sources += [
      "ESP32FactoryDataProvider.cpp",
      "ESP32FactoryDataProvider.h",
    ]
  }

  if (chip_use_device_info_provider) {
    sources += [
      "ESP32DeviceInfoProvider.cpp",
      "ESP32DeviceInfoProvider.h",
    ]
  }

  if (chip_enable_ethernet || chip_enable_wifi) {
    if (chip_mdns == "platform") {
      sources += [
        "ESP32DnssdImpl.cpp",
        "ESP32DnssdImpl.h",
      ]
    }
    if (chip_mdns == "minimal") {
      sources += [ "ESP32EndpointQueueFilter.h" ]
    }
    if (chip_enable_route_hook) {
      sources += [
        "route_hook/ESP32RouteHook.c",
        "route_hook/ESP32RouteHook.h",
        "route_hook/ESP32RouteTable.c",
        "route_hook/ESP32RouteTable.h",
      ]
    }
  }

  if (chip_enable_openthread) {
    sources += [
      "${chip_root}/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp",
      "${chip_root}/src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h",
      "${chip_root}/src/platform/OpenThread/OpenThreadUtils.cpp",
      "OpenthreadLauncher.cpp",
      "OpenthreadLauncher.h",
      "ThreadStackManagerImpl.cpp",
      "ThreadStackManagerImpl.h",
    ]
    if (chip_mdns == "platform") {
      sources += [
        "${chip_root}/src/platform/OpenThread/OpenThreadDnssdImpl.cpp",
        "${chip_root}/src/platform/OpenThread/OpenThreadDnssdImpl.h",
      ]
    }
    configs -= [ "${chip_root}/build/config/compiler:warnings_default" ]
  }
}
