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

Improve network configuration block management #1422

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# append common source files
list(APPEND COMMON_PROJECT_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Device_BlockStorage$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:-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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright (c) 2019 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

#include <nanoHAL.h>
#include <nanoHAL_v2.h>
#include <nanoWeak.h>
#include <Target_BlockStorage_STM32FlashDriver.h>

// 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;
}
59 changes: 39 additions & 20 deletions targets/CMSIS-OS/ChibiOS/common/targetHAL_ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down