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