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(DEV_AUTH_CALL_REQUEST, dataParcel, replyParcel, option);
49 LOGI("SendRequest done, ret %d", ret);
50 (ret == ERR_NONE) ? replyParcel.ReadInt32(ret) : (ret = HC_ERR_IPC_INTERNAL_FAILED);
51 return ret;
52 }
53
ServiceRunning(void)54 bool ProxyDevAuth::ServiceRunning(void)
55 {
56 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (saMgr == nullptr) {
58 return false;
59 }
60 auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
61 if (daSa == nullptr) {
62 return false;
63 }
64 return true;
65 }
66
EncodeCallRequest(int32_t type,const uint8_t * param,int32_t paramSz)67 int32_t ProxyDevAuthData::EncodeCallRequest(int32_t type, const uint8_t *param, int32_t paramSz)
68 {
69 LOGI("type %d, paramSz %d", type, paramSz);
70 if (tmpDataParcel.WriteInt32(type) && tmpDataParcel.WriteInt32(paramSz) &&
71 tmpDataParcel.WriteBuffer(reinterpret_cast<const void *>(param), static_cast<size_t>(paramSz))) {
72 paramCnt++;
73 return HC_SUCCESS;
74 }
75 return HC_ERROR;
76 }
77
FinalCallRequest(int32_t methodId)78 int32_t ProxyDevAuthData::FinalCallRequest(int32_t methodId)
79 {
80 int32_t dataLen;
81 const uint8_t *dataPtr = nullptr;
82
83 dataLen = static_cast<int32_t>(tmpDataParcel.GetDataSize());
84 dataPtr = const_cast<const uint8_t *>(reinterpret_cast<uint8_t *>(tmpDataParcel.GetData()));
85 if ((dataLen <= 0) || (dataPtr == nullptr)) {
86 LOGE("data invalid");
87 return HC_ERROR;
88 }
89 auto proxy = GetProxy();
90 if (proxy == nullptr) {
91 LOGE("get proxy failed");
92 return HC_ERR_IPC_GET_PROXY;
93 }
94 if (!dataParcel.WriteInterfaceToken(proxy->GetDescriptor())) {
95 LOGE("[IPC][C->S]: Failed to write interface token!");
96 return HC_ERROR;
97 }
98 LOGI("method id %d, param num %d, data length %d", methodId, paramCnt, dataLen);
99 /* request data length = number of params + params information */
100 if (!dataParcel.WriteInt32(methodId) || !dataParcel.WriteInt32(dataLen + sizeof(int32_t)) ||
101 !dataParcel.WriteInt32(paramCnt)) {
102 return HC_ERROR;
103 }
104 if (!dataParcel.WriteBuffer(reinterpret_cast<const void *>(dataPtr), static_cast<size_t>(dataLen))) {
105 return HC_ERROR;
106 }
107 if (withCallback) {
108 if (!dataParcel.WriteInt32(PARAM_TYPE_CB_OBJECT) || !dataParcel.WriteRemoteObject(cbStub)) {
109 return HC_ERROR;
110 }
111 LOGI("type %d, cbStub %s", PARAM_TYPE_CB_OBJECT, (cbStub != nullptr) ? "true" : "false");
112 }
113 cbStub = nullptr;
114 withCallback = false;
115 return HC_SUCCESS;
116 }
117
GetProxy() const118 sptr<ProxyDevAuth> ProxyDevAuthData::GetProxy() const
119 {
120 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
121 if (saMgr == nullptr) {
122 LOGE("GetSystemAbilityManager failed");
123 return nullptr;
124 }
125 auto daSa = saMgr->GetSystemAbility(DEVICE_AUTH_SERVICE_ID);
126 if (daSa == nullptr) {
127 LOGE("GetSystemAbility failed");
128 return nullptr;
129 }
130
131 return iface_cast<ProxyDevAuth>(daSa);
132 }
133
ActCall(bool withSync)134 int32_t ProxyDevAuthData::ActCall(bool withSync)
135 {
136 auto proxy = GetProxy();
137 if (proxy == nullptr) {
138 LOGE("proxy failed");
139 return HC_ERR_IPC_GET_PROXY;
140 }
141 return proxy->DoCallRequest(dataParcel, replyParcel, withSync);
142 }
143
GetReplyParcel(void)144 MessageParcel *ProxyDevAuthData::GetReplyParcel(void)
145 {
146 return &replyParcel;
147 }
148
SetCallbackStub(sptr<IRemoteObject> cbRemote)149 void ProxyDevAuthData::SetCallbackStub(sptr<IRemoteObject> cbRemote)
150 {
151 if (cbRemote != nullptr) {
152 this->cbStub = cbRemote;
153 withCallback = true;
154 }
155 return;
156 }
157 }
158