• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   void handleMessage(const android::Message& message) override {
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   looper_->sendMessage(sp<EventLoopCallback>::make(callback), Message());
81 }
82 
PostDelayedTask(const std::function<void ()> & callback,int64_t delay_ms)83 void LooperBackedEventLoop::PostDelayedTask(
84     const std::function<void()>& callback,
85     int64_t delay_ms) {
86   looper_->sendMessageDelayed(ms2ns(delay_ms), sp<EventLoopCallback>::make(callback), Message());
87 }
88 
WatchFileDescriptor(int fd,ReadyMode mode,const std::function<void (int)> & callback)89 bool LooperBackedEventLoop::WatchFileDescriptor(
90     int fd,
91     ReadyMode mode,
92     const std::function<void(int)>& callback) {
93   sp<android::LooperCallback>  watch_fd_callback = new WatchFdCallback(callback);
94   int event;
95   if (mode == kModeInput) {
96     event = Looper::EVENT_INPUT;
97   } else if (mode == kModeOutput) {
98     event = Looper::EVENT_OUTPUT;
99   } else {
100     LOG(ERROR) << "Invalid mode for WatchFileDescriptor().";
101     return false;
102   }
103   // addFd() returns 1 if descriptor was added, 0 if arguments were invalid.
104   // Since we are using non-NULL callback, the second parameter 'ident' will
105   // always be ignored. It is OK to use 0 for 'ident'.
106   // See Looper.h for more details.
107   if (looper_->addFd(fd, 0, event, watch_fd_callback, NULL) == 0) {
108     LOG(ERROR) << "Invalid arguments for Looper::addFd().";
109     return false;
110   }
111   return true;
112 }
113 
StopWatchFileDescriptor(int fd)114 bool LooperBackedEventLoop::StopWatchFileDescriptor(int fd) {
115   if (looper_->removeFd(fd) == 1) {
116     return true;
117   }
118   return false;
119 }
120 
Poll()121 void LooperBackedEventLoop::Poll() {
122   while (should_continue_) {
123     looper_->pollOnce(-1);
124   }
125 }
126 
PollForOne(int timeout_millis)127 void LooperBackedEventLoop::PollForOne(int timeout_millis) {
128   looper_->pollOnce(timeout_millis);
129 }
130 
TriggerExit()131 void LooperBackedEventLoop::TriggerExit() {
132   PostTask([this](){ should_continue_ = false; });
133 }
134 
135 }  // namespace wificond
136 }  // namespace android
137