Skip to content

Commit

Permalink
[Mbed] Update Mbed platform implementation (#18611)
Browse files Browse the repository at this point in the history
* Fix mbed examples debugging

Add user access to openocd in VSCode Docker file
Update the arm toolchain path in launch.json
Update path to server setting name and set correct value
for cortex-debug extension in launch.json

* Autonomous connection status for network commissioning

Add NetworkStatusChangeCallback service in network
commissioning driver
Add last disconnect reason with getter and setter
Implement OnNetworkStatusChange function

* Fix key-value builder

Key value must not inlude some specific characters in mbed lib
They will be replaced by '-' sign

* Update configuration manager implementation

Add reboot count, uptime, total operational hours keys
Implement get/set reboot count functions
Implement get/set reboot total operational hours
Improve init function - add generic class initalization
  • Loading branch information
ATmobica authored and pull[bot] committed Oct 2, 2023
1 parent 819949a commit 1227707
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ RUN chown -R $USERNAME:$USERNAME /opt/ameba/ambd_sdk_with_chip_non_NDA/
RUN chown -R $USERNAME:$USERNAME /opt/sdk/sdks/

RUN chown -R $USERNAME:$USERNAME /opt/fsl-imx-xwayland/5.10-hardknott/

# Add access to openocd for VSCode debugging
RUN chown -R $USERNAME:$USERNAME /opt/openocd
30 changes: 15 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-${input:mbedApp}-example.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"serverpath": "${env:OPENOCD_PATH}/bin/openocd",
"servertype": "openocd",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
Expand Down Expand Up @@ -130,7 +130,7 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-${input:mbedApp}-example.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "external",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
Expand All @@ -157,8 +157,8 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-${input:mbedApp}-example.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"openocdPath": "${env:OPENOCD_PATH/bin}",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"serverpath": "${env:OPENOCD_PATH}/bin/openocd",
"servertype": "openocd",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
Expand All @@ -182,7 +182,7 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/${input:mbedApp}/mbed",
"executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-${input:mbedApp}-example.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "external",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
Expand All @@ -200,9 +200,9 @@
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed/unit_tests",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-unit-tests.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"serverpath": "${env:OPENOCD_PATH}/bin/openocd",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH}/scripts"
Expand Down Expand Up @@ -233,7 +233,7 @@
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed/unit_tests",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-unit-tests.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "external",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
Expand All @@ -260,9 +260,9 @@
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed//unit_tests",
"executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-unit-tests.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"serverpath": "${env:OPENOCD_PATH}/bin/openocd",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH/scripts}"
Expand All @@ -285,7 +285,7 @@
"request": "launch",
"cwd": "${workspaceRoot}/src/test_driver/mbed/unit_tests",
"executable": "./build-${input:mbedTarget}/${input:mbedFlashProfile}/chip-mbed-unit-tests.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "external",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
Expand All @@ -303,9 +303,9 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/platform/mbed/bootloader",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-bootloader.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "openocd",
"openocdPath": "${env:OPENOCD_PATH/bin}",
"serverpath": "${env:OPENOCD_PATH}/bin/openocd",
"searchDir": [
"${workspaceRoot}/config/mbed/scripts",
"${env:OPENOCD_PATH}/scripts"
Expand Down Expand Up @@ -336,7 +336,7 @@
"request": "launch",
"cwd": "${workspaceRoot}/examples/platform/mbed/bootloader",
"executable": "./build-${input:mbedTarget}/${input:mbedDebugProfile}/chip-mbed-bootloader.elf",
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/pigweed/bin/", // Pigweed environment bootstraping required
"armToolchainPath": "${env:PW_ENVIRONMENT_ROOT}/cipd/packages/arm/bin/", // Pigweed environment bootstraping required
"servertype": "external",
"gdbTarget": "host.docker.internal:3334", //port 3333 for the CM0+, 3334 for the CM4
"overrideLaunchCommands": [
Expand Down
51 changes: 50 additions & 1 deletion src/platform/mbed/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,56 @@ ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()

CHIP_ERROR ConfigurationManagerImpl::Init()
{
return CHIP_NO_ERROR;
CHIP_ERROR err;
uint32_t rebootCount;

if (MbedConfig::ConfigValueExists(MbedConfig::kCounterKey_RebootCount))
{
err = GetRebootCount(rebootCount);
SuccessOrExit(err);

err = StoreRebootCount(rebootCount + 1);
SuccessOrExit(err);
}
else
{
// The first boot after factory reset of the Node.
err = StoreRebootCount(1);
SuccessOrExit(err);
}

if (!MbedConfig::ConfigValueExists(MbedConfig::kCounterKey_TotalOperationalHours))
{
err = StoreTotalOperationalHours(0);
SuccessOrExit(err);
}

// Initialize the generic implementation base class.
err = Internal::GenericConfigurationManagerImpl<MbedConfig>::Init();
SuccessOrExit(err);

exit:
return err;
}

CHIP_ERROR ConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount)
{
return ReadConfigValue(MbedConfig::kCounterKey_RebootCount, rebootCount);
}

CHIP_ERROR ConfigurationManagerImpl::StoreRebootCount(uint32_t rebootCount)
{
return WriteConfigValue(MbedConfig::kCounterKey_RebootCount, rebootCount);
}

CHIP_ERROR ConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
return ReadConfigValue(MbedConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
}

CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOperationalHours)
{
return WriteConfigValue(MbedConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
Expand Down
5 changes: 5 additions & 0 deletions src/platform/mbed/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
// This returns an instance of this class.
static ConfigurationManagerImpl & GetDefaultInstance();

CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override;
CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override;

private:
// ===== Members that implement the ConfigurationManager public interface.

Expand Down
2 changes: 2 additions & 0 deletions src/platform/mbed/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
CHIP_ERROR InitWiFi(void);
void OnWiFiPlatformEvent(const ChipDeviceEvent * event);

void ChangeWiFiStationState(WiFiStationState newState);

WiFiStationMode _GetWiFiStationMode();
CHIP_ERROR _SetWiFiStationMode(WiFiStationMode val);
bool _IsWiFiStationEnabled();
Expand Down
23 changes: 20 additions & 3 deletions src/platform/mbed/ConnectivityManagerImpl_WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,37 @@ void ConnectivityManagerImpl::OnWiFiPlatformEvent(const ChipDeviceEvent * event)
{
if (event->Type == DeviceEventType::kWiFiConnectivityChange)
{
WiFiStationState wifiStaState;
switch (event->WiFiConnectivityChange.Result)
{
case kConnectivity_NoChange:
mWiFiStationState = kWiFiStationState_Connecting;
wifiStaState = kWiFiStationState_Connecting;
break;
case kConnectivity_Established:
ChipLogProgress(DeviceLayer, "WiFi station connected");
mWiFiStationState = kWiFiStationState_Connected;
wifiStaState = kWiFiStationState_Connected;
break;
case kConnectivity_Lost:
ChipLogProgress(DeviceLayer, "WiFi station disconnected");
mWiFiStationState = kWiFiStationState_NotConnected;
wifiStaState = kWiFiStationState_NotConnected;
NetworkCommissioning::WiFiDriverImpl::GetInstance().SetLastDisconnectReason(event);
break;
default:
ChipLogError(DeviceLayer, "Unknown WiFi connectivity state");
return;
}
ChangeWiFiStationState(wifiStaState);
}
}

void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState)
{
if (mWiFiStationState != newState)
{
ChipLogProgress(DeviceLayer, "WiFi station state change: %s -> %s", WiFiStationStateToStr(mWiFiStationState),
WiFiStationStateToStr(newState));
mWiFiStationState = newState;
SystemLayer().ScheduleLambda([]() { NetworkCommissioning::WiFiDriverImpl::GetInstance().OnNetworkStatusChange(); });
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/platform/mbed/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class KeyBuilder
{
auto ret = snprintf(buffer, sizeof(buffer), "%s%s", prefix, key);
valid = (ret > 0) && ((size_t) ret <= (sizeof(buffer) - 1));
// Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
// It's replaced by '-'
char * ptr = buffer + strlen(prefix);
while ((ptr = strpbrk(ptr, " */?:;\"|<>\\")) != NULL)
{
*ptr = '-';
}
}

const char * str() const { return valid ? buffer : nullptr; }
Expand Down
6 changes: 6 additions & 0 deletions src/platform/mbed/MbedConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace Internal {

#define FACTORY_KEY(key) CHIP_CONFIG_KV_STORE_PARTITION CHIP_CONFIG_FACTORY_PREFIX key
#define CONFIG_KEY(key) CHIP_CONFIG_KV_STORE_PARTITION CHIP_CONFIG_CONFIG_PREFIX key
#define COUNTER_KEY(key) CHIP_CONFIG_KV_STORE_PARTITION CHIP_CONFIG_COUNTER_PREFIX key

const char MbedConfig::kConfigNamespace_ChipFactory[] = CHIP_CONFIG_KV_STORE_PARTITION CHIP_CONFIG_FACTORY_PREFIX;
const char MbedConfig::kConfigNamespace_ChipConfig[] = CHIP_CONFIG_KV_STORE_PARTITION CHIP_CONFIG_CONFIG_PREFIX;
Expand Down Expand Up @@ -85,6 +86,11 @@ const MbedConfig::Key MbedConfig::kConfigKey_RegulatoryLocation = { CONFIG_KEY("
const MbedConfig::Key MbedConfig::kConfigKey_CountryCode = { CONFIG_KEY("country-code") };
const MbedConfig::Key MbedConfig::kConfigKey_UniqueId = { CONFIG_KEY("unique-id") };

// Keys stored in the Chip-counters namespace
const MbedConfig::Key MbedConfig::kCounterKey_RebootCount = { COUNTER_KEY("reboot-count") };
const MbedConfig::Key MbedConfig::kCounterKey_UpTime = { COUNTER_KEY("up-time") };
const MbedConfig::Key MbedConfig::kCounterKey_TotalOperationalHours = { COUNTER_KEY("total-hours") };

CHIP_ERROR MbedConfig::ReadConfigValue(Key key, bool & val)
{
if (!ConfigValueExists(key))
Expand Down
5 changes: 5 additions & 0 deletions src/platform/mbed/MbedConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class MbedConfig
static const Key kConfigKey_Spake2pSalt;
static const Key kConfigKey_Spake2pVerifier;

// CHIP Counter keys
static const Key kCounterKey_RebootCount;
static const Key kCounterKey_UpTime;
static const Key kCounterKey_TotalOperationalHours;

// Config value accessors.
static CHIP_ERROR ReadConfigValue(Key key, bool & val);
static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
Expand Down
8 changes: 8 additions & 0 deletions src/platform/mbed/NetworkCommissioningDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class WiFiDriverImpl final : public WiFiDriver
uint8_t & outNetworkIndex) override;
void ScanNetworks(ByteSpan ssid, ScanCallback * callback) override;

CHIP_ERROR SetLastDisconnectReason(const ChipDeviceEvent * event);
int32_t GetLastDisconnectReason();

void OnNetworkStatusChange();

static WiFiDriverImpl & GetInstance()
{
static WiFiDriverImpl instance;
Expand Down Expand Up @@ -150,6 +155,9 @@ class WiFiDriverImpl final : public WiFiDriver
nsapi_security_t mSecurityType = NSAPI_SECURITY_NONE;
Inet::IPAddress mIp4Address = Inet::IPAddress::Any;
Inet::IPAddress mIp6Address = Inet::IPAddress::Any;

NetworkStatusChangeCallback * mStatusChangeCallback = nullptr;
int32_t mLastDisconnectedReason;
};
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
} // namespace NetworkCommissioning
Expand Down
53 changes: 43 additions & 10 deletions src/platform/mbed/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ CHIP_ERROR WiFiDriverImpl::Init(NetworkStatusChangeCallback * networkStatusChang
mWiFiInterface->set_blocking(false);
}

mScanCallback = nullptr;
mConnectCallback = nullptr;
mScanSpecific = false;
mScanCallback = nullptr;
mConnectCallback = nullptr;
mScanSpecific = false;
mStatusChangeCallback = networkStatusChangeCallback;

mIp4Address = IPAddress::Any;
mIp6Address = IPAddress::Any;
Expand Down Expand Up @@ -101,13 +102,14 @@ CHIP_ERROR WiFiDriverImpl::Shutdown()
{
networks->Release();
}
mScanCallback = nullptr;
mConnectCallback = nullptr;
mScanSpecific = false;
mWiFiInterface = nullptr;
mIp4Address = IPAddress::Any;
mIp6Address = IPAddress::Any;
mSecurityType = NSAPI_SECURITY_NONE;
mScanCallback = nullptr;
mConnectCallback = nullptr;
mStatusChangeCallback = nullptr;
mScanSpecific = false;
mWiFiInterface = nullptr;
mIp4Address = IPAddress::Any;
mIp6Address = IPAddress::Any;
mSecurityType = NSAPI_SECURITY_NONE;
memset(mScanSSID, 0, sizeof(mScanSSID));
mStagingNetwork.ssidLen = 0;
mStagingNetwork.credentialsLen = 0;
Expand Down Expand Up @@ -394,6 +396,18 @@ void WiFiDriverImpl::ScanNetworks(ByteSpan ssid, WiFiDriver::ScanCallback * call
}
}

CHIP_ERROR WiFiDriverImpl::SetLastDisconnectReason(const ChipDeviceEvent * event)
{
(void) event;
mLastDisconnectedReason = 0;
return CHIP_NO_ERROR;
}

int32_t WiFiDriverImpl::GetLastDisconnectReason()
{
return mLastDisconnectedReason;
}

size_t WiFiDriverImpl::WiFiNetworkIterator::Count()
{
return mDriver->mStagingNetwork.ssidLen == 0 ? 0 : 1;
Expand Down Expand Up @@ -602,6 +616,25 @@ WiFiAuthSecurityType WiFiDriverImpl::NsapiToNetworkSecurity(nsapi_security_t nsa
}
}

void WiFiDriverImpl::OnNetworkStatusChange()
{
// Network configuredNetwork;
bool staEnabled = ConnectivityMgrImpl().IsWiFiStationEnabled();
bool staConnected = ConnectivityMgrImpl().IsWiFiStationConnected();
VerifyOrReturn(staEnabled && mStatusChangeCallback != nullptr);

if (staConnected)
{
mStatusChangeCallback->OnNetworkingStatusChange(
Status::kSuccess, MakeOptional(ByteSpan((const uint8_t *) mStagingNetwork.ssid, mStagingNetwork.ssidLen)),
NullOptional);
return;
}
mStatusChangeCallback->OnNetworkingStatusChange(
Status::kUnknownError, MakeOptional(ByteSpan((const uint8_t *) mStagingNetwork.ssid, mStagingNetwork.ssidLen)),
MakeOptional(GetLastDisconnectReason()));
}

} // namespace NetworkCommissioning
} // namespace DeviceLayer
} // namespace chip

0 comments on commit 1227707

Please sign in to comment.