1 /******************************************************************************
2 *
3 * Copyright 2023 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 #include "Weaver.h"
19 #include <log/log.h>
20
21 namespace aidl {
22 namespace android {
23 namespace hardware {
24 namespace weaver {
25
26 using ::ndk::ScopedAStatus;
27 using std::vector;
28
29 // Methods from ::android::hardware::weaver::IWeaver follow.
30
Weaver()31 Weaver::Weaver() {
32 ALOGD("Weaver Constructor");
33 pInterface = WeaverImpl::getInstance();
34 if (pInterface != NULL) {
35 pInterface->Init();
36 } else {
37 ALOGE("Failed to get Weaver Interface");
38 }
39 }
40
getConfig(WeaverConfig * out_config)41 ScopedAStatus Weaver::getConfig(WeaverConfig *out_config) {
42 ALOGD("Weaver::getConfig");
43
44 if (out_config == NULL) {
45 ALOGE("Invalid param");
46 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
47 STATUS_FAILED, "Null pointer passed");
48 }
49 memset(out_config, 0, sizeof(WeaverConfig));
50
51 if (pInterface == NULL) {
52 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
53 STATUS_FAILED, "Weaver interface not defined");
54 }
55 SlotInfo slotInfo;
56 Status_Weaver status = pInterface->GetSlots(slotInfo);
57 if (status == WEAVER_STATUS_OK) {
58 out_config->slots = slotInfo.slots;
59 out_config->keySize = slotInfo.keySize;
60 out_config->valueSize = slotInfo.valueSize;
61 ALOGD("Weaver Success for getSlots Slots :(%d)", out_config->slots);
62 return ScopedAStatus::ok();
63 } else {
64 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
65 STATUS_FAILED, "Failed to retrieve slots info");
66 }
67 }
68
read(int32_t in_slotId,const vector<uint8_t> & in_key,WeaverReadResponse * out_response)69 ScopedAStatus Weaver::read(int32_t in_slotId, const vector<uint8_t> &in_key,
70 WeaverReadResponse *out_response) {
71
72 ALOGD("Weaver::read slot %d", in_slotId);
73 if (out_response == NULL) {
74 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
75 STATUS_FAILED, "Null pointer passed");
76 }
77 if (in_key.empty()) {
78 out_response->status = WeaverReadStatus::FAILED;
79 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
80 STATUS_FAILED, "Empty key input passed");
81 }
82 if (pInterface == NULL) {
83 out_response->status = WeaverReadStatus::FAILED;
84 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
85 STATUS_FAILED, "Weaver interface not defined");
86 }
87 memset(out_response, 0, sizeof(WeaverReadResponse));
88
89 ReadRespInfo readInfo;
90 ScopedAStatus retStatus;
91 Status_Weaver status;
92
93 status = pInterface->Read(in_slotId, in_key, readInfo);
94 switch (status) {
95 case WEAVER_STATUS_OK:
96 ALOGD("Read slot %d OK", in_slotId);
97 out_response->value = readInfo.value;
98 out_response->status = WeaverReadStatus::OK;
99 retStatus = ScopedAStatus::ok();
100 break;
101 case WEAVER_STATUS_INCORRECT_KEY:
102 ALOGE("Read Incorrect Key");
103 out_response->value.resize(0);
104 out_response->timeout = readInfo.timeout;
105 out_response->status = WeaverReadStatus::INCORRECT_KEY;
106 retStatus = ScopedAStatus::ok();
107 break;
108 case WEAVER_STATUS_THROTTLE:
109 ALOGE("Read WEAVER_THROTTLE");
110 out_response->value.resize(0);
111 out_response->timeout = readInfo.timeout;
112 out_response->status = WeaverReadStatus::THROTTLE;
113 retStatus = ScopedAStatus::ok();
114 break;
115 default:
116 out_response->timeout = 0;
117 out_response->value.resize(0);
118 out_response->status = WeaverReadStatus::FAILED;
119 retStatus = ScopedAStatus::ok();
120 break;
121 }
122 return retStatus;
123 }
124
write(int32_t in_slotId,const vector<uint8_t> & in_key,const vector<uint8_t> & in_value)125 ScopedAStatus Weaver::write(int32_t in_slotId, const vector<uint8_t> &in_key,
126 const vector<uint8_t> &in_value) {
127 ALOGD("Weaver::write slot %d", in_slotId);
128 if (in_key.empty() || in_value.empty()) {
129 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
130 STATUS_FAILED, "Invalid parameters passed");
131 }
132 if (pInterface == NULL) {
133 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
134 STATUS_FAILED, "Weaver interface not defined");
135 }
136 if (pInterface->Write(in_slotId, in_key, in_value) == WEAVER_STATUS_OK) {
137 ALOGD("Write slot %d OK", in_slotId);
138 return ScopedAStatus::ok();
139 } else {
140 return ScopedAStatus::fromServiceSpecificErrorWithMessage(STATUS_FAILED,
141 "Unknown error");
142 }
143 }
144
145 } // namespace weaver
146 } // namespace hardware
147 } // namespace android
148 } // namespace aidl
149