1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ipc_dev_auth_proxy.h"
17 
18 #include "common_defs.h"
19 #include "hc_log.h"
20 #include "ipc_adapt.h"
21 #include "ipc_sdk.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
ProxyDevAuth(const sptr<IRemoteObject> & impl)26 ProxyDevAuth::ProxyDevAuth(const sptr<IRemoteObject> &impl) : IRemoteProxy<IMethodsIpcCall>(impl)
27 {}
28 
~ProxyDevAuth()29 ProxyDevAuth::~ProxyDevAuth()
30 {}
31 
DoCallRequest(MessageParcel & dataParcel,MessageParcel & replyParcel,bool withSync)32 int32_t ProxyDevAuth::DoCallRequest(MessageParcel &dataParcel, MessageParcel &replyParcel, bool withSync)
33 {
34     int32_t ret;
35     sptr<IRemoteObject> remote = nullptr;
36     MessageOption option = { MessageOption::TF_SYNC };
37 
38     remote = Remote();
39     if (remote == nullptr) {
40         LOGE("Proxy DoCallRequest Remote() is null");
41         return HC_ERR_IPC_INTERNAL_FAILED;
42     }
43 
44     if (withSync == false) {
45         option = { MessageOption::TF_ASYNC };
46     }
47     ret = remote->SendRequest(static_cast<uint32_t>(DevAuthInterfaceCode::DEV_AUTH_CALL_REQUEST),
48         dataParcel, replyParcel, option);
49     if (ret != ERR_NONE) {
50         LOGE("SendRequest fail.");
51         ret = HC_ERR_IPC_INTERNAL_FAILED;
52     } else {
53         replyParcel.ReadInt32(ret);
54     }
55     return ret;
56 }
57 
ServiceRunning(void)58 bool ProxyDevAuth::ServiceRunning(void)
59 {
60     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (saMgr == nullptr) {
62         return false;
63     }
64     auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
65     if (daSa == nullptr) {
66         return false;
67     }
68     return true;
69 }
70 
EncodeCallRequest(int32_t type,const uint8_t * param,int32_t paramSz)71 int32_t ProxyDevAuthData::EncodeCallRequest(int32_t type, const uint8_t *param, int32_t paramSz)
72 {
73     if (tmpDataParcel.WriteInt32(type) && tmpDataParcel.WriteInt32(paramSz) &&
74         tmpDataParcel.WriteBuffer(reinterpret_cast<const void *>(param), static_cast<size_t>(paramSz))) {
75         paramCnt++;
76         return HC_SUCCESS;
77     }
78     return HC_ERROR;
79 }
80 
FinalCallRequest(int32_t methodId)81 int32_t ProxyDevAuthData::FinalCallRequest(int32_t methodId)
82 {
83     int32_t dataLen;
84     const uint8_t *dataPtr = nullptr;
85 
86     dataLen = static_cast<int32_t>(tmpDataParcel.GetDataSize());
87     dataPtr = const_cast<const uint8_t *>(reinterpret_cast<uint8_t *>(tmpDataParcel.GetData()));
88     if ((dataLen <= 0) || (dataPtr == nullptr)) {
89         LOGE("data invalid");
90         return HC_ERROR;
91     }
92     auto proxy = GetProxy();
93     if (proxy == nullptr) {
94         LOGE("get proxy failed");
95         return HC_ERR_IPC_GET_PROXY;
96     }
97     if (!dataParcel.WriteInterfaceToken(proxy->GetDescriptor())) {
98         LOGE("[IPC][C->S]: Failed to write interface token!");
99         return HC_ERROR;
100     }
101     /* request data length = number of params + params information */
102     if (!dataParcel.WriteInt32(methodId) || !dataParcel.WriteInt32(dataLen + sizeof(int32_t)) ||
103         !dataParcel.WriteInt32(paramCnt)) {
104         return HC_ERROR;
105     }
106     if (!dataParcel.WriteBuffer(reinterpret_cast<const void *>(dataPtr), static_cast<size_t>(dataLen))) {
107         return HC_ERROR;
108     }
109     if (withCallback) {
110         if (!dataParcel.WriteInt32(PARAM_TYPE_CB_OBJECT) || !dataParcel.WriteRemoteObject(cbStub)) {
111             return HC_ERROR;
112         }
113     }
114     cbStub = nullptr;
115     withCallback = false;
116     return HC_SUCCESS;
117 }
118 
GetProxy() const119 sptr<ProxyDevAuth> ProxyDevAuthData::GetProxy() const
120 {
121     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
122     if (saMgr == nullptr) {
123         LOGE("GetSystemAbilityManager failed");
124         return nullptr;
125     }
126     auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
127     if (daSa == nullptr) {
128         LOGE("GetSystemAbility failed");
129         return nullptr;
130     }
131 
132     return iface_cast<ProxyDevAuth>(daSa);
133 }
134 
ActCall(bool withSync)135 int32_t ProxyDevAuthData::ActCall(bool withSync)
136 {
137     auto proxy = GetProxy();
138     if (proxy == nullptr) {
139         LOGE("proxy failed");
140         return HC_ERR_IPC_GET_PROXY;
141     }
142     return proxy->DoCallRequest(dataParcel, replyParcel, withSync);
143 }
144 
GetReplyParcel(void)145 MessageParcel *ProxyDevAuthData::GetReplyParcel(void)
146 {
147     return &replyParcel;
148 }
149 
SetCallbackStub(sptr<IRemoteObject> cbRemote)150 void ProxyDevAuthData::SetCallbackStub(sptr<IRemoteObject> cbRemote)
151 {
152     if (cbRemote != nullptr) {
153         this->cbStub = cbRemote;
154         withCallback = true;
155     }
156     return;
157 }
158 }
159