1 /* 2 * Copyright (c) 2021-2022 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 USB_DEVICE_H 17 #define USB_DEVICE_H 18 #include <iostream> 19 #include <singleton.h> 20 #include <sstream> 21 #include <vector> 22 23 #include "iremote_object.h" 24 #include "usb_config.h" 25 #include "usb_interface.h" 26 27 namespace OHOS { 28 namespace USB { 29 class UsbDevice { 30 public: UsbDevice(std::string name,std::string manufacturerName,std::string productName,std::string version,uint8_t devAddr,uint8_t busNum,int32_t vendorId,int32_t productId,int32_t klass,int32_t subClass,int32_t protocol,std::vector<USBConfig> configs)31 UsbDevice(std::string name, std::string manufacturerName, std::string productName, std::string version, 32 uint8_t devAddr, uint8_t busNum, int32_t vendorId, int32_t productId, int32_t klass, int32_t subClass, 33 int32_t protocol, std::vector<USBConfig> configs) 34 { 35 this->name_ = name; 36 this->manufacturerName_ = manufacturerName; 37 this->productName_ = productName; 38 this->version_ = version; 39 this->devAddr_ = devAddr; 40 this->busNum_ = busNum; 41 this->vendorId_ = vendorId; 42 this->productId_ = productId; 43 this->klass_ = klass; 44 this->subClass_ = subClass; 45 this->protocol_ = protocol; 46 this->configs_ = configs; 47 } 48 UsbDevice(const Json::Value & device)49 explicit UsbDevice(const Json::Value &device) 50 { 51 busNum_ = device["busNum"].asUInt(); 52 devAddr_ = device["devAddress"].asUInt(); 53 serial_ = device["serial"].asString(); 54 name_ = device["name"].asString(); 55 manufacturerName_ = device["manufacturerName"].asString(); 56 productName_ = device["productName"].asString(); 57 version_ = device["version"].asString(); 58 vendorId_ = device["vendorId"].asInt(); 59 productId_ = device["productId"].asInt(); 60 klass_ = device["clazz"].asInt(); 61 subClass_ = device["subClass"].asInt(); 62 protocol_ = device["protocol"].asInt(); 63 64 Json::Value configs = device["configs"]; 65 for (uint32_t idx = 0; idx < configs.size(); ++idx) { 66 configs_.emplace_back(configs[idx]); 67 } 68 } 69 UsbDevice()70 UsbDevice() {} ~UsbDevice()71 ~UsbDevice() {} 72 GetName()73 const std::string &GetName() const 74 { 75 return name_; 76 } 77 GetManufacturerName()78 const std::string &GetManufacturerName() const 79 { 80 return manufacturerName_; 81 } 82 GetProductName()83 const std::string &GetProductName() const 84 { 85 return productName_; 86 } 87 GetVersion()88 const std::string &GetVersion() const 89 { 90 return version_; 91 } 92 GetVendorId()93 int32_t GetVendorId() const 94 { 95 return vendorId_; 96 } 97 GetProductId()98 int32_t GetProductId() const 99 { 100 return productId_; 101 } 102 GetClass()103 int32_t GetClass() const 104 { 105 return klass_; 106 } 107 GetSubclass()108 int32_t GetSubclass() const 109 { 110 return subClass_; 111 } 112 GetProtocol()113 int32_t GetProtocol() const 114 { 115 return protocol_; 116 } 117 GetConfigCount()118 int32_t GetConfigCount() const 119 { 120 return configs_.size(); 121 } 122 GetConfig(uint32_t index,USBConfig & config)123 int32_t GetConfig(uint32_t index, USBConfig &config) const 124 { 125 if (index >= configs_.size()) { 126 return ERR_INVALID_VALUE; 127 } 128 config = configs_[index]; 129 return ERR_OK; 130 } 131 SetConfigs(const std::vector<USBConfig> & configs)132 void SetConfigs(const std::vector<USBConfig> &configs) 133 { 134 this->configs_ = configs; 135 } 136 GetDevAddr()137 uint8_t GetDevAddr() const 138 { 139 return devAddr_; 140 } 141 GetBusNum()142 uint8_t GetBusNum() const 143 { 144 return busNum_; 145 } 146 GetDescConfigCount()147 uint8_t GetDescConfigCount() 148 { 149 return descConfigCount_; 150 } 151 SetDevAddr(uint8_t addr)152 void SetDevAddr(uint8_t addr) 153 { 154 devAddr_ = addr; 155 } 156 SetBusNum(uint8_t num)157 void SetBusNum(uint8_t num) 158 { 159 busNum_ = num; 160 } 161 SetName(const std::string & name)162 void SetName(const std::string &name) 163 { 164 name_ = name; 165 } 166 SetManufacturerName(const std::string & manufacturerName)167 void SetManufacturerName(const std::string &manufacturerName) 168 { 169 manufacturerName_ = manufacturerName; 170 } 171 SetProductName(const std::string & productName)172 void SetProductName(const std::string &productName) 173 { 174 productName_ = productName; 175 } 176 SetVersion(const std::string & version)177 void SetVersion(const std::string &version) 178 { 179 version_ = version; 180 } 181 SetVendorId(int32_t vendorId)182 void SetVendorId(int32_t vendorId) 183 { 184 vendorId_ = vendorId; 185 } 186 SetProductId(int32_t productId)187 void SetProductId(int32_t productId) 188 { 189 productId_ = productId; 190 } 191 SetClass(int32_t deviceClass)192 void SetClass(int32_t deviceClass) 193 { 194 klass_ = deviceClass; 195 } 196 SetSubclass(int32_t subClass)197 void SetSubclass(int32_t subClass) 198 { 199 subClass_ = subClass; 200 } 201 SetProtocol(int32_t protocol)202 void SetProtocol(int32_t protocol) 203 { 204 protocol_ = protocol; 205 } 206 SetDescConfigCount(uint8_t count)207 void SetDescConfigCount(uint8_t count) 208 { 209 descConfigCount_ = count; 210 } 211 GetConfigs()212 std::vector<USBConfig> &GetConfigs() 213 { 214 return configs_; 215 } 216 ToString()217 std::string ToString() const 218 { 219 std::ostringstream ss; 220 ss << "name_=" << name_ << "," 221 << "manufacturerName_=" << manufacturerName_ << "," 222 << "productName_=" << productName_ << "," 223 << "version_=" << version_ << "," 224 << "serial_=" << serial_ << "," 225 << "busNum_=" << (int32_t)busNum_ << "," 226 << "devAddr_=" << (int32_t)devAddr_ << "," 227 << "vendorId_=" << vendorId_ << "," 228 << "productId_=" << productId_ << "," 229 << "klass_=" << klass_ << "," 230 << "subClass_=" << subClass_ << "," 231 << "protocol_=" << protocol_ << ""; 232 std::string str = "UsbDevice[" + ss.str() + "]; "; 233 ss.str(""); 234 std::string strConfigs; 235 for (size_t i = 0; i < configs_.size(); ++i) { 236 const USBConfig &config = configs_[i]; 237 strConfigs += config.ToString(); 238 } 239 str += strConfigs; 240 return str; 241 } 242 SetiManufacturer(uint8_t manufacturer)243 void SetiManufacturer(uint8_t manufacturer) 244 { 245 this->iManufacturer_ = manufacturer; 246 } 247 GetiManufacturer()248 uint8_t GetiManufacturer() 249 { 250 return this->iManufacturer_; 251 } 252 SetiProduct(uint8_t product)253 void SetiProduct(uint8_t product) 254 { 255 this->iProduct_ = product; 256 } 257 GetiProduct()258 uint8_t GetiProduct() 259 { 260 return this->iProduct_; 261 } 262 SetiSerialNumber(uint8_t sn)263 void SetiSerialNumber(uint8_t sn) 264 { 265 this->iSerialNumber_ = sn; 266 } 267 GetiSerialNumber()268 uint8_t GetiSerialNumber() 269 { 270 return this->iSerialNumber_; 271 } 272 SetmSerial(std::string serial)273 void SetmSerial(std::string serial) 274 { 275 this->serial_ = serial; 276 } 277 GetmSerial()278 const std::string GetmSerial() const 279 { 280 return this->serial_; 281 } 282 SetbMaxPacketSize0(uint8_t maxSize)283 void SetbMaxPacketSize0(uint8_t maxSize) 284 { 285 this->bMaxPacketSize0_ = maxSize; 286 } 287 GetbMaxPacketSize0()288 uint8_t GetbMaxPacketSize0() 289 { 290 return this->bMaxPacketSize0_; 291 } 292 SetbcdUSB(uint16_t bcdUSB)293 void SetbcdUSB(uint16_t bcdUSB) 294 { 295 this->bcdUSB_ = bcdUSB; 296 } 297 GetbcdUSB()298 uint16_t GetbcdUSB() 299 { 300 return this->bcdUSB_; 301 } 302 SetbcdDevice(uint16_t bcdDevice)303 void SetbcdDevice(uint16_t bcdDevice) 304 { 305 this->bcdDevice_ = bcdDevice; 306 } 307 GetbcdDevice()308 uint16_t GetbcdDevice() 309 { 310 return this->bcdDevice_; 311 } 312 ToJson()313 Json::Value ToJson() const 314 { 315 Json::Value device; 316 device["busNum"] = busNum_; 317 device["devAddress"] = devAddr_; 318 device["serial"] = serial_; 319 device["name"] = name_; 320 device["manufacturerName"] = manufacturerName_; 321 device["productName"] = productName_; 322 device["version"] = version_; 323 device["vendorId"] = vendorId_; 324 device["productId"] = productId_; 325 device["clazz"] = klass_; 326 device["subClass"] = subClass_; 327 device["protocol"] = protocol_; 328 329 Json::Value configs; 330 for (auto &cfg : configs_) { 331 configs.append(cfg.ToJson()); 332 } 333 device["configs"] = configs; 334 335 return device; 336 } 337 338 private: 339 std::string name_; 340 std::string manufacturerName_; 341 std::string productName_; 342 std::string version_; 343 std::string serial_; 344 uint8_t devAddr_; 345 uint8_t busNum_; 346 uint8_t descConfigCount_ = UINT8_MAX; 347 348 uint8_t bMaxPacketSize0_ = UINT8_MAX; 349 uint16_t bcdUSB_ = UINT16_MAX; 350 uint16_t bcdDevice_ = UINT16_MAX; 351 uint8_t iManufacturer_ = UINT8_MAX; 352 uint8_t iProduct_ = UINT8_MAX; 353 uint8_t iSerialNumber_ = UINT8_MAX; 354 355 int32_t vendorId_; 356 int32_t productId_; 357 int32_t klass_; 358 int32_t subClass_; 359 int32_t protocol_; 360 std::vector<USBConfig> configs_; 361 }; 362 } // namespace USB 363 } // namespace OHOS 364 365 #endif // USB_DEVICE_H 366