From 812459651f0078e54033c1933214db8c2a1cc9e7 Mon Sep 17 00:00:00 2001
From: Boris Zbarsky <bzbarsky@apple.com>
Date: Wed, 26 Jul 2023 10:40:11 -0400
Subject: [PATCH] Factor out secure channel type definitions into a separate
 static library. (#28248)

This allows us to get rid of circular includes.
---
 src/credentials/BUILD.gn              |  1 +
 src/lib/format/BUILD.gn               |  1 +
 src/messaging/BUILD.gn                |  1 +
 src/protocols/BUILD.gn                |  6 +++++
 src/protocols/secure_channel/BUILD.gn | 35 +++++++++++++++------------
 src/setup_payload/BUILD.gn            |  1 +
 src/transport/BUILD.gn                |  1 +
 7 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/src/credentials/BUILD.gn b/src/credentials/BUILD.gn
index da66062ad93dc2..badfeaf6e4e4e7 100644
--- a/src/credentials/BUILD.gn
+++ b/src/credentials/BUILD.gn
@@ -92,6 +92,7 @@ static_library("credentials") {
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/platform",
+    "${chip_root}/src/protocols:type_definitions",
     "${nlassert_root}:nlassert",
   ]
 }
diff --git a/src/lib/format/BUILD.gn b/src/lib/format/BUILD.gn
index c75ecd084e1457..b593d88cf59d7a 100644
--- a/src/lib/format/BUILD.gn
+++ b/src/lib/format/BUILD.gn
@@ -64,6 +64,7 @@ source_set("protocol-decoder") {
     ":tlv-metadata-headers",
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
+    "${chip_root}/src/protocols:type_definitions",
   ]
 
   public_configs = [ "${chip_root}/src:includes" ]
diff --git a/src/messaging/BUILD.gn b/src/messaging/BUILD.gn
index d5e51fc0f61056..3c64ef64ebd3f1 100644
--- a/src/messaging/BUILD.gn
+++ b/src/messaging/BUILD.gn
@@ -72,6 +72,7 @@ static_library("messaging") {
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/platform",
+    "${chip_root}/src/protocols/secure_channel:type_definitions",
     "${chip_root}/src/transport",
     "${chip_root}/src/transport/raw",
   ]
diff --git a/src/protocols/BUILD.gn b/src/protocols/BUILD.gn
index ed5b9dd94e3cd9..10b48fb7e46cc3 100644
--- a/src/protocols/BUILD.gn
+++ b/src/protocols/BUILD.gn
@@ -14,6 +14,10 @@
 
 import("//build_overrides/chip.gni")
 
+source_set("type_definitions") {
+  sources = [ "Protocols.h" ]
+}
+
 static_library("protocols") {
   output_name = "libChipProtocols"
 
@@ -35,6 +39,7 @@ static_library("protocols") {
 
   public_deps = [
     ":im_status",
+    ":type_definitions",
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/messaging",
@@ -52,6 +57,7 @@ static_library("im_status") {
   cflags = [ "-Wconversion" ]
 
   public_deps = [
+    ":type_definitions",
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
   ]
diff --git a/src/protocols/secure_channel/BUILD.gn b/src/protocols/secure_channel/BUILD.gn
index 8cf46f9d334a1f..93997d0642b37f 100644
--- a/src/protocols/secure_channel/BUILD.gn
+++ b/src/protocols/secure_channel/BUILD.gn
@@ -1,5 +1,24 @@
 import("//build_overrides/chip.gni")
 
+static_library("type_definitions") {
+  output_name = "libSecureChannelTypes"
+
+  sources = [
+    "Constants.h",
+    "StatusReport.cpp",
+    "StatusReport.h",
+  ]
+
+  cflags = [ "-Wconversion" ]
+
+  public_deps = [
+    "${chip_root}/src/protocols:type_definitions",
+    "${chip_root}/src/system",
+  ]
+
+  deps = [ "${chip_root}/src/lib/support" ]
+}
+
 static_library("secure_channel") {
   output_name = "libSecureChannel"
 
@@ -10,7 +29,6 @@ static_library("secure_channel") {
     "CASEServer.h",
     "CASESession.cpp",
     "CASESession.h",
-    "Constants.h",
     "DefaultSessionResumptionStorage.cpp",
     "DefaultSessionResumptionStorage.h",
     "PASESession.cpp",
@@ -24,8 +42,6 @@ static_library("secure_channel") {
     "SessionResumptionStorage.h",
     "SimpleSessionResumptionStorage.cpp",
     "SimpleSessionResumptionStorage.h",
-    "StatusReport.cpp",
-    "StatusReport.h",
     "UnsolicitedStatusHandler.cpp",
     "UnsolicitedStatusHandler.h",
   ]
@@ -33,6 +49,7 @@ static_library("secure_channel") {
   cflags = [ "-Wconversion" ]
 
   public_deps = [
+    ":type_definitions",
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/messaging",
@@ -41,16 +58,4 @@ static_library("secure_channel") {
     "${chip_root}/src/tracing:macros",
     "${chip_root}/src/transport",
   ]
-
-  # secure channel requires messaging so it can send messages (e.g. for
-  # CASE/PASE handshakes).  messaging requires secure channel so that it can do
-  # things like send standalone acks, which are a secure channel concept.
-  #
-  # secure channel requires transport so it can deal with sessions.  transport
-  # requires secure channel so it can detect control messages, which are a
-  # secure channel concept.
-  allow_circular_includes_from = [
-    "${chip_root}/src/messaging",
-    "${chip_root}/src/transport",
-  ]
 }
diff --git a/src/setup_payload/BUILD.gn b/src/setup_payload/BUILD.gn
index 42fb1c848a0e3b..30b97ceca74349 100644
--- a/src/setup_payload/BUILD.gn
+++ b/src/setup_payload/BUILD.gn
@@ -42,6 +42,7 @@ source_set("additional_data_payload") {
     "${chip_root}/src/crypto",
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
+    "${chip_root}/src/protocols:type_definitions",
     "${chip_root}/src/setup_payload:additional_data_payload_buildconfig",
   ]
 }
diff --git a/src/transport/BUILD.gn b/src/transport/BUILD.gn
index 66f19b95e223bd..f1f7ef534322a7 100644
--- a/src/transport/BUILD.gn
+++ b/src/transport/BUILD.gn
@@ -64,6 +64,7 @@ static_library("transport") {
     "${chip_root}/src/lib/dnssd",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/platform",
+    "${chip_root}/src/protocols/secure_channel:type_definitions",
     "${chip_root}/src/setup_payload",
     "${chip_root}/src/tracing",
     "${chip_root}/src/tracing:macros",