1 /* 2 * Copyright (c) 2023-2025 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 BUS_EXTENSION_CORE_H 16 #define BUS_EXTENSION_CORE_H 17 18 #include <memory> 19 #include <unordered_map> 20 #include "ext_object.h" 21 #include "idev_change_callback.h" 22 #include "single_instance.h" 23 #include "ibus_extension.h" 24 #include "idriver_change_callback.h" 25 26 namespace OHOS { 27 namespace ExternalDeviceManager { 28 class BusExtensionCore { 29 DECLARE_SINGLE_INSTANCE_BASE(BusExtensionCore); 30 31 public: 32 ~BusExtensionCore() = default; 33 int32_t Init(std::shared_ptr<IDevChangeCallback> callback); 34 int32_t Register(BusType busType, std::shared_ptr<IBusExtension> busExtension); 35 std::shared_ptr<IBusExtension> GetBusExtensionByName(std::string busName); 36 static BusType GetBusTypeByName(const std::string &busName); 37 void LoadBusExtensionLibs(); 38 std::shared_ptr<IDriverChangeCallback> AcquireDriverChangeCallback(BusType busType); 39 40 private: 41 BusExtensionCore() = default; 42 std::unordered_map<BusType, std::shared_ptr<IBusExtension>> busExtensions_; 43 const uint32_t MAX_BUS_EXTENSIONS = 100; 44 static std::unordered_map<std::string, BusType> busTypeMap_; 45 }; 46 47 // bus extension should register by __attribute__ ((constructor)) when loading so 48 template <typename BusExtension> RegisterBusExtension(BusType busType)49void RegisterBusExtension(BusType busType) 50 { 51 BusExtensionCore::GetInstance().Register(busType, std::make_shared<BusExtension>()); 52 } 53 } // namespace ExternalDeviceManager 54 } // namespace OHOS 55 #endif // BUS_EXTENSION_CORE_H