• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 "idriver_change_callback.h"
27 #include "single_instance.h"
28 #include "timer.h"
29 
30 namespace OHOS {
31 namespace ExternalDeviceManager {
32 using namespace std;
33 class ExtDeviceManager final {
34     DECLARE_SINGLE_INSTANCE_BASE(ExtDeviceManager);
35 
36 public:
37     ~ExtDeviceManager();
38     int32_t Init();
39     int32_t RegisterDevice(shared_ptr<DeviceInfo> devInfo);
40     int32_t UnRegisterDevice(const shared_ptr<DeviceInfo> devInfo);
41     vector<shared_ptr<DeviceInfo>> QueryDevice(const BusType busType);
42     vector<shared_ptr<Device>> QueryAllDevices();
43     vector<shared_ptr<Device>> QueryDevicesById(const uint64_t deviceId);
44     int32_t ConnectDevice(uint64_t deviceId, uint32_t callingTokenId,
45         const sptr<IDriverExtMgrCallback> &connectCallback);
46     int32_t DisConnectDevice(uint64_t deviceId, uint32_t callingTokenId);
47     int32_t ConnectDriverWithDeviceId(uint64_t deviceId, uint32_t callingTokenId,
48         const unordered_set<std::string> &accessibleBundles, const sptr<IDriverExtMgrCallback> &connectCallback);
49     int32_t DisConnectDriverWithDeviceId(uint64_t deviceId, uint32_t callingTokenId);
50     void RemoveDeviceOfDeviceMap(shared_ptr<Device> device);
51     std::unordered_set<uint64_t> DeleteBundlesOfBundleInfoMap(const std::string &bundleName = "");
52     void MatchDriverInfos(std::unordered_set<uint64_t> deviceIds);
53     void ClearMatchedDrivers(const int32_t userId);
54     void SetDriverChangeCallback(shared_ptr<IDriverChangeCallback> &driverChangeCallback);
55 
56 private:
57     ExtDeviceManager() = default;
58     void PrintMatchDriverMap();
59     int32_t AddDevIdOfBundleInfoMap(shared_ptr<Device> device, string &bundleInfo);
60     int32_t RemoveDevIdOfBundleInfoMap(shared_ptr<Device> device, string &bundleInfo);
61     void UpdateDriverInfo(const shared_ptr<Device> &device);
62     void RemoveDriverInfo(const shared_ptr<Device> &device);
63     std::shared_ptr<Device> QueryDeviceByDeviceID(uint64_t deviceId);
64     void UnLoadSelf(void);
65     size_t GetTotalDeviceNum(void) const;
66     int32_t CheckAccessPermission(const std::shared_ptr<DriverInfo> &driverInfo,
67         const unordered_set<std::string> &accessibleBundles) const;
68     unordered_map<BusType, unordered_map<uint64_t, shared_ptr<Device>>> deviceMap_;
69     unordered_map<string, unordered_set<uint64_t>> bundleMatchMap_; // driver matching table
70     mutex deviceMapMutex_;
71     mutex bundleMatchMapMutex_;
72     Utils::Timer unloadSelftimer_ {"unLoadSelfTimer"};
73     uint32_t unloadSelftimerId_ {UINT32_MAX};
74     std::shared_ptr<IDriverChangeCallback> driverChangeCallback_ = nullptr;
75 };
76 } // namespace ExternalDeviceManager
77 } // namespace OHOS
78 #endif // DEVICE_MANAGER_ETX_DEVICE_MGR_H