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