uwb: Default HAL implementation

Skeletal implementation of UWB HAL which will be used for
cuttlefish/reference implementation in the future.

Bug: 195992658
Test: Manual verification of HAL booting up and handling HAL
API calls.

Change-Id: I894b7aef893ff2ed4f287f72471326b5211245c3
diff --git a/uwb/aidl/default/Android.bp b/uwb/aidl/default/Android.bp
new file mode 100644
index 0000000..8c2b60e
--- /dev/null
+++ b/uwb/aidl/default/Android.bp
@@ -0,0 +1,35 @@
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "hardware_interfaces_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["hardware_interfaces_license"],
+}
+
+cc_binary {
+    name: "android.hardware.uwb-service",
+    relative_install_path: "hw",
+    init_rc: ["uwb-service.rc"],
+    vintf_fragments: ["uwb-service.xml"],
+    vendor: true,
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-g",
+    ],
+    shared_libs: [
+        "liblog",
+        "libbinder_ndk",
+    ],
+    static_libs: [
+        "libbase",
+        "libutils",
+        "android.hardware.uwb-V1-ndk",
+    ],
+    srcs: [
+        "service.cpp",
+        "uwb.cpp",
+        "uwb_chip.cpp",
+    ],
+}
diff --git a/uwb/aidl/default/OWNERS b/uwb/aidl/default/OWNERS
new file mode 100644
index 0000000..c4ad416
--- /dev/null
+++ b/uwb/aidl/default/OWNERS
@@ -0,0 +1,2 @@
+# Bug component: 1042770
+include platform/packages/modules/Uwb:/OWNERS
diff --git a/uwb/aidl/default/service.cpp b/uwb/aidl/default/service.cpp
new file mode 100644
index 0000000..007637f
--- /dev/null
+++ b/uwb/aidl/default/service.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2021, 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 <android-base/logging.h>
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
+#include <utils/StrongPointer.h>
+
+#include "uwb.h"
+
+using ::aidl::android::hardware::uwb::IUwb;
+using ::android::sp;
+using ::android::base::InitLogging;
+using ::android::base::StderrLogger;
+using ::android::hardware::uwb::impl::Uwb;
+
+int main(int /*argc*/, char* argv[]) {
+    InitLogging(argv, StderrLogger);
+    LOG(INFO) << "UWB HAL starting up";
+
+    ABinderProcess_setThreadPoolMaxThreadCount(0);
+    std::shared_ptr<IUwb> uwb = ndk::SharedRefBase::make<Uwb>();
+    const std::string instance = std::string() + IUwb::descriptor + "/default";
+    binder_status_t status = AServiceManager_addService(uwb->asBinder().get(), instance.c_str());
+    CHECK(status == STATUS_OK);
+
+    ABinderProcess_joinThreadPool();
+    return EXIT_FAILURE;  // should not reach
+}
diff --git a/uwb/aidl/default/uwb-service.rc b/uwb/aidl/default/uwb-service.rc
new file mode 100644
index 0000000..e2c3825
--- /dev/null
+++ b/uwb/aidl/default/uwb-service.rc
@@ -0,0 +1,3 @@
+service vendor.uwb_hal /vendor/bin/hw/android.hardware.uwb-service
+    class hal
+    user uwb
diff --git a/uwb/aidl/default/uwb-service.xml b/uwb/aidl/default/uwb-service.xml
new file mode 100644
index 0000000..013d9de
--- /dev/null
+++ b/uwb/aidl/default/uwb-service.xml
@@ -0,0 +1,10 @@
+<manifest version="1.0" type="device">
+    <hal format="aidl">
+        <name>android.hardware.uwb</name>
+        <version>1</version>
+        <interface>
+            <name>IUwb</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>
diff --git a/uwb/aidl/default/uwb.cpp b/uwb/aidl/default/uwb.cpp
new file mode 100644
index 0000000..1e2ef4e
--- /dev/null
+++ b/uwb/aidl/default/uwb.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2021, 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 <android-base/logging.h>
+
+#include "uwb.h"
+
+namespace {
+static constexpr char kDefaultChipName[] = "default";
+
+}  // namespace
+
+namespace android {
+namespace hardware {
+namespace uwb {
+namespace impl {
+using namespace ::aidl::android::hardware::uwb;
+
+// The default implementation of the HAL assumes 1 chip on the device.
+Uwb::Uwb() : chips_({{kDefaultChipName, ndk::SharedRefBase::make<UwbChip>(kDefaultChipName)}}) {}
+
+Uwb::~Uwb() {}
+
+::ndk::ScopedAStatus Uwb::getChips(std::vector<std::string>* names) {
+    for (const auto& chip : chips_) {
+        names->push_back(chip.first);
+    }
+    return ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus Uwb::getChip(const std::string& name, std::shared_ptr<IUwbChip>* chip) {
+    const auto chip_found = chips_.find(name);
+    if (chip_found == chips_.end()) {
+        LOG(ERROR) << "Unknown chip name" << name;
+        return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+    }
+    *chip = chip_found->second;
+    return ndk::ScopedAStatus::ok();
+}
+}  // namespace impl
+}  // namespace uwb
+}  // namespace hardware
+}  // namespace android
diff --git a/uwb/aidl/default/uwb.h b/uwb/aidl/default/uwb.h
new file mode 100644
index 0000000..ec51fd8
--- /dev/null
+++ b/uwb/aidl/default/uwb.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2021, 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_HARDWARE_UWB_UWB
+#define ANDROID_HARDWARE_UWB_UWB
+
+#include <map>
+#include <vector>
+
+#include <aidl/android/hardware/uwb/BnUwb.h>
+#include <aidl/android/hardware/uwb/IUwbChip.h>
+
+#include "uwb_chip.h"
+
+namespace android {
+namespace hardware {
+namespace uwb {
+namespace impl {
+using namespace ::aidl::android::hardware::uwb;
+// Default implementation mean't to be used on simulator targets.
+class Uwb : public BnUwb {
+  public:
+    Uwb();
+    virtual ~Uwb();
+
+    ::ndk::ScopedAStatus getChips(std::vector<std::string>* names) override;
+    ::ndk::ScopedAStatus getChip(const std::string& name, std::shared_ptr<IUwbChip>* chip) override;
+
+  private:
+    std::map<std::string, std::shared_ptr<UwbChip>> chips_;
+};
+}  // namespace impl
+}  // namespace uwb
+}  // namespace hardware
+}  // namespace android
+
+#endif  // ANDROID_HARDWARE_UWB_UWB
\ No newline at end of file
diff --git a/uwb/aidl/default/uwb_chip.cpp b/uwb/aidl/default/uwb_chip.cpp
new file mode 100644
index 0000000..727bcf1
--- /dev/null
+++ b/uwb/aidl/default/uwb_chip.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2021, 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 "uwb.h"
+
+namespace android {
+namespace hardware {
+namespace uwb {
+namespace impl {
+using namespace ::aidl::android::hardware::uwb;
+
+UwbChip::UwbChip(const std::string& name) : name_(name) {}
+UwbChip::~UwbChip() {}
+
+::ndk::ScopedAStatus UwbChip::getName(std::string* name) {
+    *name = name_;
+    return ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus UwbChip::open(
+        const std::shared_ptr<IUwbClientCallback>& /* clientCallback */) {
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+}
+
+::ndk::ScopedAStatus UwbChip::close() {
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+}
+
+::ndk::ScopedAStatus UwbChip::coreInit() {
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+}
+
+::ndk::ScopedAStatus UwbChip::getSupportedVendorUciVersion(int32_t* /* version */) {
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+}
+
+::ndk::ScopedAStatus UwbChip::sendUciMessage(const std::vector<uint8_t>& /* data */,
+                                             int32_t* /* bytes_written */) {
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+}
+}  // namespace impl
+}  // namespace uwb
+}  // namespace hardware
+}  // namespace android
diff --git a/uwb/aidl/default/uwb_chip.h b/uwb/aidl/default/uwb_chip.h
new file mode 100644
index 0000000..5d3f55c
--- /dev/null
+++ b/uwb/aidl/default/uwb_chip.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2021, 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_HARDWARE_UWB_UWBCHIP
+#define ANDROID_HARDWARE_UWB_UWBCHIP
+
+#include <vector>
+
+#include <aidl/android/hardware/uwb/BnUwbChip.h>
+#include <aidl/android/hardware/uwb/IUwbClientCallback.h>
+
+namespace android {
+namespace hardware {
+namespace uwb {
+namespace impl {
+using namespace ::aidl::android::hardware::uwb;
+// Default implementation mean't to be used on simulator targets.
+class UwbChip : public BnUwbChip {
+  public:
+    UwbChip(const std::string& name);
+    virtual ~UwbChip();
+
+    ::ndk::ScopedAStatus getName(std::string* name) override;
+    ::ndk::ScopedAStatus open(const std::shared_ptr<IUwbClientCallback>& clientCallback) override;
+    ::ndk::ScopedAStatus close() override;
+    ::ndk::ScopedAStatus coreInit() override;
+    ::ndk::ScopedAStatus getSupportedVendorUciVersion(int32_t* version) override;
+    ::ndk::ScopedAStatus sendUciMessage(const std::vector<uint8_t>& data,
+                                        int32_t* bytes_written) override;
+
+  private:
+    std::string name_;
+};
+}  // namespace impl
+}  // namespace uwb
+}  // namespace hardware
+}  // namespace android
+
+#endif  // ANDROID_HARDWARE_UWB_UWBCHIP