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 #ifndef INTERFACE_MANAGER_H 16 #define INTERFACE_MANAGER_H 17 18 #include <functional> 19 #include <shared_mutex> 20 21 #include "data/interface_info.h" 22 #include "wifi_direct_initiator.h" 23 24 namespace OHOS::SoftBus { 25 class InterfaceManager { 26 public: GetInstance()27 static InterfaceManager& GetInstance() 28 { 29 static InterfaceManager instance; 30 return instance; 31 } 32 33 using Updater = std::function<int(InterfaceInfo &)>; 34 using Reader = std::function<int(const InterfaceInfo &)>; 35 36 int UpdateInterface(InterfaceInfo::InterfaceType type, const Updater &updater); 37 int ReadInterface(InterfaceInfo::InterfaceType type, const Reader &reader); 38 39 bool IsInterfaceAvailable(InterfaceInfo::InterfaceType type, bool forShare) const; 40 41 void LockInterface(InterfaceInfo::InterfaceType type, const std::string &owner); 42 void UnlockInterface(InterfaceInfo::InterfaceType type); 43 44 static void Init(); 45 void InitInterface(InterfaceInfo::InterfaceType type); 46 47 private: 48 class Initiator { 49 public: Initiator()50 Initiator() 51 { 52 WifiDirectInitiator::GetInstance().Add(InterfaceManager::Init); 53 } 54 }; 55 56 struct ExclusiveHelper { 57 std::shared_mutex lock_; 58 std::string owner_; 59 }; 60 61 static inline Initiator initiator_; 62 63 mutable std::shared_mutex lock_; 64 InterfaceInfo interfaces_[InterfaceInfo::MAX]; 65 ExclusiveHelper exclusives_[InterfaceInfo::MAX]; 66 }; 67 } // namespace OHOS::SoftBus 68 #endif 69