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:
us...@infra.apache.org


Reply via email to