Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / util / CHIPDeviceCallbacksMgr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  * @file
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.
25  */
26
27 #include "CHIPDeviceCallbacksMgr.h"
28
29 #include <core/CHIPCore.h>
30 #include <inttypes.h>
31
32 namespace {
33 struct ResponseCallbackInfo
34 {
35     chip::NodeId nodeId;
36     uint8_t sequenceNumber;
37
38     bool operator==(ResponseCallbackInfo const & other) { return nodeId == other.nodeId && sequenceNumber == other.sequenceNumber; }
39 };
40
41 struct ReportCallbackInfo
42 {
43     chip::NodeId nodeId;
44     chip::EndpointId endpointId;
45     chip::ClusterId clusterId;
46     chip::AttributeId attributeId;
47
48     bool operator==(ReportCallbackInfo const & other)
49     {
50         return nodeId == other.nodeId && endpointId == other.endpointId && clusterId == other.clusterId &&
51             attributeId == other.attributeId;
52     }
53 };
54 } // namespace
55
56 namespace chip {
57 namespace app {
58
59 CHIP_ERROR CHIPDeviceCallbacksMgr::AddResponseCallback(NodeId nodeId, uint8_t sequenceNumber,
60                                                        Callback::Cancelable * onSuccessCallback,
61                                                        Callback::Cancelable * onFailureCallback)
62 {
63     VerifyOrReturnError(onSuccessCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
64     VerifyOrReturnError(onFailureCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
65
66     ResponseCallbackInfo info = { nodeId, sequenceNumber };
67     memcpy(&onSuccessCallback->mInfoPtr, &info, sizeof(info));
68     memcpy(&onFailureCallback->mInfoPtr, &info, sizeof(info));
69
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);
74
75     mResponsesSuccess.Enqueue(onSuccessCallback);
76     mResponsesFailure.Enqueue(onFailureCallback);
77     return CHIP_NO_ERROR;
78 }
79
80 CHIP_ERROR CHIPDeviceCallbacksMgr::GetResponseCallback(NodeId nodeId, uint8_t sequenceNumber,
81                                                        Callback::Cancelable ** onSuccessCallback,
82                                                        Callback::Cancelable ** onFailureCallback)
83 {
84     ResponseCallbackInfo info = { nodeId, sequenceNumber };
85
86     ReturnErrorOnFailure(GetCallback(info, mResponsesSuccess, onSuccessCallback));
87     (*onSuccessCallback)->Cancel();
88
89     ReturnErrorOnFailure(GetCallback(info, mResponsesFailure, onFailureCallback));
90     (*onFailureCallback)->Cancel();
91
92     return CHIP_NO_ERROR;
93 }
94
95 CHIP_ERROR CHIPDeviceCallbacksMgr::AddReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId,
96                                                      AttributeId attributeId, Callback::Cancelable * onReportCallback)
97 {
98     VerifyOrReturnError(onReportCallback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
99
100     ReportCallbackInfo info = { nodeId, endpointId, clusterId, attributeId };
101     memmove(&onReportCallback->mInfoPtr, &info, sizeof(info));
102
103     // If a callback has already been registered for the same ReportCallbackInfo, let's cancel it.
104     CancelCallback(info, mReports);
105
106     mReports.Enqueue(onReportCallback);
107     return CHIP_NO_ERROR;
108 }
109
110 CHIP_ERROR CHIPDeviceCallbacksMgr::GetReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId,
111                                                      AttributeId attributeId, Callback::Cancelable ** onReportCallback)
112 {
113     ReportCallbackInfo info = { nodeId, endpointId, clusterId, attributeId };
114
115     ReturnErrorOnFailure(GetCallback(info, mReports, onReportCallback));
116
117     return CHIP_NO_ERROR;
118 }
119
120 } // namespace app
121 } // namespace chip