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