Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2018 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 PlatformManager features
22 : * for use on various platforms.
23 : */
24 :
25 : #pragma once
26 :
27 : namespace chip {
28 : namespace DeviceLayer {
29 : namespace Internal {
30 :
31 : /**
32 : * Provides a generic implementation of PlatformManager features that works on multiple platforms.
33 : *
34 : * This template contains implementations of select features from the PlatformManager abstract
35 : * interface that are suitable for use on all platforms. It is intended to be inherited (directly
36 : * or indirectly) by the PlatformManagerImpl class, which also appears as the template's ImplClass
37 : * parameter.
38 : */
39 : template <class ImplClass>
40 : class GenericPlatformManagerImpl
41 : {
42 : protected:
43 : struct AppEventHandler
44 : {
45 : AppEventHandler * Next;
46 : PlatformManager::EventHandlerFunct Handler;
47 : intptr_t Arg;
48 : };
49 :
50 : AppEventHandler * mAppEventHandlerList;
51 :
52 : // ===== Methods that implement the PlatformManager abstract interface.
53 :
54 : CHIP_ERROR _InitChipStack();
55 : void _Shutdown();
56 : CHIP_ERROR _AddEventHandler(PlatformManager::EventHandlerFunct handler, intptr_t arg);
57 : void _RemoveEventHandler(PlatformManager::EventHandlerFunct handler, intptr_t arg);
58 : void _HandleServerStarted();
59 : void _HandleServerShuttingDown();
60 : CHIP_ERROR _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg);
61 : CHIP_ERROR _ScheduleBackgroundWork(AsyncWorkFunct workFunct, intptr_t arg);
62 : CHIP_ERROR _PostBackgroundEvent(const ChipDeviceEvent * event);
63 : void _RunBackgroundEventLoop(void);
64 : CHIP_ERROR _StartBackgroundEventLoopTask(void);
65 : CHIP_ERROR _StopBackgroundEventLoopTask();
66 : void _DispatchEvent(const ChipDeviceEvent * event);
67 :
68 : // ===== Support methods that can be overridden by the implementation subclass.
69 :
70 : void DispatchEventToDeviceLayer(const ChipDeviceEvent * event);
71 : void DispatchEventToApplication(const ChipDeviceEvent * event);
72 : static void HandleMessageLayerActivityChanged(bool messageLayerIsActive);
73 :
74 : private:
75 : bool mMsgLayerWasActive;
76 :
77 138 : ImplClass * Impl() { return static_cast<ImplClass *>(this); }
78 : };
79 :
80 : // Instruct the compiler to instantiate the template only when explicitly told to do so.
81 : extern template class GenericPlatformManagerImpl<PlatformManagerImpl>;
82 :
83 : } // namespace Internal
84 : } // namespace DeviceLayer
85 : } // namespace chip
|