• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "service_info_profile_manager.h"
17 
18 #include "distributed_device_profile_constants.h"
19 #include "distributed_device_profile_errors.h"
20 #include "distributed_device_profile_log.h"
21 #include "dp_services_constants.h"
22 
23 namespace OHOS {
24 namespace DistributedDeviceProfile {
25 IMPLEMENT_SINGLE_INSTANCE(ServiceInfoProfileManager)
26 
27 namespace {
28 const std::string TAG = "ServiceInfoProfileManager";
29 }
30 
Init()31 int32_t ServiceInfoProfileManager::Init()
32 {
33     int32_t ret = ServiceInfoRdbAdapter::GetInstance().Init();
34     if (ret != DP_SUCCESS) {
35         HILOGE("ServiceInfoRdbAdapter Init failed");
36         return DP_INIT_DB_FAILED;
37     }
38     CreateTable();
39     CreateIndex();
40     HILOGI("end!");
41     return DP_SUCCESS;
42 }
43 
UnInit()44 int32_t ServiceInfoProfileManager::UnInit()
45 {
46     int32_t ret = ServiceInfoRdbAdapter::GetInstance().UnInit();
47     if (ret != DP_SUCCESS) {
48         HILOGE("ServiceInfoRdbAdapter UnInit failed");
49         return DP_UNINIT_FAIL;
50     }
51     return DP_SUCCESS;
52 }
53 
CreateTable()54 int32_t ServiceInfoProfileManager::CreateTable()
55 {
56     int32_t ret = ServiceInfoRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_INFO_PROFILE_TABLE_SQL);
57     if (ret != DP_SUCCESS) {
58         HILOGE("service_info_profile create failed");
59         return DP_CREATE_TABLE_FAIL;
60     }
61     return DP_SUCCESS;
62 }
63 
CreateIndex()64 int32_t ServiceInfoProfileManager::CreateIndex()
65 {
66     int32_t ret = ServiceInfoRdbAdapter::GetInstance().CreateTable(
67         CREATE_SERVICE_INFO_PROFILE_TABLE_UNIQUE_INDEX_SQL);
68     if (ret != DP_SUCCESS) {
69         HILOGE("service_info_profile unique index create failed");
70         return DP_CREATE_UNIQUE_INDEX_FAIL;
71     }
72     return DP_SUCCESS;
73 }
74 
PutServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)75 int32_t ServiceInfoProfileManager::PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
76 {
77     HILOGI("serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str());
78     if (serviceInfoProfile.GetDeviceId().empty() || serviceInfoProfile.GetUserId() == DEFAULT_USER_ID ||
79         serviceInfoProfile.GetTokenId().empty() || serviceInfoProfile.GetServiceId() == DEFAULT_USER_ID) {
80         HILOGE("Invalid parameter");
81         return DP_INVALID_PARAM;
82     }
83     ValuesBucket values;
84     ServiceInfoProfileToEntries(serviceInfoProfile, values);
85     int64_t rowId = ROWID_INIT;
86     int32_t ret = RET_INIT;
87     ServiceInfoUniqueKey key;
88     ServiceInfoProfile oldSerInfo;
89     key.SetDeviceId(serviceInfoProfile.GetDeviceId());
90     key.SetUserId(serviceInfoProfile.GetUserId());
91     key.SetTokenId(serviceInfoProfile.GetTokenId());
92     key.SetServiceId(serviceInfoProfile.GetServiceId());
93     int32_t getRet = GetServiceInfoProfileByUniqueKey(key, oldSerInfo);
94     if (getRet == DP_SUCCESS) {
95         HILOGI("serviceInfoProfile already exit");
96         return DP_SERVICE_INFO_PROFILE_EXISTS;
97     }
98     ret = ServiceInfoRdbAdapter::GetInstance().Put(rowId, SERVICE_INFO_PROFILE_TABLE, values);
99     if (ret != DP_SUCCESS) {
100         HILOGE("%{public}s insert failed", SERVICE_INFO_PROFILE_TABLE.c_str());
101         return DP_PUT_SERVICE_INFO_PROFILE_FAIL;
102     }
103     HILOGI("end!");
104     return DP_SUCCESS;
105 }
106 
DeleteServiceInfoProfile(const ServiceInfoUniqueKey & key)107 int32_t ServiceInfoProfileManager::DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key)
108 {
109     HILOGI("serviceInfoUniqueKey: %{public}s", key.dump().c_str());
110     if (key.GetDeviceId().empty() || key.GetUserId() == DEFAULT_USER_ID ||
111         key.GetTokenId().empty() || key.GetServiceId() == DEFAULT_SERVICE_ID) {
112         HILOGE("Invalid parameter");
113         return DP_INVALID_PARAM;
114     }
115     ServiceInfoProfile oldServiceInfo;
116     int32_t getRet = GetServiceInfoProfileByUniqueKey(key, oldServiceInfo);
117     if (getRet == DP_NOT_FIND_DATA) {
118         HILOGI("ServiceInfoProfile not exit, doesn't need delete");
119         return DP_SUCCESS;
120     }
121     if (getRet != DP_SUCCESS) {
122         HILOGE("GetServiceInfoProfile filed");
123         return getRet;
124     }
125     int32_t deleteRows = DELETEROWS_INIT;
126     int32_t ret = ServiceInfoRdbAdapter::GetInstance().Delete(deleteRows, SERVICE_INFO_PROFILE_TABLE,
127         SERVICE_INFO_PROFILE_UNIQUE_INDEX_EQUAL_CONDITION,
128         std::vector<ValueObject>{ValueObject(key.GetDeviceId()), ValueObject(key.GetUserId()),
129         ValueObject(key.GetTokenId()), ValueObject(key.GetServiceId())});
130     if (ret != DP_SUCCESS) {
131         HILOGE("delete %{public}s data failed", SERVICE_INFO_PROFILE_TABLE.c_str());
132         return DP_DELETE_SERVICE_INFO_PROFILE_FAIL;
133     }
134     HILOGI("end!");
135     return DP_SUCCESS;
136 }
137 
UpdateServiceInfoProfile(const ServiceInfoProfile & serviceInfoProfile)138 int32_t ServiceInfoProfileManager::UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile)
139 {
140     HILOGI("serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str());
141     if (serviceInfoProfile.GetDeviceId().empty() || serviceInfoProfile.GetUserId() == DEFAULT_USER_ID ||
142         serviceInfoProfile.GetTokenId().empty() || serviceInfoProfile.GetServiceId() == DEFAULT_SERVICE_ID) {
143         HILOGE("Invalid parameter");
144         return DP_INVALID_PARAM;
145     }
146     ServiceInfoUniqueKey key;
147     ServiceInfoProfile serInfo;
148     key.SetDeviceId(serviceInfoProfile.GetDeviceId());
149     key.SetUserId(serviceInfoProfile.GetUserId());
150     key.SetTokenId(serviceInfoProfile.GetTokenId());
151     key.SetServiceId(serviceInfoProfile.GetServiceId());
152     int32_t getRet = GetServiceInfoProfileByUniqueKey(key, serInfo);
153     if (getRet == DP_NOT_FIND_DATA) {
154         HILOGE("serviceInfoProfile not exit, can't update");
155         return getRet;
156     }
157     if (getRet != DP_SUCCESS) {
158         HILOGI("GetServiceInfoProfileByUniqueKey failed");
159         return getRet;
160     }
161     ValuesBucket values;
162     ServiceInfoProfileToEntries(serviceInfoProfile, values);
163     int32_t changeRowCnt = CHANGEROWCNT_INIT;
164     int32_t ret = ServiceInfoRdbAdapter::GetInstance().Update(changeRowCnt, SERVICE_INFO_PROFILE_TABLE, values,
165         SERVICE_INFO_PROFILE_UNIQUE_INDEX_EQUAL_CONDITION,
166         std::vector<ValueObject>{ValueObject(serviceInfoProfile.GetDeviceId()),
167         ValueObject(serviceInfoProfile.GetUserId()),
168         ValueObject(serviceInfoProfile.GetTokenId()),
169         ValueObject(serviceInfoProfile.GetServiceId())});
170     if (ret != DP_SUCCESS) {
171         HILOGE("Update %{public}s table failed", SERVICE_INFO_PROFILE_TABLE.c_str());
172         return DP_UPDATE_SERVICE_INFO_PROFILE_FAIL;
173     }
174     HILOGI("end!");
175     return DP_SUCCESS;
176 }
177 
GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey & key,ServiceInfoProfile & serviceInfoProfile)178 int32_t ServiceInfoProfileManager::GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key,
179     ServiceInfoProfile& serviceInfoProfile)
180 {
181     HILOGI("serviceInfoUniqueKey: %{public}s", key.dump().c_str());
182     if (key.GetDeviceId().empty() || key.GetUserId() == DEFAULT_USER_ID ||
183         key.GetTokenId().empty() || key.GetServiceId() == DEFAULT_SERVICE_ID) {
184         HILOGE("Invalid parameter");
185         return DP_INVALID_PARAM;
186     }
187     std::vector<ValueObject> condition;
188     condition.emplace_back(ValueObject(key.GetDeviceId()));
189     condition.emplace_back(ValueObject(key.GetUserId()));
190     condition.emplace_back(ValueObject(key.GetTokenId()));
191     condition.emplace_back(ValueObject(key.GetServiceId()));
192     std::shared_ptr<ResultSet> resultSet = ServiceInfoRdbAdapter::GetInstance().Get(
193         SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_TOKENID_SERVICEID, condition);
194     if (resultSet == nullptr) {
195         HILOGE("resultSet is nullptr");
196         return DP_GET_RESULTSET_FAIL;
197     }
198     int32_t rowCount = ROWCOUNT_INIT;
199     resultSet->GetRowCount(rowCount);
200     if (rowCount == 0) {
201         HILOGE("by condition not find data");
202         resultSet->Close();
203         return DP_NOT_FIND_DATA;
204     }
205     int32_t ret = resultSet->GoToNextRow();
206     if (ret != DP_SUCCESS) {
207         HILOGE("get ServiceInfoProfileResult failed");
208         resultSet->Close();
209         return DP_NOT_FIND_DATA;
210     }
211     ConvertToServiceInfo(resultSet, serviceInfoProfile);
212     resultSet->Close();
213     HILOGI("end!");
214     return DP_SUCCESS;
215 }
216 
GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)217 int32_t ServiceInfoProfileManager::GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey& key,
218     std::vector<ServiceInfoProfile>& serviceInfoProfiles)
219 {
220     HILOGI("serviceInfoUniqueKey: %{public}s", key.dump().c_str());
221     if (key.GetDeviceId().empty() || key.GetUserId() == DEFAULT_USER_ID || key.GetTokenId().empty()) {
222         HILOGE("Invalid parameter");
223         return DP_INVALID_PARAM;
224     }
225     std::vector<ValueObject> condition;
226     condition.emplace_back(ValueObject(key.GetDeviceId()));
227     condition.emplace_back(ValueObject(key.GetUserId()));
228     condition.emplace_back(ValueObject(key.GetTokenId()));
229     std::shared_ptr<ResultSet> resultSet = ServiceInfoRdbAdapter::GetInstance().Get(
230         SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_TOKENID, condition);
231     if (resultSet == nullptr) {
232         HILOGE("resultSet is nullptr");
233         return DP_GET_RESULTSET_FAIL;
234     }
235     int32_t rowCount = ROWCOUNT_INIT;
236     resultSet->GetRowCount(rowCount);
237     if (rowCount == 0) {
238         HILOGE("by condition not find data");
239         resultSet->Close();
240         return DP_NOT_FIND_DATA;
241     }
242     while (resultSet->GoToNextRow() == DP_SUCCESS) {
243         ServiceInfoProfile serviceInfoProfile;
244         ConvertToServiceInfo(resultSet, serviceInfoProfile);
245         serviceInfoProfiles.emplace_back(serviceInfoProfile);
246     }
247     resultSet->Close();
248     if (serviceInfoProfiles.empty()) {
249         return DP_NOT_FIND_DATA;
250     }
251     HILOGI("end!");
252     return DP_SUCCESS;
253 }
254 
GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile> & serviceInfoProfiles)255 int32_t ServiceInfoProfileManager::GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile>& serviceInfoProfiles)
256 {
257     HILOGI("call");
258     std::vector<ValueObject> condition;
259     std::shared_ptr<ResultSet> resultSet = ServiceInfoRdbAdapter::GetInstance().Get(
260         SELECT_SERVICE_INFO_PROFILE_ALL, condition);
261     if (resultSet == nullptr) {
262         HILOGE("resultSet is nullptr");
263         return DP_GET_RESULTSET_FAIL;
264     }
265     int32_t rowCount = ROWCOUNT_INIT;
266     resultSet->GetRowCount(rowCount);
267     if (rowCount == 0) {
268         HILOGE("by condition not find data");
269         resultSet->Close();
270         return DP_NOT_FIND_DATA;
271     }
272     while (resultSet->GoToNextRow() == DP_SUCCESS) {
273         ServiceInfoProfile serviceInfoProfile;
274         ConvertToServiceInfo(resultSet, serviceInfoProfile);
275         serviceInfoProfiles.emplace_back(serviceInfoProfile);
276     }
277     resultSet->Close();
278     if (serviceInfoProfiles.empty()) {
279         return DP_NOT_FIND_DATA;
280     }
281     HILOGI("end!");
282     return DP_SUCCESS;
283 }
284 
GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey & key,std::vector<ServiceInfoProfile> & serviceInfoProfiles)285 int32_t ServiceInfoProfileManager::GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey& key,
286     std::vector<ServiceInfoProfile>& serviceInfoProfiles)
287 {
288     HILOGI("serviceInfoUniqueKey: %{public}s", key.dump().c_str());
289     if (key.GetDeviceId().empty() || key.GetUserId() == DEFAULT_USER_ID || key.GetBundleName().empty()) {
290         HILOGE("Invalid parameter");
291         return DP_INVALID_PARAM;
292     }
293     std::vector<ValueObject> condition;
294     condition.emplace_back(ValueObject(key.GetDeviceId()));
295     condition.emplace_back(ValueObject(key.GetUserId()));
296     condition.emplace_back(ValueObject(key.GetBundleName()));
297     std::shared_ptr<ResultSet> resultSet = ServiceInfoRdbAdapter::GetInstance().Get(
298         SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_BUNDLENAME, condition);
299     if (resultSet == nullptr) {
300         HILOGE("resultSet is nullptr");
301         return DP_GET_RESULTSET_FAIL;
302     }
303     int32_t rowCount = ROWCOUNT_INIT;
304     resultSet->GetRowCount(rowCount);
305     if (rowCount == 0) {
306         HILOGE("by condition not find data");
307         resultSet->Close();
308         return DP_NOT_FIND_DATA;
309     }
310     while (resultSet->GoToNextRow() == DP_SUCCESS) {
311         ServiceInfoProfile serviceInfoProfile;
312         ConvertToServiceInfo(resultSet, serviceInfoProfile);
313         serviceInfoProfiles.emplace_back(serviceInfoProfile);
314     }
315     resultSet->Close();
316     if (serviceInfoProfiles.empty()) {
317         return DP_NOT_FIND_DATA;
318     }
319     HILOGI("end!");
320     return DP_SUCCESS;
321 }
322 
ConvertToServiceInfo(std::shared_ptr<ResultSet> resultSet,ServiceInfoProfile & serviceInfo)323 int32_t ServiceInfoProfileManager::ConvertToServiceInfo(std::shared_ptr<ResultSet> resultSet,
324     ServiceInfoProfile& serviceInfo)
325 {
326     if (resultSet == nullptr) {
327         HILOGE("resultSet is nullptr");
328         return DP_GET_RESULTSET_FAIL;
329     }
330     RowEntity rowEntity;
331     if (resultSet->GetRow(rowEntity) != DP_SUCCESS) {
332         HILOGE("get resultSet failed");
333         return DP_GET_RESULTSET_FAIL;
334     }
335     serviceInfo.SetDeviceId(rowEntity.Get(DEVICE_ID));
336     serviceInfo.SetUserId(rowEntity.Get(USERID));
337     serviceInfo.SetNetworkId(rowEntity.Get(SRNETWORK_ID));
338     serviceInfo.SetTokenId(rowEntity.Get(TOKENID));
339     serviceInfo.SetServiceId(rowEntity.Get(SISERVICE_ID));
340     serviceInfo.SetServiceType(rowEntity.Get(SERVICE_TYPE));
341     serviceInfo.SetServiceName(rowEntity.Get(SERVICE_NAME));
342     serviceInfo.SetServiceDisplayName(rowEntity.Get(SERVICE_DISPLAY_NAME));
343     serviceInfo.SetCustomData(rowEntity.Get(CUSTOM_DATA));
344     serviceInfo.SetCustomDataLen(rowEntity.Get(CUSTOM_DATA_LEN));
345     serviceInfo.SetBundleName(rowEntity.Get(BUNDLE_NAME));
346     serviceInfo.SetModuleName(rowEntity.Get(MODULE_NAME));
347     serviceInfo.SetAbilityName(rowEntity.Get(ABILITY_NAME));
348     serviceInfo.SetAuthBoxType(rowEntity.Get(AUTH_BOX_TYPE));
349     serviceInfo.SetAuthType(rowEntity.Get(AUTH_TYPE));
350     serviceInfo.SetPinExchangeType(rowEntity.Get(PIN_EXCHANGE_TYPE));
351     serviceInfo.SetPinCode(rowEntity.Get(PINCODE));
352     serviceInfo.SetDescription(rowEntity.Get(DESCRIPTION));
353     serviceInfo.SetServiceDicoveryScope(rowEntity.Get(SERVICE_DISCOVERY_SCOPE));
354     serviceInfo.SetExtraInfo(rowEntity.Get(EXTRAINFO));
355     return DP_SUCCESS;
356 }
357 
ServiceInfoProfileToEntries(const ServiceInfoProfile & serviceInfo,ValuesBucket & values)358 int32_t ServiceInfoProfileManager::ServiceInfoProfileToEntries(const ServiceInfoProfile& serviceInfo,
359     ValuesBucket& values)
360 {
361     values.PutString(DEVICE_ID, serviceInfo.GetDeviceId());
362     values.PutInt(USERID, serviceInfo.GetUserId());
363     values.PutString(SRNETWORK_ID, serviceInfo.GetNetworkId());
364     values.PutString(TOKENID, serviceInfo.GetTokenId());
365     values.PutLong(SISERVICE_ID, serviceInfo.GetServiceId());
366     values.PutString(SERVICE_TYPE, serviceInfo.GetServiceType());
367     values.PutString(SERVICE_NAME, serviceInfo.GetServiceName());
368     values.PutString(SERVICE_DISPLAY_NAME, serviceInfo.GetServiceDisplayName());
369     values.PutString(CUSTOM_DATA, serviceInfo.GetCustomData());
370     values.PutInt(CUSTOM_DATA_LEN, serviceInfo.GetCustomDataLen());
371     values.PutString(BUNDLE_NAME, serviceInfo.GetBundleName());
372     values.PutString(MODULE_NAME, serviceInfo.GetModuleName());
373     values.PutString(ABILITY_NAME, serviceInfo.GetAbilityName());
374     values.PutInt(AUTH_BOX_TYPE, serviceInfo.GetAuthBoxType());
375     values.PutInt(AUTH_TYPE, serviceInfo.GetAuthType());
376     values.PutInt(PIN_EXCHANGE_TYPE, serviceInfo.GetPinExchangeType());
377     values.PutString(PINCODE, serviceInfo.GetPinCode());
378     values.PutString(DESCRIPTION, serviceInfo.GetDescription());
379     values.PutString(SERVICE_DISCOVERY_SCOPE, serviceInfo.GetServiceDicoveryScope());
380     values.PutString(EXTRAINFO, serviceInfo.GetExtraInfo());
381     return DP_SUCCESS;
382 }
383 } // namespace DistributedDeviceProfile
384 } // namespace OHOS
385