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 #ifndef LINK_MANAGER_H 17 #define LINK_MANAGER_H 18 19 #include <list> 20 #include "dfx/link_snapshot.h" 21 22 namespace OHOS::SoftBus { 23 class LinkManager { 24 public: 25 static LinkManager& GetInstance(); 26 27 using Handler = std::function<void(InnerLink &)>; 28 using Checker = std::function<bool(InnerLink &)>; 29 30 int AllocateLinkId(); 31 std::shared_ptr<InnerLink> GetLinkById(int linkId); 32 33 void ForEach(const Checker &checker); 34 bool ProcessIfPresent(InnerLink::LinkType type, const std::string &remoteDeviceId, const Handler &handler); 35 bool ProcessIfAbsent(InnerLink::LinkType type, const std::string &remoteDeviceId, const Handler &handler); 36 37 bool ProcessIfPresent(const std::string &remoteMac, const Handler &handler); 38 bool ProcessIfAbsent(const std::string &remoteMac, const Handler &handler); 39 40 bool ProcessIfPresent(int linkId, const Handler &handler); 41 void RemoveLink(InnerLink::LinkType type, const std::string &remoteDeviceId); 42 void RemoveLink(const std::string &remoteMac); 43 void RemoveLinks(InnerLink::LinkType type, bool onlyRemoveConnected = false); 44 45 void GetAllLinksBasicInfo(std::vector<InnerLinkBasicInfo> &infos); 46 47 std::shared_ptr<InnerLink> GetReuseLink(const std::string &remoteMac); 48 std::shared_ptr<InnerLink> GetReuseLink(WifiDirectConnectType connectType, const std::string &remoteDeviceId); 49 std::shared_ptr<InnerLink> GetReuseLink(WifiDirectLinkType linkType, const std::string &remoteDeviceId); 50 std::string GetRemoteMacByRemoteDeviceId(const std::string &remoteDeviceId); 51 void RefreshRelationShip(const std::string &remoteDeviceId, const std::string &remoteMac); 52 bool RefreshAuthHandle(std::string remoteDeviceId, const std::shared_ptr<NegotiateChannel> &channel); 53 bool CheckOnlyVirtualLink(void); 54 55 void Dump() const; 56 57 void Dump(std::list<std::shared_ptr<LinkSnapshot>> &snapshots); 58 59 private: 60 mutable std::recursive_mutex lock_; 61 /* key = {LinkType, RemoteDeviceId} */ 62 std::map<std::pair<InnerLink::LinkType, std::string>, std::shared_ptr<InnerLink>> links_; 63 std::atomic<int> currentLinkId_ = 0; 64 }; 65 } 66 #endif 67