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 LOGI("ProxyDevAuth, SendRequest...");
39 remote = Remote();
40 if (remote == nullptr) {
41 LOGE("Proxy DoCallRequest Remote() is null");
42 return HC_ERR_IPC_INTERNAL_FAILED;
43 }
44
45 if (withSync == false) {
46 option = { MessageOption::TF_ASYNC };
47 }
48 ret = remote->SendRequest(static_cast<uint32_t>(DevAuthInterfaceCode::DEV_AUTH_CALL_REQUEST),
49 dataParcel, replyParcel, option);
50 LOGI("SendRequest done, ret %d", ret);
51 (ret == ERR_NONE) ? replyParcel.ReadInt32(ret) : (ret = HC_ERR_IPC_INTERNAL_FAILED);
52 return ret;
53 }
54
ServiceRunning(void)55 bool ProxyDevAuth::ServiceRunning(void)
56 {
57 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (saMgr == nullptr) {
59 return false;
60 }
61 auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
62 if (daSa == nullptr) {
63 return false;
64 }
65 return true;
66 }
67
EncodeCallRequest(int32_t type,const uint8_t * param,int32_t paramSz)68 int32_t ProxyDevAuthData::EncodeCallRequest(int32_t type, const uint8_t *param, int32_t paramSz)
69 {
70 LOGI("type %d, paramSz %d", type, paramSz);
71 if (tmpDataParcel.WriteInt32(type) && tmpDataParcel.WriteInt32(paramSz) &&
72 tmpDataParcel.WriteBuffer(reinterpret_cast<const void *>(param), static_cast<size_t>(paramSz))) {
73 paramCnt++;
74 return HC_SUCCESS;
75 }
76 return HC_ERROR;
77 }
78
FinalCallRequest(int32_t methodId)79 int32_t ProxyDevAuthData::FinalCallRequest(int32_t methodId)
80 {
81 int32_t dataLen;
82 const uint8_t *dataPtr = nullptr;
83
84 dataLen = static_cast<int32_t>(tmpDataParcel.GetDataSize());
85 dataPtr = const_cast<const uint8_t *>(reinterpret_cast<uint8_t *>(tmpDataParcel.GetData()));
86 if ((dataLen <= 0) || (dataPtr == nullptr)) {
87 LOGE("data invalid");
88 return HC_ERROR;
89 }
90 auto proxy = GetProxy();
91 if (proxy == nullptr) {
92 LOGE("get proxy failed");
93 return HC_ERR_IPC_GET_PROXY;
94 }
95 if (!dataParcel.WriteInterfaceToken(proxy->GetDescriptor())) {
96 LOGE("[IPC][C->S]: Failed to write interface token!");
97 return HC_ERROR;
98 }
99 LOGI("method id %d, param num %d, data length %d", methodId, paramCnt, dataLen);
100 /* request data length = number of params + params information */
101 if (!dataParcel.WriteInt32(methodId) || !dataParcel.WriteInt32(dataLen + sizeof(int32_t)) ||
102 !dataParcel.WriteInt32(paramCnt)) {
103 return HC_ERROR;
104 }
105 if (!dataParcel.WriteBuffer(reinterpret_cast<const void *>(dataPtr), static_cast<size_t>(dataLen))) {
106 return HC_ERROR;
107 }
108 if (withCallback) {
109 if (!dataParcel.WriteInt32(PARAM_TYPE_CB_OBJECT) || !dataParcel.WriteRemoteObject(cbStub)) {
110 return HC_ERROR;
111 }
112 LOGI("type %d, cbStub %s", PARAM_TYPE_CB_OBJECT, (cbStub != nullptr) ? "true" : "false");
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