Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / app / zap-templates / templates / chip / CHIPClusters-src.zapt
1 {{> header}}
2 #include "CHIPClusters.h"
3
4 #include <cstdint>
5
6 #include <app/chip-zcl-zpro-codec-api.h>
7 #include <lib/support/Span.h>
8 #include <support/ReturnMacros.h>
9
10 namespace chip {
11 namespace Controller {
12
13 // TODO(#4502): onCompletion is not used by IM for now.
14 // TODO(#4503): length should be passed to commands when byte string is in argument list.
15 // TODO(#4503): Commands should take group id as an argument.
16
17 {{#chip_server_clusters}}
18
19 // {{asCamelCased name false}} Cluster Commands
20 {{#chip_server_cluster_commands}}
21 CHIP_ERROR {{asCamelCased clusterName false}}Cluster::{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback{{#chip_server_cluster_command_arguments}}, {{chipType}} {{asCamelCased label}}{{/chip_server_cluster_command_arguments}})
22 {
23 #ifdef CHIP_APP_USE_INTERACTION_MODEL
24     VerifyOrReturnError(mDevice != nullptr, CHIP_ERROR_INCORRECT_STATE);
25     (void) onSuccessCallback;
26 (void) onFailureCallback;
27
28     app::Command::CommandParams cmdParams = { mEndpoint, /* group id */ 0, mClusterId, k{{asCamelCased name false}}CommandId,
29                                          (chip::app::Command::CommandPathFlags::kEndpointIdValid) };
30     app::Command * ZCLcommand = mDevice->GetCommandSender();
31
32     TLV::TLVWriter writer  = ZCLcommand->CreateCommandDataElementTLVWriter();
33
34     TLV::TLVType dummyType = TLV::kTLVType_NotSpecified;
35     ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag, TLV::kTLVType_Structure, dummyType));
36
37 {{#chip_server_cluster_command_arguments}}
38 {{#first}}
39     uint8_t argSeqNumber = 0;
40 {{/first}}
41     // {{asCamelCased label}}: {{asCamelCased type}}
42 {{#if (isCharString type)}}
43     ReturnErrorOnFailure(writer.PutString(TLV::ContextTag(argSeqNumber++), {{asCamelCased label}}));
44 {{else}}
45     ReturnErrorOnFailure(writer.Put(TLV::ContextTag(argSeqNumber++), {{asCamelCased label}}));
46 {{/if}}
47 {{else}}
48     // Command takes no arguments.
49 {{/chip_server_cluster_command_arguments}}
50
51     ReturnErrorOnFailure(writer.EndContainer(dummyType));
52     ReturnErrorOnFailure(writer.Finalize());
53     ReturnErrorOnFailure(ZCLcommand->AddCommand(cmdParams));
54
55     return mDevice->SendCommands();
56 #else
57     uint8_t seqNum = mDevice->GetNextSequenceNumber();
58     System::PacketBufferHandle encodedCommand = encode{{asCamelCased clusterName false}}Cluster{{asType name}}Command(seqNum, mEndpoint{{#chip_server_cluster_command_arguments}}, {{asCamelCased label}}{{/chip_server_cluster_command_arguments}});
59     return SendCommand(seqNum, std::move(encodedCommand), onSuccessCallback, onFailureCallback);
60 #endif
61 }
62
63 {{/chip_server_cluster_commands}}
64 // {{asCamelCased name false}} Cluster Attributes
65 CHIP_ERROR {{asCamelCased name false}}Cluster::DiscoverAttributes(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback)
66 {
67     uint8_t seqNum = mDevice->GetNextSequenceNumber();
68     System::PacketBufferHandle encodedCommand = encode{{asCamelCased name false}}ClusterDiscoverAttributes(seqNum, mEndpoint);
69     return SendCommand(seqNum, std::move(encodedCommand), onSuccessCallback, onFailureCallback);
70 }
71 {{#chip_server_cluster_attributes}}
72 CHIP_ERROR {{asCamelCased parent.name false}}Cluster::ReadAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback)
73 {
74     uint8_t seqNum = mDevice->GetNextSequenceNumber();
75     System::PacketBufferHandle encodedCommand = encode{{asCamelCased parent.name false}}ClusterRead{{asCamelCased name false}}Attribute(seqNum, mEndpoint);
76     return SendCommand(seqNum, std::move(encodedCommand), onSuccessCallback, onFailureCallback);
77 }
78
79 {{#if (isWritableAttribute)}}
80 CHIP_ERROR {{asCamelCased parent.name false}}Cluster::WriteAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, {{chipType}} value)
81 {
82     uint8_t seqNum = mDevice->GetNextSequenceNumber();
83     System::PacketBufferHandle encodedCommand = encode{{asCamelCased parent.name false}}ClusterWrite{{asCamelCased name false}}Attribute(seqNum, mEndpoint, value);
84     return SendCommand(seqNum, std::move(encodedCommand), onSuccessCallback, onFailureCallback);
85 }
86
87 {{/if}}
88 {{#if (isReportableAttribute)}}
89 CHIP_ERROR {{asCamelCased parent.name false}}Cluster::ConfigureAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, uint16_t minInterval, uint16_t maxInterval{{#unless (isDiscreteType)}}, {{chipType}} change{{/unless}})
90 {
91     uint8_t seqNum = mDevice->GetNextSequenceNumber();
92     System::PacketBufferHandle encodedCommand = encode{{asCamelCased parent.name false}}ClusterConfigure{{asCamelCased name false}}Attribute(seqNum, mEndpoint, minInterval, maxInterval{{#unless (isDiscreteType)}}, change{{/unless}});
93     return SendCommand(seqNum, std::move(encodedCommand), onSuccessCallback, onFailureCallback);
94 }
95
96 CHIP_ERROR {{asCamelCased parent.name false}}Cluster::ReportAttribute{{asCamelCased name false}}(Callback::Cancelable * onReportCallback)
97 {
98     return RequestAttributeReporting({{asHex attributeCode 4}}, onReportCallback);
99 }
100
101 {{/if}}
102 {{/chip_server_cluster_attributes}}
103 {{/chip_server_clusters}}
104
105 } // namespace Controller
106 } // namespace chip