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_EVENT_LOOP_H_
18 #define WIFICOND_EVENT_LOOP_H_
19 
20 #include <functional>
21 
22 namespace android {
23 namespace wificond {
24 
25 // Abstract class for dispatching tasks.
26 class EventLoop {
27  public:
28   enum ReadyMode {
29       kModeInput,
30       kModeOutput
31   };
32 
33   virtual ~EventLoop() {}
34 
35   // Enqueues a callback.
36   // This function can be called on any thread.
37   virtual void PostTask(const std::function<void()>& callback) = 0;
38 
39   // Enqueues a callback to be processed after a specified period of time.
40   // |delay_ms| is delay time in milliseconds. It should not be negative.
41   // This function can be called on any thread.
42   virtual void PostDelayedTask(const std::function<void()>& callback,
43                                int64_t delay_ms) = 0;
44 
45   // Monitoring file descriptor for data.
46   // Callback will be executed when specific file descriptor is ready.
47   // File descriptor is provided as a parameter to this callback:
48   // This function can be called on any thread.
49   // This returns true upon success and returns false when it failed.
50   virtual bool WatchFileDescriptor(
51       int fd,
52       ReadyMode mode,
53       const std::function<void(int)>& callback_) = 0;
54 
55   // Stop monitoring file descriptor |fd|.
56   // This function can be called on any thread.
57   // This returns true upon success and returns false when it failed to
58   // remove the file descriptor, or this file descriptor was not registered
59   // for watching.
60   virtual bool StopWatchFileDescriptor(int fd);
61 };
62 
63 }  // namespace wificond
64 }  // namespace android
65 
66 #endif  // WIFICOND_EVENT_LOOP_H_
67