Remove dependency on libhidl-gen-utils.

Test: hidl_test + (sanity) hidl_test_java
Test: VtsHalVibratorV1_0TargetProfiling
Test: internal marlin boots up w/o errors
Test: (sanity) YouTube/Camera works
Bug: 37107636
Change-Id: I9b3a47d17ff3d594282bc3a28cd81463c3b1ac0f
diff --git a/base/Android.bp b/base/Android.bp
index 25b2a80..dd759d2 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -18,7 +18,6 @@
     shared_libs: [
         "libbase",
         "libcutils",
-        "libhidl-gen-utils",
         "liblog",
         "libutils",
     ],
diff --git a/base/HidlInternal.cpp b/base/HidlInternal.cpp
index 36ffae8..3bb27f8 100644
--- a/base/HidlInternal.cpp
+++ b/base/HidlInternal.cpp
@@ -24,7 +24,6 @@
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
 #include <dirent.h>
 #include <dlfcn.h>
-#include <hidl-util/FQName.h>
 #include <regex>
 #endif
 
@@ -111,14 +110,25 @@
                     const char *,
                     const char *,
                     std::vector<void *> *);
-            FQName package_name = FQName(mInstrumentationLibPackage);
+            std::string package = mInstrumentationLibPackage;
+            for (size_t i = 0; i < package.size(); i++) {
+                if (package[i] == '.') {
+                    package[i] = '_';
+                    continue;
+                }
+
+                if (package[i] == '@') {
+                    package[i] = '_';
+                    package.insert(i + 1, "V");
+                    continue;
+                }
+            }
             auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
-                        + package_name.tokenName() + "_"
-                        + mInterfaceName).c_str());
+                        + package + "_" + mInterfaceName).c_str());
             if ((error = dlerror()) != NULL) {
                 LOG(WARNING)
                     << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
-                    << mInterfaceName << ", error: " << error;
+                    << package << "_" << mInterfaceName << ", error: " << error;
                 continue;
             }
             instrumentationCallbacks->push_back(cb);
diff --git a/transport/Android.bp b/transport/Android.bp
index 835c6e1..6f0b6a9 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -33,7 +33,6 @@
         "libhidlbase",
         "libhwbinder",
         "libcutils",
-        "libhidl-gen-utils"
     ],
     export_shared_lib_headers: [
         "libbase",
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 30013d3..ff9884e 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -30,8 +30,6 @@
 
 #include <android-base/logging.h>
 #include <android-base/properties.h>
-#include <hidl-util/FQName.h>
-#include <hidl-util/StringHelper.h>
 #include <hwbinder/IPCThreadState.h>
 #include <hwbinder/Parcel.h>
 
@@ -97,6 +95,16 @@
     return details::gDefaultServiceManager;
 }
 
+bool endsWith(const std::string &in, const std::string &suffix) {
+    return in.size() >= suffix.size() &&
+           in.substr(in.size() - suffix.size()) == suffix;
+}
+
+bool startsWith(const std::string &in, const std::string &prefix) {
+    return in.size() >= prefix.size() &&
+           in.substr(0, prefix.size()) == prefix;
+}
+
 std::vector<std::string> search(const std::string &path,
                               const std::string &prefix,
                               const std::string &suffix) {
@@ -109,8 +117,8 @@
     while ((dp = readdir(dir.get())) != nullptr) {
         std::string name = dp->d_name;
 
-        if (StringHelper::StartsWith(name, prefix) &&
-                StringHelper::EndsWith(name, suffix)) {
+        if (startsWith(name, prefix) &&
+                endsWith(name, suffix)) {
             results.push_back(name);
         }
     }
@@ -148,18 +156,23 @@
 
 struct PassthroughServiceManager : IServiceManager {
     Return<sp<IBase>> get(const hidl_string& fqName,
-                     const hidl_string& name) override {
-        const FQName iface(fqName);
+                          const hidl_string& name) override {
+        std::string stdFqName(fqName.c_str());
 
-        if (!iface.isValid() ||
-            !iface.isFullyQualified() ||
-            iface.isIdentifier()) {
+        //fqName looks like android.hardware.foo@1.0::IFoo
+        size_t idx = stdFqName.find("::");
+
+        if (idx == std::string::npos ||
+                idx + strlen("::") + 1 >= stdFqName.size()) {
             LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
             return nullptr;
         }
 
-        const std::string prefix = iface.getPackageAndVersion().string() + "-impl";
-        const std::string sym = "HIDL_FETCH_" + iface.name();
+        std::string packageAndVersion = stdFqName.substr(0, idx);
+        std::string ifaceName = stdFqName.substr(idx + strlen("::"));
+
+        const std::string prefix = packageAndVersion + "-impl";
+        const std::string sym = "HIDL_FETCH_" + ifaceName;
 
         const int dlMode = RTLD_LAZY;
         void *handle = nullptr;