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 "business_event_manager.h"
17
18 #include "business_event.h"
19 #include "distributed_device_profile_constants.h"
20 #include "distributed_device_profile_errors.h"
21 #include "distributed_device_profile_log.h"
22 #include "dp_services_constants.h"
23 #include "kv_data_change_listener.h"
24 #include "kv_store_death_recipient.h"
25 #include "kv_sync_completed_listener.h"
26
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 IMPLEMENT_SINGLE_INSTANCE(BusinessEventManager)
30
31 namespace {
32 const std::string DP_REJECT_KEY = "business_id_cast+_reject_event";
33 const std::string DP_DISTURBANCE_KEY = "business_id_cast+_disturbance_event";
34 const std::string STORE_ID = "dp_kv_store_business";
35 const std::string TAG = "BusinessEventManager";
36 const std::set<std::string> validKeys_ = { DP_REJECT_KEY, DP_DISTURBANCE_KEY };
37 }
38
Init()39 int32_t BusinessEventManager::Init()
40 {
41 HILOGI("call!");
42 std::lock_guard<std::mutex> lock(dynamicStoreMutex_);
43 businessEventAdapter_ = std::make_shared<BusinessEventAdapter>(
44 std::make_shared<KvDeathRecipient>(STORE_ID), DistributedKv::TYPE_DYNAMICAL);
45 int32_t ret = businessEventAdapter_->Init();
46 if (ret != DP_SUCCESS) {
47 HILOGE("businessEventAdapter init failed, ret: %{public}d", ret);
48 return DP_INIT_DB_FAILED;
49 }
50 HILOGI("Init finish, ret: %{public}d", ret);
51 return DP_SUCCESS;
52 }
53
UnInit()54 int32_t BusinessEventManager::UnInit()
55 {
56 HILOGI("call!");
57 {
58 std::lock_guard<std::mutex> lock(dynamicStoreMutex_);
59 if (businessEventAdapter_ == nullptr) {
60 HILOGE("businessEventAdapter_ is nullptr");
61 return DP_KV_DB_PTR_NULL;
62 }
63 businessEventAdapter_->UnInit();
64 businessEventAdapter_ = nullptr;
65 }
66 HILOGI("UnInit success");
67 return DP_SUCCESS;
68 }
69
PutBusinessEvent(const BusinessEvent & event)70 int32_t BusinessEventManager::PutBusinessEvent(const BusinessEvent& event)
71 {
72 if (event.GetBusinessKey().empty() || event.GetBusinessValue().empty()) {
73 HILOGE("Invalid parameter: Key or Value is empty.");
74 return DP_INVALID_PARAM;
75 }
76 if (!IsValidKey(event.GetBusinessKey())) {
77 HILOGE("Invalid BusinessKey: %{public}s", event.GetBusinessKey().c_str());
78 return DP_INVALID_PARAM;
79 }
80
81 std::string key = event.GetBusinessKey();
82 std::string value = event.GetBusinessValue();
83 {
84 std::lock_guard<std::mutex> lock(dynamicStoreMutex_);
85 if (businessEventAdapter_ == nullptr) {
86 HILOGE("businessEventAdapter_ is null");
87 return DP_KV_DB_PTR_NULL;
88 }
89 int32_t ret = businessEventAdapter_->Put(key, value);
90 if (ret != DP_SUCCESS) {
91 HILOGE("Failed to insert business event with key: %{public}s", key.c_str());
92 return DP_PUT_BUSINESS_EVENT_FAIL;
93 }
94 }
95 HILOGI("PutBusinessEvent success for key: %{public}s", key.c_str());
96 return DP_SUCCESS;
97 }
98
GetBusinessEvent(BusinessEvent & event)99 int32_t BusinessEventManager::GetBusinessEvent(BusinessEvent& event)
100 {
101 if (event.GetBusinessKey().empty()) {
102 HILOGE("Invalid parameter: BusinessKey is empty");
103 return DP_INVALID_PARAM;
104 }
105 if (!IsValidKey(event.GetBusinessKey())) {
106 HILOGE("Invalid BusinessKey: %{public}s", event.GetBusinessKey().c_str());
107 return DP_INVALID_PARAM;
108 }
109
110 std::string key = event.GetBusinessKey();
111 std::string value;
112 {
113 std::lock_guard<std::mutex> lock(dynamicStoreMutex_);
114 if (businessEventAdapter_ == nullptr) {
115 HILOGE("businessEventAdapter_ is null");
116 return DP_KV_DB_PTR_NULL;
117 }
118 int32_t ret = businessEventAdapter_->Get(key, value);
119 if (ret != DP_SUCCESS) {
120 HILOGE("Failed to get business event with key: %{public}s", key.c_str());
121 return DP_GET_BUSINESS_EVENT_FAIL;
122 }
123 event.SetBusinessValue(value);
124 }
125 HILOGI("GetBusinessEvent success for key: %{public}s", key.c_str());
126 return DP_SUCCESS;
127 }
128
IsValidKey(const std::string & key)129 bool BusinessEventManager::IsValidKey(const std::string& key)
130 {
131 return validKeys_.find(key) != validKeys_.end();
132 }
133 } // namespace DistributedDeviceProfile
134 } // namespace OHOS
135