1 /*
2 * Copyright (c) 2021-2024 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 "distributed_device_profile_service.h"
17
18 #include "file_ex.h"
19 #include "string_ex.h"
20
21 #include "authority_manager.h"
22 #include "content_sensor_manager.h"
23 #include "device_profile_dumper.h"
24 #include "device_profile_errors.h"
25 #include "device_profile_log.h"
26 #include "device_profile_storage_manager.h"
27 #include "distributed_device_profile_service_new.h"
28 #include "dp_device_manager.h"
29 #include "dp_radar_helper.h"
30 #include "event_handler_factory.h"
31 #include "hitrace_meter.h"
32 #include "if_system_ability_manager.h"
33 #include "ipc_object_proxy.h"
34 #include "ipc_skeleton.h"
35 #include "iprofile_event_notifier.h"
36 #include "iservice_registry.h"
37 #include "profile_cache.h"
38 #include "sa_profiles.h"
39 #include "service_characteristic_profile.h"
40 #include "sync_coordinator.h"
41 #include "subscribe_manager.h"
42 #include "trust_group_manager.h"
43
44 namespace OHOS {
45 namespace DeviceProfile {
46 namespace {
47 const std::string TAG = "DistributedDeviceProfileService";
48 const std::string DP_DEVICE_SUB_TRACE = "DP_DEVICE_SUB";
49 const std::string TASK_ID = "unload";
50 const std::string BOOT_COMPLETED_EVENT = "usual.event.BOOT_COMPLETED";
51 const std::string STRING_DEVICE_ONLINE = "deviceonline";
52 const std::string EVENT_ID = "eventId";
53 const std::string NAME = "name";
54 const std::string INIT_TASK_ID = "CheckAndInitDP";
55 constexpr int32_t DELAY_TIME = 180000;
56 constexpr int32_t UNLOAD_IMMEDIATELY = 0;
57 constexpr int32_t DP_IPC_THREAD_NUM = 32;
58 }
59
60 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileService);
61 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&DistributedDeviceProfileService::GetInstance());
62
DistributedDeviceProfileService()63 DistributedDeviceProfileService::DistributedDeviceProfileService()
64 : SystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, true)
65 {
66 }
67
Init()68 bool DistributedDeviceProfileService::Init()
69 {
70 HILOGI("init DistributedDeviceProfileServiceNew");
71 DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().Init();
72 {
73 std::lock_guard<std::mutex> lock(unloadMutex_);
74 if (unloadHandler_ == nullptr) {
75 unloadHandler_ = DistributedDeviceProfile::EventHandlerFactory::GetInstance().GetEventHandler();
76 }
77 if (unloadHandler_ == nullptr) {
78 return false;
79 }
80 }
81 HILOGI("init succeeded");
82 return true;
83 }
84
PutDeviceProfile(const ServiceCharacteristicProfile & profile)85 int32_t DistributedDeviceProfileService::PutDeviceProfile(const ServiceCharacteristicProfile& profile)
86 {
87 bool ret = AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
88 profile.GetServiceId());
89 struct RadarInfo info = {
90 .funcName = "PutDeviceProfile",
91 .stageRes = ret ?
92 static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL),
93 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
94 .errCode = ERR_DP_PERMISSION_DENIED,
95 };
96 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
97 HILOGE("ReportSaCheckAuth failed");
98 }
99 if (!ret) {
100 return ERR_DP_PERMISSION_DENIED;
101 }
102 return DeviceProfileStorageManager::GetInstance().PutDeviceProfile(profile);
103 }
104
DeviceOnline()105 void DistributedDeviceProfileService::DeviceOnline()
106 {
107 HILOGI("device online begin");
108 isOnline_ = true;
109 {
110 std::lock_guard<std::mutex> lock(unloadMutex_);
111 if (unloadHandler_ == nullptr) {
112 return;
113 }
114 unloadHandler_->RemoveTask(TASK_ID);
115 }
116 }
117
GetDeviceProfile(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)118 int32_t DistributedDeviceProfileService::GetDeviceProfile(const std::string& udid, const std::string& serviceId,
119 ServiceCharacteristicProfile& profile)
120 {
121 bool ret = AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_R,
122 serviceId);
123 struct RadarInfo info = {
124 .funcName = "GetDeviceProfile",
125 .stageRes = ret ?
126 static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL),
127 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
128 .errCode = ERR_DP_PERMISSION_DENIED,
129 };
130 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
131 HILOGE("ReportSaCheckAuth failed");
132 }
133 if (!ret) {
134 return ERR_DP_PERMISSION_DENIED;
135 }
136 return DeviceProfileStorageManager::GetInstance().GetDeviceProfile(udid, serviceId, profile);
137 }
138
DeleteDeviceProfile(const std::string & serviceId)139 int32_t DistributedDeviceProfileService::DeleteDeviceProfile(const std::string& serviceId)
140 {
141 bool ret = AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
142 serviceId);
143 struct RadarInfo info = {
144 .funcName = "DeleteDeviceProfile",
145 .stageRes = ret ?
146 static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL),
147 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
148 .errCode = ERR_DP_PERMISSION_DENIED,
149 };
150 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
151 HILOGE("ReportSaCheckAuth failed");
152 }
153 if (!ret) {
154 return ERR_DP_PERMISSION_DENIED;
155 }
156 return DeviceProfileStorageManager::GetInstance().DeleteDeviceProfile(serviceId);
157 }
158
SubscribeProfileEvents(const std::list<SubscribeInfo> & subscribeInfos,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)159 int32_t DistributedDeviceProfileService::SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
160 const sptr<IRemoteObject>& profileEventNotifier,
161 std::list<ProfileEvent>& failedEvents)
162 {
163 HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_SUB_TRACE);
164 struct RadarInfo info = {
165 .funcName = "SubscribeProfileEvents",
166 .stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC),
167 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
168 };
169 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
170 HILOGE("ReportSaCheckAuth failed");
171 }
172 return SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos,
173 profileEventNotifier, failedEvents);
174 }
175
UnsubscribeProfileEvents(const std::list<ProfileEvent> & profileEvents,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)176 int32_t DistributedDeviceProfileService::UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
177 const sptr<IRemoteObject>& profileEventNotifier,
178 std::list<ProfileEvent>& failedEvents)
179 {
180 struct RadarInfo info = {
181 .funcName = "UnsubscribeProfileEvents",
182 .stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC),
183 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
184 };
185 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
186 HILOGE("ReportSaCheckAuth failed");
187 }
188 return SubscribeManager::GetInstance().UnsubscribeProfileEvents(profileEvents,
189 profileEventNotifier, failedEvents);
190 }
191
SyncDeviceProfile(const SyncOptions & syncOptions,const sptr<IRemoteObject> & profileEventNotifier)192 int32_t DistributedDeviceProfileService::SyncDeviceProfile(const SyncOptions& syncOptions,
193 const sptr<IRemoteObject>& profileEventNotifier)
194 {
195 bool ret = AuthorityManager::GetInstance().CheckInterfaceAuthority("sync");
196 struct RadarInfo info = {
197 .funcName = "SyncDeviceProfile",
198 .stageRes = ret ?
199 static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL),
200 .bizState = static_cast<int32_t>(BizState::BIZ_STATE_END),
201 .errCode = ERR_DP_PERMISSION_DENIED,
202 };
203 if (!DpRadarHelper::GetInstance().ReportSaCheckAuth(info)) {
204 HILOGE("ReportSaCheckAuth failed");
205 }
206 if (!ret) {
207 return ERR_DP_PERMISSION_DENIED;
208 }
209 return DeviceProfileStorageManager::GetInstance().SyncDeviceProfile(syncOptions, profileEventNotifier);
210 }
211
Dump(int32_t fd,const std::vector<std::u16string> & args)212 int32_t DistributedDeviceProfileService::Dump(int32_t fd, const std::vector<std::u16string>& args)
213 {
214 std::vector<std::string> argsInStr8;
215 for (const auto& arg : args) {
216 argsInStr8.emplace_back(Str16ToStr8(arg));
217 }
218
219 std::string result;
220 DeviceProfileDumper::Dump(argsInStr8, result);
221
222 if (!SaveStringToFd(fd, result)) {
223 HILOGE("save to fd failed");
224 return ERR_DP_FILE_FAILED_ERR;
225 }
226 return ERR_OK;
227 }
228
DelayUnloadTask()229 void DistributedDeviceProfileService::DelayUnloadTask()
230 {
231 auto task = [this]() {
232 HILOGD("do unload task");
233 if (DistributedDeviceProfile::ProfileCache::GetInstance().IsDeviceOnline()) {
234 return;
235 }
236 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
237 if (samgrProxy == nullptr) {
238 HILOGE("get samgr failed");
239 return;
240 }
241 int32_t ret = samgrProxy->UnloadSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID);
242 if (ret != ERR_OK) {
243 HILOGE("remove system ability failed");
244 return;
245 }
246 };
247
248 {
249 std::lock_guard<std::mutex> lock(unloadMutex_);
250 if (unloadHandler_ == nullptr) {
251 return;
252 }
253 unloadHandler_->RemoveTask(TASK_ID);
254 if (!DistributedDeviceProfile::ProfileCache::GetInstance().IsDeviceOnline()) {
255 HILOGI("delay unload task post task");
256 unloadHandler_->PostTask(task, TASK_ID, DELAY_TIME);
257 }
258 }
259 }
260
OnStart(const SystemAbilityOnDemandReason & startReason)261 void DistributedDeviceProfileService::OnStart(const SystemAbilityOnDemandReason& startReason)
262 {
263 HILOGI("called");
264 if (!Init()) {
265 HILOGE("init failed");
266 }
267 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
268 AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
269 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
270 AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
271 if (!Publish(this)) {
272 HILOGE("publish SA failed");
273 return;
274 }
275 IPCSkeleton::SetMaxWorkThreadNum(DP_IPC_THREAD_NUM);
276 HILOGI("start reason %{public}s", startReason.GetName().c_str());
277 DelayUnloadTask();
278 }
279
OnIdle(const SystemAbilityOnDemandReason & idleReason)280 int32_t DistributedDeviceProfileService::OnIdle(const SystemAbilityOnDemandReason& idleReason)
281 {
282 HILOGI("idle reason %{public}d", idleReason.GetId());
283 return UNLOAD_IMMEDIATELY;
284 }
285
OnStop()286 void DistributedDeviceProfileService::OnStop()
287 {
288 HILOGI("called");
289 if (!DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().UnInit()) {
290 HILOGE("UnInit failed");
291 return;
292 }
293 }
294
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)295 void DistributedDeviceProfileService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
296 {
297 HILOGI("called systemAbilityId:%{public}d", systemAbilityId);
298 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
299 DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().SubscribeAccountCommonEvent();
300 }
301 if (DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().IsInited()) {
302 return;
303 }
304 {
305 std::lock_guard<std::mutex> lock(depSaIdsMtx_);
306 if (depSaIds_.empty()) {
307 return;
308 }
309 depSaIds_.erase(systemAbilityId);
310 if (!depSaIds_.empty()) {
311 return;
312 }
313 }
314 DoBusinessInit();
315 }
316
DoBusinessInit()317 bool DistributedDeviceProfileService::DoBusinessInit()
318 {
319 HILOGD("called");
320 if (!AuthorityManager::GetInstance().Init()) {
321 HILOGE("AuthorityManager init failed");
322 return false;
323 }
324 if (DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().PostInit() != ERR_OK) {
325 HILOGE("PostInit failed");
326 return false;
327 }
328 HILOGI("DoBusinessInit succeeded");
329 return true;
330 }
331 } // namespace DeviceProfile
332 } // namespace OHOS
333