Skip to content

Commit

Permalink
Fix WiFi Connect (#1952)
Browse files Browse the repository at this point in the history
* Fix WiFi Connect

* Code style fixes

Automated fixes for code style.

Co-authored-by: nfbot <[email protected]>
  • Loading branch information
AdrianSoundy and nfbot authored Jun 14, 2021
1 parent 1628a18 commit 911a364
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 68 deletions.
5 changes: 5 additions & 0 deletions src/CLR/Core/Hardware/Hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ void CLR_HW_Hardware::ProcessActivity()
eventsCLR |= Event_Radio;
}

if (events & SYSTEM_EVENT_FLAG_WIFI_STATION)
{
eventsCLR |= Event_Wifi_Station;
}

if (eventsCLR)
{
g_CLR_RT_ExecutionEngine.SignalEvents(eventsCLR);
Expand Down
3 changes: 2 additions & 1 deletion src/CLR/Include/nanoCLR_Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct CLR_HW_Hardware
SYSTEM_EVENT_FLAG_COM_IN | SYSTEM_EVENT_FLAG_COM_OUT | SYSTEM_EVENT_FLAG_STORAGE_IO |
SYSTEM_EVENT_FLAG_SYSTEM_TIMER | SYSTEM_EVENT_FLAG_SPI_MASTER | SYSTEM_EVENT_FLAG_I2C_MASTER |
SYSTEM_EVENT_HW_INTERRUPT | SYSTEM_EVENT_FLAG_SOCKET | SYSTEM_EVENT_FLAG_DEBUGGER_ACTIVITY |
SYSTEM_EVENT_FLAG_MESSAGING_ACTIVITY | SYSTEM_EVENT_FLAG_ONEWIRE_MASTER | SYSTEM_EVENT_FLAG_RADIO;
SYSTEM_EVENT_FLAG_MESSAGING_ACTIVITY | SYSTEM_EVENT_FLAG_ONEWIRE_MASTER | SYSTEM_EVENT_FLAG_RADIO |
SYSTEM_EVENT_FLAG_WIFI_STATION;

//--//

Expand Down
1 change: 1 addition & 0 deletions src/CLR/Include/nanoCLR_Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -3370,6 +3370,7 @@ typedef enum Events
Event_SpiMaster = 0x00000100,
Event_OneWireMaster = 0x00000200,
Event_Radio = 0x00000400,
Event_Wifi_Station = 0x00000800,
Event_AppDomain = 0x02000000,
Event_Socket = 0x20000000,
Event_IdleCPU = 0x40000000,
Expand Down
2 changes: 1 addition & 1 deletion src/HAL/Include/nanoHAL_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef enum SLEEP_LEVEL
//#define SYSTEM_EVENT_FLAG_UNUSED_0x00200000 0x00200000
//#define SYSTEM_EVENT_FLAG_UNUSED_0x00400000 0x00400000
//#define SYSTEM_EVENT_FLAG_UNUSED_0x00800000 0x00800000
//#define SYSTEM_EVENT_FLAG_UNUSED_0x01000000 0x01000000
#define SYSTEM_EVENT_FLAG_WIFI_STATION 0x01000000
#define SYSTEM_EVENT_FLAG_SPI_MASTER 0x02000000
#define SYSTEM_EVENT_FLAG_I2C_MASTER 0x04000000
#define SYSTEM_EVENT_HW_INTERRUPT 0x08000000
Expand Down
3 changes: 2 additions & 1 deletion src/PAL/Include/nanoPAL_Sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ bool Network_Interface_Bind(int index);
int Network_Interface_Open(int index);
bool Network_Interface_Close(int index);
int Network_Interface_Disconnect(int index);
int Network_Interface_Connect(int index, const char *ssid, const char *passphase, int options);
int Network_Interface_Start_Connect(int index, const char *ssid, const char *passphase, int options);
int Network_Interface_Connect_Result(int configIndex);
bool Network_Interface_Start_Scan(int index);

// Wireless AP methods
Expand Down
43 changes: 21 additions & 22 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/Network/Esp32_SmartConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include <nanoHAL.h>
#include "esp32_os.h"
#include "esp_smartconfig.h"

static const int ESPTOUCH_DONE_BIT = BIT1;
static const char *TAG = "sc";

static EventGroupHandle_t sc_wifi_event_group;

static void sc_callback(smartconfig_status_t status, void *pdata)
static void sc_callback(smartconfig_status_t status, void *pdata)
{
switch (status) {
switch (status)
{
case SC_STATUS_WAIT:
ESP_LOGI(TAG, "SC_STATUS_WAIT");
break;
Expand All @@ -35,21 +36,20 @@ static void sc_callback(smartconfig_status_t status, void *pdata)
ESP_LOGI(TAG, "PASSWORD:%s", wifi_config->sta.password);

// Try to connect and Save config
Network_Interface_Connect( 0,
(const char *)wifi_config->sta.ssid,
(const char *)wifi_config->sta.password,
NETWORK_CONNECT_SAVE_CONFIG + NETWORK_CONNECT_RECONNECT
);

Network_Interface_Start_Connect(
0,
(const char *)wifi_config->sta.ssid,
(const char *)wifi_config->sta.password,
NETWORK_CONNECT_SAVE_CONFIG + NETWORK_CONNECT_RECONNECT);
}
break;

case SC_STATUS_LINK_OVER:
ESP_LOGI(TAG, "SC_STATUS_LINK_OVER");
if (pdata != NULL)
if (pdata != NULL)
{
uint8_t phone_ip[4] = { 0 };
memcpy(phone_ip, (uint8_t* )pdata, 4);
uint8_t phone_ip[4] = {0};
memcpy(phone_ip, (uint8_t *)pdata, 4);
ESP_LOGI(TAG, "Phone ip: %d.%d.%d.%d\n", phone_ip[0], phone_ip[1], phone_ip[2], phone_ip[3]);
}
xEventGroupSetBits(sc_wifi_event_group, ESPTOUCH_DONE_BIT);
Expand All @@ -60,16 +60,18 @@ static void sc_callback(smartconfig_status_t status, void *pdata)
}
}

void smartconfig_task(void * parm)
void smartconfig_task(void *parm)
{
EventBits_t uxBits;

sc_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
ESP_ERROR_CHECK( esp_smartconfig_start(sc_callback) );
while (1) {
uxBits = xEventGroupWaitBits(sc_wifi_event_group, ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if(uxBits & ESPTOUCH_DONE_BIT) {
ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH));
ESP_ERROR_CHECK(esp_smartconfig_start(sc_callback));
while (1)
{
uxBits = xEventGroupWaitBits(sc_wifi_event_group, ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if (uxBits & ESPTOUCH_DONE_BIT)
{
ESP_LOGI(TAG, "smartconfig over");
esp_smartconfig_stop();
vEventGroupDelete(sc_wifi_event_group);
Expand All @@ -78,10 +80,7 @@ void smartconfig_task(void * parm)
}
}


void Start_wifi_smart_config(void)
{
xTaskCreate(smartconfig_task, "smartconfig_task", 4096, NULL, 3, NULL);
}


Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ esp_err_t Esp32_InitaliseWifi()
return ec;
}

esp_err_t Esp32_Wireless_Connect(HAL_Configuration_Wireless80211 *pWireless)
esp_err_t Esp32_Wireless_Start_Connect(HAL_Configuration_Wireless80211 *pWireless)
{
esp_err_t ec;

Expand All @@ -162,7 +162,7 @@ esp_err_t Esp32_Wireless_Connect(HAL_Configuration_Wireless80211 *pWireless)
return ec;

ec = esp_wifi_connect();
ESP_LOGI(TAG, "WiFi Connect to %s result %d", sta_config.sta.ssid, ec);
ESP_LOGI(TAG, "WiFi Start Connect to %s result %d", sta_config.sta.ssid, ec);
if (ec != ESP_OK)
return ec;

Expand Down Expand Up @@ -210,7 +210,7 @@ int Esp32_Wireless_Open(int index, HAL_Configuration_NetworkInterface *pConfig)
if ((pWireless->Options & Wireless80211Configuration_ConfigurationOptions_AutoConnect) &&
(hal_strlen_s((const char *)pWireless->Ssid) > 0))
{
Esp32_Wireless_Connect(pWireless);
Esp32_Wireless_Start_Connect(pWireless);

// Maybe remove SmartConfig flag if conected ok
}
Expand Down
30 changes: 25 additions & 5 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/Network/Target_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ int Esp32_Ethernet_Open(int index, HAL_Configuration_NetworkInterface *config);
bool Esp32_Ethernet_Close(int index);
int Esp32_Wireless_Scan();
int Esp32_Wireless_Disconnect();
int Esp32_Wireless_Connect(HAL_Configuration_Wireless80211 *pWireless);
int Esp32_Wireless_Start_Connect(HAL_Configuration_Wireless80211 *pWireless);

bool Esp32_ConnectInProgress = false;
int Esp32_ConnectResult = 0;

bool StoreConfigBlock(
DeviceConfigurationOption configType,
Expand Down Expand Up @@ -122,7 +125,7 @@ bool GetWirelessConfig(int configIndex, HAL_Configuration_Wireless80211 **pWirel
//
// Connect to wireless network SSID using passphase
//
int Network_Interface_Connect(int configIndex, const char *ssid, const char *passphase, int options)
int Network_Interface_Start_Connect(int configIndex, const char *ssid, const char *passphase, int options)
{
HAL_Configuration_Wireless80211 *pWireless;

Expand All @@ -133,15 +136,19 @@ int Network_Interface_Connect(int configIndex, const char *ssid, const char *pas

if (options & NETWORK_CONNECT_RECONNECT)
{
// need this stupid cast because the current gcc version with ESP32 IDF is not happy with the simple sintax '|='
// need this stupid cast because the current gcc version with ESP32 IDF is not happy with the simple syntax '|='
pWireless->Options = (Wireless80211Configuration_ConfigurationOptions)(
pWireless->Options | Wireless80211Configuration_ConfigurationOptions_AutoConnect);
}
else
{
// need this stupid cast because the current gcc version with ESP32 IDF is not happy with the simple sintax '^='
// need this stupid cast because the current gcc version with ESP32 IDF is not happy with the simple syntax '^='
pWireless->Options = (Wireless80211Configuration_ConfigurationOptions)(
pWireless->Options ^ Wireless80211Configuration_ConfigurationOptions_AutoConnect);

// Make sure we are still enabled because AutoConnect includes Enable
pWireless->Options = (Wireless80211Configuration_ConfigurationOptions)(
pWireless->Options | Wireless80211Configuration_ConfigurationOptions_Enable);
}

// Update Wireless structure with new SSID and passphase
Expand All @@ -162,14 +169,27 @@ int Network_Interface_Connect(int configIndex, const char *ssid, const char *pas
{
// Wireless
case TCPIP_ADAPTER_IF_STA:
esp_err_t err = Esp32_Wireless_Connect(pWireless);
Esp32_ConnectInProgress = true;
esp_err_t err = Esp32_Wireless_Start_Connect(pWireless);

return (int)err;
}

return SOCK_SOCKET_ERROR;
}

int Network_Interface_Connect_Result(int configIndex)
{
switch ((tcpip_adapter_if_t)configIndex)
{
// Wireless
case TCPIP_ADAPTER_IF_STA:
return Esp32_ConnectInProgress ? -1 : Esp32_ConnectResult;
}

return SOCK_SOCKET_ERROR;
}

int Network_Interface_Disconnect(int configIndex)
{
switch ((tcpip_adapter_if_t)configIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <target_lwip_sntp_opts.h>

extern "C" void set_signal_sock_function(void (*funcPtr)());
extern bool Esp32_ConnectInProgress;
extern int Esp32_ConnectResult;

#define WIFI_EVENT_TYPE_SCAN_COMPLETE 1

Expand Down Expand Up @@ -57,6 +59,19 @@ static void PostAPStationChanged(uint connect, uint netInfo)
Network_PostEvent(NetworkChange_NetworkEventType_APStationChanged, connect, netInfo);
}

static void PostConnectResult(int result)
{
Esp32_ConnectResult = result;
Esp32_ConnectInProgress = false;

#ifdef NetEventPrint
ets_printf("PostConnectResult reason : %d\n", result);
#endif

// fire event for Wifi station job complete
Events_Set(SYSTEM_EVENT_FLAG_WIFI_STATION);
}

static void initialize_sntp()
{
sntp_stop();
Expand All @@ -82,8 +97,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
{
// Wifi station events
case SYSTEM_EVENT_STA_START:
// Smart config commented out as giving exception when running
// xTaskCreate(smartconfig_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
#ifdef NetEventPrint
ets_printf("SYSTEM_EVENT_STA_START\n");
#endif
Expand All @@ -95,7 +108,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
#endif
PostAddressChanged(TCPIP_ADAPTER_IF_STA);
initialize_sntp();
// xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;

case SYSTEM_EVENT_STA_LOST_IP:
Expand All @@ -109,16 +121,28 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
#ifdef NetEventPrint
ets_printf("SYSTEM_EVENT_STA_CONNECTED\n");
#endif
if (Esp32_ConnectInProgress)
{
PostConnectResult(0);
}

PostAvailabilityOn(TCPIP_ADAPTER_IF_STA);
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
#ifdef NetEventPrint
ets_printf("SYSTEM_EVENT_STA_DISCONNECTED\n");
ets_printf("SYSTEM_EVENT_STA_DISCONNECTED reason : %d\n", event->event_info.disconnected.reason);
#endif
PostAvailabilityOff(TCPIP_ADAPTER_IF_STA);
esp_wifi_connect();
// xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);

if (Esp32_ConnectInProgress)
{
PostConnectResult(event->event_info.disconnected.reason);
}
else
{
PostAvailabilityOff(TCPIP_ADAPTER_IF_STA);
esp_wifi_connect();
}
break;

// Scan of available Wifi networks complete
Expand Down Expand Up @@ -192,7 +216,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
PostAPStationChanged(0, TCPIP_ADAPTER_IF_AP + (stationIndex << 8));
#ifdef NetEventPrint
ets_printf("SYSTEM_EVENT_AP_STADISCONNECTED %d\n", event->event_info.sta_disconnected.aid);
PrintStations();
#endif
break;

Expand Down
Loading

0 comments on commit 911a364

Please sign in to comment.