1 /* 2 * Copyright (c) 2021-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 USB_DEVICE_H 17 #define USB_DEVICE_H 18 #include <iostream> 19 #include <singleton.h> 20 #include <sstream> 21 #include <vector> 22 #include <map> 23 #include "parcel.h" 24 #include "string_ex.h" 25 #include "cJSON.h" 26 #include "iremote_object.h" 27 #include "usb_config.h" 28 #include "usb_interface.h" 29 30 namespace OHOS { 31 namespace USB { 32 constexpr uint32_t USB_CONFIG_MAX_NUM = 128; 33 class UsbDevice : public Parcelable { 34 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 baseClass,int32_t subClass,int32_t protocol,std::vector<USBConfig> configs)35 UsbDevice(std::string name, std::string manufacturerName, std::string productName, std::string version, 36 uint8_t devAddr, uint8_t busNum, int32_t vendorId, int32_t productId, int32_t baseClass, int32_t subClass, 37 int32_t protocol, std::vector<USBConfig> configs) 38 { 39 this->name_ = name; 40 this->manufacturerName_ = manufacturerName; 41 this->productName_ = productName; 42 this->version_ = version; 43 this->devAddr_ = devAddr; 44 this->busNum_ = busNum; 45 this->vendorId_ = vendorId; 46 this->productId_ = productId; 47 this->baseClass_ = baseClass; 48 this->subClass_ = subClass; 49 this->protocol_ = protocol; 50 this->configs_ = configs; 51 } Marshalling(Parcel & parcel)52 bool Marshalling(Parcel &parcel) const override 53 { 54 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->busNum_); 55 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->devAddr_); 56 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->vendorId_); 57 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->productId_); 58 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->baseClass_); 59 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->subClass_); 60 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->protocol_); 61 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->iManufacturer_); 62 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->iProduct_); 63 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->iSerialNumber_); 64 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->bMaxPacketSize0_); 65 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint16, parcel, this->bcdUSB_); 66 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint16, parcel, this->bcdDevice_); 67 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->name_); 68 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->manufacturerName_); 69 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->productName_); 70 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->version_); 71 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->serial_); 72 73 uint32_t configCount = this->configs_.size(); 74 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, configCount); 75 for (uint32_t i = 0; i < configCount && i < USB_CONFIG_MAX_NUM; i++) { 76 this->configs_[i].Marshalling(parcel); 77 } 78 return true; 79 } 80 Unmarshalling(Parcel & data)81 static UsbDevice *Unmarshalling(Parcel &data) 82 { 83 UsbDevice *usbDevice = new (std::nothrow) UsbDevice; 84 if (usbDevice == nullptr) { 85 return nullptr; 86 } 87 88 usbDevice->busNum_ = data.ReadUint8(); 89 usbDevice->devAddr_ = data.ReadUint8(); 90 usbDevice->vendorId_ = data.ReadInt32(); 91 usbDevice->productId_ = data.ReadInt32(); 92 usbDevice->baseClass_ = data.ReadInt32(); 93 usbDevice->subClass_ = data.ReadInt32(); 94 usbDevice->protocol_ = data.ReadInt32(); 95 usbDevice->iManufacturer_ = data.ReadUint8(); 96 usbDevice->iProduct_ = data.ReadUint8(); 97 usbDevice->iSerialNumber_ = data.ReadUint8(); 98 usbDevice->bMaxPacketSize0_ = data.ReadUint8(); 99 usbDevice->bcdUSB_ = data.ReadUint16(); 100 usbDevice->bcdDevice_ = data.ReadUint16(); 101 usbDevice->name_ = data.ReadString(); 102 usbDevice->manufacturerName_ = data.ReadString(); 103 usbDevice->productName_ = data.ReadString(); 104 usbDevice->version_ = data.ReadString(); 105 usbDevice->serial_ = data.ReadString(); 106 107 uint32_t configCount = data.ReadUint32(); 108 for (uint32_t i = 0; i < configCount && i < USB_CONFIG_MAX_NUM; i++) { 109 USBConfig *pConfig = USBConfig::Unmarshalling(data); 110 usbDevice->configs_.push_back(*pConfig); 111 delete pConfig; 112 pConfig = nullptr; 113 } 114 return usbDevice; 115 } UsbDevice(const cJSON * device)116 explicit UsbDevice(const cJSON *device) 117 { 118 if (device == nullptr) { 119 USB_HILOGE(MODULE_USB_SERVICE, "device pointer is nullptr"); 120 } 121 busNum_ = GetIntValue(device, "busNum"); 122 devAddr_ = GetIntValue(device, "devAddress"); 123 serial_ = GetIntValue(device, "serial"); 124 name_ = GetStringValue(device, "name"); 125 manufacturerName_ = GetStringValue(device, "manufacturerName"); 126 productName_ = GetStringValue(device, "productName"); 127 version_ = GetStringValue(device, "version"); 128 vendorId_ = GetIntValue(device, "vendorId"); 129 productId_ = GetIntValue(device, "productId"); 130 baseClass_ = GetIntValue(device, "clazz"); 131 subClass_ = GetIntValue(device, "subClass"); 132 protocol_ = GetIntValue(device, "protocol"); 133 cJSON* configs = cJSON_GetObjectItem(device, "configs"); 134 for (int i = 0; i < cJSON_GetArraySize(configs); i++) { 135 cJSON* jsonConfig = cJSON_GetArrayItem(configs, i); 136 if (jsonConfig == nullptr) { 137 USB_HILOGE(MODULE_USB_SERVICE, "get item nullptr"); 138 continue; 139 } 140 USBConfig config (jsonConfig); 141 configs_.emplace_back(config); 142 } 143 } 144 UsbDevice()145 UsbDevice() {} ~UsbDevice()146 ~UsbDevice() {} 147 GetIntValue(const cJSON * jsonObject,const char * key)148 static int GetIntValue(const cJSON *jsonObject, const char *key) 149 { 150 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 151 if (item != nullptr && cJSON_IsNumber(item)) { 152 return item->valueint; 153 } else { 154 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 155 return 0; 156 } 157 } 158 GetStringValue(const cJSON * jsonObject,const char * key)159 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 160 { 161 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 162 if (item != nullptr && cJSON_IsString(item)) { 163 return item->valuestring; 164 } else { 165 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 166 return ""; 167 } 168 } 169 GetName()170 const std::string &GetName() const 171 { 172 return name_; 173 } 174 GetManufacturerName()175 const std::string &GetManufacturerName() const 176 { 177 return manufacturerName_; 178 } 179 GetProductName()180 const std::string &GetProductName() const 181 { 182 return productName_; 183 } 184 GetVersion()185 const std::string &GetVersion() const 186 { 187 return version_; 188 } 189 GetVendorId()190 int32_t GetVendorId() const 191 { 192 return vendorId_; 193 } 194 GetProductId()195 int32_t GetProductId() const 196 { 197 return productId_; 198 } 199 GetClass()200 int32_t GetClass() const 201 { 202 return baseClass_; 203 } 204 GetSubclass()205 int32_t GetSubclass() const 206 { 207 return subClass_; 208 } 209 GetProtocol()210 int32_t GetProtocol() const 211 { 212 return protocol_; 213 } 214 GetConfigCount()215 int32_t GetConfigCount() const 216 { 217 return configs_.size(); 218 } 219 GetConfig(uint32_t index,USBConfig & config)220 int32_t GetConfig(uint32_t index, USBConfig &config) const 221 { 222 if (index >= configs_.size()) { 223 return ERR_INVALID_VALUE; 224 } 225 config = configs_[index]; 226 return ERR_OK; 227 } 228 SetConfigs(const std::vector<USBConfig> & configs)229 void SetConfigs(const std::vector<USBConfig> &configs) 230 { 231 this->configs_ = configs; 232 } 233 GetDevAddr()234 uint8_t GetDevAddr() const 235 { 236 return devAddr_; 237 } 238 GetBusNum()239 uint8_t GetBusNum() const 240 { 241 return busNum_; 242 } 243 GetDescConfigCount()244 uint8_t GetDescConfigCount() 245 { 246 return descConfigCount_; 247 } 248 SetDevAddr(uint8_t addr)249 void SetDevAddr(uint8_t addr) 250 { 251 devAddr_ = addr; 252 } 253 SetBusNum(uint8_t num)254 void SetBusNum(uint8_t num) 255 { 256 busNum_ = num; 257 } 258 SetName(const std::string & name)259 void SetName(const std::string &name) 260 { 261 name_ = name; 262 } 263 SetManufacturerName(const std::string & manufacturerName)264 void SetManufacturerName(const std::string &manufacturerName) 265 { 266 manufacturerName_ = manufacturerName; 267 } 268 SetProductName(const std::string & productName)269 void SetProductName(const std::string &productName) 270 { 271 productName_ = productName; 272 } 273 SetVersion(const std::string & version)274 void SetVersion(const std::string &version) 275 { 276 version_ = version; 277 } 278 SetVendorId(int32_t vendorId)279 void SetVendorId(int32_t vendorId) 280 { 281 vendorId_ = vendorId; 282 } 283 SetProductId(int32_t productId)284 void SetProductId(int32_t productId) 285 { 286 productId_ = productId; 287 } 288 SetClass(int32_t deviceClass)289 void SetClass(int32_t deviceClass) 290 { 291 baseClass_ = deviceClass; 292 } 293 SetSubclass(int32_t subClass)294 void SetSubclass(int32_t subClass) 295 { 296 subClass_ = subClass; 297 } 298 SetProtocol(int32_t protocol)299 void SetProtocol(int32_t protocol) 300 { 301 protocol_ = protocol; 302 } 303 SetDescConfigCount(uint8_t count)304 void SetDescConfigCount(uint8_t count) 305 { 306 descConfigCount_ = count; 307 } 308 GetConfigs()309 std::vector<USBConfig> &GetConfigs() 310 { 311 return configs_; 312 } 313 ToString()314 std::string ToString() const 315 { 316 std::ostringstream ss; 317 ss << "name_=" << name_ << "," 318 << "manufacturerName_=" << manufacturerName_ << "," 319 << "productName_=" << productName_ << "," 320 << "version_=" << version_ << "," 321 << "serial_=" << serial_ << "," 322 << "busNum_=" << (int32_t)busNum_ << "," 323 << "devAddr_=" << (int32_t)devAddr_ << "," 324 << "vendorId_=" << vendorId_ << "," 325 << "productId_=" << productId_ << "," 326 << "baseClass_=" << baseClass_ << "," 327 << "subClass_=" << subClass_ << "," 328 << "protocol_=" << protocol_ << ""; 329 std::string str = "UsbDevice[" + ss.str() + "]; "; 330 ss.str(""); 331 std::string strConfigs; 332 for (size_t i = 0; i < configs_.size(); ++i) { 333 const USBConfig &config = configs_[i]; 334 strConfigs += config.ToString(); 335 } 336 str += strConfigs; 337 return str; 338 } 339 SetiManufacturer(uint8_t manufacturer)340 void SetiManufacturer(uint8_t manufacturer) 341 { 342 this->iManufacturer_ = manufacturer; 343 } 344 GetiManufacturer()345 uint8_t GetiManufacturer() 346 { 347 return this->iManufacturer_; 348 } 349 SetiProduct(uint8_t product)350 void SetiProduct(uint8_t product) 351 { 352 this->iProduct_ = product; 353 } 354 GetiProduct()355 uint8_t GetiProduct() 356 { 357 return this->iProduct_; 358 } 359 SetiSerialNumber(uint8_t sn)360 void SetiSerialNumber(uint8_t sn) 361 { 362 this->iSerialNumber_ = sn; 363 } 364 GetiSerialNumber()365 uint8_t GetiSerialNumber() 366 { 367 return this->iSerialNumber_; 368 } 369 SetmSerial(std::string serial)370 void SetmSerial(std::string serial) 371 { 372 this->serial_ = serial; 373 } 374 GetmSerial()375 const std::string GetmSerial() const 376 { 377 return this->serial_; 378 } 379 SetbMaxPacketSize0(uint8_t maxSize)380 void SetbMaxPacketSize0(uint8_t maxSize) 381 { 382 this->bMaxPacketSize0_ = maxSize; 383 } 384 GetbMaxPacketSize0()385 uint8_t GetbMaxPacketSize0() 386 { 387 return this->bMaxPacketSize0_; 388 } 389 SetbcdUSB(uint16_t bcdUSB)390 void SetbcdUSB(uint16_t bcdUSB) 391 { 392 this->bcdUSB_ = bcdUSB; 393 } 394 GetbcdUSB()395 uint16_t GetbcdUSB() 396 { 397 return this->bcdUSB_; 398 } 399 SetbcdDevice(uint16_t bcdDevice)400 void SetbcdDevice(uint16_t bcdDevice) 401 { 402 this->bcdDevice_ = bcdDevice; 403 } 404 GetbcdDevice()405 uint16_t GetbcdDevice() 406 { 407 return this->bcdDevice_; 408 } 409 getJsonString()410 const std::string getJsonString() const 411 { 412 cJSON* device = cJSON_CreateObject(); 413 if (!device) { 414 USB_HILOGE(MODULE_USB_SERVICE, "Create device error"); 415 return ""; 416 } 417 cJSON_AddNumberToObject(device, "busNum", static_cast<double>(busNum_)); 418 cJSON_AddNumberToObject(device, "devAddress", static_cast<double>(devAddr_)); 419 cJSON_AddStringToObject(device, "serial", ""); 420 cJSON_AddStringToObject(device, "name", name_.c_str()); 421 cJSON_AddStringToObject(device, "manufacturerName", manufacturerName_.c_str()); 422 cJSON_AddStringToObject(device, "productName", productName_.c_str()); 423 cJSON_AddStringToObject(device, "version", version_.c_str()); 424 cJSON_AddNumberToObject(device, "vendorId", static_cast<double>(vendorId_)); 425 cJSON_AddNumberToObject(device, "productId", static_cast<double>(productId_)); 426 cJSON_AddNumberToObject(device, "clazz", static_cast<double>(baseClass_)); 427 cJSON_AddNumberToObject(device, "subClass", static_cast<double>(subClass_)); 428 cJSON_AddNumberToObject(device, "protocol", static_cast<double>(protocol_)); 429 cJSON* configs = cJSON_CreateArray(); 430 if (!configs) { 431 USB_HILOGE(MODULE_USB_SERVICE, "Create configs error"); 432 cJSON_Delete(device); 433 return ""; 434 } 435 for (auto &cfg : configs_) { 436 cJSON* pConfig = cJSON_Parse(cfg.getJsonString().c_str()); 437 cJSON_AddItemToArray(configs, pConfig); 438 } 439 cJSON_AddItemToObject(device, "configs", configs); 440 char *pDevice = cJSON_PrintUnformatted(device); 441 cJSON_Delete(device); 442 if (!pDevice) { 443 USB_HILOGE(MODULE_USB_SERVICE, "Print device error"); 444 return ""; 445 } 446 std::string deviceStr(pDevice); 447 cJSON_free(pDevice); 448 return deviceStr; 449 } 450 451 private: 452 std::string name_; 453 std::string manufacturerName_; 454 std::string productName_; 455 std::string version_; 456 std::string serial_; 457 uint8_t devAddr_ = UINT8_MAX; 458 uint8_t busNum_ = UINT8_MAX; 459 uint8_t descConfigCount_ = UINT8_MAX; 460 461 uint8_t bMaxPacketSize0_ = UINT8_MAX; 462 uint16_t bcdUSB_ = UINT16_MAX; 463 uint16_t bcdDevice_ = UINT16_MAX; 464 uint8_t iManufacturer_ = UINT8_MAX; 465 uint8_t iProduct_ = UINT8_MAX; 466 uint8_t iSerialNumber_ = UINT8_MAX; 467 468 int32_t vendorId_ = INVALID_USB_INT_VALUE; 469 int32_t productId_ = INVALID_USB_INT_VALUE; 470 int32_t baseClass_ = INVALID_USB_INT_VALUE; 471 int32_t subClass_ = INVALID_USB_INT_VALUE; 472 int32_t protocol_ = INVALID_USB_INT_VALUE; 473 std::vector<USBConfig> configs_; 474 }; 475 } // namespace USB 476 } // namespace OHOS 477 478 #endif // USB_DEVICE_H 479