Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / GenericConnectivityManagerImpl_Thread.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
4  *    Copyright (c) 2019 Nest Labs, Inc.
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  *          Provides an generic implementation of ConnectivityManager features
22  *          for use on platforms that support Thread.
23  */
24
25 #pragma once
26
27 #include <platform/ThreadStackManager.h>
28 #include <support/BitFlags.h>
29
30 #include <cstdint>
31
32 namespace chip {
33 namespace DeviceLayer {
34
35 class ConnectivityManagerImpl;
36
37 namespace Internal {
38
39 /**
40  * Provides a generic implementation of WiFi-specific ConnectivityManager features for
41  * use on platforms that support Thread.
42  *
43  * This class is intended to be inherited (directly or indirectly) by the ConnectivityManagerImpl
44  * class, which also appears as the template's ImplClass parameter.
45  *
46  * The GenericConnectivityManagerImpl_Thread<> class is designed to be independent of the particular
47  * Thread stack in use, implying, for example, that the code does not make direct use of any OpenThread
48  * APIs.  This is achieved by delegating all stack-specific operations to the ThreadStackManager class.
49  *
50  */
51 template <class ImplClass>
52 class GenericConnectivityManagerImpl_Thread
53 {
54 protected:
55     // ===== Methods that implement the ConnectivityManager abstract interface.
56
57     void _Init();
58     void _OnPlatformEvent(const ChipDeviceEvent * event);
59     ConnectivityManager::ThreadMode _GetThreadMode();
60     CHIP_ERROR _SetThreadMode(ConnectivityManager::ThreadMode val);
61     bool _IsThreadEnabled();
62     bool _IsThreadApplicationControlled();
63     ConnectivityManager::ThreadDeviceType _GetThreadDeviceType();
64     CHIP_ERROR _SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType);
65     void _GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig);
66     CHIP_ERROR _SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig);
67     bool _IsThreadAttached();
68     bool _IsThreadProvisioned();
69     void _ErasePersistentInfo();
70     bool _HaveServiceConnectivityViaThread();
71
72     // ===== Members for use by the implementation subclass.
73
74     void UpdateServiceConnectivity();
75
76 private:
77     // ===== Private members reserved for use by this class only.
78
79     enum class Flags : uint8_t
80     {
81         kHaveServiceConnectivity = 0x01,
82         kIsApplicationControlled = 0x02
83     };
84
85     BitFlags<Flags> mFlags;
86
87     ImplClass * Impl() { return static_cast<ImplClass *>(this); }
88 };
89
90 template <class ImplClass>
91 inline void GenericConnectivityManagerImpl_Thread<ImplClass>::_Init()
92 {
93     mFlags.ClearAll();
94 }
95
96 template <class ImplClass>
97 inline bool GenericConnectivityManagerImpl_Thread<ImplClass>::_IsThreadEnabled()
98 {
99     return ThreadStackMgrImpl().IsThreadEnabled();
100 }
101
102 template <class ImplClass>
103 inline bool GenericConnectivityManagerImpl_Thread<ImplClass>::_IsThreadApplicationControlled()
104 {
105     return mFlags.Has(Flags::kIsApplicationControlled);
106 }
107
108 template <class ImplClass>
109 inline bool GenericConnectivityManagerImpl_Thread<ImplClass>::_IsThreadAttached()
110 {
111     return ThreadStackMgrImpl().IsThreadAttached();
112 }
113
114 template <class ImplClass>
115 inline bool GenericConnectivityManagerImpl_Thread<ImplClass>::_IsThreadProvisioned()
116 {
117     return ThreadStackMgrImpl().IsThreadProvisioned();
118 }
119
120 template <class ImplClass>
121 inline void GenericConnectivityManagerImpl_Thread<ImplClass>::_ErasePersistentInfo()
122 {
123     ThreadStackMgrImpl().ErasePersistentInfo();
124 }
125
126 template <class ImplClass>
127 inline ConnectivityManager::ThreadDeviceType GenericConnectivityManagerImpl_Thread<ImplClass>::_GetThreadDeviceType()
128 {
129     return ThreadStackMgrImpl().GetThreadDeviceType();
130 }
131
132 template <class ImplClass>
133 inline CHIP_ERROR
134 GenericConnectivityManagerImpl_Thread<ImplClass>::_SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType)
135 {
136     return ThreadStackMgrImpl().SetThreadDeviceType(deviceType);
137 }
138
139 template <class ImplClass>
140 inline void
141 GenericConnectivityManagerImpl_Thread<ImplClass>::_GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig)
142 {
143     ThreadStackMgrImpl().GetThreadPollingConfig(pollingConfig);
144 }
145
146 template <class ImplClass>
147 inline CHIP_ERROR GenericConnectivityManagerImpl_Thread<ImplClass>::_SetThreadPollingConfig(
148     const ConnectivityManager::ThreadPollingConfig & pollingConfig)
149 {
150     return ThreadStackMgrImpl().SetThreadPollingConfig(pollingConfig);
151 }
152
153 template <class ImplClass>
154 inline bool GenericConnectivityManagerImpl_Thread<ImplClass>::_HaveServiceConnectivityViaThread()
155 {
156     return mFlags.Has(Flags::kHaveServiceConnectivity);
157 }
158
159 } // namespace Internal
160 } // namespace DeviceLayer
161 } // namespace chip