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_profile_manager.h"
17
18 #include "distributed_device_profile_enums.h"
19 #include "distributed_device_profile_errors.h"
20 #include "distributed_device_profile_log.h"
21 #include "profile_cache.h"
22 #include "profile_control_utils.h"
23 #include "profile_utils.h"
24 #include "switch_adapter.h"
25
26 namespace OHOS {
27 namespace DistributedDeviceProfile {
28 IMPLEMENT_SINGLE_INSTANCE(SwitchProfileManager);
29 namespace {
30 const std::string TAG = "SwitchProfileManager";
31 const std::string APP_ID = "distributed_device_profile_service";
32 }
33
Init()34 int32_t SwitchProfileManager::Init()
35 {
36 HILOGI("call!");
37 SwitchAdapter::GetInstance().Init();
38 int32_t res = SwitchAdapter::GetInstance().SubscribeSwitchData(APP_ID);
39 if (res != DP_SUCCESS) {
40 HILOGE("SubscribeSwitchData failed, res: %{public}d", res);
41 return DP_INIT_SWITCH_PROFILE_MANAGER_FAIL;
42 }
43 return DP_SUCCESS;
44 }
45
UnInit()46 int32_t SwitchProfileManager::UnInit()
47 {
48 HILOGI("call!");
49 int32_t res = SwitchAdapter::GetInstance().UnsubscribeSwitchData(APP_ID);
50 if (res != DP_SUCCESS) {
51 HILOGE("UnsubscribeSwitchData failed, res: %{public}d", res);
52 return DP_UNSUBSCRIBE_FAILED;
53 }
54 return DP_SUCCESS;
55 }
56
ReInit()57 int32_t SwitchProfileManager::ReInit()
58 {
59 HILOGI("call!");
60 UnInit();
61 return Init();
62 }
63
PutCharacteristicProfile(const CharacteristicProfile & charProfile)64 int32_t SwitchProfileManager::PutCharacteristicProfile(const CharacteristicProfile& charProfile)
65 {
66 HILOGI("Profile : %{public}s!", charProfile.dump().c_str());
67 int32_t res = DP_SUCCESS;
68 {
69 std::lock_guard<std::mutex> lock(switchProfileMutex_);
70 res = ProfileControlUtils::PutSwitchCharacteristicProfile(APP_ID, charProfile);
71 }
72 if (res != DP_SUCCESS) {
73 HILOGE("PutCharacteristicProfile fail, reason: %{public}d!", res);
74 return res;
75 }
76 HILOGD("PutCharacteristicProfile success");
77 return DP_SUCCESS;
78 }
79
PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile> & charProfiles)80 int32_t SwitchProfileManager::PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& charProfiles)
81 {
82 HILOGI("charProfiles.size:%{public}zu", charProfiles.size());
83 int32_t res = 0;
84 {
85 std::lock_guard<std::mutex> lock(switchProfileMutex_);
86 res = ProfileControlUtils::PutSwitchCharacteristicProfileBatch(APP_ID, charProfiles);
87 }
88 if (res != DP_SUCCESS) {
89 HILOGE("fail, reason: %{public}d!", res);
90 return res;
91 }
92 HILOGD("success");
93 return DP_SUCCESS;
94 }
95
GetCharacteristicProfile(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)96 int32_t SwitchProfileManager::GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
97 const std::string& characteristicKey, CharacteristicProfile& charProfile)
98 {
99 int32_t res = 0;
100 {
101 std::lock_guard<std::mutex> lock(switchProfileMutex_);
102 res = ProfileControlUtils::GetSwitchCharacteristicProfile(APP_ID, deviceId, serviceName, characteristicKey,
103 charProfile);
104 }
105 if (res != DP_SUCCESS) {
106 HILOGE("GetSwitchCharacteristicProfile fail, reason: %{public}d!", res);
107 return res;
108 }
109 return DP_SUCCESS;
110 }
111
RefreshLocalSwitchProfile()112 int32_t SwitchProfileManager::RefreshLocalSwitchProfile()
113 {
114 HILOGD("call!");
115 int32_t res = ProfileControlUtils::RefreshLocalSwitchProfile(APP_ID);
116 if (res != DP_SUCCESS) {
117 HILOGE("RefreshLocalSwitchProfile fail, reason: %{public}d!", res);
118 return res;
119 }
120 HILOGD("GetSwitchCharacteristicProfile success");
121 return DP_SUCCESS;
122 }
123
GetLocalSwitchFromDB(uint32_t & localSwitch,uint32_t & switchLength)124 int32_t SwitchProfileManager::GetLocalSwitchFromDB(uint32_t& localSwitch, uint32_t& switchLength)
125 {
126 HILOGI("calll");
127 std::string netId = ProfileCache::GetInstance().GetLocalNetworkId();
128 return SwitchAdapter::GetInstance().GetSwitch(APP_ID, netId, localSwitch, switchLength);
129 }
130 } // namespace DistributedDeviceProfile
131 } // namespace OHOS
132