1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definitions for the SPI interface to radio (RCP).
32  */
33 
34 #ifndef OT_POSIX_PLATFORM_MAINLOOP_HPP_
35 #define OT_POSIX_PLATFORM_MAINLOOP_HPP_
36 
37 #include <openthread/openthread-system.h>
38 
39 namespace ot {
40 namespace Posix {
41 namespace Mainloop {
42 
43 /**
44  * This class is the base for all mainloop event sources.
45  *
46  */
47 class Source
48 {
49     friend class Manager;
50 
51 public:
52     /**
53      * This method registers events in the mainloop.
54      *
55      * @param[inout]    aContext    A reference to the mainloop context.
56      *
57      */
58     virtual void Update(otSysMainloopContext &aContext) = 0;
59 
60     /**
61      * This method processes the mainloop events.
62      *
63      * @param[in]   aContext    A reference to the mainloop context.
64      *
65      */
66     virtual void Process(const otSysMainloopContext &aContext) = 0;
67 
68 private:
69     Source *mNext = nullptr;
70 };
71 
72 /**
73  * This class manages mainloop.
74  *
75  */
76 class Manager
77 {
78 public:
79     /**
80      * This method updates event polls in the mainloop context.
81      *
82      * @param[inout]    aContext    A reference to the mainloop context.
83      *
84      */
85     void Update(otSysMainloopContext &aContext);
86 
87     /**
88      * This method processes events in the mainloop context.
89      *
90      * @param[in]   aContext    A reference to the mainloop context.
91      *
92      */
93     void Process(const otSysMainloopContext &aContext);
94 
95     /**
96      * This method adds a new event source into the mainloop.
97      *
98      * @param[in]   aSource     A reference to the event source.
99      *
100      */
101     void Add(Source &aSource);
102 
103     /**
104      * This method removes an event source from the mainloop.
105      *
106      * @param[in]   aSource     A reference to the event source.
107      *
108      */
109     void Remove(Source &aSource);
110 
111     /**
112      * This function returns the Mainloop singleton.
113      *
114      * @returns A refernce to the Mainloop singleton.
115      *
116      */
117     static Manager &Get(void);
118 
119 private:
120     Source *mSources = nullptr;
121 };
122 
123 } // namespace Mainloop
124 } // namespace Posix
125 } // namespace ot
126 #endif // OT_POSIX_PLATFORM_MAINLOOP_HPP_
127