3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file contains implementations for the CallbacksMgr class. The object of this
22 * class will be used by Controller applications to interact with ZCL messages.
23 * This class provide mechanism to store callbacks for global message dispatching
24 * across the ZCL stack.
27 #include "CHIPDeviceCallbacksMgr.h"
29 #include <core/CHIPCore.h>
33 struct ResponseCallbackInfo
36 uint8_t sequenceNumber;
38 bool operator==(ResponseCallbackInfo const & other) { return nodeId == other.nodeId && sequenceNumber == other.sequenceNumber; }
41 struct ReportCallbackInfo
44 chip::EndpointId endpointId;
45 chip::ClusterId clusterId;
46 chip::AttributeId attributeId;
48 bool operator==(ReportCallbackInfo const & other)
50 return nodeId == other.nodeId && endpointId == other.endpointId && clusterId == other.clusterId &&
51 attributeId == other.attributeId;
59 CHIP_ERROR CHIPDeviceCallbacksMgr::AddResponseCallback(NodeId nodeId, uint8_t sequenceNumber,
60 Callback::Cancelable * onSuccessCallback,
61 Callback::Cancelable * onFailureCallback)
63 VerifyOrReturnError(onSuccessCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
64 VerifyOrReturnError(onFailureCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
66 ResponseCallbackInfo info = { nodeId, sequenceNumber };
67 memcpy(&onSuccessCallback->mInfoPtr, &info, sizeof(info));
68 memcpy(&onFailureCallback->mInfoPtr, &info, sizeof(info));
70 // If some callbacks have already been registered for the same ResponseCallbackInfo, it usually means that the response
71 // has not been received for a previous command with the same sequenceNumber. Cancel the previously registered callbacks.
72 CancelCallback(info, mResponsesSuccess);
73 CancelCallback(info, mResponsesFailure);
75 mResponsesSuccess.Enqueue(onSuccessCallback);
76 mResponsesFailure.Enqueue(onFailureCallback);
80 CHIP_ERROR CHIPDeviceCallbacksMgr::GetResponseCallback(NodeId nodeId, uint8_t sequenceNumber,
81 Callback::Cancelable ** onSuccessCallback,
82 Callback::Cancelable ** onFailureCallback)
84 ResponseCallbackInfo info = { nodeId, sequenceNumber };
86 ReturnErrorOnFailure(GetCallback(info, mResponsesSuccess, onSuccessCallback));
87 (*onSuccessCallback)->Cancel();
89 ReturnErrorOnFailure(GetCallback(info, mResponsesFailure, onFailureCallback));
90 (*onFailureCallback)->Cancel();
95 CHIP_ERROR CHIPDeviceCallbacksMgr::AddReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId,
96 AttributeId attributeId, Callback::Cancelable * onReportCallback)
98 VerifyOrReturnError(onReportCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
100 ReportCallbackInfo info = { nodeId, endpointId, clusterId, attributeId };
101 memmove(&onReportCallback->mInfoPtr, &info, sizeof(info));
103 // If a callback has already been registered for the same ReportCallbackInfo, let's cancel it.
104 CancelCallback(info, mReports);
106 mReports.Enqueue(onReportCallback);
107 return CHIP_NO_ERROR;
110 CHIP_ERROR CHIPDeviceCallbacksMgr::GetReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId,
111 AttributeId attributeId, Callback::Cancelable ** onReportCallback)
113 ReportCallbackInfo info = { nodeId, endpointId, clusterId, attributeId };
115 ReturnErrorOnFailure(GetCallback(info, mReports, onReportCallback));
117 return CHIP_NO_ERROR;