blob: 6b77924be7987592888a17197dfedf599a41a524 [file] [log] [blame]
ChengYou Ho10f8a482021-01-02 22:45:32 +08001/*
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 Hoaf611582021-12-07 14:42:37 +080018#include <array>
ChengYou Ho10f8a482021-01-02 22:45:32 +080019
20namespace aidl {
21namespace android {
22namespace hardware {
23namespace weaver {
24
ChengYou Hoaf611582021-12-07 14:42:37 +080025struct Slotinfo {
26 int slot_id;
27 std::vector<uint8_t> key;
28 std::vector<uint8_t> value;
29};
30
31std::array<struct Slotinfo, 16> slot_array;
ChengYou Ho10f8a482021-01-02 22:45:32 +080032// Methods from ::android::hardware::weaver::IWeaver follow.
33
34::ndk::ScopedAStatus Weaver::getConfig(WeaverConfig* out_config) {
ChengYou Hoaf611582021-12-07 14:42:37 +080035 *out_config = {16, 16, 16};
ChengYou Ho10f8a482021-01-02 22:45:32 +080036 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) {
ChengYou Hoaf611582021-12-07 14:42:37 +080040
41 if (in_slotId > 15 || in_key.size() > 16) {
42 *out_response = {0, {}};
43 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(Weaver::STATUS_FAILED));
44 }
45
46 if (slot_array[in_slotId].key != in_key) {
47 *out_response = {0, {}};
48 return ndk::ScopedAStatus(AStatus_fromServiceSpecificError(Weaver::STATUS_INCORRECT_KEY));
49 }
50
51 *out_response = {0, slot_array[in_slotId].value};
52
ChengYou Ho10f8a482021-01-02 22:45:32 +080053 return ::ndk::ScopedAStatus::ok();
54}
55
56::ndk::ScopedAStatus Weaver::write(int32_t in_slotId, const std::vector<uint8_t>& in_key, const std::vector<uint8_t>& in_value) {
ChengYou Hoaf611582021-12-07 14:42:37 +080057
58 if (in_slotId > 15 || in_key.size() > 16 || in_value.size() > 16)
59 return ::ndk::ScopedAStatus::fromStatus(STATUS_FAILED_TRANSACTION);
60
61 slot_array[in_slotId].key = in_key;
62 slot_array[in_slotId].value = in_value;
63
ChengYou Ho10f8a482021-01-02 22:45:32 +080064 return ::ndk::ScopedAStatus::ok();
65}
66
67} //namespace weaver
68} //namespace hardware
69} //namespace android
70} //namespace aidl