• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "business_event_manager.h"
28 #include "common_event_support.h"
29 #include "content_sensor_manager.h"
30 #include "device_profile_dumper.h"
31 #include "distributed_device_profile_constants.h"
32 #include "distributed_device_profile_errors.h"
33 #include "dm_adapter.h"
34 #include "device_profile_manager.h"
35 #include "dp_radar_helper.h"
36 #include "event_handler_factory.h"
37 #include "ibusiness_callback.h"
38 #include "i_pincode_invalid_callback.h"
39 #include "local_service_info_manager.h"
40 #include "multi_user_manager.h"
41 #include "permission_manager.h"
42 #include "profile_cache.h"
43 #include "profile_data_manager.h"
44 #include "service_info_profile_manager.h"
45 #include "settings_data_manager.h"
46 #include "static_profile_manager.h"
47 #include "static_capability_collector.h"
48 #include "subscribe_profile_manager.h"
49 #include "switch_profile_manager.h"
50 #include "trust_profile_manager.h"
51 #include "session_key_manager.h"
52 
53 namespace OHOS {
54 namespace DistributedDeviceProfile {
55 namespace {
56 const std::string TAG = "DistributedDeviceProfileServiceNew";
57 const std::string UNLOAD_TASK_ID = "unload_dp_svr";
58 const std::string IDLE_REASON_LOW_MEMORY = "resourceschedule.memmgr.low.memory.prepare";
59 constexpr int32_t DELAY_TIME = 180000;
60 constexpr int32_t SA_READY_INTO_IDLE = 0;
61 constexpr int32_t SA_REFUSE_INTO_IDLE = -1;
62 constexpr int32_t WAIT_BUSINESS_PUT_TIME_S = 5;
63 constexpr int32_t WRTE_CACHE_PROFILE_DELAY_TIME_US = 200 * 1000;
64 constexpr int32_t WRTE_CACHE_PROFILE_RETRY_TIMES = 20;
65 constexpr int32_t DP_IPC_THREAD_NUM = 32;
66 }
67 
68 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileServiceNew);
69 const bool REGISTER_RESULT =
70     SystemAbility::MakeAndRegisterAbility(&DistributedDeviceProfileServiceNew::GetInstance());
71 
DistributedDeviceProfileServiceNew()72 DistributedDeviceProfileServiceNew::DistributedDeviceProfileServiceNew()
73     : SystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, true)
74 {
75     HILOGI("DPService construct!");
76 }
77 
Init()78 int32_t DistributedDeviceProfileServiceNew::Init()
79 {
80     HILOGI("init begin");
81     if (EventHandlerFactory::GetInstance().Init() != DP_SUCCESS) {
82         HILOGE("EventHandlerFactory init failed");
83         return DP_CACHE_INIT_FAIL;
84     }
85     if (PermissionManager::GetInstance().Init() != DP_SUCCESS) {
86         HILOGE("DpDeviceManager init failed");
87         return DP_DEVICE_MANAGER_INIT_FAIL;
88     }
89     if (TrustProfileManager::GetInstance().Init() != DP_SUCCESS) {
90         HILOGE("TrustProfileManager init failed");
91         return DP_TRUST_PROFILE_MANAGER_INIT_FAIL;
92     }
93     if (ProfileDataManager::GetInstance().Init() != DP_SUCCESS) {
94         HILOGE("ProfileDataManager init failed");
95     }
96     if (SubscribeProfileManager::GetInstance().Init() != DP_SUCCESS) {
97         HILOGE("SubscribeProfileManager init failed");
98         return DP_SUBSCRIBE_PROFILE_MANAGER_INIT_FAIL;
99     }
100     HILOGI("init finish");
101     return DP_SUCCESS;
102 }
103 
PostInit()104 int32_t DistributedDeviceProfileServiceNew::PostInit()
105 {
106     HILOGI("PostInit begin");
107     if (DMAdapter::GetInstance().Init() != DP_SUCCESS) {
108         HILOGE("DMAdapter init failed");
109         return DP_DM_ADAPTER_INIT_FAIL;
110     }
111 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
112     if (SwitchProfileManager::GetInstance().Init() != DP_SUCCESS) {
113         HILOGE("SwitchProfileManager init failed");
114         return DP_DEVICE_PROFILE_MANAGER_INIT_FAIL;
115     }
116 #else
117     HILOGI("this device does not support switch data");
118 #endif // DEVICE_PROFILE_SWITCH_DISABLE
119     if (DeviceProfileManager::GetInstance().Init() != DP_SUCCESS) {
120         HILOGE("DeviceProfileManager init failed");
121         return DP_DEVICE_PROFILE_MANAGER_INIT_FAIL;
122     }
123     if (StaticProfileManager::GetInstance().Init() != DP_SUCCESS) {
124         HILOGE("StaticProfileManager init failed");
125         return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
126     }
127     if (ProfileCache::GetInstance().Init() != DP_SUCCESS) {
128         HILOGE("ProfileCache init failed");
129         return DP_CACHE_INIT_FAIL;
130     }
131     if (StaticCapabilityCollector::GetInstance().Init() != DP_SUCCESS) {
132         HILOGE("StaticCapabilityCollector init failed");
133         return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
134     }
135     if (ServiceInfoProfileManager::GetInstance().Init() != DP_SUCCESS) {
136         HILOGE("ServiceInfoProfileManager init failed");
137         return DP_SERVICE_INFO_PROFILE_MANAGER_INIT_FAIL;
138     }
139     if (LocalServiceInfoManager::GetInstance().Init() != DP_SUCCESS) {
140         HILOGE("LocalServiceInfoManager init failed");
141         return DP_LOCAL_SERVICE_INFO_MANAGER_INIT_FAIL;
142     }
143     if (MultiUserManager::GetInstance().Init() != DP_SUCCESS) {
144         HILOGE("MultiUserManager init failed");
145         return DP_MULTI_USER_MANAGER_INIT_FAIL;
146     }
147     return PostInitNext();
148 }
149 
PostInitNext()150 int32_t DistributedDeviceProfileServiceNew::PostInitNext()
151 {
152     if (SettingsDataManager::GetInstance().Init() != DP_SUCCESS) {
153         HILOGE("SettingsDataManager init failed");
154         return DP_SETTINGSDATA_MANAGER_INIT_FAIL;
155     }
156     if (ContentSensorManager::GetInstance().Init() != DP_SUCCESS) {
157         HILOGE("ContentSensorManager init failed");
158         return DP_CONTENT_SENSOR_MANAGER_INIT_FAIL;
159     }
160     if (BusinessEventManager::GetInstance().Init() != DP_SUCCESS) {
161         HILOGE("BusinessEventManager init failed");
162         return DP_BUSINESS_EVENT_MANAGER_INIT_FAIL;
163     }
164 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
165     SaveSwitchProfilesFromTempCache();
166 #endif // DEVICE_PROFILE_SWITCH_DISABLE
167     SaveDynamicProfilesFromTempCache();
168     isInited_ = true;
169     HILOGI("PostInit finish");
170     NotifyDeviceProfileInited();
171     return DP_SUCCESS;
172 }
173 
IsInited()174 bool DistributedDeviceProfileServiceNew::IsInited()
175 {
176     return isInited_;
177 }
178 
IsReadyIntoIdle()179 bool DistributedDeviceProfileServiceNew::IsReadyIntoIdle()
180 {
181     int32_t runningIpcCount = runningIpcCount_.load();
182     if (runningIpcCount > 0) {
183         HILOGE("ipc running, runningIpcCount=%{public}d, can't into idle!", runningIpcCount);
184         return false;
185     }
186     if (ProfileCache::GetInstance().IsDeviceOnline()) {
187         HILOGE("device online, can't into idle!");
188         return false;
189     }
190     HILOGI("ready into idle!");
191     return true;
192 }
193 
AddRunningIpcCount()194 void DistributedDeviceProfileServiceNew::AddRunningIpcCount()
195 {
196     runningIpcCount_.fetch_add(1);
197 }
198 
SubtractRunningIpcCount()199 void DistributedDeviceProfileServiceNew::SubtractRunningIpcCount()
200 {
201     runningIpcCount_.fetch_sub(1);
202 }
203 
UnInit()204 int32_t DistributedDeviceProfileServiceNew::UnInit()
205 {
206     isInited_ = false;
207     if (TrustProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
208         HILOGE("TrustProfileManager UnInit failed");
209         return DP_TRUST_PROFILE_MANAGER_UNINIT_FAIL;
210     }
211     if (ProfileDataManager::GetInstance().UnInit() != DP_SUCCESS) {
212         HILOGE("ProfileDataManager UnInit failed");
213         return DP_PROFILE_DATA_MANAGER_UNINIT_FAIL;
214     }
215 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
216     if (SwitchProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
217         HILOGE("SwitchProfileManager UnInit failed");
218         return DP_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
219     }
220 #else
221     HILOGI("this device does not support switch data");
222 #endif // DEVICE_PROFILE_SWITCH_DISABLE
223     if (DeviceProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
224         HILOGE("DeviceProfileManager UnInit failed");
225         return DP_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
226     }
227     if (StaticProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
228         HILOGE("StaticProfileManager UnInit failed");
229         return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
230     }
231     if (BusinessEventManager::GetInstance().UnInit() != DP_SUCCESS) {
232         HILOGE("BusinessEventManager UnInit failed");
233         return DP_BUSINESS_EVENT_MANAGER_UNINIT_FAIL;
234     }
235     int32_t ret = UnInitNext();
236     if (ret != DP_SUCCESS) {
237         return ret;
238     }
239     DestroyUnloadHandler();
240     ClearProfileCache();
241     return DP_SUCCESS;
242 }
243 
ExitIdleState()244 bool DistributedDeviceProfileServiceNew::ExitIdleState()
245 {
246     if (!CancelIdle()) {
247         HILOGE("Cancel idle failed!");
248         return false;
249     }
250     return true;
251 }
252 
IsStopped()253 bool DistributedDeviceProfileServiceNew::IsStopped()
254 {
255     return isStopped_.load();
256 }
257 
UnInitNext()258 int32_t DistributedDeviceProfileServiceNew::UnInitNext()
259 {
260     if (ProfileCache::GetInstance().UnInit() != DP_SUCCESS) {
261         HILOGE("ProfileCache UnInit failed");
262         return DP_CACHE_INIT_FAIL;
263     }
264     if (PermissionManager::GetInstance().UnInit() != DP_SUCCESS) {
265         HILOGE("DpDeviceManager UnInit failed");
266         return DP_DEVICE_MANAGER_UNINIT_FAIL;
267     }
268     if (SubscribeProfileManager::GetInstance().UnInit() != DP_SUCCESS) {
269         HILOGE("SubscribeProfileManager UnInit failed");
270         return DP_SUBSCRIBE_DEVICE_PROFILE_MANAGER_UNINIT_FAIL;
271     }
272     if (StaticCapabilityCollector::GetInstance().UnInit() != DP_SUCCESS) {
273         HILOGE("StaticCapabilityCollector UnInit failed");
274         return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
275     }
276     if (SettingsDataManager::GetInstance().UnInit() != DP_SUCCESS) {
277         HILOGE("SettingsDataManager unInit failed");
278         return DP_SETTINGSDATA_MANAGER_UNINIT_FAIL;
279     }
280     if (ContentSensorManager::GetInstance().UnInit() != DP_SUCCESS) {
281         HILOGE("ContentSensorManager UnInit failed");
282         return DP_CONTENT_SENSOR_MANAGER_UNINIT_FAIL;
283     }
284     if (DMAdapter::GetInstance().UnInit() != DP_SUCCESS) {
285         HILOGE("DMAdapter UnInit failed");
286         return DP_DM_ADAPTER_UNINIT_FAIL;
287     }
288     if (EventHandlerFactory::GetInstance().UnInit() != DP_SUCCESS) {
289         HILOGE("EventHandlerFactory UnInit failed");
290         return DP_CACHE_UNINIT_FAIL;
291     }
292     return DP_SUCCESS;
293 }
294 
CreateUnloadHandler()295 int32_t DistributedDeviceProfileServiceNew::CreateUnloadHandler()
296 {
297     HILOGD("call!");
298     std::lock_guard<std::mutex> lock(unloadMutex_);
299     if (unloadHandler_ == nullptr) {
300         unloadHandler_ = EventHandlerFactory::GetInstance().GetEventHandler();
301     }
302     if (unloadHandler_ == nullptr) {
303         HILOGE("UnloadHandler is nullptr!");
304         return DP_UNLOAD_HANDLER_NULLPTR;
305     }
306     return DP_SUCCESS;
307 }
308 
DestroyUnloadHandler()309 int32_t DistributedDeviceProfileServiceNew::DestroyUnloadHandler()
310 {
311     HILOGD("call!");
312     std::lock_guard<std::mutex> lock(unloadMutex_);
313     if (unloadHandler_ == nullptr) {
314         HILOGE("UnloadHandler is nullptr!");
315         return DP_UNLOAD_HANDLER_NULLPTR;
316     }
317     unloadHandler_->RemoveTask(UNLOAD_TASK_ID);
318     unloadHandler_ = nullptr;
319     return DP_SUCCESS;
320 }
321 
PutAccessControlProfile(const AccessControlProfile & accessControlProfile)322 int32_t DistributedDeviceProfileServiceNew::PutAccessControlProfile(const AccessControlProfile& accessControlProfile)
323 {
324     if (!PermissionManager::GetInstance().IsCallerTrust(PUT_ACCESS_CONTROL_PROFILE)) {
325         HILOGE("the caller is permission denied!");
326         return DP_PERMISSION_DENIED;
327     }
328     int32_t ret = TrustProfileManager::GetInstance().PutAccessControlProfile(accessControlProfile);
329     DpRadarHelper::GetInstance().ReportPutAclProfile(ret, accessControlProfile);
330     return ret;
331 }
332 
UpdateAccessControlProfile(const AccessControlProfile & accessControlProfile)333 int32_t DistributedDeviceProfileServiceNew::UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile)
334 {
335     if (!PermissionManager::GetInstance().IsCallerTrust(UPDATE_ACCESS_CONTROL_PROFILE)) {
336         HILOGE("the caller is permission denied!");
337         return DP_PERMISSION_DENIED;
338     }
339     int32_t ret = TrustProfileManager::GetInstance().UpdateAccessControlProfile(accessControlProfile);
340     DpRadarHelper::GetInstance().ReportUpdateAclProfile(ret, accessControlProfile);
341     return ret;
342 }
343 
PutProductInfoBatch(const std::vector<ProductInfo> & productInfos)344 int32_t DistributedDeviceProfileServiceNew::PutProductInfoBatch(const std::vector<ProductInfo>& productInfos)
345 {
346     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
347         HILOGE("the caller is permission denied!");
348         return DP_PERMISSION_DENIED;
349     }
350     HILOGD("CheckCallerPermission success interface PutProductInfoBatch");
351     int32_t ret = ProfileDataManager::GetInstance().PutProductInfoBatch(productInfos);
352     return ret;
353 }
354 
PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo> & deviceIconInfos)355 int32_t DistributedDeviceProfileServiceNew::PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo>& deviceIconInfos)
356 {
357     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
358         HILOGE("the caller is permission denied!");
359         return DP_PERMISSION_DENIED;
360     }
361     HILOGD("CheckCallerPermission success interface PutDeviceIconInfoBatch");
362     int32_t ret = ProfileDataManager::GetInstance().PutDeviceIconInfoBatch(deviceIconInfos);
363     return ret;
364 }
365 
GetDeviceIconInfos(const DeviceIconInfoFilterOptions & filterOptions,std::vector<DeviceIconInfo> & deviceIconInfos)366 int32_t DistributedDeviceProfileServiceNew::GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions,
367     std::vector<DeviceIconInfo>& deviceIconInfos)
368 {
369     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
370         HILOGE("the caller is permission denied!");
371         return DP_PERMISSION_DENIED;
372     }
373     HILOGD("CheckCallerPermission success interface GetDeviceIconInfos");
374     int32_t ret = ProfileDataManager::GetInstance().GetDeviceIconInfos(filterOptions, deviceIconInfos);
375     return ret;
376 }
377 
DeleteDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)378 int32_t DistributedDeviceProfileServiceNew::DeleteDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
379 {
380     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
381         HILOGE("the caller is permission denied!");
382         return DP_PERMISSION_DENIED;
383     }
384     HILOGD("CheckCallerPermission success interface DeleteDeviceProfileBatch");
385     int32_t ret = ProfileDataManager::GetInstance().DeleteDeviceProfileBatch(deviceProfiles);
386     return ret;
387 }
388 
GetTrustDeviceProfile(const std::string & deviceId,TrustDeviceProfile & trustDeviceProfile)389 int32_t DistributedDeviceProfileServiceNew::GetTrustDeviceProfile(const std::string& deviceId,
390     TrustDeviceProfile& trustDeviceProfile)
391 {
392     if (!PermissionManager::GetInstance().IsCallerTrust(GET_TRUST_DEVICE_PROFILE)) {
393         HILOGE("the caller is permission denied!");
394         return DP_PERMISSION_DENIED;
395     }
396     int32_t ret = TrustProfileManager::GetInstance().GetTrustDeviceProfile(deviceId, trustDeviceProfile);
397     DpRadarHelper::GetInstance().ReportGetTrustProfile(ret, deviceId, trustDeviceProfile);
398     return ret;
399 }
400 
GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile> & trustDeviceProfiles)401 int32_t DistributedDeviceProfileServiceNew::GetAllTrustDeviceProfile(
402     std::vector<TrustDeviceProfile>& trustDeviceProfiles)
403 {
404     if (!PermissionManager::GetInstance().IsCallerTrust(GET_ALL_TRUST_DEVICE_PROFILE)) {
405         HILOGE("the caller is permission denied!");
406         return DP_PERMISSION_DENIED;
407     }
408     int32_t ret = TrustProfileManager::GetInstance().GetAllTrustDeviceProfile(trustDeviceProfiles);
409     DpRadarHelper::GetInstance().ReportGetAllTrustProfile(ret, trustDeviceProfiles);
410     return ret;
411 }
412 
GetAccessControlProfile(std::map<std::string,std::string> queryParams,std::vector<AccessControlProfile> & accessControlProfiles)413 int32_t DistributedDeviceProfileServiceNew::GetAccessControlProfile(std::map<std::string, std::string> queryParams,
414     std::vector<AccessControlProfile>& accessControlProfiles)
415 {
416     if (!PermissionManager::GetInstance().IsCallerTrust(GET_ACCESS_CONTROL_PROFILE)) {
417         HILOGE("the caller is permission denied!");
418         return DP_PERMISSION_DENIED;
419     }
420     int32_t ret = TrustProfileManager::GetInstance().GetAccessControlProfile(queryParams, accessControlProfiles);
421     DpRadarHelper::GetInstance().ReportGetAclProfile(ret, accessControlProfiles);
422     return ret;
423 }
424 
GetAllAccessControlProfile(std::vector<AccessControlProfile> & accessControlProfiles)425 int32_t DistributedDeviceProfileServiceNew::GetAllAccessControlProfile(
426     std::vector<AccessControlProfile>& accessControlProfiles)
427 {
428     if (!PermissionManager::GetInstance().IsCallerTrust(GET_ALL_ACCESS_CONTROL_PROFILE)) {
429         HILOGE("the caller is permission denied!");
430         return DP_PERMISSION_DENIED;
431     }
432     int32_t ret = TrustProfileManager::GetInstance().GetAllAccessControlProfile(accessControlProfiles);
433     DpRadarHelper::GetInstance().ReportGetAllAclProfile(ret, accessControlProfiles);
434     return ret;
435 }
436 
GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile> & accessControlProfiles)437 int32_t DistributedDeviceProfileServiceNew::GetAllAclIncludeLnnAcl(
438     std::vector<AccessControlProfile>& accessControlProfiles)
439 {
440     if (!PermissionManager::GetInstance().IsCallerTrust(GET_ALL_ACL_INCLUDE_LNN_ACL)) {
441         HILOGE("the caller is permission denied!");
442         return DP_PERMISSION_DENIED;
443     }
444     int32_t ret = TrustProfileManager::GetInstance().GetAllAclIncludeLnnAcl(accessControlProfiles);
445     DpRadarHelper::GetInstance().ReportGetAllAclProfile(ret, accessControlProfiles);
446     return ret;
447 }
448 
DeleteAccessControlProfile(int32_t accessControlId)449 int32_t DistributedDeviceProfileServiceNew::DeleteAccessControlProfile(int32_t accessControlId)
450 {
451     if (!PermissionManager::GetInstance().IsCallerTrust(DELETE_ACCESS_CONTROL_PROFILE)) {
452         HILOGE("the caller is permission denied!");
453         return DP_PERMISSION_DENIED;
454     }
455     int32_t ret = TrustProfileManager::GetInstance().DeleteAccessControlProfile(accessControlId);
456     DpRadarHelper::GetInstance().ReportDeleteAclProfile(ret);
457     return ret;
458 }
459 
PutSessionKey(uint32_t userId,const std::vector<uint8_t> & sessionKey,int32_t & sessionKeyId)460 int32_t DistributedDeviceProfileServiceNew::PutSessionKey(uint32_t userId,
461     const std::vector<uint8_t>& sessionKey, int32_t& sessionKeyId)
462 {
463     if (!PermissionManager::GetInstance().IsCallerTrust(PUT_SESSION_KEY)) {
464         HILOGE("the caller is permission denied!");
465         return DP_PERMISSION_DENIED;
466     }
467     int32_t ret = SessionKeyManager::GetInstance().PutSessionKey(userId, sessionKey, sessionKeyId);
468     return ret;
469 }
470 
GetSessionKey(uint32_t userId,int32_t sessionKeyId,std::vector<uint8_t> & sessionKey)471 int32_t DistributedDeviceProfileServiceNew::GetSessionKey(uint32_t userId,
472     int32_t sessionKeyId, std::vector<uint8_t>& sessionKey)
473 {
474     if (!PermissionManager::GetInstance().IsCallerTrust(GET_SESSION_KEY)) {
475         HILOGE("the caller is permission denied!");
476         return DP_PERMISSION_DENIED;
477     }
478     int32_t ret = SessionKeyManager::GetInstance().GetSessionKey(userId, sessionKeyId, sessionKey);
479     return ret;
480 }
481 
UpdateSessionKey(uint32_t userId,int32_t sessionKeyId,const std::vector<uint8_t> & sessionKey)482 int32_t DistributedDeviceProfileServiceNew::UpdateSessionKey(
483     uint32_t userId, int32_t sessionKeyId, const std::vector<uint8_t>& sessionKey)
484 {
485     if (!PermissionManager::GetInstance().IsCallerTrust(UPDATE_SESSION_KEY)) {
486         HILOGE("the caller is permission denied!");
487         return DP_PERMISSION_DENIED;
488     }
489     int32_t ret = SessionKeyManager::GetInstance().UpdateSessionKey(userId, sessionKeyId, sessionKey);
490     return ret;
491 }
492 
DeleteSessionKey(uint32_t userId,int32_t sessionKeyId)493 int32_t DistributedDeviceProfileServiceNew::DeleteSessionKey(uint32_t userId, int32_t sessionKeyId)
494 {
495     if (!PermissionManager::GetInstance().IsCallerTrust(DELETE_SESSION_KEY)) {
496         HILOGE("the caller is permission denied!");
497         return DP_PERMISSION_DENIED;
498     }
499     int32_t ret = SessionKeyManager::GetInstance().DeleteSessionKey(userId, sessionKeyId);
500     return ret;
501 }
502 
PutDeviceProfileBatch(std::vector<DeviceProfile> & deviceProfiles)503 int32_t DistributedDeviceProfileServiceNew::PutDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles)
504 {
505     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
506         HILOGE("the caller is permission denied!");
507         return DP_PERMISSION_DENIED;
508     }
509     HILOGD("CheckCallerPermission success interface PutDeviceProfileBatch");
510     int32_t ret = ProfileDataManager::GetInstance().PutDeviceProfileBatch(deviceProfiles);
511     return ret;
512 }
513 
PutServiceProfile(const ServiceProfile & serviceProfile)514 int32_t DistributedDeviceProfileServiceNew::PutServiceProfile(const ServiceProfile& serviceProfile)
515 {
516     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
517         HILOGE("the caller is permission denied!");
518         return DP_PERMISSION_DENIED;
519     }
520     HILOGD("CheckCallerPermission success interface PutServiceProfile");
521     if (!IsInited()) {
522         return AddSvrProfilesToCache({ serviceProfile });
523     }
524     int32_t ret = DeviceProfileManager::GetInstance().PutServiceProfile(serviceProfile);
525     DpRadarHelper::GetInstance().ReportPutServiceProfile(ret, serviceProfile);
526     return ret;
527 }
528 
PutServiceProfileBatch(const std::vector<ServiceProfile> & serviceProfiles)529 int32_t DistributedDeviceProfileServiceNew::PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles)
530 {
531     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
532         HILOGE("the caller is permission denied!");
533         return DP_PERMISSION_DENIED;
534     }
535     HILOGD("CheckCallerPermission success interface PutServiceProfileBatch");
536     if (!IsInited()) {
537         return AddSvrProfilesToCache(serviceProfiles);
538     }
539     int32_t ret = DeviceProfileManager::GetInstance().PutServiceProfileBatch(serviceProfiles);
540     DpRadarHelper::GetInstance().ReportPutServiceProfileBatch(ret, serviceProfiles);
541     return ret;
542 }
543 
PutServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)544 int32_t DistributedDeviceProfileServiceNew::PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
545 {
546     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
547         HILOGE("the caller is permission denied!");
548         return DP_PERMISSION_DENIED;
549     }
550     HILOGD("CheckCallerPermission success interface PutServiceInfoProfile");
551     int32_t ret = ServiceInfoProfileManager::GetInstance().PutServiceInfoProfile(serviceInfoProfile);
552     if (ret == DP_SERVICE_INFO_PROFILE_EXISTS) {
553         ret = DP_SUCCESS;
554     }
555     return ret;
556 }
557 
DeleteServiceInfoProfile(const ServiceInfoUniqueKey & key)558 int32_t DistributedDeviceProfileServiceNew::DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key)
559 {
560     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
561         HILOGE("the caller is permission denied!");
562         return DP_PERMISSION_DENIED;
563     }
564     HILOGD("CheckCallerPermission success interface DeleteServiceInfoProfile");
565     int32_t ret = ServiceInfoProfileManager::GetInstance().DeleteServiceInfoProfile(key);
566     return ret;
567 }
568 
UpdateServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)569 int32_t DistributedDeviceProfileServiceNew::UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
570 {
571     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
572         HILOGE("the caller is permission denied!");
573         return DP_PERMISSION_DENIED;
574     }
575     HILOGD("CheckCallerPermission success interface UpdateServiceInfoProfile");
576     int32_t ret = ServiceInfoProfileManager::GetInstance().UpdateServiceInfoProfile(serviceInfoProfile);
577     return ret;
578 }
579 
GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey & key,ServiceInfoProfile & serviceInfoProfile)580 int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key,
581     ServiceInfoProfile& serviceInfoProfile)
582 {
583     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
584         HILOGE("the caller is permission denied!");
585         return DP_PERMISSION_DENIED;
586     }
587     HILOGD("CheckCallerPermission success interface GetServiceInfoProfileByUniqueKey");
588     int32_t ret = ServiceInfoProfileManager::GetInstance().GetServiceInfoProfileByUniqueKey(key, serviceInfoProfile);
589     return ret;
590 }
591 
GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)592 int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey& key,
593     std::vector<ServiceInfoProfile>& serviceInfoProfiles)
594 {
595     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
596         HILOGE("the caller is permission denied!");
597         return DP_PERMISSION_DENIED;
598     }
599     HILOGD("CheckCallerPermission success interface GetServiceInfoProfileListByTokenId");
600     int32_t ret = ServiceInfoProfileManager::GetInstance().GetServiceInfoProfileListByTokenId(key,
601         serviceInfoProfiles);
602     return ret;
603 }
604 
GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile> & serviceInfoProfiles)605 int32_t DistributedDeviceProfileServiceNew::GetAllServiceInfoProfileList(
606     std::vector<ServiceInfoProfile>& serviceInfoProfiles)
607 {
608     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
609         HILOGE("the caller is permission denied!");
610         return DP_PERMISSION_DENIED;
611     }
612     HILOGD("CheckCallerPermission success interface GetAllServiceInfoProfileList");
613     int32_t ret = ServiceInfoProfileManager::GetInstance().GetAllServiceInfoProfileList(serviceInfoProfiles);
614     return ret;
615 }
616 
GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)617 int32_t DistributedDeviceProfileServiceNew::GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey& key,
618     std::vector<ServiceInfoProfile>& serviceInfoProfiles)
619 {
620     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
621         HILOGE("the caller is permission denied!");
622         return DP_PERMISSION_DENIED;
623     }
624     HILOGD("CheckCallerPermission success interface GetServiceInfoProfileListByBundleName");
625     int32_t ret = ServiceInfoProfileManager::GetInstance().GetServiceInfoProfileListByBundleName(key,
626         serviceInfoProfiles);
627     return ret;
628 }
629 
PutCharacteristicProfile(const CharacteristicProfile & charProfile)630 int32_t DistributedDeviceProfileServiceNew::PutCharacteristicProfile(const CharacteristicProfile& charProfile)
631 {
632     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
633         HILOGE("the caller is permission denied!");
634         return DP_PERMISSION_DENIED;
635     }
636     if (!IsInited()) {
637         return AddCharProfilesToCache({ charProfile });
638     }
639     if (charProfile.GetCharacteristicKey() == SWITCH_STATUS) {
640 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
641         int32_t switchRet = SwitchProfileManager::GetInstance().PutCharacteristicProfile(charProfile);
642         DpRadarHelper::GetInstance().ReportPutCharProfile(switchRet, charProfile);
643         return switchRet;
644 #else
645         HILOGI("this device does not support switch data");
646         return DP_DEVICE_UNSUPPORTED_SWITCH;
647 #endif // DEVICE_PROFILE_SWITCH_DISABLE
648     }
649     HILOGD("CheckCallerPermission success interface DeviceProfileManager::PutCharacteristicProfile");
650     int32_t ret = DeviceProfileManager::GetInstance().PutCharacteristicProfile(charProfile);
651     DpRadarHelper::GetInstance().ReportPutCharProfile(ret, charProfile);
652     return ret;
653 }
654 
PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile> & charProfiles)655 int32_t DistributedDeviceProfileServiceNew::PutCharacteristicProfileBatch(
656     const std::vector<CharacteristicProfile>& charProfiles)
657 {
658     int32_t preprocessRes = PutCharacteristicProfileBatchPreprocess(charProfiles);
659     if (preprocessRes != DP_SUCCESS) {
660         return preprocessRes;
661     }
662     if (!IsInited()) {
663         return AddCharProfilesToCache(charProfiles);
664     }
665     std::vector<CharacteristicProfile> switchCharProfiles;
666     std::vector<CharacteristicProfile> dynamicCharProfiles;
667     for (auto& profile : charProfiles) {
668         if (profile.GetCharacteristicKey() == SWITCH_STATUS) {
669             switchCharProfiles.push_back(profile);
670             continue;
671         }
672         dynamicCharProfiles.push_back(profile);
673     }
674     int32_t switchRes = DP_SUCCESS;
675 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
676     if (switchCharProfiles.size() > 0) {
677         switchRes = SwitchProfileManager::GetInstance().PutCharacteristicProfileBatch(switchCharProfiles);
678         DpRadarHelper::GetInstance().ReportPutCharProfileBatch(switchRes, switchCharProfiles);
679     }
680     if (switchRes != DP_SUCCESS) {
681         HILOGE("PutCharacteristicProfileBatch fail, res:%{public}d", switchRes);
682     }
683 #else
684     if (dynamicCharProfiles.empty()) {
685         HILOGE("this device does not support switch data, can't put");
686         return DP_DEVICE_UNSUPPORTED_SWITCH;
687     }
688 #endif // DEVICE_PROFILE_SWITCH_DISABLE
689     int32_t dynamicRes = DP_SUCCESS;
690     if (dynamicCharProfiles.size() > 0) {
691         dynamicRes = DeviceProfileManager::GetInstance().PutCharacteristicProfileBatch(dynamicCharProfiles);
692         DpRadarHelper::GetInstance().ReportPutCharProfileBatch(dynamicRes, dynamicCharProfiles);
693     }
694     if (dynamicRes != DP_SUCCESS) {
695         HILOGE("PutCharacteristicProfileBatch fail, res:%{public}d", dynamicRes);
696     }
697     if (switchRes != DP_SUCCESS || dynamicRes != DP_SUCCESS) {
698         return DP_PUT_CHAR_BATCH_FAIL;
699     }
700     HILOGD("PutCharacteristicProfileBatch success ");
701     return DP_SUCCESS;
702 }
703 
PutCharacteristicProfileBatchPreprocess(const std::vector<CharacteristicProfile> & charProfiles)704 int32_t DistributedDeviceProfileServiceNew::PutCharacteristicProfileBatchPreprocess(
705     const std::vector<CharacteristicProfile>& charProfiles)
706 {
707     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
708         HILOGE("this caller is permission denied!");
709         return DP_PERMISSION_DENIED;
710     }
711     if (charProfiles.empty()) {
712         HILOGE("charProfiles is empty");
713         return DP_INVALID_PARAM;
714     }
715     if (charProfiles.size() > MAX_CHAR_SIZE) {
716         HILOGE("charProfiles size is too large");
717         return DP_INVALID_PARAM;
718     }
719     return DP_SUCCESS;
720 }
721 
GetDeviceProfile(const std::string & deviceId,DeviceProfile & deviceProfile)722 int32_t DistributedDeviceProfileServiceNew::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile)
723 {
724     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
725         HILOGE("this caller is permission denied!");
726         return DP_PERMISSION_DENIED;
727     }
728     HILOGD("CheckCallerPermission success interface GetDeviceProfile");
729     int32_t ret = DeviceProfileManager::GetInstance().GetDeviceProfile(deviceId, deviceProfile);
730     DpRadarHelper::GetInstance().ReportGetDeviceProfile(ret, deviceId, deviceProfile);
731     return ret;
732 }
733 
GetDeviceProfiles(DeviceProfileFilterOptions & options,std::vector<DeviceProfile> & deviceProfiles)734 int32_t DistributedDeviceProfileServiceNew::GetDeviceProfiles(DeviceProfileFilterOptions& options,
735     std::vector<DeviceProfile>& deviceProfiles)
736 {
737     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
738         HILOGE("this caller is permission denied!");
739         return DP_PERMISSION_DENIED;
740     }
741     HILOGD("CheckCallerPermission success interface GetDeviceProfiles");
742     int32_t ret = ProfileDataManager::GetInstance().GetDeviceProfiles(options, deviceProfiles);
743     return ret;
744 }
745 
GetServiceProfile(const std::string & deviceId,const std::string & serviceName,ServiceProfile & serviceProfile)746 int32_t DistributedDeviceProfileServiceNew::GetServiceProfile(const std::string& deviceId,
747     const std::string& serviceName, ServiceProfile& serviceProfile)
748 {
749     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
750         HILOGE("this caller is permission denied!");
751         return DP_PERMISSION_DENIED;
752     }
753     HILOGD("CheckCallerPermission success interface GetServiceProfile");
754     int32_t ret = DeviceProfileManager::GetInstance().GetServiceProfile(deviceId, serviceName, serviceProfile);
755     DpRadarHelper::GetInstance().ReportGetServiceProfile(ret, deviceId, serviceProfile);
756     return ret;
757 }
758 
GetCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)759 int32_t DistributedDeviceProfileServiceNew::GetCharacteristicProfile(const std::string& deviceId,
760     const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile)
761 {
762     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
763         HILOGE("this caller is permission denied!");
764         return DP_PERMISSION_DENIED;
765     }
766     if (characteristicKey == SWITCH_STATUS) {
767 #ifndef DEVICE_PROFILE_SWITCH_DISABLE
768         HILOGD("CheckCallerPermission success interface SwitchProfileManager::GetCharacteristicProfile");
769         int32_t switchRet = SwitchProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
770             characteristicKey, charProfile);
771         DpRadarHelper::GetInstance().ReportGetCharProfile(switchRet, deviceId, charProfile);
772         return switchRet;
773 #else
774         HILOGI("this device does not support switch data");
775         return DP_DEVICE_UNSUPPORTED_SWITCH;
776 #endif
777     }
778     if (characteristicKey == STATIC_CHARACTERISTIC_KEY) {
779         HILOGD("CheckCallerPermission success interface StaticProfileManager::GetCharacteristicProfile");
780         int32_t staticRet = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
781             characteristicKey, charProfile);
782         DpRadarHelper::GetInstance().ReportGetCharProfile(staticRet, deviceId, charProfile);
783         return staticRet;
784     }
785     HILOGD("CheckCallerPermission success interface DeviceProfileManager::GetCharacteristicProfile");
786     int32_t ret = DeviceProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
787         characteristicKey, charProfile);
788     DpRadarHelper::GetInstance().ReportGetCharProfile(ret, deviceId, charProfile);
789     return ret;
790 }
791 
DeleteServiceProfile(const std::string & deviceId,const std::string & serviceName,bool isMultiUser,int32_t userId)792 int32_t DistributedDeviceProfileServiceNew::DeleteServiceProfile(const std::string& deviceId,
793     const std::string& serviceName, bool isMultiUser, int32_t userId)
794 {
795     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
796         HILOGE("this caller is permission denied!");
797         return DP_PERMISSION_DENIED;
798     }
799     HILOGD("CheckCallerPermission success interface DeleteServiceProfile");
800     int32_t ret = DeviceProfileManager::GetInstance().DeleteServiceProfile(deviceId, serviceName, isMultiUser, userId);
801     DpRadarHelper::GetInstance().ReportDeleteServiceProfile(ret, deviceId);
802     return ret;
803 }
804 
DeleteCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,bool isMultiUser,int32_t userId)805 int32_t DistributedDeviceProfileServiceNew::DeleteCharacteristicProfile(const std::string& deviceId,
806     const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser, int32_t userId)
807 {
808     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
809         HILOGE("this caller is permission denied!");
810         return DP_PERMISSION_DENIED;
811     }
812     HILOGD("CheckCallerPermission success interface DeleteCharacteristicProfile");
813     int32_t ret = DeviceProfileManager::GetInstance().DeleteCharacteristicProfile(deviceId, serviceName,
814         characteristicKey, isMultiUser, userId);
815     DpRadarHelper::GetInstance().ReportDeleteCharProfile(ret, deviceId);
816     return ret;
817 }
818 
PutLocalServiceInfo(const LocalServiceInfo & localServiceInfo)819 int32_t DistributedDeviceProfileServiceNew::PutLocalServiceInfo(const LocalServiceInfo& localServiceInfo)
820 {
821     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
822         HILOGE("the caller is permission denied!");
823         return DP_PERMISSION_DENIED;
824     }
825     HILOGD("CheckCallerPermission success interface PutLocalServiceInfo");
826     int32_t ret = LocalServiceInfoManager::GetInstance().PutLocalServiceInfo(localServiceInfo);
827     if (ret == DP_SUCCESS && localServiceInfo.GetPinCode() == INVALID_PINCODE) {
828         HILOGI("NotifyPinCodeInvalid, serviceInfoProfile:%{public}s", localServiceInfo.dump().c_str());
829         NotifyPinCodeInvalid(localServiceInfo);
830     }
831     return ret;
832 }
833 
UpdateLocalServiceInfo(const LocalServiceInfo & localServiceInfo)834 int32_t DistributedDeviceProfileServiceNew::UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo)
835 {
836     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
837         HILOGE("the caller is permission denied!");
838         return DP_PERMISSION_DENIED;
839     }
840     HILOGD("CheckCallerPermission success interface UpdateLocalServiceInfo");
841     int32_t ret = LocalServiceInfoManager::GetInstance().UpdateLocalServiceInfo(localServiceInfo);
842     if (ret == DP_SUCCESS && localServiceInfo.GetPinCode() == INVALID_PINCODE) {
843         HILOGI("NotifyPinCodeInvalid, serviceInfoProfile:%{public}s", localServiceInfo.dump().c_str());
844         NotifyPinCodeInvalid(localServiceInfo);
845     }
846     return ret;
847 }
848 
GetLocalServiceInfoByBundleAndPinType(const std::string & bundleName,int32_t pinExchangeType,LocalServiceInfo & localServiceInfo)849 int32_t DistributedDeviceProfileServiceNew::GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName,
850     int32_t pinExchangeType, LocalServiceInfo& localServiceInfo)
851 {
852     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
853         HILOGE("the caller is permission denied!");
854         return DP_PERMISSION_DENIED;
855     }
856     HILOGD("CheckCallerPermission success interface GetLocalServiceInfoByBundleAndPinType");
857     int32_t ret = LocalServiceInfoManager::GetInstance().GetLocalServiceInfoByBundleAndPinType(bundleName,
858         pinExchangeType, localServiceInfo);
859     return ret;
860 }
861 
DeleteLocalServiceInfo(const std::string & bundleName,int32_t pinExchangeType)862 int32_t DistributedDeviceProfileServiceNew::DeleteLocalServiceInfo(const std::string& bundleName,
863     int32_t pinExchangeType)
864 {
865     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
866         HILOGE("the caller is permission denied!");
867         return DP_PERMISSION_DENIED;
868     }
869     HILOGD("CheckCallerPermission success interface DeleteLocalServiceInfo");
870     int32_t ret = LocalServiceInfoManager::GetInstance().DeleteLocalServiceInfo(bundleName, pinExchangeType);
871     return ret;
872 }
873 
SubscribeDeviceProfile(const SubscribeInfo & subscriberInfo)874 int32_t DistributedDeviceProfileServiceNew::SubscribeDeviceProfile(const SubscribeInfo& subscriberInfo)
875 {
876     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
877         HILOGE("this caller is permission denied!");
878         return DP_PERMISSION_DENIED;
879     }
880     HILOGD("CheckCallerPermission success interface SubscribeDeviceProfile");
881     int32_t ret = SubscribeProfileManager::GetInstance().SubscribeDeviceProfile(subscriberInfo);
882     DpRadarHelper::GetInstance().ReportSubscribeDeviceProfile(ret, subscriberInfo);
883     return ret;
884 }
885 
UnSubscribeDeviceProfile(const SubscribeInfo & subscriberInfo)886 int32_t DistributedDeviceProfileServiceNew::UnSubscribeDeviceProfile(const SubscribeInfo& subscriberInfo)
887 {
888     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
889         HILOGE("this caller is permission denied!");
890         return DP_PERMISSION_DENIED;
891     }
892     HILOGD("CheckCallerPermission success interface UnSubscribeDeviceProfile");
893     int32_t ret = SubscribeProfileManager::GetInstance().UnSubscribeDeviceProfile(subscriberInfo);
894     DpRadarHelper::GetInstance().ReportUnSubscribeDeviceProfile(ret, subscriberInfo);
895     return ret;
896 }
897 
SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions & syncOptions,sptr<IRemoteObject> syncCompletedCallback)898 int32_t DistributedDeviceProfileServiceNew::SyncDeviceProfile(
899     const DistributedDeviceProfile::DpSyncOptions& syncOptions, sptr<IRemoteObject> syncCompletedCallback)
900 {
901     if (!PermissionManager::GetInstance().CheckCallerSyncPermission()) {
902         HILOGE("this caller is permission denied!");
903         return DP_PERMISSION_DENIED;
904     }
905     HILOGD("CheckCallerSyncPermission success interface SyncDeviceProfile");
906     int32_t ret = DeviceProfileManager::GetInstance().SyncDeviceProfile(syncOptions, syncCompletedCallback);
907     DpRadarHelper::GetInstance().ReportSyncDeviceProfile(ret);
908     return ret;
909 }
910 
SendSubscribeInfos(std::map<std::string,SubscribeInfo> listenerMap)911 int32_t DistributedDeviceProfileServiceNew::SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap)
912 {
913     return SubscribeProfileManager::GetInstance().SubscribeDeviceProfile(listenerMap);
914 }
915 
Dump(int32_t fd,const std::vector<std::u16string> & args)916 int32_t DistributedDeviceProfileServiceNew::Dump(int32_t fd, const std::vector<std::u16string>& args)
917 {
918     if (args.size() > MAX_DUMP_ARGS_SIZE) {
919         HILOGE("args size is too large");
920         return DP_INVALID_PARAMS;
921     }
922     std::vector<std::string> argsInStr8;
923     for (const auto& arg : args) {
924         argsInStr8.emplace_back(Str16ToStr8(arg));
925     }
926 
927     std::string result;
928     DeviceProfileDumper::Dump(argsInStr8, result);
929 
930     if (!SaveStringToFd(fd, result)) {
931         HILOGE("save to fd failed");
932         return DP_INVALID_PARAMS;
933     }
934     return DP_SUCCESS;
935 }
936 
DelayUnloadTask()937 void DistributedDeviceProfileServiceNew::DelayUnloadTask()
938 {
939     HILOGD("delay unload task begin");
940     auto task = []() {
941         HILOGD("do unload task");
942         if (ProfileCache::GetInstance().IsDeviceOnline()) {
943             HILOGI("already device online in 3 min, not kill!");
944             return;
945         }
946         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
947         if (samgrProxy == nullptr) {
948             HILOGE("get samgr failed");
949             return;
950         }
951         int32_t unloadResult = samgrProxy->UnloadSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID);
952         if (unloadResult != DP_SUCCESS) {
953             HILOGE("remove system ability failed");
954             return;
955         }
956         HILOGI("kill dp svr success!");
957     };
958     {
959         std::lock_guard<std::mutex> lock(unloadMutex_);
960         if (unloadHandler_ == nullptr) {
961             HILOGE("unloadHandler is nullptr ");
962             return;
963         }
964         unloadHandler_->RemoveTask(UNLOAD_TASK_ID);
965         if (ProfileCache::GetInstance().IsDeviceOnline()) {
966             HILOGI("already device online, not kill!");
967             return;
968         }
969         HILOGI("delay unload task post task");
970         unloadHandler_->PostTask(task, UNLOAD_TASK_ID, DELAY_TIME);
971     }
972 }
973 
OnStart(const SystemAbilityOnDemandReason & startReason)974 void DistributedDeviceProfileServiceNew::OnStart(const SystemAbilityOnDemandReason& startReason)
975 {
976     HILOGI("start reason %{public}s", startReason.GetName().c_str());
977     if (Init() != DP_SUCCESS) {
978         HILOGE("init failed");
979         return;
980     }
981     AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
982     AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
983     AddSystemAbilityListener(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
984     AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
985     IPCSkeleton::SetMaxWorkThreadNum(DP_IPC_THREAD_NUM);
986     if (!Publish(this)) {
987         HILOGE("publish SA failed");
988         return;
989     }
990     if (CreateUnloadHandler() == DP_SUCCESS) {
991         HILOGI("CreateUnloadHandler success!");
992         DelayUnloadTask();
993     }
994 }
995 
OnStop()996 void DistributedDeviceProfileServiceNew::OnStop()
997 {
998     HILOGI("call");
999     isStopped_ = true;
1000     int32_t ret = UnInit();
1001     HILOGI("UnInit ret=%{public}d", ret);
1002 }
1003 
OnActive(const SystemAbilityOnDemandReason & activeReason)1004 void DistributedDeviceProfileServiceNew::OnActive(const SystemAbilityOnDemandReason& activeReason)
1005 {
1006     HILOGI("active reason %{public}d", activeReason.GetId());
1007 }
1008 
OnIdle(const SystemAbilityOnDemandReason & idleReason)1009 int32_t DistributedDeviceProfileServiceNew::OnIdle(const SystemAbilityOnDemandReason& idleReason)
1010 {
1011     HILOGI("idleReason name=%{public}s, id=%{public}d, value=%{public}s", idleReason.GetName().c_str(),
1012         idleReason.GetId(), idleReason.GetValue().c_str());
1013     if (idleReason.GetName() == IDLE_REASON_LOW_MEMORY) {
1014         return IsReadyIntoIdle() ? SA_READY_INTO_IDLE : SA_REFUSE_INTO_IDLE;
1015     }
1016     return SA_READY_INTO_IDLE;
1017 }
1018 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1019 void DistributedDeviceProfileServiceNew::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
1020 {
1021     HILOGI("called systemAbilityId:%{public}d", systemAbilityId);
1022     if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
1023         SubscribeAccountCommonEvent();
1024     }
1025     if (IsInited()) {
1026         return;
1027     }
1028     {
1029         std::lock_guard<std::mutex> lock(depSaIdsMtx_);
1030         if (depSaIds_.empty()) {
1031             return;
1032         }
1033         depSaIds_.erase(systemAbilityId);
1034         if (!depSaIds_.empty()) {
1035             return;
1036         }
1037     }
1038     PostInit();
1039 }
1040 
SubscribeAccountCommonEvent()1041 void DistributedDeviceProfileServiceNew::SubscribeAccountCommonEvent()
1042 {
1043     HILOGI("Start");
1044     {
1045         std::lock_guard<std::mutex> lock(accountCommonEventManagerMtx_);
1046         if (accountCommonEventManager_ == nullptr) {
1047             accountCommonEventManager_ = std::make_shared<DpAccountCommonEventManager>();
1048         }
1049     }
1050     AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
1051         this->AccountCommonEventCallback(arg1, arg2);
1052     };
1053     std::vector<std::string> AccountCommonEventVec;
1054     AccountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
1055     AccountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
1056     std::lock_guard<std::mutex> lock(accountCommonEventManagerMtx_);
1057     if (accountCommonEventManager_->SubscribeAccountCommonEvent(AccountCommonEventVec, callback)) {
1058         HILOGI("Success");
1059     }
1060     return;
1061 }
1062 
AccountCommonEventCallback(int32_t userId,const std::string commonEventType)1063 void DistributedDeviceProfileServiceNew::AccountCommonEventCallback(int32_t userId, const std::string commonEventType)
1064 {
1065     HILOGI("CommonEventType: %{public}s, userId: %{public}d", commonEventType.c_str(), userId);
1066     if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
1067         // swithed
1068         MultiUserManager::GetInstance().SetCurrentForegroundUserID(userId);
1069         if (ContentSensorManager::GetInstance().Init() != DP_SUCCESS) {
1070             HILOGE("ContentSensorManager init failed");
1071             return;
1072         }
1073         return;
1074     }
1075     if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
1076         // removed
1077         if (DeviceProfileManager::GetInstance().DeleteRemovedUserData(userId) != DP_SUCCESS) {
1078             HILOGE("DeleteRemovedUserData failed,userId=%{public}d", userId);
1079         } else {
1080             HILOGI("DeleteRemovedUserData succeed,userId=%{public}d", userId);
1081         }
1082         return;
1083     }
1084     HILOGE("Invalied account common event.");
1085     return;
1086 }
1087 
AddSvrProfilesToCache(const std::vector<ServiceProfile> & serviceProfiles)1088 int32_t DistributedDeviceProfileServiceNew::AddSvrProfilesToCache(const std::vector<ServiceProfile>& serviceProfiles)
1089 {
1090     if (serviceProfiles.empty()) {
1091         HILOGE("serviceProfiles empty");
1092         return DP_INVALID_PARAM;
1093     }
1094     HILOGI("serviceProfiles.size:%{public}zu", serviceProfiles.size());
1095     std::map<std::string, std::string> entries;
1096     for (const auto& item : serviceProfiles) {
1097         if (!ProfileUtils::IsSvrProfileValid(item)) {
1098             HILOGE("the is invalid, serviceProfile:%{public}s", item.dump().c_str());
1099             return DP_INVALID_PARAM;
1100         }
1101         if (ProfileCache::GetInstance().IsServiceProfileExist(item)) {
1102             HILOGW("the profile is exist!, serviceProfile:%{public}s", item.dump().c_str());
1103             return DP_CACHE_EXIST;
1104         }
1105         ProfileUtils::ServiceProfileToEntries(item, entries);
1106     }
1107     std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
1108     for (const auto& [key, value] : entries) {
1109         dynamicProfileMap_[key] = value;
1110     }
1111     return DP_SUCCESS;
1112 }
1113 
AddCharProfilesToCache(const std::vector<CharacteristicProfile> & charProfiles)1114 int32_t DistributedDeviceProfileServiceNew::AddCharProfilesToCache(
1115     const std::vector<CharacteristicProfile>& charProfiles)
1116 {
1117     if (charProfiles.empty()) {
1118         HILOGE("charProfiles empty");
1119         return DP_INVALID_PARAM;
1120     }
1121     HILOGI("charProfiles.size:%{public}zu", charProfiles.size());
1122     std::vector<CharacteristicProfile> switchCharProfiles;
1123     std::map<std::string, std::string> entries;
1124     for (const auto& item : charProfiles) {
1125         if (!ProfileUtils::IsCharProfileValid(item)) {
1126             HILOGE("the is invalid, charProfile:%{public}s", item.dump().c_str());
1127             return DP_INVALID_PARAM;
1128         }
1129         if (ProfileCache::GetInstance().IsCharProfileExist(item)) {
1130             HILOGW("the profile is exist!, charProfile:%{public}s", item.dump().c_str());
1131             return DP_CACHE_EXIST;
1132         }
1133         if (item.GetCharacteristicKey() == SWITCH_STATUS) {
1134             switchCharProfiles.push_back(item);
1135             continue;
1136         }
1137         ProfileUtils::CharacteristicProfileToEntries(item, entries);
1138     }
1139     if (!entries.empty()) {
1140         HILOGI("entries.size:%{public}zu", entries.size());
1141         std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
1142         for (const auto& [key, value] : entries) {
1143             dynamicProfileMap_[key] = value;
1144         }
1145     }
1146     if (!switchCharProfiles.empty()) {
1147         HILOGI("switchCharProfiles.size:%{public}zu", switchCharProfiles.size());
1148         std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
1149         for (const auto& item : charProfiles) {
1150             std::string profileKey = ProfileUtils::GenerateCharProfileKey(item.GetDeviceId(),
1151                 item.GetServiceName(), item.GetCharacteristicKey());
1152             switchProfileMap_[profileKey] = item;
1153         }
1154     }
1155     return DP_SUCCESS;
1156 }
1157 
SaveSwitchProfilesFromTempCache()1158 int32_t DistributedDeviceProfileServiceNew::SaveSwitchProfilesFromTempCache()
1159 {
1160     std::vector<CharacteristicProfile> switchCharProfiles;
1161     {
1162         std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
1163         if (switchProfileMap_.empty()) {
1164             HILOGW("switchProfileMap empty");
1165             return DP_SUCCESS;
1166         }
1167         HILOGI("switchProfileMap.size:%{public}zu", switchProfileMap_.size());
1168         for (const auto& [profileKey, item] : switchProfileMap_) {
1169             switchCharProfiles.emplace_back(item);
1170         }
1171         switchProfileMap_.clear();
1172     }
1173     int32_t ret = SwitchProfileManager::GetInstance().PutCharacteristicProfileBatch(switchCharProfiles);
1174     DpRadarHelper::GetInstance().ReportPutCharProfileBatch(ret, switchCharProfiles);
1175     if (ret != DP_SUCCESS) {
1176         HILOGE("PutCharacteristicProfileBatch fail, ret: %{public}d!", ret);
1177     }
1178     return ret;
1179 }
1180 
GetDynamicProfilesFromTempCache(std::map<std::string,std::string> & entries)1181 void DistributedDeviceProfileServiceNew::GetDynamicProfilesFromTempCache(
1182     std::map<std::string, std::string>& entries)
1183 {
1184     {
1185         std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
1186         if (dynamicProfileMap_.empty()) {
1187             HILOGW("dynamicProfileMap empty!");
1188             return;
1189         }
1190         for (const auto& [key, value] : dynamicProfileMap_) {
1191             entries[key] = value;
1192         }
1193     }
1194 }
1195 
SaveDynamicProfilesFromTempCache()1196 int32_t DistributedDeviceProfileServiceNew::SaveDynamicProfilesFromTempCache()
1197 {
1198     std::map<std::string, std::string> entries;
1199     GetDynamicProfilesFromTempCache(entries);
1200     if (entries.empty() && DeviceProfileManager::GetInstance().IsFirstInitDB()) {
1201         HILOGW("entries empty!");
1202         sleep(WAIT_BUSINESS_PUT_TIME_S);
1203         GetDynamicProfilesFromTempCache(entries);
1204     }
1205     HILOGI("entries.size:%{public}zu", entries.size());
1206     int32_t tryTimes = WRTE_CACHE_PROFILE_RETRY_TIMES;
1207     while (tryTimes > 0) {
1208         if (DeviceProfileManager::GetInstance().SavePutTempCache(entries) == DP_SUCCESS) {
1209             break;
1210         }
1211         HILOGW("SavePutTempCache fail! leftTimes:%{public}d", tryTimes);
1212         usleep(WRTE_CACHE_PROFILE_DELAY_TIME_US);
1213         tryTimes--;
1214     }
1215     if (tryTimes <= 0) {
1216         DeviceProfileManager::GetInstance().ResetFirst();
1217     }
1218     {
1219         std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
1220         dynamicProfileMap_.clear();
1221     }
1222     return DP_SUCCESS;
1223 }
1224 
ClearProfileCache()1225 void DistributedDeviceProfileServiceNew::ClearProfileCache()
1226 {
1227     {
1228         std::lock_guard<std::mutex> lock(dynamicProfileMapMtx_);
1229         dynamicProfileMap_.clear();
1230     }
1231     {
1232         std::lock_guard<std::mutex> lock(switchProfileMapMtx_);
1233         switchProfileMap_.clear();
1234     }
1235 }
1236 
SubscribeDeviceProfileInited(int32_t saId,sptr<IRemoteObject> dpInitedCallback)1237 int32_t DistributedDeviceProfileServiceNew::SubscribeDeviceProfileInited(int32_t saId,
1238     sptr <IRemoteObject> dpInitedCallback)
1239 {
1240     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1241         HILOGE("this caller is permission denied!");
1242         return DP_PERMISSION_DENIED;
1243     }
1244     if (dpInitedCallback == nullptr) {
1245         HILOGE("dpInitedCallback is nullptr");
1246         return DP_INVALID_PARAM;
1247     }
1248     if (saId <= 0 || saId > MAX_SAID) {
1249         HILOGE("saId is invalid, saId:%{public}d", saId);
1250         return DP_INVALID_PARAM;
1251     }
1252     if (isInited_.load()) {
1253         HILOGI("deviceProfile service is already inited.");
1254         sptr<IDpInitedCallback> callbackProxy = iface_cast<IDpInitedCallback>(dpInitedCallback);
1255         if (callbackProxy == nullptr) {
1256             HILOGE("Cast to IDpInitedCallback failed!");
1257             return DP_NULLPTR;
1258         }
1259         callbackProxy->OnDpInited();
1260     }
1261     std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
1262     dpInitedCallbackMap_[saId] = dpInitedCallback;
1263     return DP_SUCCESS;
1264 }
1265 
UnSubscribeDeviceProfileInited(int32_t saId)1266 int32_t DistributedDeviceProfileServiceNew::UnSubscribeDeviceProfileInited(int32_t saId)
1267 {
1268     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1269         HILOGE("this caller is permission denied!");
1270         return DP_PERMISSION_DENIED;
1271     }
1272     std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
1273     dpInitedCallbackMap_.erase(saId);
1274     return DP_SUCCESS;
1275 }
1276 
SubscribePinCodeInvalid(const std::string & bundleName,int32_t pinExchangeType,sptr<IRemoteObject> pinCodeCallback)1277 int32_t DistributedDeviceProfileServiceNew::SubscribePinCodeInvalid(const std::string& bundleName,
1278     int32_t pinExchangeType, sptr<IRemoteObject> pinCodeCallback)
1279 {
1280     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1281         HILOGE("this caller is permission denied!");
1282         return DP_PERMISSION_DENIED;
1283     }
1284     if (bundleName.empty()) {
1285         HILOGE("bundleName is invalid");
1286         return DP_INVALID_PARAM;
1287     }
1288     if (pinExchangeType == DEFAULT_PIN_EXCHANGE_TYPE) {
1289         HILOGE("pinExchangeType is invalid");
1290         return DP_INVALID_PARAM;
1291     }
1292     if (pinCodeCallback == nullptr) {
1293         HILOGE("pinCodeCallback is nullptr");
1294         return DP_INVALID_PARAM;
1295     }
1296     std::lock_guard<std::mutex> lock(pinCodeCallbackMapMtx_);
1297     pinCodeCallbackMap_[std::make_pair(bundleName, pinExchangeType)] = pinCodeCallback;
1298     return DP_SUCCESS;
1299 }
1300 
UnSubscribePinCodeInvalid(const std::string & bundleName,int32_t pinExchangeType)1301 int32_t DistributedDeviceProfileServiceNew::UnSubscribePinCodeInvalid(const std::string& bundleName,
1302     int32_t pinExchangeType)
1303 {
1304     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1305         HILOGE("this caller is permission denied!");
1306         return DP_PERMISSION_DENIED;
1307     }
1308     if (bundleName.empty()) {
1309         HILOGE("bundleName is invalid");
1310         return DP_INVALID_PARAM;
1311     }
1312     if (pinExchangeType == DEFAULT_PIN_EXCHANGE_TYPE) {
1313         HILOGE("pinExchangeType is invalid");
1314         return DP_INVALID_PARAM;
1315     }
1316     std::lock_guard<std::mutex> lock(pinCodeCallbackMapMtx_);
1317     pinCodeCallbackMap_.erase(std::make_pair(bundleName, pinExchangeType));
1318     return DP_SUCCESS;
1319 }
1320 
NotifyDeviceProfileInited()1321 int32_t DistributedDeviceProfileServiceNew::NotifyDeviceProfileInited()
1322 {
1323     std::lock_guard<std::mutex> lock(dpInitedCallbackMapMtx_);
1324     for (const auto& [saId, callback] : dpInitedCallbackMap_) {
1325         sptr<IDpInitedCallback> callbackProxy = iface_cast<IDpInitedCallback>(callback);
1326         if (callbackProxy == nullptr) {
1327             HILOGE("Cast to IDpInitedCallback failed!");
1328             continue;
1329         }
1330         callbackProxy->OnDpInited();
1331     }
1332     return DP_SUCCESS;
1333 }
1334 
NotifyPinCodeInvalid(const LocalServiceInfo & localServiceInfo)1335 int32_t DistributedDeviceProfileServiceNew::NotifyPinCodeInvalid(const LocalServiceInfo& localServiceInfo)
1336 {
1337     HILOGI("call");
1338     sptr<IPincodeInvalidCallback> callbackProxy = nullptr;
1339     {
1340         std::lock_guard<std::mutex> lock(pinCodeCallbackMapMtx_);
1341         auto item = pinCodeCallbackMap_.find({localServiceInfo.GetBundleName(),
1342             localServiceInfo.GetPinExchangeType()});
1343         if (item == pinCodeCallbackMap_.end()) {
1344             HILOGI("not subscribe PinCodeInvalid");
1345             return DP_SUCCESS;
1346         }
1347         callbackProxy = iface_cast<IPincodeInvalidCallback>(item->second);
1348         if (callbackProxy == nullptr) {
1349             HILOGE("Cast to IPincodeInvalidCallback failed!");
1350             return DP_NULLPTR;
1351         }
1352     }
1353     auto task = [callbackProxy, localServiceInfo]() {
1354         if (callbackProxy == nullptr) {
1355             HILOGI("OnPincodeInvalid task callbackProxy is nullptr");
1356             return;
1357         }
1358         callbackProxy->OnPincodeInvalid(localServiceInfo);
1359     };
1360     auto handler = EventHandlerFactory::GetInstance().GetEventHandler();
1361     HILOGI("notify");
1362     if (handler == nullptr || !handler->PostTask(task)) {
1363         HILOGE("Post OnPincodeInvalid task failed");
1364         return DP_POST_TASK_FAILED;
1365     }
1366     return DP_SUCCESS;
1367 }
1368 
PutAllTrustedDevices(const std::vector<TrustedDeviceInfo> deviceInfos)1369 int32_t DistributedDeviceProfileServiceNew::PutAllTrustedDevices(const std::vector<TrustedDeviceInfo> deviceInfos)
1370 {
1371     if (!PermissionManager::GetInstance().IsCallerTrust(PUT_ALL_TRUSTED_DEVICES)) {
1372         HILOGE("this caller is permission denied!");
1373         return DP_PERMISSION_DENIED;
1374     }
1375     return ProfileCache::GetInstance().AddAllTrustedDevices(deviceInfos);
1376 }
1377 
RegisterBusinessCallback(const std::string & saId,const std::string & businessKey,sptr<IRemoteObject> businessCallback)1378 int32_t DistributedDeviceProfileServiceNew::RegisterBusinessCallback(const std::string& saId,
1379     const std::string& businessKey, sptr<IRemoteObject> businessCallback)
1380 {
1381     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1382         HILOGE("the caller is permission denied!");
1383         return DP_PERMISSION_DENIED;
1384     }
1385     if (saId.empty() || businessKey.empty() || businessCallback == nullptr) {
1386         HILOGE("Invalid parameters: saId or businessKey is empty, or businessCallback is nullptr");
1387         return DP_INVALID_PARAM;
1388     }
1389 
1390     sptr<IBusinessCallback> callbackProxy = iface_cast<IBusinessCallback>(businessCallback);
1391     if (callbackProxy == nullptr) {
1392         HILOGE("Cast to IBusinessCallback failed!");
1393         return DP_NULLPTR;
1394     }
1395     std::lock_guard<std::mutex> lock(businessEventCallbackMapMtx_);
1396     if (businessCallbackMap_.find({saId, businessKey}) != businessCallbackMap_.end()) {
1397         HILOGE("saId and businessKey is exist, saId : %{public}s, businessKey: %{public}s",
1398             saId.c_str(), businessKey.c_str());
1399         return DP_INVALID_PARAM;
1400     }
1401     businessCallbackMap_[std::make_pair(saId, businessKey)] = businessCallback;
1402 
1403     return DP_SUCCESS;
1404 }
1405 
UnRegisterBusinessCallback(const std::string & saId,const std::string & businessKey)1406 int32_t DistributedDeviceProfileServiceNew::UnRegisterBusinessCallback(const std::string& saId, const std::string&
1407     businessKey)
1408 {
1409     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1410         HILOGE("the caller is permission denied!");
1411         return DP_PERMISSION_DENIED;
1412     }
1413     if (saId.empty() || businessKey.empty()) {
1414         HILOGE("Invalid parameters: saId or businessKey is empty");
1415         return DP_INVALID_PARAM;
1416     }
1417     std::lock_guard<std::mutex> lock(businessEventCallbackMapMtx_);
1418     auto it = businessCallbackMap_.find(std::make_pair(saId, businessKey));
1419     if (it == businessCallbackMap_.end()) {
1420         HILOGE("Callback not found for saId: %{public}s, businessKey: %{public}s", saId.c_str(), businessKey.c_str());
1421         return DP_INVALID_PARAM;
1422     }
1423     businessCallbackMap_.erase(std::make_pair(saId, businessKey));
1424     return DP_SUCCESS;
1425 }
1426 
PutBusinessEvent(const BusinessEvent & event)1427 int32_t DistributedDeviceProfileServiceNew::PutBusinessEvent(const BusinessEvent& event)
1428 {
1429     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1430         HILOGE("the caller is permission denied!");
1431         return DP_PERMISSION_DENIED;
1432     }
1433     HILOGD("CheckCallerPermission success interface PutBusinessEvent");
1434     int32_t ret = BusinessEventManager::GetInstance().PutBusinessEvent(event);
1435     if (ret != DP_SUCCESS) {
1436         HILOGE("PutBusinessEvent failed, ret: %{public}d", ret);
1437         return ret;
1438     }
1439     ret = NotifyBusinessEvent(event);
1440     if (ret != DP_SUCCESS) {
1441         HILOGE("NotifyBusinessEvent failed, BusinessKey: %{public}s, ret: %{public}d",
1442             event.GetBusinessKey().c_str(), ret);
1443         return DP_NOTIFY_BUSINESS_EVENT_FAIL;
1444     }
1445     HILOGI("NotifyBusinessEvent, BusinessEvent:%{public}s", event.dump().c_str());
1446     return DP_SUCCESS;
1447 }
1448 
GetBusinessEvent(BusinessEvent & event)1449 int32_t DistributedDeviceProfileServiceNew::GetBusinessEvent(BusinessEvent& event)
1450 {
1451     if (!PermissionManager::GetInstance().CheckCallerPermission()) {
1452         HILOGE("the caller is permission denied!");
1453         return DP_PERMISSION_DENIED;
1454     }
1455     HILOGD("CheckCallerPermission success interface GetBusinessEvent");
1456     return BusinessEventManager::GetInstance().GetBusinessEvent(event);
1457 }
1458 
NotifyBusinessEvent(const BusinessEvent & event)1459 int32_t DistributedDeviceProfileServiceNew::NotifyBusinessEvent(const BusinessEvent& event)
1460 {
1461     HILOGI("NotifyBusinessEvent called for BusinessKey: %{public}s, BusinessValue: %{public}s",
1462         event.GetBusinessKey().c_str(), event.GetBusinessValue().c_str());
1463     sptr<IBusinessCallback> callbackProxy = nullptr;
1464     std::lock_guard<std::mutex> lock(businessEventCallbackMapMtx_);
1465     for (const auto& [key, callback] : businessCallbackMap_) {
1466         if (key.second != event.GetBusinessKey()) {
1467             HILOGI("BusinessKey not find");
1468             continue;
1469         }
1470         callbackProxy = iface_cast<IBusinessCallback>(callback);
1471         if (callbackProxy == nullptr) {
1472             HILOGE("Cast to IBusinessCallback failed!");
1473             continue;
1474         }
1475         auto task = [callbackProxy, event]() {
1476             if (callbackProxy == nullptr) {
1477                 HILOGE("OnBusinessEvent task callbackProxy is nullptr");
1478                 return;
1479             }
1480             BusinessEventExt eventExt(event.GetBusinessKey(), event.GetBusinessValue());
1481             callbackProxy->OnBusinessEvent(eventExt);
1482         };
1483         auto handler = EventHandlerFactory::GetInstance().GetEventHandler();
1484         HILOGI("notify");
1485         if (handler == nullptr || !handler->PostTask(task)) {
1486             HILOGE("Post OnBusinessEvent task failed");
1487             continue;
1488         }
1489     }
1490     HILOGI("NotifyBusinessEvent task posted successfully");
1491     return DP_SUCCESS;
1492 }
1493 } // namespace DeviceProfile
1494 } // namespace OHOS
1495