This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit e04aa9ac519c5750af7de604c2f61aaafa9b704f
Author: liqinhui <[email protected]>
AuthorDate: Thu Mar 28 20:14:10 2024 +0800

    wifisim: Support the setting of the number of simulated WiFi interfaces.
    
    Signed-off-by: liqinhui <[email protected]>
---
 drivers/net/Kconfig         |  7 +++++++
 drivers/virtio/virtio-net.c | 23 +++++++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 0d3e453919..f304f829b0 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -745,4 +745,11 @@ config WIFI_SIM_CONFDIR
                BSS configuration will be accessed.  This is a string and 
should not
                include any trailing '/'.
 
+config WIFI_SIM_NUMBER
+       int "Number of Simulated WiFi Device"
+       default 0
+       depends on DRIVERS_WIFI_SIM
+       ---help---
+               The number of simulated wifi network devices.
+
 endif # NETDEVICES
diff --git a/drivers/virtio/virtio-net.c b/drivers/virtio/virtio-net.c
index 3f054e4ad9..3493db607a 100644
--- a/drivers/virtio/virtio-net.c
+++ b/drivers/virtio/virtio-net.c
@@ -193,6 +193,10 @@ static const struct netdev_ops_s g_virtio_net_ops =
   virtio_net_txfree
 };
 
+#ifdef CONFIG_DRIVERS_WIFI_SIM
+static uint8_t g_netdev_num = 0;
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -651,18 +655,27 @@ static int virtio_net_probe(FAR struct virtio_device 
*vdev)
   netdev->ops = &g_virtio_net_ops;
 
 #ifdef CONFIG_DRIVERS_WIFI_SIM
-  ret = wifi_sim_init(&priv->lower);
-  if (ret < 0)
+  /* If the WiFi interfaces has reached the setting value,
+   * no more WiFi interfaces will be created.
+   */
+
+  if (g_netdev_num < CONFIG_WIFI_SIM_NUMBER)
     {
-      goto err_with_virtqueues;
+      ret = wifi_sim_init(&priv->lower);
+      if (ret < 0)
+        {
+          goto err_with_virtqueues;
+        }
     }
+
 #endif
 
   /* Register the net deivce */
 
   ret = netdev_lower_register(netdev,
 #ifdef CONFIG_DRIVERS_WIFI_SIM
-                              NET_LL_IEEE80211
+                              g_netdev_num++ < CONFIG_WIFI_SIM_NUMBER ?
+                              NET_LL_IEEE80211 : NET_LL_ETHERNET
 #else
                               NET_LL_ETHERNET
 #endif
@@ -672,6 +685,7 @@ static int virtio_net_probe(FAR struct virtio_device *vdev)
       vrterr("netdev_lower_register failed, ret=%d\n", ret);
 #ifdef CONFIG_DRIVERS_WIFI_SIM
       wifi_sim_remove(&priv->lower);
+      g_netdev_num--;
 #endif
       goto err_with_virtqueues;
     }
@@ -700,6 +714,7 @@ static void virtio_net_remove(FAR struct virtio_device 
*vdev)
   virtio_reset_device(vdev);
   virtio_delete_virtqueues(vdev);
 #ifdef CONFIG_DRIVERS_WIFI_SIM
+  g_netdev_num--;
   wifi_sim_remove(&priv->lower);
 #endif
   kmm_free(priv);

Reply via email to