1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. 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 #ifndef USB_ACCESSORY_H 16 #define USB_ACCESSORY_H 17 #include <iostream> 18 #include <memory> 19 #include <mutex> 20 #include <sstream> 21 #include <vector> 22 #include <string> 23 #include "parcel.h" 24 #include "usb_common.h" 25 #include "cJSON.h" 26 27 const int32_t ACC_SIZE = 5; 28 const int32_t ACC_MANUFACTURER_INDEX = 0; 29 const int32_t ACC_PRODUCT_INDEX = 1; 30 const int32_t ACC_DESCRIPTION_INDEX = 2; 31 const int32_t ACC_VERSION_INDEX = 3; 32 const int32_t ACC_SERIAL_NUMBER_INDEX = 4; 33 namespace OHOS { 34 namespace USB { 35 class USBAccessory : public Parcelable { 36 public: USBAccessory(const std::string & manufacturer,const std::string & product,const std::string & description,const std::string & version,const std::string & serialNumber)37 USBAccessory(const std::string &manufacturer, const std::string &product, 38 const std::string &description, const std::string &version, const std::string &serialNumber) 39 { 40 this->manufacturer_ = manufacturer; 41 this->product_ = product; 42 this->description_ = description; 43 this->version_ = version; 44 this->serialNumber_ = serialNumber; 45 } 46 USBAccessory(const cJSON * accesory)47 explicit USBAccessory(const cJSON *accesory) 48 { 49 if (accesory == nullptr) { 50 USB_HILOGE(MODULE_USB_SERVICE, "accesory pointer is nullptr"); 51 return; 52 } 53 manufacturer_ = GetStringValue(accesory, "manufacturer"); 54 product_ = GetStringValue(accesory, "product"); 55 description_ = GetStringValue(accesory, "description"); 56 version_ = GetStringValue(accesory, "version"); 57 serialNumber_ = GetStringValue(accesory, "serialNumber"); 58 } 59 USBAccessory()60 USBAccessory() {} Marshalling(Parcel & parcel)61 bool Marshalling(Parcel &parcel) const override 62 { 63 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->manufacturer_); 64 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->product_); 65 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->description_); 66 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->version_); 67 WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(String, parcel, this->serialNumber_); 68 return true; 69 } 70 Unmarshalling(Parcel & data)71 static USBAccessory *Unmarshalling(Parcel &data) 72 { 73 USBAccessory *usbAccessory = new (std::nothrow) USBAccessory; 74 if (usbAccessory == nullptr) { 75 return nullptr; 76 } 77 usbAccessory->manufacturer_ = data.ReadString(); 78 usbAccessory->product_ = data.ReadString(); 79 usbAccessory->description_ = data.ReadString(); 80 usbAccessory->version_ = data.ReadString(); 81 usbAccessory->serialNumber_ = data.ReadString(); 82 return usbAccessory; 83 } GetStringValue(const cJSON * jsonObject,const char * key)84 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 85 { 86 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 87 if (item != nullptr && cJSON_IsString(item)) { 88 return item->valuestring; 89 } else { 90 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 91 return ""; 92 } 93 } 94 GetJsonString()95 const std::string GetJsonString() const 96 { 97 cJSON *accJson = cJSON_CreateObject(); 98 if (!accJson) { 99 USB_HILOGE(MODULE_USB_SERVICE, "Create accessory error"); 100 return ""; 101 } 102 cJSON_AddStringToObject(accJson, "manufacturer", manufacturer_.c_str()); 103 cJSON_AddStringToObject(accJson, "product", product_.c_str()); 104 cJSON_AddStringToObject(accJson, "description", description_.c_str()); 105 cJSON_AddStringToObject(accJson, "version", version_.c_str()); 106 cJSON_AddStringToObject(accJson, "serialNumber", serialNumber_.c_str()); 107 char *pAccJson = cJSON_PrintUnformatted(accJson); 108 cJSON_Delete(accJson); 109 if (!pAccJson) { 110 USB_HILOGE(MODULE_USB_SERVICE, "Print accJson error"); 111 return ""; 112 } 113 std::string accJsonStr(pAccJson); 114 cJSON_free(pAccJson); 115 pAccJson = NULL; 116 return accJsonStr; 117 } 118 ToString()119 std::string ToString() const 120 { 121 std::ostringstream ss; 122 ss << "manufacturer=" << manufacturer_ << "," 123 << "product=" << product_ << "," 124 << "description=" << description_ << "," 125 << "version=" << version_ << "," 126 << "serialNumber=" << serialNumber_ << "; "; 127 std::string str = "USBAccessory[" + ss.str() + "]"; 128 return str; 129 } 130 SetAccessory(std::vector<std::string> & accessorys)131 void SetAccessory(std::vector<std::string> &accessorys) 132 { 133 if (accessorys.size() < ACC_SIZE) { 134 USB_HILOGE(MODULE_USB_SERVICE, "accessorys param invalid"); 135 return; 136 } 137 138 this->manufacturer_ = accessorys[ACC_MANUFACTURER_INDEX]; 139 this->product_ = accessorys[ACC_PRODUCT_INDEX]; 140 this->description_ = accessorys[ACC_DESCRIPTION_INDEX]; 141 this->version_ = accessorys[ACC_VERSION_INDEX]; 142 this->serialNumber_ = accessorys[ACC_SERIAL_NUMBER_INDEX]; 143 } 144 GetManufacturer()145 std::string GetManufacturer() const 146 { 147 return manufacturer_; 148 } 149 GetProduct()150 std::string GetProduct() const 151 { 152 return product_; 153 } 154 GetDescription()155 std::string GetDescription() const 156 { 157 return description_; 158 } 159 GetVersion()160 std::string GetVersion() const 161 { 162 return version_; 163 } 164 GetSerialNumber()165 std::string GetSerialNumber() const 166 { 167 return serialNumber_; 168 } 169 SetManufacturer(const std::string & manufacturer)170 void SetManufacturer(const std::string &manufacturer) 171 { 172 this->manufacturer_ = manufacturer; 173 } 174 SetProduct(const std::string & product)175 void SetProduct(const std::string &product) 176 { 177 this->product_ = product; 178 } 179 SetDescription(const std::string & description)180 void SetDescription(const std::string &description) 181 { 182 this->description_ = description; 183 } 184 SetVersion(const std::string & version)185 void SetVersion(const std::string &version) 186 { 187 this->version_ = version; 188 } 189 SetSerialNumber(const std::string & serialNumber)190 void SetSerialNumber(const std::string &serialNumber) 191 { 192 this->serialNumber_ = serialNumber; 193 } 194 195 bool operator==(USBAccessory& obj) const 196 { 197 return (compare(manufacturer_, obj.GetManufacturer()) && 198 compare(product_, obj.GetProduct()) && 199 compare(description_, obj.GetDescription()) && 200 compare(version_, obj.GetVersion()) && 201 compare(serialNumber_, obj.GetSerialNumber())); 202 } 203 204 private: compare(const std::string & s1,const std::string & s2)205 static bool compare(const std::string &s1, const std::string &s2) 206 { 207 return s1 == s2; 208 } 209 210 private: 211 std::string manufacturer_; 212 std::string product_; 213 std::string description_; 214 std::string version_; 215 std::string serialNumber_; 216 }; 217 218 } // USB 219 } // OHOS 220 #endif // USB_ACCESSORY_H