Merge "Support customize the path to lookup for hidl instrumenation libraries."
diff --git a/Android.bp b/Android.bp
index f9fbe10..9bc0b26 100644
--- a/Android.bp
+++ b/Android.bp
@@ -41,6 +41,7 @@
"IServiceManager.cpp",
"Static.cpp",
"Status.cpp",
+ "TaskRunner.cpp",
],
product_variables: {
diff --git a/TaskRunner.cpp b/TaskRunner.cpp
new file mode 100644
index 0000000..2f7d89c
--- /dev/null
+++ b/TaskRunner.cpp
@@ -0,0 +1,48 @@
+/*
+ * 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/TaskRunner.h>
+#include <thread>
+
+namespace android {
+namespace hardware {
+
+TaskRunner::TaskRunner() {
+ bool *running = mRunning = new bool();
+ SynchronizedQueue<std::function<void(void)>> *q
+ = mQueue = new SynchronizedQueue<std::function<void(void)>>();
+ mThread = new std::thread([running, q] {
+ *running = true;
+ while (*running) {
+ (q->wait_pop())();
+ }
+ delete q;
+ delete running;
+ });
+}
+TaskRunner::~TaskRunner() {
+ bool *running = mRunning;
+ std::thread *t = mThread;
+ mThread->detach();
+ mQueue->push([running, t] {
+ *running = false;
+ delete t;
+ });
+}
+
+} // namespace hardware
+} // namespace android
+
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index 1324af5..4fbe826 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -22,6 +22,7 @@
#include <dlfcn.h>
#include <cutils/properties.h>
#include <hidl/Status.h>
+#include <hwbinder/IBinder.h>
#include <hwbinder/Parcel.h>
#include <tuple>
#include <utils/Errors.h>
@@ -76,14 +77,6 @@
status_t writeEmbeddedToParcel(
Parcel *parcel, size_t parentHandle, size_t parentOffset) const;
- inline bool operator==(const char *s) const {
- return strcmp(mBuffer, s) == 0;
- }
-
- inline bool operator!=(const char *s) const {
- return !(operator==(s));
- }
-
// offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
static const size_t kOffsetOfBuffer;
@@ -99,6 +92,22 @@
void moveFrom(hidl_string &&);
};
+inline bool operator==(const hidl_string &hs, const char *s) {
+ return strcmp(hs.c_str(), s) == 0;
+}
+
+inline bool operator!=(const hidl_string &hs, const char *s) {
+ return !(hs == s);
+}
+
+inline bool operator==(const char *s, const hidl_string &hs) {
+ return strcmp(hs.c_str(), s) == 0;
+}
+
+inline bool operator!=(const char *s, const hidl_string &hs) {
+ return !(s == hs);
+}
+
////////////////////////////////////////////////////////////////////////////////
template<typename T>
@@ -113,8 +122,9 @@
*this = other;
}
- hidl_vec(hidl_vec<T> &&other) {
- *this = static_cast<hidl_vec &&>(other);
+ hidl_vec(hidl_vec<T> &&other)
+ : mOwnsBuffer(false) {
+ *this = std::move(other);
}
hidl_vec(const std::vector<T> &other) : hidl_vec() {
@@ -157,6 +167,9 @@
}
hidl_vec &operator=(hidl_vec &&other) {
+ if (mOwnsBuffer) {
+ delete[] mBuffer;
+ }
mBuffer = other.mBuffer;
mSize = other.mSize;
mOwnsBuffer = other.mOwnsBuffer;
@@ -572,6 +585,7 @@
struct IHidlInterfaceBase : virtual public RefBase {
virtual bool isRemote() const = 0;
+ virtual sp<::android::hardware::IBinder> toBinder() = 0;
// HIDL reserved methods follow.
virtual ::android::hardware::Return<void> interfaceChain(
std::function<void(const hidl_vec<hidl_string>&)> _hidl_cb) = 0;
@@ -579,6 +593,33 @@
static const ::android::String16 descriptor;
};
+template<typename IChild, typename IParent, typename BpChild>
+sp<IChild> castInterface(sp<IParent> parent, const char *childIndicator) {
+ if (parent.get() == nullptr) {
+ // casts always succeed with nullptrs.
+ return nullptr;
+ }
+ bool canCast = false;
+ parent->interfaceChain([&](const hidl_vec<hidl_string> &allowedCastTypes) {
+ for (size_t i = 0; i < allowedCastTypes.size(); i++) {
+ if (allowedCastTypes[i] == childIndicator) {
+ canCast = true;
+ break;
+ }
+ }
+ });
+
+ if (!canCast) {
+ return sp<IChild>(nullptr); // cast failed.
+ }
+ if (parent->isRemote()) {
+ // binderized mode. Got BpChild. grab the remote and wrap it.
+ return sp<IChild>(new BpChild(parent->toBinder()));
+ }
+ // Passthrough mode. Got BnChild and BsChild.
+ return sp<IChild>(static_cast<IChild *>(parent.get()));
+}
+
#if defined(__LP64__)
#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
@@ -660,6 +701,8 @@
SYNC_CALLBACK_EXIT,
ASYNC_CALLBACK_ENTRY,
ASYNC_CALLBACK_EXIT,
+ PASSTHROUGH_ENTRY,
+ PASSTHROUGH_EXIT,
};
// Signature of the instrumentation callback function.
diff --git a/include/hidl/Status.h b/include/hidl/Status.h
index 59e4d65..6dde65c 100644
--- a/include/hidl/Status.h
+++ b/include/hidl/Status.h
@@ -153,7 +153,7 @@
public:
Return(T v) : val{v} {}
Return(Status s) : status(s) {}
- operator T() { return val; }
+ operator T() const { return val; }
const Status& getStatus() const {
return status;
}
diff --git a/include/hidl/SynchronizedQueue.h b/include/hidl/SynchronizedQueue.h
index 4095c56..80123ba 100644
--- a/include/hidl/SynchronizedQueue.h
+++ b/include/hidl/SynchronizedQueue.h
@@ -1,8 +1,29 @@
+/*
+ * 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.
+ */
+
+#ifndef ANDROID_SYNCHRONIZED_QUEUE_H
+#define ANDROID_SYNCHRONIZED_QUEUE_H
+
#include <condition_variable>
#include <mutex>
#include <queue>
#include <thread>
+namespace android {
+namespace hardware {
/* Threadsafe queue.
*/
template <typename T>
@@ -16,16 +37,22 @@
/* Puts an item onto the end of the queue.
*/
- void push(const T& item);
+ bool push(const T& item);
/* Gets the size of the array.
*/
size_t size();
+ /* Sets the limit to the queue. Will fail
+ * the push operation if the limit is reached.
+ */
+ void setLimit(size_t limit);
+
private:
std::condition_variable mCondition;
std::mutex mMutex;
std::queue<T> mQueue;
+ size_t mQueueLimit = SIZE_MAX;
};
template <typename T>
@@ -43,13 +70,20 @@
}
template <typename T>
-void SynchronizedQueue<T>::push(const T &item) {
+bool SynchronizedQueue<T>::push(const T &item) {
+ bool success;
{
std::unique_lock<std::mutex> lock(mMutex);
- mQueue.push(item);
+ if (mQueue.size() < mQueueLimit) {
+ mQueue.push(item);
+ success = true;
+ } else {
+ success = false;
+ }
}
mCondition.notify_one();
+ return success;
}
template <typename T>
@@ -57,4 +91,16 @@
std::unique_lock<std::mutex> lock(mMutex);
return mQueue.size();
-}
\ No newline at end of file
+}
+
+template <typename T>
+void SynchronizedQueue<T>::setLimit(size_t limit) {
+ std::unique_lock<std::mutex> lock(mMutex);
+
+ mQueueLimit = limit;
+}
+
+} // namespace hardware
+} // namespace android
+
+#endif
diff --git a/include/hidl/TaskRunner.h b/include/hidl/TaskRunner.h
new file mode 100644
index 0000000..bc9087f
--- /dev/null
+++ b/include/hidl/TaskRunner.h
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+#ifndef ANDROID_TASK_RUNNER_H
+#define ANDROID_TASK_RUNNER_H
+
+#include "SynchronizedQueue.h"
+#include <thread>
+
+namespace android {
+namespace hardware {
+
+/*
+ * A background infinite loop that runs the Tasks push()'ed.
+ * Just a simple single-threaded thread pool.
+ */
+class TaskRunner {
+public:
+
+ /* Kicks off the loop immediately. */
+ TaskRunner();
+
+ /*
+ * Detaches the background thread and return immediately.
+ * Tasks in the queue will continue to be done sequentially, and _after_
+ * all tasks are done, the background thread releases the resources
+ * (the queue, the std::thread object, etc.)
+ */
+ ~TaskRunner();
+
+ /*
+ * Add a task. Return true if successful, false if
+ * the queue's size exceeds limit.
+ */
+ inline bool push(const std::function<void(void)> &t) {
+ return this->mQueue->push(t);
+ }
+
+ /*
+ * Sets the queue limit. Fails the push operation once the limit is reached.
+ */
+ inline void setLimit(size_t limit) {
+ this->mQueue->setLimit(limit);
+ }
+private:
+
+ // resources managed by the background thread.
+ bool *mRunning;
+ SynchronizedQueue<std::function<void(void)>> *mQueue;
+ std::thread *mThread;
+};
+
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_TASK_RUNNER_H
diff --git a/test/Android.bp b/test/Android.bp
index b645f59..2508f65 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -1,5 +1,19 @@
+// 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.
+
cc_test {
- name: "libhidl-test",
+ name: "libhidl_test",
gtest: false,
srcs: ["main.cpp"],
diff --git a/test/main.cpp b/test/main.cpp
index c2dd773..f5b5f20 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -1,8 +1,25 @@
+/*
+ * 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.
+ */
+
#define LOG_TAG "LibHidlTest"
#include <android-base/logging.h>
#include <gtest/gtest.h>
#include <hidl/HidlSupport.h>
+#include <hidl/TaskRunner.h>
#include <vector>
#define EXPECT_ARRAYEQ(__a1__, __a2__, __size__) EXPECT_TRUE(isArrayEqual(__a1__, __a2__, __size__))
@@ -74,6 +91,33 @@
EXPECT_ARRAYEQ(v2, v, 3);
}
+TEST_F(LibHidlTest, TaskRunnerTest) {
+ using android::hardware::TaskRunner;
+ TaskRunner tr;
+ bool flag = false;
+ tr.push([&] {
+ usleep(1000);
+ flag = true;
+ });
+ usleep(500);
+ EXPECT_FALSE(flag);
+ usleep(1000);
+ EXPECT_TRUE(flag);
+}
+
+TEST_F(LibHidlTest, StringCmpTest) {
+ using android::hardware::hidl_string;
+ const char * s = "good";
+ hidl_string hs(s);
+ EXPECT_NE(hs.c_str(), s);
+
+ EXPECT_TRUE(hs == s); // operator ==
+ EXPECT_TRUE(s == hs);
+
+ EXPECT_FALSE(hs != s); // operator ==
+ EXPECT_FALSE(s != hs);
+}
+
template <typename T>
void great(android::hardware::hidl_vec<T>) {}