(reland) Provide defaults for android.hardware.sensors@2.1-impl.virtual

This will allow to reuse this library with replaced transport.
Currently we need it to reuse this library in Cuttlefish with
vconsole transport.

reland: fixed the typo in the qemud channel name: "qemud" -> "sensors".

Bug: 299105998
Test: check the emulator boots
Change-Id: Icd5369f1e74d39287583ebbb944846461dd7b68a
Signed-off-by: Dmitrii Merkurev <dimorinny@google.com>
Signed-off-by: Roman Kiryanov <rkir@google.com>
Merged-In: Icd5369f1e74d39287583ebbb944846461dd7b68a
diff --git a/sensors/Android.bp b/sensors/Android.bp
index ceb7bc1..e971f8e 100644
--- a/sensors/Android.bp
+++ b/sensors/Android.bp
@@ -23,18 +23,29 @@
     default_applicable_licenses: ["device_generic_goldfish_license"],
 }
 
-cc_library_shared {
-    name: "android.hardware.sensors@2.1-impl.ranchu",
-    vendor: true,
-    relative_install_path: "hw",
-    defaults: ["hidl_defaults"],
+filegroup {
+    name: "android.hardware.sensors@2.1-impl.virtual_srcs",
     srcs: [
-        "multihal_sensors_transport.cpp",
         "multihal_sensors.cpp",
         "multihal_sensors_epoll.cpp",
         "multihal_sensors_qemu.cpp",
         "sensor_list.cpp",
-        "entry.cpp"
+    ]
+}
+
+cc_library_headers {
+    name: "android.hardware.sensors@2.1-impl.virtual_headers",
+    vendor_available: true,
+    export_include_dirs: ["include"],
+}
+
+cc_defaults {
+    name: "android.hardware.sensors@2.1-impl.virtual.defaults",
+    vendor: true,
+    relative_install_path: "hw",
+    defaults: ["hidl_defaults"],
+    srcs: [
+        ":android.hardware.sensors@2.1-impl.virtual_srcs",
     ],
     shared_libs: [
         "android.hardware.sensors@2.0",
@@ -45,8 +56,19 @@
         "liblog",
         "libutils",
     ],
+    header_libs: [
+        "android.hardware.sensors@2.X-multihal.header",
+        "android.hardware.sensors@2.1-impl.virtual_headers"
+    ],
+}
+
+cc_library_shared {
+    name: "android.hardware.sensors@2.1-impl.ranchu",
+    defaults: ["android.hardware.sensors@2.1-impl.virtual.defaults"],
+    srcs: [
+        "entry.cpp"
+    ],
     static_libs: ["libqemud.ranchu"],
-    header_libs: ["android.hardware.sensors@2.X-multihal.header"],
     cflags: [
         "-DLOG_TAG=\"android.hardware.sensors@2.1-impl.ranchu\"",
         "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION",
diff --git a/sensors/entry.cpp b/sensors/entry.cpp
index d48c458..0730574 100644
--- a/sensors/entry.cpp
+++ b/sensors/entry.cpp
@@ -14,14 +14,48 @@
  * limitations under the License.
  */
 
-#include <log/log.h>
-#include "multihal_sensors.h"
+#include <android-base/unique_fd.h>
+#include <qemud.h>
+#include <multihal_sensors.h>
 
+using ::android::base::unique_fd;
 using ::android::hardware::sensors::V2_1::implementation::ISensorsSubHal;
 
 namespace {
-goldfish::MultihalSensors impl;
-}
+
+class QemudSensorsTransport : public goldfish::SensorsTransport {
+ public:
+    QemudSensorsTransport(const char* name)
+        : m_qemuSensorsFd(qemud_channel_open(name)) {}
+
+    int Send(const void* msg, int size) override {
+        return qemud_channel_send(m_qemuSensorsFd.get(), msg, size);
+    }
+
+    int Receive(void* msg, int maxsize) override {
+        return qemud_channel_recv(m_qemuSensorsFd.get(), msg, maxsize);
+    }
+
+    bool Ok() const override {
+        return m_qemuSensorsFd.ok();
+    }
+
+    int Fd() const override {
+        return m_qemuSensorsFd.get();
+    }
+
+    const char* Name() const override {
+        return "qemud_channel";
+    }
+
+ private:
+    const unique_fd m_qemuSensorsFd;
+};
+
+goldfish::MultihalSensors impl(
+    std::move(std::make_unique<QemudSensorsTransport>("sensors")));
+
+} // namespace
 
 extern "C" ISensorsSubHal* sensorsHalGetSubHal_2_1(uint32_t* version) {
     *version = SUB_HAL_2_1_VERSION;
diff --git a/sensors/multihal_sensors.h b/sensors/include/multihal_sensors.h
similarity index 98%
rename from sensors/multihal_sensors.h
rename to sensors/include/multihal_sensors.h
index 0d92b79..bb9ed4e 100644
--- a/sensors/multihal_sensors.h
+++ b/sensors/include/multihal_sensors.h
@@ -49,7 +49,7 @@
 using ::android::sp;
 
 struct MultihalSensors : public ahs21::implementation::ISensorsSubHal {
-    MultihalSensors();
+    MultihalSensors(std::unique_ptr<SensorsTransport> transport);
     ~MultihalSensors();
 
     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
diff --git a/sensors/multihal_sensors_transport.h b/sensors/include/multihal_sensors_transport.h
similarity index 68%
rename from sensors/multihal_sensors_transport.h
rename to sensors/include/multihal_sensors_transport.h
index df09d1a..d5de150 100644
--- a/sensors/multihal_sensors_transport.h
+++ b/sensors/include/multihal_sensors_transport.h
@@ -16,13 +16,8 @@
 
 #pragma once
 
-#include <android-base/unique_fd.h>
-#include <qemud.h>
-
 namespace goldfish {
 
-using ::android::base::unique_fd;
-
 class SensorsTransport {
  public:
     virtual int Send(const void* msg, int size) = 0;
@@ -34,18 +29,4 @@
     virtual ~SensorsTransport() = default;
 };
 
-class QemudSensorsTransport : public SensorsTransport {
- public:
-    QemudSensorsTransport(const char* name);
-
-    int Send(const void* msg, int size) override;
-    int Receive(void* msg, int maxsize) override;
-    bool Ok() const override;
-    int Fd() const override;
-    const char* Name() const override;
-
- private:
-    const unique_fd m_qemuSensorsFd;
-};
-
 }  // namespace goldfish
diff --git a/sensors/multihal_sensors.cpp b/sensors/multihal_sensors.cpp
index 2afbec0..1ce574a 100644
--- a/sensors/multihal_sensors.cpp
+++ b/sensors/multihal_sensors.cpp
@@ -16,9 +16,8 @@
 
 #include <cinttypes>
 #include <log/log.h>
-#include <qemud.h>
 #include <utils/SystemClock.h>
-#include "multihal_sensors.h"
+#include <multihal_sensors.h>
 #include "sensor_list.h"
 
 namespace goldfish {
@@ -33,8 +32,8 @@
 constexpr int64_t kMaxSamplingPeriodNs = 1000000000;
 }
 
-MultihalSensors::MultihalSensors()
-        : m_sensorsTransport(std::make_unique<QemudSensorsTransport>("sensors"))
+MultihalSensors::MultihalSensors(std::unique_ptr<SensorsTransport> transport)
+        : m_sensorsTransport(std::move(transport))
         , m_batchInfo(getSensorNumber()) {
     if (!m_sensorsTransport->Ok()) {
         ALOGE("%s:%d: sensors transport is not opened", __func__, __LINE__);
diff --git a/sensors/multihal_sensors_qemu.cpp b/sensors/multihal_sensors_qemu.cpp
index d1d7252..f9efd90 100644
--- a/sensors/multihal_sensors_qemu.cpp
+++ b/sensors/multihal_sensors_qemu.cpp
@@ -17,9 +17,8 @@
 #include <log/log.h>
 #include <utils/SystemClock.h>
 #include <math.h>
-#include <qemud.h>
 #include <random>
-#include "multihal_sensors.h"
+#include <multihal_sensors.h>
 #include "sensor_list.h"
 
 namespace goldfish {
diff --git a/sensors/multihal_sensors_transport.cpp b/sensors/multihal_sensors_transport.cpp
deleted file mode 100644
index 3ff8459..0000000
--- a/sensors/multihal_sensors_transport.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2023 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 "multihal_sensors_transport.h"
-
-namespace goldfish {
-
-QemudSensorsTransport::QemudSensorsTransport(const char* name)
-    : m_qemuSensorsFd(qemud_channel_open(name)) {}
-
-int QemudSensorsTransport::Send(const void* msg, int size) {
-    return qemud_channel_send(m_qemuSensorsFd.get(), msg, size);
-}
-
-int QemudSensorsTransport::Receive(void* msg, int maxsize) {
-    return qemud_channel_recv(m_qemuSensorsFd.get(), msg, maxsize);
-}
-
-bool QemudSensorsTransport::Ok() const {
-    return m_qemuSensorsFd.ok();
-}
-
-int QemudSensorsTransport::Fd() const {
-    return m_qemuSensorsFd.get();
-}
-
-const char* QemudSensorsTransport::Name() const {
-    return "qemud_channel";
-}
-
-}  // namespace goldfish