Skip to content

Commit 72450e4

Browse files
Make CommandHandler a pure interface class, have actual implementation in CommandHandlerImpl (project-chip#33736)
* First pass: renames only and moved things around. It should not yet compile * Move CommandHandler::Callback to CommandHandlerImpl::Callback * Prepare some compile fixes. Uses of preparation of responses is not ok yet * More replace fixes * More compile updates * Try to implement the addResponse properly * Many more updates for compilation * More compile tests - getting closer * Things compile * Restyle * Split out CommandHandler (the interface) from CommandHandlerImpl (actual IM implementation) * Restyle * make GetExchangeContext also be virtual * Fix some includes that were previously part of CommandHandler.h * Restyle * No need for casts and private APIs: Exchange context is actually available * Code review feedback * Restyle * Upgrading docs * Slight example fix --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com>
1 parent 88ebdf7 commit 72450e4

14 files changed

+1764
-1603
lines changed

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ BUG_REPORT
2222
code_generation
2323
zap_clusters
2424
spec_clusters
25+
upgrading
2526
ERROR_CODES
2627
2728
```

docs/upgrading.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Upgrading notes
2+
3+
## API changes and code migration
4+
5+
### `CommandHandler`
6+
7+
`CommandHandler` ability to directly invoke `Prepare/TLV-Write/Finish` cycles
8+
has been changed to only expose `AddResponse/AddStatus/AddClusterSpecific*`.
9+
10+
Original versions of `CommandHandler` exposed the following low-level
11+
implementation-specific methods: `PrepareCommand`,
12+
`PrepareInvokeResponseCommand`, `GetCommandDataIBTLVWriter` and `FinishCommand`.
13+
These are not exposed anymore and instead one should use `AddResponse` or
14+
`AddResponseData`. When using an `EncodableToTLV` argument, the same
15+
functionality should be achievable.
16+
17+
Example
18+
19+
Before:
20+
21+
```cpp
22+
23+
const CommandHandler::InvokeResponseParameters prepareParams(requestPath);
24+
ReturnOnFailure(commandHandler->PrepareInvokeResponseCommand(path, prepareParams));
25+
26+
TLV::TLVWriter *writer = commandHandler->GetCommandDataIBTLVWriter();
27+
ReturnOnFailure(writer->Put(chip::TLV::ContextTag(1), 123));
28+
ReturnOnFailure(writer->Put(chip::TLV::ContextTag(2), 234));
29+
return commandHandler->FinishCommand();
30+
```
31+
32+
After:
33+
34+
```cpp
35+
36+
class ReplyEncoder : public DataModel::EncodableToTLV
37+
{
38+
public:
39+
CHIP_ERROR EncodeTo(TLV::TLVWriter & writer, TLV::Tag tag) const override
40+
{
41+
TLV::TLVType outerType;
42+
ReturnErrorOnFailure(writer.StartContainer(tag, TLV::kTLVType_Structure, outerType));
43+
44+
ReturnOnFailure(writer.Put(chip::TLV::ContextTag(1), 123));
45+
ReturnOnFailure(writer.Put(chip::TLV::ContextTag(2), 234));
46+
47+
return writer.EndContainer(outerType);
48+
}
49+
};
50+
51+
// ...
52+
ReplyEncoder replyEncoder;
53+
commandHandler->AddResponse(path, kReplyCommandId, replyEncoder);
54+
55+
// or if error handling is implemented:
56+
//
57+
// ReturnErrorOnFailure(commandHandler->AddResponseData(path, kReplyCommandId, replyEncoder));
58+
//
59+
// In many cases error recovery from not being able to send a reply is not easy or expected,
60+
// so code does AddResponse rather than AddResponseData.
61+
62+
```

examples/lighting-app/tizen/src/DBusInterface.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <app-common/zap-generated/cluster-enums.h>
2323
#include <app-common/zap-generated/ids/Attributes.h>
2424
#include <app-common/zap-generated/ids/Clusters.h>
25+
#include <app/CommandHandlerImpl.h>
2526
#include <app/ConcreteAttributePath.h>
2627
#include <app/clusters/color-control-server/color-control-server.h>
2728
#include <app/clusters/level-control/level-control.h>
@@ -36,13 +37,13 @@ using namespace chip::app;
3637

3738
namespace example {
3839

39-
// Dummy class to satisfy the CommandHandler::Callback interface.
40-
class CommandHandlerCallback : public CommandHandler::Callback
40+
// Dummy class to satisfy the CommandHandlerImpl::Callback interface.
41+
class CommandHandlerImplCallback : public CommandHandlerImpl::Callback
4142
{
4243
public:
4344
using Status = Protocols::InteractionModel::Status;
44-
void OnDone(CommandHandler & apCommandObj) {}
45-
void DispatchCommand(CommandHandler & apCommandObj, const ConcreteCommandPath & aCommandPath, TLV::TLVReader & apPayload) {}
45+
void OnDone(CommandHandlerImpl & apCommandObj) {}
46+
void DispatchCommand(CommandHandlerImpl & apCommandObj, const ConcreteCommandPath & aCommandPath, TLV::TLVReader & apPayload) {}
4647
Status CommandExists(const ConcreteCommandPath & aCommandPath) { return Status::Success; }
4748
};
4849

@@ -188,8 +189,10 @@ gboolean DBusInterface::OnColorTemperatureChanged(LightAppColorControl * colorCo
188189
// Do not handle on-change event if it was triggered by internal set
189190
VerifyOrReturnValue(!self->mInternalSet, G_DBUS_METHOD_INVOCATION_HANDLED);
190191

191-
CommandHandlerCallback callback;
192-
CommandHandler handler(&callback);
192+
// TODO: creating such a complex object seems odd here
193+
// as handler seems not used to send back any response back anywhere.
194+
CommandHandlerImplCallback callback;
195+
CommandHandlerImpl handler(&callback);
193196

194197
ConcreteCommandPath path{ self->mEndpointId, Clusters::ColorControl::Id, 0 };
195198

src/app/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ source_set("command-handler") {
338338
"CommandHandler.cpp",
339339
"CommandHandler.h",
340340
"CommandHandlerExchangeInterface.h",
341+
"CommandHandlerImpl.cpp",
342+
"CommandHandlerImpl.h",
341343
]
342344

343345
public_deps = [

0 commit comments

Comments
 (0)