Skip to content

Commit

Permalink
Merge b314b5c into 9c389ba
Browse files Browse the repository at this point in the history
  • Loading branch information
nk-shelly authored Nov 5, 2024
2 parents 9c389ba + b314b5c commit 9368963
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 86 deletions.
18 changes: 10 additions & 8 deletions src/app/MessageDef/MessageDefHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <app/SpecificationDefinedRevisions.h>
#include <app/util/basic-types.h>
#include <inttypes.h>
#include <lib/core/Global.h>
#include <lib/support/logging/CHIPLogging.h>
#include <stdarg.h>
#include <stdio.h>
Expand All @@ -37,26 +38,27 @@ namespace app {
// this is used to run in signle thread for IM message debug purpose
namespace {
uint32_t gPrettyPrintingDepthLevel = 0;
char gLineBuffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
Global<char[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]> gLineBuffer;
size_t gCurLineBufferSize = 0;
} // namespace

void PrettyPrintIMBlankLine()
{
char * lineBuffer = gLineBuffer.get();
if (gCurLineBufferSize)
{
// Don't need to explicitly NULL-terminate the string because
// snprintf takes care of that.
ChipLogDetail(DataManagement, "%s", gLineBuffer);
ChipLogDetail(DataManagement, "%s", lineBuffer);
gCurLineBufferSize = 0;
}

for (uint32_t i = 0; i < gPrettyPrintingDepthLevel; i++)
{
if (sizeof(gLineBuffer) > gCurLineBufferSize)
if (CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE > gCurLineBufferSize)
{
size_t sizeLeft = sizeof(gLineBuffer) - gCurLineBufferSize;
size_t ret = (size_t) (snprintf(gLineBuffer + gCurLineBufferSize, sizeLeft, "\t"));
size_t sizeLeft = CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE - gCurLineBufferSize;
size_t ret = (size_t) (snprintf(lineBuffer + gCurLineBufferSize, sizeLeft, "\t"));
if (ret > 0)
{
gCurLineBufferSize += std::min(ret, sizeLeft);
Expand All @@ -75,10 +77,10 @@ void PrettyPrintIM(bool aIsNewLine, const char * aFmt, ...)
PrettyPrintIMBlankLine();
}

if (sizeof(gLineBuffer) > gCurLineBufferSize)
if (CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE > gCurLineBufferSize)
{
size_t sizeLeft = sizeof(gLineBuffer) - gCurLineBufferSize;
size_t ret = (size_t) (vsnprintf(gLineBuffer + gCurLineBufferSize, sizeLeft, aFmt, args));
size_t sizeLeft = CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE - gCurLineBufferSize;
size_t ret = (size_t) (vsnprintf(gLineBuffer.get() + gCurLineBufferSize, sizeLeft, aFmt, args));
if (ret > 0)
{
gCurLineBufferSize += std::min(ret, sizeLeft);
Expand Down
7 changes: 7 additions & 0 deletions src/app/reporting/ReportScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,17 @@ class ReportScheduler : public ReadHandler::Observer, public ICDStateObserver
BitFlags<ReadHandlerNodeFlags> mFlags;
};

ReportScheduler() : mTimerDelegate(nullptr) {}
ReportScheduler(TimerDelegate * aTimerDelegate) : mTimerDelegate(aTimerDelegate) {}

virtual ~ReportScheduler() = default;

void Init(TimerDelegate * aTimerDelegate)
{
mTimerDelegate = aTimerDelegate;
VerifyOrDie(nullptr != mTimerDelegate);
}

virtual void ReportTimerCallback() = 0;

/// @brief Check whether a ReadHandler is reportable right now, taking into account its minimum and maximum intervals.
Expand Down
5 changes: 0 additions & 5 deletions src/app/reporting/ReportSchedulerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ void ReportSchedulerImpl::ReportTimerCallback()
InteractionModelEngine::GetInstance()->GetReportingEngine().ScheduleRun();
}

ReportSchedulerImpl::ReportSchedulerImpl(TimerDelegate * aTimerDelegate) : ReportScheduler(aTimerDelegate)
{
VerifyOrDie(nullptr != mTimerDelegate);
}

/// @brief Method that triggers a report emission on each ReadHandler that is not blocked on its min interval.
/// Each read handler that is not blocked is immediately marked dirty so that it will report as soon as possible.
void ReportSchedulerImpl::OnEnterActiveMode()
Expand Down
3 changes: 2 additions & 1 deletion src/app/reporting/ReportSchedulerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class ReportSchedulerImpl : public ReportScheduler
public:
using Timeout = System::Clock::Timeout;

ReportSchedulerImpl(TimerDelegate * aTimerDelegate);
ReportSchedulerImpl() = default;
ReportSchedulerImpl(TimerDelegate * aTimerDelegate) : ReportScheduler(aTimerDelegate) {}
~ReportSchedulerImpl() override { UnregisterAllHandlers(); }

// ICDStateObserver
Expand Down
1 change: 1 addition & 0 deletions src/app/reporting/SynchronizedReportSchedulerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SynchronizedReportSchedulerImpl : public ReportSchedulerImpl, public Timer
public:
void OnReadHandlerDestroyed(ReadHandler * aReadHandler) override;

SynchronizedReportSchedulerImpl() = default;
SynchronizedReportSchedulerImpl(TimerDelegate * aTimerDelegate) : ReportSchedulerImpl(aTimerDelegate) {}
~SynchronizedReportSchedulerImpl() override { UnregisterAllHandlers(); }

Expand Down
50 changes: 25 additions & 25 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <inet/IPAddress.h>
#include <inet/InetError.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/core/Global.h>
#include <lib/dnssd/Advertiser.h>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
Expand Down Expand Up @@ -104,15 +105,15 @@ class DeviceTypeResolver : public chip::Access::AccessControl::DeviceTypeResolve

namespace chip {

Server Server::sServer;
Global<Server> Server::sServer;

#if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
#define CHIP_NUM_EVENT_LOGGING_BUFFERS 3
static uint8_t sInfoEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE];
static uint8_t sDebugEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE];
static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE];
static ::chip::PersistedCounter<chip::EventNumber> sGlobalEventIdCounter;
static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS];
static Global<uint8_t[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE]> sInfoEventBuffer;
static Global<uint8_t[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE]> sDebugEventBuffer;
static Global<uint8_t[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE]> sCritEventBuffer;
static Global<::chip::PersistedCounter<chip::EventNumber>> sGlobalEventIdCounter;
static Global<::chip::app::CircularEventBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS]> sLoggingBuffer;
#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT

CHIP_ERROR Server::Init(const ServerInitParams & initParams)
Expand Down Expand Up @@ -278,19 +279,19 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)

#if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
// Initialize event logging subsystem
err = sGlobalEventIdCounter.Init(mDeviceStorage, DefaultStorageKeyAllocator::IMEventNumber(),
CHIP_DEVICE_CONFIG_EVENT_ID_COUNTER_EPOCH);
err = sGlobalEventIdCounter->Init(mDeviceStorage, DefaultStorageKeyAllocator::IMEventNumber(),
CHIP_DEVICE_CONFIG_EVENT_ID_COUNTER_EPOCH);
SuccessOrExit(err);

{
::chip::app::LogStorageResources logStorageResources[] = {
{ &sDebugEventBuffer[0], sizeof(sDebugEventBuffer), ::chip::app::PriorityLevel::Debug },
{ &sInfoEventBuffer[0], sizeof(sInfoEventBuffer), ::chip::app::PriorityLevel::Info },
{ &sCritEventBuffer[0], sizeof(sCritEventBuffer), ::chip::app::PriorityLevel::Critical }
{ sDebugEventBuffer.get(), CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE, ::chip::app::PriorityLevel::Debug },
{ sInfoEventBuffer.get(), CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE, ::chip::app::PriorityLevel::Info },
{ sCritEventBuffer.get(), CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE, ::chip::app::PriorityLevel::Critical }
};

chip::app::EventManagement::GetInstance().Init(&mExchangeMgr, CHIP_NUM_EVENT_LOGGING_BUFFERS, &sLoggingBuffer[0],
&logStorageResources[0], &sGlobalEventIdCounter,
chip::app::EventManagement::GetInstance().Init(&mExchangeMgr, CHIP_NUM_EVENT_LOGGING_BUFFERS, sLoggingBuffer.get(),
&logStorageResources[0], &sGlobalEventIdCounter.get(),
std::chrono::duration_cast<System::Clock::Milliseconds64>(mInitTimestamp));
}
#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
Expand Down Expand Up @@ -787,23 +788,22 @@ void Server::ResumeSubscriptions()

Credentials::IgnoreCertificateValidityPeriodPolicy Server::sDefaultCertValidityPolicy;

KvsPersistentStorageDelegate CommonCaseDeviceServerInitParams::sKvsPersistenStorageDelegate;
PersistentStorageOperationalKeystore CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore;
Credentials::PersistentStorageOpCertStore CommonCaseDeviceServerInitParams::sPersistentStorageOpCertStore;
Credentials::GroupDataProviderImpl CommonCaseDeviceServerInitParams::sGroupDataProvider;
app::DefaultTimerDelegate CommonCaseDeviceServerInitParams::sTimerDelegate;
app::reporting::ReportSchedulerImpl
CommonCaseDeviceServerInitParams::sReportScheduler(&CommonCaseDeviceServerInitParams::sTimerDelegate);
Global<KvsPersistentStorageDelegate> CommonCaseDeviceServerInitParams::sKvsPersistenStorageDelegate;
Global<PersistentStorageOperationalKeystore> CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore;
Global<Credentials::PersistentStorageOpCertStore> CommonCaseDeviceServerInitParams::sPersistentStorageOpCertStore;
Global<Credentials::GroupDataProviderImpl> CommonCaseDeviceServerInitParams::sGroupDataProvider;
Global<app::DefaultTimerDelegate> CommonCaseDeviceServerInitParams::sTimerDelegate;
Global<app::reporting::ReportSchedulerImpl> CommonCaseDeviceServerInitParams::sReportScheduler;
#if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION
SimpleSessionResumptionStorage CommonCaseDeviceServerInitParams::sSessionResumptionStorage;
Global<SimpleSessionResumptionStorage> CommonCaseDeviceServerInitParams::sSessionResumptionStorage;
#endif
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
app::SimpleSubscriptionResumptionStorage CommonCaseDeviceServerInitParams::sSubscriptionResumptionStorage;
Global<app::SimpleSubscriptionResumptionStorage> CommonCaseDeviceServerInitParams::sSubscriptionResumptionStorage;
#endif
app::DefaultAclStorage CommonCaseDeviceServerInitParams::sAclStorage;
Crypto::DefaultSessionKeystore CommonCaseDeviceServerInitParams::sSessionKeystore;
Global<app::DefaultAclStorage> CommonCaseDeviceServerInitParams::sAclStorage;
Global<Crypto::DefaultSessionKeystore> CommonCaseDeviceServerInitParams::sSessionKeystore;
#if CHIP_CONFIG_ENABLE_ICD_CIP
app::DefaultICDCheckInBackOffStrategy CommonCaseDeviceServerInitParams::sDefaultICDCheckInBackOffStrategy;
Global<app::DefaultICDCheckInBackOffStrategy> CommonCaseDeviceServerInitParams::sDefaultICDCheckInBackOffStrategy;
#endif

} // namespace chip
65 changes: 34 additions & 31 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <crypto/PersistentStorageOperationalKeystore.h>
#include <inet/InetConfig.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/Global.h>
#include <lib/support/SafeInt.h>
#include <messaging/ExchangeMgr.h>
#include <platform/DeviceInstanceInfoProvider.h>
Expand Down Expand Up @@ -241,17 +242,17 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
{
chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager =
DeviceLayer::PersistedStorage::KeyValueStoreMgr();
ReturnErrorOnFailure(sKvsPersistenStorageDelegate.Init(&kvsManager));
this->persistentStorageDelegate = &sKvsPersistenStorageDelegate;
ReturnErrorOnFailure(sKvsPersistenStorageDelegate->Init(&kvsManager));
this->persistentStorageDelegate = &sKvsPersistenStorageDelegate.get();
}

// PersistentStorageDelegate "software-based" operational key access injection
if (this->operationalKeystore == nullptr)
{
// WARNING: PersistentStorageOperationalKeystore::Finish() is never called. It's fine for
// for examples and for now.
ReturnErrorOnFailure(sPersistentStorageOperationalKeystore.Init(this->persistentStorageDelegate));
this->operationalKeystore = &sPersistentStorageOperationalKeystore;
ReturnErrorOnFailure(sPersistentStorageOperationalKeystore->Init(this->persistentStorageDelegate));
this->operationalKeystore = &sPersistentStorageOperationalKeystore.get();
}

// OpCertStore can be injected but default to persistent storage default
Expand All @@ -260,30 +261,31 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
{
// WARNING: PersistentStorageOpCertStore::Finish() is never called. It's fine for
// for examples and for now, since all storage is immediate for that impl.
ReturnErrorOnFailure(sPersistentStorageOpCertStore.Init(this->persistentStorageDelegate));
this->opCertStore = &sPersistentStorageOpCertStore;
ReturnErrorOnFailure(sPersistentStorageOpCertStore->Init(this->persistentStorageDelegate));
this->opCertStore = &sPersistentStorageOpCertStore.get();
}

// Injection of report scheduler WILL lead to two schedulers being allocated. As recommended above, this should only be used
// for IN-TREE examples. If a default scheduler is desired, the basic ServerInitParams should be used by the application and
// CommonCaseDeviceServerInitParams should not be allocated.
if (this->reportScheduler == nullptr)
{
reportScheduler = &sReportScheduler;
sReportScheduler->Init(&sTimerDelegate.get());
reportScheduler = &sReportScheduler.get();
}

// Session Keystore injection
this->sessionKeystore = &sSessionKeystore;
this->sessionKeystore = &sSessionKeystore.get();

// Group Data provider injection
sGroupDataProvider.SetStorageDelegate(this->persistentStorageDelegate);
sGroupDataProvider.SetSessionKeystore(this->sessionKeystore);
ReturnErrorOnFailure(sGroupDataProvider.Init());
this->groupDataProvider = &sGroupDataProvider;
sGroupDataProvider->SetStorageDelegate(this->persistentStorageDelegate);
sGroupDataProvider->SetSessionKeystore(this->sessionKeystore);
ReturnErrorOnFailure(sGroupDataProvider->Init());
this->groupDataProvider = &sGroupDataProvider.get();

#if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION
ReturnErrorOnFailure(sSessionResumptionStorage.Init(this->persistentStorageDelegate));
this->sessionResumptionStorage = &sSessionResumptionStorage;
ReturnErrorOnFailure(sSessionResumptionStorage->Init(this->persistentStorageDelegate));
this->sessionResumptionStorage = &sSessionResumptionStorage.get();
#else
this->sessionResumptionStorage = nullptr;
#endif
Expand All @@ -292,44 +294,44 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams
this->accessDelegate = Access::Examples::GetAccessControlDelegate();

// Inject ACL storage. (Don't initialize it.)
this->aclStorage = &sAclStorage;
this->aclStorage = &sAclStorage.get();

#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
ChipLogProgress(AppServer, "Initializing subscription resumption storage...");
ReturnErrorOnFailure(sSubscriptionResumptionStorage.Init(this->persistentStorageDelegate));
this->subscriptionResumptionStorage = &sSubscriptionResumptionStorage;
ReturnErrorOnFailure(sSubscriptionResumptionStorage->Init(this->persistentStorageDelegate));
this->subscriptionResumptionStorage = &sSubscriptionResumptionStorage.get();
#else
ChipLogProgress(AppServer, "Subscription persistence not supported");
#endif

#if CHIP_CONFIG_ENABLE_ICD_CIP
if (this->icdCheckInBackOffStrategy == nullptr)
{
this->icdCheckInBackOffStrategy = &sDefaultICDCheckInBackOffStrategy;
this->icdCheckInBackOffStrategy = &sDefaultICDCheckInBackOffStrategy.get();
}
#endif

return CHIP_NO_ERROR;
}

private:
static KvsPersistentStorageDelegate sKvsPersistenStorageDelegate;
static PersistentStorageOperationalKeystore sPersistentStorageOperationalKeystore;
static Credentials::PersistentStorageOpCertStore sPersistentStorageOpCertStore;
static Credentials::GroupDataProviderImpl sGroupDataProvider;
static chip::app::DefaultTimerDelegate sTimerDelegate;
static app::reporting::ReportSchedulerImpl sReportScheduler;
static Global<KvsPersistentStorageDelegate> sKvsPersistenStorageDelegate;
static Global<PersistentStorageOperationalKeystore> sPersistentStorageOperationalKeystore;
static Global<Credentials::PersistentStorageOpCertStore> sPersistentStorageOpCertStore;
static Global<Credentials::GroupDataProviderImpl> sGroupDataProvider;
static Global<chip::app::DefaultTimerDelegate> sTimerDelegate;
static Global<app::reporting::ReportSchedulerImpl> sReportScheduler;

#if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION
static SimpleSessionResumptionStorage sSessionResumptionStorage;
static Global<SimpleSessionResumptionStorage> sSessionResumptionStorage;
#endif
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
static app::SimpleSubscriptionResumptionStorage sSubscriptionResumptionStorage;
static Global<app::SimpleSubscriptionResumptionStorage> sSubscriptionResumptionStorage;
#endif
static app::DefaultAclStorage sAclStorage;
static Crypto::DefaultSessionKeystore sSessionKeystore;
static Global<app::DefaultAclStorage> sAclStorage;
static Global<Crypto::DefaultSessionKeystore> sSessionKeystore;
#if CHIP_CONFIG_ENABLE_ICD_CIP
static app::DefaultICDCheckInBackOffStrategy sDefaultICDCheckInBackOffStrategy;
static Global<app::DefaultICDCheckInBackOffStrategy> sDefaultICDCheckInBackOffStrategy;
#endif
};

Expand Down Expand Up @@ -442,12 +444,13 @@ class Server
return System::SystemClock().GetMonotonicMicroseconds64() - mInitTimestamp;
}

static Server & GetInstance() { return sServer; }
static Server & GetInstance() { return sServer.get(); }

private:
Server() {}

static Server sServer;
friend class Global<Server>;
static Global<Server> sServer;

void InitFailSafe();
void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent & event);
Expand Down
2 changes: 1 addition & 1 deletion src/app/util/binding-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace chip {

BindingTable BindingTable::sInstance;
Global<BindingTable> BindingTable::sInstance;

BindingTable::BindingTable()
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/util/binding-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app/util/af-types.h>
#include <app/util/config.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/core/Global.h>
#include <lib/core/TLV.h>
#include <lib/support/DefaultStorageKeyAllocator.h>

Expand Down Expand Up @@ -82,10 +83,10 @@ class BindingTable

CHIP_ERROR LoadFromStorage();

static BindingTable & GetInstance() { return sInstance; }
static BindingTable & GetInstance() { return sInstance.get(); }

private:
static BindingTable sInstance;
static Global<BindingTable> sInstance;

static constexpr uint32_t kStorageVersion = 1;
static constexpr uint8_t kEntryStorageSize = TLV::EstimateStructOverhead(
Expand Down
Loading

0 comments on commit 9368963

Please sign in to comment.