blob: 56fcb0d82f9bae24a4f2f702b335f28317403f46 [file] [log] [blame]
Steven Moreland5fb3d652016-11-03 13:45:18 -07001#define LOG_TAG "hwservicemanager"
Steven Morelandd83d1102016-10-25 15:01:47 -07002
3#include <utils/Log.h>
4
5#include <inttypes.h>
6#include <unistd.h>
7
Yifan Hong61ff9092017-01-10 10:19:18 -08008#include <android/hidl/manager/1.0/BnHwServiceManager.h>
Steven Morelandd83d1102016-10-25 15:01:47 -07009#include <android/hidl/manager/1.0/IServiceManager.h>
Steven Moreland66ac4012016-12-21 15:06:10 -080010#include <android/hidl/token/1.0/ITokenManager.h>
Steven Morelandd83d1102016-10-25 15:01:47 -070011#include <cutils/properties.h>
12#include <hidl/Status.h>
13#include <hwbinder/IPCThreadState.h>
Steven Morelandd83d1102016-10-25 15:01:47 -070014#include <utils/Errors.h>
15#include <utils/Looper.h>
16#include <utils/StrongPointer.h>
17
18#include "ServiceManager.h"
Steven Moreland66ac4012016-12-21 15:06:10 -080019#include "TokenManager.h"
Steven Morelandd83d1102016-10-25 15:01:47 -070020
21// libutils:
22using android::BAD_TYPE;
23using android::Looper;
24using android::LooperCallback;
25using android::OK;
26using android::sp;
27using android::status_t;
28
29// libhwbinder:
30using android::hardware::IPCThreadState;
Steven Morelandd83d1102016-10-25 15:01:47 -070031
32// libhidl
Martijn Coenen437bbb42016-12-28 17:06:54 +010033using android::hardware::configureRpcThreadpool;
Steven Morelandd83d1102016-10-25 15:01:47 -070034using android::hardware::hidl_string;
Steven Moreland5fb3d652016-11-03 13:45:18 -070035using android::hardware::hidl_vec;
Steven Morelandd83d1102016-10-25 15:01:47 -070036
Steven Moreland66ac4012016-12-21 15:06:10 -080037// hidl types
Yifan Hong61ff9092017-01-10 10:19:18 -080038using android::hidl::manager::V1_0::BnHwServiceManager;
Steven Moreland6f4fbe12017-07-21 18:07:42 -070039using android::hidl::manager::V1_1::IServiceManager;
Steven Moreland66ac4012016-12-21 15:06:10 -080040using android::hidl::token::V1_0::ITokenManager;
Steven Morelandd83d1102016-10-25 15:01:47 -070041
Steven Moreland66ac4012016-12-21 15:06:10 -080042// implementations
Steven Moreland6f4fbe12017-07-21 18:07:42 -070043using android::hidl::manager::implementation::ServiceManager;
Steven Moreland66ac4012016-12-21 15:06:10 -080044using android::hidl::token::V1_0::implementation::TokenManager;
Steven Morelandd83d1102016-10-25 15:01:47 -070045
Yifan Hong18996882017-02-24 17:04:48 -080046static std::string serviceName = "default";
Steven Morelandd83d1102016-10-25 15:01:47 -070047
48class BinderCallback : public LooperCallback {
49public:
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
59int main() {
Martijn Coenen437bbb42016-12-28 17:06:54 +010060 configureRpcThreadpool(1, true /* callerWillJoin */);
61
Steven Morelandd83d1102016-10-25 15:01:47 -070062 ServiceManager *manager = new ServiceManager();
Steven Morelandd83d1102016-10-25 15:01:47 -070063
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010064 if (!manager->add(serviceName, manager)) {
65 ALOGE("Failed to register hwservicemanager with itself.");
66 }
Steven Moreland76237812016-11-08 15:59:04 -080067
Steven Moreland66ac4012016-12-21 15:06:10 -080068 TokenManager *tokenManager = new TokenManager();
69
Martijn Coenen1a5da1c2017-03-06 13:05:39 +010070 if (!manager->add(serviceName, tokenManager)) {
71 ALOGE("Failed to register ITokenManager with hwservicemanager.");
72 }
Steven Moreland76237812016-11-08 15:59:04 -080073
Steven Morelandd83d1102016-10-25 15:01:47 -070074 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 Moreland50bf1822017-03-27 08:33:16 -070080 ALOGE("Failed to aquire binder FD. Aborting...");
Steven Morelandd83d1102016-10-25 15:01:47 -070081 return -1;
82 }
Martijn Coenened0cc072017-03-27 12:57:31 -070083 // Flush after setupPolling(), to make sure the binder driver
84 // knows about this thread handling commands.
85 IPCThreadState::self()->flushCommands();
Steven Morelandd83d1102016-10-25 15:01:47 -070086
87 sp<BinderCallback> cb(new BinderCallback);
88 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
Steven Moreland50bf1822017-03-27 08:33:16 -070089 nullptr) != 1) {
90 ALOGE("Failed to add hwbinder FD to Looper. Aborting...");
91 return -1;
Steven Morelandd83d1102016-10-25 15:01:47 -070092 }
93
94 // Tell IPCThreadState we're the service manager
Yifan Hong61ff9092017-01-10 10:19:18 -080095 sp<BnHwServiceManager> service = new BnHwServiceManager(manager);
Steven Morelandd83d1102016-10-25 15:01:47 -070096 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 Moreland50bf1822017-03-27 08:33:16 -0700102 ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\
103 "HAL services will not start!\n", rc);
Steven Morelandd83d1102016-10-25 15:01:47 -0700104 }
105
106 while (true) {
107 looper->pollAll(-1 /* timeoutMillis */);
108 }
109
110 return 0;
Martijn Coenenfcfcdfb2016-12-12 13:46:44 +0100111}