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 usbConfig->interfaces_.push_back(*pInf); 105 delete pInf; 106 pInf = nullptr; 107 } 108 return usbConfig; 109 } GetIntValue(const cJSON * jsonObject,const char * key)110 static int GetIntValue(const cJSON *jsonObject, const char *key) 111 { 112 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 113 if (item != nullptr && cJSON_IsNumber(item)) { 114 return item->valueint; 115 } else { 116 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 117 return 0; 118 } 119 } 120 GetStringValue(const cJSON * jsonObject,const char * key)121 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 122 { 123 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 124 if (item != nullptr && cJSON_IsString(item)) { 125 return item->valuestring; 126 } else { 127 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 128 return ""; 129 } 130 } 131 GetId()132 const int32_t &GetId() const 133 { 134 return id_; 135 } 136 GetAttributes()137 const uint32_t &GetAttributes() const 138 { 139 return attributes_; 140 } 141 GetInterface(uint32_t index,UsbInterface & interface)142 bool GetInterface(uint32_t index, UsbInterface &interface) const 143 { 144 if (index >= interfaces_.size()) { 145 return false; 146 } 147 interface = interfaces_[index]; 148 return true; 149 } 150 GetInterfaceCount()151 uint32_t GetInterfaceCount() const 152 { 153 return interfaces_.size(); 154 } 155 GetMaxPower()156 int32_t GetMaxPower() const 157 { 158 // 2 represent maxPower units 159 return maxPower_ * 2; 160 } 161 GetName()162 const std::string &GetName() const 163 { 164 return name_; 165 } 166 IsRemoteWakeup()167 bool IsRemoteWakeup() const 168 { 169 return (attributes_ & USB_CFG_REMOTE_WAKEUP) != 0; 170 } 171 IsSelfPowered()172 bool IsSelfPowered() const 173 { 174 return (attributes_ & USB_CFG_SELF_POWERED) != 0; 175 } 176 SetInterfaces(const std::vector<UsbInterface> & interfaces)177 void SetInterfaces(const std::vector<UsbInterface> &interfaces) 178 { 179 this->interfaces_ = interfaces; 180 } 181 GetInterfaces()182 std::vector<UsbInterface> &GetInterfaces() 183 { 184 return interfaces_; 185 } 186 SetId(int32_t id)187 void SetId(int32_t id) 188 { 189 this->id_ = id; 190 } 191 SetAttribute(uint32_t attributes)192 void SetAttribute(uint32_t attributes) 193 { 194 this->attributes_ = attributes; 195 } 196 SetMaxPower(int32_t maxPower)197 void SetMaxPower(int32_t maxPower) 198 { 199 this->maxPower_ = maxPower; 200 } 201 ToString()202 std::string ToString() const 203 { 204 std::ostringstream ss; 205 ss << "name=" << name_ << "," 206 << "id=" << id_ << "," 207 << "iConfiguration=" << (int32_t)iConfiguration_ << "," 208 << "attributes=" << attributes_ << "," 209 << "maxPower=" << maxPower_ << "; "; 210 std::string str = "USBConfig[" + ss.str() + "]"; 211 ss.str(""); 212 for (size_t i = 0; i < interfaces_.size(); ++i) { 213 const UsbInterface &interface = interfaces_[i]; 214 str += interface.ToString(); 215 } 216 return str; 217 } 218 SetName(const std::string & name)219 void SetName(const std::string &name) 220 { 221 this->name_ = name; 222 } 223 SetiConfiguration(uint8_t idx)224 void SetiConfiguration(uint8_t idx) 225 { 226 this->iConfiguration_ = idx; 227 } 228 GetiConfiguration()229 uint8_t GetiConfiguration() 230 { 231 return this->iConfiguration_; 232 } 233 getJsonString()234 const std::string getJsonString() const 235 { 236 cJSON* config = cJSON_CreateObject(); 237 if (!config) { 238 USB_HILOGE(MODULE_USB_SERVICE, "Create config error"); 239 return ""; 240 } 241 cJSON_AddNumberToObject(config, "id", static_cast<double>(id_)); 242 cJSON_AddNumberToObject(config, "attributes", static_cast<double>(attributes_)); 243 cJSON_AddNumberToObject(config, "maxPower", static_cast<double>(maxPower_)); 244 cJSON_AddStringToObject(config, "name", name_.c_str()); 245 cJSON_AddBoolToObject(config, "isRemoteWakeup", IsRemoteWakeup()); 246 cJSON_AddBoolToObject(config, "isSelfPowered", IsSelfPowered()); 247 248 cJSON* interfaces = cJSON_CreateArray(); 249 if (!interfaces) { 250 USB_HILOGE(MODULE_USB_SERVICE, "Create interfaces error"); 251 cJSON_Delete(config); 252 return ""; 253 } 254 for (auto &intf : interfaces_) { 255 cJSON* pInterface = cJSON_Parse(intf.getJsonString().c_str()); 256 cJSON_AddItemToArray(interfaces, pInterface); 257 } 258 cJSON_AddItemToObject(config, "interfaces", interfaces); 259 char *pConfigStr = cJSON_PrintUnformatted(config); 260 cJSON_Delete(config); 261 if (!pConfigStr) { 262 USB_HILOGE(MODULE_USB_SERVICE, "Print config error"); 263 return ""; 264 } 265 std::string configStr(pConfigStr); 266 cJSON_free(pConfigStr); 267 return configStr; 268 } 269 270 private: 271 int32_t id_ = INVALID_USB_INT_VALUE; 272 uint32_t attributes_ = 0; 273 std::vector<UsbInterface> interfaces_; 274 int32_t maxPower_ = INVALID_USB_INT_VALUE; 275 std::string name_; 276 uint8_t iConfiguration_ = UINT8_MAX; 277 }; 278 } // namespace USB 279 } // namespace OHOS 280 281 #endif // USB_CONFIG_H 282