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 SCAN_CONSTANT_H
17 #define SCAN_CONSTANT_H
18 
19 #include <string>
20 
21 namespace OHOS::Scan {
22 
23 #define SCAN_RET_NONE
24 #define SCAN_MAX_COUNT 1000
25 
26 #define SCAN_ASSERT_BASE(env, assertion, message, retVal)  \
27     do {                                                    \
28         if (!(assertion)) {                                 \
29             SCAN_HILOGE(message);                          \
30             return retVal;                                  \
31         }                                                   \
32     } while (0)
33 
34 #define SCAN_ASSERT(env, assertion, message) SCAN_ASSERT_BASE(env, assertion, message, nullptr)
35 
36 #define SCAN_ASSERT_RETURN_VOID(env, assertion, message) SCAN_ASSERT_BASE(env, assertion, message, SCAN_RET_NONE)
37 
38 #define SCAN_CALL_BASE(env, theCall, retVal)   \
39     do {                                        \
40         if ((theCall) != napi_ok) {             \
41             return retVal;                      \
42         }                                       \
43     } while (0)
44 
45 #define SCAN_CALL(env, theCall) SCAN_CALL_BASE(env, theCall, nullptr)
46 
47 #define SCAN_CALL_RETURN_VOID(env, theCall) SCAN_CALL_BASE(env, theCall, SCAN_RET_NONE)
48 
49 #define DELETE_AND_NULLIFY(ptr) \
50     if ((ptr) != nullptr) { \
51         delete (ptr); \
52         (ptr) = nullptr; \
53     }
54 
55 #define DELETE_ARRAY_AND_NULLIFY(ptr) \
56     if ((ptr) != nullptr) { \
57         delete[] (ptr); \
58         (ptr) = nullptr; \
59     }
60 
61 #define FREE_AND_NULLPTR(ptr) \
62     if ((ptr) != nullptr) { \
63         free (ptr); \
64         (ptr) = nullptr; \
65     }
66 
67 #define INIT_CALLBACK_PARAMS \
68         uv_loop_s *loop = nullptr; \
69         bool flag = true; \
70         napi_get_uv_event_loop(env_, &loop); \
71         CHECK_AND_CREATE(loop, "Failed to get uv event loop", flag); \
72         uv_work_t *work = nullptr; \
73         CallbackParam *param = nullptr; \
74         CallbackContext *context = nullptr; \
75         CreateCallbackParam(work, param, context, flag) \
76 
77 #define CHECK_AND_CREATE(pointer, error_message, flag) \
78     if ((pointer) == nullptr) { \
79         SCAN_HILOGE(error_message); \
80         (flag) = false; \
81     }
82 
83 #define CREATE_PRC_MESSAGE \
84     MessageParcel data; \
85     MessageParcel reply; \
86     MessageOption option; \
87     data.WriteInterfaceToken(GetDescriptor()) \
88 
89 #define CHECK_IS_EXCEED_SCAN_RANGE_BASE(count, retVal)                                             \
90     do {                                                                                            \
91         if ((count) > SCAN_MAX_COUNT) {                                                      \
92             SCAN_HILOGW("input val is exceed scan max range:%{public}d", SCAN_MAX_COUNT);  \
93             return retVal;                                                                          \
94         }                                                                                           \
95     } while (0)
96 
97 #define CHECK_IS_EXCEED_SCAN_RANGE(count)          CHECK_IS_EXCEED_SCAN_RANGE_BASE(count, nullptr)
98 #define CHECK_IS_EXCEED_SCAN_RANGE_BOOL(count)     CHECK_IS_EXCEED_SCAN_RANGE_BASE(count, false)
99 #define CHECK_IS_EXCEED_SCAN_RANGE_VOID(count)     CHECK_IS_EXCEED_SCAN_RANGE_BASE(count, E_SCAN_NONE)
100 #define CHECK_IS_EXCEED_SCAN_RANGE_INT(count)      CHECK_IS_EXCEED_SCAN_RANGE_BASE(count, E_SCAN_INVALID_PARAMETER)
101 
102 enum ScanErrorCode {
103     // FWK ERROR
104     E_SCAN_NONE = 0, // no error
105     E_SCAN_NO_PERMISSION = 201, // no permission
106     E_SCAN_INVALID_PARAMETER = 401, // invalid parameter
107     E_SCAN_GENERIC_FAILURE = 13100001, // generic failure of scan
108     E_SCAN_RPC_FAILURE = 13100002, // RPC failure
109     E_SCAN_SERVER_FAILURE = 13100003, // failure of scan service
110 
111     // DEVICE ERROR
112     E_SCAN_GOOD = 13200000,  /* everything A-OK */
113     E_SCAN_UNSUPPORTED = 13200001,  /* operation is not supported */
114     E_SCAN_CANCELLED = 13200002,  /* operation was cancelled */
115     E_SCAN_DEVICE_BUSY = 13200003,  /* device is busy; try again later */
116     E_SCAN_INVAL = 13200004,  /* data is invalid (includes no dev at open) */
117     E_SCAN_EOF = 13200005,  /* no more data available (end-of-file) */
118     E_SCAN_JAMMED = 13200006,  /* document feeder jammed */
119     E_SCAN_NO_DOCS = 13200007,  /* document feeder out of documents */
120     E_SCAN_COVER_OPEN = 13200008,  /* scanner cover is open */
121     E_SCAN_IO_ERROR = 13200009,  /* error during device I/O */
122     E_SCAN_NO_MEM = 13200010,  /* out of memory */
123     E_SCAN_ACCESS_DENIED = 13200011,  /* access to resource has been denied */
124 };
125 
126 const uint32_t SCAN_INVALID_ID = 0xFFFFFFFF;   // -1
127 const uint16_t USB_VALUE_DESCRIPTOR_INDEX_SERIAL_NUMBER = 0X03;
128 const uint8_t USB_REQUESTTYPE_DEVICE_TO_HOST = 0X80;
129 const uint8_t USB_REQUEST_GET_DESCRIPTOR = 0X06;
130 const uint16_t USB_VALUE_DESCRIPTOR_TYPE_STRING = 0X03;
131 const int HTTP_COMMON_CONST_VALUE_8 = 8;
132 const uint16_t USB_INDEX_LANGUAGE_ID_ENGLISH = 0X409;
133 const int HTTP_COMMON_CONST_VALUE_500 = 500;
134 const int HTTP_COMMON_CONST_VALUE_100 = 100;
135 const int HTTP_COMMON_CONST_VALUE_2 = 2;
136 const int USB_DEVICEID_FIRSTID_LEN_3 = 3;
137 
138 enum ScanFrame  {
139     SCAN_FRAME_GRAY = 0,  /* band covering human visual range */
140     SCAN_FRAME_RGB = 1,  /* pixel-interleaved red/green/blue bands */
141     SCAN_FRAME_RED = 2,  /* red band only */
142     SCAN_FRAME_GREEN = 3,  /* green band only */
143     SCAN_FRAME_BLUE = 4,  /* blue band only */
144 };
145 
146 enum ScanExtensionState {
147     SCAN_EXTENSION_UNLOAD,
148     SCAN_EXTENSION_LOADING,
149     SCAN_EXTENSION_LOADED,
150 };
151 
152 enum ScanParamStatus {
153     SCAN_PARAM_NOT_SET,
154     SCAN_PARAM_OPT,
155     SCAN_PARAM_SET,
156 };
157 
158 enum ScanOptionOpType {
159     SCAN_ACTION_GET_VALUE = 0,
160     SCAN_ACTION_SET_VALUE,
161     SCAN_ACTION_SET_AUTO
162 };
163 
164 enum ScanOptionValueType {
165     SCAN_VALUE_NONE,
166     SCAN_VALUE_NUM,
167     SCAN_VALUE_NUM_LIST,
168     SCAN_VALUE_STR,
169     SCAN_VALUE_BOOL,
170 };
171 
172 enum ScanConstraintType {
173     SCAN_CONSTRAINT_NONE = 0,
174     SCAN_CONSTRAINT_RANGE = 1,
175     SCAN_CONSTRAINT_WORD_LIST = 2,
176     SCAN_CONSTRAINT_STRING_LIST = 3,
177 };
178 
179 enum ScannerState {
180     SCANNER_READY = 0,
181     SCANNER_SCANING = 1,
182     SCANNER_SEARCHING = 2,
183     SCANNER_CANCELING = 3,
184 };
185 } // namespace OHOS::Scan
186 #endif // SCAN_CONSTANT_H
187