2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "wificond/looper_backed_event_loop.h"
19 #include <android-base/logging.h>
20 #include <utils/Looper.h>
21 #include <utils/Timers.h>
25 class EventLoopCallback : public android::MessageHandler {
27 explicit EventLoopCallback(const std::function<void()>& callback)
28 : callback_(callback) {
31 ~EventLoopCallback() override = default;
33 virtual void handleMessage(const android::Message& message) {
38 const std::function<void()> callback_;
40 DISALLOW_COPY_AND_ASSIGN(EventLoopCallback);
43 class WatchFdCallback : public android::LooperCallback {
45 explicit WatchFdCallback(const std::function<void(int)>& callback)
46 : callback_(callback) {
49 ~WatchFdCallback() override = default;
51 virtual int handleEvent(int fd, int events, void* data) {
53 // Returning 1 means Looper keeps watching this file descriptor after
54 // callback is called.
55 // See Looper.h for details.
60 const std::function<void(int)> callback_;
62 DISALLOW_COPY_AND_ASSIGN(WatchFdCallback);
71 LooperBackedEventLoop::LooperBackedEventLoop()
72 : should_continue_(true) {
73 looper_ = android::Looper::prepare(Looper::PREPARE_ALLOW_NON_CALLBACKS);
76 LooperBackedEventLoop::~LooperBackedEventLoop() {
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);
85 void LooperBackedEventLoop::PostDelayedTask(
86 const std::function<void()>& callback,
88 sp<android::MessageHandler> looper_callback = new EventLoopCallback(callback);
89 looper_->sendMessageDelayed(ms2ns(delay_ms), looper_callback, NULL);
92 bool LooperBackedEventLoop::WatchFileDescriptor(
95 const std::function<void(int)>& callback) {
96 sp<android::LooperCallback> watch_fd_callback = new WatchFdCallback(callback);
98 if (mode == kModeInput) {
99 event = Looper::EVENT_INPUT;
100 } else if (mode == kModeOutput) {
101 event = Looper::EVENT_OUTPUT;
103 LOG(ERROR) << "Invalid mode for WatchFileDescriptor().";
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().";
117 bool LooperBackedEventLoop::StopWatchFileDescriptor(int fd) {
118 if (looper_->removeFd(fd) == 1) {
124 void LooperBackedEventLoop::Poll() {
125 while (should_continue_) {
126 looper_->pollOnce(-1);
130 void LooperBackedEventLoop::PollForOne(int timeout_millis) {
131 looper_->pollOnce(timeout_millis);
134 void LooperBackedEventLoop::TriggerExit() {
135 PostTask([this](){ should_continue_ = false; });
138 } // namespace wificond
139 } // namespace android