• 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 OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H
17 #define OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 #include "driver_extension_controller.h"
25 #include "ext_object.h"
26 #include "idriver_ext_mgr_callback.h"
27 
28 namespace OHOS {
29 namespace ExternalDeviceManager {
30 struct CallerInfo {
31     bool isBound = false;
32 };
33 
34 class DrvExtConnNotify;
35 class Device : public std::enable_shared_from_this<Device> {
36 public:
Device(std::shared_ptr<DeviceInfo> info)37     explicit Device(std::shared_ptr<DeviceInfo> info) : info_(info) {}
38 
39     int32_t Connect();
40     int32_t Connect(const sptr<IDriverExtMgrCallback> &connectCallback, uint32_t callingTokenId);
41     int32_t Disconnect(const bool isFromBind);
42 
HasDriver()43     bool HasDriver() const
44     {
45         return !bundleInfo_.empty();
46     };
47 
GetDriverInfo()48     std::shared_ptr<DriverInfo> GetDriverInfo() const
49     {
50         return driverInfo_;
51     }
52 
GetDeviceInfo()53     std::shared_ptr<DeviceInfo> GetDeviceInfo() const
54     {
55         return info_;
56     }
57 
AddBundleInfo(const std::string & bundleInfo,const std::shared_ptr<DriverInfo> & driverInfo)58     void AddBundleInfo(const std::string &bundleInfo, const std::shared_ptr<DriverInfo> &driverInfo)
59     {
60         bundleInfo_ = bundleInfo;
61         driverInfo_ = driverInfo;
62         if (driverInfo != nullptr) {
63             driverUid_ = driverInfo->GetDriverUid();
64         }
65     }
66 
GetDriverUid()67     std::string GetDriverUid()
68     {
69         return driverUid_;
70     }
71 
RemoveBundleInfo()72     void RemoveBundleInfo()
73     {
74         bundleInfo_.clear();
75         driverUid_.clear();
76     }
77 
RemoveDriverInfo()78     void RemoveDriverInfo()
79     {
80         driverInfo_ = nullptr;
81     }
82 
GetBundleInfo()83     std::string GetBundleInfo() const
84     {
85         return bundleInfo_;
86     }
87 
GetStiching()88     static inline std::string GetStiching()
89     {
90         return stiching_;
91     }
92 
GetDrvExtRemote()93     sptr<IRemoteObject> GetDrvExtRemote()
94     {
95         return drvExtRemote_;
96     }
97 
UpdateDrvExtRemote(const sptr<IRemoteObject> & remote)98     void UpdateDrvExtRemote(const sptr<IRemoteObject> &remote)
99     {
100         drvExtRemote_ = remote;
101     }
102 
ClearDrvExtRemote()103     void ClearDrvExtRemote()
104     {
105         drvExtRemote_ = nullptr;
106     }
107 
AddDrvExtConnNotify()108     void AddDrvExtConnNotify()
109     {
110         if (connectNofitier_ == nullptr) {
111             connectNofitier_ = std::make_shared<DrvExtConnNotify>(shared_from_this());
112         }
113     }
114 
RemoveDrvExtConnNotify()115     void RemoveDrvExtConnNotify()
116     {
117         connectNofitier_ = nullptr;
118     }
119 
IsUnRegisted()120     bool IsUnRegisted()
121     {
122         return isUnRegisted;
123     }
124 
UnRegist()125     void UnRegist()
126     {
127         isUnRegisted = true;
128     }
129 
RemoveCaller(uint32_t callingTokenId)130     void RemoveCaller(uint32_t callingTokenId)
131     {
132         boundCallerInfos_.erase(callingTokenId);
133     }
134 
ClearBoundCallerInfos()135     void ClearBoundCallerInfos()
136     {
137         boundCallerInfos_.clear();
138     }
139 
140     bool IsLastCaller(uint32_t caller) const;
141     bool IsBindCaller(uint32_t caller) const;
142 
143     static std::string GetBundleName(const std::string &bundleInfo);
144     static std::string GetAbilityName(const std::string &bundleInfo);
145 
146 private:
147     void OnConnect(const sptr<IRemoteObject> &remote, int resultCode);
148     void OnDisconnect(int resultCode);
149     void UpdateDrvExtConnNotify();
150     int32_t RegisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
151     void UnregisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
152     void UnregisterDrvExtMgrCallback(const wptr<IRemoteObject> &object);
153     bool RegisteDeathRecipient(const sptr<IDriverExtMgrCallback> &callback);
154 
155     struct DrvExtMgrCallbackCompare {
operatorDrvExtMgrCallbackCompare156         bool operator()(const sptr<IDriverExtMgrCallback> &lhs, const sptr<IDriverExtMgrCallback> &rhs) const
157         {
158             sptr<IRemoteObject> lhsRemote = lhs->AsObject();
159             sptr<IRemoteObject> rhsRemote = rhs->AsObject();
160             if (lhsRemote != rhsRemote) {
161                 return false;
162             }
163 
164             return lhsRemote.GetRefPtr() < rhsRemote.GetRefPtr();
165         }
166     };
167 
168     friend class DriverExtMgrCallbackDeathRecipient;
169     friend class DrvExtConnNotify;
170     static std::string stiching_;
171     std::string bundleInfo_;
172     std::string driverUid_;
173     std::shared_ptr<DriverInfo> driverInfo_;
174     std::shared_ptr<DeviceInfo> info_;
175 
176     std::recursive_mutex deviceMutex_;
177     sptr<IRemoteObject> drvExtRemote_;
178     std::set<sptr<IDriverExtMgrCallback>, DrvExtMgrCallbackCompare> callbacks_;
179     std::shared_ptr<DrvExtConnNotify> connectNofitier_;
180     bool isUnRegisted = false;
181     std::unordered_map<uint32_t, CallerInfo> boundCallerInfos_;
182 };
183 
184 class DriverExtMgrCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
185 public:
DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device)186     DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device) : device_(device) {}
187     ~DriverExtMgrCallbackDeathRecipient() = default;
188     void OnRemoteDied(const wptr<IRemoteObject> &remote);
189 
190 private:
191     DISALLOW_COPY_AND_MOVE(DriverExtMgrCallbackDeathRecipient);
192     std::weak_ptr<Device> device_;
193 };
194 
195 class DrvExtConnNotify : public IDriverExtensionConnectCallback {
196 public:
DrvExtConnNotify(std::weak_ptr<Device> device)197     explicit DrvExtConnNotify(std::weak_ptr<Device> device) : device_(device) {}
198     int32_t OnConnectDone(const sptr<IRemoteObject> &remote, int resultCode) override;
199     int32_t OnDisconnectDone(int resultCode) override;
IsInvalidDrvExtConnectionInfo()200     bool IsInvalidDrvExtConnectionInfo()
201     {
202         return info_ == nullptr;
203     }
ClearDrvExtConnectionInfo()204     void ClearDrvExtConnectionInfo()
205     {
206         info_ = nullptr;
207     }
208     int32_t GetCurrentActiveUserId();
209 
210 private:
211     std::weak_ptr<Device> device_;
212 };
213 } // namespace ExternalDeviceManager
214 } // namespace OHOS
215 #endif // OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H
216