LOCAL_CPPFLAGS := -std=c++11 -Wall -Werror -Wno-unused-parameter
LOCAL_INIT_RC := wificond.rc
LOCAL_SRC_FILES := \
- main.cpp
+ main.cpp \
+ looper_backed_event_loop.cpp
LOCAL_SHARED_LIBRARIES := \
- libbase
+ libbase \
+ libutils
+LOCAL_MODULE_HOST_OS := linux
include $(BUILD_EXECUTABLE)
###
--- /dev/null
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef WIFICOND_EVENT_LOOP_H_
+#define WIFICOND_EVENT_LOOP_H_
+
+#include <functional>
+
+namespace android {
+namespace wificond {
+
+// Abstract class for dispatching tasks.
+class EventLoop {
+ public:
+ virtual ~EventLoop() {}
+
+ // Enqueues a callback.
+ // This function can be called on any thread.
+ virtual void PostTask(const std::function<void()>& callback) = 0;
+
+ // Enqueues a callback to be processed after a specified period of time.
+ // |delay_ms| is delay time in milliseconds. It should not be negative.
+ // This function can be called on any thread.
+ virtual void PostDelayedTask(const std::function<void()>& callback,
+ int64_t delay_ms) = 0;
+ //TODO(nywang): monitoring file descriptor for data
+};
+
+} // namespace wificond
+} // namespace android
+
+#endif // WIFICOND_EVENT_LOOP_H_
--- /dev/null
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <looper_backed_event_loop.h>
+
+#include <utils/Looper.h>
+#include <utils/Timers.h>
+
+namespace {
+
+class EventLoopCallback : public android::MessageHandler {
+ public:
+ explicit EventLoopCallback(const std::function<void()>& callback)
+ : callback_(callback) {
+ }
+
+ ~EventLoopCallback() override = default;
+
+ virtual void handleMessage(const android::Message& message) {
+ callback_();
+ }
+
+ private:
+ const std::function<void()> callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(EventLoopCallback);
+};
+
+} // namespace
+
+namespace android {
+namespace wificond {
+
+
+LooperBackedEventLoop::LooperBackedEventLoop()
+ : should_continue_(true) {
+ looper_ = android::Looper::prepare(Looper::PREPARE_ALLOW_NON_CALLBACKS);
+}
+
+LooperBackedEventLoop::~LooperBackedEventLoop() {
+}
+
+void LooperBackedEventLoop::PostTask(const std::function<void()>& callback) {
+ sp<android::MessageHandler> event_loop_callback =
+ new EventLoopCallback(callback);
+ looper_->sendMessage(event_loop_callback, NULL);
+}
+
+void LooperBackedEventLoop::PostDelayedTask(
+ const std::function<void()>& callback,
+ int64_t delay_ms) {
+ sp<android::MessageHandler> looper_callback = new EventLoopCallback(callback);
+ looper_->sendMessageDelayed(ms2ns(delay_ms), looper_callback, NULL);
+}
+
+void LooperBackedEventLoop::Poll() {
+ while (should_continue_) {
+ looper_->pollOnce(-1);
+ }
+}
+
+void LooperBackedEventLoop::TriggerExit() {
+ PostTask([this](){ should_continue_ = false; });
+}
+
+} // namespace wificond
+} // namespace android
--- /dev/null
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
+#define WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
+
+#include <event_loop.h>
+
+#include <android-base/macros.h>
+#include <utils/Looper.h>
+
+namespace android {
+namespace wificond {
+
+class LooperBackedEventLoop: public EventLoop {
+ public:
+ LooperBackedEventLoop();
+ ~LooperBackedEventLoop() override;
+
+ // See event_loop.h
+ void PostTask(const std::function<void()>& callback) override;
+
+ // See event_loop.h
+ void PostDelayedTask(const std::function<void()>& callback,
+ int64_t delay_ms) override;
+
+ // Performs all pending callbacks and waiting for new events until
+ // TriggerExit() is called.
+ // This method can be called from any thread context.
+ void Poll();
+
+ // Posts a task to stop event loop polling.
+ // This method can be called from any thread context.
+ void TriggerExit();
+
+ private:
+ sp<android::Looper> looper_;
+ bool should_continue_;
+
+ DISALLOW_COPY_AND_ASSIGN(LooperBackedEventLoop);
+};
+
+} // namespace wificond
+} // namespace android
+
+#endif // WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
#include <unistd.h>
#include <csignal>
+#include <memory>
#include <android-base/logging.h>
+#include <android-base/macros.h>
+
+#include <looper_backed_event_loop.h>
namespace {
-volatile bool ShouldContinue = true;
+class ScopedSignalHandler final {
+ public:
+ ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
+ if (s_event_loop_ != nullptr) {
+ LOG(FATAL) << "Only instantiate one signal handler per process!";
+ }
+ s_event_loop_ = event_loop;
+ std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
+ std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
+ }
+
+ ~ScopedSignalHandler() {
+ std::signal(SIGINT, SIG_DFL);
+ std::signal(SIGTERM, SIG_DFL);
+ s_event_loop_ = nullptr;
+ }
+
+ private:
+ static android::wificond::LooperBackedEventLoop* s_event_loop_;
+ static void LeaveLoop(int signal) {
+ if (s_event_loop_ != nullptr) {
+ s_event_loop_->TriggerExit();
+ }
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
+};
+
+android::wificond::LooperBackedEventLoop*
+ ScopedSignalHandler::s_event_loop_ = nullptr;
} // namespace
-void leave_loop(int signal) {
- ShouldContinue = false;
+void callback_for_test() {
+ LOG(INFO) << "callback is executed...";
}
int main(int argc, char** argv) {
android::base::InitLogging(argv);
LOG(INFO) << "wificond is starting up...";
- std::signal(SIGINT, &leave_loop);
- std::signal(SIGTERM, &leave_loop);
- while (ShouldContinue) {
- sleep(1);
- }
+ std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher_(
+ new android::wificond::LooperBackedEventLoop());
+ ScopedSignalHandler scoped_signal_handler(event_dispatcher_.get());
+ //TODO(nywang): b//28982981, Remove this when we have unittest for event loop checked in.
+ event_dispatcher_->PostTask(&callback_for_test);
+ event_dispatcher_->PostDelayedTask(&callback_for_test, 5000);
+ event_dispatcher_->Poll();
LOG(INFO) << "Leaving the loop...";
return 0;
}