You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/03/01 08:39:44 UTC

[GitHub] [incubator-nuttx] donghengqaz opened a new pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

donghengqaz opened a new pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939


   ## Summary
   
   Refactor ESP32 WiFi driver to support station and softAP coexistence.
   
   Add Wi-Fi station and softAP coexistence config file to test.
   
   ## Impact
   
   ## Testing
   
   ```sh
   ifup wlan0
   wapi psk wlan0 espressif002 1
   wapi essid wlan0 AFAST_IK 1
   renew wlan0
   
   # You can use ifconfig to check the local IP address.
   
   wapi psk wlan1 19901101 1
   wapi essid wlan1 nuttx-softap 1
   dhcpd wlan1
   
   # You can use phone or compute to connect AP whose name is nuttx-softap and password is 19901101
   ```
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593178715



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.h
##########
@@ -152,26 +235,178 @@ int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
 int esp_wifi_sta_read_mac(uint8_t *mac);
 
 /****************************************************************************
- * Name: esp_wifi_free_eb
+ * Name: esp_wifi_set_password
  *
  * Description:
- *   Free WiFi receive callback input eb pointer
+ *   Set WiFi station password
  *
  * Input Parameters:
- *   eb - WiFi receive callback input eb pointer
+ *   pdata - Password buffer pointer
+ *   len   - Password length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_password(const uint8_t *pdata, uint8_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_set_ssid
+ *
+ * Description:
+ *   Set WiFi station SSID
+ *
+ * Input Parameters:
+ *   pdata - SSID buffer pointer
+ *   len   - SSID length
  *
  * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_ssid(const uint8_t *pdata, uint8_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_sta_connect
+ *
+ * Description:
+ *   Trigger WiFi station connection action
+ *
+ * Input Parameters:
  *   None
  *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
  ****************************************************************************/
 
-void esp_wifi_free_eb(void *eb);
+int esp_wifi_sta_connect(void);
 
 /****************************************************************************
- * Name: esp_wifi_set_password
+ * Name: esp_wifi_sta_disconnect
  *
  * Description:
- *   Set WiFi password
+ *   Trigger WiFi station disconnection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_disconnect(void);
+#endif
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+
+/****************************************************************************
+ * Name: esp_wifi_softap_start
+ *
+ * Description:
+ *   Start WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_start(void);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_stop
+ *
+ * Description:
+ *   Stop WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_stop(void);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_send_data
+ *
+ * Description:
+ *   Use WiFi softAP interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_send_data(void *pbuf, uint32_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi softAP receive packet callback function

Review comment:
       Please use git grep `Regitser` to catch other occurrences I may not have seen




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r595107205



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);

Review comment:
       ok




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593177827



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.h
##########
@@ -121,10 +188,10 @@ int esp_wifi_sta_send_data(void *pbuf, uint32_t len);
  * Name: esp_wifi_sta_register_recv_cb
  *
  * Description:
- *   Regitser WiFi receive packet callback function
+ *   Regitser WiFi station receive packet callback function

Review comment:
       ```suggestion
    *   Register WiFi station receive packet callback function
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz edited a comment on pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz edited a comment on pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#issuecomment-787778861


   Draft: The code is mainly done, but some friends would not like to merge into `master` before Mar 15, so I set this PR to be Draft to avoid this PR being merged into `master`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz edited a comment on pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz edited a comment on pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#issuecomment-787778861


   Draft: The code is mainly done, but some friends would not like to merge into master before mar 15, so I set this PR to be Draft to avoid this PR being merged into main.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r595107751



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);
 
-  irq = esp32_alloc_levelint(1);

Review comment:
       Ok.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593178367



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.h
##########
@@ -152,26 +235,178 @@ int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
 int esp_wifi_sta_read_mac(uint8_t *mac);
 
 /****************************************************************************
- * Name: esp_wifi_free_eb
+ * Name: esp_wifi_set_password
  *
  * Description:
- *   Free WiFi receive callback input eb pointer
+ *   Set WiFi station password
  *
  * Input Parameters:
- *   eb - WiFi receive callback input eb pointer
+ *   pdata - Password buffer pointer
+ *   len   - Password length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_password(const uint8_t *pdata, uint8_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_set_ssid
+ *
+ * Description:
+ *   Set WiFi station SSID
+ *
+ * Input Parameters:
+ *   pdata - SSID buffer pointer
+ *   len   - SSID length
  *
  * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_ssid(const uint8_t *pdata, uint8_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_sta_connect
+ *
+ * Description:
+ *   Trigger WiFi station connection action
+ *
+ * Input Parameters:
  *   None
  *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
  ****************************************************************************/
 
-void esp_wifi_free_eb(void *eb);
+int esp_wifi_sta_connect(void);
 
 /****************************************************************************
- * Name: esp_wifi_set_password
+ * Name: esp_wifi_sta_disconnect
  *
  * Description:
- *   Set WiFi password
+ *   Trigger WiFi station disconnection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_disconnect(void);
+#endif
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+
+/****************************************************************************
+ * Name: esp_wifi_softap_start
+ *
+ * Description:
+ *   Start WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_start(void);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_stop
+ *
+ * Description:
+ *   Stop WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_stop(void);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_send_data
+ *
+ * Description:
+ *   Use WiFi softAP interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_send_data(void *pbuf, uint32_t len);
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi softAP receive packet callback function

Review comment:
       ```suggestion
    *   Register WiFi softAP receive packet callback function
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593148992



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -3960,6 +4088,47 @@ static unsigned long esp_random_ulong(void)
   return esp_random();
 }
 
+/****************************************************************************
+ * Name: esp_wifi_tx_done_cb
+ *
+ * Description:
+ *   WiFi TX done callback function.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data,
+                                          uint16_t *len, bool txstatus)
+{
+#if 0
+  wlinfo("INFO: ifidx=%d data=%p *len=%p txstatus=%d\n",
+         ifidx, data, len, txstatus);
+#endif

Review comment:
       Since this macro depends on DEBUG enabled I think you can remove the "#if 0"




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r594016658



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);
 
-  irq = esp32_alloc_levelint(1);

Review comment:
       The wifi will directly register peripheral to CPU interrupt in `wifi_set_intr`.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz edited a comment on pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz edited a comment on pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#issuecomment-787778861


   Draft: The code is mainly done, but some friends would not like to merge into `master` before mar 15, so I set this PR to be Draft to avoid this PR being merged into `master`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593176188



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -4682,82 +4775,812 @@ int esp_wifi_set_ssid(const uint8_t *pdata, uint8_t len)
  *
  ****************************************************************************/
 
-int esp_wifi_connect_internal(void)
+int esp_wifi_sta_start(void)
 {
   int ret;
-  wifi_config_t wifi_cfg;
-  struct timespec timeout;
+  wifi_mode_t mode;
 
-  if (g_connected)
-    {
-      wlinfo("INFO: WiFi has connected AP\n");
-      return 0;
-    }
+  esp_wifi_lock(true);
 
-  ret = esp_wifi_set_mode(WIFI_MODE_STA);
+  ret = esp_wifi_stop();
   if (ret)
     {
-      wlerr("ERROR: Failed to set station mode error=%d\n", ret);
-      esp_wifi_deinit();
-      return -1;
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
     }
 
-  memset(&wifi_cfg, 0, sizeof(wifi_config_t));
-  memcpy((char *)wifi_cfg.sta.ssid, g_ssid, g_ssid_len);
-  memcpy((char *)wifi_cfg.sta.password, g_password, g_password_len);
-
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      mode = WIFI_MODE_APSTA;
+    }
+#else
+  mode = WIFI_MODE_STA;
+#endif
+
+  ret = esp_wifi_set_mode(mode);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  ret = esp_wifi_start();
+  if (ret)
+    {
+      wlerr("ERROR: Failed to start WiFi with mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  g_sta_started = true;
+
+  wlinfo("INFO: OK to start WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_set_mode:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_stop
+ *
+ * Description:
+ *   Stop WiFi station.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_stop(void)
+{
+  int ret;
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
+  if (ret)
+    {
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
+    }
+
+  g_sta_started = false;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      ret = esp_wifi_set_mode(WIFI_MODE_AP);
+      if (ret)
+        {
+          wlerr("ERROR: Failed to set WiFi AP mode ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+
+      ret = esp_wifi_start();
+      if (ret)
+        {
+          wlerr("ERROR: Failed to start WiFi AP ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+    }
+#endif
+
+  wlinfo("INFO: OK to stop WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+errout_set_mode:
+  esp_wifi_lock(true);
+  return ret;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_send_data
+ *
+ * Description:
+ *   Use WiFi station interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_send_data(void *pbuf, uint32_t len)
+{
+  int ret;
+
+  ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi station receive packet callback function
+ *
+ * Input Parameters:
+ *   recv_cb - Receive callback function
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
+                                                 uint16_t len,
+                                                 void *eb))
+{
+  int ret;
+
+  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_txdone_cb
+ *
+ * Description:
+ *   Register the station TX done callback function.
+ *
+ * Input Parameters:
+ *   cb - The callback function
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb)
+{
+  g_sta_txdone_cb = cb;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_read_mac
+ *
+ * Description:
+ *   Read station interface MAC address from efuse
+ *
+ * Input Parameters:
+ *   mac  - MAC address buffer pointer
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_read_mac(uint8_t *mac)
+{
+  return esp_read_mac(mac, ESP_MAC_WIFI_STA);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_set_password
+ *
+ * Description:
+ *   Set WiFi station password
+ *
+ * Input Parameters:
+ *   pdata - Password buffer pointer
+ *   len   - Password length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_password(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[65];
+#endif
+
+  if (len > 64)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.sta.password, pdata, len);
+
   wifi_cfg.sta.pmf_cfg.capable = true;
 
-  ret = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg);
+  ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi station password=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_set_ssid
+ *
+ * Description:
+ *   Set WiFi station SSID
+ *
+ * Input Parameters:
+ *   pdata - SSID buffer pointer
+ *   len   - SSID length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_ssid(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[33];
+#endif
+
+  if (len > 32)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.sta.ssid, pdata, len);
+
+  ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi station ssid=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_connect
+ *
+ * Description:
+ *   Trigger WiFi station connection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_connect(void)
+{
+  int ret;
+  uint32_t ticks;
+
+  esp_wifi_lock(true);
+
+  if (g_sta_connected)
+    {
+      wlinfo("INFO: WiFi has connected AP\n");
+      esp_wifi_lock(false);
+      return OK;
+    }
+
+  g_sta_reconnect = true;
+
+  ret = esp_wifi_connect();
   if (ret)
     {
-      wlerr("ERROR: Failed to set WiFi config error=%d\n", ret);
+      wlerr("ERROR: Failed to connect ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_wifi_connect;
+    }
+
+  esp_wifi_lock(false);
+
+  ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT);
+  do
+    {
+      if (g_sta_connected)
+        {
+          break;
+        }
+
+      esp_task_delay(1);
+    }
+  while (ticks--);
+
+  if (!g_sta_connected)
+    {
+      g_sta_reconnect = false;
+      wlinfo("INFO: Failed to connect to AP\n");
       return -1;
     }
 
+  return OK;
+
+errout_wifi_connect:
+  g_sta_reconnect = false;
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_disconnect
+ *
+ * Description:
+ *   Trigger WiFi station disconnection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_disconnect(void)
+{
+  int ret;
+
+  esp_wifi_lock(true);
+
+  g_sta_reconnect = false;
+
+  ret = esp_wifi_disconnect();
+  if (ret)
+    {
+      wlerr("ERROR: Failed to disconnect ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+    }
+  else
+    {
+      wlinfo("INFO: OK to disconnect WiFi station\n");
+    }
+
+  esp_wifi_lock(false);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * SoftAP functions
+ ****************************************************************************/
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+
+/****************************************************************************
+ * Name: esp_wifi_softap_start
+ *
+ * Description:
+ *   Start WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_start(void)
+{
+  int ret;
+  wifi_mode_t mode;
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
+  if (ret)
+    {
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
+    }
+
+#ifdef ESP32_WLAN_HAS_STA
+  if (g_sta_started)
+    {
+      mode = WIFI_MODE_APSTA;
+    }
+#else
+  mode = WIFI_MODE_AP;
+#endif
+
+  ret = esp_wifi_set_mode(mode);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
   ret = esp_wifi_start();
   if (ret)
     {
-      wlerr("ERROR: Failed to set start config error=%d\n", ret);
-      return -1;
+      wlerr("ERROR: Failed to start WiFi with mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
     }
 
-  clock_gettime(CLOCK_REALTIME, &timeout);
-  timeout.tv_sec += WIFI_CONNECT_TIMEOUT;
+  g_softap_started = true;
+
+  wlinfo("INFO: OK to start WiFi softAP\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_set_mode:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_stop
+ *
+ * Description:
+ *   Stop WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_stop(void)
+{
+  int ret;
 
-  ret = nxsem_timedwait(&g_connect_sem, &timeout);
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
   if (ret)
     {
-      wlerr("ERROR: Failed to wait sem error=%d\n", ret);
-      esp_wifi_stop();
-      return -1;
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
     }
 
-  if (!g_connected)
+  g_softap_started = false;
+
+#ifdef ESP32_WLAN_HAS_STA
+  if (g_sta_started)
     {
-      wlerr("ERROR: Process connection error\n");
-      esp_wifi_stop();
-      return -1;
+      ret = esp_wifi_set_mode(WIFI_MODE_STA);
+      if (ret)
+        {
+          wlerr("ERROR: Failed to set WiFi AP mode ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+
+      ret = esp_wifi_start();
+      if (ret)
+        {
+          wlerr("ERROR: Failed to start WiFi AP ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+    }
+#endif
+
+  wlinfo("INFO: OK to stop WiFi softAP\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+#ifdef ESP32_WLAN_HAS_STA
+errout_set_mode:
+  esp_wifi_lock(true);
+  return ret;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_send_data
+ *
+ * Description:
+ *   Use WiFi softAP interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_send_data(void *pbuf, uint32_t len)
+{
+  int ret;
+
+  ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi softAP receive packet callback function

Review comment:
       ```suggestion
    *   Register WiFi SoftAP receive packet callback function
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593177302



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -4682,82 +4775,812 @@ int esp_wifi_set_ssid(const uint8_t *pdata, uint8_t len)
  *
  ****************************************************************************/
 
-int esp_wifi_connect_internal(void)
+int esp_wifi_sta_start(void)
 {
   int ret;
-  wifi_config_t wifi_cfg;
-  struct timespec timeout;
+  wifi_mode_t mode;
 
-  if (g_connected)
-    {
-      wlinfo("INFO: WiFi has connected AP\n");
-      return 0;
-    }
+  esp_wifi_lock(true);
 
-  ret = esp_wifi_set_mode(WIFI_MODE_STA);
+  ret = esp_wifi_stop();
   if (ret)
     {
-      wlerr("ERROR: Failed to set station mode error=%d\n", ret);
-      esp_wifi_deinit();
-      return -1;
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
     }
 
-  memset(&wifi_cfg, 0, sizeof(wifi_config_t));
-  memcpy((char *)wifi_cfg.sta.ssid, g_ssid, g_ssid_len);
-  memcpy((char *)wifi_cfg.sta.password, g_password, g_password_len);
-
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      mode = WIFI_MODE_APSTA;
+    }
+#else
+  mode = WIFI_MODE_STA;
+#endif
+
+  ret = esp_wifi_set_mode(mode);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  ret = esp_wifi_start();
+  if (ret)
+    {
+      wlerr("ERROR: Failed to start WiFi with mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  g_sta_started = true;
+
+  wlinfo("INFO: OK to start WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_set_mode:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_stop
+ *
+ * Description:
+ *   Stop WiFi station.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_stop(void)
+{
+  int ret;
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
+  if (ret)
+    {
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
+    }
+
+  g_sta_started = false;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      ret = esp_wifi_set_mode(WIFI_MODE_AP);
+      if (ret)
+        {
+          wlerr("ERROR: Failed to set WiFi AP mode ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+
+      ret = esp_wifi_start();
+      if (ret)
+        {
+          wlerr("ERROR: Failed to start WiFi AP ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+    }
+#endif
+
+  wlinfo("INFO: OK to stop WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+errout_set_mode:
+  esp_wifi_lock(true);
+  return ret;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_send_data
+ *
+ * Description:
+ *   Use WiFi station interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_send_data(void *pbuf, uint32_t len)
+{
+  int ret;
+
+  ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi station receive packet callback function
+ *
+ * Input Parameters:
+ *   recv_cb - Receive callback function
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer,
+                                                 uint16_t len,
+                                                 void *eb))
+{
+  int ret;
+
+  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_txdone_cb
+ *
+ * Description:
+ *   Register the station TX done callback function.
+ *
+ * Input Parameters:
+ *   cb - The callback function
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb)
+{
+  g_sta_txdone_cb = cb;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_read_mac
+ *
+ * Description:
+ *   Read station interface MAC address from efuse
+ *
+ * Input Parameters:
+ *   mac  - MAC address buffer pointer
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_read_mac(uint8_t *mac)
+{
+  return esp_read_mac(mac, ESP_MAC_WIFI_STA);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_set_password
+ *
+ * Description:
+ *   Set WiFi station password
+ *
+ * Input Parameters:
+ *   pdata - Password buffer pointer
+ *   len   - Password length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_password(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[65];
+#endif
+
+  if (len > 64)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.sta.password, pdata, len);
+
   wifi_cfg.sta.pmf_cfg.capable = true;
 
-  ret = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg);
+  ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi station password=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_set_ssid
+ *
+ * Description:
+ *   Set WiFi station SSID
+ *
+ * Input Parameters:
+ *   pdata - SSID buffer pointer
+ *   len   - SSID length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_set_ssid(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[33];
+#endif
+
+  if (len > 32)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.sta.ssid, pdata, len);
+
+  ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi station ssid=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_connect
+ *
+ * Description:
+ *   Trigger WiFi station connection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_connect(void)
+{
+  int ret;
+  uint32_t ticks;
+
+  esp_wifi_lock(true);
+
+  if (g_sta_connected)
+    {
+      wlinfo("INFO: WiFi has connected AP\n");
+      esp_wifi_lock(false);
+      return OK;
+    }
+
+  g_sta_reconnect = true;
+
+  ret = esp_wifi_connect();
   if (ret)
     {
-      wlerr("ERROR: Failed to set WiFi config error=%d\n", ret);
+      wlerr("ERROR: Failed to connect ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_wifi_connect;
+    }
+
+  esp_wifi_lock(false);
+
+  ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT);
+  do
+    {
+      if (g_sta_connected)
+        {
+          break;
+        }
+
+      esp_task_delay(1);
+    }
+  while (ticks--);
+
+  if (!g_sta_connected)
+    {
+      g_sta_reconnect = false;
+      wlinfo("INFO: Failed to connect to AP\n");
       return -1;
     }
 
+  return OK;
+
+errout_wifi_connect:
+  g_sta_reconnect = false;
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_disconnect
+ *
+ * Description:
+ *   Trigger WiFi station disconnection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_disconnect(void)
+{
+  int ret;
+
+  esp_wifi_lock(true);
+
+  g_sta_reconnect = false;
+
+  ret = esp_wifi_disconnect();
+  if (ret)
+    {
+      wlerr("ERROR: Failed to disconnect ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+    }
+  else
+    {
+      wlinfo("INFO: OK to disconnect WiFi station\n");
+    }
+
+  esp_wifi_lock(false);
+  return ret;
+}
+#endif
+
+/****************************************************************************
+ * SoftAP functions
+ ****************************************************************************/
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+
+/****************************************************************************
+ * Name: esp_wifi_softap_start
+ *
+ * Description:
+ *   Start WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_start(void)
+{
+  int ret;
+  wifi_mode_t mode;
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
+  if (ret)
+    {
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
+    }
+
+#ifdef ESP32_WLAN_HAS_STA
+  if (g_sta_started)
+    {
+      mode = WIFI_MODE_APSTA;
+    }
+#else
+  mode = WIFI_MODE_AP;
+#endif
+
+  ret = esp_wifi_set_mode(mode);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
   ret = esp_wifi_start();
   if (ret)
     {
-      wlerr("ERROR: Failed to set start config error=%d\n", ret);
-      return -1;
+      wlerr("ERROR: Failed to start WiFi with mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
     }
 
-  clock_gettime(CLOCK_REALTIME, &timeout);
-  timeout.tv_sec += WIFI_CONNECT_TIMEOUT;
+  g_softap_started = true;
+
+  wlinfo("INFO: OK to start WiFi softAP\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_set_mode:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_stop
+ *
+ * Description:
+ *   Stop WiFi softAP.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_stop(void)
+{
+  int ret;
 
-  ret = nxsem_timedwait(&g_connect_sem, &timeout);
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
   if (ret)
     {
-      wlerr("ERROR: Failed to wait sem error=%d\n", ret);
-      esp_wifi_stop();
-      return -1;
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
     }
 
-  if (!g_connected)
+  g_softap_started = false;
+
+#ifdef ESP32_WLAN_HAS_STA
+  if (g_sta_started)
     {
-      wlerr("ERROR: Process connection error\n");
-      esp_wifi_stop();
-      return -1;
+      ret = esp_wifi_set_mode(WIFI_MODE_STA);
+      if (ret)
+        {
+          wlerr("ERROR: Failed to set WiFi AP mode ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+
+      ret = esp_wifi_start();
+      if (ret)
+        {
+          wlerr("ERROR: Failed to start WiFi AP ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+    }
+#endif
+
+  wlinfo("INFO: OK to stop WiFi softAP\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+#ifdef ESP32_WLAN_HAS_STA
+errout_set_mode:
+  esp_wifi_lock(true);
+  return ret;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_send_data
+ *
+ * Description:
+ *   Use WiFi softAP interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_send_data(void *pbuf, uint32_t len)
+{
+  int ret;
+
+  ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi softAP receive packet callback function
+ *
+ * Input Parameters:
+ *   recv_cb - Receive callback function
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer,
+                                                    uint16_t len,
+                                                    void *eb))
+{
+  int ret;
+
+  ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)recv_cb);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_register_txdone_cb
+ *
+ * Description:
+ *   Register the softAP TX done callback function.
+ *
+ * Input Parameters:
+ *   cb - The callback function
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb)
+{
+  g_softap_txdone_cb = cb;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_read_mac
+ *
+ * Description:
+ *   Read softAP interface MAC address from efuse
+ *
+ * Input Parameters:
+ *   mac  - MAC address buffer pointer
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_read_mac(uint8_t *mac)
+{
+  return esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_set_password
+ *
+ * Description:
+ *   Set WiFi softAP password
+ *
+ * Input Parameters:
+ *   pdata - Password buffer pointer
+ *   len   - Password length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_set_password(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[65];
+#endif
+
+  if (len > 64)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.ap.password, pdata, len);
+
+  ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi softAP password=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_set_ssid
+ *
+ * Description:
+ *   Set WiFi softAP SSID
+ *
+ * Input Parameters:
+ *   pdata - SSID buffer pointer
+ *   len   - SSID length
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_set_ssid(const uint8_t *pdata, uint8_t len)
+{
+  int ret;
+  wifi_config_t wifi_cfg;
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  char buf[33];
+#endif
+
+  if (len > 32)
+    {
+      return -EINVAL;
+    }
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to get WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
+    }
+
+  memcpy(wifi_cfg.ap.ssid, pdata, len);
+
+  ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi config data ret=%d\n", ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_get_config;
     }
 
+#ifdef CONFIG_DEBUG_WIRELESS_INFO
+  memcpy(buf, pdata, len);
+  buf[len] = 0;
+  wlinfo("INFO: OK to set WiFi softAP ssid=%s len=%d\n", buf, len);
+#endif
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_get_config:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_softap_connect
+ *
+ * Description:
+ *   Trigger WiFi softAP accept connection action
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_softap_connect(void)
+{
   return 0;
 }
 
 /****************************************************************************
- * Name: esp_wifi_sta_register_txdone_cb
+ * Name: esp_wifi_softap_disconnect
  *
  * Description:
- *   Register the txDone callback function of type wifi_tx_done_cb_t
+ *   Trigger WiFi softAP drop connection action
  *
  * Input Parameters:
- *   callback - The callback function
+ *   None
  *
  * Returned Value:
  *   0 if success or -1 if fail
  *
  ****************************************************************************/
 
-int esp_wifi_sta_register_txdone_cb(void *callback)
+int esp_wifi_softap_disconnect(void)
 {
-  return esp_wifi_set_tx_done_cb((wifi_tx_done_cb_t)callback);
+  return 0;

Review comment:
       ```suggestion
     return OK;
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r594015749



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);

Review comment:
       This is defined in esp-idf's https://github.com/espressif/esp-idf/blob/c49d03b8eb9cb2070960927cbd7194645c44a874/components/esp_wifi/include/esp_private/wifi_os_adapter.h#L38, I also add comment for this, I think we can keep it origin.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz removed a comment on pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz removed a comment on pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#issuecomment-787778861


   Draft: The code is mainly done, but some friends would not like to merge into `master` before Mar 15, so I set this PR to be Draft to avoid this PR being merged into `master`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593163753



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -516,6 +561,70 @@ static inline int32_t esp_errno_trans(int ret)
     }
 }
 
+/****************************************************************************
+ * Name: osi_errno_trans
+ *
+ * Description:
+ *   Transform from ESP WiFi error code to NuttX error code
+ *
+ * Input Parameters:
+ *   ret - ESP WiFi error code
+ *
+ * Returned Value:
+ *   NuttX error code
+ *
+ ****************************************************************************/
+
+static int32_t wifi_errno_trans(int ret)
+{
+  int wifierr;
+
+  /* Unmask component error bits */
+
+  wifierr = ret & 0xfff;
+
+  if (wifierr == ESP_OK)
+    {
+      return 0;
+    }
+  else if (wifierr == ESP_ERR_NO_MEM)
+    {
+      return -ENOMEM;
+    }
+  else if (wifierr == ESP_ERR_INVALID_ARG)
+    {
+      return -EINVAL;
+    }
+  else if (wifierr == ESP_ERR_INVALID_STATE)
+    {
+      return -EIO;
+    }
+  else if (wifierr == ESP_ERR_INVALID_SIZE)
+    {
+      return -EINVAL;
+    }
+  else if (wifierr == ESP_ERR_NOT_FOUND)
+    {
+      return -ENOSYS;
+    }
+  else if (wifierr == ESP_ERR_NOT_SUPPORTED)
+    {
+      return -ENOSYS;
+    }
+  else if (wifierr == ESP_ERR_TIMEOUT)
+    {
+      return -ETIMEDOUT;
+    }
+  else if (wifierr == ESP_ERR_INVALID_MAC)
+    {
+      return -EINVAL;
+    }
+  else
+    {
+      return -1;

Review comment:
       ```suggestion
         return ERROR;
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r594016217



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);
 
-  irq = esp32_alloc_levelint(1);

Review comment:
       The wifi will directly register peripheral to CPU interrupt in `wifi_set_intr`.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis merged pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
acassis merged pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r594014901



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -3960,6 +4088,47 @@ static unsigned long esp_random_ulong(void)
   return esp_random();
 }
 
+/****************************************************************************
+ * Name: esp_wifi_tx_done_cb
+ *
+ * Description:
+ *   WiFi TX done callback function.
+ *
+ ****************************************************************************/
+
+static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data,
+                                          uint16_t *len, bool txstatus)
+{
+#if 0
+  wlinfo("INFO: ifidx=%d data=%p *len=%p txstatus=%d\n",
+         ifidx, data, len, txstatus);
+#endif

Review comment:
       About this, because I also don't want to enable this debugging unless it is necessary. Here will be called when packet id RX done, so this will lead to too much log.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593173482



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);
 
-  irq = esp32_alloc_levelint(1);

Review comment:
       Where is the `alloc_levelint` now?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] donghengqaz commented on pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
donghengqaz commented on pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#issuecomment-787778861


   Draft: The code is mainly done, but some friends would not like to merge into master before mar 15, so I set this PR to be `Draft`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593165669



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -638,26 +755,20 @@ static int esp_event_lock(bool lock)
 static void esp_set_isr(int32_t n, void *f, void *arg)
 {
   int ret;
+  uint32_t tmp;
   struct irq_adpt *adapter;
-  int irq;
-  int cpu = 0;
-  int tmp;
+  int irq = n + XTENSA_IRQ_FIRSTPERIPH;
 
-  if (g_wifi_irq >= 0)
-    {
-      return ;
-    }
+  wlinfo("INFO: n=%d f=%p arg=%p irq=%d\n", n, f, arg, irq);

Review comment:
       Could we improve the name of these variables? `n`, `f` ?
   They are little expressive




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593152732



##########
File path: arch/xtensa/src/esp32/Kconfig
##########
@@ -852,6 +852,21 @@ endmenu # ESP32_EMAC
 menu "WiFi configuration"
 	depends on ESP32_WIRELESS
 
+choice
+	prompt "ESP32 WiFi mode"
+	default ESP32_WIFI_STATION
+
+config ESP32_WIFI_STATION
+	bool "station"

Review comment:
       ```suggestion
   	bool "Station mode"
   ```

##########
File path: arch/xtensa/src/esp32/Kconfig
##########
@@ -852,6 +852,21 @@ endmenu # ESP32_EMAC
 menu "WiFi configuration"
 	depends on ESP32_WIRELESS
 
+choice
+	prompt "ESP32 WiFi mode"
+	default ESP32_WIFI_STATION
+
+config ESP32_WIFI_STATION
+	bool "station"
+
+config ESP32_WIFI_SOFTAP
+	bool "softAP"
+
+config ESP32_WIFI_STATION_SOFTAP_COEXISTENCE
+	bool "station + softAP coexistence"

Review comment:
       ```suggestion
   	bool "Station + SoftAP coexistence"
   ```

##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -82,6 +83,26 @@
  * Private Types
  ****************************************************************************/
 
+/* WiFi station state */
+
+enum wifi_sta_state
+{
+  WIFI_STA_STATE_NULL,
+  WIFI_STA_STATE_START,
+  WIFI_STA_STATE_CONNECT,
+  WIFI_STA_STATE_DISCONNECT,
+  WIFI_STA_STATE_STOP
+};
+
+/* WiFi station state */

Review comment:
       ```suggestion
   /* WiFi SoftAP state */
   ```

##########
File path: arch/xtensa/src/esp32/Kconfig
##########
@@ -852,6 +852,21 @@ endmenu # ESP32_EMAC
 menu "WiFi configuration"
 	depends on ESP32_WIRELESS
 
+choice
+	prompt "ESP32 WiFi mode"
+	default ESP32_WIFI_STATION
+
+config ESP32_WIFI_STATION
+	bool "station"
+
+config ESP32_WIFI_SOFTAP
+	bool "softAP"

Review comment:
       ```suggestion
   	bool "SoftAP mode"
   ```

##########
File path: arch/xtensa/src/esp32/esp32_cpuint.c
##########
@@ -179,9 +179,17 @@ static uint32_t g_intenable[1];
  * devices.
  */
 
-static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET;
+static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET
+#ifdef CONFIG_ESP32_WIRELESS 
+                                  & (~(1 << ESP32_CPUINT_MAC))
+#endif
+                                  ;
 #ifdef CONFIG_SMP
-static uint32_t g_cpu1_freeints = EPS32_CPUINT_PERIPHSET;
+static uint32_t g_cpu1_freeints = EPS32_CPUINT_PERIPHSET
+#ifdef CONFIG_ESP32_WIRELESS 
+                                  & (~(1 << ESP32_CPUINT_MAC))
+#endif
+                                  ;

Review comment:
       The same here.

##########
File path: arch/xtensa/src/esp32/esp32_cpuint.c
##########
@@ -179,9 +179,17 @@ static uint32_t g_intenable[1];
  * devices.
  */
 
-static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET;
+static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET
+#ifdef CONFIG_ESP32_WIRELESS 
+                                  & (~(1 << ESP32_CPUINT_MAC))
+#endif
+                                  ;

Review comment:
       ```suggestion
   #ifdef CONFIG_ESP32_WIRELESS 
   static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET & (~ (1  <<  ESP32_CPUINT_MAC));
   #else
   static uint32_t g_cpu0_freeints = EPS32_CPUINT_PERIPHSET;
   #endif
   ```
   I think this way it gets easier to understand. What do you think?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] saramonteiro commented on a change in pull request #2939: xtensa/esp32: Refactor ESP32 WiFi driver to support station and softAP coexistence

Posted by GitBox <gi...@apache.org>.
saramonteiro commented on a change in pull request #2939:
URL: https://github.com/apache/incubator-nuttx/pull/2939#discussion_r593173928



##########
File path: arch/xtensa/src/esp32/esp32_wifi_adapter.c
##########
@@ -4682,82 +4775,812 @@ int esp_wifi_set_ssid(const uint8_t *pdata, uint8_t len)
  *
  ****************************************************************************/
 
-int esp_wifi_connect_internal(void)
+int esp_wifi_sta_start(void)
 {
   int ret;
-  wifi_config_t wifi_cfg;
-  struct timespec timeout;
+  wifi_mode_t mode;
 
-  if (g_connected)
-    {
-      wlinfo("INFO: WiFi has connected AP\n");
-      return 0;
-    }
+  esp_wifi_lock(true);
 
-  ret = esp_wifi_set_mode(WIFI_MODE_STA);
+  ret = esp_wifi_stop();
   if (ret)
     {
-      wlerr("ERROR: Failed to set station mode error=%d\n", ret);
-      esp_wifi_deinit();
-      return -1;
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
     }
 
-  memset(&wifi_cfg, 0, sizeof(wifi_config_t));
-  memcpy((char *)wifi_cfg.sta.ssid, g_ssid, g_ssid_len);
-  memcpy((char *)wifi_cfg.sta.password, g_password, g_password_len);
-
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      mode = WIFI_MODE_APSTA;
+    }
+#else
+  mode = WIFI_MODE_STA;
+#endif
+
+  ret = esp_wifi_set_mode(mode);
+  if (ret)
+    {
+      wlerr("ERROR: Failed to set WiFi mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  ret = esp_wifi_start();
+  if (ret)
+    {
+      wlerr("ERROR: Failed to start WiFi with mode=%d ret=%d\n", mode, ret);
+      ret = wifi_errno_trans(ret);
+      goto errout_set_mode;
+    }
+
+  g_sta_started = true;
+
+  wlinfo("INFO: OK to start WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+errout_set_mode:
+  esp_wifi_lock(false);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_stop
+ *
+ * Description:
+ *   Stop WiFi station.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   0 if success or -1 if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_stop(void)
+{
+  int ret;
+
+  esp_wifi_lock(true);
+
+  ret = esp_wifi_stop();
+  if (ret)
+    {
+      wlinfo("INFO: Failed to stop WiFi ret=%d\n", ret);
+    }
+
+  g_sta_started = false;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+  if (g_softap_started)
+    {
+      ret = esp_wifi_set_mode(WIFI_MODE_AP);
+      if (ret)
+        {
+          wlerr("ERROR: Failed to set WiFi AP mode ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+
+      ret = esp_wifi_start();
+      if (ret)
+        {
+          wlerr("ERROR: Failed to start WiFi AP ret=%d\n", ret);
+          ret = wifi_errno_trans(ret);
+          goto errout_set_mode;
+        }
+    }
+#endif
+
+  wlinfo("INFO: OK to stop WiFi station\n");
+
+  esp_wifi_lock(false);
+  return OK;
+
+#ifdef ESP32_WLAN_HAS_SOFTAP
+errout_set_mode:
+  esp_wifi_lock(true);
+  return ret;
+#endif
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_send_data
+ *
+ * Description:
+ *   Use WiFi station interface to send 802.3 frame
+ *
+ * Input Parameters:
+ *   pbuf - Packet buffer pointer
+ *   len  - Packet length
+ *
+ * Returned Value:
+ *   0 if success or others if fail
+ *
+ ****************************************************************************/
+
+int esp_wifi_sta_send_data(void *pbuf, uint32_t len)
+{
+  int ret;
+
+  ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len);
+
+  return wifi_errno_trans(ret);
+}
+
+/****************************************************************************
+ * Name: esp_wifi_sta_register_recv_cb
+ *
+ * Description:
+ *   Regitser WiFi station receive packet callback function

Review comment:
       ```suggestion
    *   Register WiFi station receive packet callback function
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org