1 /******************************************************************************
2  *
3  *  Copyright 2020, 2022 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 #define LOG_TAG "Weaver@1.0-service"
19 
20 #include "Weaver.h"
21 #include <log/log.h>
22 #include <string.h>
23 #include <hidl/LegacySupport.h>
24 #include <weaver_interface.h>
25 #include <weaver-impl.h>
26 
27 /* Mutex to synchronize multiple transceive */
28 
29 namespace android {
30 namespace hardware {
31 namespace weaver {
32 namespace V1_0 {
33 namespace implementation {
34 
35   WeaverInterface *pInterface = nullptr;
Weaver()36   Weaver::Weaver() {
37     ALOGI("INITIALIZING WEAVER");
38     pInterface = WeaverImpl::getInstance();
39     if(pInterface != NULL) {
40       pInterface->Init();
41     }
42   }
43 
getConfig(getConfig_cb _hidl_cb)44   Return<void> Weaver::getConfig(getConfig_cb _hidl_cb) {
45   ALOGI("GETCONFIG API ENTRY");
46     if(_hidl_cb == NULL) {
47       return Void();
48     }
49     WeaverConfig configResp;
50     if(pInterface == NULL) {
51       ALOGI("Weaver Interface not defined");
52       _hidl_cb(WeaverStatus::FAILED, configResp);
53       return Void();
54     }
55     SlotInfo slotInfo;
56     Status_Weaver status = pInterface->GetSlots(slotInfo);
57     if(status == WEAVER_STATUS_OK) {
58       configResp.slots =  slotInfo.slots;
59       configResp.keySize = slotInfo.keySize;
60       configResp.valueSize = slotInfo.valueSize;
61       ALOGI("Weaver Success for getSlots Slots :(%d)", configResp.slots);
62       _hidl_cb(WeaverStatus::OK, configResp);
63     } else {
64       _hidl_cb(WeaverStatus::FAILED, configResp);
65     }
66     return Void();
67   }
68 
69   Return<::android::hardware::weaver::V1_0::WeaverStatus>
write(uint32_t slotId,const hidl_vec<uint8_t> & key,const hidl_vec<uint8_t> & value)70     Weaver::write(uint32_t slotId, const hidl_vec<uint8_t>& key, const hidl_vec<uint8_t>& value) {
71       ALOGI("Write API ENTRY");
72       WeaverStatus status = WeaverStatus::FAILED;
73       if(key != NULL && value != NULL && pInterface != NULL
74           && (pInterface->Write(slotId, key, value) == WEAVER_STATUS_OK)) {
75         status = WeaverStatus::OK;
76       }
77       return status;
78     }
79 
80   Return<void>
read(uint32_t slotId,const hidl_vec<uint8_t> & key,read_cb _hidl_cb)81     Weaver::read(uint32_t slotId, const hidl_vec<uint8_t>& key, read_cb _hidl_cb) {
82       ALOGI("Read API ENTRY");
83       if (_hidl_cb == NULL) {
84         return Void();
85       }
86       WeaverReadResponse readResp;
87       if (key == NULL || pInterface == NULL) {
88         _hidl_cb(WeaverReadStatus::FAILED, readResp);
89       } else {
90         ReadRespInfo readInfo;
91         Status_Weaver status = pInterface->Read(slotId, key, readInfo);
92         switch (status) {
93           case WEAVER_STATUS_OK:
94             ALOGI("Read OK");
95             readResp.value = readInfo.value;
96             _hidl_cb(WeaverReadStatus::OK, readResp);
97             break;
98           case WEAVER_STATUS_INCORRECT_KEY:
99             ALOGI("Read Incorrect Key");
100             readResp.value.resize(0);
101             readResp.timeout = readInfo.timeout;
102             _hidl_cb(WeaverReadStatus::INCORRECT_KEY, readResp);
103             break;
104           case WEAVER_STATUS_THROTTLE:
105             ALOGI("Read WEAVER_THROTTLE");
106             readResp.value.resize(0);
107             readResp.timeout = readInfo.timeout;
108             _hidl_cb(WeaverReadStatus::THROTTLE, readResp);
109             break;
110           default:
111             readResp.timeout = 0;
112             readResp.value.resize(0);
113             _hidl_cb(WeaverReadStatus::FAILED, readResp);
114         }
115       }
116       return Void();
117     }
118 
serviceDied(uint64_t,const wp<IBase> &)119   void Weaver::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
120     if(pInterface != NULL) {
121       pInterface->DeInit();
122     }
123   }
124 }
125 }
126 }
127 }
128 }
129