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 "json.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 Json::Value & endpoint)37 explicit USBEndpoint(const Json::Value &endpoint) 38 { 39 address_ = endpoint["address"].asUInt(); 40 attributes_ = endpoint["attributes"].asUInt(); 41 interval_ = endpoint["interval"].asInt(); 42 maxPacketSize_ = endpoint["maxPacketSize"].asInt(); 43 interfaceId_ = endpoint["interfaceId"].asUInt(); 44 } 45 USBEndpoint()46 USBEndpoint() {} ~USBEndpoint()47 ~USBEndpoint() {} 48 GetNumber()49 uint8_t GetNumber() const 50 { 51 return address_ & USB_ENDPOINT_NUMBER_MASK; 52 } 53 GetAddress()54 const uint32_t &GetAddress() const 55 { 56 return address_; 57 } 58 GetDirection()59 uint32_t GetDirection() const 60 { 61 return address_ & USB_ENDPOINT_DIR_MASK; 62 } 63 GetAttributes()64 const uint32_t &GetAttributes() const 65 { 66 return attributes_; 67 } 68 GetEndpointNumber()69 uint32_t GetEndpointNumber() const 70 { 71 return address_ & USB_ENDPOINT_NUMBER_MASK; 72 } 73 GetInterval()74 const int32_t &GetInterval() const 75 { 76 return interval_; 77 } 78 GetMaxPacketSize()79 const int32_t &GetMaxPacketSize() const 80 { 81 return maxPacketSize_; 82 } 83 GetType()84 uint32_t GetType() const 85 { 86 return (attributes_ & USB_ENDPOINT_XFERTYPE_MASK); 87 } 88 ToString()89 std::string ToString() const 90 { 91 std::string ret = "USBEndpoint:[Address:"; 92 ret.append(std::to_string(address_)) 93 .append(", Direction:") 94 .append(std::to_string(GetDirection())) 95 .append(", Attributes:") 96 .append(std::to_string(attributes_)) 97 .append(", EndpointNumber:") 98 .append(std::to_string(GetEndpointNumber())) 99 .append(", Interval:") 100 .append(std::to_string(interval_)) 101 .append(", MaxPacketSize:") 102 .append(std::to_string(maxPacketSize_)) 103 .append(", Type:") 104 .append(std::to_string(GetType())) 105 .append("]"); 106 return ret; 107 } 108 SetAddr(uint32_t val)109 void SetAddr(uint32_t val) 110 { 111 address_ = val; 112 } 113 SetAttr(uint32_t val)114 void SetAttr(uint32_t val) 115 { 116 attributes_ = val; 117 } 118 SetInterval(int32_t val)119 void SetInterval(int32_t val) 120 { 121 interval_ = val; 122 } 123 SetMaxPacketSize(int32_t val)124 void SetMaxPacketSize(int32_t val) 125 { 126 maxPacketSize_ = val; 127 } 128 SetInterfaceId(uint8_t interfaceId)129 void SetInterfaceId(uint8_t interfaceId) 130 { 131 this->interfaceId_ = interfaceId; 132 } 133 GetInterfaceId()134 int8_t GetInterfaceId() const 135 { 136 return interfaceId_; 137 } 138 ToJson()139 Json::Value ToJson() const 140 { 141 Json::Value endpoint; 142 endpoint["address"] = address_; 143 endpoint["attributes"] = attributes_; 144 endpoint["interval"] = interval_; 145 endpoint["maxPacketSize"] = maxPacketSize_; 146 endpoint["direction"] = GetDirection(); 147 endpoint["number"] = GetEndpointNumber(); 148 endpoint["type"] = GetType(); 149 endpoint["interfaceId"] = interfaceId_; 150 151 return endpoint; 152 } 153 154 private: 155 uint32_t address_ = 0; 156 uint32_t attributes_ = 0; 157 int32_t interval_ = INVALID_USB_INT_VALUE; 158 int32_t maxPacketSize_ = INVALID_USB_INT_VALUE; 159 uint8_t interfaceId_ = UINT8_MAX; 160 }; 161 } // namespace USB 162 } // namespace OHOS 163 164 #endif // USB_ENDPOINT_H 165