• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 "driver_extension_controller.h"
24 #include "ext_object.h"
25 #include "idriver_ext_mgr_callback.h"
26 
27 namespace OHOS {
28 namespace ExternalDeviceManager {
29 class DrvExtConnNotify;
30 class Device : public std::enable_shared_from_this<Device> {
31 public:
Device(std::shared_ptr<DeviceInfo> info)32     explicit Device(std::shared_ptr<DeviceInfo> info) : info_(info) {}
33 
34     int32_t Connect();
35     int32_t Connect(const sptr<IDriverExtMgrCallback> &connectCallback);
36     int32_t Disconnect();
37 
HasDriver()38     bool HasDriver() const
39     {
40         return !bundleInfo_.empty();
41     };
42 
GetDeviceInfo()43     std::shared_ptr<DeviceInfo> GetDeviceInfo() const
44     {
45         return info_;
46     }
47 
AddBundleInfo(const std::string & bundleInfo)48     void AddBundleInfo(const std::string &bundleInfo)
49     {
50         bundleInfo_ = bundleInfo;
51     }
52 
RemoveBundleInfo()53     void RemoveBundleInfo()
54     {
55         bundleInfo_.clear();
56     }
57 
GetBundleInfo()58     std::string GetBundleInfo() const
59     {
60         return bundleInfo_;
61     }
62 
GetStiching()63     static inline std::string GetStiching()
64     {
65         return stiching_;
66     }
67 
GetDrvExtRemote()68     sptr<IRemoteObject> GetDrvExtRemote()
69     {
70         return drvExtRemote_;
71     }
72 
UpdateDrvExtRemote(const sptr<IRemoteObject> & remote)73     void UpdateDrvExtRemote(const sptr<IRemoteObject> &remote)
74     {
75         drvExtRemote_ = remote;
76     }
77 
AddDrvExtConnNotify()78     void AddDrvExtConnNotify()
79     {
80         if (connectNofitier_ == nullptr) {
81             connectNofitier_ = std::make_shared<DrvExtConnNotify>(shared_from_this());
82         }
83     }
84 
RemoveDrvExtConnNotify()85     void RemoveDrvExtConnNotify()
86     {
87         connectNofitier_ = nullptr;
88     }
89 
IsUnRegisted()90     bool IsUnRegisted()
91     {
92         return isUnRegisted;
93     }
94 
UnRegist()95     void UnRegist()
96     {
97         isUnRegisted = true;
98     }
99 
100     static std::string GetBundleName(const std::string &bundleInfo);
101     static std::string GetAbilityName(const std::string &bundleInfo);
102 
103 private:
104     void OnConnect(const sptr<IRemoteObject> &remote, int resultCode);
105     void OnDisconnect(int resultCode);
106     void UpdateDrvExtConnNotify();
107     int32_t RegisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
108     void UnregisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
109     void UnregisterDrvExtMgrCallback(const wptr<IRemoteObject> &object);
110     bool RegisteDeathRecipient(const sptr<IDriverExtMgrCallback> &callback);
111 
112     struct DrvExtMgrCallbackCompare {
operatorDrvExtMgrCallbackCompare113         bool operator()(const sptr<IDriverExtMgrCallback> &lhs, const sptr<IDriverExtMgrCallback> &rhs) const
114         {
115             sptr<IRemoteObject> lhsRemote = lhs->AsObject();
116             sptr<IRemoteObject> rhsRemote = rhs->AsObject();
117             if (lhsRemote != rhsRemote) {
118                 return false;
119             }
120 
121             return lhsRemote.GetRefPtr() < rhsRemote.GetRefPtr();
122         }
123     };
124 
125     friend class DriverExtMgrCallbackDeathRecipient;
126     friend class DrvExtConnNotify;
127     static std::string stiching_;
128     std::string bundleInfo_;
129     std::shared_ptr<DriverInfo> driver_;
130     std::shared_ptr<DeviceInfo> info_;
131 
132     std::recursive_mutex deviceMutex_;
133     sptr<IRemoteObject> drvExtRemote_;
134     std::set<sptr<IDriverExtMgrCallback>, DrvExtMgrCallbackCompare> callbacks_;
135     std::shared_ptr<DrvExtConnNotify> connectNofitier_;
136     bool isUnRegisted = false;
137 };
138 
139 class DriverExtMgrCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
140 public:
DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device)141     DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device) : device_(device) {}
142     ~DriverExtMgrCallbackDeathRecipient() = default;
143     void OnRemoteDied(const wptr<IRemoteObject> &remote);
144 
145 private:
146     DISALLOW_COPY_AND_MOVE(DriverExtMgrCallbackDeathRecipient);
147     std::weak_ptr<Device> device_;
148 };
149 
150 class DrvExtConnNotify : public IDriverExtensionConnectCallback {
151 public:
DrvExtConnNotify(std::weak_ptr<Device> device)152     explicit DrvExtConnNotify(std::weak_ptr<Device> device) : device_(device) {}
153     int32_t OnConnectDone(const sptr<IRemoteObject> &remote, int resultCode) override;
154     int32_t OnDisconnectDone(int resultCode) override;
IsInvalidDrvExtConnectionInfo()155     bool IsInvalidDrvExtConnectionInfo()
156     {
157         return info_ == nullptr;
158     }
ClearDrvExtConnectionInfo()159     void ClearDrvExtConnectionInfo()
160     {
161         info_ = nullptr;
162     }
163 
164 private:
165     std::weak_ptr<Device> device_;
166 };
167 } // namespace ExternalDeviceManager
168 } // namespace OHOS
169 #endif // OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H
170