1 /* 2 * Copyright (c) 2021 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_COMMON_H 17 #define USB_COMMON_H 18 19 #include <cstdint> 20 #include <utility> 21 22 #include "hilog_wrapper.h" 23 namespace OHOS { 24 namespace USB { 25 /** 26 * Invalid usb int value 27 */ 28 constexpr int32_t INVALID_USB_INT_VALUE = -1; 29 30 /** 31 * Bitmask used for extracting the USBEndpoint type from it's address 32 */ 33 constexpr uint32_t USB_ENDPOINT_XFERTYPE_MASK = 0x03; 34 35 /** 36 * Control USBEndpoint type 37 */ 38 constexpr int32_t USB_ENDPOINT_XFER_CONTROL = 0; 39 40 /** 41 * Isochronous USBEndpoint type 42 */ 43 constexpr int32_t USB_ENDPOINT_XFER_ISOC = 1; 44 45 /** 46 * Bulk USBEndpoint type 47 */ 48 constexpr int32_t USB_ENDPOINT_XFER_BULK = 2; 49 50 /** 51 * Interrupt USBEndpoint type 52 */ 53 constexpr int32_t USB_ENDPOINT_XFER_INT = 3; 54 55 /** 56 * Bitmask used for extracting the USBEndpoint number from it's address 57 */ 58 constexpr uint32_t USB_ENDPOINT_NUMBER_MASK = 0x0f; 59 60 /** 61 * Bitmask used for extracting the USBEndpoint direction from it's address 62 */ 63 constexpr uint32_t USB_ENDPOINT_DIR_MASK = 0x80; 64 65 /** 66 * Used to signify direction of data for USBEndpoint is OUT, host to device 67 */ 68 constexpr uint32_t USB_ENDPOINT_DIR_OUT = 0; 69 70 /** 71 * Used to signify direction of data for USBEndpoint is IN, device to host 72 */ 73 constexpr uint32_t USB_ENDPOINT_DIR_IN = 0x80; 74 75 /** 76 * Bitmask for self power in the USBConfig 77 */ 78 constexpr uint32_t USB_CFG_SELF_POWERED = 0x80; 79 80 /** 81 * Bitmask for remote wakeup in the USBConfig 82 */ 83 constexpr uint32_t USB_CFG_REMOTE_WAKEUP = 0x20; 84 85 #define INVALID_STRING_VALUE ("") 86 #define RETURN_IF_WITH_RET(cond, retval) \ 87 if (cond) { \ 88 return (retval); \ 89 } 90 #define RETURN_IF(cond) \ 91 if (cond) { \ 92 return; \ 93 } 94 #define RETURN_IF_WITH_LOG(cond, loginfo) \ 95 do { \ 96 if (cond) { \ 97 USB_HILOGE(MODULE_COMMON, "%{public}s " #loginfo " ", __func__); \ 98 return; \ 99 } \ 100 } while (0) 101 102 #define READ_PARCEL_NO_RET(parcel, type, out) \ 103 do { \ 104 if (!(parcel).Read##type(out)) { \ 105 USB_HILOGE(MODULE_COMMON, "%{public}s read " #out " failed", __func__); \ 106 return; \ 107 } \ 108 } while (0) 109 110 #define WRITE_PARCEL_NO_RET(parcel, type, data) \ 111 do { \ 112 if (!(parcel).Write##type(data)) { \ 113 USB_HILOGE(MODULE_COMMON, "%{public}s write " #data " failed", __func__); \ 114 return; \ 115 } \ 116 } while (0) 117 118 #define WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(type, parcel, data) \ 119 do { \ 120 if (!(parcel).Write##type(data)) { \ 121 USB_HILOGE(MODULE_COMMON, "%{public}s write " #data " failed", __func__); \ 122 return false; \ 123 } \ 124 } while (0) 125 126 #define READ_PARCEL_WITH_RET(parcel, type, out, retval) \ 127 do { \ 128 if (!(parcel).Read##type(out)) { \ 129 USB_HILOGE(MODULE_COMMON, "%{public}s read " #out " failed", __func__); \ 130 return (retval); \ 131 } \ 132 } while (0) 133 134 #define WRITE_PARCEL_WITH_RET(parcel, type, data, retval) \ 135 do { \ 136 if (!(parcel).Write##type(data)) { \ 137 USB_HILOGE(MODULE_COMMON, "%{public}s write " #data " failed", __func__); \ 138 return (retval); \ 139 } \ 140 } while (0) 141 142 template <typename E> 143 constexpr auto ToUnderlying(E e) noexcept 144 { 145 return static_cast<std::underlying_type_t<E>>(e); 146 } 147 } // namespace USB 148 } // namespace OHOS 149 150 #endif // USB_COMMON_H 151