[ESP32]: Fixed the lighting-app build with external platform enabled. (#38070)

* [ESP32]: Fixed the lighting-app external platform build

* Move the external platform example CI build to regular CI builds

* Fix the external platform failure for BLEManagerImpl
diff --git a/.github/workflows/examples-esp32.yaml b/.github/workflows/examples-esp32.yaml
index 618a089..b19392c 100644
--- a/.github/workflows/examples-esp32.yaml
+++ b/.github/workflows/examples-esp32.yaml
@@ -119,6 +119,9 @@
             - name: Build example Lighting App (Target:ESP32C6)
               run: scripts/examples/esp_example.sh lighting-app sdkconfig.wifi_thread.defaults esp32c6
 
+            - name: Build example Lighting App (external platform)
+              run: scripts/examples/esp_example.sh lighting-app sdkconfig.ext_plat.defaults
+
             - name: Uploading Size Reports
               uses: ./.github/actions/upload-size-reports
               if: ${{ !env.ACT }}
@@ -165,9 +168,6 @@
             - name: Build example Light Switch App (Target:ESP32C3)
               run: scripts/examples/esp_example.sh light-switch-app sdkconfig.defaults esp32c3
 
-            - name: Build example Lighting App (external platform)
-              run: scripts/examples/esp_example.sh lighting-app sdkconfig.ext_plat.defaults
-
             - name: Build example Energy Gateway App
               run: scripts/examples/esp_example.sh energy-gateway-app sdkconfig.defaults
 
diff --git a/examples/platform/esp32/external_platform/ESP32_custom/BUILD.gn b/examples/platform/esp32/external_platform/ESP32_custom/BUILD.gn
index c6725cb..d3dd5e2 100644
--- a/examples/platform/esp32/external_platform/ESP32_custom/BUILD.gn
+++ b/examples/platform/esp32/external_platform/ESP32_custom/BUILD.gn
@@ -15,6 +15,7 @@
 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() {
@@ -24,11 +25,13 @@
   chip_use_factory_data_provider = false
   chip_use_device_info_provider = false
   chip_config_software_version_number = 0
-  chip_enable_chipoble = true
-  chip_bt_nimble_enabled = true
-  chip_bt_bluedroid_enabled = true
+  chip_enable_chipoble = false
+  chip_bt_nimble_enabled = false
+  chip_bt_bluedroid_enabled = false
   chip_max_discovered_ip_addresses = 5
+  chip_enable_ble_controller = false
   chip_enable_route_hook = false
+  chip_memory_alloc_mode = "default"
 }
 
 buildconfig_header("custom_buildconfig") {
@@ -48,6 +51,7 @@
     "CHIP_DEVICE_LAYER_TARGET_ESP32=1",
     "CHIP_DEVICE_LAYER_TARGET=ESP32_custom",
     "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=1",
+    "CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE=${chip_enable_chipoble}",
     "BLE_PLATFORM_CONFIG_INCLUDE=<external_platform/ESP32_custom/BlePlatformConfig.h>",
     "CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<external_platform/ESP32_custom/CHIPDevicePlatformConfig.h>",
     "CHIP_PLATFORM_CONFIG_INCLUDE=<external_platform/ESP32_custom/CHIPPlatformConfig.h>",
@@ -110,6 +114,41 @@
     "${chip_root}/src/platform:platform_base",
   ]
 
+  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_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_ota_requestor) {
     sources += [
       "OTAImageProcessorImpl.cpp",
@@ -117,18 +156,6 @@
     ]
   }
 
-  if (chip_enable_chipoble) {
-    sources += [ "BLEManagerImpl.h" ]
-  }
-
-  if (chip_bt_nimble_enabled) {
-    sources += [ "nimble/BLEManagerImpl.cpp" ]
-  }
-
-  if (chip_bt_bluedroid_enabled) {
-    sources += [ "bluedroid/BLEManagerImpl.cpp" ]
-  }
-
   if (chip_enable_wifi) {
     sources += [
       "ConnectivityManagerImpl_WiFi.cpp",
diff --git a/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformDefault.cpp b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformDefault.cpp
new file mode 120000
index 0000000..eea57b5
--- /dev/null
+++ b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformDefault.cpp
@@ -0,0 +1 @@
+../../../../../src/platform/ESP32/CHIPMem-PlatformDefault.cpp
\ No newline at end of file
diff --git a/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformExternal.cpp b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformExternal.cpp
new file mode 120000
index 0000000..e0b52f2
--- /dev/null
+++ b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformExternal.cpp
@@ -0,0 +1 @@
+../../../../../src/platform/ESP32/CHIPMem-PlatformExternal.cpp
\ No newline at end of file
diff --git a/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformInternal.cpp b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformInternal.cpp
new file mode 120000
index 0000000..c5a783c
--- /dev/null
+++ b/examples/platform/esp32/external_platform/ESP32_custom/CHIPMem-PlatformInternal.cpp
@@ -0,0 +1 @@
+../../../../../src/platform/ESP32/CHIPMem-PlatformInternal.cpp
\ No newline at end of file