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 #ifndef EXT_OBJECT_H 16 #define EXT_OBJECT_H 17 18 #include <memory> 19 #include <string> 20 #include "edm_errors.h" 21 namespace OHOS { 22 namespace ExternalDeviceManager { 23 enum BusType : uint32_t { 24 BUS_TYPE_INVALID = 0, 25 BUS_TYPE_USB = 1, 26 BUS_TYPE_MAX, 27 BUS_TYPE_TEST, 28 }; 29 30 class DrvBundleStateCallback; 31 class DriverInfoExt { 32 public: 33 virtual ~DriverInfoExt() = default; 34 virtual int32_t Serialize(std::string &str) = 0; 35 virtual int32_t UnSerialize(const std::string &str) = 0; 36 }; 37 38 class DriverInfo : public DriverInfoExt { 39 public: 40 DriverInfo() = default; 41 DriverInfo(const std::string &bundleName, const std::string &driverName, const std::string &driverUid = "") bundleName_(bundleName)42 : bundleName_(bundleName), driverName_(driverName) 43 { 44 if (driverUid.empty()) { 45 driverUid_ = bundleName + "-" + driverName; 46 } else { 47 driverUid_ = driverUid; 48 } 49 } 50 int32_t Serialize(std::string &str) override; 51 int32_t UnSerialize(const std::string &str) override; GetBusName()52 std::string GetBusName() const 53 { 54 return bus_; 55 } GetBusType()56 BusType GetBusType() const 57 { 58 return busType_; 59 } GetDriverUid()60 std::string GetDriverUid() const 61 { 62 return driverUid_; 63 } GetBundleName()64 std::string GetBundleName() const 65 { 66 return bundleName_; 67 } GetDriverName()68 std::string GetDriverName() const 69 { 70 return driverName_; 71 } GetVersion()72 std::string GetVersion() const 73 { 74 return version_; 75 } GetDescription()76 std::string GetDescription() const 77 { 78 return description_; 79 } GetDriverSize()80 std::string GetDriverSize() const 81 { 82 return driverSize_; 83 } GetLaunchOnBind()84 bool GetLaunchOnBind() const 85 { 86 return launchOnBind_; 87 } GetInfoExt()88 std::shared_ptr<DriverInfoExt> GetInfoExt() const 89 { 90 return driverInfoExt_; 91 } IsAccessAllowed()92 bool IsAccessAllowed() const 93 { 94 return accessAllowed_; 95 } 96 private: 97 friend class DrvBundleStateCallback; 98 std::string bus_; 99 BusType busType_{0}; 100 std::string driverUid_; 101 std::string bundleName_; 102 std::string driverName_; 103 std::string vendor_; 104 std::string version_; 105 std::string description_; 106 std::string driverSize_; 107 bool launchOnBind_ = false; 108 bool accessAllowed_ = false; 109 std::shared_ptr<DriverInfoExt> driverInfoExt_; 110 }; 111 112 class DeviceInfo { 113 public: 114 DeviceInfo( 115 uint32_t busDeviceId, 116 BusType busType = BusType::BUS_TYPE_INVALID, description_(description)117 const std::string &description = "") : description_(description) 118 { 119 devInfo_.devBusInfo.busType = busType; 120 devInfo_.devBusInfo.busDeviceId = busDeviceId; 121 } 122 virtual ~DeviceInfo() = default; GetBusType()123 BusType GetBusType() const 124 { 125 return devInfo_.devBusInfo.busType; 126 } GetDeviceId()127 uint64_t GetDeviceId() const 128 { 129 return devInfo_.deviceId; 130 } GetBusDevId()131 uint32_t GetBusDevId() const 132 { 133 return devInfo_.devBusInfo.busDeviceId; 134 } GetDeviceDescription()135 const std::string& GetDeviceDescription() const 136 { 137 return description_; 138 } 139 140 private: 141 union DevInfo { 142 uint64_t deviceId; 143 struct { 144 BusType busType; 145 uint32_t busDeviceId; 146 } devBusInfo; 147 } devInfo_; 148 std::string description_ {""}; 149 }; 150 } // namespace ExternalDeviceManager 151 } // namespace OHOS 152 #endif // EXT_OBJECT_H