#
#  Copyright 2021 Google, Inc.
#
#  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("//bt/system/gd/packet/parser/packetgen.gni")
import("//common-mk/flatbuffer.gni")

config("gd_defaults") {
  cflags_cc = [
    "-DGOOGLE_PROTOBUF_NO_RTTI",
    "-Wno-unused-result",
    "-Wno-tautological-overlap-compare",
  ]

  defines = [ "TARGET_FLOSS" ]

  libs = [
    "ssl",
    "crypto",
  ]

  include_dirs = [ "//bt/system/gd" ]
  configs = [
    "//bt/system:target_defaults",
    "//bt/system/log:log_defaults",
  ]
}

group("gd_default_deps") {
  deps = [ "//bt/system/pdl:BluetoothGeneratedPackets_h" ]
}

static_library("libbluetooth_gd") {
  sources = [
    "common/audit_log.cc",
    "common/stop_watch.cc",
    "common/strings.cc",
    "common/utils.cc",
    "hal/link_clocker.cc",
    "hal/snoop_logger.cc",
    "hal/snoop_logger_file.cc",
    "hal/snoop_logger_socket.cc",
    "hal/snoop_logger_socket_thread.cc",
    "hal/syscall_wrapper_impl.cc",
    "hci/acl_manager/acl_connection.cc",
    "hci/acl_manager/acl_fragmenter.cc",
    "hci/acl_manager/acl_manager_classic_impl.cc",
    "hci/acl_manager/acl_manager_le_impl.cc",
    "hci/acl_manager/acl_scheduler.cc",
    "hci/acl_manager/classic_acl_connection.cc",
    "hci/acl_manager/le_acl_connection.cc",
    "hci/acl_manager/round_robin_scheduler.cc",
    "hci/channel_sounding/cs_metrics.cc",
    "hci/controller_impl.cc",
    "hci/distance_measurement_manager_impl.cc",
    "hci/hci_layer.cc",
    "hci/hci_metrics_logging.cc",
    "hci/le_address_manager.cc",
    "hci/le_advertising_manager_impl.cc",
    "hci/le_scanning_manager_impl.cc",
    "hci/le_scanning_reassembler.cc",
    "hci/link_key.cc",
    "hci/msft.cc",
    "hci/remote_name_request_impl.cc",
    "hci/uuid.cc",
    "lpp/lpp_offload_manager.cc",
    "storage/classic_device.cc",
    "storage/config_cache.cc",
    "storage/config_cache_helper.cc",
    "storage/device.cc",
    "storage/le_device.cc",
    "storage/legacy_config_file.cc",
    "storage/mutation.cc",
    "storage/mutation_entry.cc",
    "storage/storage_module.cc",
    "sysprops/sysprops_module.cc",
  ]

  if (use.floss_rootcanal) {
    sources += [ "hal/hci_hal_impl_host_rootcanal.cc" ]
  } else {
    sources += [ "hal/hci_hal_impl_host.cc" ]
  }

  include_dirs = [ "." ]
  configs += [
    ":gd_defaults",
    "//bt/system:target_defaults",
    "//bt/system/log:log_defaults",
  ]
  deps = [
    "//bt/flags:bluetooth_flags_c_lib",
    "//bt/floss/rust/topshim:libbluetooth_topshim",
    "//bt/sysprop:libcom.android.sysprop.bluetooth",
    "//bt/system/gd:gd_default_deps",
    "//bt/system/metrics",
    "//bt/system/pdl:BluetoothGeneratedPackets_h",
  ]

  if (target_os == "chromeos") {
    sources += [
      "metrics/chromeos/metrics.cc",
      "metrics/chromeos/metrics_allowlist.cc",
      "metrics/chromeos/metrics_event.cc",
    ]
    deps += [ "//bt/floss/android-base:android-base" ]
  } else {
    sources += [
      "metrics/linux/metrics.cc",
    ]
  }
}

static_library("libbluetooth_gd_os") {
  sources = [
    "os/handler.cc",
    "os/linux_generic/alarm.cc",
    "os/linux_generic/boottime_clock.cc",
    "os/linux_generic/files.cc",
    "os/linux_generic/mgmt.cc",
    "os/linux_generic/reactive_semaphore.cc",
    "os/linux_generic/reactor.cc",
    "os/linux_generic/repeating_alarm.cc",
    "os/linux_generic/thread.cc",
    "os/linux_generic/wakelock_manager.cc",
    "os/system_properties_common.cc",
  ]

  include_dirs = [ "." ]
  configs += [
    ":gd_defaults",
    "//bt/system:target_defaults",
    "//bt/system/log:log_defaults",
  ]

  deps = [
    "//bt/flags:bluetooth_flags_c_lib",
    "//bt/sysprop:libcom.android.sysprop.bluetooth",
    "//bt/system/gd:gd_default_deps",
  ]

  if (target_os == "chromeos") {
    sources += [
      "os/chromeos/parameter_provider.cc",
      "os/chromeos/system_properties.cc",
      "os/chromeos/wakelock_native.cc",
    ]
  } else {
    sources += [
      "os/linux/parameter_provider.cc",
      "os/linux/system_properties.cc",
      "os/linux/wakelock_native.cc",
    ]
  }
}

if (use.test) {
  executable("hci_tests") {
    sources = [ "hci/hci_packets_test.cc" ]

    include_dirs = [ "//bt/system/gd" ]

    deps = [ "//bt/system/main:bluetooth-static" ]

    configs += [
      "//bt/system:target_defaults",
      "//bt/system:external_gtest_main",
    ]

    libs = [
      "pthread",
      "rt",
      "dl",
    ]

    ldflags = [ "-lpthread" ]
  }
}
