Skip to content

Commit

Permalink
Add persistent storage to session manager (#15062)
Browse files Browse the repository at this point in the history
* Add persistent storage to session manager
  • Loading branch information
jepenven-silabs authored and pull[bot] committed Jan 17, 2024
1 parent d6df1bb commit 8279028
Show file tree
Hide file tree
Showing 28 changed files with 81 additions and 38 deletions.
5 changes: 3 additions & 2 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ CHIP_ERROR CHIPCommand::Run()
ReturnLogErrorOnFailure(mFabricStorage.Initialize(&mDefaultStorage));

chip::Controller::FactoryInitParams factoryInitParams;
factoryInitParams.fabricStorage = &mFabricStorage;
factoryInitParams.listenPort = static_cast<uint16_t>(mDefaultStorage.GetListenPort() + CurrentCommissionerId());
factoryInitParams.fabricStorage = &mFabricStorage;
factoryInitParams.fabricIndependentStorage = &mDefaultStorage;
factoryInitParams.listenPort = static_cast<uint16_t>(mDefaultStorage.GetListenPort() + CurrentCommissionerId());
ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams));

ReturnLogErrorOnFailure(InitializeCommissioner(kIdentityNull, kIdentityNullFabricId));
Expand Down
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void StartPinging(streamer_t * stream, char * destination)
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gPingArguments.IsUsingTCP())
{
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager);
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
Expand All @@ -315,7 +315,7 @@ void StartPinging(streamer_t * stream, char * destination)
else
#endif
{
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
Expand Down
4 changes: 2 additions & 2 deletions examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ void ProcessCommand(streamer_t * stream, char * destination)
{
peerAddress = Transport::PeerAddress::TCP(gDestAddr, gSendArguments.GetPort());

err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager);
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}
else
#endif
{
peerAddress = Transport::PeerAddress::UDP(gDestAddr, gSendArguments.GetPort(), chip::Inet::InterfaceId::Null());

err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}

Expand Down
1 change: 1 addition & 0 deletions examples/shell/shell_common/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ chip::Messaging::ExchangeManager gExchangeManager;
chip::SessionManager gSessionManager;
chip::Inet::IPAddress gDestAddr;
chip::SessionHolder gSession;
chip::TestPersistentStorageDelegate gStorage;

chip::FabricIndex gFabricIndex = 0;

Expand Down
2 changes: 2 additions & 0 deletions examples/shell/shell_common/include/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <lib/core/CHIPCore.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#include <transport/SessionHolder.h>
Expand All @@ -36,6 +37,7 @@ extern chip::Messaging::ExchangeManager gExchangeManager;
extern chip::SessionManager gSessionManager;
extern chip::Inet::IPAddress gDestAddr;
extern chip::SessionHolder gSession;
extern chip::TestPersistentStorageDelegate gStorage;

extern chip::FabricIndex gFabricIndex;

Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint
#endif
SuccessOrExit(err);

err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager);
err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, &mDeviceStorage);
SuccessOrExit(err);

err = mExchangeMgr.Init(&mSessions);
Expand Down
4 changes: 3 additions & 1 deletion src/app/tests/TestOperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <app/OperationalDeviceProxy.h>
#include <inet/IPAddress.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <protocols/secure_channel/MessageCounterManager.h>
Expand Down Expand Up @@ -50,12 +51,13 @@ void TestOperationalDeviceProxy_EstablishSessionDirectly(nlTestSuite * inSuite,
FabricTable * fabrics = Platform::New<FabricTable>();
FabricInfo * fabric = fabrics->FindFabricWithIndex(1);
secure_channel::MessageCounterManager messageCounterManager;
chip::TestPersistentStorageDelegate deviceStorage;
SessionIDAllocator idAllocator;

systemLayer.Init();
udpEndPointManager.Init(systemLayer);
transportMgr.Init(UdpListenParameters(udpEndPointManager).SetAddressType(Inet::IPAddressType::kIPv4).SetListenPort(CHIP_PORT));
sessionManager.Init(&systemLayer, &transportMgr, &messageCounterManager);
sessionManager.Init(&systemLayer, &transportMgr, &messageCounterManager, &deviceStorage);
exchangeMgr.Init(&sessionManager);
messageCounterManager.Init(&exchangeMgr);

Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ int main(int argc, char * argv[])
.SetListenPort(IM_CLIENT_PORT));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTransportManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTransportManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int main(int argc, char * argv[])
.SetAddressType(chip::Inet::IPAddressType::kIPv6));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTransportManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTransportManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
Expand Down
1 change: 1 addition & 0 deletions src/app/tests/integration/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ chip::Messaging::ExchangeManager gExchangeManager;
chip::SessionManager gSessionManager;
chip::secure_channel::MessageCounterManager gMessageCounterManager;
chip::SessionHolder gSession;
chip::TestPersistentStorageDelegate gStorage;

void InitializeChip(void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/tests/integration/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include <app/util/basic-types.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#include <transport/SessionHolder.h>
Expand All @@ -36,6 +37,7 @@ extern chip::Messaging::ExchangeManager gExchangeManager;
extern chip::SessionManager gSessionManager;
extern chip::secure_channel::MessageCounterManager gMessageCounterManager;
extern chip::SessionHolder gSession;
extern chip::TestPersistentStorageDelegate gStorage;

constexpr chip::NodeId kTestNodeId = 0x1ULL;
constexpr chip::NodeId kTestNodeId1 = 0x2ULL;
Expand Down
15 changes: 10 additions & 5 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
return CHIP_NO_ERROR;
}

mListenPort = params.listenPort;
mFabricStorage = params.fabricStorage;
mListenPort = params.listenPort;
mFabricStorage = params.fabricStorage;
mFabricIndependentStorage = params.fabricIndependentStorage;

CHIP_ERROR err = InitSystemState(params);

Expand All @@ -71,6 +72,8 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState()
#endif
}

params.fabricIndependentStorage = mFabricIndependentStorage;

return InitSystemState(params);
}

Expand Down Expand Up @@ -137,8 +140,9 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
stateParams.messageCounterManager = chip::Platform::New<secure_channel::MessageCounterManager>();

ReturnErrorOnFailure(stateParams.fabricTable->Init(mFabricStorage));
ReturnErrorOnFailure(
stateParams.sessionMgr->Init(stateParams.systemLayer, stateParams.transportMgr, stateParams.messageCounterManager));

ReturnErrorOnFailure(stateParams.sessionMgr->Init(stateParams.systemLayer, stateParams.transportMgr,
stateParams.messageCounterManager, params.fabricIndependentStorage));
ReturnErrorOnFailure(stateParams.exchangeMgr->Init(stateParams.sessionMgr));
ReturnErrorOnFailure(stateParams.messageCounterManager->Init(stateParams.exchangeMgr));

Expand Down Expand Up @@ -222,7 +226,8 @@ void DeviceControllerFactory::Shutdown()
chip::Platform::Delete(mSystemState);
mSystemState = nullptr;
}
mFabricStorage = nullptr;
mFabricStorage = nullptr;
mFabricIndependentStorage = nullptr;
}

CHIP_ERROR DeviceControllerSystemState::Shutdown()
Expand Down
6 changes: 4 additions & 2 deletions src/controller/CHIPDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct FactoryInitParams
{
FabricStorage * fabricStorage = nullptr;
System::Layer * systemLayer = nullptr;
PersistentStorageDelegate * fabricIndependentStorage = nullptr;
Inet::EndPointManager<Inet::TCPEndPoint> * tcpEndPointManager = nullptr;
Inet::EndPointManager<Inet::UDPEndPoint> * udpEndPointManager = nullptr;
#if CONFIG_NETWORK_LAYER_BLE
Expand Down Expand Up @@ -130,8 +131,9 @@ class DeviceControllerFactory
CHIP_ERROR InitSystemState();

uint16_t mListenPort;
FabricStorage * mFabricStorage = nullptr;
DeviceControllerSystemState * mSystemState = nullptr;
FabricStorage * mFabricStorage = nullptr;
DeviceControllerSystemState * mSystemState = nullptr;
PersistentStorageDelegate * mFabricIndependentStorage = nullptr;
};

} // namespace Controller
Expand Down
2 changes: 2 additions & 0 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ AndroidDeviceControllerWrapper::AllocateNew(JavaVM * vm, jobject deviceControlle
initParams.tcpEndPointManager = tcpEndPointManager;
initParams.udpEndPointManager = udpEndPointManager;
initParams.fabricStorage = wrapper.get();

// move bleLayer into platform/android to share with app server
#if CONFIG_NETWORK_LAYER_BLE
initParams.bleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer();
Expand All @@ -127,6 +128,7 @@ AndroidDeviceControllerWrapper::AllocateNew(JavaVM * vm, jobject deviceControlle
setupParams.storageDelegate = wrapper.get();
setupParams.pairingDelegate = wrapper.get();
setupParams.operationalCredentialsDelegate = opCredsIssuer;
initParams.fabricIndependentStorage = setupParams.storageDelegate;

opCredsIssuer->Initialize(*wrapper.get(), wrapper.get()->mJavaObjectRef);

Expand Down
3 changes: 2 additions & 1 deletion src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ ChipError::StorageType pychip_DeviceController_StackInit()
VerifyOrReturnError(err == CHIP_NO_ERROR, err.AsInteger());

FactoryInitParams factoryParams;
factoryParams.fabricStorage = &sFabricStorage;
factoryParams.fabricStorage = &sFabricStorage;
factoryParams.fabricIndependentStorage = sStorageAdapter;

ReturnErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryParams).AsInteger());

Expand Down
3 changes: 2 additions & 1 deletion src/controller/python/chip/internal/CommissionerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ extern "C" chip::Controller::DeviceCommissioner * pychip_internal_Commissioner_N
err = gFabricStorage.Initialize(&gServerStorage);
SuccessOrExit(err);

factoryParams.fabricStorage = &gFabricStorage;
factoryParams.fabricStorage = &gFabricStorage;
factoryParams.fabricIndependentStorage = &gServerStorage;

commissionerParams.pairingDelegate = &gPairingDelegate;
commissionerParams.storageDelegate = &gServerStorage;
Expand Down
5 changes: 4 additions & 1 deletion src/controller/tests/TestDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <inet/IPAddress.h>
#include <inetInetLayer.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <platform/CHIPDeviceLayer.h>
Expand Down Expand Up @@ -51,6 +52,8 @@ void TestDevice_EstablishSessionDirectly(nlTestSuite * inSuite, void * inContext
ExchangeManager exchangeMgr;
Inet::UDPEndPointManagerImpl udpEndPointManager;
System::LayerImpl systemLayer;
chip::TestPersistentStorageDelegate deviceStorage;

#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer blelayer;
#endif // CONFIG_NETWORK_LAYER_BLE
Expand All @@ -72,7 +75,7 @@ void TestDevice_EstablishSessionDirectly(nlTestSuite * inSuite, void * inContext
BleListenParameters(&blelayer)
#endif
);
sessionManager.Init(&systemLayer, &transportMgr, &messageCounterManager);
sessionManager.Init(&systemLayer, &transportMgr, &messageCounterManager, &deviceStorage;);
exchangeMgr.Init(&sessionManager);
messageCounterManager.Init(&exchangeMgr);

Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ - (BOOL)startup:(_Nullable id<CHIPPersistentStorageDelegate>)storageDelegate
chip::Credentials::SetDeviceAttestationVerifier(chip::Credentials::GetDefaultDACVerifier(testingRootStore));

params.fabricStorage = _fabricStorage;
params.fabricIndependentStorage = _persistentStorageDelegateBridge;
commissionerParams.storageDelegate = _persistentStorageDelegateBridge;
commissionerParams.deviceAddressUpdateDelegate = _pairingDelegateBridge;
commissionerParams.pairingDelegate = _pairingDelegateBridge;
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/tests/MessagingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo
mTransport = transport;

ReturnErrorOnFailure(PlatformMemoryUser::Init());
ReturnErrorOnFailure(mSessionManager.Init(&GetSystemLayer(), transport, &mMessageCounterManager));
ReturnErrorOnFailure(mSessionManager.Init(&GetSystemLayer(), transport, &mMessageCounterManager, &mStorage));

ReturnErrorOnFailure(mExchangeManager.Init(&mSessionManager));
ReturnErrorOnFailure(mMessageCounterManager.Init(&mExchangeManager));
Expand Down
4 changes: 3 additions & 1 deletion src/messaging/tests/MessagingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#pragma once

#include <lib/support/TestPersistentStorageDelegate.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/secure_channel/MessageCounterManager.h>
Expand Down Expand Up @@ -145,7 +146,8 @@ class MessagingContext : public PlatformMemoryUser
Messaging::ExchangeManager mExchangeManager;
secure_channel::MessageCounterManager mMessageCounterManager;
IOContext * mIOContext;
TransportMgrBase * mTransport; // Only needed for InitFromExisting.
TransportMgrBase * mTransport; // Only needed for InitFromExisting.
chip::TestPersistentStorageDelegate mStorage; // for SessionManagerInit

NodeId mBobNodeId = 123654;
NodeId mAliceNodeId = 111222333;
Expand Down
1 change: 1 addition & 0 deletions src/messaging/tests/echo/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
chip::SessionManager gSessionManager;
chip::Messaging::ExchangeManager gExchangeManager;
chip::secure_channel::MessageCounterManager gMessageCounterManager;
chip::TestPersistentStorageDelegate gStorage;

void InitializeChip(void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/messaging/tests/echo/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#pragma once

#include <lib/support/TestPersistentStorageDelegate.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#include <transport/SessionManager.h>
Expand All @@ -35,6 +36,7 @@ constexpr size_t kNetworkSleepTimeMsecs = (100 * 1000);
extern chip::SessionManager gSessionManager;
extern chip::Messaging::ExchangeManager gExchangeManager;
extern chip::secure_channel::MessageCounterManager gMessageCounterManager;
extern chip::TestPersistentStorageDelegate gStorage;

void InitializeChip(void);
void ShutdownChip(void);
4 changes: 2 additions & 2 deletions src/messaging/tests/echo/echo_requester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ int main(int argc, char * argv[])
.SetListenPort(ECHO_CLIENT_PORT));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}
else
Expand All @@ -248,7 +248,7 @@ int main(int argc, char * argv[])
.SetListenPort(ECHO_CLIENT_PORT));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}

Expand Down
4 changes: 2 additions & 2 deletions src/messaging/tests/echo/echo_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main(int argc, char * argv[])
);
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}
else
Expand All @@ -102,7 +102,7 @@ int main(int argc, char * argv[])
.SetAddressType(chip::Inet::IPAddressType::kIPv6));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager, &gStorage);
SuccessOrExit(err);
}

Expand Down
10 changes: 5 additions & 5 deletions src/protocols/secure_channel/tests/TestCASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,18 @@ void CASE_SecurePairingHandshakeTest(nlTestSuite * inSuite, void * inContext)
CASE_SecurePairingHandshakeTestCommon(inSuite, inContext, pairingCommissioner, delegateCommissioner);
}

class TestPersistentStorageDelegate : public PersistentStorageDelegate, public FabricStorage
class TestCASESessionPersistentStorageDelegate : public PersistentStorageDelegate, public FabricStorage
{
public:
TestPersistentStorageDelegate()
TestCASESessionPersistentStorageDelegate()
{
memset(keys, 0, sizeof(keys));
memset(keysize, 0, sizeof(keysize));
memset(values, 0, sizeof(values));
memset(valuesize, 0, sizeof(valuesize));
}

~TestPersistentStorageDelegate() { Cleanup(); }
~TestCASESessionPersistentStorageDelegate() { Cleanup(); }

void Cleanup()
{
Expand Down Expand Up @@ -347,8 +347,8 @@ class TestPersistentStorageDelegate : public PersistentStorageDelegate, public F
uint16_t valuesize[16];
};

TestPersistentStorageDelegate gCommissionerStorageDelegate;
TestPersistentStorageDelegate gDeviceStorageDelegate;
TestCASESessionPersistentStorageDelegate gCommissionerStorageDelegate;
TestCASESessionPersistentStorageDelegate gDeviceStorageDelegate;

TestCASEServerIPK gPairingServer;

Expand Down
Loading

0 comments on commit 8279028

Please sign in to comment.