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