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 "interface_manager.h"
17 
18 #include "adapter/p2p_adapter.h"
19 #include "conn_log.h"
20 #include "data/interface_info.h"
21 #include "link_info.h"
22 #include "utils/wifi_direct_anonymous.h"
23 #include "utils/wifi_direct_utils.h"
24 
25 namespace OHOS::SoftBus {
UpdateInterface(InterfaceInfo::InterfaceType type,const Updater & updater)26 int InterfaceManager::UpdateInterface(InterfaceInfo::InterfaceType type, const Updater &updater)
27 {
28     std::unique_lock lock(lock_);
29     return updater(interfaces_[static_cast<int>(type)]);
30 }
31 
ReadInterface(InterfaceInfo::InterfaceType type,const Reader & reader)32 int InterfaceManager::ReadInterface(InterfaceInfo::InterfaceType type, const Reader &reader)
33 {
34     std::shared_lock lock(lock_);
35     return reader(interfaces_[static_cast<int>(type)]);
36 }
37 
IsInterfaceAvailable(InterfaceInfo::InterfaceType type,bool forShare) const38 bool InterfaceManager::IsInterfaceAvailable(InterfaceInfo::InterfaceType type, bool forShare) const
39 {
40     std::shared_lock lock(lock_);
41     auto info = interfaces_[static_cast<int>(type)];
42     if (!info.IsEnable()) {
43         CONN_LOGW(CONN_WIFI_DIRECT, "isEnable=0, interface type=%{public}d", static_cast<int>(type));
44         return false;
45     }
46 
47     if (info.GetRole() == LinkInfo::LinkMode::GC) {
48         CONN_LOGW(CONN_WIFI_DIRECT, "already gc");
49         return false;
50     }
51 
52     (void)forShare;
53     return true;
54 }
55 
LockInterface(InterfaceInfo::InterfaceType type,const std::string & owner)56 void InterfaceManager::LockInterface(InterfaceInfo::InterfaceType type, const std::string &owner)
57 {
58     // ATTENTION: MUST NOT access interface lock under interface manager lock, otherwise deadlock will happen
59     CONN_LOGI(CONN_WIFI_DIRECT, "current owner=%{public}s",
60         WifiDirectAnonymizeDeviceId(exclusives_[static_cast<int>(type)].owner_).c_str());
61     exclusives_[static_cast<int>(type)].lock_.lock();
62     exclusives_[static_cast<int>(type)].owner_ = owner;
63     CONN_LOGI(CONN_WIFI_DIRECT, "success owner=%{public}s",
64         WifiDirectAnonymizeDeviceId(exclusives_[static_cast<int>(type)].owner_).c_str());
65 }
66 
UnlockInterface(InterfaceInfo::InterfaceType type)67 void InterfaceManager::UnlockInterface(InterfaceInfo::InterfaceType type)
68 {
69     CONN_LOGI(CONN_WIFI_DIRECT, "current owner=%{public}s",
70         WifiDirectAnonymizeDeviceId(exclusives_[static_cast<int>(type)].owner_).c_str());
71     // ATTENTION: MUST NOT access interface lock under interface manager lock, otherwise deadlock will happen
72     exclusives_[static_cast<int>(type)].lock_.unlock();
73     exclusives_[static_cast<int>(type)].owner_ = "";
74 }
75 
InitInterface(InterfaceInfo::InterfaceType type)76 void InterfaceManager::InitInterface(InterfaceInfo::InterfaceType type)
77 {
78     std::string name;
79     int32_t capability = 0;
80     if (type == InterfaceInfo::InterfaceType::P2P) {
81         name = IF_NAME_P2P0;
82         capability = static_cast<int32_t>(LinkInfo::LinkMode::GO) | static_cast<uint32_t>(LinkInfo::LinkMode::GC);
83     }
84     if (type == InterfaceInfo::InterfaceType::HML) {
85         name = IF_NAME_HML;
86         capability = static_cast<int32_t>(LinkInfo::LinkMode::HML);
87     }
88     interfaces_[type].SetRole(LinkInfo::LinkMode::NONE);
89     interfaces_[type].SetName(name);
90     interfaces_[type].SetIsEnable(P2pAdapter::IsWifiP2pEnabled());
91     interfaces_[type].SetBaseMac(WifiDirectUtils::MacArrayToString(WifiDirectUtils::GetInterfaceMacAddr(name)));
92     interfaces_[type].SetCapability(capability);
93 }
94 
Init()95 void InterfaceManager::Init()
96 {
97     GetInstance().InitInterface(InterfaceInfo::InterfaceType::P2P);
98     GetInstance().InitInterface(InterfaceInfo::InterfaceType::HML);
99 }
100 } // namespace OHOS::SoftBus