1 /*
2 * Copyright (c) 2024 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 "switch_adapter.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 "kv_data_change_listener.h"
22 #include "types.h"
23
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 IMPLEMENT_SINGLE_INSTANCE(SwitchAdapter);
27
28 namespace {
29 const std::string TAG = "SwitchAdapter";
30 }
31
Init()32 void SwitchAdapter::Init()
33 {
34 HILOGI("called");
35 std::lock_guard<std::mutex> lock(switchAdapterMutex_);
36 if (observer_ == nullptr) {
37 observer_ = std::make_shared<KvDataChangeListener>(EMPTY_STRING);
38 }
39 }
40
Uninit()41 void SwitchAdapter::Uninit() {}
42
PutSwitch(const std::string & appId,uint32_t value,uint16_t length)43 int32_t SwitchAdapter::PutSwitch(const std::string& appId, uint32_t value, uint16_t length)
44 {
45 HILOGD("called");
46 if (appId.empty()) {
47 HILOGE("params are invalid");
48 return DP_INVALID_PARAMS;
49 }
50 DistributedKv::AppId appID;
51 appID.appId = appId;
52 const DistributedKv::SwitchData switchData = {value, length};
53 DistributedKv::Status res = kvDataMgr_.PutSwitch(appID, switchData);
54 if (res != DistributedKv::Status::SUCCESS) {
55 HILOGE("PutSwitch kv to db failed, ret: %{public}d", res);
56 return DP_PUT_KV_DB_FAIL;
57 }
58 return DP_SUCCESS;
59 }
60
GetSwitch(const std::string & appId,const std::string & networkId,uint32_t & value,uint32_t & switchLength)61 int32_t SwitchAdapter::GetSwitch(const std::string& appId, const std::string& networkId,
62 uint32_t& value, uint32_t& switchLength)
63 {
64 HILOGD("called");
65 if (appId.empty() || networkId.empty()) {
66 HILOGE("params are invalid");
67 return DP_INVALID_PARAMS;
68 }
69 DistributedKv::AppId appID;
70 appID.appId = appId;
71 auto res = kvDataMgr_.GetSwitch(appID, networkId);
72 if (res.first != DistributedKv::Status::SUCCESS) {
73 HILOGE("switch from db failed, ret: %{public}d", res.first);
74 return DP_GET_KV_DB_FAIL;
75 }
76 HILOGI("switch value %{public}u, length : %{public}u", res.second.value, res.second.length);
77 value = res.second.value;
78 switchLength = res.second.length;
79 return DP_SUCCESS;
80 }
81
SubscribeSwitchData(const std::string & appId)82 int32_t SwitchAdapter::SubscribeSwitchData(const std::string& appId)
83 {
84 HILOGI("called");
85 if (appId.empty()) {
86 HILOGE("appid is empty");
87 return DP_INVALID_PARAMS;
88 }
89 DistributedKv::AppId appID;
90 appID.appId = appId;
91 std::lock_guard<std::mutex> lock(switchAdapterMutex_);
92 if (observer_ == nullptr) {
93 HILOGE("observer_ is empty");
94 return DP_INVALID_PARAMS;
95 }
96 auto res = kvDataMgr_.SubscribeSwitchData(appID, observer_);
97 if (res != DistributedKv::Status::SUCCESS) {
98 HILOGE("SubscribeSwitchData failed, ret: %{public}d", res);
99 return DP_SUBSCRIBE_FAILED;
100 }
101 return DP_SUCCESS;
102 }
103
UnsubscribeSwitchData(const std::string & appId)104 int32_t SwitchAdapter::UnsubscribeSwitchData(const std::string& appId)
105 {
106 HILOGI("called");
107 if (appId.empty()) {
108 HILOGE("appid is empty");
109 return DP_INVALID_PARAMS;
110 }
111
112 DistributedKv::AppId appID;
113 appID.appId = appId;
114 std::lock_guard<std::mutex> lock(switchAdapterMutex_);
115 auto res = kvDataMgr_.UnsubscribeSwitchData(appID, observer_);
116 if (res != DistributedKv::Status::SUCCESS) {
117 HILOGE("UnSubscribeSwitchData failed, ret: %{public}d", res);
118 return DP_UNSUBSCRIBE_FAILED;
119 }
120 return DP_SUCCESS;
121 }
122 } // namespace DeviceProfile
123 } // namespace OHOS
124