1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
18 #define WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
19 
20 #include "event_loop.h"
21 
22 #include <android-base/macros.h>
23 #include <utils/Looper.h>
24 
25 namespace android {
26 namespace wificond {
27 
28 class LooperBackedEventLoop: public EventLoop {
29  public:
30   LooperBackedEventLoop();
31   ~LooperBackedEventLoop() override;
32 
33   // See event_loop.h
34   void PostTask(const std::function<void()>& callback) override;
35 
36   // See event_loop.h
37   void PostDelayedTask(const std::function<void()>& callback,
38                        int64_t delay_ms) override;
39   // See event_loop.h
40   bool WatchFileDescriptor(
41       int fd,
42       ReadyMode mode,
43       const std::function<void(int)>& callback) override;
44 
45   // See event_loop.h
46   bool StopWatchFileDescriptor(int fd) override;
47 
48   // Performs all pending callbacks and waiting for new events until
49   // TriggerExit() is called.
50   // This method can be called from any thread context.
51   void Poll();
52 
53   // Blocks for |timeout_millis| for the next event.
54   // This method can be called from any thread context.
55   void PollForOne(int timeout_millis);
56 
57   // Posts a task to stop event loop polling.
58   // This method can be called from any thread context.
59   void TriggerExit();
60 
61  private:
62   sp<android::Looper> looper_;
63   bool should_continue_;
64 
65   DISALLOW_COPY_AND_ASSIGN(LooperBackedEventLoop);
66 };
67 
68 }  // namespace wificond
69 }  // namespace android
70 
71 #endif  // WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
72