Skip to content

Commit

Permalink
Apply PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jepenven-silabs committed Mar 7, 2022
1 parent 19cf0da commit fe8e6d8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint
err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, &mDeviceStorage, &GetFabricTable());
SuccessOrExit(err);

err = mFabricListener.Init(&mSessions);
err = mFabricDelegate.Init(&mSessions);
SuccessOrExit(err);
mFabrics.SetListener(&mFabricListener);
mFabrics.AddFabricDelegate(&mFabricDelegate);

err = mExchangeMgr.Init(&mSessions);
SuccessOrExit(err);
Expand Down
16 changes: 10 additions & 6 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ class Server
ServerTransportMgr * mTransports;
};

class FabricTableListener final : public FabricTable::FabricListener
class ServerFabricDelegate final : public FabricTableDelegate
{
public:
FabricTableListener() {}
ServerFabricDelegate() {}

CHIP_ERROR Init(SessionManager * sessionManager)
{
Expand All @@ -195,18 +195,22 @@ class Server
return CHIP_NO_ERROR;
};

void OnFabricRemoved(FabricIndex fabric_index) override
void OnFabricDeletedFromStorage(CompressedFabricId compressedId, FabricIndex fabricIndex) override
{
(void) compressedId;
if (mSessionManager != nullptr)
{
mSessionManager->SyncRemovalFabricIndex(fabric_index);
mSessionManager->SyncRemovalFabricIndex(fabricIndex);
}
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
if (groupDataProvider != nullptr)
{
groupDataProvider->RemoveFabric(fabric_index);
groupDataProvider->RemoveFabric(fabricIndex);
}
};
void OnFabricRetrievedFromStorage(FabricInfo * fabricInfo) override { (void) fabricInfo; }

void OnFabricPersistedToStorage(FabricInfo * fabricInfo) override { (void) fabricInfo; }

private:
SessionManager * mSessionManager = nullptr;
Expand Down Expand Up @@ -240,7 +244,7 @@ class Server
Credentials::GroupDataProviderImpl mGroupsProvider;
app::DefaultAttributePersistenceProvider mAttributePersister;
GroupDataProviderListener mListener;
FabricTableListener mFabricListener;
ServerFabricDelegate mFabricDelegate;

Access::AccessControl mAccessControl;

Expand Down
2 changes: 1 addition & 1 deletion src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
stateParams.messageCounterManager = chip::Platform::New<secure_channel::MessageCounterManager>();

ReturnErrorOnFailure(
stateParams.fabricTable->Init(mFabricStorage, chip::Platform::New<FabricTableListener>(stateParams.sessionMgr)));
stateParams.fabricTable->Init(mFabricStorage, chip::Platform::New<ControllerFabricDelegate>(stateParams.sessionMgr)));

ReturnErrorOnFailure(stateParams.sessionMgr->Init(stateParams.systemLayer, stateParams.transportMgr,
stateParams.messageCounterManager, params.fabricIndependentStorage,
Expand Down
16 changes: 10 additions & 6 deletions src/controller/CHIPDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ class DeviceControllerFactory
//
void ReleaseSystemState() { mSystemState->Release(); }

class FabricTableListener final : public FabricTable::FabricListener
class ControllerFabricDelegate final : public FabricTableDelegate
{
public:
FabricTableListener() {}
FabricTableListener(SessionManager * sessionManager) : mSessionManager(sessionManager) {}
ControllerFabricDelegate() {}
ControllerFabricDelegate(SessionManager * sessionManager) : FabricTableDelegate(true), mSessionManager(sessionManager) {}

CHIP_ERROR Init(SessionManager * sessionManager)
{
Expand All @@ -153,19 +153,23 @@ class DeviceControllerFactory
return CHIP_NO_ERROR;
};

void OnFabricRemoved(FabricIndex fabric_index) override
void OnFabricDeletedFromStorage(CompressedFabricId compressedId, FabricIndex fabricIndex) override
{
if (mSessionManager != nullptr)
{
mSessionManager->SyncRemovalFabricIndex(fabric_index);
mSessionManager->SyncRemovalFabricIndex(fabricIndex);
}
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
if (groupDataProvider != nullptr)
{
groupDataProvider->RemoveFabric(fabric_index);
groupDataProvider->RemoveFabric(fabricIndex);
}
};

void OnFabricRetrievedFromStorage(FabricInfo * fabricInfo) override { (void) fabricInfo; }

void OnFabricPersistedToStorage(FabricInfo * fabricInfo) override { (void) fabricInfo; }

private:
SessionManager * mSessionManager = nullptr;
};
Expand Down
11 changes: 3 additions & 8 deletions src/credentials/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,6 @@ CHIP_ERROR FabricTable::Delete(FabricIndex index)
mFabricCount--;
}
ChipLogProgress(Discovery, "Fabric (%d) deleted. Calling OnFabricDeletedFromStorage", index);
if (mFabricListener != nullptr)
{
mFabricListener->OnFabricRemoved(index);
}

FabricTableDelegate * delegate = mDelegate;
while (delegate)
Expand All @@ -653,13 +649,12 @@ void FabricTable::DeleteAllFabrics()
}
}

CHIP_ERROR FabricTable::Init(FabricStorage * storage, FabricListener * fabricListener)
CHIP_ERROR FabricTable::Init(FabricStorage * storage, FabricTableDelegate * fabricTableDelegate)
{
VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
if (fabricListener != nullptr)
if (fabricTableDelegate != nullptr)
{
mFabricListener = std::move(fabricListener);
mlistenerOwneship = true;
ReturnErrorOnFailure(AddFabricDelegate(fabricTableDelegate));
}

mStorage = storage;
Expand Down
42 changes: 11 additions & 31 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class DLL_EXPORT FabricTableDelegate
friend class FabricTable;

public:
FabricTableDelegate(bool selfOwned = false) : mSelfOwned(selfOwned) {}
virtual ~FabricTableDelegate() {}
/**
* Gets called when a fabric is deleted from KVS store.
Expand All @@ -347,6 +348,7 @@ class DLL_EXPORT FabricTableDelegate

private:
FabricTableDelegate * mNext = nullptr;
bool mSelfOwned = false;
};

/**
Expand Down Expand Up @@ -422,9 +424,15 @@ class DLL_EXPORT FabricTable
FabricTable() { Reset(); }
~FabricTable()
{
if (mlistenerOwneship)
FabricTableDelegate * delegate = mDelegate;
while (delegate)
{
chip::Platform::MemoryFree(mFabricListener);
FabricTableDelegate * temp = delegate->mNext;
if (delegate->mSelfOwned)
{
chip::Platform::Delete(delegate);
}
delegate = temp;
}
}
CHIP_ERROR Store(FabricIndex index);
Expand Down Expand Up @@ -456,29 +464,7 @@ class DLL_EXPORT FabricTable

void Reset();

/**
* Interface to listen for changes in FabricRemoval.
*/
class FabricListener
{
public:
virtual ~FabricListener() = default;
/**
* Callback invoked when an existing fabric is removed.
*
* @param[in] fabric_index Fabric Index of the removed fabric.
*/
virtual void OnFabricRemoved(FabricIndex fabric_index) = 0;
};

/**
* @brief Initialize the fabric table
* @arg FabricStorage * Pointer to the FabricStorage delegate for persistence
* @arg FabricListener * Transfert ownership of the listener pointer to the fabric table
* To prevent transfert of ownership, use SetListener method.
* @return CHIP_ERROR error code
*/
CHIP_ERROR Init(FabricStorage * storage, FabricListener * fabricListener);
CHIP_ERROR Init(FabricStorage * storage, FabricTableDelegate * fabricTableDelegate);
CHIP_ERROR AddFabricDelegate(FabricTableDelegate * delegate);

uint8_t FabricCount() const { return mFabricCount; }
Expand All @@ -488,20 +474,14 @@ class DLL_EXPORT FabricTable
ConstFabricIterator begin() const { return cbegin(); }
ConstFabricIterator end() const { return cend(); }

void SetListener(FabricListener * fabricListener) { mFabricListener = fabricListener; }

private:
FabricInfo mStates[CHIP_CONFIG_MAX_FABRICS];
FabricStorage * mStorage = nullptr;

FabricTableDelegate * mDelegate = nullptr;

FabricListener * mFabricListener = nullptr;

FabricIndex mNextAvailableFabricIndex = kMinValidFabricIndex;
uint8_t mFabricCount = 0;

bool mlistenerOwneship = false;
};

} // namespace chip

0 comments on commit fe8e6d8

Please sign in to comment.