1 /*
2 * Copyright (c) 2021-2022 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 "device_profile_storage_manager.h"
17
18 #include <chrono>
19 #include <thread>
20
21 #include "device_profile_errors.h"
22 #include "device_profile_log.h"
23 #include "device_profile_utils.h"
24 #include "dp_device_manager.h"
25 #include "hisysevent.h"
26 #include "hitrace_meter.h"
27 #include "sync_coordinator.h"
28
29 #include "ipc_object_proxy.h"
30 #include "ipc_skeleton.h"
31 #include "iservice_registry.h"
32 #include "subscribe_info.h"
33 #include "subscribe_manager.h"
34 #include "system_ability_definition.h"
35
36 namespace OHOS {
37 namespace DeviceProfile {
38 using namespace OHOS::HiviewDFX;
39 using namespace std::chrono_literals;
40 using namespace OHOS::DistributedKv;
41
42 namespace {
43 const std::string TAG = "DeviceProfileStorageManager";
44
45 const std::string SERVICE_TYPE = "type";
46 const std::string SERVICES = "services";
47 const std::string DOMAIN_NAME = std::string(HiSysEvent::Domain::DEVICE_PROFILE);
48 const std::string DEVICE_PROFILE_SYNC_FAILED = "DEVICE_PROFILE_SYNC_FAILED";
49 const std::string DEVICE_PROFILE_SYNC_EVENT = "DEVICE_PROFILE_SYNC_EVENT";
50 const std::string FAULT_CODE_KEY = "FAULT_CODE";
51 const std::string DP_DEVICE_PUT_TRACE = "DP_DEVICE_PUT";
52 const std::string DP_DEVICE_GET_TRACE = "DP_DEVICE_GET";
53 const std::string DP_DEVICE_DELETE_TRACE = "DP_DEVICE_DELETE";
54 const std::string DP_DEVICE_SYNC_TRACE = "DP_DEVICE_SYNC";
55 constexpr int32_t RETRY_TIMES_WAIT_KV_DATA = 30;
56 constexpr int32_t FIX_TASK_ID = 0;
57 constexpr int32_t INDENT = -1;
58 const char INDENT_CHAR = ' ';
59 }
60
61 IMPLEMENT_SINGLE_INSTANCE(DeviceProfileStorageManager);
62
Init()63 bool DeviceProfileStorageManager::Init()
64 {
65 if (!inited_) {
66 if (!SyncCoordinator::GetInstance().Init()) {
67 HILOGE("SyncCoordinator init failed");
68 return false;
69 }
70 DpDeviceManager::GetInstance().GetLocalDeviceUdid(localUdid_);
71 if (localUdid_.empty()) {
72 HILOGE("get local udid failed");
73 return false;
74 }
75 onlineSyncTbl_ = std::make_shared<OnlineSyncTable>();
76 kvStoreDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new KvStoreDeathRecipient());
77 auto runner = AppExecFwk::EventRunner::Create("dpstorage");
78 storageHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
79 if (storageHandler_ == nullptr) {
80 return false;
81 }
82 inited_ = true;
83 }
84
85 auto waitTask = [this]() {
86 if (!WaitKvDataService()) {
87 std::lock_guard<std::mutex> autoLock(serviceLock_);
88 profileItems_.clear();
89 kvDataServiceFailed_ = true;
90 return;
91 }
92 auto callback = std::bind(&DeviceProfileStorageManager::OnKvStoreInitDone, this);
93 onlineSyncTbl_->RegisterKvStoreInitCallback(callback);
94 onlineSyncTbl_->Init();
95 };
96 if (!storageHandler_->PostTask(waitTask)) {
97 HILOGE("post task failed");
98 return false;
99 }
100 HILOGI("init succeeded");
101 return true;
102 }
103
WaitKvDataService()104 bool DeviceProfileStorageManager::WaitKvDataService()
105 {
106 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
107 if (samgrProxy == nullptr) {
108 HILOGE("get samgrProxy failed");
109 return false;
110 }
111 int32_t retryTimes = RETRY_TIMES_WAIT_KV_DATA;
112 do {
113 auto kvDataSvr = samgrProxy->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
114 if (kvDataSvr != nullptr) {
115 IPCObjectProxy* proxy = reinterpret_cast<IPCObjectProxy*>(kvDataSvr.GetRefPtr());
116 if (proxy != nullptr && !proxy->IsObjectDead()) {
117 HILOGI("get service succeed");
118 proxy->AddDeathRecipient(kvStoreDeathRecipient_);
119 return true;
120 }
121 }
122 HILOGD("waiting for service...");
123 std::this_thread::sleep_for(1s);
124 if (--retryTimes <= 0) {
125 HILOGE("waiting service timeout(30)s");
126 return false;
127 }
128 } while (true);
129 return false;
130 }
131
GenerateKey(const std::string & udid,const std::string & key,KeyType keyType)132 std::string DeviceProfileStorageManager::GenerateKey(const std::string& udid,
133 const std::string& key, KeyType keyType)
134 {
135 std::string tmp;
136 tmp.append(udid).append("/").append(std::to_string(static_cast<int8_t>(keyType))).append("/").append(key);
137 return tmp;
138 }
139
PutDeviceProfile(const ServiceCharacteristicProfile & profile)140 int32_t DeviceProfileStorageManager::PutDeviceProfile(const ServiceCharacteristicProfile& profile)
141 {
142 HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_PUT_TRACE);
143 if (kvDataServiceFailed_ || onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
144 HILOGE("kvstore init failed");
145 return ERR_DP_INIT_DB_FAILED;
146 }
147
148 std::vector<std::string> keys;
149 std::vector<std::string> values;
150 std::string serviceId = profile.GetServiceId();
151 keys.emplace_back(GenerateKey(localUdid_, serviceId, KeyType::SERVICE));
152 values.emplace_back(profile.GetCharacteristicProfileJson());
153 std::unique_lock<std::mutex> autoLock(serviceLock_);
154 if (servicesJson_[serviceId] == nullptr) {
155 nlohmann::json j;
156 j[SERVICE_TYPE] = profile.GetServiceType();
157 servicesJson_[serviceId] = j;
158 keys.emplace_back(GenerateKey(localUdid_, SERVICES, KeyType::SERVICE_LIST));
159 values.emplace_back(servicesJson_.dump(INDENT, INDENT_CHAR, false,
160 nlohmann::json::error_handler_t::ignore));
161 }
162
163 int32_t errCode = ERR_OK;
164 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_SUCCEED) {
165 autoLock.unlock();
166 if (keys.size() > 1) {
167 errCode = onlineSyncTbl_->PutDeviceProfileBatch(keys, values);
168 } else {
169 errCode = onlineSyncTbl_->PutDeviceProfile(keys[0], values[0]);
170 }
171 } else {
172 for (size_t i = 0; i < keys.size(); i++) {
173 profileItems_[keys[i]] = values[i];
174 }
175 }
176 return errCode;
177 }
178
GetDeviceProfile(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)179 int32_t DeviceProfileStorageManager::GetDeviceProfile(const std::string& udid,
180 const std::string& serviceId, ServiceCharacteristicProfile& profile)
181 {
182 HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_GET_TRACE);
183 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
184 HILOGE("kvstore init failed");
185 return ERR_DP_INIT_DB_FAILED;
186 }
187
188 std::string key;
189 std::string value;
190 int32_t result = ERR_OK;
191 if (udid.empty()) {
192 key = GenerateKey(localUdid_, serviceId, KeyType::SERVICE);
193 SetServiceType(udid, serviceId, profile);
194 } else {
195 std::string queryUdid;
196 if (!DpDeviceManager::GetInstance().TransformDeviceId(udid, queryUdid,
197 DeviceIdType::UDID)) {
198 HILOGE("transform to networkid failed");
199 return ERR_DP_INVALID_PARAMS;
200 }
201 key = GenerateKey(queryUdid, serviceId, KeyType::SERVICE);
202 SetServiceType(queryUdid, serviceId, profile);
203 }
204 std::unique_lock<std::mutex> autoLock(serviceLock_);
205 auto itItem = profileItems_.find(key);
206 if (itItem != profileItems_.end()) {
207 value = profileItems_[key];
208 } else {
209 autoLock.unlock();
210 result = onlineSyncTbl_->GetDeviceProfile(key, value);
211 }
212 profile.SetServiceId(serviceId);
213 profile.SetCharacteristicProfileJson(value);
214 return result;
215 }
216
SetServiceType(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)217 void DeviceProfileStorageManager::SetServiceType(const std::string& udid,
218 const std::string& serviceId, ServiceCharacteristicProfile& profile)
219 {
220 std::unique_lock<std::mutex> autoLock(serviceLock_);
221 if (udid.empty()) {
222 auto jsonData = servicesJson_[serviceId];
223 if (jsonData != nullptr) {
224 profile.SetServiceType(jsonData[SERVICE_TYPE]);
225 }
226 return;
227 }
228
229 std::string value;
230 std::string key = GenerateKey(udid, SERVICES, KeyType::SERVICE_LIST);
231 int32_t result = onlineSyncTbl_->GetDeviceProfile(key, value);
232 if (result != ERR_OK) {
233 HILOGE("get service type failed");
234 return;
235 }
236 auto jsonData = nlohmann::json::parse(value, nullptr, false);
237 if (jsonData.is_discarded()) {
238 HILOGE("parse error");
239 return;
240 }
241 auto typeData = jsonData[serviceId];
242 if (typeData != nullptr && typeData[SERVICE_TYPE] != nullptr) {
243 profile.SetServiceType(typeData[SERVICE_TYPE]);
244 }
245 }
246
DeleteDeviceProfile(const std::string & serviceId)247 int32_t DeviceProfileStorageManager::DeleteDeviceProfile(const std::string& serviceId)
248 {
249 HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_DELETE_TRACE);
250 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
251 HILOGE("kvstore init failed");
252 return ERR_DP_INIT_DB_FAILED;
253 }
254
255 std::unique_lock<std::mutex> autoLock(serviceLock_);
256 if (servicesJson_[serviceId] == nullptr) {
257 HILOGW("can't find service %{public}s", serviceId.c_str());
258 return ERR_DP_INVALID_PARAMS;
259 }
260 nlohmann::json original = servicesJson_[serviceId];
261 servicesJson_.erase(serviceId);
262 std::string servicesKey = GenerateKey(localUdid_, SERVICES, KeyType::SERVICE_LIST);
263 std::string servicesValue = servicesJson_.dump();
264 int32_t errCode = ERR_OK;
265 std::string serviceKey = GenerateKey(localUdid_, serviceId, KeyType::SERVICE);
266 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_SUCCEED) {
267 errCode = onlineSyncTbl_->DeleteDeviceProfile(serviceKey);
268 if (errCode != ERR_OK) {
269 servicesJson_[serviceId] = std::move(original);
270 return errCode;
271 }
272 errCode = onlineSyncTbl_->PutDeviceProfile(servicesKey, servicesValue);
273 if (errCode != ERR_OK) {
274 HILOGW("update services failed, errorCode = %{public}d", errCode);
275 }
276 } else {
277 profileItems_.erase(serviceKey);
278 profileItems_[servicesKey] = std::move(servicesValue);
279 }
280 return errCode;
281 }
282
RemoveUnBoundDeviceProfile(const std::string & udid)283 int32_t DeviceProfileStorageManager::RemoveUnBoundDeviceProfile(const std::string& udid)
284 {
285 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
286 HILOGE("kvstore init failed");
287 return ERR_DP_INIT_DB_FAILED;
288 }
289
290 std::unique_lock<std::mutex> autoLock(serviceLock_);
291 if (onlineSyncTbl_->GetInitStatus() != StorageInitStatus::INIT_SUCCEED) {
292 HILOGE("kvstore not init");
293 return ERR_DP_NOT_INIT_DB;
294 }
295
296 int32_t errCode = ERR_OK;
297 std::string networkId;
298 if (!DpDeviceManager::GetInstance().TransformDeviceId(udid, networkId, DeviceIdType::NETWORKID)) {
299 HILOGE("udid transform to networkid failed, udid = %{public}s",
300 DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
301 return ERR_DP_GET_NETWORKID_FAILED;
302 }
303
304 errCode = onlineSyncTbl_->RemoveDeviceData(networkId);
305 return errCode;
306 }
307
SyncDeviceProfile(const SyncOptions & syncOptions,const sptr<IRemoteObject> & profileEventNotifier)308 int32_t DeviceProfileStorageManager::SyncDeviceProfile(const SyncOptions& syncOptions,
309 const sptr<IRemoteObject>& profileEventNotifier)
310 {
311 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_FAILED) {
312 HILOGE("kvstore init failed");
313 return ERR_DP_INIT_DB_FAILED;
314 }
315
316 if (!CheckSyncOption(syncOptions)) {
317 HILOGW("device list has offline device");
318 return ERR_DP_INVALID_PARAMS;
319 }
320
321 int32_t result = NotifySyncStart(profileEventNotifier);
322 if (result != ERR_OK) {
323 return result;
324 }
325
326 StartAsyncTrace(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_SYNC_TRACE, FIX_TASK_ID);
327 auto syncTask = [syncOptions, this]() {
328 HILOGI("start sync");
329 ReportBehaviorEvent(DEVICE_PROFILE_SYNC_EVENT);
330 auto devicesList = syncOptions.GetDeviceList();
331 if (devicesList.empty()) {
332 DpDeviceManager::GetInstance().GetDeviceIdList(devicesList);
333 }
334 SyncCoordinator::GetInstance().SetSyncTrigger(false);
335 std::vector<std::string> devicesVector(std::vector<std::string> { devicesList.begin(), devicesList.end() });
336 int32_t result = onlineSyncTbl_->SyncDeviceProfile(devicesVector, syncOptions.GetSyncMode());
337 if (result != ERR_OK) {
338 ReportFaultEvent(DEVICE_PROFILE_SYNC_FAILED, FAULT_CODE_KEY, result);
339 HILOGE("sync failed result : %{public}d", result);
340 NotifySyncCompleted();
341 return;
342 }
343 };
344 if (!SyncCoordinator::GetInstance().DispatchSyncTask(syncTask)) {
345 HILOGE("post sync task failed");
346 NotifySyncCompleted();
347 return ERR_DP_POST_TASK_FAILED;
348 }
349 return ERR_OK;
350 }
351
NotifySyncStart(const sptr<IRemoteObject> & profileEventNotifier)352 int32_t DeviceProfileStorageManager::NotifySyncStart(const sptr<IRemoteObject>& profileEventNotifier)
353 {
354 if (!SyncCoordinator::GetInstance().AcquireSync()) {
355 HILOGW("sync busy");
356 return ERR_DP_DEVICE_SYNC_BUSY;
357 }
358
359 {
360 std::lock_guard<std::mutex> autoLock(profileSyncLock_);
361 syncEventNotifier_ = profileEventNotifier;
362 }
363
364 SubscribeInfo subscribeInfo;
365 subscribeInfo.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED;
366 std::list<SubscribeInfo> subscribeInfos;
367 subscribeInfos.emplace_back(subscribeInfo);
368 std::list<ProfileEvent> failedEvents;
369 if (SubscribeManager::GetInstance().SubscribeProfileEvents(
370 subscribeInfos, profileEventNotifier, failedEvents) != ERR_OK) {
371 HILOGE("subscribe sync event failed");
372 SyncCoordinator::GetInstance().ReleaseSync();
373 std::lock_guard<std::mutex> autoLock(profileSyncLock_);
374 syncEventNotifier_ = nullptr;
375 return ERR_DP_SUBSCRIBE_FAILED;
376 }
377 return ERR_OK;
378 }
379
NotifySyncCompleted()380 void DeviceProfileStorageManager::NotifySyncCompleted()
381 {
382 HILOGI("called");
383 SyncCoordinator::GetInstance().ReleaseSync();
384 FinishAsyncTrace(HITRACE_TAG_DEVICE_PROFILE, DP_DEVICE_SYNC_TRACE, FIX_TASK_ID);
385 std::lock_guard<std::mutex> autoLock(profileSyncLock_);
386 std::list<ProfileEvent> profileEvents;
387 profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED);
388 std::list<ProfileEvent> failedEvents;
389 int32_t ret = SubscribeManager::GetInstance().UnsubscribeProfileEvents(
390 profileEvents, syncEventNotifier_, failedEvents);
391 if (ret != ERR_OK) {
392 HILOGW("unsubscribe sync event failed");
393 }
394 syncEventNotifier_ = nullptr;
395 }
396
NotifySubscriberDied(const sptr<IRemoteObject> & profileEventNotifier)397 void DeviceProfileStorageManager::NotifySubscriberDied(const sptr<IRemoteObject>& profileEventNotifier)
398 {
399 HILOGI("called");
400 std::lock_guard<std::mutex> autoLock(profileSyncLock_);
401 if (profileEventNotifier != syncEventNotifier_) {
402 return;
403 }
404
405 SyncCoordinator::GetInstance().ReleaseSync();
406 syncEventNotifier_ = nullptr;
407 }
408
CheckSyncOption(const SyncOptions & syncOptions)409 bool DeviceProfileStorageManager::CheckSyncOption(const SyncOptions& syncOptions)
410 {
411 std::list<std::shared_ptr<DeviceInfo>> onlineDevices;
412 DpDeviceManager::GetInstance().GetDeviceList(onlineDevices);
413 std::list<std::string> onlineDeviceIds;
414 for (const auto& onlineDevice : onlineDevices) {
415 onlineDeviceIds.emplace_back(onlineDevice->GetDeviceId());
416 }
417
418 // check whether deviceId is online
419 auto syncDeviceIds = syncOptions.GetDeviceList();
420 for (const auto& syncDeviceId : syncDeviceIds) {
421 auto iter = find(onlineDeviceIds.begin(), onlineDeviceIds.end(), syncDeviceId);
422 if (iter == onlineDeviceIds.end()) {
423 HILOGE("deviceId: %{public}s is not online", DeviceProfileUtils::AnonymizeDeviceId(syncDeviceId).c_str());
424 return false;
425 }
426 }
427 return true;
428 }
429
RestoreServiceItemLocked(const std::string & value)430 void DeviceProfileStorageManager::RestoreServiceItemLocked(const std::string& value)
431 {
432 auto restoreItems = nlohmann::json::parse(value, nullptr, false);
433 if (restoreItems.is_discarded()) {
434 HILOGE("parse error");
435 return;
436 }
437 for (const auto& [key, value] : servicesJson_.items()) {
438 restoreItems[key] = value;
439 }
440 servicesJson_ = std::move(restoreItems);
441 }
442
FlushProfileItems()443 void DeviceProfileStorageManager::FlushProfileItems()
444 {
445 std::string services;
446 std::string servicesKey = GenerateKey(localUdid_, SERVICES, KeyType::SERVICE_LIST);
447 int32_t errCode = onlineSyncTbl_->GetDeviceProfile(servicesKey, services);
448 std::unique_lock<std::mutex> autoLock(serviceLock_);
449 if (errCode == ERR_OK) {
450 RestoreServiceItemLocked(services);
451 }
452
453 std::vector<std::string> keys;
454 std::vector<std::string> values;
455 size_t itemSize = profileItems_.size();
456 HILOGI("profile item size = %{public}zu", itemSize);
457 if (itemSize == 0) {
458 return;
459 }
460 keys.reserve(itemSize);
461 values.reserve(itemSize);
462 // update service list to avoid overwriting the value in db storage
463 profileItems_[servicesKey] = servicesJson_.dump();
464 for (const auto& [key, value] : profileItems_) {
465 keys.emplace_back(key);
466 values.emplace_back(value);
467 }
468 profileItems_.clear();
469 autoLock.unlock();
470
471 errCode = onlineSyncTbl_->PutDeviceProfileBatch(keys, values);
472 if (errCode != ERR_OK) {
473 HILOGE("put failed, errCode = %{public}d", errCode);
474 }
475 }
476
RegisterCallbacks()477 void DeviceProfileStorageManager::RegisterCallbacks()
478 {
479 HILOGI("called");
480 int32_t errCode = ERR_OK;
481 if (kvStoreObserver_ != nullptr) {
482 errCode = onlineSyncTbl_->SubscribeKvStore(kvStoreObserver_);
483 HILOGI("SubscribeKvStore errCode = %{public}d", errCode);
484 }
485 if (kvStoreSyncCallback_ != nullptr) {
486 errCode = onlineSyncTbl_->RegisterSyncCallback(kvStoreSyncCallback_);
487 HILOGI("RegisterSyncCallback errCode = %{public}d", errCode);
488 }
489 }
490
OnKvStoreInitDone()491 void DeviceProfileStorageManager::OnKvStoreInitDone()
492 {
493 RegisterCallbacks();
494 FlushProfileItems();
495 }
496
SubscribeKvStore(const std::shared_ptr<KvStoreObserver> & observer)497 int32_t DeviceProfileStorageManager::SubscribeKvStore(const std::shared_ptr<KvStoreObserver>& observer)
498 {
499 std::lock_guard<std::mutex> autoLock(callbackLock_);
500 kvStoreObserver_ = observer;
501 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_SUCCEED) {
502 return onlineSyncTbl_->SubscribeKvStore(observer);
503 }
504 return ERR_OK;
505 }
506
UnSubscribeKvStore(const std::shared_ptr<KvStoreObserver> & observer)507 int32_t DeviceProfileStorageManager::UnSubscribeKvStore(const std::shared_ptr<KvStoreObserver>& observer)
508 {
509 std::lock_guard<std::mutex> autoLock(callbackLock_);
510 kvStoreObserver_ = nullptr;
511 return onlineSyncTbl_->UnSubscribeKvStore(observer);
512 }
513
RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback> & sycnCb)514 int32_t DeviceProfileStorageManager::RegisterSyncCallback(const std::shared_ptr<KvStoreSyncCallback>& sycnCb)
515 {
516 std::lock_guard<std::mutex> autoLock(callbackLock_);
517 kvStoreSyncCallback_ = sycnCb;
518 if (onlineSyncTbl_->GetInitStatus() == StorageInitStatus::INIT_SUCCEED) {
519 return onlineSyncTbl_->RegisterSyncCallback(sycnCb);
520 }
521 return ERR_OK;
522 }
523
UnRegisterSyncCallback()524 int32_t DeviceProfileStorageManager::UnRegisterSyncCallback()
525 {
526 std::lock_guard<std::mutex> autoLock(callbackLock_);
527 kvStoreSyncCallback_ = nullptr;
528 return onlineSyncTbl_->UnRegisterSyncCallback();
529 }
530
ReportBehaviorEvent(const std::string & event)531 void DeviceProfileStorageManager::ReportBehaviorEvent(const std::string& event)
532 {
533 int ret = HiSysEvent::Write(DOMAIN_NAME, event, HiSysEvent::EventType::BEHAVIOR);
534 if (ret != 0) {
535 HILOGE("hisysevent write failed! ret %{public}d.", ret);
536 }
537 }
538
ReportFaultEvent(const std::string & event,const std::string & key,const int32_t result)539 void DeviceProfileStorageManager::ReportFaultEvent(const std::string& event,
540 const std::string& key, const int32_t result)
541 {
542 int ret = HiSysEvent::Write(DOMAIN_NAME, event, HiSysEvent::EventType::FAULT, key, result);
543 if (ret != 0) {
544 HILOGE("hisysevent write failed! ret %{public}d.", ret);
545 }
546 }
547
DumpLocalProfile(std::string & result)548 void DeviceProfileStorageManager::DumpLocalProfile(std::string& result)
549 {
550 for (const auto& [key, value] : servicesJson_.items()) {
551 result.append("key:");
552 result.append(key);
553 result.append(" value:");
554 result.append(value.dump());
555 result.append("\n");
556 }
557 }
558 } // namespace DeviceProfile
559 } // namespace OHOS
560