Weaver AIDL support added am: f395b2d6c6 am: ac6b68b8fd

Original change: https://android-review.googlesource.com/c/platform/hardware/nxp/weaver/+/2770202

Change-Id: I4dfdffe9202795ba3e47226c7fd3ccbe544e3cc9
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/Android.bp b/Android.bp
index 6f06246..bb9b998 100755
--- a/Android.bp
+++ b/Android.bp
@@ -67,3 +67,30 @@
         "-fexceptions",
     ],
 }
+
+cc_binary {
+    name: "android.hardware.weaver-service.nxp",
+    relative_install_path: "hw",
+    init_rc: ["aidl_impl/android.hardware.weaver-service.nxp.rc"],
+    vintf_fragments: ["aidl_impl/android.hardware.weaver-service.nxp.xml"],
+    vendor: true,
+    srcs: [
+        "aidl_impl/service.cpp",
+        "aidl_impl/Weaver.cpp",
+    ],
+
+    local_include_dirs: [
+        "libese_weaver/inc",
+    ],
+    cppflags: [
+        "-Wall",
+        "-fexceptions",
+    ],
+    shared_libs: [
+        "android.hardware.weaver-V2-ndk",
+        "libbase",
+        "libbinder_ndk",
+        "liblog",
+        "ese_weaver.nxp",
+    ],
+}
diff --git a/NOTICE b/NOTICE
index a0ac881..e14ff16 100644
--- a/NOTICE
+++ b/NOTICE
@@ -1,6 +1,6 @@
 Copyright (C) 2017 The Android Open Source Project
 
-icensed under the Apache License, Version 2.0 (the "License");
+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
 
diff --git a/aidl_impl/Weaver.cpp b/aidl_impl/Weaver.cpp
new file mode 100644
index 0000000..66f1313
--- /dev/null
+++ b/aidl_impl/Weaver.cpp
@@ -0,0 +1,148 @@
+/******************************************************************************
+ *
+ *  Copyright 2023 NXP
+ *
+ *  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 "Weaver.h"
+#include <log/log.h>
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace weaver {
+
+using ::ndk::ScopedAStatus;
+using std::vector;
+
+// Methods from ::android::hardware::weaver::IWeaver follow.
+
+Weaver::Weaver() {
+  ALOGD("Weaver Constructor");
+  pInterface = WeaverImpl::getInstance();
+  if (pInterface != NULL) {
+    pInterface->Init();
+  } else {
+    ALOGE("Failed to get Weaver Interface");
+  }
+}
+
+ScopedAStatus Weaver::getConfig(WeaverConfig *out_config) {
+  ALOGD("Weaver::getConfig");
+
+  if (out_config == NULL) {
+    ALOGE("Invalid param");
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Null pointer passed");
+  }
+  memset(out_config, 0, sizeof(WeaverConfig));
+
+  if (pInterface == NULL) {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Weaver interface not defined");
+  }
+  SlotInfo slotInfo;
+  Status_Weaver status = pInterface->GetSlots(slotInfo);
+  if (status == WEAVER_STATUS_OK) {
+    out_config->slots = slotInfo.slots;
+    out_config->keySize = slotInfo.keySize;
+    out_config->valueSize = slotInfo.valueSize;
+    ALOGD("Weaver Success for getSlots Slots :(%d)", out_config->slots);
+    return ScopedAStatus::ok();
+  } else {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Failed to retrieve slots info");
+  }
+}
+
+ScopedAStatus Weaver::read(int32_t in_slotId, const vector<uint8_t> &in_key,
+                           WeaverReadResponse *out_response) {
+
+  ALOGD("Weaver::read slot %d", in_slotId);
+  if (out_response == NULL) {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Null pointer passed");
+  }
+  if (in_key.empty()) {
+    out_response->status = WeaverReadStatus::FAILED;
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Empty key input passed");
+  }
+  if (pInterface == NULL) {
+    out_response->status = WeaverReadStatus::FAILED;
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Weaver interface not defined");
+  }
+  memset(out_response, 0, sizeof(WeaverReadResponse));
+
+  ReadRespInfo readInfo;
+  ScopedAStatus retStatus;
+  Status_Weaver status;
+
+  status = pInterface->Read(in_slotId, in_key, readInfo);
+  switch (status) {
+  case WEAVER_STATUS_OK:
+    ALOGD("Read slot %d OK", in_slotId);
+    out_response->value = readInfo.value;
+    out_response->status = WeaverReadStatus::OK;
+    retStatus = ScopedAStatus::ok();
+    break;
+  case WEAVER_STATUS_INCORRECT_KEY:
+    ALOGE("Read Incorrect Key");
+    out_response->value.resize(0);
+    out_response->timeout = readInfo.timeout;
+    out_response->status = WeaverReadStatus::INCORRECT_KEY;
+    retStatus = ScopedAStatus::ok();
+    break;
+  case WEAVER_STATUS_THROTTLE:
+    ALOGE("Read WEAVER_THROTTLE");
+    out_response->value.resize(0);
+    out_response->timeout = readInfo.timeout;
+    out_response->status = WeaverReadStatus::THROTTLE;
+    retStatus = ScopedAStatus::ok();
+    break;
+  default:
+    out_response->timeout = 0;
+    out_response->value.resize(0);
+    out_response->status = WeaverReadStatus::FAILED;
+    retStatus = ScopedAStatus::ok();
+    break;
+  }
+  return retStatus;
+}
+
+ScopedAStatus Weaver::write(int32_t in_slotId, const vector<uint8_t> &in_key,
+                            const vector<uint8_t> &in_value) {
+  ALOGD("Weaver::write slot %d", in_slotId);
+  if (in_key.empty() || in_value.empty()) {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Invalid parameters passed");
+  }
+  if (pInterface == NULL) {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(
+        STATUS_FAILED, "Weaver interface not defined");
+  }
+  if (pInterface->Write(in_slotId, in_key, in_value) == WEAVER_STATUS_OK) {
+    ALOGD("Write slot %d OK", in_slotId);
+    return ScopedAStatus::ok();
+  } else {
+    return ScopedAStatus::fromServiceSpecificErrorWithMessage(STATUS_FAILED,
+                                                              "Unknown error");
+  }
+}
+
+} // namespace weaver
+} // namespace hardware
+} // namespace android
+} // namespace aidl
diff --git a/aidl_impl/Weaver.h b/aidl_impl/Weaver.h
new file mode 100644
index 0000000..1eaa5cc
--- /dev/null
+++ b/aidl_impl/Weaver.h
@@ -0,0 +1,51 @@
+/******************************************************************************
+ *
+ *  Copyright 2023 NXP
+ *
+ *  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.
+ *
+ ******************************************************************************/
+#pragma once
+
+#include <aidl/android/hardware/weaver/BnWeaver.h>
+#include <weaver-impl.h>
+#include <weaver_interface.h>
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace weaver {
+
+using ::aidl::android::hardware::weaver::WeaverConfig;
+using ::aidl::android::hardware::weaver::WeaverReadResponse;
+using ::ndk::ScopedAStatus;
+using std::vector;
+
+struct Weaver : public BnWeaver {
+public:
+  Weaver();
+  // Methods from ::android::hardware::weaver::IWeaver follow.
+  ScopedAStatus getConfig(WeaverConfig *_aidl_return) override;
+  ScopedAStatus read(int32_t in_slotId, const vector<uint8_t> &in_key,
+                     WeaverReadResponse *_aidl_return) override;
+  ScopedAStatus write(int32_t in_slotId, const vector<uint8_t> &in_key,
+                      const vector<uint8_t> &in_value) override;
+
+private:
+  WeaverInterface *pInterface = nullptr;
+};
+
+} // namespace weaver
+} // namespace hardware
+} // namespace android
+} // namespace aidl
diff --git a/aidl_impl/android.hardware.weaver-service.nxp.rc b/aidl_impl/android.hardware.weaver-service.nxp.rc
new file mode 100644
index 0000000..5516e65
--- /dev/null
+++ b/aidl_impl/android.hardware.weaver-service.nxp.rc
@@ -0,0 +1,4 @@
+service vendor.weaver_nxp /vendor/bin/hw/android.hardware.weaver-service.nxp
+    class hal
+    user vendor_nxp_weaver
+    group system drmrpc
diff --git a/aidl_impl/android.hardware.weaver-service.nxp.xml b/aidl_impl/android.hardware.weaver-service.nxp.xml
new file mode 100644
index 0000000..bfe4396
--- /dev/null
+++ b/aidl_impl/android.hardware.weaver-service.nxp.xml
@@ -0,0 +1,10 @@
+<manifest version="1.0" type="device">
+    <hal format="aidl">
+        <name>android.hardware.weaver</name>
+        <version>2</version>
+        <interface>
+            <name>IWeaver</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>
diff --git a/aidl_impl/service.cpp b/aidl_impl/service.cpp
new file mode 100644
index 0000000..570f427
--- /dev/null
+++ b/aidl_impl/service.cpp
@@ -0,0 +1,44 @@
+/******************************************************************************
+ *
+ *  Copyright 2023 NXP
+ *
+ *  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 "Weaver.h"
+#include <android-base/logging.h>
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
+#include <log/log.h>
+
+using ::aidl::android::hardware::weaver::Weaver;
+
+int main() {
+  try {
+    ABinderProcess_setThreadPoolMaxThreadCount(0);
+    std::shared_ptr<Weaver> weaver = ndk::SharedRefBase::make<Weaver>();
+
+    const std::string instance =
+        std::string() + Weaver::descriptor + "/default";
+    binder_status_t status =
+        AServiceManager_addService(weaver->asBinder().get(), instance.c_str());
+    CHECK(status == STATUS_OK);
+
+    ABinderProcess_joinThreadPool();
+  } catch (std::__1::ios_base::failure &e) {
+    ALOGE("ios failure Exception occurred = %s ", e.what());
+  } catch (std::__1::system_error &e) {
+    ALOGE("system error Exception occurred = %s ", e.what());
+  }
+  return -1; // Should never be reached
+}