OSDN Git Service

Setup wlan0 in AP mode on request
[android-x86/system-connectivity-wificond.git] / main.cpp
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 <unistd.h>
18
19 #include <csignal>
20 #include <memory>
21
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>
32
33 #include "ipc_constants.h"
34 #include "looper_backed_event_loop.h"
35 #include "server.h"
36
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;
45
46 namespace {
47
48 class ScopedSignalHandler final {
49  public:
50   ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
51     if (s_event_loop_ != nullptr) {
52       LOG(FATAL) << "Only instantiate one signal handler per process!";
53     }
54     s_event_loop_ = event_loop;
55     std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
56     std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
57   }
58
59   ~ScopedSignalHandler() {
60     std::signal(SIGINT, SIG_DFL);
61     std::signal(SIGTERM, SIG_DFL);
62     s_event_loop_ = nullptr;
63   }
64
65  private:
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();
70     }
71   }
72
73   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
74 };
75
76 android::wificond::LooperBackedEventLoop*
77     ScopedSignalHandler::s_event_loop_ = nullptr;
78
79
80 // Setup our interface to the Binder driver or die trying.
81 int SetupBinderOrCrash() {
82   int binder_fd = -1;
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;
88   return binder_fd;
89 }
90
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";
94
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),
98            android::NO_ERROR);
99 }
100
101 }  // namespace
102
103 void OnBinderReadReady(int fd) {
104   android::IPCThreadState::self()->handlePolledCommands();
105 }
106
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...";
110
111   unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
112       new android::wificond::LooperBackedEventLoop());
113   ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
114
115   int binder_fd = SetupBinderOrCrash();
116   CHECK(event_dispatcher->WatchFileDescriptor(
117       binder_fd,
118       android::wificond::EventLoop::kModeInput,
119       &OnBinderReadReady)) << "Failed to watch binder FD";
120
121
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);
127
128   event_dispatcher->Poll();
129   LOG(INFO) << "wificond is about to exit";
130   return 0;
131 }