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 
16 #ifndef DEVICE_MANAGER_ETX_DEVICE_MGR_H
17 #define DEVICE_MANAGER_ETX_DEVICE_MGR_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include "device.h"
25 #include "ext_object.h"
26 #include "single_instance.h"
27 #include "timer.h"
28 
29 namespace OHOS {
30 namespace ExternalDeviceManager {
31 using namespace std;
32 class ExtDeviceManager final {
33     DECLARE_SINGLE_INSTANCE_BASE(ExtDeviceManager);
34 
35 public:
36     ~ExtDeviceManager();
37     int32_t Init();
38     int32_t RegisterDevice(shared_ptr<DeviceInfo> devInfo);
39     int32_t UnRegisterDevice(const shared_ptr<DeviceInfo> devInfo);
40     vector<shared_ptr<DeviceInfo>> QueryDevice(const BusType busType);
41     vector<shared_ptr<Device>> QueryAllDevices();
42     vector<shared_ptr<Device>> QueryDevicesById(const uint64_t deviceId);
43     int32_t ConnectDevice(uint64_t deviceId, const sptr<IDriverExtMgrCallback> &connectCallback);
44     int32_t DisConnectDevice(uint64_t deviceId);
45     void RemoveDeviceOfDeviceMap(shared_ptr<Device> device);
46     void UnLoadSA(void);
47     std::unordered_set<uint64_t> DeleteBundlesOfBundleInfoMap(const std::string &bundleName = "");
48     void MatchDriverInfos(std::unordered_set<uint64_t> deviceIds, bool isDriversReset = false);
49     void ClearMatchedDrivers(const int32_t userId);
50 
51 private:
52     ExtDeviceManager() = default;
53     void PrintMatchDriverMap();
54     int32_t AddDevIdOfBundleInfoMap(shared_ptr<Device> device, string &bundleInfo);
55     int32_t RemoveDevIdOfBundleInfoMap(shared_ptr<Device> device, string &bundleInfo);
56     std::shared_ptr<Device> QueryDeviceByDeviceID(uint64_t deviceId);
57     void UnLoadSelf(void);
58     size_t GetTotalDeviceNum(void) const;
59     unordered_map<BusType, unordered_map<uint64_t, shared_ptr<Device>>> deviceMap_;
60     unordered_map<string, unordered_set<uint64_t>> bundleMatchMap_; // driver matching table
61     mutex deviceMapMutex_;
62     mutex bundleMatchMapMutex_;
63     Utils::Timer unloadSelftimer_ {"unLoadSelfTimer"};
64     uint32_t unloadSelftimerId_ {UINT32_MAX};
65 };
66 } // namespace ExternalDeviceManager
67 } // namespace OHOS
68 #endif // DEVICE_MANAGER_ETX_DEVICE_MGR_H