| {{> header}} |
| |
| #import <Foundation/Foundation.h> |
| |
| #import "MTRAttributeCacheContainer_Internal.h" |
| #import "MTRBaseClusters_internal.h" |
| #import "MTRBaseDevice.h" |
| #import "MTRBaseDevice_Internal.h" |
| #import "MTRCallbackBridge_internal.h" |
| #import "MTRCluster_internal.h" |
| #import "MTRStructsObjc.h" |
| #import "MTRCommandPayloadsObjc.h" |
| |
| #include <lib/support/CHIPListUtils.h> |
| #include <platform/CHIPDeviceLayer.h> |
| #include <type_traits> |
| |
| using chip::Callback::Callback; |
| using chip::Callback::Cancelable; |
| using namespace chip::app::Clusters; |
| using chip::Messaging::ExchangeManager; |
| using chip::SessionHandle; |
| |
| // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks): Linter is unable to locate the delete on these objects. |
| {{#chip_client_clusters includeAll=true}} |
| @implementation MTRBaseCluster{{asUpperCamelCase name}} |
| |
| - (instancetype)initWithDevice:(MTRBaseDevice *)device endpoint:(uint16_t)endpoint queue:(dispatch_queue_t)queue |
| { |
| if (self = [super initWithQueue:queue]) { |
| if (device == nil) { |
| return nil; |
| } |
| |
| _device = device; |
| _endpoint = endpoint; |
| } |
| return self; |
| } |
| |
| {{#chip_cluster_commands}} |
| {{#*inline "callbackName"}}{{#if hasSpecificResponse}}{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}{{else}}CommandSuccess{{/if}}{{/inline}} |
| {{#unless (hasArguments)}} |
| - (void){{asLowerCamelCase name}}WithCompletionHandler:({{>command_completion_type command=.}})completionHandler |
| { |
| [self {{asLowerCamelCase name}}WithParams:nil completionHandler:completionHandler]; |
| } |
| {{/unless}} |
| - (void){{asLowerCamelCase name}}WithParams: (MTR{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=.}})completionHandler |
| { |
| // Make a copy of params before we go async. |
| params = [params copy]; |
| new MTR{{>callbackName}}CallbackBridge(self.callbackQueue, |
| self.device, |
| {{#if hasSpecificResponse}} |
| {{! This treats completionHandler as taking an id for the data. This is |
| not great from a type-safety perspective, of course. }} |
| completionHandler, |
| {{else}} |
| {{! For now, don't change the bridge API; instead just use an adapter |
| to invoke our completion handler. This is not great from a |
| type-safety perspective, of course. }} |
| ^(id _Nullable value, NSError * _Nullable error) { |
| completionHandler(error); |
| }, |
| {{/if}} |
| ^(ExchangeManager & exchangeManager, const SessionHandle & session, Cancelable * success, Cancelable * failure) { |
| chip::Optional<uint16_t> timedInvokeTimeoutMs; |
| ListFreer listFreer; |
| {{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type request; |
| if (params != nil) { |
| if (params.timedInvokeTimeoutMs != nil) { |
| timedInvokeTimeoutMs.SetValue(params.timedInvokeTimeoutMs.unsignedShortValue); |
| } |
| } |
| {{#if mustUseTimedInvoke}} |
| if (!timedInvokeTimeoutMs.HasValue()) { |
| timedInvokeTimeoutMs.SetValue(10000); |
| } |
| {{/if}} |
| {{#chip_cluster_command_arguments}} |
| {{#first}} |
| {{#unless (commandHasRequiredField parent)}} |
| if (params != nil) { |
| {{/unless}} |
| {{/first}} |
| {{>encode_value target=(concat "request." (asLowerCamelCase label)) source=(concat "params." (asStructPropertyName label)) cluster=parent.parent.name errorCode="return CHIP_ERROR_INVALID_ARGUMENT;" depth=0}} |
| {{#last}} |
| {{#unless (commandHasRequiredField parent)}} |
| } |
| {{/unless}} |
| {{/last}} |
| {{/chip_cluster_command_arguments}} |
| |
| auto successFn = Callback<{{>callbackName}}CallbackType>::FromCancelable(success); |
| auto failureFn = Callback<DefaultFailureCallbackType>::FromCancelable(failure); |
| chip::Controller::{{asUpperCamelCase parent.name}}Cluster cppCluster(exchangeManager, session, self->_endpoint); |
| return cppCluster.InvokeCommand(request, successFn->mContext, successFn->mCall, failureFn->mCall, timedInvokeTimeoutMs); |
| }); |
| } |
| {{/chip_cluster_commands}} |
| |
| {{#chip_server_cluster_attributes}} |
| {{#*inline "attribute"}}Attribute{{asUpperCamelCase name}}{{/inline}} |
| - (void)read{{>attribute}}With |
| {{~#if_is_fabric_scoped_struct type~}} |
| Params:(MTRReadParams * _Nullable)params completionHandler: |
| {{~else~}} |
| CompletionHandler: |
| {{~/if_is_fabric_scoped_struct~}} |
| (void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler |
| { |
| {{~#if_is_fabric_scoped_struct type}} |
| // Make a copy of params before we go async. |
| params = [params copy]; |
| {{/if_is_fabric_scoped_struct~}} |
| new MTR{{>attribute_data_callback_name}}CallbackBridge(self.callbackQueue, |
| self.device, |
| {{! This treats completionHandler as taking an id for the data. This is |
| not great from a type-safety perspective, of course. }} |
| completionHandler, |
| ^(ExchangeManager & exchangeManager, const SessionHandle & session, Cancelable * success, Cancelable * failure) { |
| using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo; |
| auto successFn = Callback<{{>attribute_data_callback_name}}Callback>::FromCancelable(success); |
| auto failureFn = Callback<DefaultFailureCallbackType>::FromCancelable(failure); |
| chip::Controller::{{asUpperCamelCase parent.name}}Cluster cppCluster(exchangeManager, session, self->_endpoint); |
| return cppCluster.ReadAttribute<TypeInfo>(successFn->mContext, successFn->mCall, failureFn->mCall |
| {{~#if_is_fabric_scoped_struct type~}} |
| , params == nil || params.fabricFiltered == nil || [params.fabricFiltered boolValue] |
| {{~/if_is_fabric_scoped_struct~}} |
| ); |
| }); |
| } |
| |
| {{#if isWritableAttribute}} |
| {{#*inline "callbackName"}}DefaultSuccess{{/inline}} |
| - (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value completionHandler:(StatusCompletion)completionHandler |
| { |
| [self write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:nil completionHandler:completionHandler]; |
| } |
| - (void)write{{>attribute}}WithValue:({{asObjectiveCType type parent.name}})value params:(MTRWriteParams * _Nullable)params completionHandler:(StatusCompletion)completionHandler |
| { |
| // Make a copy of params before we go async. |
| params = [params copy]; |
| value = [value copy]; |
| |
| new MTR{{>callbackName}}CallbackBridge(self.callbackQueue, |
| self.device, |
| {{! For now, don't change the bridge API; instead just use an adapter |
| to invoke our completion handler. This is not great from a |
| type-safety perspective, of course. }} |
| ^(id _Nullable ignored, NSError * _Nullable error) { |
| completionHandler(error); |
| }, |
| ^(ExchangeManager & exchangeManager, const SessionHandle & session, Cancelable * success, Cancelable * failure) { |
| chip::Optional<uint16_t> timedWriteTimeout; |
| if (params != nil) { |
| if (params.timedWriteTimeout != nil){ |
| timedWriteTimeout.SetValue(params.timedWriteTimeout.unsignedShortValue); |
| } |
| } |
| {{#if mustUseTimedInvoke}} |
| if (!timedWriteTimeout.HasValue()) { |
| timedWriteTimeout.SetValue(10000); |
| } |
| {{/if}} |
| |
| ListFreer listFreer; |
| using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo; |
| TypeInfo::Type cppValue; |
| {{>encode_value target="cppValue" source="value" cluster=parent.name errorCode="return CHIP_ERROR_INVALID_ARGUMENT;" depth=0}} |
| auto successFn = Callback<{{>callbackName}}CallbackType>::FromCancelable(success); |
| auto failureFn = Callback<DefaultFailureCallbackType>::FromCancelable(failure); |
| |
| chip::Controller::{{asUpperCamelCase parent.name}}Cluster cppCluster(exchangeManager, session, self->_endpoint); |
| return cppCluster.WriteAttribute<TypeInfo>(cppValue, successFn->mContext, successFn->mCall, failureFn->mCall, timedWriteTimeout); |
| }); |
| |
| } |
| |
| {{/if}} |
| {{#if isReportableAttribute}} |
| - (void) subscribe{{>attribute}}WithMinInterval:(NSNumber * _Nonnull)minInterval maxInterval:(NSNumber * _Nonnull)maxInterval |
| params:(MTRSubscribeParams * _Nullable)params |
| subscriptionEstablished:(SubscriptionEstablishedHandler _Nullable)subscriptionEstablishedHandler reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))reportHandler |
| { |
| // Make a copy of params before we go async. |
| minInterval = [minInterval copy]; |
| maxInterval = [maxInterval copy]; |
| params = [params copy]; |
| new MTR{{>attribute_data_callback_name}}CallbackSubscriptionBridge(self.callbackQueue, |
| self.device, |
| {{! This treats reportHandler as taking an id for the data. This is |
| not great from a type-safety perspective, of course. }} |
| reportHandler, |
| ^(ExchangeManager & exchangeManager, const SessionHandle & session, Cancelable * success, Cancelable * failure) { |
| if (params != nil && params.autoResubscribe != nil && ![params.autoResubscribe boolValue]) { |
| // We don't support disabling auto-resubscribe. |
| return CHIP_ERROR_INVALID_ARGUMENT; |
| } |
| using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo; |
| auto successFn = Callback<{{>attribute_data_callback_name}}Callback>::FromCancelable(success); |
| auto failureFn = Callback<DefaultFailureCallbackType>::FromCancelable(failure); |
| |
| chip::Controller::{{asUpperCamelCase parent.name}}Cluster cppCluster(exchangeManager, session, self->_endpoint); |
| return cppCluster.SubscribeAttribute<TypeInfo>(successFn->mContext, successFn->mCall, failureFn->mCall, [minInterval unsignedShortValue], [maxInterval unsignedShortValue], MTR{{>attribute_data_callback_name}}CallbackSubscriptionBridge::OnSubscriptionEstablished, nil, |
| params == nil || params.fabricFiltered == nil || [params.fabricFiltered boolValue], |
| params != nil && params.keepPreviousSubscriptions != nil && [params.keepPreviousSubscriptions boolValue] |
| ); |
| }, subscriptionEstablishedHandler); |
| } |
| |
| + (void) read{{>attribute}}WithAttributeCache:(MTRAttributeCacheContainer *)attributeCacheContainer endpoint:(NSNumber *)endpoint queue:(dispatch_queue_t)queue completionHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value, NSError * _Nullable error))completionHandler |
| { |
| new MTR{{>attribute_data_callback_name}}CallbackBridge(queue, |
| completionHandler, |
| ^(Cancelable * success, Cancelable * failure) { |
| if (attributeCacheContainer.cppAttributeCache) { |
| chip::app::ConcreteAttributePath path; |
| using TypeInfo = {{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo; |
| path.mEndpointId = static_cast<chip::EndpointId>([endpoint unsignedShortValue]); |
| path.mClusterId = TypeInfo::GetClusterId(); |
| path.mAttributeId = TypeInfo::GetAttributeId(); |
| TypeInfo::DecodableType value; |
| CHIP_ERROR err = attributeCacheContainer.cppAttributeCache->Get<TypeInfo>(path, value); |
| auto successFn = Callback<{{>attribute_data_callback_name}}Callback>::FromCancelable(success); |
| if (err == CHIP_NO_ERROR) |
| { |
| successFn->mCall(successFn->mContext, value); |
| } |
| return err; |
| } |
| return CHIP_ERROR_NOT_FOUND; |
| }); |
| } |
| |
| {{/if}} |
| {{/chip_server_cluster_attributes}} |
| |
| @end |
| |
| {{/chip_client_clusters}} |
| // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) |