Skip to content

Commit

Permalink
[Darwin] Get the StartUp event to work
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Feb 13, 2022
1 parent 5cb8800 commit 4897445
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()

// TODO Initialize the Software Update Manager object.

// TODO: Attempt to diagnose Darwin CI, REMOVE ONCE FIXED
#if !CHIP_DEVICE_LAYER_TARGET_DARWIN
_ScheduleWork(HandleDeviceRebooted, 0);
#endif

exit:
return err;
Expand Down
103 changes: 97 additions & 6 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <lib/core/CHIPVendorIdentifiers.hpp>
#include <platform/ConfigurationManager.h>
#include <platform/Darwin/DiagnosticDataProviderImpl.h>
#include <platform/Darwin/PosixConfig.h>
#include <platform/internal/GenericConfigurationManagerImpl.cpp>

Expand Down Expand Up @@ -137,14 +138,44 @@ ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()

CHIP_ERROR ConfigurationManagerImpl::Init()
{
CHIP_ERROR err;

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

exit:
return err;
uint32_t rebootCount;
if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_RebootCount))
{
// The first boot after factory reset of the Node.
ReturnErrorOnFailure(StoreRebootCount(1));
}
else
{
ReturnErrorOnFailure(GetRebootCount(rebootCount));
ReturnErrorOnFailure(StoreRebootCount(rebootCount + 1));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_TotalOperationalHours))
{
ReturnErrorOnFailure(StoreTotalOperationalHours(0));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_BootReason))
{
ReturnErrorOnFailure(StoreBootReason(DiagnosticDataProvider::BootReasonType::Unspecified));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kConfigKey_RegulatoryLocation))
{
uint32_t location = to_underlying(chip::app::Clusters::GeneralCommissioning::RegulatoryLocationType::kIndoor);
ReturnErrorOnFailure(WriteConfigValue(PosixConfig::kConfigKey_RegulatoryLocation, location));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kConfigKey_LocationCapability))
{
uint32_t location = to_underlying(chip::app::Clusters::GeneralCommissioning::RegulatoryLocationType::kIndoor);
ReturnErrorOnFailure(WriteConfigValue(PosixConfig::kConfigKey_LocationCapability, location));
}

return CHIP_NO_ERROR;
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
Expand Down Expand Up @@ -176,6 +207,66 @@ void ConfigurationManagerImpl::InitiateFactoryReset()
ChipLogError(DeviceLayer, "InitiateFactoryReset not implemented");
}

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

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

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

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

CHIP_ERROR ConfigurationManagerImpl::GetBootReason(uint32_t & bootReason)
{
return ReadConfigValue(PosixConfig::kCounterKey_BootReason, bootReason);
}

CHIP_ERROR ConfigurationManagerImpl::StoreBootReason(uint32_t bootReason)
{
return WriteConfigValue(PosixConfig::kCounterKey_BootReason, bootReason);
}

CHIP_ERROR ConfigurationManagerImpl::GetRegulatoryLocation(uint8_t & location)
{
uint32_t value = 0;

CHIP_ERROR err = ReadConfigValue(PosixConfig::kConfigKey_RegulatoryLocation, value);

if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(value <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
location = static_cast<uint8_t>(value);
}

return err;
}

CHIP_ERROR ConfigurationManagerImpl::GetLocationCapability(uint8_t & location)
{
uint32_t value = 0;

CHIP_ERROR err = ReadConfigValue(PosixConfig::kConfigKey_LocationCapability, value);

if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(value <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
location = static_cast<uint8_t>(value);
}

return err;
}

CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
PosixConfig::Key configKey{ PosixConfig::kConfigNamespace_ChipCounters, key };
Expand Down
8 changes: 8 additions & 0 deletions src/platform/Darwin/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
bool CanFactoryReset(void) override;
void InitiateFactoryReset(void) override;
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;
CHIP_ERROR GetBootReason(uint32_t & bootReason) override;
CHIP_ERROR StoreBootReason(uint32_t bootReason) override;
CHIP_ERROR GetRegulatoryLocation(uint8_t & location) override;
CHIP_ERROR GetLocationCapability(uint8_t & location) override;
CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) override;
CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override;

Expand Down
32 changes: 32 additions & 0 deletions src/platform/Darwin/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,37 @@ DiagnosticDataProviderImpl & DiagnosticDataProviderImpl::GetDefaultInstance()
return sInstance;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime)
{
System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp();
System::Clock::Timestamp startTime = PlatformMgrImpl().GetStartTime();

if (currentTime >= startTime)
{
upTime = std::chrono::duration_cast<System::Clock::Seconds64>(currentTime - startTime).count();
return CHIP_NO_ERROR;
}

return CHIP_ERROR_INVALID_TIME;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
uint64_t upTime = 0;

if (GetUpTime(upTime) == CHIP_NO_ERROR)
{
uint32_t totalHours = 0;
if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR)
{
VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
totalOperationalHours = totalHours + static_cast<uint32_t>(upTime / 3600);
return CHIP_NO_ERROR;
}
}

return CHIP_ERROR_INVALID_TIME;
}

} // namespace DeviceLayer
} // namespace chip
4 changes: 4 additions & 0 deletions src/platform/Darwin/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
{
public:
static DiagnosticDataProviderImpl & GetDefaultInstance();

// ===== Methods that implement the PlatformManager abstract interface.
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
};

} // namespace DeviceLayer
Expand Down
10 changes: 10 additions & 0 deletions src/platform/Darwin/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()

mRunLoopSem = dispatch_semaphore_create(0);

// Ensure there is a dispatch queue available
GetWorkQueue();

// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
err = Internal::GenericPlatformManagerImpl<PlatformManagerImpl>::_InitChipStack();
SuccessOrExit(err);

mStartTime = System::SystemClock().GetMonotonicTimestamp();

static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer()).SetDispatchQueue(GetWorkQueue());

exit:
Expand Down Expand Up @@ -119,6 +124,11 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown()

CHIP_ERROR PlatformManagerImpl::_PostEvent(const ChipDeviceEvent * event)
{
if (mWorkQueue == nullptr)
{
return CHIP_ERROR_INCORRECT_STATE;
}

const ChipDeviceEvent eventCopy = *event;
dispatch_async(mWorkQueue, ^{
Impl()->DispatchEvent(&eventCopy);
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Darwin/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
return mWorkQueue;
}

System::Clock::Timestamp GetStartTime() { return mStartTime; }

private:
// ===== Methods that implement the PlatformManager abstract interface.
CHIP_ERROR _InitChipStack();
Expand Down Expand Up @@ -86,6 +88,8 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener

static PlatformManagerImpl sInstance;

System::Clock::Timestamp mStartTime = System::Clock::kZero;

dispatch_queue_t mWorkQueue = nullptr;
// Semaphore used to implement blocking behavior in _RunEventLoop.
dispatch_semaphore_t mRunLoopSem;
Expand Down

0 comments on commit 4897445

Please sign in to comment.