Skip to content

Commit 7211016

Browse files
author
Martijn Coenen
committed
Move HidlSupport and IServiceManager from libhwbinder.
libhidl will contain data types and functionality that is independent from libhwbinder, such as hidl_vec, hidl_string, etc. libhidl also contains an implementation of the service manager interface. Initially that implementation is exactly the same as the servicemanager we had in libhwbinder, but eventually it should be capable of passing out pass-through or remote HAL interfaces as well. Therefore, the servicemanager belongs more in libhidl than in libhwbinder. This initial version of the library still links against libhwbinder because of the following dependencies: - hidl_vec/hidl_string have methods for (de)serialization to/from a Parcel - IServiceManager requires instantiation of a proxy for the hwbinder ServiceManager. These can be dealt with in the future, if we deem it necessary, since they don't leak through to clients; clients can link against libhidl only when we have the other changes to the class hierarchy in place. Bug: 30839546 Change-Id: Ib05de8c98ba8a807618a0b2e37d809a29d2de9ca
1 parent 12cb797 commit 7211016

File tree

7 files changed

+637
-0
lines changed

7 files changed

+637
-0
lines changed

Android.mk

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) 2016 The Android Open Source Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
LOCAL_PATH:= $(call my-dir)
15+
16+
include $(CLEAR_VARS)
17+
LOCAL_MODULE := libhidl
18+
LOCAL_SHARED_LIBRARIES := libbase liblog libutils libhwbinder
19+
LOCAL_EXPORT_SHARED_LIBRARY_HEADERS := libbase libutils
20+
21+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
22+
23+
LOCAL_CLANG := true
24+
LOCAL_SANITIZE := integer
25+
LOCAL_SRC_FILES := \
26+
HidlSupport.cpp \
27+
IServiceManager.cpp \
28+
Static.cpp
29+
30+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
31+
LOCAL_MULTILIB := both
32+
LOCAL_COMPATIBILITY_SUITE := vts
33+
include $(BUILD_SHARED_LIBRARY)

HidlSupport.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 <hidl/HidlSupport.h>
18+
19+
namespace android {
20+
namespace hardware {
21+
22+
static const char *const kEmptyString = "";
23+
24+
hidl_string::hidl_string()
25+
: mBuffer(const_cast<char *>(kEmptyString)),
26+
mSize(0),
27+
mOwnsBuffer(true) {
28+
}
29+
30+
hidl_string::~hidl_string() {
31+
clear();
32+
}
33+
34+
hidl_string::hidl_string(const hidl_string &other)
35+
: mBuffer(const_cast<char *>(kEmptyString)),
36+
mSize(0),
37+
mOwnsBuffer(true) {
38+
setTo(other.c_str(), other.size());
39+
}
40+
41+
hidl_string &hidl_string::operator=(const hidl_string &other) {
42+
if (this != &other) {
43+
setTo(other.c_str(), other.size());
44+
}
45+
46+
return *this;
47+
}
48+
49+
hidl_string &hidl_string::operator=(const char *s) {
50+
return setTo(s, strlen(s));
51+
}
52+
53+
hidl_string &hidl_string::setTo(const char *data, size_t size) {
54+
clear();
55+
56+
mBuffer = (char *)malloc(size + 1);
57+
memcpy(mBuffer, data, size);
58+
mBuffer[size] = '\0';
59+
60+
mSize = size;
61+
mOwnsBuffer = true;
62+
63+
return *this;
64+
}
65+
66+
void hidl_string::clear() {
67+
if (mOwnsBuffer && (mBuffer != kEmptyString)) {
68+
free(mBuffer);
69+
}
70+
71+
mBuffer = const_cast<char *>(kEmptyString);
72+
mSize = 0;
73+
mOwnsBuffer = true;
74+
}
75+
76+
void hidl_string::setToExternal(const char *data, size_t size) {
77+
clear();
78+
79+
mBuffer = const_cast<char *>(data);
80+
mSize = size;
81+
mOwnsBuffer = false;
82+
}
83+
84+
const char *hidl_string::c_str() const {
85+
return mBuffer ? mBuffer : "";
86+
}
87+
88+
size_t hidl_string::size() const {
89+
return mSize;
90+
}
91+
92+
bool hidl_string::empty() const {
93+
return mSize == 0;
94+
}
95+
96+
status_t hidl_string::readEmbeddedFromParcel(
97+
const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
98+
const void *ptr = parcel.readEmbeddedBuffer(
99+
nullptr /* buffer_handle */,
100+
parentHandle,
101+
parentOffset + offsetof(hidl_string, mBuffer));
102+
103+
return ptr != NULL ? OK : UNKNOWN_ERROR;
104+
}
105+
106+
status_t hidl_string::writeEmbeddedToParcel(
107+
Parcel *parcel, size_t parentHandle, size_t parentOffset) const {
108+
return parcel->writeEmbeddedBuffer(
109+
mBuffer,
110+
mSize + 1,
111+
nullptr /* handle */,
112+
parentHandle,
113+
parentOffset + offsetof(hidl_string, mBuffer));
114+
}
115+
116+
} // namespace hardware
117+
} // namespace android
118+
119+

IServiceManager.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
#define LOG_TAG "HwServiceManager"
18+
19+
#include <hidl/IServiceManager.h>
20+
#include <hidl/Static.h>
21+
22+
#include <utils/Log.h>
23+
#include <hwbinder/IPCThreadState.h>
24+
#include <hwbinder/Parcel.h>
25+
#include <hwbinder/Static.h>
26+
#include <utils/String8.h>
27+
#include <utils/SystemClock.h>
28+
29+
#include <unistd.h>
30+
31+
32+
namespace android {
33+
namespace hardware {
34+
sp<IServiceManager> defaultServiceManager()
35+
{
36+
if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
37+
38+
{
39+
AutoMutex _l(gDefaultServiceManagerLock);
40+
while (gDefaultServiceManager == NULL) {
41+
gDefaultServiceManager = interface_cast<IServiceManager>(
42+
ProcessState::self()->getContextObject(NULL));
43+
if (gDefaultServiceManager == NULL)
44+
sleep(1);
45+
}
46+
}
47+
48+
return gDefaultServiceManager;
49+
}
50+
51+
// ----------------------------------------------------------------------
52+
53+
class BpServiceManager : public BpInterface<IServiceManager>
54+
{
55+
public:
56+
explicit BpServiceManager(const sp<IBinder>& impl)
57+
: BpInterface<IServiceManager>(impl)
58+
{
59+
}
60+
61+
virtual sp<IBinder> getService(const String16& name, const hidl_version& version) const
62+
{
63+
unsigned n;
64+
for (n = 0; n < 5; n++){
65+
sp<IBinder> svc = checkService(name, version);
66+
if (svc != NULL) return svc;
67+
ALOGI("Waiting for service %s...\n", String8(name).string());
68+
sleep(1);
69+
}
70+
return NULL;
71+
}
72+
73+
virtual sp<IBinder> checkService( const String16& name, const hidl_version& version) const
74+
{
75+
Parcel data, reply;
76+
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
77+
data.writeString16(name);
78+
version.writeToParcel(data);
79+
remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
80+
return reply.readStrongBinder();
81+
}
82+
83+
virtual status_t addService(const String16& name,
84+
const sp<IBinder>& service, const hidl_version& version,
85+
bool allowIsolated)
86+
{
87+
Parcel data, reply;
88+
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
89+
data.writeString16(name);
90+
data.writeStrongBinder(service);
91+
version.writeToParcel(data);
92+
data.writeInt32(allowIsolated ? 1 : 0);
93+
status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
94+
return err == NO_ERROR ? reply.readExceptionCode() : err;
95+
}
96+
97+
virtual Vector<String16> listServices()
98+
{
99+
Vector<String16> res;
100+
int n = 0;
101+
102+
for (;;) {
103+
Parcel data, reply;
104+
data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
105+
data.writeInt32(n++);
106+
status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
107+
if (err != NO_ERROR)
108+
break;
109+
res.add(reply.readString16());
110+
}
111+
return res;
112+
}
113+
};
114+
115+
IMPLEMENT_HWBINDER_META_INTERFACE(ServiceManager, "android.hardware.IServiceManager");
116+
117+
}; // namespace hardware
118+
}; // namespace android

Static.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// All static variables go here, to control initialization and
18+
// destruction order in the library.
19+
20+
#include <hidl/Static.h>
21+
22+
namespace android {
23+
namespace hardware {
24+
25+
Mutex gDefaultServiceManagerLock;
26+
sp<IServiceManager> gDefaultServiceManager;
27+
28+
} // namespace hardware
29+
} // namespace android

0 commit comments

Comments
 (0)