memtrack: Add android.hardware.memtrack@1.0-impl

Bug: 31180823
Change-Id: I5afa790a94eebf8f1755eb51ff8cfeb28b836543
Signed-off-by: Ruchi Kandoi<kandoiruchi@google.com>
diff --git a/Android.bp b/Android.bp
index 143f0f8..71c9ea1 100644
--- a/Android.bp
+++ b/Android.bp
@@ -4,6 +4,7 @@
     "audio/effect/2.0",
     "benchmarks/msgq/1.0",
     "memtrack/1.0",
+    "memtrack/1.0/default",
     "nfc/1.0",
     "nfc/1.0/default",
     "tests/bar/1.0",
diff --git a/memtrack/1.0/default/Android.bp b/memtrack/1.0/default/Android.bp
new file mode 100644
index 0000000..a235729
--- /dev/null
+++ b/memtrack/1.0/default/Android.bp
@@ -0,0 +1,30 @@
+// 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_library_shared {
+    name: "android.hardware.memtrack@1.0-impl",
+    relative_install_path: "hw",
+    srcs: ["Memtrack.cpp"],
+
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libhidl",
+        "libhardware",
+        "libhwbinder",
+        "libutils",
+        "android.hardware.memtrack@1.0",
+    ],
+
+}
diff --git a/memtrack/1.0/default/Memtrack.cpp b/memtrack/1.0/default/Memtrack.cpp
new file mode 100644
index 0000000..b953e7c
--- /dev/null
+++ b/memtrack/1.0/default/Memtrack.cpp
@@ -0,0 +1,98 @@
+/*
+ * 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 "android.hardware.memtrack@1.0-impl"
+#include <hardware/hardware.h>
+#include <hardware/memtrack.h>
+
+#include "Memtrack.h"
+namespace android {
+namespace hardware {
+namespace memtrack {
+namespace V1_0 {
+namespace implementation {
+
+Memtrack::Memtrack(memtrack_module_t *module) : mModule(module) {
+    if (mModule)
+        mModule->init(mModule);
+}
+
+Memtrack::~Memtrack() {
+    delete(mModule);
+}
+
+Return<void> Memtrack::getMemory(int32_t pid, MemtrackType type,
+        getMemory_cb _hidl_cb)  {
+    hidl_vec<MemtrackRecord> records;
+    size_t temp = 0;
+    size_t *size = &temp;
+    int ret = 0;
+
+    if (mModule->getMemory == nullptr)
+    {
+        _hidl_cb(MemtrackStatus::SUCCESS, records);
+        return Void();
+    }
+    ret = mModule->getMemory(mModule, pid, static_cast<memtrack_type>(type),
+            NULL, size);
+    if (ret == 0)
+    {
+        memtrack_record *legacy_records = new memtrack_record[*size];
+        ret = mModule->getMemory(mModule, pid,
+                static_cast<memtrack_type>(type), legacy_records, size);
+        if (ret == 0)
+        {
+            records.resize(*size);
+            for(size_t i = 0; i < *size; i++)
+            {
+                records[i].sizeInBytes = legacy_records[i].size_in_bytes;
+                records[i].flags = legacy_records[i].flags;
+            }
+        }
+        delete[] legacy_records;
+    }
+    _hidl_cb(MemtrackStatus::SUCCESS, records);
+    return Void();
+}
+
+
+IMemtrack* HIDL_FETCH_IMemtrack(const char* name) {
+    int ret = 0;
+    const hw_module_t* hw_module = NULL;
+    memtrack_module_t *memtrack_module = NULL;
+
+    ret = hw_get_module(name, &hw_module);
+    if (ret == 0 && hw_module->methods->open > 0)
+    {
+        ret = hw_module->methods->open(hw_module, name,
+                reinterpret_cast<hw_device_t**>(&memtrack_module));
+        if (ret == 0)
+                return new Memtrack(memtrack_module);
+        else {
+            ALOGE("Passthrough failed to load legacy HAL.");
+        }
+    }
+    else {
+        ALOGE ("hw_get_module %s failed: %d", name, ret);
+    }
+    return nullptr;
+}
+
+} // namespace implementation
+}  // namespace V1_0
+}  // namespace memtrack
+}  // namespace hardware
+}  // namespace android
diff --git a/memtrack/1.0/default/Memtrack.h b/memtrack/1.0/default/Memtrack.h
new file mode 100644
index 0000000..86dae72
--- /dev/null
+++ b/memtrack/1.0/default/Memtrack.h
@@ -0,0 +1,57 @@
+/*
+ * 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 HIDL_GENERATED_android_hardware_memtrack_V1_0_Memtrack_H_
+#define HIDL_GENERATED_android_hardware_memtrack_V1_0_Memtrack_H_
+
+#include <android/hardware/memtrack/1.0/IMemtrack.h>
+#include <hidl/Status.h>
+
+#include <hidl/MQDescriptor.h>
+namespace android {
+namespace hardware {
+namespace memtrack {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hardware::memtrack::V1_0::IMemtrack;
+using ::android::hardware::memtrack::V1_0::MemtrackRecord;
+using ::android::hardware::memtrack::V1_0::MemtrackStatus;
+using ::android::hardware::memtrack::V1_0::MemtrackType;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::hidl_string;
+using ::android::sp;
+
+struct Memtrack : public IMemtrack {
+    Memtrack(memtrack_module_t* module);
+    ~Memtrack();
+    Return<void> getMemory(int32_t pid, MemtrackType type, getMemory_cb _hidl_cb)  override;
+
+  private:
+    memtrack_module_t* mModule;
+};
+
+extern "C" IMemtrack* HIDL_FETCH_IMemtrack(const char* name);
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace memtrack
+}  // namespace hardware
+}  // namespace android
+
+#endif  // HIDL_GENERATED_android_hardware_memtrack_V1_0_Memtrack_H_