blob: 9a19554b842d6cd49d6971ed026fd190f5d4855d [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
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#define LOG_TAG "HwServiceManager"
18
19#include <hidl/IServiceManager.h>
20#include <hidl/Static.h>
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020021#include <hidl/Status.h>
Martijn Coenen72110162016-08-19 14:28:25 +020022
23#include <utils/Log.h>
24#include <hwbinder/IPCThreadState.h>
25#include <hwbinder/Parcel.h>
Martijn Coenen72110162016-08-19 14:28:25 +020026#include <utils/String8.h>
27#include <utils/SystemClock.h>
28
29#include <unistd.h>
30
31
32namespace android {
33namespace hardware {
34sp<IServiceManager> defaultServiceManager()
35{
36 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
Iliyan Malchev770d91c2016-09-12 16:19:56 -070037 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
Martijn Coenen277f0612016-09-06 16:40:20 +020038 // HwBinder not available on this device or not accessible to
39 // this process.
40 return nullptr;
41 }
Martijn Coenen72110162016-08-19 14:28:25 +020042 {
43 AutoMutex _l(gDefaultServiceManagerLock);
44 while (gDefaultServiceManager == NULL) {
Martijn Coenenc28f1152016-08-22 14:06:56 +020045 gDefaultServiceManager = interface_cast<IHwServiceManager>(
Martijn Coenen72110162016-08-19 14:28:25 +020046 ProcessState::self()->getContextObject(NULL));
47 if (gDefaultServiceManager == NULL)
48 sleep(1);
49 }
50 }
51
52 return gDefaultServiceManager;
53}
54
55// ----------------------------------------------------------------------
56
Martijn Coenenc28f1152016-08-22 14:06:56 +020057class BpServiceManager : public BpInterface<IHwServiceManager>
Martijn Coenen72110162016-08-19 14:28:25 +020058{
59public:
60 explicit BpServiceManager(const sp<IBinder>& impl)
Martijn Coenenc28f1152016-08-22 14:06:56 +020061 : BpInterface<IHwServiceManager>(impl)
Martijn Coenen72110162016-08-19 14:28:25 +020062 {
63 }
64
65 virtual sp<IBinder> getService(const String16& name, const hidl_version& version) const
66 {
67 unsigned n;
68 for (n = 0; n < 5; n++){
69 sp<IBinder> svc = checkService(name, version);
70 if (svc != NULL) return svc;
71 ALOGI("Waiting for service %s...\n", String8(name).string());
72 sleep(1);
73 }
74 return NULL;
75 }
76
77 virtual sp<IBinder> checkService( const String16& name, const hidl_version& version) const
78 {
79 Parcel data, reply;
Martijn Coenenc28f1152016-08-22 14:06:56 +020080 data.writeInterfaceToken(getInterfaceDescriptor());
Martijn Coenen72110162016-08-19 14:28:25 +020081 data.writeString16(name);
82 version.writeToParcel(data);
83 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
84 return reply.readStrongBinder();
85 }
86
87 virtual status_t addService(const String16& name,
88 const sp<IBinder>& service, const hidl_version& version,
89 bool allowIsolated)
90 {
91 Parcel data, reply;
Martijn Coenenc28f1152016-08-22 14:06:56 +020092 data.writeInterfaceToken(getInterfaceDescriptor());
Martijn Coenen72110162016-08-19 14:28:25 +020093 data.writeString16(name);
94 data.writeStrongBinder(service);
95 version.writeToParcel(data);
96 data.writeInt32(allowIsolated ? 1 : 0);
97 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Martijn Coenenbb5e9bb2016-09-01 01:36:18 +020098 if (err == NO_ERROR) {
99 Status status;
100 status.readFromParcel(reply);
101 return status.exceptionCode();
102 } else {
103 return err;
104 }
Martijn Coenen72110162016-08-19 14:28:25 +0200105 }
106
107 virtual Vector<String16> listServices()
108 {
109 Vector<String16> res;
110 int n = 0;
111
112 for (;;) {
113 Parcel data, reply;
Martijn Coenenc28f1152016-08-22 14:06:56 +0200114 data.writeInterfaceToken(getInterfaceDescriptor());
Martijn Coenen72110162016-08-19 14:28:25 +0200115 data.writeInt32(n++);
116 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
117 if (err != NO_ERROR)
118 break;
119 res.add(reply.readString16());
120 }
121 return res;
122 }
123};
124
125IMPLEMENT_HWBINDER_META_INTERFACE(ServiceManager, "android.hardware.IServiceManager");
126
127}; // namespace hardware
128}; // namespace android