• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 mainloop events and manager.
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  * Is the base for all mainloop event sources.
45  */
46 class Source
47 {
48     friend class Manager;
49 
50 public:
51     /**
52      * Registers events in the mainloop.
53      *
54      * @param[in,out]   aContext    A reference to the mainloop context.
55      */
56     virtual void Update(otSysMainloopContext &aContext) = 0;
57 
58     /**
59      * Processes the mainloop events.
60      *
61      * @param[in]   aContext    A reference to the mainloop context.
62      */
63     virtual void Process(const otSysMainloopContext &aContext) = 0;
64 
65     /**
66      * Marks destructor virtual method.
67      */
68     virtual ~Source() = default;
69 
70 private:
71     Source *mNext = nullptr;
72 };
73 
74 /**
75  * Manages mainloop.
76  */
77 class Manager
78 {
79 public:
80     /**
81      * Updates event polls in the mainloop context.
82      *
83      * @param[in,out]   aContext    A reference to the mainloop context.
84      */
85     void Update(otSysMainloopContext &aContext);
86 
87     /**
88      * Processes events in the mainloop context.
89      *
90      * @param[in]   aContext    A reference to the mainloop context.
91      */
92     void Process(const otSysMainloopContext &aContext);
93 
94     /**
95      * Adds a new event source into the mainloop.
96      *
97      * @param[in]   aSource     A reference to the event source.
98      */
99     void Add(Source &aSource);
100 
101     /**
102      * Removes an event source from the mainloop.
103      *
104      * @param[in]   aSource     A reference to the event source.
105      */
106     void Remove(Source &aSource);
107 
108     /**
109      * Returns the Mainloop singleton.
110      *
111      * @returns A reference to the Mainloop singleton.
112      */
113     static Manager &Get(void);
114 
115 private:
116     Source *mSources = nullptr;
117 };
118 
119 } // namespace Mainloop
120 } // namespace Posix
121 } // namespace ot
122 #endif // OT_POSIX_PLATFORM_MAINLOOP_HPP_
123