• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
51 
52 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = {
53     {OHEC_COMMON_PERMISSION_NOT_ALLOWED,
54         "BusinessError 201:Permission verification failed. "
55         "The application does not have the permission required to call the API."},
56     {OHEC_COMMON_NORMAL_APP_NOT_ALLOWED, "BusinessError 202:Permission denied. Normal application uses system api."   },
57     {OHEC_COMMON_PARAM_ERROR,       "BusinessError 401:Parameter error."                                         },
58     {CAPABILITY_NOT_SUPPORT, "BusinessError 801:Capability not supported."},
59     {UEC_COMMON_HAS_NO_RIGHT, "BusinessError 14400001:Permission denied."                                  },
60     {UEC_COMMON_HDC_NOT_ALLOWED,    "BusinessError 14400002:Permission denied. The HDC is disabled by the system."},
61     {UEC_COMMON_PORTROLE_SWITCH_NOT_ALLOWED,
62      "BusinessError 14400003:Unsupported operation.The current device does not support port role switching."    },
63     {UEC_COMMON_SERVICE_EXCEPTION,
64      "BusinessError 14400004:Service exception. Possible causes:No accessory is plugged in."},
65     {UEC_COMMON_RIGHT_DATABASE_ERROR,    "BusinessError 14400005:Database operation exception."},
66     {UEC_COMMON_FUNCTION_NOT_SUPPORT,
67         "BusinessError 14400006:Unsupported operation. The function is not supported."},
68     {UEC_ACCESSORY_NOT_MATCH,    "BusinessError 14401001:The target USBAccessory not matched."},
69     {UEC_ACCESSORY_OPEN_FAILED, "BusinessError 14401002:Failed to open the native accessory node."},
70     {UEC_ACCESSORY_CAN_NOT_REOPEN,    "BusinessError 14401003:Cannot reopen the accessory."},
71 
72     {USB_SUBMIT_TRANSFER_RESOURCE_BUSY_ERROR, "BusinessError 14400007:Resource busy. Possible causes: \
73         1. The transfer has already been submitted. 2. The interface is claimed by another program or driver."},
74     {USB_SUBMIT_TRANSFER_NO_DEVICE_ERROR, "BusinessError 14400008:No such device (it may have been disconnected)."},
75     {USB_SUBMIT_TRANSFER_NO_MEM_ERROR,
76         "BusinessError 14400009:Insufficient memory. Possible causes: 1. Malloc memory failed."},
77     {USB_SUBMIT_TRANSFER_OTHER_ERROR, "BusinessError 14400010:Other USB error."},
78     {USB_SUBMIT_TRANSFER_NOT_FOUND_ERROR,
79         "BusinessError 14400011:The transfer is not in progress, or is already complete or cancelled."},
80     {USB_SUBMIT_TRANSFER_IO_ERROR, "BusinessError 14400012:Transmission I/O error."},
81 };
82 
83 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
84 napi_value CreateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
85 
86 #define USB_ASSERT_BASE(env, assertion, errCode, errMsg, retVal) \
87     do {                                                         \
88         if (!(assertion)) {                                      \
89             USB_HILOGE(MODULE_JS_NAPI, #errMsg);                 \
90             ThrowBusinessError((env), errCode, errMsg);          \
91             return retVal;                                       \
92         }                                                        \
93     } while (0)
94 
95 #define NOTHING
96 #define USB_ASSERT(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, nullptr)
97 #define USB_ASSERT_RETURN_VOID(env, assertion, errCode, errMsg) \
98     USB_ASSERT_BASE(env, assertion, errCode, errMsg, NOTHING)
99 #define USB_ASSERT_RETURN_FALSE(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, false)
100 #define USB_ASSERT_RETURN_UNDEF(env, assertion, errCode, errMsg) \
101     do {                                                         \
102         napi_value obj = nullptr;                                \
103         napi_get_undefined(env, &obj);                           \
104         USB_ASSERT_BASE(env, assertion, errCode, errMsg, obj);   \
105     } while (0)
106 
107 #define NAPI_CHECK(env, theCall, loginfo)                                     \
108     do {                                                                      \
109         if ((theCall) != napi_ok) {                                           \
110             USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \
111             napi_value obj = nullptr;                                         \
112             napi_get_undefined(env, &obj);                                    \
113             return obj;                                                       \
114         }                                                                     \
115     } while (0)
116 
117 #define NAPI_CHECK_BASE(theCall, loginfo, retVal)                             \
118     do {                                                                      \
119         if ((theCall) != napi_ok) {                                           \
120             USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \
121             return retVal;                                                    \
122         }                                                                     \
123     } while (0)
124 
125 #define NAPI_CHECK_RETURN_VOID(theCall, loginfo)  NAPI_CHECK_BASE(theCall, loginfo, NOTHING)
126 #define NAPI_CHECK_RETURN_FALSE(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, false)
127 } // namespace USB
128 } // namespace OHOS
129 #endif // USB_NAPI_ERRORS_H
130