1 /* 2 * Copyright (c) 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_NAPI_ERRORS_H 17 #define USB_NAPI_ERRORS_H 18 19 #include <map> 20 #include <string_view> 21 #include "napi/native_api.h" 22 23 namespace OHOS { 24 namespace USB { 25 enum UsbJsErrCode : int32_t { 26 OHEC_COMMON_PERMISSION_NOT_ALLOWED = 201, 27 OHEC_COMMON_NORMAL_APP_NOT_ALLOWED = 202, 28 OHEC_COMMON_PARAM_ERROR = 401, 29 CAPABILITY_NOT_SUPPORT = 801, 30 31 UEC_COMMON_BASE = 14400000, 32 UEC_COMMON_HAS_NO_RIGHT = UEC_COMMON_BASE + 1, 33 UEC_COMMON_HDC_NOT_ALLOWED = UEC_COMMON_BASE + 2, 34 UEC_COMMON_PORTROLE_SWITCH_NOT_ALLOWED = UEC_COMMON_BASE + 3, 35 UEC_COMMON_SERVICE_EXCEPTION = UEC_COMMON_BASE + 4, 36 UEC_COMMON_RIGHT_DATABASE_ERROR = UEC_COMMON_BASE + 5, 37 UEC_COMMON_FUNCTION_NOT_SUPPORT = UEC_COMMON_BASE + 6, 38 39 UEC_ACCESSORY_BASE = UEC_COMMON_BASE + 1000, 40 UEC_ACCESSORY_NOT_MATCH = UEC_ACCESSORY_BASE + 1, 41 UEC_ACCESSORY_OPEN_FAILED = UEC_ACCESSORY_BASE + 2, 42 UEC_ACCESSORY_CAN_NOT_REOPEN = UEC_ACCESSORY_BASE + 3, 43 44 USB_SUBMIT_TRANSFER_RESOURCE_BUSY_ERROR = 14400007, 45 USB_SUBMIT_TRANSFER_NO_DEVICE_ERROR = 14400008, 46 USB_SUBMIT_TRANSFER_NO_MEM_ERROR = 14400009, 47 USB_SUBMIT_TRANSFER_OTHER_ERROR = 14400010, 48 USB_SUBMIT_TRANSFER_NOT_FOUND_ERROR = 14400011, 49 USB_SUBMIT_TRANSFER_IO_ERROR = 14400012, 50 USB_DEVICE_PIPE_CHECK_ERROR = 14400013, 51 }; 52 53 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = { 54 {OHEC_COMMON_PERMISSION_NOT_ALLOWED, 55 "BusinessError 201:Permission verification failed. " 56 "The application does not have the permission required to call the API."}, 57 {OHEC_COMMON_NORMAL_APP_NOT_ALLOWED, "BusinessError 202:Permission denied. Normal application uses system api." }, 58 {OHEC_COMMON_PARAM_ERROR, "BusinessError 401:Parameter error." }, 59 {CAPABILITY_NOT_SUPPORT, "BusinessError 801:Capability not supported."}, 60 {UEC_COMMON_HAS_NO_RIGHT, "BusinessError 14400001:Permission denied." }, 61 {UEC_COMMON_HDC_NOT_ALLOWED, "BusinessError 14400002:Permission denied. The HDC is disabled by the system."}, 62 {UEC_COMMON_PORTROLE_SWITCH_NOT_ALLOWED, 63 "BusinessError 14400003:Unsupported operation.The current device does not support port role switching." }, 64 {UEC_COMMON_SERVICE_EXCEPTION, 65 "BusinessError 14400004:Service exception. Possible causes:No accessory is plugged in."}, 66 {UEC_COMMON_RIGHT_DATABASE_ERROR, "BusinessError 14400005:Database operation exception."}, 67 {UEC_COMMON_FUNCTION_NOT_SUPPORT, 68 "BusinessError 14400006:Unsupported operation. The function is not supported."}, 69 {UEC_ACCESSORY_NOT_MATCH, "BusinessError 14401001:The target USBAccessory not matched."}, 70 {UEC_ACCESSORY_OPEN_FAILED, "BusinessError 14401002:Failed to open the native accessory node."}, 71 {UEC_ACCESSORY_CAN_NOT_REOPEN, "BusinessError 14401003:Cannot reopen the accessory."}, 72 73 {USB_SUBMIT_TRANSFER_RESOURCE_BUSY_ERROR, "BusinessError 14400007:Resource busy. Possible causes:" 74 "1. The transfer has already been submitted." 75 "2. The interface is claimed by another program or driver."}, 76 {USB_SUBMIT_TRANSFER_NO_DEVICE_ERROR, "BusinessError 14400008:No such device (it may have been disconnected)."}, 77 {USB_SUBMIT_TRANSFER_NO_MEM_ERROR, 78 "BusinessError 14400009:Insufficient memory. Possible causes: 1. Malloc memory failed."}, 79 {USB_SUBMIT_TRANSFER_OTHER_ERROR, "BusinessError 14400010:Other USB error."}, 80 {USB_SUBMIT_TRANSFER_NOT_FOUND_ERROR, 81 "BusinessError 14400011:The transfer is not in progress, or is already complete or cancelled."}, 82 {USB_SUBMIT_TRANSFER_IO_ERROR, "BusinessError 14400012:Transmission I/O error."}, 83 {USB_DEVICE_PIPE_CHECK_ERROR, "BusinessError 14400013:The USBDevicePipe validity check failed. Possible causes:" 84 "1.The input parameters fail the validation check." 85 "2.The call chain used to obtain the input parameters is not reasonable."}, 86 }; 87 88 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 89 napi_value CreateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 90 91 #define USB_ASSERT_BASE(env, assertion, errCode, errMsg, retVal) \ 92 do { \ 93 if (!(assertion)) { \ 94 USB_HILOGE(MODULE_JS_NAPI, #errMsg); \ 95 ThrowBusinessError((env), errCode, errMsg); \ 96 return retVal; \ 97 } \ 98 } while (0) 99 100 #define NOTHING 101 #define USB_ASSERT(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, nullptr) 102 #define USB_ASSERT_RETURN_VOID(env, assertion, errCode, errMsg) \ 103 USB_ASSERT_BASE(env, assertion, errCode, errMsg, NOTHING) 104 #define USB_ASSERT_RETURN_FALSE(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, false) 105 #define USB_ASSERT_RETURN_UNDEF(env, assertion, errCode, errMsg) \ 106 do { \ 107 napi_value obj = nullptr; \ 108 napi_get_undefined(env, &obj); \ 109 USB_ASSERT_BASE(env, assertion, errCode, errMsg, obj); \ 110 } while (0) 111 112 #define NAPI_CHECK(env, theCall, loginfo) \ 113 do { \ 114 if ((theCall) != napi_ok) { \ 115 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 116 napi_value obj = nullptr; \ 117 napi_get_undefined(env, &obj); \ 118 return obj; \ 119 } \ 120 } while (0) 121 122 #define NAPI_CHECK_BASE(theCall, loginfo, retVal) \ 123 do { \ 124 if ((theCall) != napi_ok) { \ 125 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 126 return retVal; \ 127 } \ 128 } while (0) 129 130 #define NAPI_CHECK_RETURN_VOID(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, NOTHING) 131 #define NAPI_CHECK_RETURN_FALSE(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, false) 132 } // namespace USB 133 } // namespace OHOS 134 #endif // USB_NAPI_ERRORS_H 135