From e7bf3fe33042d812694c32163528650cf1389781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Fri, 26 Jul 2019 14:55:09 +0100 Subject: [PATCH] Improve network configuration blocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add default network config block for STM32F769I using default STM MAC address. - Rework logic to create default network config block on enumeration, only. - Add setting to use auto DNS on default configuration. - Default config block for ESP32 now grabs default MAC from device. Signed-off-by: José Simões --- .../common/CMakeLists.txt | 1 + .../common/targetHAL_ConfigurationManager.cpp | 35 +++++++++++ .../common/targetHAL_ConfigurationManager.cpp | 59 ++++++++++++------- .../targetHAL_ConfigurationManager.cpp | 10 +++- .../targetHAL_ConfigurationManager_CC32xx.cpp | 49 +++++---------- 5 files changed, 100 insertions(+), 54 deletions(-) create mode 100644 targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/targetHAL_ConfigurationManager.cpp diff --git a/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/CMakeLists.txt b/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/CMakeLists.txt index 01e0f5b98f..7c21ff9b57 100644 --- a/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/CMakeLists.txt +++ b/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/CMakeLists.txt @@ -5,6 +5,7 @@ # append common source files list(APPEND COMMON_PROJECT_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Device_BlockStorage$<$,$>:-DEBUG>.c") +list(APPEND COMMON_PROJECT_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/targetHAL_ConfigurationManager.cpp") # make var global set(COMMON_PROJECT_SOURCES ${COMMON_PROJECT_SOURCES} CACHE INTERNAL "make global") diff --git a/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/targetHAL_ConfigurationManager.cpp b/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/targetHAL_ConfigurationManager.cpp new file mode 100644 index 0000000000..178f6aa783 --- /dev/null +++ b/targets/CMSIS-OS/ChibiOS/ST_STM32F769I_DISCOVERY/common/targetHAL_ConfigurationManager.cpp @@ -0,0 +1,35 @@ +// +// Copyright (c) 2019 The nanoFramework project contributors +// See LICENSE file in the project root for full license information. +// + +#include +#include +#include +#include + +// Default initialisation for Network interface config blocks +// strong implementation replacing ChibiOS 'weak' one +bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface * pconfig, uint32_t configurationIndex) +{ + (void)configurationIndex; + + // make sure the config block marker is set + memcpy(pconfig->Marker, c_MARKER_CONFIGURATION_NETWORK_V1, sizeof(c_MARKER_CONFIGURATION_NETWORK_V1)); + + pconfig->InterfaceType = NetworkInterfaceType_Ethernet; + pconfig->StartupAddressMode = AddressMode_DHCP; + pconfig->AutomaticDNS = 1; + pconfig->SpecificConfigId = 0; + + // set MAC address with ST provided MAC for development boards + // 00:80:E1:01:35:D1 + pconfig->MacAddress[0] = 0x00; + pconfig->MacAddress[1] = 0x80; + pconfig->MacAddress[2] = 0xE1; + pconfig->MacAddress[3] = 0x01; + pconfig->MacAddress[4] = 0x35; + pconfig->MacAddress[5] = 0xD1; + + return true; +} diff --git a/targets/CMSIS-OS/ChibiOS/common/targetHAL_ConfigurationManager.cpp b/targets/CMSIS-OS/ChibiOS/common/targetHAL_ConfigurationManager.cpp index 115bcabb0c..705536722c 100644 --- a/targets/CMSIS-OS/ChibiOS/common/targetHAL_ConfigurationManager.cpp +++ b/targets/CMSIS-OS/ChibiOS/common/targetHAL_ConfigurationManager.cpp @@ -26,6 +26,24 @@ __nfweak void ConfigurationManager_EnumerateConfigurationBlocks() // find network configuration blocks HAL_CONFIGURATION_NETWORK* networkConfigs = (HAL_CONFIGURATION_NETWORK*)ConfigurationManager_FindNetworkConfigurationBlocks((uint32_t)&__nanoConfig_start__, (uint32_t)&__nanoConfig_end__); + // check network configs count + if(networkConfigs->Count == 0) + { + // there is no network config block available, get a default + HAL_Configuration_NetworkInterface* networkConfig = (HAL_Configuration_NetworkInterface*)platform_malloc(sizeof(HAL_Configuration_NetworkInterface)); + + if(InitialiseNetworkDefaultConfig(networkConfig, 0)) + { + // config block created, store it + ConfigurationManager_StoreConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, 0, sizeof(HAL_Configuration_NetworkInterface), 0); + + // have to enumerate again to pick it up + networkConfigs = (HAL_CONFIGURATION_NETWORK*)ConfigurationManager_FindNetworkConfigurationBlocks((uint32_t)&__nanoConfig_start__, (uint32_t)&__nanoConfig_end__); + } + + platform_free(networkConfig); + } + // find wireless 80211 network configuration blocks HAL_CONFIGURATION_NETWORK_WIRELESS80211* networkWirelessConfigs = (HAL_CONFIGURATION_NETWORK_WIRELESS80211*)ConfigurationManager_FindNetworkWireless80211ConfigurationBlocks((uint32_t)&__nanoConfig_start__, (uint32_t)&__nanoConfig_end__); @@ -71,20 +89,11 @@ __nfweak bool ConfigurationManager_GetConfigurationBlock(void* configurationBloc // requested Index has to exist (array index starts at zero, so need to add one) if(configuration == DeviceConfigurationOption_Network) { - if(g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0) + if( g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0 || + (configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) { - // there is no network config block, init one with default settings - if(!InitialiseNetworkDefaultConfig(NULL, 0)) - { - return FALSE; - } - } - else - { - if((configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) - { - return FALSE; - } + // the requested config block is beyond the available count + return false; } // set block size @@ -142,16 +151,26 @@ __nfweak bool ConfigurationManager_StoreConfigurationBlock(void* configurationBl if(configuration == DeviceConfigurationOption_Network) { - if( g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0 || - (configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) + if( g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0 && + configurationIndex == 0 ) { - // there is no room for this block, or there are no blocks stored at all - // failing the operation - return FALSE; + // there is no network config block, we are storing the default one + // THIS IS THE ONLY CONFIG BLOCK THAT'S AUTO-CREATED + // OK to continue + // set storage address as the start of the flash configuration sector + storageAddress = (ByteAddress)&__nanoConfig_start__; } + else + { + // the requested config block is beyond the available count + if((configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) + { + return FALSE; + } - // set storage address from block address, plus the requested offset - storageAddress = (ByteAddress)g_TargetConfiguration.NetworkInterfaceConfigs->Configs[configurationIndex] + offset; + // set storage address from block address, plus the requested offset + storageAddress = (ByteAddress)g_TargetConfiguration.NetworkInterfaceConfigs->Configs[configurationIndex] + offset; + } // set block size, in case it's not already set blockSize = sizeof(HAL_Configuration_NetworkInterface); diff --git a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetHAL_ConfigurationManager.cpp b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetHAL_ConfigurationManager.cpp index 65d3116724..06aa3afc57 100644 --- a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetHAL_ConfigurationManager.cpp +++ b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/targetHAL_ConfigurationManager.cpp @@ -294,12 +294,16 @@ void InitialiseWirelessDefaultConfig(HAL_Configuration_Wireless80211 * pconfig, bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface * pconfig, uint32_t configurationIndex) { memset( pconfig, 0, sizeof(HAL_Configuration_NetworkInterface)); - + + // make sure the config block marker is set + memcpy(pconfig->Marker, c_MARKER_CONFIGURATION_NETWORK_V1, sizeof(c_MARKER_CONFIGURATION_NETWORK_V1)); + switch(configurationIndex) { case 0: // Wireless Station pconfig->InterfaceType = NetworkInterfaceType_Wireless80211; pconfig->StartupAddressMode = AddressMode_DHCP; + pconfig->AutomaticDNS = 1; pconfig->SpecificConfigId = 0; break; @@ -316,9 +320,13 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface * pconfig case 2: // Ethernet pconfig->InterfaceType = NetworkInterfaceType_Ethernet; pconfig->StartupAddressMode = AddressMode_DHCP; + pconfig->AutomaticDNS = 1; break; } + // get default MAC + esp_efuse_mac_get_default(pconfig->MacAddress); + // always good return TRUE; } diff --git a/targets/TI-SimpleLink/common/targetHAL_ConfigurationManager_CC32xx.cpp b/targets/TI-SimpleLink/common/targetHAL_ConfigurationManager_CC32xx.cpp index 72260c0537..ad3c99a7d6 100644 --- a/targets/TI-SimpleLink/common/targetHAL_ConfigurationManager_CC32xx.cpp +++ b/targets/TI-SimpleLink/common/targetHAL_ConfigurationManager_CC32xx.cpp @@ -125,14 +125,16 @@ void ConfigurationManager_EnumerateConfigurationBlocks() { // there is no network config block available, get a default HAL_Configuration_NetworkInterface* networkConfig = (HAL_Configuration_NetworkInterface*)platform_malloc(sizeof(HAL_Configuration_NetworkInterface)); - InitialiseNetworkDefaultConfig(networkConfig, 0); - - // store it - ConfigurationManager_StoreConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, 0, sizeof(HAL_Configuration_NetworkInterface), 0); - platform_free(networkConfig); + if(InitialiseNetworkDefaultConfig(networkConfig, 0)) + { + // config block created, store it + ConfigurationManager_StoreConfigurationBlock(networkConfig, DeviceConfigurationOption_Network, 0, sizeof(HAL_Configuration_NetworkInterface), 0); + + // have to enumerate again to pick it up + networkConfigs = (HAL_CONFIGURATION_NETWORK*)ConfigurationManagerCC32xx_FindNetworkConfigurationBlocks(); + } - // have to enumerate again to pick it up - networkConfigs = (HAL_CONFIGURATION_NETWORK*)ConfigurationManagerCC32xx_FindNetworkConfigurationBlocks(); + platform_free(networkConfig); } // find wireless 80211 network configuration blocks @@ -228,33 +230,11 @@ bool ConfigurationManager_GetConfigurationBlock(void* configurationBlock, Device // requested Index has to exist (array index starts at zero, so need to add one) if(configuration == DeviceConfigurationOption_Network) { - if(g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0) + if( g_TargetConfiguration.NetworkInterfaceConfigs->Count == 0 || + (configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) { - // there is no network config block, init one with default settings - if(InitialiseNetworkDefaultConfig((HAL_Configuration_NetworkInterface*)configurationBlock, 0)) - { - // force storing profile - if(ConfigurationManager_StoreConfigurationBlock(configurationBlock, DeviceConfigurationOption_Network, 0, sizeof(HAL_Configuration_NetworkInterface), 0)) - { - // need to enumerate blocks - ConfigurationManager_EnumerateConfigurationBlocks(); - - // done here - return true; - } - else - { - // couldn't store the config block - return false; - } - } - } - else - { - if((configurationIndex + 1) > g_TargetConfiguration.NetworkInterfaceConfigs->Count) - { - return false; - } + // the requested config block is beyond the available count + return false; } } else if(configuration == DeviceConfigurationOption_Wireless80211Network) @@ -510,6 +490,8 @@ void InitialiseWirelessDefaultConfig(HAL_Configuration_Wireless80211 * pconfig, // Default initialisation for Network interface config blocks bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface * pconfig, uint32_t configurationIndex) { + (void)configurationIndex; + uint16_t macAddressLen = SL_MAC_ADDR_LEN; memset(pconfig, 0, sizeof(HAL_Configuration_NetworkInterface)); @@ -519,6 +501,7 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface * pconfig pconfig->InterfaceType = NetworkInterfaceType_Wireless80211; pconfig->StartupAddressMode = AddressMode_DHCP; + pconfig->AutomaticDNS = 1; pconfig->SpecificConfigId = 0; sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen, pconfig->MacAddress);