Fix failure to load memtrack HAL implementation
Since some implementations of the libhardware memtrack HAL do not
implement an open() method, the HIDL implementation should not rely
on that method being available.
Test: Memtrack VTS tests pass
Bug: 31180823
Bug: 34103653
Change-Id: Ia0dda5e027894009bdcf12cd5c2e9eb635aca87e
Signed-off-by: Connor O'Brien <connoro@google.com>
diff --git a/memtrack/1.0/default/Memtrack.cpp b/memtrack/1.0/default/Memtrack.cpp
index fa09c25..cc2d341 100644
--- a/memtrack/1.0/default/Memtrack.cpp
+++ b/memtrack/1.0/default/Memtrack.cpp
@@ -29,7 +29,7 @@
namespace V1_0 {
namespace implementation {
-Memtrack::Memtrack(memtrack_module_t *module) : mModule(module) {
+Memtrack::Memtrack(const memtrack_module_t *module) : mModule(module) {
if (mModule)
mModule->init(mModule);
}
@@ -74,25 +74,25 @@
IMemtrack* HIDL_FETCH_IMemtrack(const char* name) {
- int ret = 0;
- const hw_module_t* hw_module = NULL;
- memtrack_module_t *memtrack_module = NULL;
+ const hw_module_t* hw_module = nullptr;
+ const memtrack_module_t* memtrack_module = nullptr;
+ int err = hw_get_module(name, &hw_module);
+ if (err) {
+ ALOGE ("hw_get_module %s failed: %d", name, err);
+ return nullptr;
+ }
- ret = hw_get_module(name, &hw_module);
- if (ret == 0 && hw_module->methods->open)
- {
- ret = hw_module->methods->open(hw_module, name,
- reinterpret_cast<hw_device_t**>(&memtrack_module));
- if (ret == 0)
- return new Memtrack(memtrack_module);
- else {
+ if (!hw_module->methods || !hw_module->methods->open) {
+ memtrack_module = reinterpret_cast<const memtrack_module_t*>(hw_module);
+ } else {
+ err = hw_module->methods->open(hw_module, name,
+ reinterpret_cast<hw_device_t**>(const_cast<memtrack_module_t**>(&memtrack_module)));
+ if (err) {
ALOGE("Passthrough failed to load legacy HAL.");
+ return nullptr;
}
}
- else {
- ALOGE ("hw_get_module %s failed: %d", name, ret);
- }
- return nullptr;
+ return new Memtrack(memtrack_module);
}
} // namespace implementation
diff --git a/memtrack/1.0/default/Memtrack.h b/memtrack/1.0/default/Memtrack.h
index a3c55e4..0adba76 100644
--- a/memtrack/1.0/default/Memtrack.h
+++ b/memtrack/1.0/default/Memtrack.h
@@ -38,12 +38,12 @@
using ::android::sp;
struct Memtrack : public IMemtrack {
- Memtrack(memtrack_module_t* module);
+ Memtrack(const memtrack_module_t* module);
~Memtrack();
Return<void> getMemory(int32_t pid, MemtrackType type, getMemory_cb _hidl_cb) override;
private:
- memtrack_module_t* mModule;
+ const memtrack_module_t* mModule;
};
extern "C" IMemtrack* HIDL_FETCH_IMemtrack(const char* name);