forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterCommandBridge.h
138 lines (118 loc) · 5.11 KB
/
ClusterCommandBridge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* 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
#import "MTRError_Utils.h"
#import <Matter/Matter.h>
#include "ModelCommandBridge.h"
class ClusterCommand : public ModelCommand {
public:
ClusterCommand()
: ModelCommand("command-by-id")
{
AddArgument("cluster-id", 0, UINT32_MAX, &mClusterId);
AddArgument("command-id", 0, UINT32_MAX, &mCommandId);
AddArgument("payload", &mPayload);
AddArguments();
}
ClusterCommand(chip::ClusterId clusterId)
: ModelCommand("command-by-id")
, mClusterId(clusterId)
{
AddArgument("command-id", 0, UINT32_MAX, &mCommandId);
AddArgument("payload", &mPayload);
AddArguments();
}
~ClusterCommand() {}
CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId) override
{
chip::TLV::TLVWriter writer;
chip::TLV::TLVReader reader;
mData = static_cast<uint8_t *>(chip::Platform::MemoryCalloc(sizeof(uint8_t), mDataMaxLen));
VerifyOrReturnError(mData != nullptr, CHIP_ERROR_NO_MEMORY);
writer.Init(mData, mDataMaxLen);
ReturnErrorOnFailure(mPayload.Encode(writer, chip::TLV::AnonymousTag()));
reader.Init(mData, writer.GetLengthWritten());
ReturnErrorOnFailure(reader.Next());
id commandFields = NSObjectFromCHIPTLV(&reader);
if (commandFields == nil) {
return CHIP_ERROR_INTERNAL;
}
return ClusterCommand::SendCommand(device, endpointId, mClusterId, mCommandId, commandFields);
}
CHIP_ERROR SendCommand(MTRBaseDevice * _Nonnull device, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::CommandId commandId, id _Nonnull commandFields)
{
uint16_t repeatCount = mRepeatCount.ValueOr(1);
uint16_t __block responsesNeeded = repeatCount;
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.command", DISPATCH_QUEUE_SERIAL);
while (repeatCount--) {
[device invokeCommandWithEndpointID:[NSNumber numberWithUnsignedShort:endpointId]
clusterID:[NSNumber numberWithUnsignedInteger:clusterId]
commandID:[NSNumber numberWithUnsignedInteger:commandId]
commandFields:commandFields
timedInvokeTimeout:mTimedInteractionTimeoutMs.HasValue()
? [NSNumber numberWithUnsignedShort:mTimedInteractionTimeoutMs.Value()]
: nil
clientQueue:callbackQueue
completion:^(
NSArray<NSDictionary<NSString *, id> *> * _Nullable values, NSError * _Nullable error) {
responsesNeeded--;
if (error != nil) {
mError = error;
LogNSError("Error", error);
}
if (responsesNeeded == 0) {
SetCommandExitStatus(mError);
}
}];
if (mRepeatDelayInMs.HasValue()) {
[NSThread sleepForTimeInterval:((double) mRepeatDelayInMs.Value()) / 1000];
}
}
return CHIP_NO_ERROR;
}
void Shutdown() override
{
mError = nil;
ModelCommand::Shutdown();
}
protected:
ClusterCommand(const char * _Nonnull commandName)
: ModelCommand(commandName)
{
// Subclasses are responsible for calling AddArguments.
}
void AddArguments()
{
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs,
"If provided, do a timed invoke with the given timed interaction timeout.");
AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
ModelCommand::AddArguments();
}
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<uint16_t> mRepeatCount;
chip::Optional<uint16_t> mRepeatDelayInMs;
NSError * _Nullable mError = nil;
private:
chip::ClusterId mClusterId;
chip::CommandId mCommandId;
CustomArgument mPayload;
static constexpr uint32_t mDataMaxLen = 4096;
uint8_t * _Nullable mData = nullptr;
};