Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Smart Config #1139

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/PAL/Include/nanoPAL_Sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,14 @@ 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 reconOption);
int Network_Interface_Connect(int index, const char * ssid, const char * passphase, int options);
bool Network_Interface_Start_Scan(int index);
//--//

// Network_Interface_Connect options
#define NETWORK_CONNECT_SAVE_CONFIG 1
#define NETWORK_CONNECT_RECONNECT 2

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH System.Net.NetworkInformation.NetworkChange.NetworkEventType (in managed code) !!! //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 1 addition & 13 deletions src/PAL/Lwip/lwIP_Sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@

#include "LWIP_sockets.h"

// Make sure the ESP32 version of FD_xx macro are used
// as the socket number is offset
// FIXME - The problem is these MACROs are also defined in the newlib sys/types.h
#ifdef PLATFORM_ESP32
#undef FD_SET
#undef FD_CLR
#undef FD_ISSET
#undef FD_ZERO
#undef _types_fd_set
#undef fd_set
#endif

extern "C"
{
#include "lwip\init.h"
Expand Down Expand Up @@ -625,7 +613,7 @@ int LWIP_SOCKETS_Driver::Select( int nfds, SOCK_fd_set* readfds, SOCK_fd_set* wr
max_sd = LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN;
#endif

ret = lwip_select(max_sd, pR, pW, pE, (struct timeval *)timeout);
ret = select(max_sd, pR, pW, pE, (struct timeval *)timeout);

MARSHAL_FDSET_TO_SOCK_FDSET(readfds , pR);
MARSHAL_FDSET_TO_SOCK_FDSET(writefds , pW);
Expand Down
13 changes: 0 additions & 13 deletions targets/FreeRTOS/ESP32_DevKitC/Include/wifi_smart_config.h

This file was deleted.

10 changes: 0 additions & 10 deletions targets/FreeRTOS/ESP32_DevKitC/Lwip/nf_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@

#include "lwip/opt.h"

// FIXME should not need these
#ifdef PLATFORM_ESP32
#undef FD_SET
#undef FD_CLR
#undef FD_ISSET
#undef FD_ZERO
#undef _types_fd_set
#undef fd_set
#endif

#if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */

#include "lwip/sockets.h"
Expand Down
1 change: 1 addition & 0 deletions targets/FreeRTOS/ESP32_DevKitC/Network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ list(APPEND TARGET_ESP32_NETWORK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_
list(APPEND TARGET_ESP32_NETWORK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/target_Network.cpp")
list(APPEND TARGET_ESP32_NETWORK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Esp32_Ethernet_Lan8720.cpp")
list(APPEND TARGET_ESP32_NETWORK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Esp32_Wireless.cpp")
list(APPEND TARGET_ESP32_NETWORK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Esp32_SmartConfig.cpp")

# make var global
set(TARGET_ESP32_NETWORK_SOURCES ${TARGET_ESP32_NETWORK_SOURCES} CACHE INTERNAL "make global")
87 changes: 87 additions & 0 deletions targets/FreeRTOS/ESP32_DevKitC/Network/Esp32_SmartConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

#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)
{
switch (status) {
case SC_STATUS_WAIT:
ESP_LOGI(TAG, "SC_STATUS_WAIT");
break;

case SC_STATUS_FIND_CHANNEL:
ESP_LOGI(TAG, "SC_STATUS_FINDING_CHANNEL");
break;

case SC_STATUS_GETTING_SSID_PSWD:
ESP_LOGI(TAG, "SC_STATUS_GETTING_SSID_PSWD");
break;

case SC_STATUS_LINK:
ESP_LOGI(TAG, "SC_STATUS_LINK");
{
wifi_config_t *wifi_config = (wifi_config_t *)pdata;
ESP_LOGI(TAG, "SSID:%s", wifi_config->sta.ssid);
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
);

}
break;

case SC_STATUS_LINK_OVER:
ESP_LOGI(TAG, "SC_STATUS_LINK_OVER");
if (pdata != NULL)
{
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);
break;

default:
break;
}
}

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_LOGI(TAG, "smartconfig over");
esp_smartconfig_stop();
vEventGroupDelete(sc_wifi_event_group);
vTaskDelete(NULL);
}
}
}


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


50 changes: 38 additions & 12 deletions targets/FreeRTOS/ESP32_DevKitC/Network/Esp32_Wireless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extern "C"
}

static const char *TAG = "wifi";
static bool WifiInitialised = false;

// Forward / external references
struct netif * Esp32_find_netif(esp_interface_t esp_if);
int Esp32_Wait_NetNumber(esp_interface_t esp_if);
void Start_wifi_smart_config();


static bool WifiInitialised = false;

esp_err_t Esp32_InitaliseWifi()
{
Expand All @@ -28,6 +32,10 @@ esp_err_t Esp32_InitaliseWifi()
// Init WiFi Alloc resource for WiFi driver, such as WiFi control structure,
// RX/TX buffer, WiFi NVS structure etc, this WiFi also start WiFi task.
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

// Don't store Wireless params in NVS
cfg.nvs_enable = 0;

ec = esp_wifi_init(&cfg);
if ( ec != ESP_OK) return ec;

Expand Down Expand Up @@ -89,19 +97,14 @@ int Esp32_Wireless_Open(int index, HAL_Configuration_NetworkInterface * pConfig
{
Esp32_Wireless_Connect(pWireless);
}

// FIXME find a better way to get the netif ptr
struct netif *pNetIf;
while(true)
else
{
vTaskDelay(100 / portTICK_PERIOD_MS);
// Start Samrt config (if enabled (TODO) )
// probably best to have a config flag for this, but for now just start if no Wireless config set up
Start_wifi_smart_config();
}

// Return NetIf number for Esp32 wireless station
pNetIf = Esp32_find_netif(ESP_IF_WIFI_STA);
if (pNetIf != NULL) break;
}

return pNetIf->num;
return Esp32_Wait_NetNumber(ESP_IF_WIFI_STA);
}

bool Esp32_Wireless_Close(int index)
Expand Down Expand Up @@ -145,4 +148,27 @@ extern struct netif * Esp32_find_netif(esp_interface_t esp_if)
}
}
return NULL;
}

// Esp32_Wait_NetNumber
//
// Find the NetiF number used by esp_interface_t
// If not availbale then loop and wait for interface to start
//
int Esp32_Wait_NetNumber(esp_interface_t esp_if)
{
int number = 0;

// FIXME find a better way to get the netif ptr
struct netif *pNetIf;
while(true)
{
// Return NetIf number for Esp32 wireless station
pNetIf = Esp32_find_netif(esp_if);
if (pNetIf != NULL) break;

vTaskDelay(20 / portTICK_PERIOD_MS);
}

return pNetIf->num;
}
15 changes: 12 additions & 3 deletions targets/FreeRTOS/ESP32_DevKitC/Network/Target_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ int Esp32_Wireless_Scan();
int Esp32_Wireless_Disconnect();
int Esp32_Wireless_Connect(HAL_Configuration_Wireless80211 * pWireless);

bool StoreConfigBlock(DeviceConfigurationOption configType, uint32_t configurationIndex, void * pConfigBlock, size_t writeSize );


bool Network_Interface_Bind(int index)
{
Expand Down Expand Up @@ -118,18 +120,25 @@ bool GetWirelessConfig(int configIndex, HAL_Configuration_Wireless80211 ** pWire
//
// Connect to wireless network SSID using passphase
//
int Network_Interface_Connect(int configIndex, const char * ssid, const char * passphase, int reconOption)
int Network_Interface_Connect(int configIndex, const char * ssid, const char * passphase, int options)
{
(void)reconOption;

HAL_Configuration_Wireless80211 * pWireless;

if ( GetWirelessConfig(configIndex, & pWireless) == false ) return SOCK_SOCKET_ERROR;

// TODO update Reconnect option ( ) - first we need a space for it in config block
//pWireless->reconnect = options & NETWORK_CONNECT_RECONNECT;

// Update Wireless structure with new SSID and passphase
hal_strcpy_s( (char *)pWireless->Ssid, sizeof(pWireless->Ssid), ssid );
hal_strcpy_s( (char *)pWireless->Password, sizeof(pWireless->Password), passphase );

// Option to Save new config
if ( options & NETWORK_CONNECT_SAVE_CONFIG)
{
StoreConfigBlock( DeviceConfigurationOption_Wireless80211Network, configIndex, pWireless, sizeof(HAL_Configuration_Wireless80211) );
}

switch((tcpip_adapter_if_t)configIndex)
{
// Wireless
Expand Down
6 changes: 1 addition & 5 deletions targets/FreeRTOS/ESP32_DevKitC/nanoCLR/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <LaunchCLR.h>
#include <string.h>

#include <wifi_smart_config.h>

extern void CLRStartupThread(void const * argument);

// Mutex for GLOBAL_LOCK / GLOBAL_UNLOCK
Expand Down Expand Up @@ -66,9 +64,7 @@ void app_main()

ESP_ERROR_CHECK( nvs_flash_init() );

//initialise_wifi_smart_config();

Esp32FlashDriver_InitializeDevice(0);
Esp32FlashDriver_InitializeDevice(0);

xTaskCreatePinnedToCore(&receiver_task, "ReceiverThread", 2048, NULL, 5, NULL, 1);

Expand Down