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 "file_ex.h"
19 #include "string_ex.h"
20
21 #include "authority_manager.h"
22 #include "content_sensor_manager.h"
23 #include "device_profile_dumper.h"
24 #include "device_profile_errors.h"
25 #include "device_profile_log.h"
26 #include "device_profile_storage_manager.h"
27 #include "dp_device_manager.h"
28 #include "hitrace_meter.h"
29 #include "service_characteristic_profile.h"
30 #include "subscribe_manager.h"
31 #include "system_ability_definition.h"
32 #include "trust_group_manager.h"
33
34 namespace OHOS {
35 namespace DeviceProfile {
36 namespace {
37 const std::string TAG = "DistributedDeviceProfileService";
38 const std::string DP_DEVICE_SUB_TRACE = "DP_DEVICE_SUB";
39 }
40
41 IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileService);
42 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&DistributedDeviceProfileService::GetInstance());
43
DistributedDeviceProfileService()44 DistributedDeviceProfileService::DistributedDeviceProfileService()
45 : SystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, true)
46 {
47 }
48
Init()49 bool DistributedDeviceProfileService::Init()
50 {
51 if (!DpDeviceManager::GetInstance().Init()) {
52 HILOGE("DeviceManager init failed");
53 return false;
54 }
55 if (!DeviceProfileStorageManager::GetInstance().Init()) {
56 HILOGE("DeviceProfileStorageManager init failed");
57 return false;
58 }
59 if (!SubscribeManager::GetInstance().Init()) {
60 HILOGE("SubscribeManager init failed");
61 return false;
62 }
63 if (!AuthorityManager::GetInstance().Init()) {
64 HILOGE("AuthorityManager init failed");
65 return false;
66 }
67 TrustGroupManager::GetInstance().InitHichainService();
68 ContentSensorManager::GetInstance().Init();
69 HILOGI("init succeeded");
70 return true;
71 }
72
PutDeviceProfile(const ServiceCharacteristicProfile & profile)73 int32_t DistributedDeviceProfileService::PutDeviceProfile(const ServiceCharacteristicProfile& profile)
74 {
75 if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
76 profile.GetServiceId())) {
77 return ERR_DP_PERMISSION_DENIED;
78 }
79 return DeviceProfileStorageManager::GetInstance().PutDeviceProfile(profile);
80 }
81
GetDeviceProfile(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)82 int32_t DistributedDeviceProfileService::GetDeviceProfile(const std::string& udid, const std::string& serviceId,
83 ServiceCharacteristicProfile& profile)
84 {
85 if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_R,
86 serviceId)) {
87 return ERR_DP_PERMISSION_DENIED;
88 }
89 return DeviceProfileStorageManager::GetInstance().GetDeviceProfile(udid, serviceId, profile);
90 }
91
DeleteDeviceProfile(const std::string & serviceId)92 int32_t DistributedDeviceProfileService::DeleteDeviceProfile(const std::string& serviceId)
93 {
94 if (!AuthorityManager::GetInstance().CheckServiceAuthority(AuthValue::AUTH_W,
95 serviceId)) {
96 return ERR_DP_PERMISSION_DENIED;
97 }
98 return DeviceProfileStorageManager::GetInstance().DeleteDeviceProfile(serviceId);
99 }
100
SubscribeProfileEvents(const std::list<SubscribeInfo> & subscribeInfos,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)101 int32_t DistributedDeviceProfileService::SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
102 const sptr<IRemoteObject>& profileEventNotifier,
103 std::list<ProfileEvent>& failedEvents)
104 {
105 HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_SUB_TRACE);
106 return SubscribeManager::GetInstance().SubscribeProfileEvents(subscribeInfos,
107 profileEventNotifier, failedEvents);
108 }
109
UnsubscribeProfileEvents(const std::list<ProfileEvent> & profileEvents,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)110 int32_t DistributedDeviceProfileService::UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
111 const sptr<IRemoteObject>& profileEventNotifier,
112 std::list<ProfileEvent>& failedEvents)
113 {
114 return SubscribeManager::GetInstance().UnsubscribeProfileEvents(profileEvents,
115 profileEventNotifier, failedEvents);
116 }
117
SyncDeviceProfile(const SyncOptions & syncOptions,const sptr<IRemoteObject> & profileEventNotifier)118 int32_t DistributedDeviceProfileService::SyncDeviceProfile(const SyncOptions& syncOptions,
119 const sptr<IRemoteObject>& profileEventNotifier)
120 {
121 if (!AuthorityManager::GetInstance().CheckInterfaceAuthority("sync")) {
122 return ERR_DP_PERMISSION_DENIED;
123 }
124 return DeviceProfileStorageManager::GetInstance().SyncDeviceProfile(syncOptions, profileEventNotifier);
125 }
126
Dump(int32_t fd,const std::vector<std::u16string> & args)127 int32_t DistributedDeviceProfileService::Dump(int32_t fd, const std::vector<std::u16string>& args)
128 {
129 std::vector<std::string> argsInStr8;
130 for (const auto& arg : args) {
131 argsInStr8.emplace_back(Str16ToStr8(arg));
132 }
133
134 std::string result;
135 DeviceProfileDumper::Dump(argsInStr8, result);
136
137 if (!SaveStringToFd(fd, result)) {
138 HILOGE("save to fd failed");
139 return ERR_DP_FILE_FAILED_ERR;
140 }
141 return ERR_OK;
142 }
143
OnStart()144 void DistributedDeviceProfileService::OnStart()
145 {
146 HILOGI("called");
147 if (!Init()) {
148 HILOGE("init failed");
149 return;
150 }
151 if (!Publish(this)) {
152 HILOGE("publish SA failed");
153 return;
154 }
155 }
156
OnStop()157 void DistributedDeviceProfileService::OnStop()
158 {
159 HILOGI("called");
160 }
161 } // namespace DeviceProfile
162 } // namespace OHOS