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