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
diff --git a/HidlSupport.cpp b/HidlSupport.cpp
new file mode 100644
index 0000000..71ac2b3
--- /dev/null
+++ b/HidlSupport.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hidl/HidlSupport.h>
+
+namespace android {
+namespace hardware {
+
+static const char *const kEmptyString = "";
+
+hidl_string::hidl_string()
+ : mBuffer(const_cast<char *>(kEmptyString)),
+ mSize(0),
+ mOwnsBuffer(true) {
+}
+
+hidl_string::~hidl_string() {
+ clear();
+}
+
+hidl_string::hidl_string(const hidl_string &other)
+ : mBuffer(const_cast<char *>(kEmptyString)),
+ mSize(0),
+ mOwnsBuffer(true) {
+ setTo(other.c_str(), other.size());
+}
+
+hidl_string &hidl_string::operator=(const hidl_string &other) {
+ if (this != &other) {
+ setTo(other.c_str(), other.size());
+ }
+
+ return *this;
+}
+
+hidl_string &hidl_string::operator=(const char *s) {
+ return setTo(s, strlen(s));
+}
+
+hidl_string &hidl_string::setTo(const char *data, size_t size) {
+ clear();
+
+ mBuffer = (char *)malloc(size + 1);
+ memcpy(mBuffer, data, size);
+ mBuffer[size] = '\0';
+
+ mSize = size;
+ mOwnsBuffer = true;
+
+ return *this;
+}
+
+void hidl_string::clear() {
+ if (mOwnsBuffer && (mBuffer != kEmptyString)) {
+ free(mBuffer);
+ }
+
+ mBuffer = const_cast<char *>(kEmptyString);
+ mSize = 0;
+ mOwnsBuffer = true;
+}
+
+void hidl_string::setToExternal(const char *data, size_t size) {
+ clear();
+
+ mBuffer = const_cast<char *>(data);
+ mSize = size;
+ mOwnsBuffer = false;
+}
+
+const char *hidl_string::c_str() const {
+ return mBuffer ? mBuffer : "";
+}
+
+size_t hidl_string::size() const {
+ return mSize;
+}
+
+bool hidl_string::empty() const {
+ return mSize == 0;
+}
+
+status_t hidl_string::readEmbeddedFromParcel(
+ const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
+ const void *ptr = parcel.readEmbeddedBuffer(
+ nullptr /* buffer_handle */,
+ parentHandle,
+ parentOffset + offsetof(hidl_string, mBuffer));
+
+ return ptr != NULL ? OK : UNKNOWN_ERROR;
+}
+
+status_t hidl_string::writeEmbeddedToParcel(
+ Parcel *parcel, size_t parentHandle, size_t parentOffset) const {
+ return parcel->writeEmbeddedBuffer(
+ mBuffer,
+ mSize + 1,
+ nullptr /* handle */,
+ parentHandle,
+ parentOffset + offsetof(hidl_string, mBuffer));
+}
+
+} // namespace hardware
+} // namespace android
+
+