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 if (pEp == nullptr) { 111 continue; 112 } 113 usbInterface->endpoints_.push_back(*pEp); 114 delete pEp; 115 pEp = nullptr; 116 } 117 return usbInterface; 118 } 119 GetIntValue(const cJSON * jsonObject,const char * key)120 static int GetIntValue(const cJSON *jsonObject, const char *key) 121 { 122 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 123 if (item != nullptr && cJSON_IsNumber(item)) { 124 return item->valueint; 125 } else { 126 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 127 return 0; 128 } 129 } 130 GetStringValue(const cJSON * jsonObject,const char * key)131 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 132 { 133 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 134 if (item != nullptr && cJSON_IsString(item)) { 135 return item->valuestring; 136 } else { 137 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 138 return ""; 139 } 140 } 141 GetName()142 const std::string &GetName() const 143 { 144 return name_; 145 } 146 GetId()147 int32_t GetId() const 148 { 149 return id_; 150 } 151 GetClass()152 int32_t GetClass() const 153 { 154 return klass_; 155 } 156 GetSubClass()157 int32_t GetSubClass() const 158 { 159 return subClass_; 160 } 161 GetAlternateSetting()162 int32_t GetAlternateSetting() const 163 { 164 return alternateSetting_; 165 } 166 GetProtocol()167 int32_t GetProtocol() const 168 { 169 return protocol_; 170 } 171 GetEndpointCount()172 int32_t GetEndpointCount() const 173 { 174 return endpoints_.size(); 175 } 176 GetEndpoint(uint32_t index)177 std::optional<USBEndpoint> GetEndpoint(uint32_t index) const 178 { 179 if (index >= endpoints_.size()) { 180 USB_HILOGE(MODULE_USB_INNERKIT, "invalid index=%{public}u !", index); 181 return std::nullopt; 182 } 183 184 return endpoints_[index]; 185 } 186 GetEndpoints()187 std::vector<USBEndpoint> &GetEndpoints() 188 { 189 return endpoints_; 190 } 191 SetEndpoints(const std::vector<USBEndpoint> & eps)192 void SetEndpoints(const std::vector<USBEndpoint> &eps) 193 { 194 endpoints_ = eps; 195 } 196 SetId(int32_t id)197 void SetId(int32_t id) 198 { 199 id_ = id; 200 } 201 SetProtocol(int32_t protocol)202 void SetProtocol(int32_t protocol) 203 { 204 protocol_ = protocol; 205 } 206 SetClass(int32_t klass)207 void SetClass(int32_t klass) 208 { 209 klass_ = klass; 210 } 211 SetSubClass(int32_t subClass)212 void SetSubClass(int32_t subClass) 213 { 214 subClass_ = subClass; 215 } 216 SetAlternateSetting(int32_t alternateSetting)217 void SetAlternateSetting(int32_t alternateSetting) 218 { 219 alternateSetting_ = alternateSetting; 220 } 221 SetName(const std::string & name)222 void SetName(const std::string &name) 223 { 224 name_ = name; 225 } 226 ~UsbInterface()227 ~UsbInterface() {} 228 ToString()229 std::string ToString() const 230 { 231 std::ostringstream ss; 232 ss << "id=" << id_ << "," 233 << "name_=" << name_ << "," 234 << "iInterface_=" << (int32_t)iInterface_ << "," 235 << "klass_=" << klass_ << "," 236 << "subClass_=" << subClass_ << "," 237 << "protocol_=" << protocol_ << "," 238 << "alternateSetting_=" << alternateSetting_ << ""; 239 std::string str = "UsbInterface[" + ss.str() + "]; "; 240 ss.str(""); 241 for (size_t i = 0; i < endpoints_.size(); ++i) { 242 const USBEndpoint &endpoint = endpoints_[i]; 243 str += endpoint.ToString(); 244 } 245 return str; 246 } 247 SetiInterface(uint8_t idx)248 void SetiInterface(uint8_t idx) 249 { 250 this->iInterface_ = idx; 251 } 252 GetiInterface()253 uint8_t GetiInterface() 254 { 255 return this->iInterface_; 256 } 257 getJsonString()258 const std::string getJsonString() const 259 { 260 cJSON* interface = cJSON_CreateObject(); 261 if (!interface) { 262 USB_HILOGE(MODULE_USB_SERVICE, "Create interface error"); 263 return ""; 264 } 265 cJSON_AddNumberToObject(interface, "id", static_cast<double>(id_)); 266 cJSON_AddNumberToObject(interface, "protocol", static_cast<double>(protocol_)); 267 cJSON_AddNumberToObject(interface, "clazz", static_cast<double>(klass_)); 268 cJSON_AddNumberToObject(interface, "subClass", static_cast<double>(subClass_)); 269 cJSON_AddNumberToObject(interface, "alternateSetting", alternateSetting_); 270 cJSON_AddStringToObject(interface, "name", name_.c_str()); 271 cJSON* endpoints = cJSON_CreateArray(); 272 if (!endpoints) { 273 USB_HILOGE(MODULE_USB_SERVICE, "Create endpoints error"); 274 cJSON_Delete(interface); 275 return ""; 276 } 277 for (size_t i = 0; i < endpoints_.size(); ++i) { 278 const USBEndpoint &ep = endpoints_[i]; 279 cJSON* pEp = cJSON_Parse(ep.getJsonString().c_str()); 280 cJSON_AddItemToArray(endpoints, pEp); 281 } 282 cJSON_AddItemToObject(interface, "endpoints", endpoints); 283 char *pInterface = cJSON_PrintUnformatted(interface); 284 cJSON_Delete(interface); 285 if (!pInterface) { 286 USB_HILOGE(MODULE_USB_SERVICE, "Print interface error"); 287 return ""; 288 } 289 std::string interfaceJsonStr(pInterface); 290 cJSON_free(pInterface); 291 return interfaceJsonStr; 292 } 293 294 private: 295 int32_t id_ = INT32_MAX; 296 int32_t protocol_ = INT32_MAX; 297 int32_t klass_ = INT32_MAX; 298 int32_t subClass_ = INT32_MAX; 299 int32_t alternateSetting_ = INT32_MAX; 300 std::string name_; 301 std::vector<USBEndpoint> endpoints_; 302 uint8_t iInterface_ = UINT8_MAX; 303 }; 304 } // namespace USB 305 } // namespace OHOS 306 307 #endif // USB_INTERFACE_H 308