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.
22 #include <android-base/logging.h>
23 #include <android-base/macros.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/ProcessState.h>
27 #include <cutils/properties.h>
28 #include <utils/String16.h>
29 #include <wifi_hal/driver_tool.h>
30 #include <wifi_system/hal_tool.h>
31 #include <wifi_system/interface_tool.h>
33 #include "ipc_constants.h"
34 #include "looper_backed_event_loop.h"
37 using android::net::wifi::IWificond;
38 using android::wifi_hal::DriverTool;
39 using android::wifi_system::HalTool;
40 using android::wifi_system::InterfaceTool;
41 using android::wificond::ipc_constants::kDevModePropertyKey;
42 using android::wificond::ipc_constants::kDevModeServiceName;
43 using android::wificond::ipc_constants::kServiceName;
44 using std::unique_ptr;
48 class ScopedSignalHandler final {
50 ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
51 if (s_event_loop_ != nullptr) {
52 LOG(FATAL) << "Only instantiate one signal handler per process!";
54 s_event_loop_ = event_loop;
55 std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
56 std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
59 ~ScopedSignalHandler() {
60 std::signal(SIGINT, SIG_DFL);
61 std::signal(SIGTERM, SIG_DFL);
62 s_event_loop_ = nullptr;
66 static android::wificond::LooperBackedEventLoop* s_event_loop_;
67 static void LeaveLoop(int signal) {
68 if (s_event_loop_ != nullptr) {
69 s_event_loop_->TriggerExit();
73 DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
76 android::wificond::LooperBackedEventLoop*
77 ScopedSignalHandler::s_event_loop_ = nullptr;
80 // Setup our interface to the Binder driver or die trying.
81 int SetupBinderOrCrash() {
83 android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
84 android::IPCThreadState::self()->disableBackgroundScheduling(true);
85 int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
86 CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
87 CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
91 void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
92 android::sp<android::IServiceManager> sm = android::defaultServiceManager();
93 CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
95 const int8_t dev_mode_on = property_get_bool(kDevModePropertyKey, 0);
96 const char* service_name = (dev_mode_on) ? kDevModeServiceName : kServiceName;
97 CHECK_EQ(sm->addService(android::String16(service_name), service),
103 void OnBinderReadReady(int fd) {
104 android::IPCThreadState::self()->handlePolledCommands();
107 int main(int argc, char** argv) {
108 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
109 LOG(INFO) << "wificond is starting up...";
111 unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
112 new android::wificond::LooperBackedEventLoop());
113 ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
115 int binder_fd = SetupBinderOrCrash();
116 CHECK(event_dispatcher->WatchFileDescriptor(
118 android::wificond::EventLoop::kModeInput,
119 &OnBinderReadReady)) << "Failed to watch binder FD";
122 android::sp<android::IBinder> server = new android::wificond::Server(
123 unique_ptr<HalTool>(new HalTool),
124 unique_ptr<InterfaceTool>(new InterfaceTool),
125 unique_ptr<DriverTool>(new DriverTool));
126 RegisterServiceOrCrash(server);
128 event_dispatcher->Poll();
129 LOG(INFO) << "wificond is about to exit";