diff options
author | Christopher Wiley | 2016-06-27 13:38:27 -0500 |
---|---|---|
committer | Christopher Wiley | 2016-06-28 10:19:35 -0500 |
commit | bfcb1aa405a4e1c1249acf6dc61c7cb6f1471b5b (patch) | |
tree | 700b20682500804dd1a331ccd5aafaaa72f896c5 /main.cpp | |
parent | f229bbe6676ac7d12f4aa22a902eb2eefb2fc5c6 (diff) | |
download | system-connectivity-wificond-bfcb1aa405a4e1c1249acf6dc61c7cb6f1471b5b.tar.gz system-connectivity-wificond-bfcb1aa405a4e1c1249acf6dc61c7cb6f1471b5b.tar.xz system-connectivity-wificond-bfcb1aa405a4e1c1249acf6dc61c7cb6f1471b5b.zip |
Refactor initial binder configuration
Separate blocks of functionality into functions for readability.
Rename event_dispatcher_ to event_dispatcher, since it isn't a class
member.
Check that service registration was successful.
Bug: 29584274
Change-Id: Iec7c01e8974062403c1a207a80f028b70d9d11c1
Test: unit, integration tests pass
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 50 |
1 files changed, 31 insertions, 19 deletions
@@ -26,8 +26,8 @@ | |||
26 | #include <binder/ProcessState.h> | 26 | #include <binder/ProcessState.h> |
27 | #include <utils/String16.h> | 27 | #include <utils/String16.h> |
28 | 28 | ||
29 | #include <looper_backed_event_loop.h> | 29 | #include "looper_backed_event_loop.h" |
30 | #include <server.h> | 30 | #include "server.h" |
31 | 31 | ||
32 | using android::net::wifi::IWificond; | 32 | using android::net::wifi::IWificond; |
33 | 33 | ||
@@ -64,6 +64,25 @@ class ScopedSignalHandler final { | |||
64 | android::wificond::LooperBackedEventLoop* | 64 | android::wificond::LooperBackedEventLoop* |
65 | ScopedSignalHandler::s_event_loop_ = nullptr; | 65 | ScopedSignalHandler::s_event_loop_ = nullptr; |
66 | 66 | ||
67 | |||
68 | // Setup our interface to the Binder driver or die trying. | ||
69 | int SetupBinderOrCrash() { | ||
70 | int binder_fd = -1; | ||
71 | android::ProcessState::self()->setThreadPoolMaxThreadCount(0); | ||
72 | android::IPCThreadState::self()->disableBackgroundScheduling(true); | ||
73 | int err = android::IPCThreadState::self()->setupPolling(&binder_fd); | ||
74 | CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err); | ||
75 | CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd; | ||
76 | return binder_fd; | ||
77 | } | ||
78 | |||
79 | void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) { | ||
80 | android::sp<android::IServiceManager> sm = android::defaultServiceManager(); | ||
81 | CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager"; | ||
82 | CHECK_EQ(sm->addService(android::String16("wificond"), service), | ||
83 | android::NO_ERROR); | ||
84 | } | ||
85 | |||
67 | } // namespace | 86 | } // namespace |
68 | 87 | ||
69 | void OnBinderReadReady(int fd) { | 88 | void OnBinderReadReady(int fd) { |
@@ -73,28 +92,21 @@ void OnBinderReadReady(int fd) { | |||
73 | int main(int argc, char** argv) { | 92 | int main(int argc, char** argv) { |
74 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); | 93 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
75 | LOG(INFO) << "wificond is starting up..."; | 94 | LOG(INFO) << "wificond is starting up..."; |
76 | std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher_( | 95 | |
96 | std::unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher( | ||
77 | new android::wificond::LooperBackedEventLoop()); | 97 | new android::wificond::LooperBackedEventLoop()); |
78 | ScopedSignalHandler scoped_signal_handler(event_dispatcher_.get()); | 98 | ScopedSignalHandler scoped_signal_handler(event_dispatcher.get()); |
79 | 99 | ||
80 | int binder_fd = -1; | 100 | int binder_fd = SetupBinderOrCrash(); |
81 | android::ProcessState::self()->setThreadPoolMaxThreadCount(0); | 101 | CHECK(event_dispatcher->WatchFileDescriptor( |
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, | 102 | binder_fd, |
88 | android::wificond::EventLoop::kModeInput, | 103 | android::wificond::EventLoop::kModeInput, |
89 | &OnBinderReadReady)) { | 104 | &OnBinderReadReady)) << "Failed to watch binder FD"; |
90 | LOG(FATAL) << "Failed to watch binder FD"; | 105 | |
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(); | 106 | android::sp<android::IBinder> server = new android::wificond::Server(); |
95 | sm->addService(android::String16("wificond"), server); | 107 | RegisterServiceOrCrash(server); |
96 | 108 | ||
97 | event_dispatcher_->Poll(); | 109 | event_dispatcher->Poll(); |
98 | LOG(INFO) << "Leaving the loop..."; | 110 | LOG(INFO) << "wificond is about to exit"; |
99 | return 0; | 111 | return 0; |
100 | } | 112 | } |