• 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_client.h"
17 
18 #include "iservice_registry.h"
19 #include "devattest_log.h"
20 #include "devattest_errno.h"
21 #include "devattest_profile_load_callback.h"
22 
23 namespace OHOS {
24 namespace DevAttest {
25 using namespace std;
26 using namespace OHOS;
27 constexpr int32_t ATTEST_LOADSA_TIMEOUT_MS = 10000;
DevAttestClient()28 DevAttestClient::DevAttestClient()
29 {
30 }
31 
~DevAttestClient()32 DevAttestClient::~DevAttestClient()
33 {
34 }
35 
GetDeviceProfileService()36 sptr<DevAttestInterface> DevAttestClient::GetDeviceProfileService()
37 {
38     {
39         std::lock_guard<std::mutex> lock(clientLock_);
40         sptr<ISystemAbilityManager> samgrProxy =
41             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42         if (samgrProxy == nullptr) {
43             HILOGE("[GetDeviceProfileService] Failed to get system ability mgr.");
44             return nullptr;
45         }
46         sptr<IRemoteObject> object =
47             samgrProxy->CheckSystemAbility(DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE);
48         if (object != nullptr) {
49             HILOGI("[GetDeviceProfileService] attestClientInterface currently exists");
50             attestClientInterface_ = iface_cast<DevAttestInterface>(object);
51             return attestClientInterface_;
52         }
53     }
54 
55     HILOGW("[GetDeviceProfileService] object is null");
56     if (LoadDevAttestProfile()) {
57         std::lock_guard<std::mutex> lock(clientLock_);
58         if (attestClientInterface_ != nullptr) {
59             return attestClientInterface_;
60         } else {
61             HILOGE("[GetDeviceProfileService] load devattest_service failed");
62             return nullptr;
63         }
64     }
65     HILOGE("[GetDeviceProfileService] load service failed");
66     return nullptr;
67 }
68 
LoadDevAttestProfile()69 bool DevAttestClient::LoadDevAttestProfile()
70 {
71     std::unique_lock<std::mutex> lock(clientLock_);
72     sptr<DevAttestProfileLoadCallback> loadCallback = new DevAttestProfileLoadCallback();
73     if (loadCallback == nullptr) {
74         HILOGE("[LoadDevAttestProfile] loadCallback is nullptr.");
75         return false;
76     }
77 
78     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79     if (samgr == nullptr) {
80         HILOGE("[LoadDevAttestProfile] Failed to get system ability mgr.");
81         return false;
82     }
83     int32_t ret = samgr->LoadSystemAbility(DevAttestInterface::SA_ID_DEVICE_ATTEST_SERVICE, loadCallback);
84     if (ret != DEVATTEST_SUCCESS) {
85         HILOGE("[LoadDevAttestProfile] Failed to Load systemAbility");
86         return false;
87     }
88     // 阻塞
89     bool waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(ATTEST_LOADSA_TIMEOUT_MS),
90         [this]() { return attestClientInterface_ != nullptr; });
91     if (!waitStatus) {
92         HILOGE("[LoadDevAttestProfile] load sa timeout");
93         return false;
94     }
95     return true;
96 }
97 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)98 void DevAttestClient::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
99 {
100     std::lock_guard<std::mutex> lock(clientLock_);
101     if (remoteObject == nullptr) {
102         return;
103     }
104     attestClientInterface_ = iface_cast<DevAttestInterface>(remoteObject);
105     proxyConVar_.notify_one();
106     return;
107 }
108 
LoadSystemAbilityFail()109 void DevAttestClient::LoadSystemAbilityFail()
110 {
111     std::lock_guard<std::mutex> lock(clientLock_);
112     attestClientInterface_ = nullptr;
113     return;
114 }
115 
GetAttestStatus(AttestResultInfo & attestResultInfo)116 int DevAttestClient::GetAttestStatus(AttestResultInfo &attestResultInfo)
117 {
118     sptr<DevAttestInterface> attestClientInterface = GetDeviceProfileService();
119     if (attestClientInterface == nullptr) {
120         HILOGE("[GetAttestStatus] DevAttestClient attestClientInterface is null");
121         return DEVATTEST_FAIL;
122     }
123     int ret = attestClientInterface->GetAttestStatus(attestResultInfo);
124     if (ret != DEVATTEST_SUCCESS) {
125         HILOGE("[GetAttestStatus] DevAttestClient failed ret = %{public}d", ret);
126         LoadSystemAbilityFail();
127         return ret;
128     }
129     LoadSystemAbilityFail();
130     return DEVATTEST_SUCCESS;
131 }
132 }
133 }