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_CONFIG_H 17 #define USB_CONFIG_H 18 19 #include <iostream> 20 #include <memory> 21 #include <mutex> 22 #include <sstream> 23 #include <vector> 24 #include "parcel.h" 25 #include "usb_common.h" 26 #include "usb_interface.h" 27 #include "cJSON.h" 28 29 namespace OHOS { 30 namespace USB { 31 constexpr uint32_t USB_INTERFACE_MAX_NUM = 128; 32 33 class USBConfig : public Parcelable { 34 public: USBConfig(uint32_t id,uint32_t attributes,std::string name,uint32_t maxPower,std::vector<UsbInterface> interfaces)35 USBConfig(uint32_t id, uint32_t attributes, std::string name, uint32_t maxPower, 36 std::vector<UsbInterface> interfaces) 37 { 38 this->id_ = static_cast<int32_t>(id); 39 this->attributes_ = attributes; 40 this->maxPower_ = static_cast<int32_t>(maxPower); 41 this->name_ = name; 42 this->interfaces_ = interfaces; 43 } 44 USBConfig(const cJSON * config)45 explicit USBConfig(const cJSON *config) 46 { 47 if (config == nullptr) { 48 USB_HILOGE(MODULE_USB_SERVICE, "config pointer is nullptr"); 49 } 50 id_ = GetIntValue(config, "id"); 51 attributes_ = static_cast<uint32_t>(GetIntValue(config, "attributes")); 52 maxPower_ = GetIntValue(config, "maxPower"); 53 name_ = GetStringValue(config, "name"); 54 cJSON *jsonInterfaces = cJSON_GetObjectItem(config, "interfaces"); 55 if (jsonInterfaces != nullptr) { 56 for (int i = 0; i < cJSON_GetArraySize(jsonInterfaces); i++) { 57 cJSON *jsonInterface = cJSON_GetArrayItem(jsonInterfaces, i); 58 if (jsonInterface == nullptr) { 59 USB_HILOGE(MODULE_USB_SERVICE, "get item nullptr"); 60 continue; 61 } 62 UsbInterface interface(jsonInterface); 63 interfaces_.emplace_back(interface); 64 } 65 } 66 } 67 USBConfig()68 USBConfig() {} ~USBConfig()69 ~USBConfig() {} Marshalling(Parcel & parcel)70 bool Marshalling(Parcel &parcel) const override 71 { 72 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->id_); 73 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint32, parcel, this->attributes_); 74 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, this->maxPower_); 75 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint8, parcel, this->iConfiguration_); 76 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->name_); 77 78 uint32_t infCount = this->interfaces_.size(); 79 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Uint32, parcel, infCount); 80 81 for (uint32_t i = 0; i < infCount && i < USB_INTERFACE_MAX_NUM; i++) { 82 this->interfaces_[i].Marshalling(parcel); 83 } 84 return true; 85 } 86 Unmarshalling(Parcel & data)87 static USBConfig *Unmarshalling(Parcel &data) 88 { 89 USBConfig *usbConfig = new (std::nothrow) USBConfig; 90 if (usbConfig == nullptr) { 91 return nullptr; 92 } 93 usbConfig->id_ = data.ReadInt32(); 94 usbConfig->attributes_ = data.ReadUint32(); 95 usbConfig->maxPower_ = data.ReadInt32(); 96 usbConfig->iConfiguration_ = data.ReadUint8(); 97 usbConfig->name_ = data.ReadString(); 98 99 uint32_t infCount = 0; 100 infCount = data.ReadUint32(); 101 102 for (uint32_t i = 0; i < infCount && i < USB_INTERFACE_MAX_NUM; i++) { 103 UsbInterface *pInf = UsbInterface::Unmarshalling(data); 104 if (pInf == nullptr) { 105 continue; 106 } 107 usbConfig->interfaces_.push_back(*pInf); 108 delete pInf; 109 pInf = nullptr; 110 } 111 return usbConfig; 112 } GetIntValue(const cJSON * jsonObject,const char * key)113 static int GetIntValue(const cJSON *jsonObject, const char *key) 114 { 115 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 116 if (item != nullptr && cJSON_IsNumber(item)) { 117 return item->valueint; 118 } else { 119 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 120 return 0; 121 } 122 } 123 GetStringValue(const cJSON * jsonObject,const char * key)124 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 125 { 126 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 127 if (item != nullptr && cJSON_IsString(item)) { 128 return item->valuestring; 129 } else { 130 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 131 return ""; 132 } 133 } 134 GetId()135 const int32_t &GetId() const 136 { 137 return id_; 138 } 139 GetAttributes()140 const uint32_t &GetAttributes() const 141 { 142 return attributes_; 143 } 144 GetInterface(uint32_t index,UsbInterface & interface)145 bool GetInterface(uint32_t index, UsbInterface &interface) const 146 { 147 if (index >= interfaces_.size()) { 148 return false; 149 } 150 interface = interfaces_[index]; 151 return true; 152 } 153 GetInterfaceCount()154 uint32_t GetInterfaceCount() const 155 { 156 return interfaces_.size(); 157 } 158 GetMaxPower()159 int32_t GetMaxPower() const 160 { 161 // 2 represent maxPower units 162 return maxPower_ * 2; 163 } 164 GetName()165 const std::string &GetName() const 166 { 167 return name_; 168 } 169 IsRemoteWakeup()170 bool IsRemoteWakeup() const 171 { 172 return (attributes_ & USB_CFG_REMOTE_WAKEUP) != 0; 173 } 174 IsSelfPowered()175 bool IsSelfPowered() const 176 { 177 return (attributes_ & USB_CFG_SELF_POWERED) != 0; 178 } 179 SetInterfaces(const std::vector<UsbInterface> & interfaces)180 void SetInterfaces(const std::vector<UsbInterface> &interfaces) 181 { 182 this->interfaces_ = interfaces; 183 } 184 GetInterfaces()185 std::vector<UsbInterface> &GetInterfaces() 186 { 187 return interfaces_; 188 } 189 SetId(int32_t id)190 void SetId(int32_t id) 191 { 192 this->id_ = id; 193 } 194 SetAttribute(uint32_t attributes)195 void SetAttribute(uint32_t attributes) 196 { 197 this->attributes_ = attributes; 198 } 199 SetMaxPower(int32_t maxPower)200 void SetMaxPower(int32_t maxPower) 201 { 202 this->maxPower_ = maxPower; 203 } 204 ToString()205 std::string ToString() const 206 { 207 std::ostringstream ss; 208 ss << "name=" << name_ << "," 209 << "id=" << id_ << "," 210 << "iConfiguration=" << (int32_t)iConfiguration_ << "," 211 << "attributes=" << attributes_ << "," 212 << "maxPower=" << maxPower_ << "; "; 213 std::string str = "USBConfig[" + ss.str() + "]"; 214 ss.str(""); 215 for (size_t i = 0; i < interfaces_.size(); ++i) { 216 const UsbInterface &interface = interfaces_[i]; 217 str += interface.ToString(); 218 } 219 return str; 220 } 221 SetName(const std::string & name)222 void SetName(const std::string &name) 223 { 224 this->name_ = name; 225 } 226 SetiConfiguration(uint8_t idx)227 void SetiConfiguration(uint8_t idx) 228 { 229 this->iConfiguration_ = idx; 230 } 231 GetiConfiguration()232 uint8_t GetiConfiguration() 233 { 234 return this->iConfiguration_; 235 } 236 getJsonString()237 const std::string getJsonString() const 238 { 239 cJSON* config = cJSON_CreateObject(); 240 if (!config) { 241 USB_HILOGE(MODULE_USB_SERVICE, "Create config error"); 242 return ""; 243 } 244 cJSON_AddNumberToObject(config, "id", static_cast<double>(id_)); 245 cJSON_AddNumberToObject(config, "attributes", static_cast<double>(attributes_)); 246 cJSON_AddNumberToObject(config, "maxPower", static_cast<double>(maxPower_)); 247 cJSON_AddStringToObject(config, "name", name_.c_str()); 248 cJSON_AddBoolToObject(config, "isRemoteWakeup", IsRemoteWakeup()); 249 cJSON_AddBoolToObject(config, "isSelfPowered", IsSelfPowered()); 250 251 cJSON* interfaces = cJSON_CreateArray(); 252 if (!interfaces) { 253 USB_HILOGE(MODULE_USB_SERVICE, "Create interfaces error"); 254 cJSON_Delete(config); 255 return ""; 256 } 257 for (auto &intf : interfaces_) { 258 cJSON* pInterface = cJSON_Parse(intf.getJsonString().c_str()); 259 cJSON_AddItemToArray(interfaces, pInterface); 260 } 261 cJSON_AddItemToObject(config, "interfaces", interfaces); 262 char *pConfigStr = cJSON_PrintUnformatted(config); 263 cJSON_Delete(config); 264 if (!pConfigStr) { 265 USB_HILOGE(MODULE_USB_SERVICE, "Print config error"); 266 return ""; 267 } 268 std::string configStr(pConfigStr); 269 cJSON_free(pConfigStr); 270 return configStr; 271 } 272 273 private: 274 int32_t id_ = INVALID_USB_INT_VALUE; 275 uint32_t attributes_ = 0; 276 std::vector<UsbInterface> interfaces_; 277 int32_t maxPower_ = INVALID_USB_INT_VALUE; 278 std::string name_; 279 uint8_t iConfiguration_ = UINT8_MAX; 280 }; 281 } // namespace USB 282 } // namespace OHOS 283 284 #endif // USB_CONFIG_H 285