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 48 void AddBundleInfo(const std::string &bundleInfo, const std::string &driverUid = "") 49 { 50 bundleInfo_ = bundleInfo; 51 driverUid_ = driverUid; 52 } 53 GetDriverUid()54 std::string GetDriverUid() 55 { 56 return driverUid_; 57 } 58 RemoveBundleInfo()59 void RemoveBundleInfo() 60 { 61 bundleInfo_.clear(); 62 driverUid_.clear(); 63 } 64 GetBundleInfo()65 std::string GetBundleInfo() const 66 { 67 return bundleInfo_; 68 } 69 GetStiching()70 static inline std::string GetStiching() 71 { 72 return stiching_; 73 } 74 GetDrvExtRemote()75 sptr<IRemoteObject> GetDrvExtRemote() 76 { 77 return drvExtRemote_; 78 } 79 UpdateDrvExtRemote(const sptr<IRemoteObject> & remote)80 void UpdateDrvExtRemote(const sptr<IRemoteObject> &remote) 81 { 82 drvExtRemote_ = remote; 83 } 84 ClearDrvExtRemote()85 void ClearDrvExtRemote() 86 { 87 drvExtRemote_ = nullptr; 88 } 89 AddDrvExtConnNotify()90 void AddDrvExtConnNotify() 91 { 92 if (connectNofitier_ == nullptr) { 93 connectNofitier_ = std::make_shared<DrvExtConnNotify>(shared_from_this()); 94 } 95 } 96 RemoveDrvExtConnNotify()97 void RemoveDrvExtConnNotify() 98 { 99 connectNofitier_ = nullptr; 100 } 101 IsUnRegisted()102 bool IsUnRegisted() 103 { 104 return isUnRegisted; 105 } 106 UnRegist()107 void UnRegist() 108 { 109 isUnRegisted = true; 110 } 111 112 static std::string GetBundleName(const std::string &bundleInfo); 113 static std::string GetAbilityName(const std::string &bundleInfo); 114 115 private: 116 void OnConnect(const sptr<IRemoteObject> &remote, int resultCode); 117 void OnDisconnect(int resultCode); 118 void UpdateDrvExtConnNotify(); 119 int32_t RegisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback); 120 void UnregisterDrvExtMgrCallback(const sptr<IDriverExtMgrCallback> &callback); 121 void UnregisterDrvExtMgrCallback(const wptr<IRemoteObject> &object); 122 bool RegisteDeathRecipient(const sptr<IDriverExtMgrCallback> &callback); 123 124 struct DrvExtMgrCallbackCompare { operatorDrvExtMgrCallbackCompare125 bool operator()(const sptr<IDriverExtMgrCallback> &lhs, const sptr<IDriverExtMgrCallback> &rhs) const 126 { 127 sptr<IRemoteObject> lhsRemote = lhs->AsObject(); 128 sptr<IRemoteObject> rhsRemote = rhs->AsObject(); 129 if (lhsRemote != rhsRemote) { 130 return false; 131 } 132 133 return lhsRemote.GetRefPtr() < rhsRemote.GetRefPtr(); 134 } 135 }; 136 137 friend class DriverExtMgrCallbackDeathRecipient; 138 friend class DrvExtConnNotify; 139 static std::string stiching_; 140 std::string bundleInfo_; 141 std::string driverUid_; 142 std::shared_ptr<DriverInfo> driver_; 143 std::shared_ptr<DeviceInfo> info_; 144 145 std::recursive_mutex deviceMutex_; 146 sptr<IRemoteObject> drvExtRemote_; 147 std::set<sptr<IDriverExtMgrCallback>, DrvExtMgrCallbackCompare> callbacks_; 148 std::shared_ptr<DrvExtConnNotify> connectNofitier_; 149 bool isUnRegisted = false; 150 }; 151 152 class DriverExtMgrCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 153 public: DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device)154 DriverExtMgrCallbackDeathRecipient(const std::weak_ptr<Device> device) : device_(device) {} 155 ~DriverExtMgrCallbackDeathRecipient() = default; 156 void OnRemoteDied(const wptr<IRemoteObject> &remote); 157 158 private: 159 DISALLOW_COPY_AND_MOVE(DriverExtMgrCallbackDeathRecipient); 160 std::weak_ptr<Device> device_; 161 }; 162 163 class DrvExtConnNotify : public IDriverExtensionConnectCallback { 164 public: DrvExtConnNotify(std::weak_ptr<Device> device)165 explicit DrvExtConnNotify(std::weak_ptr<Device> device) : device_(device) {} 166 int32_t OnConnectDone(const sptr<IRemoteObject> &remote, int resultCode) override; 167 int32_t OnDisconnectDone(int resultCode) override; IsInvalidDrvExtConnectionInfo()168 bool IsInvalidDrvExtConnectionInfo() 169 { 170 return info_ == nullptr; 171 } ClearDrvExtConnectionInfo()172 void ClearDrvExtConnectionInfo() 173 { 174 info_ = nullptr; 175 } 176 177 private: 178 std::weak_ptr<Device> device_; 179 }; 180 } // namespace ExternalDeviceManager 181 } // namespace OHOS 182 #endif // OHOS_EXTERNAL_DEVICE_MANAGER_DEVICE_H 183