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 #include "wificond/looper_backed_event_loop.h"
18 
19 #include <android-base/logging.h>
20 #include <utils/Looper.h>
21 #include <utils/Timers.h>
22 
23 namespace {
24 
25 class EventLoopCallback : public android::MessageHandler {
26  public:
EventLoopCallback(const std::function<void ()> & callback)27   explicit EventLoopCallback(const std::function<void()>& callback)
28       : callback_(callback) {
29   }
30 
31   ~EventLoopCallback() override = default;
32 
handleMessage(const android::Message & message)33   virtual void handleMessage(const android::Message& message) {
34     callback_();
35   }
36 
37  private:
38   const std::function<void()> callback_;
39 
40   DISALLOW_COPY_AND_ASSIGN(EventLoopCallback);
41 };
42 
43 class WatchFdCallback : public android::LooperCallback {
44  public:
WatchFdCallback(const std::function<void (int)> & callback)45   explicit WatchFdCallback(const std::function<void(int)>& callback)
46       : callback_(callback) {
47   }
48 
49   ~WatchFdCallback() override = default;
50 
handleEvent(int fd,int events,void * data)51   virtual int handleEvent(int fd, int events, void* data) {
52     callback_(fd);
53     // Returning 1 means Looper keeps watching this file descriptor after
54     // callback is called.
55     // See Looper.h for details.
56     return 1;
57   }
58 
59  private:
60   const std::function<void(int)> callback_;
61 
62   DISALLOW_COPY_AND_ASSIGN(WatchFdCallback);
63 };
64 
65 }  // namespace
66 
67 namespace android {
68 namespace wificond {
69 
70 
LooperBackedEventLoop()71 LooperBackedEventLoop::LooperBackedEventLoop()
72     : should_continue_(true) {
73   looper_ = android::Looper::prepare(Looper::PREPARE_ALLOW_NON_CALLBACKS);
74 }
75 
~LooperBackedEventLoop()76 LooperBackedEventLoop::~LooperBackedEventLoop() {
77 }
78 
PostTask(const std::function<void ()> & callback)79 void LooperBackedEventLoop::PostTask(const std::function<void()>& callback) {
80   sp<android::MessageHandler> event_loop_callback =
81       new EventLoopCallback(callback);
82   looper_->sendMessage(event_loop_callback, NULL);
83 }
84 
PostDelayedTask(const std::function<void ()> & callback,int64_t delay_ms)85 void LooperBackedEventLoop::PostDelayedTask(
86     const std::function<void()>& callback,
87     int64_t delay_ms) {
88   sp<android::MessageHandler> looper_callback = new EventLoopCallback(callback);
89   looper_->sendMessageDelayed(ms2ns(delay_ms), looper_callback, NULL);
90 }
91 
WatchFileDescriptor(int fd,ReadyMode mode,const std::function<void (int)> & callback)92 bool LooperBackedEventLoop::WatchFileDescriptor(
93     int fd,
94     ReadyMode mode,
95     const std::function<void(int)>& callback) {
96   sp<android::LooperCallback>  watch_fd_callback = new WatchFdCallback(callback);
97   int event;
98   if (mode == kModeInput) {
99     event = Looper::EVENT_INPUT;
100   } else if (mode == kModeOutput) {
101     event = Looper::EVENT_OUTPUT;
102   } else {
103     LOG(ERROR) << "Invalid mode for WatchFileDescriptor().";
104     return false;
105   }
106   // addFd() returns 1 if descriptor was added, 0 if arguments were invalid.
107   // Since we are using non-NULL callback, the second parameter 'ident' will
108   // always be ignored. It is OK to use 0 for 'ident'.
109   // See Looper.h for more details.
110   if (looper_->addFd(fd, 0, event, watch_fd_callback, NULL) == 0) {
111     LOG(ERROR) << "Invalid arguments for Looper::addFd().";
112     return false;
113   }
114   return true;
115 }
116 
StopWatchFileDescriptor(int fd)117 bool LooperBackedEventLoop::StopWatchFileDescriptor(int fd) {
118   if (looper_->removeFd(fd) == 1) {
119     return true;
120   }
121   return false;
122 }
123 
Poll()124 void LooperBackedEventLoop::Poll() {
125   while (should_continue_) {
126     looper_->pollOnce(-1);
127   }
128 }
129 
PollForOne(int timeout_millis)130 void LooperBackedEventLoop::PollForOne(int timeout_millis) {
131   looper_->pollOnce(timeout_millis);
132 }
133 
TriggerExit()134 void LooperBackedEventLoop::TriggerExit() {
135   PostTask([this](){ should_continue_ = false; });
136 }
137 
138 }  // namespace wificond
139 }  // namespace android
140