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 COMMON_H 17 #define COMMON_H 18 19 #include <list> 20 #include <memory> 21 #include <vector> 22 #include <map> 23 #include <cstring> 24 #include <mutex> 25 #include <pthread.h> 26 #include <iostream> 27 #include <unistd.h> 28 #include <signal.h> 29 #include <chrono> 30 #include <future> 31 #include <thread> 32 #include "output/video_output.h" 33 #include "surface.h" 34 #include "surface_buffer.h" 35 36 namespace OHOS { 37 namespace UserIAM { 38 namespace FaceAuth { 39 typedef enum { 40 FACE_INVALID_OPERATE_TYPE = 0, 41 FACE_OPERATE_TYPE_ENROLL, 42 FACE_OPERATE_TYPE_LOCAL_AUTH, 43 FACE_OPERATE_TYPE_CO_AUTH, 44 FACE_OPERATE_TYPE_DEL, 45 FACE_OPERATE_TYPE_MAX, 46 } FaceOperateType; 47 48 typedef struct faceReqType { 49 uint64_t reqId = 0; 50 FaceOperateType operateType = FACE_INVALID_OPERATE_TYPE; 51 bool operator<(const faceReqType &s) const 52 { 53 if (this->reqId < s.reqId) { 54 return true; 55 } 56 return false; 57 } 58 } FaceReqType; 59 typedef struct faceInfo { 60 int32_t eventId = -1; 61 int32_t uId = 0; 62 bool isCanceled = false; 63 } FaceInfo; 64 // constant 65 namespace { 66 static const int32_t FA_RET_OK = 0; 67 static const int32_t FA_RET_ERROR = -1; 68 static const int32_t FA_RET_GENERAL_ERROR = 2; 69 static const int32_t IMAGE_STREAM_EXTRA_SIZE = 10; 70 static const int32_t RESULT_MAX_SIZE = 10; 71 static const int32_t ENROLL_FACE_REC_SIZE = 4; 72 static const int32_t UNUSED_PARAM_SIZE = 4; 73 static const int32_t AUTH_FAIL_MAX_TIMES = 5; 74 static const int32_t OPEN_CAMERA_TIME_OUT = 11000000; 75 static const int32_t INIT_DYNAMIC_TIME_OUT = 5000000; 76 static const int32_t RELEASE_DYNAMIC_TIME_OUT = 5000000; 77 static const int32_t GET_RESULT_TIME_OUT = 30000000; 78 static const int32_t AUTH_FAIL_WAIT_TIME = 30; 79 static const int32_t ALO_GETRESULT_PARAM_LEN = 10; 80 static const int32_t ALO_GETRESULT_PARAM_CAR_FAIL_REASON = 1; 81 static const uint32_t ENROLL_ANGLE_MAX_NUM = 9; 82 static const int32_t CO_AUTH_MAX_NUM = 3; 83 static const int32_t PARAM_NUM = 10; 84 constexpr static double MAX_ELAPSED = 2; 85 static const int32_t USER_ID_DEFAULT = 0; 86 static const int32_t TOKEN_NUM = 69; 87 static const int32_t PREVIEW_DEFAULT_WIDTH = 640; 88 static const int32_t PREVIEW_DEFAULT_HEIGHT = 480; 89 static const int32_t FACE_AUTH_THREAD_NUM = 3; 90 static const int64_t SEC_TO_NANOSEC = 1000000000; 91 // executor register or not 92 static const int32_t EXECUTOR_REGISTER = 0; 93 static const int32_t EXECUTOR_NOT_REGISTER = 1; 94 // command from Co-Auth(OnBeginExecute) 95 static const int32_t FACE_COMMAND_ENROLL = 0; 96 static const int32_t FACE_COMMAND_AUTH = 1; 97 static const int32_t FACE_COMMAND_CANCEL_ENROLL = 0; 98 static const int32_t FACE_COMMAND_CANCEL_AUTH = 1; 99 // command from Co-Auth(OnSetProperty) 100 static const int32_t FACE_COMMAND_REMOVE = 0; 101 static const int32_t FACE_COMMAND_QUERY_CREDENTIAL = 1; 102 static const int32_t FACE_COMMAND_PROPERMODE_FREEZE = 3; 103 static const int32_t FACE_COMMAND_PROPERMODE_UNFREEZE = 4; 104 static const int32_t FACE_COMMAND_INIT_ALGORITHM = 5; 105 static const int32_t FACE_COMMAND_RELEASE_ALGORITHM = 6; 106 // command from Co-Auth(OnGetProperty) 107 static const int32_t FACE_COMMAND_GET = 1; 108 // algorithm state from FaceAuth CA 109 static const int32_t FACE_ALGORITHM_OPERATION_CONTINUE = 0; 110 static const int32_t FACE_ALGORITHM_OPERATION_BREAK = 1; 111 } 112 typedef struct { 113 uint64_t scheduleID = 0; 114 uint64_t templateID = 0; 115 uint64_t callerUID = 0; 116 uint32_t eventID = 0; 117 }AuthParam; 118 119 typedef struct { 120 uint64_t scheduleID = 0; 121 uint64_t templateID = 0; 122 uint64_t callerUID = 0; 123 uint32_t eventID = 0; 124 sptr<IBufferProducer> producer = nullptr; 125 }EnrollParam; 126 127 typedef struct { 128 uint64_t scheduleID = 0; 129 uint64_t templateID = 0; 130 uint64_t callerUID = 0; 131 }RemoveParam; 132 133 typedef struct callBackParam { 134 uint64_t reqId = 0; 135 int32_t code = 0; 136 int32_t errorCode = 0; 137 int32_t flags = 0; 138 std::vector<uint8_t> authtoken; 139 } CallBackParam; 140 141 typedef enum FIRetCode { 142 FI_RC_OK = 0, 143 FI_RC_ERROR, 144 FI_RC_INVALID_ARGUMENT, 145 FI_RC_INVALID_HANDLE, 146 FI_RC_NULLPOINTER, 147 FI_RC_OUTORMEMORY, 148 } FIRetCode; 149 150 typedef struct PrepareParam { 151 bool isNeedGaze; // Need to look at the camera 152 bool isMaskMode; // Mask mode 153 int32_t authProtocol; // Authentication protocol 154 int32_t faceId; // Face ID 155 int32_t enrollFaceRec[ENROLL_FACE_REC_SIZE]; 156 int32_t sensorType; // Camera type 157 int32_t skipEyeOpenCheck; // Skip eye opening check 158 int32_t unusedParam[UNUSED_PARAM_SIZE]; // Reserved parameters 159 } PrepareParam; 160 161 typedef enum HWExeType { 162 HW_EXEC_TYPE_ENROOL = 0, 163 HW_EXEC_TYPE_UNLOCK, 164 HW_EXEC_TYPE_PAY, 165 HW_EXEC_TYPE_LEARN, 166 } HWExeType; 167 168 typedef enum FIResultCode { 169 FI_NONE = 0, 170 FI_FACE_INFO, 171 FI_FACE_NOT_FOUND, 172 FI_FACE_SCALE_TOO_SMALL, 173 FI_FACE_SCALE_TOO_LARGE, 174 FI_FACE_OFFSET_LEFT, 175 FI_FACE_OFFSET_TOP, 176 FI_FACE_OFFSET_RIGHT, 177 FI_FACE_OFFSET_BOTTOM, 178 FI_NOT_GAZE, 179 FI_ENROLL_FACE_ANGLE_OK, 180 FI_ENROLL_NOT_THE_SAME_FACE, 181 FI_ENROLL_TOO_MANY_FACE, 182 FI_FACE_TOO_DARK, 183 FI_FACE_FACE_SHADE, 184 FI_ENROLL_SUCCESS, 185 FI_ENROLL_FAIL, 186 FI_COMPARE_SUCCESS, 187 FI_COMPARE_FAIL, 188 FI_MSG_TO_DEPTH, 189 FI_FRAME_COMPARE_FAIL, 190 FI_FRAME_LIVENESS_FAIL, 191 FI_FACE_OUT_OF_BOUNDS, 192 FI_FACE_ROTATE_LEFT, 193 FI_FACE_ROTATE_TOP, 194 FI_FACE_ROTATE_RIGHT, 195 FI_FACE_ROTATE_BOTTOM, 196 FI_FACE_ROTATE_TOP_RIGHT, 197 FI_FACE_ROTATE_TOP_LEFT, 198 FI_FACE_ROTATE_BOTTOM_RIGHT, 199 FI_FACE_ROTATE_BOTTOM_LEFT, 200 FI_FACE_DARKPIC, 201 FI_FACE_DETECTED = 38, 202 } FIResultCode; 203 204 typedef enum RESULT_ERRORCODE { 205 FACE_NONE = -1, 206 ERRCODE_SUCCESS = 0, 207 ERRCODE_FAIL, 208 ERRCODE_CANCEL, 209 ERRCODE_COMPARE_FAIL, 210 ERRCODE_TIMEOUT, 211 ERRCODE_ALGORITHM_NOT_INIT, 212 ERRCODE_HAL_INVALIDE, 213 ERRCODE_OVER_MAX_FACES, 214 ERRCODE_IN_LOCKOUT_MODE, 215 ERRCODE_INVALID_PARAMETERS, 216 ERRCODE_NO_FACE_DATA, 217 ERRCODE_NOT_GAZE, 218 ERRCODE_CAMERA_FAIL, 219 ERRCODE_BUSY, 220 ERRCODE_ENROLL_HAS_REGISTERED = 16, 221 ERRCODE_COMPARE_OK_NO_TRUSTED_THINGS, 222 } CALLBACK_RESULT_ERRORCODE; 223 224 typedef enum AUTH_CODE_CALLBACK_ACQUIRE { 225 FACE_BAD_QUALITY = 4, 226 FACE_NOT_FOUND, 227 FACE_SCALE_TOO_SMALL, 228 FACE_SCALE_TOO_LARGE, 229 FACE_OFFSET_LEFT, 230 FACE_OFFSET_TOP, 231 FACE_OFFSET_RIGHT, 232 FACE_OFFSET_BOTTOM, 233 FACE_LIVENESS_WARNING = 13, 234 FACE_LIVENESS_FAILURE, 235 FACE_ROTATED_LEFT, 236 FACE_RISE, 237 FACE_ROTATED_RIGHT, 238 FACE_DOWN, 239 FACE_EYE_OCCLUSION = 21, 240 FACE_EYE_CLOSE, 241 FACE_EYE_MOUTH_OCCLUSION, 242 FACE_MULTI = 27, 243 FACE_BLUR, 244 FACE_NOT_COMPLETE = 29, 245 FACE_UNLOCK_FACE_DARKLIGHT, 246 FACE_UNLOCK_FACE_HIGHTLIGHT, 247 FACE_UNLOCK_FACE_HALF_SHADOW, 248 FACE_MOVED, 249 FACE_OUT_OF_BOUNDS, 250 FACE_NOT_GAZE = 36, 251 FACE_HAS_REGISTERED, 252 FACE_DETECTED, 253 FACE_DARKPIC, 254 FACE_ROTATE_TOP_RIGHT, 255 FACE_ROTATE_TOP_LEFT, 256 FACE_ROTATE_BOTTOM_RIGHT, 257 FACE_ROTATE_BOTTOM_LEFT, 258 FACE_WITHOUT_MASK, 259 FACE_COVERED_WITH_MASK, 260 FACE_OPEN_CAMERA_FAILED, 261 FACE_ANGLE_BASE = 1000, 262 FACE_ENROLL_INFO_BEGIN = 2000, 263 } AUTH_CODE_CALLBACK_ACQUIRE; 264 265 typedef enum FIResultType { 266 TYPE_CALLBACK_ENROLL, 267 TYPE_CALLBACK_AUTH, 268 TYPE_CALLBACK_REMOVE, 269 } FIResultType; 270 271 typedef enum FICode { 272 CODE_CALLBACK_START = 0, 273 CODE_CALLBACK_RESULT, 274 CODE_CALLBACK_CANCEL, 275 CODE_CALLBACK_ACQUIRE, 276 CODE_CALLBACK_BUSY, 277 CODE_CALLBACK_FACEID = 6, 278 CODE_CALLBACK_MAX, 279 } FICode; 280 281 typedef enum MGULKStatus { 282 MG_UNLOCK_OK = 0x0000, // <The interface status is normal 283 MG_UNLOCK_INVALID_ARGUMENT, // <Wrong parameter passed in 284 MG_UNLOCK_INVALID_HANDLE, // <The handle object passed in is illegal 285 MG_UNLOCK_FAILURE, // <Other errors, such as improper calling process or expiration 286 // compare and save_feature, The following are returned 287 MG_UNLOCK_FACE_BAD_QUALITY = 0x1000, // <Poor picture quality 288 MG_UNLOCK_FACE_NOT_FOUND, // <No found face 289 MG_UNLOCK_FACE_SCALE_TOO_SMALL, // <The face is too small 290 MG_UNLOCK_FACE_SCALE_TOO_LARGE, // <Face too big 291 MG_UNLOCK_FACE_OFFSET_LEFT, // <Face left 292 MG_UNLOCK_FACE_OFFSET_TOP, // <Face up 293 MG_UNLOCK_FACE_OFFSET_RIGHT, // <Face right 294 MG_UNLOCK_FACE_OFFSET_BOTTOM, // <Face down 295 MG_UNLOCK_ATTR_BLUR, // <Face blur 296 MG_UNLOCK_ATTR_EYE_CLOSE, // <Eyes close 297 MG_UNLOCK_FACE_NOT_COMPLETE, // <Incomplete face 298 MG_UNLOCK_LIVENESS_FAILURE, // <When a possible live attack is detected or the comparison fails, the external 299 // should 300 // continue to call this interface to pass in the picture data, and the SDK will judge 301 // whether it is really a live attack according to the continuous frames 302 MG_UNLOCK_KEEP, // <It is not possible to judge whether it is successful, so it is necessary to continue the 303 // detection 304 MG_UNLOCK_DARKLIGHT, // <Too dark 305 // Only save The value returned by the feature 306 MG_UNLOCK_ATTR_EYE_OCCLUSION = 0x2000, // <Eye occlusion 307 MG_UNLOCK_ATTR_MOUTH_OCCLUSION, // Mouth occlusion (compare will return from 2.0.39.1061) 308 MG_UNLOCK_FACE_ROTATED_LEFT, // <Turn left 309 MG_UNLOCK_FACE_RISE, // <rise 310 MG_UNLOCK_FACE_ROTATED_RIGHT, // <Turn right 311 MG_UNLOCK_FACE_DOWN, // <Bow your head 312 MG_UNLOCK_FACE_MULTI, // <Multiple faces 313 MG_UNLOCK_FACE_BLUR, // <Face blur 314 MG_UNLOCK_HIGHLIGHT, // <Too bright 315 MG_UNLOCK_HALF_SHADOW, // <Half shadow 316 // Only compare The value returned by the feature 317 MG_UNLOCK_COMPARE_FAILURE = 0x3000, // <Face comparison failed (not the same person) 318 // The algorithm has been upgraded and restore needs to be called_ Feature re extracts the bottom library feature 319 MG_UNLOCK_NEED_RESTORE_FEATURE, 320 MG_UNLOCK_CONTINUE, // If there are not enough 5 base libraries, you have to continue to enter the base library 321 MG_UNLOCK_MAX, 322 } MGULKStatus; 323 324 typedef enum { 325 AR_SUCCESS, 326 AR_FAIL, 327 AR_EMPTY, 328 AR_NOT_FOUND, 329 AR_ADD_AGAIN, 330 } AlgoResult; 331 332 typedef enum FIFailReason { 333 FI_FAIL_REASON_FACE_NOT_MATCH = 0, 334 FI_FAIL_REASON_FACE_MOVED = 1, 335 FI_FAIL_REASON_FACE_OUT_OF_BOUNDS = 2, 336 FI_FAIL_REASON_FACE_NOT_FOUND = 3, 337 FI_FAIL_REASON_NOT_GAZE = 4, 338 FI_FAIL_REASON_LIVENESS_FAILURE = 5, 339 FI_FAIL_REASON_TIME_OUT = 6, 340 FI_FAIL_REASON_NEED_UPGRADE = 7, 341 } FIFailReason; 342 343 typedef enum FaceErrorCode { 344 FACE_SUCCESS = 0, /* Used for init, enroll, authenticate, remove success */ 345 FACE_ERROR_FAIL, /* Used for init, enroll, authenticate, remove fail */ 346 FACE_ERROR_CANCEL, /* Operation was canceled */ 347 FACE_ERROR_COMPARE_FAIL, /* Compare fail, only for authenticate **/ 348 FACE_ERROR_TIMEOUT, /* timeout */ 349 FACE_ERROR_NOT_INIT, 350 FACE_SUCCESS_NEED_UPGRADE = 14, 351 FACE_SUCCESS_NO_NEED_UPGRADE = 15, 352 FACE_ENROLL_HAS_REGISTERED = 16, /* For car only */ 353 } FaceErrorCode; 354 355 typedef enum ResultCodeForCoAuth { 356 /** 357 * Indicates that authentication is success or ability is supported. 358 */ 359 SUCCESS = 0, 360 361 /** 362 * Indicates the authenticator fails to identify user. 363 */ 364 FAIL = 1, 365 366 /** 367 * Indicates other errors. 368 */ 369 GENERAL_ERROR = 2, 370 371 /** 372 * Indicates that authentication has been canceled. 373 */ 374 CANCELED = 3, 375 376 /** 377 * Indicates that authentication has timed out. 378 */ 379 TIMEOUT = 4, 380 381 /** 382 * Indicates that this authentication type is not supported. 383 */ 384 TYPE_NOT_SUPPORT = 5, 385 386 /** 387 * Indicates that the authentication trust level is not supported. 388 */ 389 TRUST_LEVEL_NOT_SUPPORT = 6, 390 391 /** 392 * Indicates that the authentication task is busy. Wait for a few seconds and try again. 393 */ 394 BUSY = 7, 395 396 /** 397 * Indicates incorrect parameters. 398 */ 399 INVALID_PARAMETERS = 8, 400 401 /** 402 * Indicates that the authenticator is locked. 403 */ 404 LOCKED = 9, 405 406 /** 407 * Indicates that the user has not enrolled the authenticator. 408 */ 409 NOT_ENROLLED = 10 410 } ResultCodeForCoAuth; 411 } // namespace FaceAuth 412 } // namespace UserIAM 413 } // namespace OHOS 414 #endif // COMMON_H