1 /*
2 * Copyright (c) 2023 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 "oaid_service_client.h"
17
18 #include <cinttypes>
19 #include <mutex>
20
21 #include "oaid_common.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "system_ability_load_callback_stub.h"
25
26 namespace OHOS {
27 namespace Cloud {
28 namespace {
29 static const int32_t OAID_SYSTME_ID = 6101; // The system component ID of the OAID is 6101.
30
31 static const std::string OAID_ALLZERO_STR = "00000000-0000-0000-0000-000000000000";
32
33 /**
34 * load time out: 10s
35 */
36 static const int8_t LOAD_TIME_OUT = 10;
37 } // namespace
38
39 std::mutex OAIDServiceClient::instanceLock_;
40 sptr<OAIDServiceClient> OAIDServiceClient::instance_;
41
42 class OAIDServiceLoadCallback : public SystemAbilityLoadCallbackStub {
43 public:
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)44 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject) override
45 {
46 if (systemAbilityId != OAID_SYSTME_ID) {
47 OAID_HILOGE(OAID_MODULE_SERVICE, "Start systemAbility is not oaid service: %{public}d.", systemAbilityId);
48 return;
49 }
50
51 OAIDServiceClient::GetInstance()->LoadServerSuccess(remoteObject);
52 }
53
OnLoadSystemAbilityFail(int32_t systemAbilityId)54 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override
55 {
56 if (systemAbilityId != OAID_SYSTME_ID) {
57 OAID_HILOGE(OAID_MODULE_SERVICE, "Start systemAbility is not oaid service: %{public}d.", systemAbilityId);
58 return;
59 }
60
61 OAIDServiceClient::GetInstance()->LoadServerFail();
62 }
63 };
64
OAIDServiceClient()65 OAIDServiceClient::OAIDServiceClient()
66 {
67 deathRecipient_ = new (std::nothrow) OAIDSaDeathRecipient();
68 }
69
~OAIDServiceClient()70 OAIDServiceClient::~OAIDServiceClient()
71 {
72 if (oaidServiceProxy_ != nullptr) {
73 auto remoteObject = oaidServiceProxy_->AsObject();
74 if (remoteObject != nullptr && deathRecipient_ != nullptr) {
75 remoteObject->RemoveDeathRecipient(deathRecipient_);
76 }
77 }
78 }
79
GetInstance()80 sptr<OAIDServiceClient> OAIDServiceClient::GetInstance()
81 {
82 if (instance_ == nullptr) {
83 std::lock_guard<std::mutex> autoLock(instanceLock_);
84 if (instance_ == nullptr) {
85 instance_ = new OAIDServiceClient;
86 }
87 }
88 return instance_;
89 }
90
LoadService()91 bool OAIDServiceClient::LoadService()
92 {
93 if (loadServiceReady_) {
94 return true;
95 }
96
97 std::lock_guard<std::mutex> lock(loadServiceLock_);
98 if (loadServiceReady_) {
99 return true;
100 }
101
102 sptr<ISystemAbilityManager> systemAbilityManager =
103 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104 if (systemAbilityManager == nullptr) {
105 OAID_HILOGE(OAID_MODULE_CLIENT, "Getting SystemAbilityManager failed.");
106 return false;
107 }
108
109 sptr<OAIDServiceLoadCallback> loadCallback = new (std::nothrow) OAIDServiceLoadCallback();
110 if (loadCallback == nullptr) {
111 OAID_HILOGE(OAID_MODULE_CLIENT, "New OAIDServiceLoadCallback failed.");
112 return false;
113 }
114
115 int32_t result = systemAbilityManager->LoadSystemAbility(OAID_SYSTME_ID, loadCallback);
116 if (result != ERR_OK) {
117 OAID_HILOGE(
118 OAID_MODULE_CLIENT, "LoadSystemAbility %{public}d failed, result: %{public}d.", OAID_SYSTME_ID, result);
119 return false;
120 }
121
122 std::unique_lock<std::mutex> conditionLock(loadServiceConditionLock_);
123 auto waitStatus = loadServiceCondition_.wait_for(
124 conditionLock, std::chrono::seconds(LOAD_TIME_OUT), [this]() { return loadServiceReady_; });
125 if (!waitStatus) {
126 OAID_HILOGE(OAID_MODULE_CLIENT, "LoadSystemAbility timeout.");
127 return false;
128 }
129
130 return true;
131 }
132
GetOAID()133 std::string OAIDServiceClient::GetOAID()
134 {
135 OAID_HILOGI(OAID_MODULE_CLIENT, "Begin.");
136
137 if (!LoadService()) {
138 OAID_HILOGW(OAID_MODULE_CLIENT, "Redo load oaid service.");
139 LoadService();
140 }
141
142 if (oaidServiceProxy_ == nullptr) {
143 OAID_HILOGE(OAID_MODULE_CLIENT, "Quit because redoing load oaid service failed.");
144 return OAID_ALLZERO_STR;
145 }
146
147 auto oaid = oaidServiceProxy_->GetOAID();
148 if (oaid == "") {
149 OAID_HILOGE(OAID_MODULE_CLIENT, "Get OAID failed.");
150 return OAID_ALLZERO_STR;
151 }
152 OAID_HILOGI(OAID_MODULE_SERVICE, "Get OAID id %{public}zu.", oaid.size());
153 return oaid;
154 }
155
ResetOAID()156 void OAIDServiceClient::ResetOAID()
157 {
158 OAID_HILOGI(OAID_MODULE_CLIENT, "Begin.");
159
160 if (!LoadService()) {
161 OAID_HILOGW(OAID_MODULE_CLIENT, "Redo load oaid service.");
162 LoadService();
163 }
164
165 if (oaidServiceProxy_ == nullptr) {
166 OAID_HILOGE(OAID_MODULE_CLIENT, "Quit because redoing load oaid service failed.");
167 }
168
169 oaidServiceProxy_->ResetOAID();
170
171 OAID_HILOGI(OAID_MODULE_CLIENT, "End.");
172 }
173
OnRemoteSaDied(const wptr<IRemoteObject> & remote)174 void OAIDServiceClient::OnRemoteSaDied(const wptr<IRemoteObject>& remote)
175 {
176 OAID_HILOGE(OAID_MODULE_CLIENT, "OnRemoteSaDied");
177 std::unique_lock<std::mutex> lock(loadServiceConditionLock_);
178 if (oaidServiceProxy_ != nullptr) {
179 auto remoteObject = oaidServiceProxy_->AsObject();
180 if (remoteObject != nullptr && deathRecipient_ != nullptr) {
181 remoteObject->RemoveDeathRecipient(deathRecipient_);
182 }
183 oaidServiceProxy_ = nullptr;
184 }
185 loadServiceReady_ = false;
186 }
187
LoadServerSuccess(const sptr<IRemoteObject> & remoteObject)188 void OAIDServiceClient::LoadServerSuccess(const sptr<IRemoteObject>& remoteObject)
189 {
190 std::unique_lock<std::mutex> lock(loadServiceConditionLock_);
191 if (remoteObject == nullptr) {
192 OAID_HILOGE(OAID_MODULE_CLIENT, "Load OAID service is null.");
193 return;
194 }
195
196 if (deathRecipient_ != nullptr) {
197 remoteObject->AddDeathRecipient(deathRecipient_);
198 }
199 oaidServiceProxy_ = iface_cast<IOAIDService>(remoteObject);
200 loadServiceReady_ = true;
201 loadServiceCondition_.notify_one();
202 OAID_HILOGI(OAID_MODULE_CLIENT, "Load OAID service success.");
203 }
204
LoadServerFail()205 void OAIDServiceClient::LoadServerFail()
206 {
207 std::unique_lock<std::mutex> lock(loadServiceConditionLock_);
208 loadServiceReady_ = false;
209 OAID_HILOGE(OAID_MODULE_CLIENT, "Load OAID service fail.");
210 }
211
OAIDSaDeathRecipient()212 OAIDSaDeathRecipient::OAIDSaDeathRecipient() {}
213
OnRemoteDied(const wptr<IRemoteObject> & object)214 void OAIDSaDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
215 {
216 OAID_HILOGW(OAID_MODULE_CLIENT, "Remote systemAbility died.");
217 OAIDServiceClient::GetInstance()->OnRemoteSaDied(object);
218 }
219 } // namespace Cloud
220 } // namespace OHOS
221