OSDN Git Service

Add android.net.wifi.IWificond AIDL
[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 <utils/String16.h>
28
29 #include <looper_backed_event_loop.h>
30 #include <server.h>
31
32 using android::net::wifi::IWificond;
33
34 namespace {
35
36 class ScopedSignalHandler final {
37  public:
38   ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
39     if (s_event_loop_ != nullptr) {
40       LOG(FATAL) << "Only instantiate one signal handler per process!";
41     }
42     s_event_loop_ = event_loop;
43     std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
44     std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
45   }
46
47   ~ScopedSignalHandler() {
48     std::signal(SIGINT, SIG_DFL);
49     std::signal(SIGTERM, SIG_DFL);
50     s_event_loop_ = nullptr;
51   }
52
53  private:
54   static android::wificond::LooperBackedEventLoop* s_event_loop_;
55   static void LeaveLoop(int signal) {
56     if (s_event_loop_ != nullptr) {
57       s_event_loop_->TriggerExit();
58     }
59   }
60
61   DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
62 };
63
64 android::wificond::LooperBackedEventLoop*
65     ScopedSignalHandler::s_event_loop_ = nullptr;
66
67 }  // namespace
68
69 void OnBinderReadReady(int fd) {
70   android::IPCThreadState::self()->handlePolledCommands();
71 }
72
73 int main(int argc, char** argv) {
74   android::base::InitLogging(argv);
75   LOG(INFO) << "wificond is starting up...";
76   std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher_(
77       new android::wificond::LooperBackedEventLoop());
78   ScopedSignalHandler scoped_signal_handler(event_dispatcher_.get());
79
80   int binder_fd = -1;
81   android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
82   android::IPCThreadState::self()->disableBackgroundScheduling(true);
83   int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
84   CHECK_EQ(err, 0) << "Error setting up binder polling: " << err;
85   CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
86   if (!event_dispatcher_->WatchFileDescriptor(
87       binder_fd,
88       android::wificond::EventLoop::kModeInput,
89       &OnBinderReadReady)) {
90     LOG(FATAL) << "Failed to watch binder FD";
91   }
92   android::sp<android::IServiceManager> sm = android::defaultServiceManager();
93   CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
94   android::sp<android::IBinder> server = new android::wificond::Server();
95   sm->addService(android::String16("wificond"), server);
96
97   event_dispatcher_->Poll();
98   LOG(INFO) << "Leaving the loop...";
99   return 0;
100 }