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