• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "distributed_device_profile_service.h"
17 
18 #include "authority_manager.h"
19 #include "content_sensor_manager.h"
20 #include "device_manager.h"
21 #include "device_profile_errors.h"
22 #include "device_profile_log.h"
23 #include "device_profile_storage_manager.h"
24 #include "service_characteristic_profile.h"
25 #include "subscribe_manager.h"
26 #include "system_ability_definition.h"
27 #include "trust_group_manager.h"
28 
29 namespace OHOS {
30 namespace DeviceProfile {
31 namespace {
32 const std::string TAG = "DistributedDeviceProfileService";
33 }
34 
35 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileService);
36 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&DistributedDeviceProfileService::GetInstance());
37 
DistributedDeviceProfileService()38 DistributedDeviceProfileService::DistributedDeviceProfileService()
39     : SystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, true)
40 {
41 }
42 
Init()43 bool DistributedDeviceProfileService::Init()
44 {
45     if (!DeviceManager::GetInstance().Init()) {
46         HILOGE("DeviceManager init failed");
47         return false;
48     }
49     if (!DeviceProfileStorageManager::GetInstance().Init()) {
50         HILOGE("DeviceProfileStorageManager init failed");
51         return false;
52     }
53     if (!SubscribeManager::GetInstance().Init()) {
54         HILOGE("SubscribeManager init failed");
55         return false;
56     }
57     if (!AuthorityManager::GetInstance().Init()) {
58         HILOGE("AuthorityManager init failed");
59         return false;
60     }
61     TrustGroupManager::GetInstance().Init();
62     ContentSensorManager::GetInstance().Init();
63     HILOGI("init succeeded");
64     return true;
65 }
66 
PutDeviceProfile(const ServiceCharacteristicProfile & profile)67 int32_t DistributedDeviceProfileService::PutDeviceProfile(const ServiceCharacteristicProfile& profile)
68 {
69     if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
70         profile.GetServiceId())) {
71         return ERR_DP_PERMISSION_DENIED;
72     }
73     return DeviceProfileStorageManager::GetInstance().PutDeviceProfile(profile);
74 }
75 
GetDeviceProfile(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)76 int32_t DistributedDeviceProfileService::GetDeviceProfile(const std::string& udid, const std::string& serviceId,
77     ServiceCharacteristicProfile& profile)
78 {
79     if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_R,
80         serviceId)) {
81         return ERR_DP_PERMISSION_DENIED;
82     }
83     return DeviceProfileStorageManager::GetInstance().GetDeviceProfile(udid, serviceId, profile);
84 }
85 
DeleteDeviceProfile(const std::string & serviceId)86 int32_t DistributedDeviceProfileService::DeleteDeviceProfile(const std::string& serviceId)
87 {
88     if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
89         serviceId)) {
90         return ERR_DP_PERMISSION_DENIED;
91     }
92     return DeviceProfileStorageManager::GetInstance().DeleteDeviceProfile(serviceId);
93 }
94 
SubscribeProfileEvents(const std::list<SubscribeInfo> & subscribeInfos,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)95 int32_t DistributedDeviceProfileService::SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
96     const sptr<IRemoteObject>& profileEventNotifier,
97     std::list<ProfileEvent>& failedEvents)
98 {
99     return SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos,
100         profileEventNotifier, failedEvents);
101 }
102 
UnsubscribeProfileEvents(const std::list<ProfileEvent> & profileEvents,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)103 int32_t DistributedDeviceProfileService::UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
104     const sptr<IRemoteObject>& profileEventNotifier,
105     std::list<ProfileEvent>& failedEvents)
106 {
107     return SubscribeManager::GetInstance().UnsubscribeProfileEvents(profileEvents,
108         profileEventNotifier, failedEvents);
109 }
110 
SyncDeviceProfile(const SyncOptions & syncOptions,const sptr<IRemoteObject> & profileEventNotifier)111 int32_t DistributedDeviceProfileService::SyncDeviceProfile(const SyncOptions& syncOptions,
112     const sptr<IRemoteObject>& profileEventNotifier)
113 {
114     if (!AuthorityManager::GetInstance().CheckInterfaceAuthority("sync")) {
115         return ERR_DP_PERMISSION_DENIED;
116     }
117     return DeviceProfileStorageManager::GetInstance().SyncDeviceProfile(syncOptions, profileEventNotifier);
118 }
119 
OnStart()120 void DistributedDeviceProfileService::OnStart()
121 {
122     HILOGI("called");
123     if (!Init()) {
124         HILOGE("init failed");
125         return;
126     }
127     if (!Publish(this)) {
128         HILOGE("publish SA failed");
129         return;
130     }
131 }
132 
OnStop()133 void DistributedDeviceProfileService::OnStop()
134 {
135     HILOGI("called");
136 }
137 } // namespace DeviceProfile
138 } // namespace OHOS