• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_INTERFACE_H
17 #define USB_INTERFACE_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 #include <optional>
24 #include "parcel.h"
25 #include "usb_endpoint.h"
26 
27 namespace OHOS {
28 namespace USB {
29 constexpr uint32_t USB_ENDPOINT_MAX_NUM = 128;
30 
31 class UsbInterface : public Parcelable {
32 public:
UsbInterface(int32_t id,int32_t protocol,int32_t interfaceClass,int32_t subClass,int32_t alternateSetting,std::string name,std::vector<USBEndpoint> endpoints)33     UsbInterface(int32_t id,
34                  int32_t protocol,
35                  int32_t interfaceClass,
36                  int32_t subClass,
37                  int32_t alternateSetting,
38                  std::string name,
39                  std::vector<USBEndpoint> endpoints)
40     {
41         this->id_ = id;
42         this->protocol_ = protocol;
43         this->klass_ = interfaceClass;
44         this->subClass_ = subClass;
45         this->alternateSetting_ = alternateSetting;
46         this->endpoints_ = endpoints;
47     }
48 
UsbInterface(const cJSON * interface)49     explicit UsbInterface(const cJSON *interface)
50     {
51         if (interface == nullptr) {
52             USB_HILOGE(MODULE_USB_SERVICE, "interface pointer is nullptr");
53         }
54         id_ = GetIntValue(interface, "id");
55         protocol_ = GetIntValue(interface, "protocol");
56         klass_ = GetIntValue(interface, "clazz");
57         subClass_ = GetIntValue(interface, "subClass");
58         alternateSetting_ = GetIntValue(interface, "alternateSetting");
59         name_ = GetStringValue(interface, "name");
60 
61         cJSON *endpoints = cJSON_GetObjectItem(interface, "endpoints");
62         for (int i = 0; i < cJSON_GetArraySize(endpoints); i++) {
63             cJSON *jsonEp = cJSON_GetArrayItem(endpoints, i);
64             if (jsonEp == nullptr) {
65                 USB_HILOGE(MODULE_USB_SERVICE, "get item nullptr");
66                 continue;
67             }
68             USBEndpoint ep(jsonEp);
69             endpoints_.emplace_back(ep);
70         }
71     }
72 
UsbInterface()73     UsbInterface() {}
Marshalling(Parcel & parcel)74     bool Marshalling(Parcel &parcel) const override
75     {
76         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->id_);
77         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->protocol_);
78         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->klass_);
79         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->subClass_);
80         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->alternateSetting_);
81         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->name_);
82         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->iInterface_);
83 
84         uint32_t epCount = this->endpoints_.size();
85         WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint32, parcel, epCount);
86         for (uint32_t i = 0; i < epCount && i < USB_ENDPOINT_MAX_NUM; i++) {
87             this->endpoints_[i].Marshalling(parcel);
88         }
89         return true;
90     }
91 
Unmarshalling(Parcel & data)92     static UsbInterface *Unmarshalling(Parcel &data)
93     {
94         UsbInterface *usbInterface = new (std::nothrow) UsbInterface;
95         if (usbInterface == nullptr) {
96             return nullptr;
97         }
98         usbInterface->id_ = data.ReadInt32();
99         usbInterface->protocol_ = data.ReadInt32();
100         usbInterface->klass_ = data.ReadInt32();
101         usbInterface->subClass_ = data.ReadInt32();
102         usbInterface->alternateSetting_ = data.ReadInt32();
103         usbInterface->name_ = data.ReadString();
104         usbInterface->iInterface_ = data.ReadUint8();
105 
106         uint32_t epCount = 0;
107         epCount = data.ReadUint32();
108         for (uint32_t i = 0; i < epCount && i < USB_ENDPOINT_MAX_NUM; i++) {
109             USBEndpoint *pEp = USBEndpoint::Unmarshalling(data);
110             usbInterface->endpoints_.push_back(*pEp);
111             delete pEp;
112             pEp = nullptr;
113         }
114         return usbInterface;
115     }
116 
GetIntValue(const cJSON * jsonObject,const char * key)117     static int GetIntValue(const cJSON *jsonObject, const char *key)
118     {
119         cJSON *item = cJSON_GetObjectItem(jsonObject, key);
120         if (item != nullptr && cJSON_IsNumber(item)) {
121             return item->valueint;
122         } else {
123             USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key);
124             return 0;
125         }
126     }
127 
GetStringValue(const cJSON * jsonObject,const char * key)128     static std::string GetStringValue(const cJSON *jsonObject, const char *key)
129     {
130         cJSON *item = cJSON_GetObjectItem(jsonObject, key);
131         if (item != nullptr && cJSON_IsString(item)) {
132             return item->valuestring;
133         } else {
134             USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key);
135             return "";
136         }
137     }
138 
GetName()139     const std::string &GetName() const
140     {
141         return name_;
142     }
143 
GetId()144     int32_t GetId() const
145     {
146         return id_;
147     }
148 
GetClass()149     int32_t GetClass() const
150     {
151         return klass_;
152     }
153 
GetSubClass()154     int32_t GetSubClass() const
155     {
156         return subClass_;
157     }
158 
GetAlternateSetting()159     int32_t GetAlternateSetting() const
160     {
161         return alternateSetting_;
162     }
163 
GetProtocol()164     int32_t GetProtocol() const
165     {
166         return protocol_;
167     }
168 
GetEndpointCount()169     int32_t GetEndpointCount() const
170     {
171         return endpoints_.size();
172     }
173 
GetEndpoint(uint32_t index)174     std::optional<USBEndpoint> GetEndpoint(uint32_t index) const
175     {
176         if (index >= endpoints_.size()) {
177             USB_HILOGE(MODULE_USB_INNERKIT, "invalid index=%{public}u !", index);
178             return std::nullopt;
179         }
180 
181         return endpoints_[index];
182     }
183 
GetEndpoints()184     std::vector<USBEndpoint> &GetEndpoints()
185     {
186         return endpoints_;
187     }
188 
SetEndpoints(const std::vector<USBEndpoint> & eps)189     void SetEndpoints(const std::vector<USBEndpoint> &eps)
190     {
191         endpoints_ = eps;
192     }
193 
SetId(int32_t id)194     void SetId(int32_t id)
195     {
196         id_ = id;
197     }
198 
SetProtocol(int32_t protocol)199     void SetProtocol(int32_t protocol)
200     {
201         protocol_ = protocol;
202     }
203 
SetClass(int32_t klass)204     void SetClass(int32_t klass)
205     {
206         klass_ = klass;
207     }
208 
SetSubClass(int32_t subClass)209     void SetSubClass(int32_t subClass)
210     {
211         subClass_ = subClass;
212     }
213 
SetAlternateSetting(int32_t alternateSetting)214     void SetAlternateSetting(int32_t alternateSetting)
215     {
216         alternateSetting_ = alternateSetting;
217     }
218 
SetName(const std::string & name)219     void SetName(const std::string &name)
220     {
221         name_ = name;
222     }
223 
~UsbInterface()224     ~UsbInterface() {}
225 
ToString()226     std::string ToString() const
227     {
228         std::ostringstream ss;
229         ss << "id=" << id_ << ","
230            << "name_=" << name_ << ","
231            << "iInterface_=" << (int32_t)iInterface_ << ","
232            << "klass_=" << klass_ << ","
233            << "subClass_=" << subClass_ << ","
234            << "protocol_=" << protocol_ << ","
235            << "alternateSetting_=" << alternateSetting_ << "";
236         std::string str = "UsbInterface[" + ss.str() + "];    ";
237         ss.str("");
238         for (size_t i = 0; i < endpoints_.size(); ++i) {
239             const USBEndpoint &endpoint = endpoints_[i];
240             str += endpoint.ToString();
241         }
242         return str;
243     }
244 
SetiInterface(uint8_t idx)245     void SetiInterface(uint8_t idx)
246     {
247         this->iInterface_ = idx;
248     }
249 
GetiInterface()250     uint8_t GetiInterface()
251     {
252         return this->iInterface_;
253     }
254 
getJsonString()255     const std::string getJsonString() const
256     {
257         cJSON* interface = cJSON_CreateObject();
258         if (!interface) {
259             USB_HILOGE(MODULE_USB_SERVICE, "Create interface error");
260             return "";
261         }
262         cJSON_AddNumberToObject(interface, "id", static_cast<double>(id_));
263         cJSON_AddNumberToObject(interface, "protocol", static_cast<double>(protocol_));
264         cJSON_AddNumberToObject(interface, "clazz", static_cast<double>(klass_));
265         cJSON_AddNumberToObject(interface, "subClass", static_cast<double>(subClass_));
266         cJSON_AddNumberToObject(interface, "alternateSetting", alternateSetting_);
267         cJSON_AddStringToObject(interface, "name", name_.c_str());
268         cJSON* endpoints = cJSON_CreateArray();
269         if (!endpoints) {
270             USB_HILOGE(MODULE_USB_SERVICE, "Create endpoints error");
271             cJSON_Delete(interface);
272             return "";
273         }
274         for (size_t i = 0; i < endpoints_.size(); ++i) {
275             const USBEndpoint &ep = endpoints_[i];
276             cJSON* pEp =  cJSON_Parse(ep.getJsonString().c_str());
277             cJSON_AddItemToArray(endpoints, pEp);
278         }
279         cJSON_AddItemToObject(interface, "endpoints", endpoints);
280         char *pInterface = cJSON_PrintUnformatted(interface);
281         cJSON_Delete(interface);
282         if (!pInterface) {
283             USB_HILOGE(MODULE_USB_SERVICE, "Print interface error");
284             return "";
285         }
286         std::string interfaceJsonStr(pInterface);
287         cJSON_free(pInterface);
288         return interfaceJsonStr;
289     }
290 
291 private:
292     int32_t id_ = INT32_MAX;
293     int32_t protocol_ = INT32_MAX;
294     int32_t klass_ = INT32_MAX;
295     int32_t subClass_ = INT32_MAX;
296     int32_t alternateSetting_ = INT32_MAX;
297     std::string name_;
298     std::vector<USBEndpoint> endpoints_;
299     uint8_t iInterface_ = UINT8_MAX;
300 };
301 } // namespace USB
302 } // namespace OHOS
303 
304 #endif // USB_INTERFACE_H
305