• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (pConfig == nullptr) {
111                 continue;
112             }
113             usbDevice->configs_.push_back(*pConfig);
114             delete pConfig;
115             pConfig = nullptr;
116         }
117         return usbDevice;
118     }
UsbDevice(const cJSON * device)119     explicit UsbDevice(const cJSON *device)
120     {
121         if (device == nullptr) {
122             USB_HILOGE(MODULE_USB_SERVICE, "device pointer is nullptr");
123         }
124         busNum_ = GetIntValue(device, "busNum");
125         devAddr_ = GetIntValue(device, "devAddress");
126         serial_ = GetIntValue(device, "serial");
127         name_ = GetStringValue(device, "name");
128         manufacturerName_ = GetStringValue(device, "manufacturerName");
129         productName_ = GetStringValue(device, "productName");
130         version_ = GetStringValue(device, "version");
131         vendorId_ = GetIntValue(device, "vendorId");
132         productId_ = GetIntValue(device, "productId");
133         baseClass_ = GetIntValue(device, "clazz");
134         subClass_ = GetIntValue(device, "subClass");
135         protocol_ = GetIntValue(device, "protocol");
136         cJSON* configs = cJSON_GetObjectItem(device, "configs");
137         for (int i = 0; i < cJSON_GetArraySize(configs); i++) {
138             cJSON* jsonConfig =  cJSON_GetArrayItem(configs, i);
139             if (jsonConfig == nullptr) {
140                 USB_HILOGE(MODULE_USB_SERVICE, "get item nullptr");
141                 continue;
142             }
143             USBConfig config (jsonConfig);
144             configs_.emplace_back(config);
145         }
146     }
147 
UsbDevice()148     UsbDevice() {}
~UsbDevice()149     ~UsbDevice() {}
150 
GetIntValue(const cJSON * jsonObject,const char * key)151     static int GetIntValue(const cJSON *jsonObject, const char *key)
152     {
153         cJSON *item = cJSON_GetObjectItem(jsonObject, key);
154         if (item != nullptr && cJSON_IsNumber(item)) {
155             return item->valueint;
156         } else {
157             USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key);
158             return 0;
159         }
160     }
161 
GetStringValue(const cJSON * jsonObject,const char * key)162     static std::string GetStringValue(const cJSON *jsonObject, const char *key)
163     {
164         cJSON *item = cJSON_GetObjectItem(jsonObject, key);
165         if (item != nullptr && cJSON_IsString(item)) {
166             return item->valuestring;
167         } else {
168             USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key);
169             return "";
170         }
171     }
172 
GetName()173     const std::string &GetName() const
174     {
175         return name_;
176     }
177 
GetManufacturerName()178     const std::string &GetManufacturerName() const
179     {
180         return manufacturerName_;
181     }
182 
GetProductName()183     const std::string &GetProductName() const
184     {
185         return productName_;
186     }
187 
GetVersion()188     const std::string &GetVersion() const
189     {
190         return version_;
191     }
192 
GetVendorId()193     int32_t GetVendorId() const
194     {
195         return vendorId_;
196     }
197 
GetProductId()198     int32_t GetProductId() const
199     {
200         return productId_;
201     }
202 
GetClass()203     int32_t GetClass() const
204     {
205         return baseClass_;
206     }
207 
GetSubclass()208     int32_t GetSubclass() const
209     {
210         return subClass_;
211     }
212 
GetProtocol()213     int32_t GetProtocol() const
214     {
215         return protocol_;
216     }
217 
GetConfigCount()218     int32_t GetConfigCount() const
219     {
220         return configs_.size();
221     }
222 
GetConfig(uint32_t index,USBConfig & config)223     int32_t GetConfig(uint32_t index, USBConfig &config) const
224     {
225         if (index >= configs_.size()) {
226             return ERR_INVALID_VALUE;
227         }
228         config = configs_[index];
229         return ERR_OK;
230     }
231 
SetConfigs(const std::vector<USBConfig> & configs)232     void SetConfigs(const std::vector<USBConfig> &configs)
233     {
234         this->configs_ = configs;
235     }
236 
GetDevAddr()237     uint8_t GetDevAddr() const
238     {
239         return devAddr_;
240     }
241 
GetBusNum()242     uint8_t GetBusNum() const
243     {
244         return busNum_;
245     }
246 
GetDescConfigCount()247     uint8_t GetDescConfigCount()
248     {
249         return descConfigCount_;
250     }
251 
SetDevAddr(uint8_t addr)252     void SetDevAddr(uint8_t addr)
253     {
254         devAddr_ = addr;
255     }
256 
SetBusNum(uint8_t num)257     void SetBusNum(uint8_t num)
258     {
259         busNum_ = num;
260     }
261 
SetName(const std::string & name)262     void SetName(const std::string &name)
263     {
264         name_ = name;
265     }
266 
SetManufacturerName(const std::string & manufacturerName)267     void SetManufacturerName(const std::string &manufacturerName)
268     {
269         manufacturerName_ = manufacturerName;
270     }
271 
SetProductName(const std::string & productName)272     void SetProductName(const std::string &productName)
273     {
274         productName_ = productName;
275     }
276 
SetVersion(const std::string & version)277     void SetVersion(const std::string &version)
278     {
279         version_ = version;
280     }
281 
SetVendorId(int32_t vendorId)282     void SetVendorId(int32_t vendorId)
283     {
284         vendorId_ = vendorId;
285     }
286 
SetProductId(int32_t productId)287     void SetProductId(int32_t productId)
288     {
289         productId_ = productId;
290     }
291 
SetClass(int32_t deviceClass)292     void SetClass(int32_t deviceClass)
293     {
294         baseClass_ = deviceClass;
295     }
296 
SetSubclass(int32_t subClass)297     void SetSubclass(int32_t subClass)
298     {
299         subClass_ = subClass;
300     }
301 
SetProtocol(int32_t protocol)302     void SetProtocol(int32_t protocol)
303     {
304         protocol_ = protocol;
305     }
306 
SetDescConfigCount(uint8_t count)307     void SetDescConfigCount(uint8_t count)
308     {
309         descConfigCount_ = count;
310     }
311 
GetConfigs()312     std::vector<USBConfig> &GetConfigs()
313     {
314         return configs_;
315     }
316 
ToString()317     std::string ToString() const
318     {
319         std::ostringstream ss;
320         ss << "name_=" << name_ << ","
321            << "manufacturerName_=" << manufacturerName_ << ","
322            << "productName_=" << productName_ << ","
323            << "version_=" << version_ << ","
324            << "serial_=" << serial_ << ","
325            << "busNum_=" << (int32_t)busNum_ << ","
326            << "devAddr_=" << (int32_t)devAddr_ << ","
327            << "vendorId_=" << vendorId_ << ","
328            << "productId_=" << productId_ << ","
329            << "baseClass_=" << baseClass_ << ","
330            << "subClass_=" << subClass_ << ","
331            << "protocol_=" << protocol_ << "";
332         std::string str = "UsbDevice[" + ss.str() + "];    ";
333         ss.str("");
334         std::string strConfigs;
335         for (size_t i = 0; i < configs_.size(); ++i) {
336             const USBConfig &config = configs_[i];
337             strConfigs += config.ToString();
338         }
339         str += strConfigs;
340         return str;
341     }
342 
SetiManufacturer(uint8_t manufacturer)343     void SetiManufacturer(uint8_t manufacturer)
344     {
345         this->iManufacturer_ = manufacturer;
346     }
347 
GetiManufacturer()348     uint8_t GetiManufacturer()
349     {
350         return this->iManufacturer_;
351     }
352 
SetiProduct(uint8_t product)353     void SetiProduct(uint8_t product)
354     {
355         this->iProduct_ = product;
356     }
357 
GetiProduct()358     uint8_t GetiProduct()
359     {
360         return this->iProduct_;
361     }
362 
SetiSerialNumber(uint8_t sn)363     void SetiSerialNumber(uint8_t sn)
364     {
365         this->iSerialNumber_ = sn;
366     }
367 
GetiSerialNumber()368     uint8_t GetiSerialNumber()
369     {
370         return this->iSerialNumber_;
371     }
372 
SetmSerial(std::string serial)373     void SetmSerial(std::string serial)
374     {
375         this->serial_ = serial;
376     }
377 
GetmSerial()378     const std::string GetmSerial() const
379     {
380         return this->serial_;
381     }
382 
SetbMaxPacketSize0(uint8_t maxSize)383     void SetbMaxPacketSize0(uint8_t maxSize)
384     {
385         this->bMaxPacketSize0_ = maxSize;
386     }
387 
GetbMaxPacketSize0()388     uint8_t GetbMaxPacketSize0()
389     {
390         return this->bMaxPacketSize0_;
391     }
392 
SetbcdUSB(uint16_t bcdUSB)393     void SetbcdUSB(uint16_t bcdUSB)
394     {
395         this->bcdUSB_ = bcdUSB;
396     }
397 
GetbcdUSB()398     uint16_t GetbcdUSB()
399     {
400         return this->bcdUSB_;
401     }
402 
SetbcdDevice(uint16_t bcdDevice)403     void SetbcdDevice(uint16_t bcdDevice)
404     {
405         this->bcdDevice_ = bcdDevice;
406     }
407 
GetbcdDevice()408     uint16_t GetbcdDevice()
409     {
410         return this->bcdDevice_;
411     }
412 
getJsonString()413     const std::string getJsonString() const
414     {
415         cJSON* device = cJSON_CreateObject();
416         if (!device) {
417             USB_HILOGE(MODULE_USB_SERVICE, "Create device error");
418             return "";
419         }
420         cJSON_AddNumberToObject(device, "busNum", static_cast<double>(busNum_));
421         cJSON_AddNumberToObject(device, "devAddress", static_cast<double>(devAddr_));
422         cJSON_AddStringToObject(device, "serial", "");
423         cJSON_AddStringToObject(device, "name", name_.c_str());
424         cJSON_AddStringToObject(device, "manufacturerName", manufacturerName_.c_str());
425         cJSON_AddStringToObject(device, "productName", productName_.c_str());
426         cJSON_AddStringToObject(device, "version", version_.c_str());
427         cJSON_AddNumberToObject(device, "vendorId", static_cast<double>(vendorId_));
428         cJSON_AddNumberToObject(device, "productId", static_cast<double>(productId_));
429         cJSON_AddNumberToObject(device, "clazz", static_cast<double>(baseClass_));
430         cJSON_AddNumberToObject(device, "subClass", static_cast<double>(subClass_));
431         cJSON_AddNumberToObject(device, "protocol", static_cast<double>(protocol_));
432         cJSON* configs = cJSON_CreateArray();
433         if (!configs) {
434             USB_HILOGE(MODULE_USB_SERVICE, "Create configs error");
435             cJSON_Delete(device);
436             return "";
437         }
438         for (auto &cfg : configs_) {
439             cJSON* pConfig =  cJSON_Parse(cfg.getJsonString().c_str());
440             cJSON_AddItemToArray(configs, pConfig);
441         }
442         cJSON_AddItemToObject(device, "configs", configs);
443         char *pDevice = cJSON_PrintUnformatted(device);
444         cJSON_Delete(device);
445         if (!pDevice) {
446             USB_HILOGE(MODULE_USB_SERVICE, "Print device error");
447             return "";
448         }
449         std::string deviceStr(pDevice);
450         cJSON_free(pDevice);
451         return deviceStr;
452     }
453 
454 private:
455     std::string name_;
456     std::string manufacturerName_;
457     std::string productName_;
458     std::string version_;
459     std::string serial_;
460     uint8_t devAddr_ = UINT8_MAX;
461     uint8_t busNum_ = UINT8_MAX;
462     uint8_t descConfigCount_ = UINT8_MAX;
463 
464     uint8_t bMaxPacketSize0_ = UINT8_MAX;
465     uint16_t bcdUSB_ = UINT16_MAX;
466     uint16_t bcdDevice_ = UINT16_MAX;
467     uint8_t iManufacturer_ = UINT8_MAX;
468     uint8_t iProduct_ = UINT8_MAX;
469     uint8_t iSerialNumber_ = UINT8_MAX;
470 
471     int32_t vendorId_ = INVALID_USB_INT_VALUE;
472     int32_t productId_ = INVALID_USB_INT_VALUE;
473     int32_t baseClass_ = INVALID_USB_INT_VALUE;
474     int32_t subClass_ = INVALID_USB_INT_VALUE;
475     int32_t protocol_ = INVALID_USB_INT_VALUE;
476     std::vector<USBConfig> configs_;
477 };
478 } // namespace USB
479 } // namespace OHOS
480 
481 #endif // USB_DEVICE_H
482