• 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 
90     static std::string GetBundleName(const std::string &bundleInfo);
91     static std::string GetAbilityName(const std::string &bundleInfo);
92 
93 private:
94     void OnConnect(const sptr<IRemoteObject> &remote, int resultCode);
95     void OnDisconnect(int resultCode);
96     void UpdateDrvExtConnNotify();
97     int32_t RegisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
98     void UnregisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback);
99     void UnregisterDrvExtMgrCallback(const wptr<IRemoteObject> &object);
100     bool RegisteDeathRecipient(const sptr<IDriverExtMgrCallback> &callback);
101 
102     struct DrvExtMgrCallbackCompare {
operatorDrvExtMgrCallbackCompare103         bool operator()(const sptr<IDriverExtMgrCallback> &lhs, const sptr<IDriverExtMgrCallback> &rhs) const
104         {
105             sptr<IRemoteObject> lhsRemote = lhs->AsObject();
106             sptr<IRemoteObject> rhsRemote = rhs->AsObject();
107             if (lhsRemote != rhsRemote) {
108                 return false;
109             }
110 
111             return lhsRemote.GetRefPtr() < rhsRemote.GetRefPtr();
112         }
113     };
114 
115     friend class DriverExtMgrCallbackDeathRecipient;
116     friend class DrvExtConnNotify;
117     static std::string stiching_;
118     std::string bundleInfo_;
119     std::shared_ptr<DriverInfo> driver_;
120     std::shared_ptr<DeviceInfo> info_;
121 
122     std::recursive_mutex deviceMutex_;
123     sptr<IRemoteObject> drvExtRemote_;
124     std::set<sptr<IDriverExtMgrCallback>, DrvExtMgrCallbackCompare> callbacks_;
125     std::shared_ptr<DrvExtConnNotify> connectNofitier_;
126 };
127 
128 class DriverExtMgrCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
129 public:
DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device)130     DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device) : device_(device) {}
131     ~DriverExtMgrCallbackDeathRecipient() = default;
132     void OnRemoteDied(const wptr<IRemoteObject> &remote);
133 
134 private:
135     DISALLOW_COPY_AND_MOVE(DriverExtMgrCallbackDeathRecipient);
136     std::weak_ptr<Device> device_;
137 };
138 
139 class DrvExtConnNotify : public IDriverExtensionConnectCallback {
140 public:
DrvExtConnNotify(std::weak_ptr<Device> device)141     explicit DrvExtConnNotify(std::weak_ptr<Device> device) : device_(device) {}
142     int32_t OnConnectDone(const sptr<IRemoteObject> &remote, int resultCode) override;
143     int32_t OnDisconnectDone(int resultCode) override;
IsInvalidDrvExtConnectionInfo()144     bool IsInvalidDrvExtConnectionInfo()
145     {
146         return info_ == nullptr;
147     }
ClearDrvExtConnectionInfo()148     void ClearDrvExtConnectionInfo()
149     {
150         info_ = nullptr;
151     }
152 
153 private:
154     std::weak_ptr<Device> device_;
155 };
156 } // namespace ExternalDeviceManager
157 } // namespace OHOS
158 #endif // OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H
159