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