[brcmfmac] Pass simulated env to simulated fw

Pass the simulated environment to the bus registration, so it can
be used when creating the simulated firmware.

BUG: 29743 (brcm: Unit test driver)
TEST: Part of test framework, should have no visible change for now.
      Manually smoke tested against Ubiquiti AP.
Change-Id: I34c9cbf051adbf8f806d57700c9740b7123fc04c
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/BUILD.gn b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/BUILD.gn
index fa4e775..ea81c8f 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/BUILD.gn
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/BUILD.gn
@@ -130,6 +130,9 @@
   public_deps = [
     ":linuxisms",
     "//garnet/lib/wlan/protocol",
+
+    # TODO: we should try to refactor Device so this isn't required outside of the :sim target
+    "//src/connectivity/wlan/drivers/testing/lib/sim-env:simenv",
     "//zircon/public/banjo/ddk.protocol.ethernet",
     "//zircon/public/banjo/ddk.protocol.wlanphyimpl",
     "//zircon/public/lib/async",
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/binding.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/binding.cc
index 392086f..41099d6 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/binding.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/binding.cc
@@ -24,8 +24,10 @@
 
 static constexpr zx_driver_ops_t brcmfmac_driver_ops = {
     .version = DRIVER_OPS_VERSION,
-    .bind = [](void* ctx,
-               zx_device_t* device) { return ::wlan::brcmfmac::Device::Create(device, nullptr); },
+    .bind =
+        [](void* ctx, zx_device_t* device) {
+          return ::wlan::brcmfmac::Device::Create(device, nullptr);
+        },
 };
 
 // clang-format off
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.cc
index 210bde4..85d9863 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.cc
@@ -18,7 +18,8 @@
 // Note that this file does not include any headers that define the bus-specific functions it calls,
 // since it cannot depend on them.  Hence we just declare them directly before use.
 
-zx_status_t brcmf_bus_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus) {
+zx_status_t brcmf_bus_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus,
+                               ::wlan::simulation::Environment* env) {
 #if CONFIG_BRCMFMAC_SDIO
   {
     extern zx_status_t brcmf_sdio_register(brcmf_pub * drvr, std::unique_ptr<brcmf_bus> * out_bus);
@@ -33,8 +34,9 @@
 
 #if CONFIG_BRCMFMAC_SIM
   {
-    extern zx_status_t brcmf_sim_register(brcmf_pub * drvr, std::unique_ptr<brcmf_bus> * out_bus);
-    const zx_status_t result = brcmf_sim_register(drvr, out_bus);
+    extern zx_status_t brcmf_sim_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus,
+                                          ::wlan::simulation::Environment* env);
+    const zx_status_t result = brcmf_sim_register(drvr, out_bus, env);
     if (result != ZX_OK) {
       BRCMF_DBG(INFO, "SIM registration failed: %d\n", result);
     } else {
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.h b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.h
index e7cd956..16396b7 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.h
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.h
@@ -22,6 +22,7 @@
 
 #include <ddk/device.h>
 #include <ddk/driver.h>
+#include "src/connectivity/wlan/drivers/testing/lib/sim-env/sim-env.h"
 
 #include "netbuf.h"
 
@@ -208,7 +209,8 @@
 }
 
 // Interface to the system bus.
-zx_status_t brcmf_bus_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus);
+zx_status_t brcmf_bus_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus,
+                               ::wlan::simulation::Environment* env);
 void brcmf_bus_exit(brcmf_bus* bus);
 
 #endif  // SRC_CONNECTIVITY_WLAN_DRIVERS_THIRD_PARTY_BROADCOM_BRCMFMAC_BUS_H_
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.cc
index 9bbeb52..6d19d25 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.cc
@@ -21,7 +21,8 @@
 namespace brcmfmac {
 
 // static
-zx_status_t Device::Create(zx_device_t* parent_device, Device** device_out) {
+zx_status_t Device::Create(zx_device_t* parent_device, Device** device_out,
+                           simulation::Environment* env) {
   zx_status_t status = ZX_OK;
 
   auto dispatcher = std::make_unique<::async::Loop>(&kAsyncLoopConfigNoAttachToThread);
@@ -45,7 +46,7 @@
   }
 
   std::unique_ptr<brcmf_bus> bus;
-  if ((status = brcmf_bus_register(pub.get(), &bus)) != ZX_OK) {
+  if ((status = brcmf_bus_register(pub.get(), &bus, env)) != ZX_OK) {
     BRCMF_ERR("brcmf_bus_register() returned %s\n", zx_status_get_string(status));
     return status;
   }
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.h b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.h
index c57e3df..ff9af6c 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.h
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/device.h
@@ -23,6 +23,7 @@
 #include <ddktl/device.h>
 #include <ddktl/protocol/wlanphyimpl.h>
 
+#include "src/connectivity/wlan/drivers/testing/lib/sim-env/sim-env.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bus.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/core.h"
 
@@ -35,7 +36,8 @@
  public:
   // Static factory function for Device instances. This factory does not return an owned instance,
   // as on successful invocation the instance will have its lifecycle maanged by the devhost.
-  static zx_status_t Create(zx_device_t* parent_device, Device** device_out);
+  static zx_status_t Create(zx_device_t* parent_device, Device** device_out,
+                            simulation::Environment* env = nullptr);
   explicit Device(std::unique_ptr<::async::Loop> dispatcher, std::unique_ptr<brcmf_pub> brcmf_pub,
                   std::unique_ptr<brcmf_bus> brcmf_bus);
 
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.cc
index ccf37ea..e21ae8b 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.cc
@@ -25,6 +25,8 @@
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/debug.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/fwil.h"
 
+namespace wlan::brcmfmac {
+
 void SimFirmware::GetChipInfo(uint32_t* chip, uint32_t* chiprev) {
   *chip = BRCM_CC_4356_CHIP_ID;
   *chiprev = 2;
@@ -241,3 +243,5 @@
   }
   return ZX_OK;
 }
+
+}  // namespace wlan::brcmfmac
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.h b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.h
index 0eabe3b..3dfd91f 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.h
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_fw.h
@@ -23,9 +23,12 @@
 #include <zircon/types.h>
 
 #include "sim_hw.h"
+#include "src/connectivity/wlan/drivers/testing/lib/sim-env/sim-env.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/bcdc.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/core.h"
 
+namespace wlan::brcmfmac {
+
 class SimFirmware {
   class BcdcResponse {
    public:
@@ -46,6 +49,8 @@
   };
 
  public:
+  explicit SimFirmware(simulation::Environment* env) : hw_(env) {}
+
   void GetChipInfo(uint32_t* chip, uint32_t* chiprev);
 
   // Bus operations
@@ -76,4 +81,6 @@
   SimHardware hw_;
 };
 
+}  // namespace wlan::brcmfmac
+
 #endif  // SRC_CONNECTIVITY_WLAN_DRIVERS_THIRD_PARTY_BROADCOM_BRCMFMAC_SIM_FW_SIM_FW_H_
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.cc
index 50d50a8..8aeae76 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.cc
@@ -20,6 +20,8 @@
 
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/brcm_hw_ids.h"
 
+namespace wlan::brcmfmac {
+
 zx_status_t SimHardware::SetMacAddr(const uint8_t* mac_addr) {
   std::memcpy(mac_addr_, mac_addr, ETH_ALEN);
   return ZX_OK;
@@ -45,3 +47,5 @@
   rev_info->chippkg = 2;
   rev_info->nvramrev = 0x5b2b4;
 }
+
+}  // namespace wlan::brcmfmac
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.h b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.h
index befaf52..5fc977e 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.h
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim-fw/sim_hw.h
@@ -20,15 +20,23 @@
 #include <net/ethernet.h>
 #include <zircon/status.h>
 
+#include "src/connectivity/wlan/drivers/testing/lib/sim-env/sim-env.h"
 #include "src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/fwil_types.h"
 
+namespace wlan::brcmfmac {
+
 class SimHardware {
  public:
+  explicit SimHardware(simulation::Environment* env) : env_(env) {}
+
   zx_status_t SetMacAddr(const uint8_t* mac_addr);
   void GetRevInfo(brcmf_rev_info_le* rev_info);
 
  private:
+  simulation::Environment* env_ __UNUSED;
   uint8_t mac_addr_[ETH_ALEN];
 };
 
+}  // namespace wlan::brcmfmac
+
 #endif  // SRC_CONNECTIVITY_WLAN_DRIVERS_THIRD_PARTY_BROADCOM_BRCMFMAC_SIM_FW_SIM_HW_H_
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.cc
index b86390c..a572337 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.cc
@@ -25,6 +25,7 @@
 #include "common.h"
 #include "debug.h"
 #include "src/connectivity/wlan/drivers/testing/lib/sim-device/device.h"
+#include "src/connectivity/wlan/drivers/testing/lib/sim-env/sim-env.h"
 
 #define BUS_OP(bus) bus->bus_priv.sim->sim_fw
 static const struct brcmf_bus_ops brcmf_sim_bus_ops = {
@@ -70,13 +71,14 @@
 }
 
 // Allocate necessary memory and initialize simulator-specific structures
-zx_status_t brcmf_sim_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus) {
+zx_status_t brcmf_sim_register(brcmf_pub* drvr, std::unique_ptr<brcmf_bus>* out_bus,
+                               ::wlan::simulation::Environment* env) {
   zx_status_t status = ZX_OK;
   auto simdev = std::make_unique<brcmf_simdev>();
   auto bus_if = std::make_unique<brcmf_bus>();
 
   // Initialize inter-structure pointers
-  simdev->sim_fw = std::make_unique<SimFirmware>();
+  simdev->sim_fw = std::make_unique<::wlan::brcmfmac::SimFirmware>(env);
   bus_if->bus_priv.sim = simdev.get();
 
   BRCMF_DBG(SIM, "Registering simulator target\n");
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.h b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.h
index c552c11..8e482d1 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.h
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/sim.h
@@ -23,7 +23,7 @@
 #include "sim-fw/sim_fw.h"
 
 struct brcmf_simdev {
-  std::unique_ptr<SimFirmware> sim_fw;
+  std::unique_ptr<::wlan::brcmfmac::SimFirmware> sim_fw;
   struct brcmf_mp_device* settings;
 };
 
diff --git a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/test/lifecycle_test.cc b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/test/lifecycle_test.cc
index 4251468..9e563d3 100644
--- a/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/test/lifecycle_test.cc
+++ b/src/connectivity/wlan/drivers/third_party/broadcom/brcmfmac/test/lifecycle_test.cc
@@ -30,8 +30,9 @@
 TEST(LifecycleTest, StartStop) {
   zx_status_t status = ZX_OK;
   Device* device = nullptr;
+  simulation::Environment env;
 
-  status = Device::Create(nullptr, &device);
+  status = Device::Create(nullptr, &device, &env);
   EXPECT_EQ(status, ZX_OK);
 
   device->SimUnbind();