Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 1 | #define LOG_TAG "hwservicemanager" |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 2 | |
| 3 | #include <utils/Log.h> |
| 4 | |
| 5 | #include <inttypes.h> |
| 6 | #include <unistd.h> |
| 7 | |
Yifan Hong | 61ff909 | 2017-01-10 10:19:18 -0800 | [diff] [blame] | 8 | #include <android/hidl/manager/1.0/BnHwServiceManager.h> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 9 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 10 | #include <android/hidl/token/1.0/ITokenManager.h> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 11 | #include <cutils/properties.h> |
| 12 | #include <hidl/Status.h> |
| 13 | #include <hwbinder/IPCThreadState.h> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 14 | #include <utils/Errors.h> |
| 15 | #include <utils/Looper.h> |
| 16 | #include <utils/StrongPointer.h> |
| 17 | |
| 18 | #include "ServiceManager.h" |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 19 | #include "TokenManager.h" |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 20 | |
| 21 | // libutils: |
| 22 | using android::BAD_TYPE; |
| 23 | using android::Looper; |
| 24 | using android::LooperCallback; |
| 25 | using android::OK; |
| 26 | using android::sp; |
| 27 | using android::status_t; |
| 28 | |
| 29 | // libhwbinder: |
| 30 | using android::hardware::IPCThreadState; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 31 | |
| 32 | // libhidl |
Martijn Coenen | 437bbb4 | 2016-12-28 17:06:54 +0100 | [diff] [blame] | 33 | using android::hardware::configureRpcThreadpool; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 34 | using android::hardware::hidl_string; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 35 | using android::hardware::hidl_vec; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 36 | |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 37 | // hidl types |
Yifan Hong | 61ff909 | 2017-01-10 10:19:18 -0800 | [diff] [blame] | 38 | using android::hidl::manager::V1_0::BnHwServiceManager; |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame^] | 39 | using android::hidl::manager::V1_1::IServiceManager; |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 40 | using android::hidl::token::V1_0::ITokenManager; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 41 | |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 42 | // implementations |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame^] | 43 | using android::hidl::manager::implementation::ServiceManager; |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 44 | using android::hidl::token::V1_0::implementation::TokenManager; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 45 | |
Yifan Hong | 1899688 | 2017-02-24 17:04:48 -0800 | [diff] [blame] | 46 | static std::string serviceName = "default"; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 47 | |
| 48 | class BinderCallback : public LooperCallback { |
| 49 | public: |
| 50 | BinderCallback() {} |
| 51 | ~BinderCallback() override {} |
| 52 | |
| 53 | int handleEvent(int /* fd */, int /* events */, void* /* data */) override { |
| 54 | IPCThreadState::self()->handlePolledCommands(); |
| 55 | return 1; // Continue receiving callbacks. |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | int main() { |
Martijn Coenen | 437bbb4 | 2016-12-28 17:06:54 +0100 | [diff] [blame] | 60 | configureRpcThreadpool(1, true /* callerWillJoin */); |
| 61 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 62 | ServiceManager *manager = new ServiceManager(); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 63 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 64 | if (!manager->add(serviceName, manager)) { |
| 65 | ALOGE("Failed to register hwservicemanager with itself."); |
| 66 | } |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 67 | |
Steven Moreland | 66ac401 | 2016-12-21 15:06:10 -0800 | [diff] [blame] | 68 | TokenManager *tokenManager = new TokenManager(); |
| 69 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 70 | if (!manager->add(serviceName, tokenManager)) { |
| 71 | ALOGE("Failed to register ITokenManager with hwservicemanager."); |
| 72 | } |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 73 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 74 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 75 | |
| 76 | int binder_fd = -1; |
| 77 | |
| 78 | IPCThreadState::self()->setupPolling(&binder_fd); |
| 79 | if (binder_fd < 0) { |
Steven Moreland | 50bf182 | 2017-03-27 08:33:16 -0700 | [diff] [blame] | 80 | ALOGE("Failed to aquire binder FD. Aborting..."); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 81 | return -1; |
| 82 | } |
Martijn Coenen | ed0cc07 | 2017-03-27 12:57:31 -0700 | [diff] [blame] | 83 | // Flush after setupPolling(), to make sure the binder driver |
| 84 | // knows about this thread handling commands. |
| 85 | IPCThreadState::self()->flushCommands(); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 86 | |
| 87 | sp<BinderCallback> cb(new BinderCallback); |
| 88 | if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb, |
Steven Moreland | 50bf182 | 2017-03-27 08:33:16 -0700 | [diff] [blame] | 89 | nullptr) != 1) { |
| 90 | ALOGE("Failed to add hwbinder FD to Looper. Aborting..."); |
| 91 | return -1; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Tell IPCThreadState we're the service manager |
Yifan Hong | 61ff909 | 2017-01-10 10:19:18 -0800 | [diff] [blame] | 95 | sp<BnHwServiceManager> service = new BnHwServiceManager(manager); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 96 | IPCThreadState::self()->setTheContextObject(service); |
| 97 | // Then tell binder kernel |
| 98 | ioctl(binder_fd, BINDER_SET_CONTEXT_MGR, 0); |
| 99 | |
| 100 | int rc = property_set("hwservicemanager.ready", "true"); |
| 101 | if (rc) { |
Steven Moreland | 50bf182 | 2017-03-27 08:33:16 -0700 | [diff] [blame] | 102 | ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\ |
| 103 | "HAL services will not start!\n", rc); |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | while (true) { |
| 107 | looper->pollAll(-1 /* timeoutMillis */); |
| 108 | } |
| 109 | |
| 110 | return 0; |
Martijn Coenen | fcfcdfb | 2016-12-12 13:46:44 +0100 | [diff] [blame] | 111 | } |