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_ENDPOINT_H 17 #define USB_ENDPOINT_H 18 19 #include <iostream> 20 #include <sstream> 21 #include <string> 22 #include "usb_common.h" 23 #include "cJSON.h" 24 25 namespace OHOS { 26 namespace USB { 27 class USBEndpoint { 28 public: USBEndpoint(uint32_t address,uint32_t attributes,uint32_t interval,uint32_t maxPacketSize)29 USBEndpoint(uint32_t address, uint32_t attributes, uint32_t interval, uint32_t maxPacketSize) 30 { 31 this->address_ = address; 32 this->attributes_ = attributes; 33 this->interval_ = static_cast<int32_t>(interval); 34 this->maxPacketSize_ = static_cast<int32_t>(maxPacketSize); 35 } 36 USBEndpoint(const cJSON * endpoint)37 explicit USBEndpoint(const cJSON *endpoint) 38 { 39 if (endpoint == nullptr) { 40 USB_HILOGE(MODULE_USB_SERVICE, "endpoint pointer is nullptr"); 41 } 42 address_ = static_cast<uint32_t>(GetIntValue(endpoint, "address")); 43 attributes_ = static_cast<uint32_t>(GetIntValue(endpoint, "attributes")); 44 interval_ = GetIntValue(endpoint, "interval"); 45 maxPacketSize_ = GetIntValue(endpoint, "maxPacketSize"); 46 interfaceId_ = GetIntValue(endpoint, "interfaceId"); 47 } 48 USBEndpoint()49 USBEndpoint() {} ~USBEndpoint()50 ~USBEndpoint() {} 51 GetIntValue(const cJSON * jsonObject,const char * key)52 static int GetIntValue(const cJSON *jsonObject, const char *key) 53 { 54 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 55 if (item != nullptr && cJSON_IsNumber(item)) { 56 return item->valueint; 57 } else { 58 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 59 return 0; 60 } 61 } 62 GetStringValue(const cJSON * jsonObject,const char * key)63 static std::string GetStringValue(const cJSON *jsonObject, const char *key) 64 { 65 cJSON *item = cJSON_GetObjectItem(jsonObject, key); 66 if (item != nullptr && cJSON_IsString(item)) { 67 return item->valuestring; 68 } else { 69 USB_HILOGE(MODULE_USB_SERVICE, "Invalid or missing %s field", key); 70 return ""; 71 } 72 } 73 GetNumber()74 uint8_t GetNumber() const 75 { 76 return address_ & USB_ENDPOINT_NUMBER_MASK; 77 } 78 GetAddress()79 const uint32_t &GetAddress() const 80 { 81 return address_; 82 } 83 GetDirection()84 uint32_t GetDirection() const 85 { 86 return address_ & USB_ENDPOINT_DIR_MASK; 87 } 88 GetAttributes()89 const uint32_t &GetAttributes() const 90 { 91 return attributes_; 92 } 93 GetEndpointNumber()94 uint32_t GetEndpointNumber() const 95 { 96 return address_ & USB_ENDPOINT_NUMBER_MASK; 97 } 98 GetInterval()99 const int32_t &GetInterval() const 100 { 101 return interval_; 102 } 103 GetMaxPacketSize()104 const int32_t &GetMaxPacketSize() const 105 { 106 return maxPacketSize_; 107 } 108 GetType()109 uint32_t GetType() const 110 { 111 return (attributes_ & USB_ENDPOINT_XFERTYPE_MASK); 112 } 113 ToString()114 std::string ToString() const 115 { 116 std::string ret = "USBEndpoint:[Address:"; 117 ret.append(std::to_string(address_)) 118 .append(", Direction:") 119 .append(std::to_string(GetDirection())) 120 .append(", Attributes:") 121 .append(std::to_string(attributes_)) 122 .append(", EndpointNumber:") 123 .append(std::to_string(GetEndpointNumber())) 124 .append(", Interval:") 125 .append(std::to_string(interval_)) 126 .append(", MaxPacketSize:") 127 .append(std::to_string(maxPacketSize_)) 128 .append(", Type:") 129 .append(std::to_string(GetType())) 130 .append("]"); 131 return ret; 132 } 133 SetAddr(uint32_t val)134 void SetAddr(uint32_t val) 135 { 136 address_ = val; 137 } 138 SetAttr(uint32_t val)139 void SetAttr(uint32_t val) 140 { 141 attributes_ = val; 142 } 143 SetInterval(int32_t val)144 void SetInterval(int32_t val) 145 { 146 interval_ = val; 147 } 148 SetMaxPacketSize(int32_t val)149 void SetMaxPacketSize(int32_t val) 150 { 151 maxPacketSize_ = val; 152 } 153 SetInterfaceId(uint8_t interfaceId)154 void SetInterfaceId(uint8_t interfaceId) 155 { 156 this->interfaceId_ = interfaceId; 157 } 158 GetInterfaceId()159 int8_t GetInterfaceId() const 160 { 161 return interfaceId_; 162 } 163 getJsonString()164 const std::string getJsonString() const 165 { 166 cJSON *endPointJson = cJSON_CreateObject(); 167 if (!endPointJson) { 168 USB_HILOGE(MODULE_USB_SERVICE, "Create endPointJson error"); 169 return ""; 170 } 171 cJSON_AddNumberToObject(endPointJson, "address", static_cast<double>(address_)); 172 cJSON_AddNumberToObject(endPointJson, "attributes", static_cast<double>(attributes_)); 173 cJSON_AddNumberToObject(endPointJson, "interval", static_cast<double>(interval_)); 174 cJSON_AddNumberToObject(endPointJson, "maxPacketSize", static_cast<double>(maxPacketSize_)); 175 cJSON_AddNumberToObject(endPointJson, "direction", static_cast<double>(GetDirection())); 176 cJSON_AddNumberToObject(endPointJson, "number", static_cast<double>(GetEndpointNumber())); 177 cJSON_AddNumberToObject(endPointJson, "type", static_cast<double>(GetType())); 178 cJSON_AddNumberToObject(endPointJson, "interfaceId", static_cast<double>(interfaceId_)); 179 char *pEndPointJson = cJSON_PrintUnformatted(endPointJson); 180 cJSON_Delete(endPointJson); 181 if (!pEndPointJson) { 182 USB_HILOGE(MODULE_USB_SERVICE, "Print endPointJson error"); 183 return ""; 184 } 185 std::string endPointJsonStr(pEndPointJson); 186 cJSON_free(pEndPointJson); 187 pEndPointJson = NULL; 188 return endPointJsonStr; 189 } 190 191 private: 192 uint32_t address_ = 0; 193 uint32_t attributes_ = 0; 194 int32_t interval_ = INVALID_USB_INT_VALUE; 195 int32_t maxPacketSize_ = INVALID_USB_INT_VALUE; 196 uint8_t interfaceId_ = UINT8_MAX; 197 }; 198 } // namespace USB 199 } // namespace OHOS 200 201 #endif // USB_ENDPOINT_H 202