-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move command handler registered list out of InteractionModelEngine an…
…d into a separate `Registry` (#34414) * Starting to define a commandhandlerregistry * Add missing files and replace some deprecated files usage * Replace all usages of IME to the command handler registry direct calls * Restyle * Fix wrong comment paste * Fix include * Remove unused include * Fix linter error * Update src/app/util/attribute-storage.cpp Co-authored-by: Boris Zbarsky <[email protected]> * Update src/controller/tests/TestServerCommandDispatch.cpp Co-authored-by: Boris Zbarsky <[email protected]> * Update src/controller/tests/TestServerCommandDispatch.cpp Co-authored-by: Boris Zbarsky <[email protected]> --------- Co-authored-by: Andrei Litvin <[email protected]> Co-authored-by: Boris Zbarsky <[email protected]>
- Loading branch information
1 parent
10b67ad
commit 3b97495
Showing
27 changed files
with
266 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include <app/CommandHandlerInterfaceRegistry.h> | ||
|
||
using namespace chip::app; | ||
|
||
namespace { | ||
|
||
CommandHandlerInterface * gCommandHandlerList = nullptr; | ||
|
||
} | ||
|
||
namespace chip { | ||
namespace app { | ||
namespace CommandHandlerInterfaceRegistry { | ||
|
||
void UnregisterAllHandlers() | ||
{ | ||
|
||
CommandHandlerInterface * handlerIter = gCommandHandlerList; | ||
|
||
// | ||
// Walk our list of command handlers and de-register them, before finally | ||
// nulling out the list entirely. | ||
// | ||
while (handlerIter) | ||
{ | ||
CommandHandlerInterface * nextHandler = handlerIter->GetNext(); | ||
handlerIter->SetNext(nullptr); | ||
handlerIter = nextHandler; | ||
} | ||
|
||
gCommandHandlerList = nullptr; | ||
} | ||
|
||
CHIP_ERROR RegisterCommandHandler(CommandHandlerInterface * handler) | ||
{ | ||
VerifyOrReturnError(handler != nullptr, CHIP_ERROR_INVALID_ARGUMENT); | ||
|
||
for (auto * cur = gCommandHandlerList; cur; cur = cur->GetNext()) | ||
{ | ||
if (cur->Matches(*handler)) | ||
{ | ||
ChipLogError(InteractionModel, "Duplicate command handler registration failed"); | ||
return CHIP_ERROR_INCORRECT_STATE; | ||
} | ||
} | ||
|
||
handler->SetNext(gCommandHandlerList); | ||
gCommandHandlerList = handler; | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
void UnregisterAllCommandHandlersForEndpoint(EndpointId endpointId) | ||
{ | ||
|
||
CommandHandlerInterface * prev = nullptr; | ||
|
||
for (auto * cur = gCommandHandlerList; cur; cur = cur->GetNext()) | ||
{ | ||
if (cur->MatchesEndpoint(endpointId)) | ||
{ | ||
if (prev == nullptr) | ||
{ | ||
gCommandHandlerList = cur->GetNext(); | ||
} | ||
else | ||
{ | ||
prev->SetNext(cur->GetNext()); | ||
} | ||
|
||
cur->SetNext(nullptr); | ||
} | ||
else | ||
{ | ||
prev = cur; | ||
} | ||
} | ||
} | ||
|
||
CHIP_ERROR UnregisterCommandHandler(CommandHandlerInterface * handler) | ||
{ | ||
VerifyOrReturnError(handler != nullptr, CHIP_ERROR_INVALID_ARGUMENT); | ||
CommandHandlerInterface * prev = nullptr; | ||
|
||
for (auto * cur = gCommandHandlerList; cur; cur = cur->GetNext()) | ||
{ | ||
if (cur->Matches(*handler)) | ||
{ | ||
if (prev == nullptr) | ||
{ | ||
gCommandHandlerList = cur->GetNext(); | ||
} | ||
else | ||
{ | ||
prev->SetNext(cur->GetNext()); | ||
} | ||
|
||
cur->SetNext(nullptr); | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
prev = cur; | ||
} | ||
|
||
return CHIP_ERROR_KEY_NOT_FOUND; | ||
} | ||
|
||
CommandHandlerInterface * GetCommandHandler(EndpointId endpointId, ClusterId clusterId) | ||
{ | ||
for (auto * cur = gCommandHandlerList; cur; cur = cur->GetNext()) | ||
{ | ||
if (cur->Matches(endpointId, clusterId)) | ||
{ | ||
return cur; | ||
} | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
} // namespace CommandHandlerInterfaceRegistry | ||
} // namespace app | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <app/CommandHandlerInterface.h> | ||
|
||
namespace chip { | ||
namespace app { | ||
namespace CommandHandlerInterfaceRegistry { | ||
|
||
/// Remove the entire linked list of handlers | ||
void UnregisterAllHandlers(); | ||
|
||
/// Add a new handler to the list of registered command handlers | ||
/// | ||
/// At most one command handler can exist for a given endpoint/cluster combination. Trying | ||
/// to register conflicting handlers will result in a `CHIP_ERROR_INCORRECT_STATE` error. | ||
CHIP_ERROR RegisterCommandHandler(CommandHandlerInterface * handler); | ||
|
||
/// Unregister all commandHandlers that `MatchesEndpoint` for the given endpointId. | ||
void UnregisterAllCommandHandlersForEndpoint(EndpointId endpointId); | ||
|
||
/// Unregister a single handler. | ||
/// | ||
/// If the handler is not registered, a `CHIP_ERROR_KEY_NOT_FOUND` is returned. | ||
CHIP_ERROR UnregisterCommandHandler(CommandHandlerInterface * handler); | ||
|
||
/// Find the command handler for the given endpoint/cluster combination or return | ||
/// nullptr if no such command handler exists. | ||
CommandHandlerInterface * GetCommandHandler(EndpointId endpointId, ClusterId clusterId); | ||
|
||
} // namespace CommandHandlerInterfaceRegistry | ||
} // namespace app | ||
} // namespace chip |
Oops, something went wrong.