1 /*
2 * Copyright (c) 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 "communication_strategy.h"
17 #include "log_print.h"
18 #include "device_manager_adapter.h"
19 #include "kvstore_utils.h"
20 #ifdef LOG_TAG
21 #undef LOG_TAG
22 #endif
23 #define LOG_TAG "CommunicationStrategy"
24
25 namespace OHOS {
26 namespace AppDistributedKv {
27 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
28 using KvStoreUtils = OHOS::DistributedKv::KvStoreUtils;
GetInstance()29 CommunicationStrategy &CommunicationStrategy::GetInstance()
30 {
31 static CommunicationStrategy instance;
32 return instance;
33 }
34
Init()35 Status CommunicationStrategy::Init()
36 {
37 return DmAdapter::GetInstance().StartWatchDeviceChange(this, {"strategy"});
38 }
39
OnDeviceChanged(const AppDistributedKv::DeviceInfo & info,const AppDistributedKv::DeviceChangeType & type) const40 void CommunicationStrategy::OnDeviceChanged(const AppDistributedKv::DeviceInfo &info,
41 const AppDistributedKv::DeviceChangeType &type) const
42 {
43 UpdateCommunicationStrategy(info, type);
44 }
45
UpdateCommunicationStrategy(const AppDistributedKv::DeviceInfo & info,const AppDistributedKv::DeviceChangeType & type) const46 void CommunicationStrategy::UpdateCommunicationStrategy(const AppDistributedKv::DeviceInfo &info,
47 const AppDistributedKv::DeviceChangeType &type) const
48 {
49 ZLOGD("[UpdateCommunicationStrategy] to %{public}s, type:%{public}d",
50 KvStoreUtils::ToBeAnonymous(info.uuid).c_str(), type);
51 if (type == AppDistributedKv::DeviceChangeType::DEVICE_ONLINE) {
52 strategys_.InsertOrAssign(info.uuid, true);
53 } else if (type == AppDistributedKv::DeviceChangeType::DEVICE_ONREADY) {
54 strategys_.Erase(info.uuid);
55 } else {
56 ;
57 }
58 }
59
GetStrategy(const std::string & deviceId,int32_t dataLen,std::vector<LinkType> & linkTypes)60 bool CommunicationStrategy::GetStrategy(const std::string &deviceId, int32_t dataLen, std::vector<LinkType> &linkTypes)
61 {
62 if (dataLen < SWITCH_CONNECTION_THRESHOLD || !strategys_.Contains(deviceId)) {
63 return false;
64 }
65
66 linkTypes.emplace_back(LINK_TYPE_WIFI_WLAN_5G);
67 linkTypes.emplace_back(LINK_TYPE_WIFI_WLAN_2G);
68 linkTypes.emplace_back(LINK_TYPE_WIFI_P2P);
69 linkTypes.emplace_back(LINK_TYPE_BR);
70 return true;
71 }
72 } // namespace AppDistributedKv
73 } // namespace OHOS