Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / util / CHIPDeviceCallbacksMgr.h
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 definitions 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 #pragma once
28
29 #include "basic-types.h"
30 #include <core/CHIPCallback.h>
31 #include <core/CHIPError.h>
32 #include <support/DLLUtil.h>
33
34 namespace chip {
35 namespace app {
36
37 class DLL_EXPORT CHIPDeviceCallbacksMgr
38 {
39 public:
40     CHIPDeviceCallbacksMgr(const CHIPDeviceCallbacksMgr &)  = delete;
41     CHIPDeviceCallbacksMgr(const CHIPDeviceCallbacksMgr &&) = delete;
42     CHIPDeviceCallbacksMgr & operator=(const CHIPDeviceCallbacksMgr &) = delete;
43
44     static CHIPDeviceCallbacksMgr & GetInstance()
45     {
46         static CHIPDeviceCallbacksMgr instance;
47         return instance;
48     }
49
50     CHIP_ERROR AddResponseCallback(NodeId nodeId, uint8_t sequenceNumber, Callback::Cancelable * onSuccessCallback,
51                                    Callback::Cancelable * onFailureCallback);
52     CHIP_ERROR GetResponseCallback(NodeId nodeId, uint8_t sequenceNumber, Callback::Cancelable ** onSuccessCallback,
53                                    Callback::Cancelable ** onFailureCallback);
54
55     CHIP_ERROR AddReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId, AttributeId attributeId,
56                                  Callback::Cancelable * onReportCallback);
57     CHIP_ERROR GetReportCallback(NodeId nodeId, EndpointId endpointId, ClusterId clusterId, AttributeId attributeId,
58                                  Callback::Cancelable ** onReportCallback);
59
60 private:
61     CHIPDeviceCallbacksMgr() {}
62
63     template <typename T>
64     CHIP_ERROR CancelCallback(T & info, Callback::CallbackDeque & queue)
65     {
66         Callback::Cancelable * ca = nullptr;
67         CHIP_ERROR err            = GetCallback(info, queue, &ca);
68         if (CHIP_NO_ERROR == err)
69         {
70             ca->Cancel();
71             queue.Dequeue(ca);
72         }
73
74         return err;
75     }
76
77     template <typename T>
78     CHIP_ERROR GetCallback(T & info, Callback::CallbackDeque & queue, Callback::Cancelable ** callback)
79     {
80         Callback::Cancelable * ca = &queue;
81         while (ca != nullptr && ca->mNext != &queue)
82         {
83             if (*reinterpret_cast<T *>(&ca->mNext->mInfoPtr) == info)
84             {
85                 *callback = ca->mNext;
86                 return CHIP_NO_ERROR;
87             }
88
89             ca = ca->mNext;
90         }
91
92         return CHIP_ERROR_KEY_NOT_FOUND;
93     }
94
95     Callback::CallbackDeque mResponsesSuccess;
96     Callback::CallbackDeque mResponsesFailure;
97     Callback::CallbackDeque mReports;
98 };
99
100 } // namespace app
101 } // namespace chip