ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Weaver.h" |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 18 | #include <array> |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 19 | |
| 20 | namespace aidl { |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace weaver { |
| 24 | |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 25 | struct Slotinfo { |
| 26 | int slot_id; |
| 27 | std::vector<uint8_t> key; |
| 28 | std::vector<uint8_t> value; |
| 29 | }; |
| 30 | |
| 31 | std::array<struct Slotinfo, 16> slot_array; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 32 | // Methods from ::android::hardware::weaver::IWeaver follow. |
| 33 | |
| 34 | ::ndk::ScopedAStatus Weaver::getConfig(WeaverConfig* out_config) { |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 35 | *out_config = {16, 16, 16}; |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 36 | return ::ndk::ScopedAStatus::ok(); |
| 37 | } |
| 38 | |
| 39 | ::ndk::ScopedAStatus Weaver::read(int32_t in_slotId, const std::vector<uint8_t>& in_key, WeaverReadResponse* out_response) { |
Devin Moore | daf12d9 | 2022-12-07 00:43:46 +0000 | [diff] [blame] | 40 | using ::aidl::android::hardware::weaver::WeaverReadStatus; |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 41 | |
| 42 | if (in_slotId > 15 || in_key.size() > 16) { |
Devin Moore | daf12d9 | 2022-12-07 00:43:46 +0000 | [diff] [blame] | 43 | *out_response = {0, {}, WeaverReadStatus::FAILED}; |
| 44 | return ndk::ScopedAStatus::ok(); |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (slot_array[in_slotId].key != in_key) { |
Devin Moore | daf12d9 | 2022-12-07 00:43:46 +0000 | [diff] [blame] | 48 | *out_response = {0, {}, WeaverReadStatus::INCORRECT_KEY}; |
| 49 | return ndk::ScopedAStatus::ok(); |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Devin Moore | daf12d9 | 2022-12-07 00:43:46 +0000 | [diff] [blame] | 52 | *out_response = {0, slot_array[in_slotId].value, WeaverReadStatus::OK}; |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 53 | |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 54 | return ::ndk::ScopedAStatus::ok(); |
| 55 | } |
| 56 | |
| 57 | ::ndk::ScopedAStatus Weaver::write(int32_t in_slotId, const std::vector<uint8_t>& in_key, const std::vector<uint8_t>& in_value) { |
ChengYou Ho | af61158 | 2021-12-07 14:42:37 +0800 | [diff] [blame] | 58 | |
| 59 | if (in_slotId > 15 || in_key.size() > 16 || in_value.size() > 16) |
| 60 | return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION); |
| 61 | |
| 62 | slot_array[in_slotId].key = in_key; |
| 63 | slot_array[in_slotId].value = in_value; |
| 64 | |
ChengYou Ho | 10f8a48 | 2021-01-02 22:45:32 +0800 | [diff] [blame] | 65 | return ::ndk::ScopedAStatus::ok(); |
| 66 | } |
| 67 | |
| 68 | } //namespace weaver |
| 69 | } //namespace hardware |
| 70 | } //namespace android |
| 71 | } //namespace aidl |