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