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 
30     UEC_COMMON_BASE = 14400000,
31     UEC_COMMON_HAS_NO_RIGHT = UEC_COMMON_BASE + 1,
32     UEC_COMMON_HDC_NOT_ALLOWED = UEC_COMMON_BASE + 2,
33     UEC_COMMON_PORTROLE_SWITCH_NOT_ALLOWED = UEC_COMMON_BASE + 3,
34     UEC_COMMON_SERVICE_EXCEPTION = UEC_COMMON_BASE + 4,
35     UEC_COMMON_RIGHT_DATABASE_ERROR = UEC_COMMON_BASE + 5,
36     UEC_COMMON_FUNCTION_NOT_SUPPORT = UEC_COMMON_BASE + 6,
37 
38     UEC_ACCESSORY_BASE = UEC_COMMON_BASE + 1000,
39     UEC_ACCESSORY_NOT_MATCH = UEC_ACCESSORY_BASE + 1,
40     UEC_ACCESSORY_OPEN_FAILED = UEC_ACCESSORY_BASE + 2,
41     UEC_ACCESSORY_CAN_NOT_REOPEN = UEC_ACCESSORY_BASE + 3,
42 };
43 
44 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = {
45     {OHEC_COMMON_PERMISSION_NOT_ALLOWED, "BusinessError 201:The permission check failed."},
46     {OHEC_COMMON_NORMAL_APP_NOT_ALLOWED, "BusinessError 202:Permission denied. Normal application uses system api."   },
47     {OHEC_COMMON_PARAM_ERROR,       "BusinessError 401:Parameter error."                                         },
48     {UEC_COMMON_HAS_NO_RIGHT, "BusinessError 14400001:Permission denied."                                  },
49     {UEC_COMMON_HDC_NOT_ALLOWED,    "BusinessError 14400002:Permission denied. The HDC is disabled by the system."},
50     {UEC_COMMON_PORTROLE_SWITCH_NOT_ALLOWED,
51      "BusinessError 14400003:Unsupported operation.The current device does not support port role switching."    },
52     {UEC_COMMON_SERVICE_EXCEPTION,
53      "BusinessError 14400004:Service exception. Possible causes:No accessory is plugged in."},
54     {UEC_COMMON_RIGHT_DATABASE_ERROR,    "BusinessError 14400005:Database operation exception."},
55     {UEC_COMMON_FUNCTION_NOT_SUPPORT,
56         "BusinessError 14400006:Unsupported operation. The function is not supported."},
57     {UEC_ACCESSORY_NOT_MATCH,    "BusinessError 14401001:The target USBAccessory not matched."},
58     {UEC_ACCESSORY_OPEN_FAILED, "BusinessError 14401002:Failed to open the native accessory node."},
59     {UEC_ACCESSORY_CAN_NOT_REOPEN,    "BusinessError 14401003:Cannot reopen the accessory."},
60 };
61 
62 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
63 napi_value CreateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
64 
65 #define USB_ASSERT_BASE(env, assertion, errCode, errMsg, retVal) \
66     do {                                                         \
67         if (!(assertion)) {                                      \
68             USB_HILOGE(MODULE_JS_NAPI, #errMsg);                 \
69             ThrowBusinessError((env), errCode, errMsg);          \
70             return retVal;                                       \
71         }                                                        \
72     } while (0)
73 
74 #define NOTHING
75 #define USB_ASSERT(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, nullptr)
76 #define USB_ASSERT_RETURN_VOID(env, assertion, errCode, errMsg) \
77     USB_ASSERT_BASE(env, assertion, errCode, errMsg, NOTHING)
78 #define USB_ASSERT_RETURN_FALSE(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, false)
79 #define USB_ASSERT_RETURN_UNDEF(env, assertion, errCode, errMsg) \
80     do {                                                         \
81         napi_value obj = nullptr;                                \
82         napi_get_undefined(env, &obj);                           \
83         USB_ASSERT_BASE(env, assertion, errCode, errMsg, obj);   \
84     } while (0)
85 
86 #define NAPI_CHECK(env, theCall, loginfo)                                     \
87     do {                                                                      \
88         if ((theCall) != napi_ok) {                                           \
89             USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \
90             napi_value obj = nullptr;                                         \
91             napi_get_undefined(env, &obj);                                    \
92             return obj;                                                       \
93         }                                                                     \
94     } while (0)
95 
96 #define NAPI_CHECK_BASE(theCall, loginfo, retVal)                             \
97     do {                                                                      \
98         if ((theCall) != napi_ok) {                                           \
99             USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \
100             return retVal;                                                    \
101         }                                                                     \
102     } while (0)
103 
104 #define NAPI_CHECK_RETURN_VOID(theCall, loginfo)  NAPI_CHECK_BASE(theCall, loginfo, NOTHING)
105 #define NAPI_CHECK_RETURN_FALSE(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, false)
106 } // namespace USB
107 } // namespace OHOS
108 #endif // USB_NAPI_ERRORS_H
109