1 /*
2 * Copyright (C) 2021-2024 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_defines.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
RetryLoadDeviceAuthSa(void)32 void ProxyDevAuth::RetryLoadDeviceAuthSa(void)
33 {
34 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35 if (saMgr == nullptr) {
36 LOGE("Get systemabilitymanager instance failed.");
37 return;
38 }
39 auto deviceAuthSa = saMgr->LoadSystemAbility(DEVICE_AUTH_SERVICE_ID, DEVICE_AUTH_SA_LOAD_TIME);
40 if (deviceAuthSa == nullptr) {
41 LOGE("Retry load device auth sa failed.");
42 }
43 }
44
DoCallRequest(MessageParcel & dataParcel,MessageParcel & replyParcel,bool withSync)45 int32_t ProxyDevAuth::DoCallRequest(MessageParcel &dataParcel, MessageParcel &replyParcel, bool withSync)
46 {
47 int32_t ret;
48 sptr<IRemoteObject> remote = nullptr;
49 MessageOption option = { MessageOption::TF_SYNC };
50
51 remote = Remote();
52 if (remote == nullptr) {
53 LOGE("Proxy DoCallRequest Remote() is null");
54 return HC_ERR_IPC_INTERNAL_FAILED;
55 }
56
57 if (withSync == false) {
58 option = { MessageOption::TF_ASYNC };
59 }
60 ret = remote->SendRequest(static_cast<uint32_t>(DevAuthInterfaceCode::DEV_AUTH_CALL_REQUEST),
61 dataParcel, replyParcel, option);
62 if (ret == HC_ERR_IPC_SA_IS_UNLOADING) {
63 LOGI("Try to retry load device auth sa, and send request again, %" LOG_PUB "d.", ret);
64 RetryLoadDeviceAuthSa();
65 ret = remote->SendRequest(static_cast<uint32_t>(DevAuthInterfaceCode::DEV_AUTH_CALL_REQUEST),
66 dataParcel, replyParcel, option);
67 }
68 if (ret != ERR_NONE) {
69 LOGE("ProxyDevAuth::DoCallRequest SendRequest fail. ret = %" LOG_PUB "d", ret);
70 ret = HC_ERR_IPC_INTERNAL_FAILED;
71 } else {
72 replyParcel.ReadInt32(ret);
73 }
74 return ret;
75 }
76
ServiceRunning(void)77 bool ProxyDevAuth::ServiceRunning(void)
78 {
79 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
80 if (saMgr == nullptr) {
81 return false;
82 }
83 auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
84 if (daSa == nullptr) {
85 return false;
86 }
87 return true;
88 }
89
EncodeCallRequest(int32_t type,const uint8_t * param,int32_t paramSz)90 int32_t ProxyDevAuthData::EncodeCallRequest(int32_t type, const uint8_t *param, int32_t paramSz)
91 {
92 if (tmpDataParcel.WriteInt32(type) && tmpDataParcel.WriteInt32(paramSz) &&
93 tmpDataParcel.WriteBuffer(reinterpret_cast<const void *>(param), static_cast<size_t>(paramSz))) {
94 paramCnt++;
95 return HC_SUCCESS;
96 }
97 return HC_ERROR;
98 }
99
FinalCallRequest(int32_t methodId)100 int32_t ProxyDevAuthData::FinalCallRequest(int32_t methodId)
101 {
102 int32_t dataLen;
103 const uint8_t *dataPtr = nullptr;
104
105 dataLen = static_cast<int32_t>(tmpDataParcel.GetDataSize());
106 dataPtr = const_cast<const uint8_t *>(reinterpret_cast<uint8_t *>(tmpDataParcel.GetData()));
107 if ((dataLen <= 0) || (dataPtr == nullptr)) {
108 LOGE("data invalid");
109 return HC_ERROR;
110 }
111 auto proxy = GetProxy();
112 if (proxy == nullptr) {
113 LOGE("get proxy failed");
114 return HC_ERR_IPC_GET_PROXY;
115 }
116 if (!dataParcel.WriteInterfaceToken(proxy->GetDescriptor())) {
117 LOGE("[IPC][C->S]: Failed to write interface token!");
118 return HC_ERROR;
119 }
120 /* request data length = number of params + params information */
121 if (!dataParcel.WriteInt32(methodId) || !dataParcel.WriteInt32(dataLen + sizeof(int32_t)) ||
122 !dataParcel.WriteInt32(paramCnt)) {
123 return HC_ERROR;
124 }
125 if (!dataParcel.WriteBuffer(reinterpret_cast<const void *>(dataPtr), static_cast<size_t>(dataLen))) {
126 return HC_ERROR;
127 }
128 if (withCallback) {
129 if (!dataParcel.WriteInt32(PARAM_TYPE_CB_OBJECT) || !dataParcel.WriteRemoteObject(cbStub)) {
130 return HC_ERROR;
131 }
132 }
133 cbStub = nullptr;
134 withCallback = false;
135 return HC_SUCCESS;
136 }
137
GetProxy() const138 sptr<ProxyDevAuth> ProxyDevAuthData::GetProxy() const
139 {
140 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
141 if (saMgr == nullptr) {
142 LOGE("GetSystemAbilityManager failed");
143 return nullptr;
144 }
145 auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
146 if (daSa == nullptr) {
147 LOGE("GetSystemAbility failed");
148 return nullptr;
149 }
150
151 return iface_cast<ProxyDevAuth>(daSa);
152 }
153
ActCall(bool withSync)154 int32_t ProxyDevAuthData::ActCall(bool withSync)
155 {
156 auto proxy = GetProxy();
157 if (proxy == nullptr) {
158 LOGE("proxy failed");
159 return HC_ERR_IPC_GET_PROXY;
160 }
161 return proxy->DoCallRequest(dataParcel, replyParcel, withSync);
162 }
163
GetReplyParcel(void)164 MessageParcel *ProxyDevAuthData::GetReplyParcel(void)
165 {
166 return &replyParcel;
167 }
168
SetCallbackStub(sptr<IRemoteObject> cbRemote)169 void ProxyDevAuthData::SetCallbackStub(sptr<IRemoteObject> cbRemote)
170 {
171 if (cbRemote != nullptr) {
172 this->cbStub = cbRemote;
173 withCallback = true;
174 }
175 return;
176 }
177 }
178