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