1 /* 2 * Copyright (c) 2021-2022 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 "usb_endpoint.h" 25 26 namespace OHOS { 27 namespace USB { 28 class UsbInterface { 29 public: UsbInterface(int32_t id,int32_t protocol,int32_t interfaceClass,int32_t subClass,int32_t alternateSetting,std::string name,std::vector<USBEndpoint> endpoints)30 UsbInterface(int32_t id, 31 int32_t protocol, 32 int32_t interfaceClass, 33 int32_t subClass, 34 int32_t alternateSetting, 35 std::string name, 36 std::vector<USBEndpoint> endpoints) 37 { 38 this->id_ = id; 39 this->protocol_ = protocol; 40 this->klass_ = interfaceClass; 41 this->subClass_ = subClass; 42 this->alternateSetting_ = alternateSetting; 43 this->endpoints_ = endpoints; 44 } 45 UsbInterface(const Json::Value & interface)46 explicit UsbInterface(const Json::Value &interface) 47 { 48 id_ = interface["id"].asInt(); 49 protocol_ = interface["protocol"].asInt(); 50 klass_ = interface["clazz"].asInt(); 51 subClass_ = interface["subClass"].asInt(); 52 alternateSetting_ = interface["alternateSetting"].asInt(); 53 name_ = interface["name"].asString(); 54 55 Json::Value endpoints = interface["endpoints"]; 56 for (uint32_t idx = 0; idx < endpoints.size(); ++idx) { 57 endpoints_.emplace_back(endpoints[idx]); 58 } 59 } 60 UsbInterface()61 UsbInterface() {} 62 GetName()63 const std::string &GetName() const 64 { 65 return name_; 66 } 67 GetId()68 int32_t GetId() const 69 { 70 return id_; 71 } 72 GetClass()73 int32_t GetClass() const 74 { 75 return klass_; 76 } 77 GetSubClass()78 int32_t GetSubClass() const 79 { 80 return subClass_; 81 } 82 GetAlternateSetting()83 int32_t GetAlternateSetting() const 84 { 85 return alternateSetting_; 86 } 87 GetProtocol()88 int32_t GetProtocol() const 89 { 90 return protocol_; 91 } 92 GetEndpointCount()93 int32_t GetEndpointCount() const 94 { 95 return endpoints_.size(); 96 } 97 GetEndpoint(uint32_t index)98 std::optional<USBEndpoint> GetEndpoint(uint32_t index) const 99 { 100 if (index >= endpoints_.size()) { 101 USB_HILOGE(MODULE_USB_INNERKIT, "invalid index=%{public}u !", index); 102 return std::nullopt; 103 } 104 105 return endpoints_[index]; 106 } 107 GetEndpoints()108 std::vector<USBEndpoint> &GetEndpoints() 109 { 110 return endpoints_; 111 } 112 SetEndpoints(const std::vector<USBEndpoint> & eps)113 void SetEndpoints(const std::vector<USBEndpoint> &eps) 114 { 115 endpoints_ = eps; 116 } 117 SetId(int32_t id)118 void SetId(int32_t id) 119 { 120 id_ = id; 121 } 122 SetProtocol(int32_t protocol)123 void SetProtocol(int32_t protocol) 124 { 125 protocol_ = protocol; 126 } 127 SetClass(int32_t klass)128 void SetClass(int32_t klass) 129 { 130 klass_ = klass; 131 } 132 SetSubClass(int32_t subClass)133 void SetSubClass(int32_t subClass) 134 { 135 subClass_ = subClass; 136 } 137 SetAlternateSetting(int32_t alternateSetting)138 void SetAlternateSetting(int32_t alternateSetting) 139 { 140 alternateSetting_ = alternateSetting; 141 } 142 SetName(const std::string & name)143 void SetName(const std::string &name) 144 { 145 name_ = name; 146 } 147 ~UsbInterface()148 ~UsbInterface() {} 149 ToString()150 std::string ToString() const 151 { 152 std::ostringstream ss; 153 ss << "id=" << id_ << "," 154 << "name_=" << name_ << "," 155 << "iInterface_=" << (int32_t)iInterface_ << "," 156 << "klass_=" << klass_ << "," 157 << "subClass_=" << subClass_ << "," 158 << "protocol_=" << protocol_ << "," 159 << "alternateSetting_=" << alternateSetting_ << ""; 160 std::string str = "UsbInterface[" + ss.str() + "]; "; 161 ss.str(""); 162 for (size_t i = 0; i < endpoints_.size(); ++i) { 163 const USBEndpoint &endpoint = endpoints_[i]; 164 str += endpoint.ToString(); 165 } 166 return str; 167 } 168 SetiInterface(uint8_t idx)169 void SetiInterface(uint8_t idx) 170 { 171 this->iInterface_ = idx; 172 } 173 GetiInterface()174 uint8_t GetiInterface() 175 { 176 return this->iInterface_; 177 } 178 ToJson()179 Json::Value ToJson() const 180 { 181 Json::Value interface; 182 interface["id"] = id_; 183 interface["protocol"] = protocol_; 184 interface["clazz"] = klass_; 185 interface["subClass"] = subClass_; 186 interface["alternateSetting"] = alternateSetting_; 187 interface["name"] = name_; 188 189 Json::Value endpoints; 190 for (const auto &ep : endpoints_) { 191 endpoints.append(ep.ToJson()); 192 } 193 interface["endpoints"] = endpoints; 194 195 return interface; 196 } 197 198 private: 199 int32_t id_ = INT32_MAX; 200 int32_t protocol_ = INT32_MAX; 201 int32_t klass_ = INT32_MAX; 202 int32_t subClass_ = INT32_MAX; 203 int32_t alternateSetting_ = INT32_MAX; 204 std::string name_; 205 std::vector<USBEndpoint> endpoints_; 206 uint8_t iInterface_ = UINT8_MAX; 207 }; 208 } // namespace USB 209 } // namespace OHOS 210 211 #endif // USB_INTERFACE_H 212