Skip to content

Commit

Permalink
Add persistent storage to session manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jepenven-silabs committed Feb 11, 2022
1 parent 4126b1c commit 4f57404
Show file tree
Hide file tree
Showing 24 changed files with 74 additions and 33 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.storageDelegate = &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 @@ -698,7 +698,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 @@ -168,7 +168,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
11 changes: 9 additions & 2 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)

mListenPort = params.listenPort;
mFabricStorage = params.fabricStorage;
mStorage = params.storageDelegate;

CHIP_ERROR err = InitSystemState(params);

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

params.storageDelegate = mStorage;

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.storageDelegate));
ReturnErrorOnFailure(stateParams.exchangeMgr->Init(stateParams.sessionMgr));
ReturnErrorOnFailure(stateParams.messageCounterManager->Init(stateParams.exchangeMgr));

Expand Down Expand Up @@ -175,6 +179,7 @@ void DeviceControllerFactory::PopulateInitParams(ControllerInitParams & controll
CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceController & controller)
{
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
mStorage = params.storageDelegate;
ReturnErrorOnFailure(InitSystemState());

ControllerInitParams controllerParams;
Expand All @@ -187,6 +192,7 @@ CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceCo
CHIP_ERROR DeviceControllerFactory::SetupCommissioner(SetupParams params, DeviceCommissioner & commissioner)
{
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
mStorage = params.storageDelegate;
ReturnErrorOnFailure(InitSystemState());

CommissionerInitParams commissionerParams;
Expand Down Expand Up @@ -223,6 +229,7 @@ void DeviceControllerFactory::Shutdown()
mSystemState = nullptr;
}
mFabricStorage = nullptr;
mStorage = nullptr;
}

CHIP_ERROR DeviceControllerSystemState::Shutdown()
Expand Down
2 changes: 2 additions & 0 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 * storageDelegate = nullptr;
Inet::EndPointManager<Inet::TCPEndPoint> * tcpEndPointManager = nullptr;
Inet::EndPointManager<Inet::UDPEndPoint> * udpEndPointManager = nullptr;
#if CONFIG_NETWORK_LAYER_BLE
Expand Down Expand Up @@ -132,6 +133,7 @@ class DeviceControllerFactory
uint16_t mListenPort;
FabricStorage * mFabricStorage = nullptr;
DeviceControllerSystemState * mSystemState = nullptr;
PersistentStorageDelegate * mStorage = nullptr;
};

} // namespace Controller
Expand Down
3 changes: 3 additions & 0 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew(
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 @@ -237,6 +238,8 @@ AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew(
setupParams.pairingDelegate = wrapper.get();
setupParams.operationalCredentialsDelegate = wrapper.get();

initParams.storageDelegate = setupParams.storageDelegate;

wrapper->InitializeOperationalCredentialsIssuer();

Platform::ScopedMemoryBuffer<uint8_t> noc;
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.storageDelegate = 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.storageDelegate = &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.storageDelegate = _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
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
6 changes: 5 additions & 1 deletion src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ SessionManager::SessionManager() : mState(State::kNotReady) {}
SessionManager::~SessionManager() {}

CHIP_ERROR SessionManager::Init(System::Layer * systemLayer, TransportMgrBase * transportMgr,
Transport::MessageCounterManagerInterface * messageCounterManager)
Transport::MessageCounterManagerInterface * messageCounterManager,
chip::PersistentStorageDelegate * storageDelegate)
{
VerifyOrReturnError(mState == State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(transportMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(storageDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

mState = State::kInitialized;
mSystemLayer = systemLayer;
mTransportMgr = transportMgr;
mMessageCounterManager = messageCounterManager;
mStorage = storageDelegate;


// TODO: Handle error from mGlobalEncryptedMessageCounter! Unit tests currently crash if you do!
(void) mGlobalEncryptedMessageCounter.Init();
Expand Down
Loading

0 comments on commit 4f57404

Please sign in to comment.