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_new.h"
17
18 #include "file_ex.h"
19 #include "string_ex.h"
20
21 #include "if_system_ability_manager.h"
22 #include "ipc_object_proxy.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "sa_profiles.h"
26
27 #include "content_sensor_manager.h"
28 #include "device_profile_dumper.h"
29 #include "distributed_device_profile_constants.h"
30 #include "distributed_device_profile_errors.h"
31 #include "dm_adapter.h"
32 #include "device_profile_manager.h"
33 #include "dp_radar_helper.h"
34 #include "event_handler_factory.h"
35 #include "permission_manager.h"
36 #include "profile_cache.h"
37 #include "static_profile_manager.h"
38 #include "static_capability_collector.h"
39 #include "subscribe_profile_manager.h"
40 #include "switch_profile_manager.h"
41 #include "trust_profile_manager.h"
42
43 namespace OHOS {
44 namespace DistributedDeviceProfile {
45 namespace {
46 const std::string TAG = "DistributedDeviceProfileServiceNew";
47 const std::string UNLOAD_TASK_ID = "unload_dp_svr";
48 constexpr int32_t DELAY_TIME = 180000;
49 constexpr int32_t UNLOAD_IMMEDIATELY = 0;
50 constexpr int32_t WAIT_BUSINESS_PUT_TIME_S = 5;
51 constexpr int32_t WRTE_CACHE_PROFILE_DELAY_TIME_US = 200 * 1000;
52 constexpr int32_t WRTE_CACHE_PROFILE_RETRY_TIMES = 20;
53 }
54
55 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileServiceNew);
56
DistributedDeviceProfileServiceNew()57 DistributedDeviceProfileServiceNew::DistributedDeviceProfileServiceNew()
58 : SystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, true)
59 {
60 HILOGI("DPService construct!");
61 }
62
~DistributedDeviceProfileServiceNew()63 DistributedDeviceProfileServiceNew::~DistributedDeviceProfileServiceNew()
64 {
65 HILOGI("DPService destruction!");
66 }
67
Init()68 int32_t DistributedDeviceProfileServiceNew::Init()
69 {
70 HILOGI("init begin");
71 if (EventHandlerFactory::GetInstance().Init() != DP_SUCCESS) {
72 HILOGE("EventHandlerFactory init failed");
73 return DP_CACHE_INIT_FAIL;
74 }
75 if (PermissionManager::GetInstance().Init() != DP_SUCCESS) {
76 HILOGE("DpDeviceManager init failed");
77 return DP_DEVICE_MANAGER_INIT_FAIL;
78 }
79 if (TrustProfileManager::GetInstance().Init() != DP_SUCCESS) {
80 HILOGE("TrustProfileManager init failed");
81 return DP_TRUST_PROFILE_MANAGER_INIT_FAIL;
82 }
83 if (SubscribeProfileManager::GetInstance().Init() != DP_SUCCESS) {
84 HILOGE("SubscribeProfileManager init failed");
85 return DP_SUBSCRIBE_PROFILE_MANAGER_INIT_FAIL;
86 }
87 HILOGI("init finish");
88 return DP_SUCCESS;
89 }
90
PostInit()91 int32_t DistributedDeviceProfileServiceNew::PostInit()
92 {
93 HILOGI("PostInit begin");
94 if (SwitchProfileManager::GetInstance().Init() != DP_SUCCESS) {
95 HILOGE("SwitchProfileManager init failed");
96 return DP_DEVICE_PROFILE_MANAGER_INIT_FAIL;
97 }
98 if (DeviceProfileManager::GetInstance().Init() != DP_SUCCESS) {
99 HILOGE("DeviceProfileManager init failed");
100 return DP_DEVICE_PROFILE_MANAGER_INIT_FAIL;
101 }
102 if (StaticProfileManager::GetInstance().Init() != DP_SUCCESS) {
103 HILOGE("StaticProfileManager init failed");
104 return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
105 }
106 if (ProfileCache::GetInstance().Init() != DP_SUCCESS) {
107 HILOGE("ProfileCache init failed");
108 return DP_CACHE_INIT_FAIL;
109 }
110 if (StaticCapabilityCollector::GetInstance().Init() != DP_SUCCESS) {
111 HILOGE("StaticCapabilityCollector init failed");
112 return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
113 }
114 if (ContentSensorManager::GetInstance().Init() != DP_SUCCESS) {
115 HILOGE("ContentSensorManager init failed");
116 return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
117 }
118 SaveSwitchProfilesFromTempCache();
119 SaveDynamicProfilesFromTempCache();
120 isInited_ = true;
121 HILOGI("PostInit finish");
122 NotifyDeviceProfileInited();
123 return DP_SUCCESS;
124 }
125
IsInited()126 bool DistributedDeviceProfileServiceNew::IsInited()
127 {
128 return isInited_;
129 }
130
UnInit()131 int32_t DistributedDeviceProfileServiceNew::UnInit()
132 {
133 isInited_ = false;
134 if (TrustProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
135 HILOGE("TrustProfileManager UnInit failed");
136 return DP_TRUST_PROFILE_MANAGER_UNINIT_FAIL;
137 }
138 if (SwitchProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
139 HILOGE("SwitchProfileManager UnInit failed");
140 return DP_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
141 }
142 if (DeviceProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
143 HILOGE("DeviceProfileManager UnInit failed");
144 return DP_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
145 }
146 if (StaticProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
147 HILOGE("StaticProfileManager UnInit failed");
148 return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
149 }
150 if (ProfileCache::GetInstance().UnInit() != DP_SUCCESS) {
151 HILOGE("ProfileCache UnInit failed");
152 return DP_CACHE_INIT_FAIL;
153 }
154 if (PermissionManager::GetInstance().UnInit() != DP_SUCCESS) {
155 HILOGE("DpDeviceManager UnInit failed");
156 return DP_DEVICE_MANAGER_UNINIT_FAIL;
157 }
158 if (SubscribeProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
159 HILOGE("SubscribeProfileManager UnInit failed");
160 return DP_SUBSCRIBE_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
161 }
162 if (StaticCapabilityCollector::GetInstance().UnInit() != DP_SUCCESS) {
163 HILOGE("StaticCapabilityCollector UnInit failed");
164 return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
165 }
166 if (ContentSensorManager::GetInstance().UnInit() != DP_SUCCESS) {
167 HILOGE("ContentSensorManager UnInit failed");
168 return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
169 }
170 if (EventHandlerFactory::GetInstance().UnInit() != DP_SUCCESS) {
171 HILOGE("EventHandlerFactory UnInit failed");
172 return DP_CACHE_UNINIT_FAIL;
173 }
174 DestroyUnloadHandler();
175 ClearProfileCache();
176 HILOGI("UnInit succeeded");
177 return DP_SUCCESS;
178 }
179
CreateUnloadHandler()180 int32_t DistributedDeviceProfileServiceNew::CreateUnloadHandler()
181 {
182 HILOGD("call!");
183 std::lock_guard<std::mutex> lock(unloadMutex_);
184 if (unloadHandler_ == nullptr) {
185 unloadHandler_ = EventHandlerFactory::GetInstance().GetEventHandler();
186 }
187 if (unloadHandler_ == nullptr) {
188 HILOGE("UnloadHandler is nullptr!");
189 return DP_UNLOAD_HANDLER_NULLPTR;
190 }
191 return DP_SUCCESS;
192 }
193
DestroyUnloadHandler()194 int32_t DistributedDeviceProfileServiceNew::DestroyUnloadHandler()
195 {
196 HILOGD("call!");
197 std::lock_guard<std::mutex> lock(unloadMutex_);
198 if (unloadHandler_ == nullptr) {
199 HILOGE("UnloadHandler is nullptr!");
200 return DP_UNLOAD_HANDLER_NULLPTR;
201 }
202 unloadHandler_->RemoveTask(UNLOAD_TASK_ID);
203 unloadHandler_ = nullptr;
204 return DP_SUCCESS;
205 }
206
PutAccessControlProfile(const AccessControlProfile & accessControlProfile)207 int32_t DistributedDeviceProfileServiceNew::PutAccessControlProfile(const AccessControlProfile& accessControlProfile)
208 {
209 if (!PermissionManager::GetInstance().IsCallerTrust(PUT_ACCESS_CONTROL_PROFILE)) {
210 HILOGE("the caller is permission denied!");
211 return DP_PERMISSION_DENIED;
212 }
213 int32_t ret = TrustProfileManager::GetInstance().PutAccessControlProfile(accessControlProfile);
214 DpRadarHelper::GetInstance().ReportPutAclProfile(ret, accessControlProfile);
215 return ret;
216 }
217
UpdateAccessControlProfile(const AccessControlProfile & accessControlProfile)218 int32_t DistributedDeviceProfileServiceNew::UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile)
219 {
220 if (!PermissionManager::GetInstance().IsCallerTrust(UPDATE_ACCESS_CONTROL_PROFILE)) {
221 HILOGE("the caller is permission denied!");
222 return DP_PERMISSION_DENIED;
223 }
224 int32_t ret = TrustProfileManager::GetInstance().UpdateAccessControlProfile(accessControlProfile);
225 DpRadarHelper::GetInstance().ReportUpdateAclProfile(ret, accessControlProfile);
226 return ret;
227 }
228
GetTrustDeviceProfile(const std::string & deviceId,TrustDeviceProfile & trustDeviceProfile)229 int32_t DistributedDeviceProfileServiceNew::GetTrustDeviceProfile(const std::string& deviceId,
230 TrustDeviceProfile& trustDeviceProfile)
231 {
232 if (!PermissionManager::GetInstance().IsCallerTrust(GET_TRUST_DEVICE_PROFILE)) {
233 HILOGE("the caller is permission denied!");
234 return DP_PERMISSION_DENIED;
235 }
236 int32_t ret = TrustProfileManager::GetInstance().GetTrustDeviceProfile(deviceId, trustDeviceProfile);
237 DpRadarHelper::GetInstance().ReportGetTrustProfile(ret, deviceId, trustDeviceProfile);
238 return ret;
239 }
240
GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile> & trustDeviceProfiles)241 int32_t DistributedDeviceProfileServiceNew::GetAllTrustDeviceProfile(
242 std::vector<TrustDeviceProfile>& trustDeviceProfiles)
243 {
244 if (!PermissionManager::GetInstance().IsCallerTrust(GET_ALL_TRUST_DEVICE_PROFILE)) {
245 HILOGE("the caller is permission denied!");
246 return DP_PERMISSION_DENIED;
247 }
248 int32_t ret = TrustProfileManager::GetInstance().GetAllTrustDeviceProfile(trustDeviceProfiles);
249 DpRadarHelper::GetInstance().ReportGetAllTrustProfile(ret, trustDeviceProfiles);
250 return ret;
251 }
252
GetAccessControlProfile(std::map<std::string,std::string> queryParams,std::vector<AccessControlProfile> & accessControlProfiles)253 int32_t DistributedDeviceProfileServiceNew::GetAccessControlProfile(std::map<std::string, std::string> queryParams,
254 std::vector<AccessControlProfile>& accessControlProfiles)
255 {
256 if (!PermissionManager::GetInstance().IsCallerTrust(GET_ACCESS_CONTROL_PROFILE)) {
257 HILOGE("the caller is permission denied!");
258 return DP_PERMISSION_DENIED;
259 }
260 int32_t ret = TrustProfileManager::GetInstance().GetAccessControlProfile(queryParams, accessControlProfiles);
261 DpRadarHelper::GetInstance().ReportGetAclProfile(ret, accessControlProfiles);
262 return ret;
263 }
264
GetAllAccessControlProfile(std::vector<AccessControlProfile> & accessControlProfiles)265 int32_t DistributedDeviceProfileServiceNew::GetAllAccessControlProfile(
266 std::vector<AccessControlProfile>& accessControlProfiles)
267 {
268 if (!PermissionManager::GetInstance().IsCallerTrust(GET_ALL_ACCESS_CONTROL_PROFILE)) {
269 HILOGE("the caller is permission denied!");
270 return DP_PERMISSION_DENIED;
271 }
272 int32_t ret = TrustProfileManager::GetInstance().GetAllAccessControlProfile(accessControlProfiles);
273 DpRadarHelper::GetInstance().ReportGetAllAclProfile(ret, accessControlProfiles);
274 return ret;
275 }
276
DeleteAccessControlProfile(int32_t accessControlId)277 int32_t DistributedDeviceProfileServiceNew::DeleteAccessControlProfile(int32_t accessControlId)
278 {
279 if (!PermissionManager::GetInstance().IsCallerTrust(DELETE_ACCESS_CONTROL_PROFILE)) {
280 HILOGE("the caller is permission denied!");
281 return DP_PERMISSION_DENIED;
282 }
283 int32_t ret = TrustProfileManager::GetInstance().DeleteAccessControlProfile(accessControlId);
284 DpRadarHelper::GetInstance().ReportDeleteAclProfile(ret);
285 return ret;
286 }
287
PutServiceProfile(const ServiceProfile & serviceProfile)288 int32_t DistributedDeviceProfileServiceNew::PutServiceProfile(const ServiceProfile& serviceProfile)
289 {
290 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
291 HILOGE("the caller is permission denied!");
292 return DP_PERMISSION_DENIED;
293 }
294 HILOGD("CheckCallerPermission success interface PutServiceProfile");
295 if (!IsInited()) {
296 return AddSvrProfilesToCache({ serviceProfile });
297 }
298 int32_t ret = DeviceProfileManager::GetInstance().PutServiceProfile(serviceProfile);
299 DpRadarHelper::GetInstance().ReportPutServiceProfile(ret, serviceProfile);
300 return ret;
301 }
302
PutServiceProfileBatch(const std::vector<ServiceProfile> & serviceProfiles)303 int32_t DistributedDeviceProfileServiceNew::PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles)
304 {
305 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
306 HILOGE("the caller is permission denied!");
307 return DP_PERMISSION_DENIED;
308 }
309 HILOGD("CheckCallerPermission success interface PutServiceProfileBatch");
310 if (!IsInited()) {
311 return AddSvrProfilesToCache(serviceProfiles);
312 }
313 int32_t ret = DeviceProfileManager::GetInstance().PutServiceProfileBatch(serviceProfiles);
314 DpRadarHelper::GetInstance().ReportPutServiceProfileBatch(ret, serviceProfiles);
315 return ret;
316 }
317
PutCharacteristicProfile(const CharacteristicProfile & charProfile)318 int32_t DistributedDeviceProfileServiceNew::PutCharacteristicProfile(const CharacteristicProfile& charProfile)
319 {
320 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
321 HILOGE("the caller is permission denied!");
322 return DP_PERMISSION_DENIED;
323 }
324 if (!IsInited()) {
325 return AddCharProfilesToCache({ charProfile });
326 }
327 if (charProfile.GetCharacteristicKey() == SWITCH_STATUS) {
328 int32_t switchRet = SwitchProfileManager::GetInstance().PutCharacteristicProfile(charProfile);
329 DpRadarHelper::GetInstance().ReportPutCharProfile(switchRet, charProfile);
330 return switchRet;
331 }
332 HILOGD("CheckCallerPermission success interface DeviceProfileManager::PutCharacteristicProfile");
333 int32_t ret = DeviceProfileManager::GetInstance().PutCharacteristicProfile(charProfile);
334 DpRadarHelper::GetInstance().ReportPutCharProfile(ret, charProfile);
335 return ret;
336 }
337
PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile> & charProfiles)338 int32_t DistributedDeviceProfileServiceNew::PutCharacteristicProfileBatch(
339 const std::vector<CharacteristicProfile>& charProfiles)
340 {
341 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
342 HILOGE("this caller is permission denied!");
343 return DP_PERMISSION_DENIED;
344 }
345 if (charProfiles.empty()) {
346 HILOGE("charProfiles is empty");
347 return DP_PUT_CHAR_BATCH_FAIL;
348 }
349 if (!IsInited()) {
350 return AddCharProfilesToCache(charProfiles);
351 }
352 std::vector<CharacteristicProfile> switchCharProfiles;
353 std::vector<CharacteristicProfile> dynamicCharProfiles;
354 for (auto& profile : charProfiles) {
355 if (profile.GetCharacteristicKey() == SWITCH_STATUS) {
356 switchCharProfiles.push_back(profile);
357 continue;
358 }
359 dynamicCharProfiles.push_back(profile);
360 }
361 int32_t switchRes = DP_SUCCESS;
362 if (switchCharProfiles.size() > 0) {
363 switchRes = SwitchProfileManager::GetInstance().PutCharacteristicProfileBatch(switchCharProfiles);
364 DpRadarHelper::GetInstance().ReportPutCharProfileBatch(switchRes, switchCharProfiles);
365 }
366 if (switchRes != DP_SUCCESS) {
367 HILOGE("PutCharacteristicProfileBatch fail, res:%{public}d", switchRes);
368 }
369 int32_t dynamicRes = DP_SUCCESS;
370 if (dynamicCharProfiles.size() > 0) {
371 dynamicRes = DeviceProfileManager::GetInstance().PutCharacteristicProfileBatch(dynamicCharProfiles);
372 DpRadarHelper::GetInstance().ReportPutCharProfileBatch(dynamicRes, dynamicCharProfiles);
373 }
374 if (dynamicRes != DP_SUCCESS) {
375 HILOGE("PutCharacteristicProfileBatch fail, res:%{public}d", dynamicRes);
376 }
377 if (switchRes != DP_SUCCESS || dynamicRes != DP_SUCCESS) {
378 return DP_PUT_CHAR_BATCH_FAIL;
379 }
380 HILOGD("PutCharacteristicProfileBatch success ");
381 return DP_SUCCESS;
382 }
383
GetDeviceProfile(const std::string & deviceId,DeviceProfile & deviceProfile)384 int32_t DistributedDeviceProfileServiceNew::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile)
385 {
386 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
387 HILOGE("this caller is permission denied!");
388 return DP_PERMISSION_DENIED;
389 }
390 HILOGD("CheckCallerPermission success interface GetDeviceProfile");
391 int32_t ret = DeviceProfileManager::GetInstance().GetDeviceProfile(deviceId, deviceProfile);
392 DpRadarHelper::GetInstance().ReportGetDeviceProfile(ret, deviceId, deviceProfile);
393 return ret;
394 }
395
GetServiceProfile(const std::string & deviceId,const std::string & serviceName,ServiceProfile & serviceProfile)396 int32_t DistributedDeviceProfileServiceNew::GetServiceProfile(const std::string& deviceId,
397 const std::string& serviceName, ServiceProfile& serviceProfile)
398 {
399 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
400 HILOGE("this caller is permission denied!");
401 return DP_PERMISSION_DENIED;
402 }
403 HILOGD("CheckCallerPermission success interface GetServiceProfile");
404 int32_t ret = DeviceProfileManager::GetInstance().GetServiceProfile(deviceId, serviceName, serviceProfile);
405 DpRadarHelper::GetInstance().ReportGetServiceProfile(ret, deviceId, serviceProfile);
406 return ret;
407 }
408
GetCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)409 int32_t DistributedDeviceProfileServiceNew::GetCharacteristicProfile(const std::string& deviceId,
410 const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile)
411 {
412 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
413 HILOGE("this caller is permission denied!");
414 return DP_PERMISSION_DENIED;
415 }
416 if (characteristicKey == SWITCH_STATUS) {
417 HILOGD("CheckCallerPermission success interface SwitchProfileManager::GetCharacteristicProfile");
418 int32_t switchRet = SwitchProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
419 characteristicKey, charProfile);
420 DpRadarHelper::GetInstance().ReportGetCharProfile(switchRet, deviceId, charProfile);
421 return switchRet;
422 }
423 if (characteristicKey == STATIC_CHARACTERISTIC_KEY) {
424 HILOGD("CheckCallerPermission success interface StaticProfileManager::GetCharacteristicProfile");
425 int32_t staticRet = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
426 characteristicKey, charProfile);
427 DpRadarHelper::GetInstance().ReportGetCharProfile(staticRet, deviceId, charProfile);
428 return staticRet;
429 }
430 HILOGD("CheckCallerPermission success interface DeviceProfileManager::GetCharacteristicProfile");
431 int32_t ret = DeviceProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
432 characteristicKey, charProfile);
433 DpRadarHelper::GetInstance().ReportGetCharProfile(ret, deviceId, charProfile);
434 return ret;
435 }
436
DeleteServiceProfile(const std::string & deviceId,const std::string & serviceName)437 int32_t DistributedDeviceProfileServiceNew::DeleteServiceProfile(const std::string& deviceId,
438 const std::string& serviceName)
439 {
440 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
441 HILOGE("this caller is permission denied!");
442 return DP_PERMISSION_DENIED;
443 }
444 HILOGD("CheckCallerPermission success interface DeleteServiceProfile");
445 int32_t ret = DeviceProfileManager::GetInstance().DeleteServiceProfile(deviceId, serviceName);
446 DpRadarHelper::GetInstance().ReportDeleteServiceProfile(ret, deviceId);
447 return ret;
448 }
449
DeleteCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey)450 int32_t DistributedDeviceProfileServiceNew::DeleteCharacteristicProfile(const std::string& deviceId,
451 const std::string& serviceName, const std::string& characteristicKey)
452 {
453 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
454 HILOGE("this caller is permission denied!");
455 return DP_PERMISSION_DENIED;
456 }
457 HILOGD("CheckCallerPermission success interface DeleteCharacteristicProfile");
458 int32_t ret = DeviceProfileManager::GetInstance().DeleteCharacteristicProfile(deviceId, serviceName,
459 characteristicKey);
460 DpRadarHelper::GetInstance().ReportDeleteCharProfile(ret, deviceId);
461 return ret;
462 }
463
SubscribeDeviceProfile(const SubscribeInfo & subscriberInfo)464 int32_t DistributedDeviceProfileServiceNew::SubscribeDeviceProfile(const SubscribeInfo& subscriberInfo)
465 {
466 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
467 HILOGE("this caller is permission denied!");
468 return DP_PERMISSION_DENIED;
469 }
470 HILOGD("CheckCallerPermission success interface SubscribeDeviceProfile");
471 int32_t ret = SubscribeProfileManager::GetInstance().SubscribeDeviceProfile(subscriberInfo);
472 DpRadarHelper::GetInstance().ReportSubscribeDeviceProfile(ret, subscriberInfo);
473 return ret;
474 }
475
UnSubscribeDeviceProfile(const SubscribeInfo & subscriberInfo)476 int32_t DistributedDeviceProfileServiceNew::UnSubscribeDeviceProfile(const SubscribeInfo& subscriberInfo)
477 {
478 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
479 HILOGE("this caller is permission denied!");
480 return DP_PERMISSION_DENIED;
481 }
482 HILOGD("CheckCallerPermission success interface UnSubscribeDeviceProfile");
483 int32_t ret = SubscribeProfileManager::GetInstance().UnSubscribeDeviceProfile(subscriberInfo);
484 DpRadarHelper::GetInstance().ReportUnSubscribeDeviceProfile(ret, subscriberInfo);
485 return ret;
486 }
487
SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions & syncOptions,sptr<IRemoteObject> syncCompletedCallback)488 int32_t DistributedDeviceProfileServiceNew::SyncDeviceProfile(
489 const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr<IRemoteObject> syncCompletedCallback)
490 {
491 if (!PermissionManager::GetInstance().CheckCallerSyncPermission()) {
492 HILOGE("this caller is permission denied!");
493 return DP_PERMISSION_DENIED;
494 }
495 HILOGD("CheckCallerSyncPermission success interface SyncDeviceProfile");
496 int32_t ret = DeviceProfileManager::GetInstance().SyncDeviceProfile(syncOptions, syncCompletedCallback);
497 DpRadarHelper::GetInstance().ReportSyncDeviceProfile(ret);
498 return ret;
499 }
500
SendSubscribeInfos(std::map<std::string,SubscribeInfo> listenerMap)501 int32_t DistributedDeviceProfileServiceNew::SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap)
502 {
503 return SubscribeProfileManager::GetInstance().SubscribeDeviceProfile(listenerMap);
504 }
505
Dump(int32_t fd,const std::vector<std::u16string> & args)506 int32_t DistributedDeviceProfileServiceNew::Dump(int32_t fd, const std::vector<std::u16string>& args)
507 {
508 std::vector<std::string> argsInStr8;
509 for (const auto& arg : args) {
510 argsInStr8.emplace_back(Str16ToStr8(arg));
511 }
512
513 std::string result;
514 DeviceProfileDumper::Dump(argsInStr8, result);
515
516 if (!SaveStringToFd(fd, result)) {
517 HILOGE("save to fd failed");
518 return DP_INVALID_PARAMS;
519 }
520 return DP_SUCCESS;
521 }
522
DelayUnloadTask()523 void DistributedDeviceProfileServiceNew::DelayUnloadTask()
524 {
525 HILOGI("delay unload task begin");
526 if (!ProfileUtils::GetOnlineDevices().empty()) {
527 HILOGI("already device online, not kill!");
528 return;
529 }
530 auto task = []() {
531 HILOGI("do unload task");
532 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
533 if (samgrProxy == nullptr) {
534 HILOGE("get samgr failed");
535 return;
536 }
537 if (!ProfileUtils::GetOnlineDevices().empty()) {
538 HILOGI("already device online in 3 min, not kill!");
539 return;
540 }
541 int32_t unloadResult = samgrProxy->UnloadSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID);
542 if (unloadResult != DP_SUCCESS) {
543 HILOGE("remove system ability failed");
544 return;
545 }
546 HILOGI("kill dp svr success!");
547 };
548 if (unloadHandler_ == nullptr) {
549 return;
550 }
551 unloadHandler_->RemoveTask(UNLOAD_TASK_ID);
552 HILOGI("delay unload task post task");
553 unloadHandler_->PostTask(task, UNLOAD_TASK_ID, DELAY_TIME);
554 }
555
OnStart(const SystemAbilityOnDemandReason & startReason)556 void DistributedDeviceProfileServiceNew::OnStart(const SystemAbilityOnDemandReason& startReason)
557 {
558 HILOGI("start reason %{public}s", startReason.GetName().c_str());
559 if (!Init()) {
560 HILOGE("init failed");
561 return;
562 }
563 if (CreateUnloadHandler() == DP_SUCCESS) {
564 HILOGI("CreateUnloadHandler success!");
565 DelayUnloadTask();
566 }
567 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
568 AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
569 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
570 if (!Publish(this)) {
571 HILOGE("publish SA failed");
572 return;
573 }
574 }
575
OnStop()576 void DistributedDeviceProfileServiceNew::OnStop()
577 {
578 HILOGI("called");
579 if (!UnInit()) {
580 HILOGE("Uninit failed");
581 return;
582 }
583 }
584
OnIdle(const SystemAbilityOnDemandReason & idleReason)585 int32_t DistributedDeviceProfileServiceNew::OnIdle(const SystemAbilityOnDemandReason& idleReason)
586 {
587 HILOGI("idle reason %{public}d", idleReason.GetId());
588 return UNLOAD_IMMEDIATELY;
589 }
590
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)591 void DistributedDeviceProfileServiceNew::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
592 {
593 HILOGI("called systemAbilityId:%{public}d", systemAbilityId);
594 if (DistributedDeviceProfile::DistributedDeviceProfileServiceNew::GetInstance().IsInited()) {
595 return;
596 }
597 {
598 std::lock_guard<std::mutex> lock(depSaIdsMtx_);
599 if (depSaIds_.empty()) {
600 return;
601 }
602 depSaIds_.erase(systemAbilityId);
603 if (!depSaIds_.empty()) {
604 return;
605 }
606 }
607 PostInit();
608 }
609
AddSvrProfilesToCache(const std::vector<ServiceProfile> & serviceProfiles)610 int32_t DistributedDeviceProfileServiceNew::AddSvrProfilesToCache(const std::vector<ServiceProfile>& serviceProfiles)
611 {
612 if (serviceProfiles.empty()) {
613 HILOGE("serviceProfiles empty");
614 return DP_INVALID_PARAM;
615 }
616 HILOGI("serviceProfiles.size:%{public}zu", serviceProfiles.size());
617 std::map<std::string, std::string> entries;
618 for (const auto& item : serviceProfiles) {
619 if (!ProfileUtils::IsSvrProfileValid(item)) {
620 HILOGE("the is invalid, serviceProfile:%{public}s", item.dump().c_str());
621 return DP_INVALID_PARAM;
622 }
623 if (ProfileCache::GetInstance().IsServiceProfileExist(item)) {
624 HILOGW("the profile is exist!, serviceProfile:%{public}s", item.dump().c_str());
625 return DP_CACHE_EXIST;
626 }
627 ProfileUtils::ServiceProfileToEntries(item, entries);
628 }
629 std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
630 for (const auto& [key, value] : entries) {
631 dynamicProfileMap_[key] = value;
632 }
633 return DP_SUCCESS;
634 }
635
AddCharProfilesToCache(const std::vector<CharacteristicProfile> & charProfiles)636 int32_t DistributedDeviceProfileServiceNew::AddCharProfilesToCache(
637 const std::vector<CharacteristicProfile>& charProfiles)
638 {
639 if (charProfiles.empty()) {
640 HILOGE("charProfiles empty");
641 return DP_INVALID_PARAM;
642 }
643 HILOGI("charProfiles.size:%{public}zu", charProfiles.size());
644 std::vector<CharacteristicProfile> switchCharProfiles;
645 std::map<std::string, std::string> entries;
646 for (const auto& item : charProfiles) {
647 if (!ProfileUtils::IsCharProfileValid(item)) {
648 HILOGE("the is invalid, charProfile:%{public}s", item.dump().c_str());
649 return DP_INVALID_PARAM;
650 }
651 if (ProfileCache::GetInstance().IsCharProfileExist(item)) {
652 HILOGW("the profile is exist!, charProfile:%{public}s", item.dump().c_str());
653 return DP_CACHE_EXIST;
654 }
655 if (item.GetCharacteristicKey() == SWITCH_STATUS) {
656 switchCharProfiles.push_back(item);
657 continue;
658 }
659 ProfileUtils::CharacteristicProfileToEntries(item, entries);
660 }
661 if (!entries.empty()) {
662 HILOGI("entries.size:%{public}zu", entries.size());
663 std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
664 for (const auto& [key, value] : entries) {
665 dynamicProfileMap_[key] = value;
666 }
667 }
668 if (!switchCharProfiles.empty()) {
669 HILOGI("switchCharProfiles.size:%{public}zu", switchCharProfiles.size());
670 std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
671 for (const auto& item : charProfiles) {
672 std::string profileKey = ProfileUtils::GenerateCharProfileKey(item.GetDeviceId(),
673 item.GetServiceName(), item.GetCharacteristicKey());
674 switchProfileMap_[profileKey] = item;
675 }
676 }
677 return DP_SUCCESS;
678 }
679
SaveSwitchProfilesFromTempCache()680 int32_t DistributedDeviceProfileServiceNew::SaveSwitchProfilesFromTempCache()
681 {
682 std::vector<CharacteristicProfile> switchCharProfiles;
683 {
684 std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
685 if (switchProfileMap_.empty()) {
686 HILOGW("switchProfileMap empty");
687 return DP_SUCCESS;
688 }
689 HILOGI("switchProfileMap.size:%{public}zu", switchProfileMap_.size());
690 for (const auto& [profileKey, item] : switchProfileMap_) {
691 switchCharProfiles.emplace_back(item);
692 }
693 switchProfileMap_.clear();
694 }
695 int32_t ret = SwitchProfileManager::GetInstance().PutCharacteristicProfileBatch(switchCharProfiles);
696 DpRadarHelper::GetInstance().ReportPutCharProfileBatch(ret, switchCharProfiles);
697 if (ret != DP_SUCCESS) {
698 HILOGE("PutCharacteristicProfileBatch fail, ret: %{public}d!", ret);
699 }
700 return ret;
701 }
702
GetDynamicProfilesFromTempCache(std::map<std::string,std::string> & entries)703 void DistributedDeviceProfileServiceNew::GetDynamicProfilesFromTempCache(
704 std::map<std::string, std::string>& entries)
705 {
706 {
707 std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
708 if (dynamicProfileMap_.empty()) {
709 HILOGW("dynamicProfileMap empty!");
710 return;
711 }
712 for (const auto& [key, value] : dynamicProfileMap_) {
713 entries[key] = value;
714 }
715 }
716 }
717
SaveDynamicProfilesFromTempCache()718 int32_t DistributedDeviceProfileServiceNew::SaveDynamicProfilesFromTempCache()
719 {
720 std::map<std::string, std::string> entries;
721 GetDynamicProfilesFromTempCache(entries);
722 if (entries.empty() && DeviceProfileManager::GetInstance().IsFirstInitDB()) {
723 HILOGW("entries empty!");
724 sleep(WAIT_BUSINESS_PUT_TIME_S);
725 GetDynamicProfilesFromTempCache(entries);
726 }
727 HILOGI("entries.size:%{public}zu", entries.size());
728 int32_t tryTimes = WRTE_CACHE_PROFILE_RETRY_TIMES;
729 while (tryTimes > 0) {
730 if (DeviceProfileManager::GetInstance().SavePutTempCache(entries) == DP_SUCCESS) {
731 break;
732 }
733 HILOGW("SavePutTempCache fail! leftTimes:%{public}d", tryTimes);
734 usleep(WRTE_CACHE_PROFILE_DELAY_TIME_US);
735 tryTimes--;
736 }
737 if (tryTimes <= 0) {
738 DeviceProfileManager::GetInstance().ResetFirst();
739 }
740 {
741 std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
742 dynamicProfileMap_.clear();
743 }
744 return DP_SUCCESS;
745 }
746
ClearProfileCache()747 void DistributedDeviceProfileServiceNew::ClearProfileCache()
748 {
749 {
750 std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
751 dynamicProfileMap_.clear();
752 }
753 {
754 std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
755 switchProfileMap_.clear();
756 }
757 }
758
SubscribeDeviceProfileInited(int32_t saId,sptr<IRemoteObject> dpInitedCallback)759 int32_t DistributedDeviceProfileServiceNew::SubscribeDeviceProfileInited(int32_t saId,
760 sptr <IRemoteObject> dpInitedCallback)
761 {
762 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
763 HILOGE("this caller is permission denied!");
764 return DP_PERMISSION_DENIED;
765 }
766 if (dpInitedCallback == nullptr) {
767 HILOGE("dpInitedCallback is nullptr");
768 return DP_INVALID_PARAM;
769 }
770 if (saId <= 0 || saId > MAX_SAID) {
771 HILOGE("saId is invalid, saId:%{public}d", saId);
772 return DP_INVALID_PARAM;
773 }
774 if (isInited_.load()) {
775 HILOGI("deviceProfile service is already inited.");
776 sptr<IDpInitedCallback> callbackProxy = iface_cast<IDpInitedCallback>(dpInitedCallback);
777 if (callbackProxy == nullptr) {
778 HILOGE("Cast to IDpInitedCallback failed!");
779 return DP_NULLPTR;
780 }
781 callbackProxy->OnDpInited();
782 }
783 std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
784 dpInitedCallbackMap_[saId] = dpInitedCallback;
785 return DP_SUCCESS;
786 }
787
UnSubscribeDeviceProfileInited(int32_t saId)788 int32_t DistributedDeviceProfileServiceNew::UnSubscribeDeviceProfileInited(int32_t saId)
789 {
790 if (!PermissionManager::GetInstance().CheckCallerPermission()) {
791 HILOGE("this caller is permission denied!");
792 return DP_PERMISSION_DENIED;
793 }
794 std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
795 dpInitedCallbackMap_.erase(saId);
796 return DP_SUCCESS;
797 }
798
NotifyDeviceProfileInited()799 int32_t DistributedDeviceProfileServiceNew::NotifyDeviceProfileInited()
800 {
801 std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
802 for (const auto& [saId, callback] : dpInitedCallbackMap_) {
803 sptr<IDpInitedCallback> callbackProxy = iface_cast<IDpInitedCallback>(callback);
804 if (callbackProxy == nullptr) {
805 HILOGE("Cast to IDpInitedCallback failed!");
806 continue;
807 }
808 callbackProxy->OnDpInited();
809 }
810 return DP_SUCCESS;
811 }
812 } // namespace DeviceProfile
813 } // namespace OHOS
814