1 /*
2 * Copyright (C) 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 "devattest_service_stub.h"
17
18 #include "permission.h"
19 #include "devattest_log.h"
20 #include "devattest_errno.h"
21
22 namespace OHOS {
23 namespace DevAttest {
DevAttestServiceStub()24 DevAttestServiceStub::DevAttestServiceStub()
25 {
26 requestFuncMap_[GET_AUTH_RESULT] = &DevAttestServiceStub::GetAttestStatusInner;
27 }
28
~DevAttestServiceStub()29 DevAttestServiceStub::~DevAttestServiceStub()
30 {
31 requestFuncMap_.clear();
32 }
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int DevAttestServiceStub::OnRemoteRequest(uint32_t code,
35 MessageParcel& data, MessageParcel& reply, MessageOption& option)
36 {
37 HILOGD("[OnRemoteRequest] cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
38 if (data.ReadInterfaceToken() != GetDescriptor()) {
39 HILOGE("[OnRemoteRequest] failed, descriptor is not matched!");
40 return DEVATTEST_SERVICE_FAILED;
41 }
42 DelayUnloadTask();
43 auto itFunc = requestFuncMap_.find(code);
44 if (itFunc != requestFuncMap_.end()) {
45 auto requestFunc = itFunc->second;
46 if (requestFunc != nullptr) {
47 return (this->*requestFunc)(data, reply);
48 }
49 }
50 HILOGE("[OnRemoteRequest] default case");
51 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53
GetAttestStatusInner(MessageParcel & data,MessageParcel & reply)54 int DevAttestServiceStub::GetAttestStatusInner(MessageParcel& data, MessageParcel& reply)
55 {
56 (void)data;
57 if (!DelayedSingleton<Permission>::GetInstance()->IsSystem()) {
58 HILOGE("[GetAttestStatusInner] not a system");
59 if (!reply.WriteInt32(DEVATTEST_ERR_JS_IS_NOT_SYSTEM_APP)) {
60 HILOGE("[GetAttestStatusInner] write DEVATTEST_ERR_JS_IS_NOT_SYSTEM_APP fail");
61 return DEVATTEST_FAIL;
62 }
63 return DEVATTEST_SUCCESS;
64 }
65
66 AttestResultInfo attestResultInfo;
67 int ret = GetAttestStatus(attestResultInfo);
68 if (!reply.WriteInt32(ret)) {
69 HILOGE("[GetAttestStatusInner] write result fail, %{public}d", ret);
70 return DEVATTEST_FAIL;
71 }
72 if (ret == DEVATTEST_SUCCESS) {
73 sptr<AttestResultInfo> attestResultInfoPtr = (std::make_unique<AttestResultInfo>(attestResultInfo)).release();
74 if (!attestResultInfoPtr->Marshalling(reply)) {
75 HILOGE("[GetAttestStatusInner] stub Marshalling failed");
76 return DEVATTEST_FAIL;
77 }
78 } else {
79 HILOGE("[GetAttestStatusInner] GetAttestStatus fail, %{public}d", ret);
80 return DEVATTEST_FAIL;
81 }
82 return DEVATTEST_SUCCESS;
83 }
84 } // end of DevAttest
85 } // end of OHOS