diff --git a/common/include/face_auth_defines.h b/common/include/face_auth_defines.h index a11a5a9..85874cc 100644 --- a/common/include/face_auth_defines.h +++ b/common/include/face_auth_defines.h @@ -16,399 +16,77 @@ #ifndef COMMON_H #define COMMON_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "output/video_output.h" -#include "surface.h" -#include "surface_buffer.h" - namespace OHOS { namespace UserIAM { namespace FaceAuth { -typedef enum { - FACE_INVALID_OPERATE_TYPE = 0, - FACE_OPERATE_TYPE_ENROLL, - FACE_OPERATE_TYPE_LOCAL_AUTH, - FACE_OPERATE_TYPE_CO_AUTH, - FACE_OPERATE_TYPE_DEL, - FACE_OPERATE_TYPE_MAX, -} FaceOperateType; +#include -typedef struct faceReqType { - uint64_t reqId = 0; - FaceOperateType operateType = FACE_INVALID_OPERATE_TYPE; - bool operator<(const faceReqType &s) const - { - if (this->reqId < s.reqId) { - return true; - } - return false; - } -} FaceReqType; -typedef struct faceInfo { - int32_t eventId = -1; - int32_t uId = 0; - bool isCanceled = false; -} FaceInfo; // constant namespace { static const int32_t FA_RET_OK = 0; static const int32_t FA_RET_ERROR = -1; -static const int32_t FA_RET_GENERAL_ERROR = 2; -static const int32_t IMAGE_STREAM_EXTRA_SIZE = 10; -static const int32_t RESULT_MAX_SIZE = 10; -static const int32_t ENROLL_FACE_REC_SIZE = 4; -static const int32_t UNUSED_PARAM_SIZE = 4; -static const int32_t AUTH_FAIL_MAX_TIMES = 5; -static const int32_t OPEN_CAMERA_TIME_OUT = 11000000; -static const int32_t INIT_DYNAMIC_TIME_OUT = 5000000; -static const int32_t RELEASE_DYNAMIC_TIME_OUT = 5000000; -static const int32_t GET_RESULT_TIME_LIMIT_IN_SEC = 30; -static const int32_t AUTH_FAIL_WAIT_TIME = 30; -static const int32_t ALO_GETRESULT_PARAM_LEN = 10; -static const int32_t ALO_GETRESULT_PARAM_CAR_FAIL_REASON = 1; -static const uint32_t ENROLL_ANGLE_MAX_NUM = 9; -static const int32_t CO_AUTH_MAX_NUM = 3; -static const int32_t PARAM_NUM = 10; -constexpr static double MAX_ELAPSED = 2; -static const int32_t USER_ID_DEFAULT = 0; -static const int32_t TOKEN_NUM = 69; -static const int32_t PREVIEW_DEFAULT_WIDTH = 640; -static const int32_t PREVIEW_DEFAULT_HEIGHT = 480; -static const int32_t FACE_AUTH_THREAD_NUM = 3; -static const int64_t SEC_TO_NANOSEC = 1000000000; -// executor register or not -static const int32_t EXECUTOR_REGISTER = 0; -static const int32_t EXECUTOR_NOT_REGISTER = 1; -// command from Co-Auth(OnBeginExecute) -static const int32_t FACE_COMMAND_ENROLL = 0; -static const int32_t FACE_COMMAND_AUTH = 1; -static const int32_t FACE_COMMAND_CANCEL_ENROLL = 0; -static const int32_t FACE_COMMAND_CANCEL_AUTH = 1; -// command from Co-Auth(OnSetProperty) -static const int32_t FACE_COMMAND_REMOVE = 0; -static const int32_t FACE_COMMAND_QUERY_CREDENTIAL = 1; -static const int32_t FACE_COMMAND_PROPERMODE_FREEZE = 3; -static const int32_t FACE_COMMAND_PROPERMODE_UNFREEZE = 4; -static const int32_t FACE_COMMAND_INIT_ALGORITHM = 5; -static const int32_t FACE_COMMAND_RELEASE_ALGORITHM = 6; -// command from Co-Auth(OnGetProperty) -static const int32_t FACE_COMMAND_GET = 1; -// algorithm state from FaceAuth CA -static const int32_t FACE_ALGORITHM_OPERATION_CONTINUE = 0; -static const int32_t FACE_ALGORITHM_OPERATION_BREAK = 1; -} -typedef struct { - uint64_t scheduleID = 0; - uint64_t templateID = 0; - uint64_t callerUID = 0; - uint32_t eventID = 0; -} AuthParam; +} // namespace -typedef struct { - uint64_t scheduleID = 0; - uint64_t templateID = 0; - uint64_t callerUID = 0; - uint32_t eventID = 0; - sptr producer = nullptr; -} EnrollParam; +enum FaceAuthRet { + FACEAUTH_SUCCESS = 0, + FACEAUTH_ERROR = 1, +}; -typedef struct { - uint64_t scheduleID = 0; - uint64_t templateID = 0; - uint64_t callerUID = 0; -} RemoveParam; - -typedef struct callBackParam { - uint64_t reqId = 0; - int32_t code = 0; - int32_t errorCode = 0; - int32_t flags = 0; - std::vector authtoken; -} CallBackParam; - -typedef enum FIRetCode { - FI_RC_OK = 0, - FI_RC_ERROR, - FI_RC_INVALID_ARGUMENT, - FI_RC_INVALID_HANDLE, - FI_RC_NULL_POINTER, - FI_RC_OUT_OF_MEMORY, -} FIRetCode; - -typedef struct PrepareParam { - bool isNeedGaze; // Need to look at the camera - bool isMaskMode; // Mask mode - int32_t authProtocol; // Authentication protocol - int32_t faceId; // Face ID - int32_t enrollFaceRec[ENROLL_FACE_REC_SIZE]; - int32_t sensorType; // Camera type - int32_t skipEyeOpenCheck; // Skip eye opening check - int32_t unusedParam[UNUSED_PARAM_SIZE]; // Reserved parameters -} PrepareParam; - -typedef enum HWExeType { - HW_EXEC_TYPE_ENROOL = 0, - HW_EXEC_TYPE_UNLOCK, - HW_EXEC_TYPE_PAY, - HW_EXEC_TYPE_LEARN, -} HWExeType; - -typedef enum FIResultCode { - FI_NONE = 0, - FI_FACE_INFO, - FI_FACE_NOT_FOUND, - FI_FACE_SCALE_TOO_SMALL, - FI_FACE_SCALE_TOO_LARGE, - FI_FACE_OFFSET_LEFT, - FI_FACE_OFFSET_TOP, - FI_FACE_OFFSET_RIGHT, - FI_FACE_OFFSET_BOTTOM, - FI_NOT_GAZE, - FI_ENROLL_FACE_ANGLE_OK, - FI_ENROLL_NOT_THE_SAME_FACE, - FI_ENROLL_TOO_MANY_FACE, - FI_FACE_TOO_DARK, - FI_FACE_FACE_SHADE, - FI_ENROLL_SUCCESS, - FI_ENROLL_FAIL, - FI_COMPARE_SUCCESS, - FI_COMPARE_FAIL, - FI_MSG_TO_DEPTH, - FI_FRAME_COMPARE_FAIL, - FI_FRAME_LIVENESS_FAIL, - FI_FACE_OUT_OF_BOUNDS, - FI_FACE_ROTATE_LEFT, - FI_FACE_ROTATE_TOP, - FI_FACE_ROTATE_RIGHT, - FI_FACE_ROTATE_BOTTOM, - FI_FACE_ROTATE_TOP_RIGHT, - FI_FACE_ROTATE_TOP_LEFT, - FI_FACE_ROTATE_BOTTOM_RIGHT, - FI_FACE_ROTATE_BOTTOM_LEFT, - FI_FACE_DARKPIC, - FI_FACE_DETECTED = 38, -} FIResultCode; - -typedef enum RESULT_ERRORCODE { - FACE_NONE = -1, - ERRCODE_SUCCESS = 0, - ERRCODE_FAIL, - ERRCODE_CANCEL, - ERRCODE_COMPARE_FAIL, - ERRCODE_TIMEOUT, - ERRCODE_ALGORITHM_NOT_INIT, - ERRCODE_HAL_INVALID, - ERRCODE_OVER_MAX_FACES, - ERRCODE_IN_LOCKOUT_MODE, - ERRCODE_INVALID_PARAMETERS, - ERRCODE_NO_FACE_DATA, - ERRCODE_NOT_GAZE, - ERRCODE_CAMERA_FAIL, - ERRCODE_BUSY, - ERRCODE_ENROLL_HAS_REGISTERED = 16, - ERRCODE_COMPARE_OK_NO_TRUSTED_THINGS, -} CALLBACK_RESULT_ERRORCODE; - -typedef enum AUTH_CODE_CALLBACK_ACQUIRE { - FACE_BAD_QUALITY = 4, - FACE_NOT_FOUND, - FACE_SCALE_TOO_SMALL, - FACE_SCALE_TOO_LARGE, - FACE_OFFSET_LEFT, - FACE_OFFSET_TOP, - FACE_OFFSET_RIGHT, - FACE_OFFSET_BOTTOM, - FACE_LIVENESS_WARNING = 13, - FACE_LIVENESS_FAILURE, - FACE_ROTATED_LEFT, - FACE_RISE, - FACE_ROTATED_RIGHT, - FACE_DOWN, - FACE_EYE_OCCLUSION = 21, - FACE_EYE_CLOSE, - FACE_EYE_MOUTH_OCCLUSION, - FACE_MULTI = 27, - FACE_BLUR, - FACE_NOT_COMPLETE = 29, - FACE_UNLOCK_FACE_DARKLIGHT, - FACE_UNLOCK_FACE_HIGHTLIGHT, - FACE_UNLOCK_FACE_HALF_SHADOW, - FACE_MOVED, - FACE_OUT_OF_BOUNDS, - FACE_NOT_GAZE = 36, - FACE_HAS_REGISTERED, - FACE_DETECTED, - FACE_DARKPIC, - FACE_ROTATE_TOP_RIGHT, - FACE_ROTATE_TOP_LEFT, - FACE_ROTATE_BOTTOM_RIGHT, - FACE_ROTATE_BOTTOM_LEFT, - FACE_WITHOUT_MASK, - FACE_COVERED_WITH_MASK, - FACE_OPEN_CAMERA_FAILED, - FACE_ANGLE_BASE = 1000, - FACE_ENROLL_INFO_BEGIN = 2000, -} AUTH_CODE_CALLBACK_ACQUIRE; - -typedef enum FIResultType { - TYPE_CALLBACK_ENROLL, - TYPE_CALLBACK_AUTH, - TYPE_CALLBACK_REMOVE, -} FIResultType; - -typedef enum FICode { - CODE_CALLBACK_START = 0, - CODE_CALLBACK_RESULT, - CODE_CALLBACK_CANCEL, - CODE_CALLBACK_ACQUIRE, - CODE_CALLBACK_BUSY, - CODE_CALLBACK_FACEID = 6, - CODE_CALLBACK_MAX, -} FICode; - -typedef enum MGULKStatus { - MG_UNLOCK_OK = 0x0000, // - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class ReturnCallback { -public: - explicit ReturnCallback(std::function callback); - ~ReturnCallback(); - -private: - std::function callback_; -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS - -#endif // FACEAUTH_SERVICES_RETURN_CALLBACK_H \ No newline at end of file diff --git a/common/src/return_callback.cpp b/common/src/return_callback.cpp deleted file mode 100644 index e66c2c3..0000000 --- a/common/src/return_callback.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "return_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -ReturnCallback::ReturnCallback(std::function callback) -{ - this->callback_ = callback; -} - -ReturnCallback::~ReturnCallback() -{ - this->callback_(); -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/faceauth/include/iface_auth.h b/interfaces/innerkits/faceauth/include/iface_auth.h index 0741589..efff165 100644 --- a/interfaces/innerkits/faceauth/include/iface_auth.h +++ b/interfaces/innerkits/faceauth/include/iface_auth.h @@ -20,6 +20,7 @@ #include "face_auth_defines.h" #include "iremote_broker.h" #include "iremote_object.h" +#include "surface.h" namespace OHOS { namespace UserIAM { diff --git a/services/BUILD.gn b/services/BUILD.gn index 0d7629a..24756cc 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -22,55 +22,35 @@ group("faceauthservice_group") { } ohos_shared_library("faceauthservice") { - include_dirs = [ - "//base/user_iam/face_auth/services/faceauth/include", - "//base/user_iam/face_auth/common/include", - "//base/user_iam/face_auth/services/ca_mock/include", - "//base/user_iam/face_auth/interfaces/innerkits/faceauth/include", - "//foundation/multimedia/camera_standard/interfaces/inner_api/native/camera/include", - "//foundation/multimedia/camera_standard/interfaces/innerkits/native/camera/include", - "//drivers/peripheral/camera/interfaces/metadata/include", - "//foundation/multimedia/camera_standard/services/camera_service/binder/base/include", - "//foundation/multimedia/camera_standard/services/camera_service/binder/client/include", - "//foundation/multimedia/camera_standard/services/camera_service/binder/server/include", - ] - sources = [ - "//base/user_iam/face_auth/common/src/return_callback.cpp", - "//base/user_iam/face_auth/services/ca_mock/src/adaptor_algorithm.cpp", - "//base/user_iam/face_auth/services/ca_mock/src/buffer.cpp", - "//base/user_iam/face_auth/services/ca_mock/src/face_auth_ca.cpp", - "//base/user_iam/face_auth/services/ca_mock/src/face_auth_func.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_camera.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_camera_buffer_listener.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_event.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_event_handler.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_executor_callback.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_get_info_callback.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_manager.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_query_callback.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_req.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_service.cpp", - "//base/user_iam/face_auth/services/faceauth/src/face_auth_thread_pool.cpp", + "src/face_auth_driver_hdi.cpp", + "src/face_auth_executor_callback_hdi.cpp", + "src/face_auth_executor_hdi.cpp", + "src/face_auth_service.cpp", + ] + include_dirs = [ + "include", + "../common/include", + "//base/user_iam/user_auth/interfaces/inner_api/include/userauth/", + "//base/user_iam/user_auth/common/logs", + "//base/user_iam/user_auth/common/utils", ] deps = [ "//base/user_iam/face_auth/interfaces/innerkits/faceauth:faceauth_framework", - "//drivers/peripheral/camera/interfaces/metadata:metadata", - "//foundation/multimedia/camera_standard/frameworks/native/camera:camera_framework", - "//third_party/openssl:libcrypto_static", + "//base/user_iam/user_auth/common/executors:userauth_executors", + "//drivers/interface/face_auth/v1_0:libfaceauth_proxy_1.0", + "//drivers/interface/face_auth/v1_0:libfaceauth_stub_1.0", ] external_deps = [ - "eventhandler:libeventhandler", + "device_driver_framework:libhdf_utils", "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", - "startup_l2:syspara_watchagent", "user_auth:coauth_framework", - "user_auth:useridm_framework", "utils_base:utils", ] + remove_configs = [ "//build/config/compiler:no_exceptions" ] if (ability_runtime_graphics) { external_deps += [ "graphic_standard:surface" ] diff --git a/services/ca_mock/include/adaptor_algorithm.h b/services/ca_mock/include/adaptor_algorithm.h deleted file mode 100644 index aa20678..0000000 --- a/services/ca_mock/include/adaptor_algorithm.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ADAPTOR_ALGORITHM_H -#define ADAPTOR_ALGORITHM_H - -#include -#include "buffer.h" -#include "stdint.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -const uint32_t ED25519_FIX_SIGN_BUFFER_SIZE = 64; - -typedef struct { - Buffer *pubKey; - Buffer *priKey; -} KeyPair; - -bool IsEd25519KeyPairValid(const KeyPair *keyPair); -void DestoryKeyPair(KeyPair *keyPair); -KeyPair *GenerateEd25519KeyPair(void); -int32_t Ed25519Sign(const KeyPair *keyPair, const Buffer *data, Buffer **sign); -int32_t Ed25519Verify(const Buffer *pubKey, const Buffer *data, const Buffer *sign); - -int32_t HmacSha256(const Buffer *hmacKey, const Buffer *data, Buffer **hmac); -int32_t HmacSha512(const Buffer *hmacKey, const Buffer *data, Buffer **hmac); - -int32_t SecureRandom(uint8_t *buffer, uint32_t size); -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif - diff --git a/services/ca_mock/include/buffer.h b/services/ca_mock/include/buffer.h deleted file mode 100644 index a5c3a96..0000000 --- a/services/ca_mock/include/buffer.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef COMMON_BUFFER_H -#define COMMON_BUFFER_H - -#include "stdbool.h" -#include "stdint.h" -#include "defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -typedef struct { - uint8_t *buf; - uint32_t contentSize; - uint32_t maxSize; -} Buffer; - -bool IsBufferValid(const Buffer *buffer); -Buffer *CreateBufferBySize(const uint32_t size); -ResultCode InitBuffer(Buffer *buffer, const uint8_t *buf, const uint32_t bufSize); -void DestoryBuffer(Buffer *buffer); -Buffer *CopyBuffer(const Buffer *buffer); -bool CompareBuffer(const Buffer *buffer1, const Buffer *buffer2); -ResultCode GetBufferData(const Buffer *buffer, uint8_t *data, uint32_t *dataSize); -bool CheckBufferWithSize(const Buffer *buffer, const uint32_t size); -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif diff --git a/services/ca_mock/include/defines.h b/services/ca_mock/include/defines.h deleted file mode 100644 index d70229f..0000000 --- a/services/ca_mock/include/defines.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef COMMON_DEFINES_H -#define COMMON_DEFINES_H - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -typedef enum ResultCode { - RESULT_SUCCESS = 0x0, - RESULT_GENERAL_ERROR = 0x1, - RESULT_BAD_PARAM = 0x2, - RESULT_BAD_COPY = 0x3, - RESULT_NO_MEMORY = 0x4, - RESULT_NEED_INIT = 0x5, - RESULT_NOT_FOUND = 0x6, - RESULT_REACH_LIMIT = 0x7, - RESULT_DUPLICATE_CHECK_FAILED = 0x8, - RESULT_BAD_READ = 0x9, - RESULT_BAD_WRITE = 0xA, - RESULT_BAD_DEL = 0xB, - RESULT_UNKNOWN = 0xC, - RESULT_BAD_MATCH = 0xD, - RESULT_BAD_SIGN = 0xE, - RESULT_BUSY = 0xF, - RESULT_PIN_FREEZE = 0x11, - RESULT_PIN_FAIL = 0X12, -} ResultCode; - -typedef enum AuthType { - DEFAULT_AUTH_TYPE = 0, - PIN_AUTH = 1, - FACE_AUTH = 2, -} AuthType; - -typedef enum AuthSubType { - FACE_2D = 20000, - FACE_3D = 20001, -} AuthSubType; - -#define MAX_REMAIN_TIMES 5 -#define MAX_DUPLICATE_CHECK 100 -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif diff --git a/services/ca_mock/include/face_auth_ca.h b/services/ca_mock/include/face_auth_ca.h deleted file mode 100644 index 37d57ad..0000000 --- a/services/ca_mock/include/face_auth_ca.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_CA_H -#define FACE_AUTH_CA_H - -#include -#include -#include -#include -#include -#include -#include -#include "face_auth_defines.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -typedef enum { - Enroll, - Auth, -} AlgorithmOperation; - -typedef struct { - uint64_t templateId = 0; - uint64_t scheduleId = 0; -} AlgorithmParam; - -typedef struct { - uint8_t *image = nullptr; - uint32_t imageSize = 0; - uint32_t width = 0; - uint32_t height = 0; - uint32_t stride = 0; - int64_t timestamp = 0; -} CameraImage; - -typedef struct { - int32_t result = 0; - int32_t remainTimes = 0; - uint64_t templateId = 0; - std::vector coauthMsg; // coauth signed msg -} AlgorithmResult; - -typedef struct { - int32_t resultCode; - int32_t param[RESULT_MAX_SIZE]; -} ResultInfo; - -typedef struct FaceCredentialInfo { - uint64_t subType; - uint32_t remainTimes; - uint32_t freezingTime; -} FaceCredentialInfo; - -class FaceAuthCA { -public: - static std::shared_ptr GetInstance(); - FaceAuthCA(); - ~FaceAuthCA() = default; - int32_t Init(); - int32_t Close(); - int32_t LoadAlgorithm(); - int32_t ReleaseAlgorithm(); - int32_t StartAlgorithmOperation(AlgorithmOperation algorithmOperation, AlgorithmParam param); - int32_t TransferImageToAlgorithm(CameraImage images); - void GetAlgorithmState(int32_t &retCode, std::vector &retCoauthMsg); - int32_t GetExecutorInfo(std::vector &pubKey, uint32_t &esl, uint64_t &authAbility); - int32_t FinishAlgorithmOperation(AlgorithmResult &retResult); - int32_t DeleteTemplate(uint64_t templateId); - int32_t VerifyTemplateData(std::vector templateIdList); - int32_t GetRemainTimes(uint64_t templateId, int32_t &remainingTimes); - int32_t GetFaceInfo(uint64_t templateId, FaceCredentialInfo &faceCredentialInfo); - int32_t ResetRemainTimes(uint64_t templateId); - int32_t FreezeTemplate(uint64_t templateId); - int32_t CancelAlgorithmOperation(); - void SetAlgorithmParam(const AlgorithmParam ¶m); - -private: - static std::shared_ptr faceAuthCA_; - static std::mutex mutex_; - int32_t resultNum_ = 0; - uint64_t cancelReqId_ = 0; - bool isReturnFaceId_ = false; - bool isInitFail_ = false; - bool isCancel_ = false; - HWExeType type_; - AlgorithmParam param_; - std::map resultInfos_; - std::map errorCode_; - std::vector templateIdList_; - std::map remainTimesMap_; - AlgorithmOperation algorithmOperation_; - -private: - void GetAuthResult(int32_t &result); - FIRetCode GetAuthState(int32_t &authErrorCode, FICode &code, uint64_t reqId); - FIRetCode GetState(int32_t &resultCode, int32_t (¶m)[RESULT_MAX_SIZE]); - void ReadFile(); - int32_t SwitchAuthErrorCode(int32_t param); - int32_t CheckIsCancel(int32_t &authErrorCode, FICode &code, uint64_t reqId); - FIRetCode Cancel(uint64_t reqId); - void InitErrorCode(); - void CheckFile(std::string s); - void GetEnrollAngleResult(); - void GetEnrollSuccessResult(); - void GetEnrollHasRegistered(); - void GetOverMaxFace(); - void GetCaremaFail(); - void GetAngleTimeout(); - void GetResultFail(); - void GetResultTimeOut(); - FIRetCode DynamicInit(); - FIRetCode DynamicRelease(); - void ReadInitFile(); - void ReadReleaseFile(); - void CheckInitFile(std::string s); - void CheckReleaseFile(std::string s); - FIRetCode Prepare(HWExeType type); - int getAlgorithmResult(); - uint64_t GetNewTemplateId(); -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACE_AUTH_CA_H diff --git a/services/ca_mock/include/face_auth_func.h b/services/ca_mock/include/face_auth_func.h deleted file mode 100644 index 663a062..0000000 --- a/services/ca_mock/include/face_auth_func.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTHTA_FUNC_H -#define FACE_AUTHTA_FUNC_H - -#include -#include "defines.h" -#include "buffer.h" -#include "co_auth_info_define.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -#define TAG_AND_LEN_BYTE 8 -#define TAG_ANG_LEN_T 12 -#define TAG_AND_LEN_S 16 -#define MAX_TLV_LEN 200 -#define SIGN_DATA_LEN 64 -#define PIN_RET_TYPE_LEN 8 -#define PIN_RET_DATA_LEN 72 -#define FACE_AUTH_CAPABILITY_LEVEL 2 -#define ED25519_FIX_PUBKEY_BUFFER_SIZE 32 -#define ED25519_FIX_PRIKEY_BUFFER_SIZE 64 -#define FACE_EXECUTOR_SECURITY_LEVEL 1 -#define PIN_AUTH_ABILITY 7 -#define FACE_AUTH_ABILITY 7 -#define RESULT_TLV_LEN 160U -#define CONST_PUB_KEY_LEN 32U - -ResultCode GenerateRetTlv(uint32_t result, uint64_t scheduleId, uint64_t subType, uint64_t templatedId, Buffer *retTlv); -ResultCode GenerateKeyPair(); -ResultCode SetResultTlv(Buffer *retTlv, std::vector &resultTlv); -ResultCode DoGetExecutorInfo(std::vector &vPubKey, uint32_t &esl, uint64_t &authAbility); -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACE_AUTHTA_FUNC_H diff --git a/services/ca_mock/src/adaptor_algorithm.cpp b/services/ca_mock/src/adaptor_algorithm.cpp deleted file mode 100644 index 3647198..0000000 --- a/services/ca_mock/src/adaptor_algorithm.cpp +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "adaptor_algorithm.h" -#include -#include -#include -#include "buffer.h" -#include "defines.h" -#include "face_auth_log_wrapper.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -const int OPENSSL_SUCCESS = 1; -const uint32_t ED25519_FIX_PRIKEY_BUFFER_SIZE = 32; -const uint32_t ED25519_FIX_PUBKEY_BUFFER_SIZE = 32; -const uint32_t SHA256_DIGEST_SIZE = 32; -const uint32_t SHA512_DIGEST_SIZE = 64; - -static KeyPair *CreateEd25519KeyPair(void) -{ - KeyPair *keyPair = new KeyPair; - keyPair->pubKey = CreateBufferBySize(ED25519_FIX_PUBKEY_BUFFER_SIZE); - if (keyPair->pubKey == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "no memory for public key"); - delete keyPair; - return nullptr; - } - keyPair->priKey = CreateBufferBySize(ED25519_FIX_PRIKEY_BUFFER_SIZE); - if (keyPair->priKey == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "no memory for private key"); - DestoryBuffer(keyPair->pubKey); - delete keyPair; - return nullptr; - } - return keyPair; -} - -void DestoryKeyPair(KeyPair *keyPair) -{ - if (keyPair == nullptr) { - return; - } - if (keyPair->pubKey != nullptr) { - DestoryBuffer(keyPair->pubKey); - } - if (keyPair->priKey != nullptr) { - DestoryBuffer(keyPair->priKey); - } - delete keyPair; -} - -bool IsEd25519KeyPairValid(const KeyPair *keyPair) -{ - if (keyPair == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "invalid key pair"); - return false; - } - if (!CheckBufferWithSize(keyPair->pubKey, ED25519_FIX_PUBKEY_BUFFER_SIZE)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "invalid public key"); - return false; - } - if (!CheckBufferWithSize(keyPair->priKey, ED25519_FIX_PRIKEY_BUFFER_SIZE)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "invalid private key"); - return false; - } - return true; -} - -KeyPair *GenerateEd25519KeyPair() -{ - KeyPair *keyPair = CreateEd25519KeyPair(); - if (keyPair == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create key pair fail"); - return nullptr; - } - EVP_PKEY *key = nullptr; - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, nullptr); - if (ctx == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "new ctx fail"); - DestoryKeyPair(keyPair); - keyPair = nullptr; - return keyPair; - } - if (EVP_PKEY_keygen_init(ctx) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "init ctx fail"); - DestoryKeyPair(keyPair); - keyPair = nullptr; - return keyPair; - } - if (EVP_PKEY_keygen(ctx, &key) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "generate key fail"); - DestoryKeyPair(keyPair); - keyPair = nullptr; - return keyPair; - } - size_t pubKeySize = keyPair->pubKey->maxSize; - if (EVP_PKEY_get_raw_public_key(key, keyPair->pubKey->buf, &pubKeySize) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get public key fail"); - DestoryKeyPair(keyPair); - keyPair = nullptr; - return keyPair; - } - keyPair->pubKey->contentSize = pubKeySize; - size_t priKeySize = keyPair->priKey->maxSize; - if (EVP_PKEY_get_raw_private_key(key, keyPair->priKey->buf, &priKeySize) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get private key fail"); - DestoryKeyPair(keyPair); - keyPair = nullptr; - return keyPair; - } - keyPair->priKey->contentSize = priKeySize; - if (key != nullptr) { - EVP_PKEY_free(key); - } - if (ctx != nullptr) { - EVP_PKEY_CTX_free(ctx); - } - return keyPair; -} - -int32_t Ed25519Sign(const KeyPair *keyPair, const Buffer *data, Buffer **sign) -{ - if (!IsEd25519KeyPairValid(keyPair) || !IsBufferValid(data) || sign == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "bad parameter"); - return RESULT_BAD_PARAM; - } - int32_t ret = RESULT_GENERAL_ERROR; - EVP_PKEY *key = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr, - keyPair->priKey->buf, keyPair->priKey->contentSize); - if (key == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get private key fail"); - return ret; - } - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - if (ctx == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get ctx fail"); - EVP_PKEY_free(key); - return ret; - } - if (EVP_DigestSignInit(ctx, nullptr, nullptr, nullptr, key) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "init sign fail"); - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; - } - *sign = CreateBufferBySize(ED25519_FIX_SIGN_BUFFER_SIZE); - if (!IsBufferValid(*sign)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create buffer fail"); - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; - } - size_t signSize = (*sign)->maxSize; - if (EVP_DigestSign(ctx, (*sign)->buf, &signSize, data->buf, data->contentSize) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "sign fail"); - DestoryBuffer(*sign); - *sign = nullptr; - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; - } - (*sign)->contentSize = signSize; - ret = RESULT_SUCCESS; - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; -} - -int32_t Ed25519Verify(const Buffer *pubKey, const Buffer *data, const Buffer *sign) -{ - if (!CheckBufferWithSize(pubKey, ED25519_FIX_PUBKEY_BUFFER_SIZE) || !IsBufferValid(data) || - !CheckBufferWithSize(sign, ED25519_FIX_SIGN_BUFFER_SIZE)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "bad parameter"); - return RESULT_BAD_PARAM; - } - int32_t ret = RESULT_GENERAL_ERROR; - EVP_PKEY *key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, pubKey->buf, pubKey->contentSize); - if (key == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get public key fail"); - return ret; - } - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - if (ctx == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get ctx fail"); - EVP_PKEY_free(key); - return ret; - } - if (EVP_DigestVerifyInit(ctx, nullptr, nullptr, nullptr, key) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "init verify fail"); - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; - } - if (EVP_DigestVerify(ctx, sign->buf, sign->contentSize, data->buf, data->contentSize) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "verify fail"); - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; - } - ret = RESULT_SUCCESS; - EVP_PKEY_free(key); - EVP_MD_CTX_free(ctx); - return ret; -} - -static int32_t CalcHmac(const EVP_MD *alg, - const Buffer *hmacKey, const Buffer *data, Buffer *hmac) -{ - if (!IsBufferValid(hmacKey) || hmacKey->contentSize > INT_MAX || - !IsBufferValid(data) || !IsBufferValid(hmac) || hmac->maxSize > UINT_MAX) { - FACEAUTH_HILOGE(MODULE_SERVICE, "bad parameter"); - return RESULT_BAD_PARAM; - } - unsigned int hmacSize = hmac->maxSize; - uint8_t *hmacData = HMAC(alg, hmacKey->buf, (int)hmacKey->contentSize, data->buf, data->contentSize, - hmac->buf, &hmacSize); - if (hmacData == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "generate hmac fail"); - return RESULT_GENERAL_ERROR; - } - hmac->contentSize = hmacSize; - return RESULT_SUCCESS; -} - -int32_t HmacSha256(const Buffer *hmacKey, const Buffer *data, Buffer **hmac) -{ - if (hmac == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "hmac is nullptr"); - return RESULT_BAD_PARAM; - } - const EVP_MD *alg = EVP_sha256(); - if (alg == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get algorithm fail"); - return RESULT_GENERAL_ERROR; - } - *hmac = CreateBufferBySize(SHA256_DIGEST_SIZE); - if (*hmac == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create buffer fail"); - return RESULT_NO_MEMORY; - } - if (CalcHmac(alg, hmacKey, data, *hmac) != RESULT_SUCCESS) { - DestoryBuffer(*hmac); - *hmac = nullptr; - FACEAUTH_HILOGE(MODULE_SERVICE, "calculate hmac fail"); - return RESULT_GENERAL_ERROR; - } - return RESULT_SUCCESS; -} - -int32_t HmacSha512(const Buffer *hmacKey, const Buffer *data, Buffer **hmac) -{ - if (hmac == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "hmac is nullptr"); - return RESULT_BAD_PARAM; - } - const EVP_MD *alg = EVP_sha512(); - if (alg == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get EVP_MD fail"); - return RESULT_GENERAL_ERROR; - } - *hmac = CreateBufferBySize(SHA512_DIGEST_SIZE); - if (*hmac == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create buffer fail"); - return RESULT_NO_MEMORY; - } - if (CalcHmac(alg, hmacKey, data, *hmac) != RESULT_SUCCESS) { - DestoryBuffer(*hmac); - *hmac = nullptr; - FACEAUTH_HILOGE(MODULE_SERVICE, "calculate hmac fail"); - return RESULT_GENERAL_ERROR; - } - return RESULT_SUCCESS; -} - -int32_t SecureRandom(uint8_t *buffer, uint32_t size) -{ - if (buffer == nullptr || size > INT_MAX) { - FACEAUTH_HILOGE(MODULE_SERVICE, "bad parameter"); - return RESULT_BAD_PARAM; - } - if (RAND_bytes(buffer, (int)size) != OPENSSL_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get random bytes fail"); - return RESULT_GENERAL_ERROR; - } - return RESULT_SUCCESS; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/ca_mock/src/buffer.cpp b/services/ca_mock/src/buffer.cpp deleted file mode 100644 index 66f4017..0000000 --- a/services/ca_mock/src/buffer.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "buffer.h" -#include "securec.h" -#include "face_auth_log_wrapper.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -const int MAX_BUFFER_SIZE = 512000; - -bool IsBufferValid(const Buffer *buffer) -{ - if ((buffer == nullptr) || (buffer->buf == nullptr) || - (buffer->maxSize == 0) || (buffer->maxSize > MAX_BUFFER_SIZE) || - (buffer->contentSize > buffer->maxSize)) { - return false; - } - - return true; -} - -bool CheckBufferWithSize(const Buffer *buffer, const uint32_t size) -{ - if ((!IsBufferValid(buffer)) || (buffer->contentSize != size)) { - return false; - } - - return true; -} - -Buffer *CreateBufferBySize(const uint32_t size) -{ - if ((size == 0) || (size > MAX_BUFFER_SIZE)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Bad param size:%{public}u", size); - return nullptr; - } - - Buffer *buffer = static_cast(malloc(sizeof(Buffer))); - if (buffer == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "malloc buffer fail"); - return nullptr; - } - - buffer->buf = static_cast(malloc(size)); - if (buffer->buf == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "malloc buffer content fail"); - free(buffer); - return nullptr; - } - - if (memset_s(buffer->buf, size, 0, size) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "clear buffer content fail"); - free(buffer->buf); - free(buffer); - return nullptr; - } - buffer->maxSize = size; - buffer->contentSize = 0; - - return buffer; -} - -ResultCode InitBuffer(Buffer *buffer, const uint8_t *buf, const uint32_t bufSize) -{ - if (!IsBufferValid(buffer) || (buf == nullptr) || (bufSize == 0)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Bad param"); - return RESULT_BAD_PARAM; - } - - if (memcpy_s(buffer->buf, buffer->maxSize, buf, bufSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Copy buffer fail"); - return RESULT_BAD_COPY; - } - buffer->contentSize = bufSize; - - return RESULT_SUCCESS; -} - -void DestoryBuffer(Buffer *buffer) -{ - if (buffer != nullptr) { - if (buffer->buf != nullptr) { - if (memset_s(buffer->buf, buffer->contentSize, 0, buffer->contentSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "memset fail"); - } - free(buffer->buf); - buffer->buf = nullptr; - buffer->contentSize = 0; - buffer->maxSize = 0; - } - free(buffer); - } -} - -Buffer *CopyBuffer(const Buffer *buffer) -{ - if (!IsBufferValid(buffer)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Invalid buffer"); - return nullptr; - } - - Buffer *copyBuffer = CreateBufferBySize(buffer->maxSize); - if (copyBuffer == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create buffer fail"); - return nullptr; - } - - if (memcpy_s(copyBuffer->buf, copyBuffer->maxSize, buffer->buf, buffer->contentSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Copy buffer content fail"); - DestoryBuffer(copyBuffer); - return nullptr; - } - copyBuffer->contentSize = buffer->contentSize; - - return copyBuffer; -} - -bool CompareBuffer(const Buffer *buffer1, const Buffer *buffer2) -{ - if (!IsBufferValid(buffer1) || !IsBufferValid(buffer2) || (buffer1->contentSize != buffer2->contentSize)) { - return false; - } - - if (memcmp(buffer1->buf, buffer2->buf, buffer1->contentSize) == 0) { - return true; - } - - return false; -} - -ResultCode GetBufferData(const Buffer *buffer, uint8_t *data, uint32_t *dataSize) -{ - if (!IsBufferValid(buffer) || (data == nullptr) || (dataSize == nullptr)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Bad param"); - return RESULT_BAD_PARAM; - } - if (memcpy_s(data, *dataSize, buffer->buf, buffer->contentSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Copy buffer fail"); - return RESULT_BAD_COPY; - } - *dataSize = buffer->contentSize; - return RESULT_SUCCESS; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/ca_mock/src/face_auth_ca.cpp b/services/ca_mock/src/face_auth_ca.cpp deleted file mode 100644 index bc4acdc..0000000 --- a/services/ca_mock/src/face_auth_ca.cpp +++ /dev/null @@ -1,873 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_ca.h" -#include -#include -#include -#include "securec.h" -#include "adaptor_algorithm.h" -#include "defines.h" -#include "face_auth_func.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -static const int32_t CA_RESULT_SUCCESS = 0; -static const int32_t CA_RESULT_FAILED = 1; -static const int32_t CA_RESULT_CANCELED = 3; -static const int32_t CA_RESULT_LOCKED = 9; -static const int32_t BUFF_MAX_LEN = 128; -static const int32_t CASE_NUM = 100; -static const int32_t CODE_NUM = 5; -static const int32_t RESULT_CODE = 6; -static const int32_t IFACE_OVER_MAX_FACES = 901; -static const int32_t IFACE_ENROLL_HAS_REGISTERED = 902; -static const int32_t IFACE_CAMERA_FAIL = 903; -static const int32_t IFACE_TIME_OUT = 904; -static const std::string TEST_ANGLE = "999"; -static const std::string TEST_ENROLL_SUCCESS = "998"; -static const std::string TEST_OVER_MAX_FACES = "901"; -static const std::string TEST_ENROLL_HAS_REGISTERED = "902"; -static const std::string TEST_CAMERA_FAIL = "903"; -static const std::string TEST_ANGLE_TIMEOUT = "905"; -static const int32_t FACE_AUTH_GETRESULT_FAIL = 1001; -static const int32_t FACE_AUTH_GETRESULT_TIMEOUT = 1002; -static const std::string FACE_AUTH_GETRESULT_FAIL_STRING = "1001"; -static const std::string FACE_AUTH_GETRESULT_TIMEOUT_STRING = "1002"; -static const std::string FACE_AUTH_FAIL_STRING = "1003"; -static const std::string FACE_AUTH_INIT_TIMEOUT_STRING = "1004"; -static const char *INIT_FILENAME("/data/useriam/init.dat"); -static const char *RELEASE_FILENAME("/data/useriam/release.dat"); -static const char *CONFIG_FILENAME("/data/useriam/config.dat"); -static const char *FACEID_FILENAME("/data/useriam/faceId.dat"); -static const char *AUTH_RESULT_FILENAME("/data/useriam/auth_result.dat"); -static const int32_t SLEEP_LONG_NUM = 2000; -static const int32_t PARAM_RANGE = 10; -static const int32_t TEST_ANGLE_START_NUM = 1000; -static const int32_t TEST_ANGLE_MAX_NUM = 1013; -static const int32_t TEST_ANGLE_ADD_NUM = 4; -static const int32_t DEFAULT_REMAIN_TIMES = 5; -static const int32_t SLEEP_TIME = 5000; -static int32_t faceId_ = 1; -static bool isRunningFlag = false; -std::shared_ptr FaceAuthCA::faceAuthCA_ = nullptr; -std::mutex FaceAuthCA::mutex_; -FaceAuthCA::FaceAuthCA() - : resultNum_(0), cancelReqId_(0), isReturnFaceId_(false), isInitFail_(false), - isCancel_(false), type_(HW_EXEC_TYPE_LEARN) -{ - InitErrorCode(); -} - -std::shared_ptr FaceAuthCA::GetInstance() -{ - if (faceAuthCA_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (faceAuthCA_ == nullptr) { - faceAuthCA_ = std::make_shared(); - } - } - return faceAuthCA_; -} - -int32_t FaceAuthCA::Init() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - GenerateKeyPair(); - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::Close() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::LoadAlgorithm() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (DynamicInit() == FI_RC_OK) { - return CA_RESULT_SUCCESS; - } else { - return CA_RESULT_FAILED; - } -} - -int32_t FaceAuthCA::ReleaseAlgorithm() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (DynamicRelease() == FI_RC_OK) { - return CA_RESULT_SUCCESS; - } else { - return CA_RESULT_FAILED; - } -} - -int32_t FaceAuthCA::StartAlgorithmOperation(AlgorithmOperation algorithmOperation, AlgorithmParam param) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - isRunningFlag = true; - SetAlgorithmParam(param); - algorithmOperation_ = algorithmOperation; - if (algorithmOperation == Enroll) { - Prepare(HW_EXEC_TYPE_ENROOL); - return CA_RESULT_SUCCESS; - } else if (algorithmOperation == Auth) { - Prepare(HW_EXEC_TYPE_UNLOCK); - return CA_RESULT_SUCCESS; - } else { - return CA_RESULT_FAILED; - } -} - -int32_t FaceAuthCA::TransferImageToAlgorithm(CameraImage images) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - (void)(images); - return CA_RESULT_SUCCESS; -} - -void FaceAuthCA::GetAlgorithmState(int32_t &retCode, std::vector &retCoauthMsg) -{ - retCoauthMsg.clear(); - int32_t algorithmState = -1; - FICode code = CODE_CALLBACK_START; - GetAuthState(algorithmState, code, param_.scheduleId); - FACEAUTH_HILOGI(MODULE_SERVICE, "algorithmState = %{public}d", algorithmState); - retCoauthMsg.resize(sizeof(int32_t)); - if (memcpy_s(&retCoauthMsg[0], retCoauthMsg.size(), &algorithmState, sizeof(int32_t)) != EOK) { - FACEAUTH_HILOGI(MODULE_SERVICE, "memcpy_s fail"); - retCode = 1; - return; - } - retCode = 1; -} - -int32_t FaceAuthCA::GetExecutorInfo(std::vector &pubKey, uint32_t &esl, uint64_t &authAbility) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (DoGetExecutorInfo(pubKey, esl, authAbility) != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "DoGetExecutorInfo failed"); - return CA_RESULT_FAILED; - } - return CA_RESULT_SUCCESS; -} - -int FaceAuthCA::getAlgorithmResult() -{ - if (isCancel_ == true) { - FACEAUTH_HILOGI(MODULE_SERVICE, "operation has been canceled"); - return CA_RESULT_CANCELED; - } else { - if (algorithmOperation_ == Auth) { - FACEAUTH_HILOGI(MODULE_SERVICE, "face auth remain times is %{public}d", - remainTimesMap_[param_.templateId]); - if (remainTimesMap_[param_.templateId] == 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "operation has been locked"); - return CA_RESULT_LOCKED; - } - } - } - int authResult = 0; - GetAuthResult(authResult); - FACEAUTH_HILOGI(MODULE_SERVICE, "get authResult %{public}d", authResult); - return authResult; -} - -int32_t FaceAuthCA::FinishAlgorithmOperation(AlgorithmResult &retResult) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - // wait for 1s for cancel and image receive - sleep(1); - isRunningFlag = false; - FACEAUTH_HILOGI(MODULE_SERVICE, "set isRunningFlag to false"); - int32_t authResult = getAlgorithmResult(); - FACEAUTH_HILOGI(MODULE_SERVICE, "get auth result = %{public}d", authResult); - if (algorithmOperation_ == Enroll) { - if (authResult == 0) { - templateIdList_.push_back(param_.templateId); - remainTimesMap_[param_.templateId] = DEFAULT_REMAIN_TIMES; - } - } else { - if (authResult == 0) { - remainTimesMap_[param_.templateId] = DEFAULT_REMAIN_TIMES; - } - if ((authResult == CA_RESULT_FAILED) && (remainTimesMap_[param_.templateId] > 0)) { - remainTimesMap_[param_.templateId]--; - } - } - isCancel_ = false; - - retResult.result = authResult; - retResult.templateId = param_.templateId; - retResult.remainTimes = remainTimesMap_[param_.templateId]; - - Buffer *retTlv = CreateBufferBySize(RESULT_TLV_LEN); - if (retTlv == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "CreateBufferBySize failed"); - return CA_RESULT_FAILED; - } - ResultCode result = GenerateRetTlv(authResult, param_.scheduleId, FACE_2D, param_.templateId, retTlv); - if (result != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GenerateRetTlv failed"); - DestoryBuffer(retTlv); - return CA_RESULT_FAILED; - } - result = SetResultTlv(retTlv, retResult.coauthMsg); - if (result != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "SetResultTlv failed"); - DestoryBuffer(retTlv); - return CA_RESULT_FAILED; - } - DestoryBuffer(retTlv); - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::DeleteTemplate(uint64_t templateId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::vector::const_iterator iter; - for (iter = templateIdList_.cbegin(); iter != templateIdList_.cend(); ++iter) { - if (*iter == templateId) { - break; - } - } - if (iter == templateIdList_.cend()) { - FACEAUTH_HILOGE(MODULE_SERVICE, "template id not found"); - return CA_RESULT_FAILED; - } - templateIdList_.erase(iter); - remainTimesMap_.erase(templateId); - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::VerifyTemplateData(std::vector templateIdList) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - templateIdList_.assign(templateIdList.begin(), templateIdList.end()); - remainTimesMap_.clear(); - for (uint32_t index = 0; index < templateIdList_.size(); index++) { - remainTimesMap_[templateIdList_.at(index)] = DEFAULT_REMAIN_TIMES; - } - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::GetFaceInfo(uint64_t templateId, FaceCredentialInfo &faceCredentialInfo) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - faceCredentialInfo.subType = FACE_2D; - faceCredentialInfo.freezingTime = 0; - faceCredentialInfo.remainTimes = static_cast(remainTimesMap_[templateId]); - return 0; -} - -int32_t FaceAuthCA::GetRemainTimes(uint64_t templateId, int32_t &remainingTimes) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - remainingTimes = remainTimesMap_[templateId]; - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::ResetRemainTimes(uint64_t templateId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "reset templateId %{public}s remain times", - getMaskedString(templateId).c_str()); - remainTimesMap_[templateId] = DEFAULT_REMAIN_TIMES; - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::FreezeTemplate(uint64_t templateId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "freeze template %{public}s", - getMaskedString(templateId).c_str()); - remainTimesMap_[templateId] = 0; - return CA_RESULT_SUCCESS; -} - -int32_t FaceAuthCA::CancelAlgorithmOperation() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (Cancel(param_.scheduleId) != FI_RC_OK) { - return CA_RESULT_FAILED; - } - - return CA_RESULT_SUCCESS; -} - -uint64_t FaceAuthCA::GetNewTemplateId() -{ - const int MAX_TRY = 10; - for (int i = 0; i < MAX_TRY; i++) { - uint64_t templateId = 0; - int ret = SecureRandom(reinterpret_cast(&templateId), sizeof(templateId)); - if ((ret != CA_RESULT_SUCCESS) || (templateId == 0)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "generated secure random fail"); - continue; - } - if (remainTimesMap_.count(templateId) > 0) { - FACEAUTH_HILOGE(MODULE_SERVICE, "duplicate template id"); - continue; - } - return templateId; - } - return 0; -} - -void FaceAuthCA::SetAlgorithmParam(const AlgorithmParam ¶m) -{ - param_ = param; - if (param_.templateId == 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "template id is 0, generate new templateId"); - param_.templateId = GetNewTemplateId(); - if (param_.templateId == 0) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get new template id failed"); - } - } -} - -void FaceAuthCA::GetAuthResult(int32_t &result) -{ - FILE *file = nullptr; - file = fopen(AUTH_RESULT_FILENAME, "r"); - if (file == nullptr) { - FACEAUTH_HILOGI(MODULE_SERVICE, "open file failed"); - return; - } - if (fseek(file, 0, SEEK_END) != 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "fseek failed"); - fclose(file); - return; - } - if (ftell(file) > 0) { - fseek(file, 0, SEEK_SET); - char str[BUFF_MAX_LEN] = {0}; - fread((void *)str, sizeof(char), BUFF_MAX_LEN - 1, file); - result = atoi(str); - } - if (fclose(file) != 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "fclose failed"); - } -} -FIRetCode FaceAuthCA::GetAuthState(int32_t &authErrorCode, FICode &code, uint64_t reqId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - int32_t resultCode = 0; - int32_t params[ALO_GETRESULT_PARAM_LEN] = {0}; - if (!CheckIsCancel(authErrorCode, code, reqId)) { - return FI_RC_OK; - } - FIRetCode result = GetState(resultCode, params); - if (result != FI_RC_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetState failed"); - } - FACEAUTH_HILOGI(MODULE_SERVICE, "params[0] is %{public}d", params[0]); - if (resultCode == FI_COMPARE_FAIL) { - authErrorCode = SwitchAuthErrorCode(params[0]); - code = CODE_CALLBACK_RESULT; - return result; - } - if (errorCode_.find(resultCode) != errorCode_.end()) { - authErrorCode = errorCode_[resultCode]; - } else { - authErrorCode = resultCode; - } - if (authErrorCode == FACE_AUTH_GETRESULT_FAIL) { - authErrorCode = ERRCODE_FAIL; - code = CODE_CALLBACK_RESULT; - return result; - } - if (authErrorCode == FACE_AUTH_GETRESULT_TIMEOUT) { - authErrorCode = ERRCODE_TIMEOUT; - code = CODE_CALLBACK_RESULT; - return result; - } - if (authErrorCode == IFACE_TIME_OUT) { - authErrorCode = ERRCODE_TIMEOUT; - code = CODE_CALLBACK_RESULT; - return result; - } - if (authErrorCode == IFACE_CAMERA_FAIL) { - authErrorCode = ERRCODE_CAMERA_FAIL; - } - if (authErrorCode == ERRCODE_SUCCESS || authErrorCode == ERRCODE_CAMERA_FAIL) { - code = CODE_CALLBACK_RESULT; - } else { - code = CODE_CALLBACK_ACQUIRE; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "reqid_ is %{public}s, code_ is %{public}d, errorcode_ is %{public}d", - getMaskedString(reqId).c_str(), code, authErrorCode); - FACEAUTH_HILOGI(MODULE_SERVICE, "end"); - return result; -} - -FIRetCode FaceAuthCA::GetState(int32_t &resultCode, int32_t (¶m)[RESULT_MAX_SIZE]) -{ - ReadFile(); - FACEAUTH_HILOGI(MODULE_SERVICE, "resultNum_ is %{public}d", resultNum_); - FACEAUTH_HILOGI(MODULE_SERVICE, "resultInfos_.size() is %{public}zu", resultInfos_.size()); - if (resultNum_ < (int)resultInfos_.size()) { - resultCode = resultInfos_[resultNum_].resultCode; - FACEAUTH_HILOGI(MODULE_SERVICE, "memcpy length is %{public}zu", sizeof(int32_t) * RESULT_MAX_SIZE); - if (memcpy_s(param, sizeof(param), resultInfos_[resultNum_].param, - sizeof(resultInfos_[resultNum_].param)) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "memcpy_s fail"); - return FIRetCode::FI_RC_ERROR; - } - if (resultNum_ >= 1 && resultInfos_[resultNum_ - 1].resultCode == FACE_DETECTED) { - std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_LONG_NUM)); - } - resultNum_++; - int32_t result = 0; - - if (resultCode == FI_COMPARE_FAIL || param[0] != 0) { - result = SwitchAuthErrorCode(param[0]); - } - int32_t num = 6; - if (resultCode == FI_COMPARE_FAIL && param[0] == num) { - resultCode = IFACE_TIME_OUT; - resultInfos_.clear(); - resultNum_ = 0; - remove(CONFIG_FILENAME); - return FIRetCode::FI_RC_OK; - } - if (resultCode == FI_ENROLL_SUCCESS && param[0] != 0 && isReturnFaceId_ == false) { - isReturnFaceId_ = true; - resultNum_--; - return FIRetCode::FI_RC_OK; - } - - if ((resultCode >= FI_ENROLL_SUCCESS && resultCode <= FI_COMPARE_FAIL) || resultCode == IFACE_OVER_MAX_FACES || - resultCode == IFACE_ENROLL_HAS_REGISTERED || resultCode == FACE_AUTH_GETRESULT_FAIL || - resultCode == FACE_AUTH_GETRESULT_TIMEOUT) { - if (result != FACE_MOVED) { - resultInfos_.clear(); - resultNum_ = 0; - remove(CONFIG_FILENAME); - } - } - FACEAUTH_HILOGI(MODULE_SERVICE, "authErrorCode is %{public}d", resultCode); - if (param[0] != 0) { - for (int32_t i = 0; i < RESULT_MAX_SIZE; i++) { - FACEAUTH_HILOGI(MODULE_SERVICE, "param[%{public}d] is %{public}d", i, param[i]); - } - } - } - return FIRetCode::FI_RC_OK; -} -void FaceAuthCA::ReadFile() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "ReadFile start"); - resultInfos_.clear(); - std::ifstream mystream(CONFIG_FILENAME); - - if (!mystream.is_open()) { - FACEAUTH_HILOGI(MODULE_SERVICE, "Prepare type is %{public}d", type_); - if (type_ == HW_EXEC_TYPE_ENROOL) { - resultInfos_[0].resultCode = FI_ENROLL_SUCCESS; - resultInfos_[1].resultCode = FI_FACE_SCALE_TOO_SMALL; - } else { - resultInfos_[0].resultCode = FI_COMPARE_SUCCESS; - resultInfos_[1].resultCode = FI_FACE_OFFSET_RIGHT; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "ReadFile open fail"); - return; - } - char casenum[CASE_NUM]; - mystream.getline(casenum, CASE_NUM); - std::string s = casenum; - FACEAUTH_HILOGI(MODULE_SERVICE, "casenum is %{public}s", s.c_str()); - CheckFile(s); - char buffer[CODE_NUM]; - mystream.getline(buffer, CODE_NUM); - char str[RESULT_CODE]; - int32_t num = atoi(buffer); - for (int32_t i = 0; i < num; i++) { - mystream.getline(str, RESULT_CODE); - resultInfos_[i].resultCode = atoi(str); - FACEAUTH_HILOGI(MODULE_SERVICE, "authErrorCode is %{public}d", resultInfos_[i].resultCode); - for (int32_t j = 0; j < PARAM_NUM; j++) { - mystream.getline(str, RESULT_CODE); - resultInfos_[i].param[j] = atoi(str); - } - } - mystream.close(); -} -int32_t FaceAuthCA::SwitchAuthErrorCode(int32_t param) -{ - int32_t authErrorCode = 0; - switch (param) { - case FI_FAIL_REASON_FACE_NOT_MATCH: - authErrorCode = ERRCODE_COMPARE_FAIL; - break; - case FI_FAIL_REASON_FACE_MOVED: - authErrorCode = FACE_MOVED; - break; - case FI_FAIL_REASON_NOT_GAZE: - authErrorCode = ERRCODE_NOT_GAZE; - break; - case FI_FAIL_REASON_TIME_OUT: - authErrorCode = ERRCODE_TIMEOUT; - break; - case FI_FAIL_REASON_FACE_OUT_OF_BOUNDS: - case FI_FAIL_REASON_FACE_NOT_FOUND: - case FI_FAIL_REASON_LIVENESS_FAILURE: - authErrorCode = ERRCODE_FAIL; - break; - default: - authErrorCode = ERRCODE_NO_FACE_DATA; - break; - } - return authErrorCode; -} -int32_t FaceAuthCA::CheckIsCancel(int32_t &authErrorCode, FICode &code, uint64_t reqId) -{ - if (isCancel_) { - if (cancelReqId_ != reqId) { - isCancel_ = false; - FACEAUTH_HILOGW(MODULE_SERVICE, - "cancelReqId_ and reqId are different. cancelReqId_ is %{public}s, reqId is %{public}s", - getMaskedString(cancelReqId_).c_str(), getMaskedString(reqId).c_str()); - } - authErrorCode = ERRCODE_CANCEL; - code = CODE_CALLBACK_RESULT; - std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME)); - return FI_RC_OK; - } - return FI_RC_ERROR; -} -FIRetCode FaceAuthCA::Cancel(uint64_t reqId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "isRunningFlag is %{public}d", isRunningFlag); - if (isRunningFlag) { - isCancel_ = true; - cancelReqId_ = reqId; - remove(CONFIG_FILENAME); - return FI_RC_OK; - } - return FI_RC_ERROR; -} -void FaceAuthCA::InitErrorCode() -{ - errorCode_[FI_COMPARE_SUCCESS] = ERRCODE_SUCCESS; - errorCode_[FI_FACE_NOT_FOUND] = FACE_NOT_FOUND; - errorCode_[FI_FACE_SCALE_TOO_SMALL] = FACE_SCALE_TOO_SMALL; - errorCode_[FI_FACE_SCALE_TOO_LARGE] = FACE_SCALE_TOO_LARGE; - errorCode_[FI_FACE_OFFSET_LEFT] = FACE_OFFSET_LEFT; - errorCode_[FI_FACE_OFFSET_TOP] = FACE_OFFSET_TOP; - errorCode_[FI_FACE_OFFSET_RIGHT] = FACE_OFFSET_RIGHT; - errorCode_[FI_FACE_OFFSET_BOTTOM] = FACE_OFFSET_BOTTOM; - errorCode_[MG_UNLOCK_ATTR_EYE_OCCLUSION] = FACE_EYE_OCCLUSION; - errorCode_[MG_UNLOCK_ATTR_EYE_CLOSE] = FACE_EYE_CLOSE; - errorCode_[MG_UNLOCK_ATTR_MOUTH_OCCLUSION] = FACE_EYE_MOUTH_OCCLUSION; - errorCode_[MG_UNLOCK_FACE_NOT_COMPLETE] = FACE_NOT_COMPLETE; - errorCode_[FI_FACE_TOO_DARK] = FACE_UNLOCK_FACE_DARKLIGHT; - errorCode_[MG_UNLOCK_HIGHLIGHT] = FACE_UNLOCK_FACE_HIGHTLIGHT; - errorCode_[MG_UNLOCK_HALF_SHADOW] = FACE_UNLOCK_FACE_HALF_SHADOW; - errorCode_[FI_NOT_GAZE] = FACE_NOT_GAZE; - errorCode_[FI_FACE_ROTATE_TOP_RIGHT] = FACE_ROTATE_TOP_RIGHT; - errorCode_[FI_FACE_ROTATE_TOP_LEFT] = FACE_ROTATE_TOP_LEFT; - errorCode_[FI_FACE_ROTATE_BOTTOM_RIGHT] = FACE_ROTATE_BOTTOM_RIGHT; - errorCode_[FI_FACE_ROTATE_BOTTOM_LEFT] = FACE_ROTATE_BOTTOM_LEFT; - errorCode_[FI_FACE_FACE_SHADE] = FACE_WITHOUT_MASK; - errorCode_[FI_FACE_INFO] = FACE_COVERED_WITH_MASK; - errorCode_[MG_UNLOCK_FACE_BAD_QUALITY] = FACE_BAD_QUALITY; - errorCode_[FI_COMPARE_FAIL] = FACE_LIVENESS_FAILURE; - errorCode_[FI_FACE_ROTATE_LEFT] = FACE_ROTATED_LEFT; - errorCode_[MG_UNLOCK_FACE_RISE] = FACE_RISE; - errorCode_[MG_UNLOCK_FACE_ROTATED_RIGHT] = FACE_ROTATED_RIGHT; - errorCode_[MG_UNLOCK_FACE_DOWN] = FACE_DOWN; - errorCode_[MG_UNLOCK_FACE_MULTI] = FACE_MULTI; - errorCode_[MG_UNLOCK_FACE_BLUR] = FACE_BLUR; - errorCode_[FI_ENROLL_FACE_ANGLE_OK] = FACE_ANGLE_BASE; - errorCode_[FI_FACE_INFO] = FACE_ENROLL_INFO_BEGIN; - errorCode_[FI_ENROLL_SUCCESS] = ERRCODE_SUCCESS; - errorCode_[FI_FACE_DETECTED] = FACE_DETECTED; - errorCode_[FI_FACE_OUT_OF_BOUNDS] = FACE_OUT_OF_BOUNDS; - errorCode_[FI_FACE_DARKPIC] = FACE_DARKPIC; - errorCode_[IFACE_ENROLL_HAS_REGISTERED] = ERRCODE_ENROLL_HAS_REGISTERED; -} -void FaceAuthCA::CheckFile(std::string s) -{ - if (s.compare(TEST_ANGLE) == 0) { - GetEnrollAngleResult(); - } - if (s.compare(TEST_ENROLL_SUCCESS) == 0) { - GetEnrollSuccessResult(); - } - if (s.compare(TEST_ENROLL_HAS_REGISTERED) == 0) { - GetEnrollHasRegistered(); - } - if (s.compare(TEST_OVER_MAX_FACES) == 0) { - GetOverMaxFace(); - } - if (s.compare(TEST_CAMERA_FAIL) == 0) { - GetCaremaFail(); - } - if (s.compare(TEST_ANGLE_TIMEOUT) == 0) { - GetAngleTimeout(); - } - if (s.compare(FACE_AUTH_GETRESULT_FAIL_STRING) == 0) { - GetResultFail(); - } - if (s.compare(FACE_AUTH_GETRESULT_TIMEOUT_STRING) == 0) { - GetResultTimeOut(); - } -} -void FaceAuthCA::GetEnrollAngleResult() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - resultInfos_.clear(); - int mapNum = 0; - ResultInfo info; - info.resultCode = FACE_DETECTED; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(mapNum, info)); - for (int32_t i = TEST_ANGLE_START_NUM; i < TEST_ANGLE_MAX_NUM; i += TEST_ANGLE_ADD_NUM) { - info.resultCode = i; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(mapNum, info)); - mapNum++; - } - std::ofstream mystream(FACEID_FILENAME, std::ios::trunc); - if (mystream.is_open()) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetEnrollAngleResult open"); - mystream << (std::to_string(faceId_) + "\n"); - info.param[0] = faceId_; - faceId_++; - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetEnrollAngleResult exist"); - mystream << (std::to_string(1) + "\n"); - info.param[0] = 1; - } - mystream.close(); - for (int32_t j = 1; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - info.resultCode = FI_ENROLL_SUCCESS; - resultInfos_.insert(std::pair(mapNum, info)); -} -void FaceAuthCA::GetEnrollSuccessResult() -{ - resultInfos_.clear(); - ResultInfo info; - int mapNum = 0; - std::ofstream mystream(FACEID_FILENAME, std::ios::trunc); - info.resultCode = FI_FACE_DETECTED; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(mapNum, info)); - mapNum++; - if (mystream.is_open()) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetEnrollSuccessResult open"); - mystream << (std::to_string(faceId_) + "\n"); - info.param[0] = faceId_; - faceId_++; - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetEnrollSuccessResult exist"); - mystream << (std::to_string(1) + "\n"); - info.param[0] = 1; - } - mystream.close(); - for (int32_t j = 1; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - info.resultCode = FI_ENROLL_SUCCESS; - resultInfos_.insert(std::pair(mapNum, info)); -} -void FaceAuthCA::GetEnrollHasRegistered() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = IFACE_ENROLL_HAS_REGISTERED; - resultInfos_.insert(std::pair(0, info)); -} - -void FaceAuthCA::GetOverMaxFace() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = IFACE_OVER_MAX_FACES; - resultInfos_.insert(std::pair(0, info)); -} - -void FaceAuthCA::GetCaremaFail() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = IFACE_CAMERA_FAIL; - resultInfos_.insert(std::pair(0, info)); - info.resultCode = FI_ENROLL_FAIL; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(1, info)); -} -void FaceAuthCA::GetAngleTimeout() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = FI_FACE_DETECTED; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(0, info)); - int32_t num = 1004; - info.resultCode = num; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - resultInfos_.insert(std::pair(1, info)); - - info.resultCode = FACE_HAS_REGISTERED; - for (int32_t j = 0; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - int32_t numTwo = 2; - resultInfos_.insert(std::pair(numTwo, info)); - - info.resultCode = FI_COMPARE_FAIL; - int32_t infoNum = 6; - info.param[0] = infoNum; - for (int32_t j = 1; j < PARAM_RANGE; j++) { - info.param[j] = 0; - } - int32_t num1 = 3; - resultInfos_.insert(std::pair(num1, info)); -} -void FaceAuthCA::GetResultFail() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = FACE_AUTH_GETRESULT_FAIL; - resultInfos_.insert(std::pair(0, info)); -} - -void FaceAuthCA::GetResultTimeOut() -{ - resultInfos_.clear(); - ResultInfo info; - info.resultCode = FACE_AUTH_GETRESULT_TIMEOUT; - resultInfos_.insert(std::pair(0, info)); -} -FIRetCode FaceAuthCA::DynamicInit() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - ReadInitFile(); - FACEAUTH_HILOGI(MODULE_SERVICE, "DynamicInit isInitFail_ is %{public}d", isInitFail_); - if (isInitFail_) { - isInitFail_ = false; - remove(INIT_FILENAME); - return FIRetCode::FI_RC_ERROR; - } - return FIRetCode::FI_RC_OK; -} - -FIRetCode FaceAuthCA::DynamicRelease() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - ReadReleaseFile(); - FACEAUTH_HILOGI(MODULE_SERVICE, "DynamicInit isInitFail_ is %{public}d", isInitFail_); - if (isInitFail_) { - isInitFail_ = false; - remove(RELEASE_FILENAME); - return FIRetCode::FI_RC_ERROR; - } - return FIRetCode::FI_RC_OK; -} -void FaceAuthCA::ReadInitFile() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::ifstream mystream(INIT_FILENAME); - if (!mystream.is_open()) { - FACEAUTH_HILOGI(MODULE_SERVICE, "ReadInitFile open fail"); - return; - } - char casenum[CASE_NUM]; - mystream.getline(casenum, CASE_NUM); - std::string s = casenum; - FACEAUTH_HILOGI(MODULE_SERVICE, "casenum is %{public}s", s.c_str()); - CheckInitFile(s); - mystream.close(); - return; -} - -void FaceAuthCA::ReadReleaseFile() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::ifstream mystream(RELEASE_FILENAME); - if (!mystream.is_open()) { - FACEAUTH_HILOGI(MODULE_SERVICE, "ReadReleaseFile open fail"); - return; - } - char casenum[CASE_NUM]; - mystream.getline(casenum, CASE_NUM); - std::string s = casenum; - FACEAUTH_HILOGI(MODULE_SERVICE, "casenum is %{public}s", s.c_str()); - CheckReleaseFile(s); - mystream.close(); - return; -} -void FaceAuthCA::CheckInitFile(std::string s) -{ - if (s.compare(FACE_AUTH_FAIL_STRING) == 0) { - isInitFail_ = true; - } - if (s.compare(FACE_AUTH_INIT_TIMEOUT_STRING) == 0) { - isInitFail_ = true; - } - if (s.compare(FACE_AUTH_GETRESULT_FAIL_STRING) == 0) { - GetResultFail(); - } - if (s.compare(FACE_AUTH_GETRESULT_TIMEOUT_STRING) == 0) { - GetResultTimeOut(); - } -} - -void FaceAuthCA::CheckReleaseFile(std::string s) -{ - if (s.compare(FACE_AUTH_FAIL_STRING) == 0) { - isInitFail_ = true; - } - if (s.compare(FACE_AUTH_INIT_TIMEOUT_STRING) == 0) { - isInitFail_ = true; - } - if (s.compare(FACE_AUTH_GETRESULT_FAIL_STRING) == 0) { - GetResultFail(); - } - if (s.compare(FACE_AUTH_GETRESULT_TIMEOUT_STRING) == 0) { - GetResultTimeOut(); - } -} -FIRetCode FaceAuthCA::Prepare(HWExeType type) -{ - std::mutex mt; - std::lock_guard lock_l(mt); - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - type_ = type; - resultNum_ = 0; - resultInfos_.clear(); - FACEAUTH_HILOGI(MODULE_SERVICE, "resultInfos_.size is %{public}zu", resultInfos_.size()); - FACEAUTH_HILOGI(MODULE_SERVICE, "Prepare type is %{public}d", type_); - return FIRetCode::FI_RC_OK; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/ca_mock/src/face_auth_func.cpp b/services/ca_mock/src/face_auth_func.cpp deleted file mode 100644 index 3d396d5..0000000 --- a/services/ca_mock/src/face_auth_func.cpp +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_func.h" -#include "adaptor_algorithm.h" -#include "buffer.h" -#include "securec.h" -#include "face_auth_log_wrapper.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -static KeyPair *g_keyPair = nullptr; - -ResultCode GenerateKeyPair() -{ - DestoryKeyPair(g_keyPair); - g_keyPair = GenerateEd25519KeyPair(); - if (g_keyPair == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GenerateEd25519Keypair fail"); - return RESULT_GENERAL_ERROR; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "GenerateKeyPair success"); - return RESULT_SUCCESS; -} - -static ResultCode WriteTlvHead(const AuthAttributeType type, const uint32_t length, Buffer *buf) -{ - int32_t tempType = type; - if (memcpy_s(buf->buf + buf->contentSize, buf->maxSize - buf->contentSize, &tempType, sizeof(tempType)) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "copy type fail"); - return RESULT_BAD_COPY; - } - buf->contentSize += sizeof(tempType); - if (memcpy_s(buf->buf + buf->contentSize, buf->maxSize - buf->contentSize, &length, sizeof(length)) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "copy length fail"); - return RESULT_BAD_COPY; - } - buf->contentSize += sizeof(length); - return RESULT_SUCCESS; -} - -static ResultCode WriteTlv(const AuthAttributeType type, const uint32_t length, const uint8_t *value, Buffer *buf) -{ - if (WriteTlvHead(type, length, buf) != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "write tlv head fail"); - return RESULT_BAD_COPY; - } - if (memcpy_s(buf->buf + buf->contentSize, buf->maxSize - buf->contentSize, value, length) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "copy buffer content fail"); - return RESULT_BAD_COPY; - } - buf->contentSize += length; - return RESULT_SUCCESS; -} - -static Buffer *GetDataTlvContent(uint32_t result, uint64_t scheduleId, uint64_t subType, uint64_t templatedId) -{ - Buffer *ret = CreateBufferBySize(MAX_TLV_LEN); - if (!IsBufferValid(ret)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create buffer fail"); - return nullptr; - } - uint32_t acl = FACE_AUTH_CAPABILITY_LEVEL; - if (WriteTlv(AUTH_RESULT_CODE, sizeof(result), (const uint8_t *)&result, ret) != RESULT_SUCCESS || - WriteTlv(AUTH_TEMPLATE_ID, sizeof(templatedId), (const uint8_t *)&templatedId, ret) != RESULT_SUCCESS || - WriteTlv(AUTH_SCHEDULE_ID, sizeof(scheduleId), (const uint8_t *)&scheduleId, ret) != RESULT_SUCCESS || - WriteTlv(AUTH_SUBTYPE, sizeof(subType), (const uint8_t *)&subType, ret) != RESULT_SUCCESS || - WriteTlv(AUTH_CAPABILITY_LEVEL, sizeof(acl), (const uint8_t *)&acl, ret) != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "write tlv fail"); - DestoryBuffer(ret); - return nullptr; - } - return ret; -} - -ResultCode GenerateRetTlv(uint32_t result, uint64_t scheduleId, uint64_t subType, uint64_t templatedId, - Buffer *retTlv) -{ - if (!IsBufferValid(retTlv)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "param(retTlv) is invalid"); - return RESULT_BAD_PARAM; - } - if (!IsEd25519KeyPairValid(g_keyPair)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "param(g_keyPair) is invalid"); - return RESULT_BAD_PARAM; - } - Buffer *dataContent = GetDataTlvContent(result, scheduleId, subType, templatedId); - if (!IsBufferValid(dataContent)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get data content fail"); - return RESULT_BAD_COPY; - } - Buffer *signContent = nullptr; - if (Ed25519Sign(g_keyPair, dataContent, &signContent) != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "sign data fail"); - DestoryBuffer(dataContent); - return RESULT_GENERAL_ERROR; - } - uint32_t rootLen = TAG_AND_LEN_BYTE + dataContent->contentSize + TAG_AND_LEN_BYTE + ED25519_FIX_SIGN_BUFFER_SIZE; - if (WriteTlvHead(AUTH_ROOT, rootLen, retTlv) != RESULT_SUCCESS || - WriteTlv(AUTH_DATA, dataContent->contentSize, dataContent->buf, retTlv) != RESULT_SUCCESS || - WriteTlv(AUTH_SIGNATURE, signContent->contentSize, signContent->buf, retTlv) != RESULT_SUCCESS) { - FACEAUTH_HILOGE(MODULE_SERVICE, "write tlv fail"); - DestoryBuffer(dataContent); - DestoryBuffer(signContent); - return RESULT_BAD_COPY; - } - DestoryBuffer(dataContent); - DestoryBuffer(signContent); - return RESULT_SUCCESS; -} - -ResultCode SetResultTlv(Buffer *retTlv, std::vector &resultTlv) -{ - resultTlv.resize(retTlv->contentSize); - if (memcpy_s(&resultTlv[0], retTlv->contentSize, retTlv->buf, retTlv->contentSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "copy retTlv to resultTlv fail"); - return RESULT_GENERAL_ERROR; - } - return RESULT_SUCCESS; -} - -ResultCode DoGetExecutorInfo(std::vector &vPubKey, uint32_t &esl, uint64_t &authAbility) -{ - if (!IsEd25519KeyPairValid(g_keyPair)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "key pair is not initted"); - return RESULT_NEED_INIT; - } - Buffer *pubKey = g_keyPair->pubKey; - vPubKey.resize(pubKey->contentSize); - if (memcpy_s(&vPubKey[0], pubKey->contentSize, pubKey->buf, pubKey->contentSize) != EOK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "copy public key fail"); - return RESULT_GENERAL_ERROR; - } - esl = FACE_EXECUTOR_SECURITY_LEVEL; - authAbility = FACE_AUTH_ABILITY; - return RESULT_SUCCESS; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/include/face_auth_camera.h b/services/faceauth/include/face_auth_camera.h deleted file mode 100644 index fc01b00..0000000 --- a/services/faceauth/include/face_auth_camera.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_CAMERA_H -#define FACE_AUTH_CAMERA_H - -#include -#include "input/camera_input.h" -#include "input/camera_manager.h" -#include "system_ability_definition.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthCamera { -public: - static std::shared_ptr GetInstance(); - FaceAuthCamera(); - virtual ~FaceAuthCamera(); - int32_t OpenCamera(sptr producer); - void CloseCamera(); - void SetZoomRatio(float zoom); - void SetFlashMode(camera_flash_mode_enum_t flash); - void SetFocusMode(camera_af_mode_t focus); - void SetExposureMode(camera_ae_mode_t exposure); - -private: - int32_t CreateCamera(sptr producer); - int32_t PrepareCamera(sptr producer); - int32_t CreateDisplayPreviewOutput(sptr &camManagerObj, - sptr producer); - int32_t Start(); - void Stop(); - void Release(); - sptr CreatePreviewOutput(sptr &camManagerObj); - sptr CreateTempPreviewOutput(sptr &camManagerObj); - sptr camInput_; - sptr capSession_; - sptr previewOutput_; - sptr disPlayPreviewOutput_; - static std::mutex mutex_; - static std::shared_ptr instance_; - bool isDisplay_; -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS - -#endif // FACE_AUTH_CAMERA_H diff --git a/services/faceauth/include/face_auth_camera_buffer_listener.h b/services/faceauth/include/face_auth_camera_buffer_listener.h deleted file mode 100644 index 32f88e0..0000000 --- a/services/faceauth/include/face_auth_camera_buffer_listener.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_CAMERA_BUFFER_LISTENER_H -#define FACE_AUTH_CAMERA_BUFFER_LISTENER_H - -#include "surface.h" -#include "surface_buffer.h" -#include "input/camera_input.h" -#include "input/camera_manager.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthCameraBufferListener : public IBufferConsumerListener { -public: - int32_t SendCameraImage(sptr buffer, int64_t timestamp); - void OnBufferAvailable() override; - sptr cameraBuffer_; -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS - -#endif // FACE_AUTH_CAMERA_BUFFER_LISTENER_H \ No newline at end of file diff --git a/services/faceauth/include/face_auth_event.h b/services/faceauth/include/face_auth_event.h deleted file mode 100644 index 475feb9..0000000 --- a/services/faceauth/include/face_auth_event.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_EVENT_H -#define FACE_AUTH_EVENT_H - -#include -#include "face_auth_defines.h" -#include "face_auth_event_handler.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthEvent { -public: - FaceAuthEvent(); - virtual ~FaceAuthEvent(); - static std::shared_ptr GetInstance(); - void HandleTask(const AppExecFwk::InnerEvent::Pointer &event); - void ProcessAuthenticateTask(const AppExecFwk::InnerEvent::Pointer &event); - void ProcessEnrollTask(const AppExecFwk::InnerEvent::Pointer &event); - void ProcessRemoveTask(const AppExecFwk::InnerEvent::Pointer &event); - inline void SetEventHandler(const std::shared_ptr &handler) - { - eventHandler_ = handler; - } - -private: - static std::mutex mutex_; - static std::shared_ptr instance_; - std::shared_ptr eventHandler_; -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACE_AUTH_EVENT_H diff --git a/services/faceauth/include/face_auth_event_handler.h b/services/faceauth/include/face_auth_event_handler.h deleted file mode 100644 index 111203c..0000000 --- a/services/faceauth/include/face_auth_event_handler.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_EVENT_HANDLER_H -#define FACE_AUTH_EVENT_HANDLER_H - -#include "event_handler.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthEventHandler : public AppExecFwk::EventHandler { -public: - explicit FaceAuthEventHandler(const std::shared_ptr &runner); - virtual ~FaceAuthEventHandler(); - void RemoveEvent(const uint64_t reqId); - template - inline bool SendEvent(int32_t innerEventId, std::unique_ptr &&object, Priority priority) - { - return AppExecFwk::EventHandler::SendEvent(AppExecFwk::InnerEvent::Get((uint32_t) innerEventId, object), 0, - priority); - } - inline bool SendEvent(int32_t innerEventId, Priority priority) - { - auto info = std::make_unique(0); - return AppExecFwk::EventHandler::SendEvent(AppExecFwk::InnerEvent::Get((uint32_t) innerEventId, - std::move(info)), 0, priority); - } - void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACE_AUTH_EVENT_HANDLER_H diff --git a/services/faceauth/include/face_auth_executor_callback.h b/services/faceauth/include/face_auth_executor_callback.h deleted file mode 100644 index d8ad412..0000000 --- a/services/faceauth/include/face_auth_executor_callback.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_EXECUTOR_CALLBACK_H -#define FACE_AUTH_EXECUTOR_CALLBACK_H - -#include "face_auth_defines.h" -#include "executor_callback.h" -#include "auth_attributes.h" -#include "auth_executor.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -using pAuthAttributes = std::shared_ptr; -using pAuthMessage = std::shared_ptr; - -class FaceAuthExecutorCallback : public AuthResPool::ExecutorCallback { -public: - FaceAuthExecutorCallback() = default; - virtual ~FaceAuthExecutorCallback() = default; - int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, pAuthAttributes commandAttrs) override; - int32_t OnEndExecute(uint64_t scheduleId, pAuthAttributes consumerAttr) override; - int32_t OnSetProperty(pAuthAttributes properties) override; - void OnMessengerReady(const sptr &messenger) override; - int32_t OnGetProperty(std::shared_ptr conditions, - std::shared_ptr values) override; -}; -} // namespace FaceAuth -} // namespace userIAM -} // namespace OHOS -#endif // FACE_AUTH_EXECUTOR_CALLBACK_H diff --git a/services/faceauth/include/face_auth_get_info_callback.h b/services/faceauth/include/face_auth_get_info_callback.h deleted file mode 100644 index a6d2471..0000000 --- a/services/faceauth/include/face_auth_get_info_callback.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_GET_INFO_CALLBACK_H -#define FACE_AUTH_GET_INFO_CALLBACK_H - -#include "useridm_info.h" -#include "useridm_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthGetInfoCallback : public UserIDM::GetInfoCallback { -public: - void OnGetInfo(std::vector &info) override; - virtual ~FaceAuthGetInfoCallback() = default; -}; -} // namespace FaceAuth -} // namespace userIAM -} // namespace OHOS -#endif // ACE_AUTH_GET_INFO_CALLBACK_H diff --git a/services/faceauth/include/face_auth_manager.h b/services/faceauth/include/face_auth_manager.h deleted file mode 100644 index 220970d..0000000 --- a/services/faceauth/include/face_auth_manager.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_MANAGER_H -#define FACE_AUTH_MANAGER_H - -#include "face_auth_query_callback.h" -#include "face_auth_executor_callback.h" -#include "face_auth_event.h" -#include "face_auth_ca.h" -#include "output/video_output.h" -#include "surface.h" -#include "surface_buffer.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthSequence; -class FaceAuthManager { -public: - static std::shared_ptr GetInstance(); - ~FaceAuthManager() = default; - FaceAuthManager() = default; - int32_t Init(); - int32_t Release(); - void RegisterExecutor(); - void VerifyAuthInfo(); - // about authenticate - ResultCodeForCoAuth AddAuthenticationRequest(const AuthParam ¶m); - void DoAuthenticate(const AuthParam ¶m); - int32_t CancelAuth(const AuthParam ¶m); - // about enroll - ResultCodeForCoAuth AddEnrollmentRequest(const EnrollParam ¶m); - void DoEnroll(const EnrollParam ¶m); - int32_t CancelEnrollment(const EnrollParam ¶m); - // about remove - int32_t AddRemoveRequest(const RemoveParam ¶m); - void DoRemove(const RemoveParam ¶m); - // about algorithm - FIRetCode InitAlgorithm(std::string bundleName); - FIRetCode DoWaitInitAlgorithm(std::future futureobj); - FIRetCode ReleaseAlgorithm(std::string bundleName); - void QueryRegStatus(); - // about messager - void SetExecutorMessenger(const sptr &messager); - void UnfreezeTemplates(std::vector templateIdList); - void FreezeTemplates(std::vector templateIdList); - void SetBufferProducer(sptr &producer); - sptr GetBufferProducer(); -private: - static std::shared_ptr manager_; - static std::mutex mutex_; - static sptr executorMessenger_; - static std::shared_ptr handler_; - static std::shared_ptr runner_; - static std::shared_ptr queryCallback_; - static std::shared_ptr executorCallback_; - std::map bundleNameList_; - sptr producer_; - FaceAuthManager(const FaceAuthManager&) = delete; - FaceAuthManager &operator=(const FaceAuthManager&) = delete; - bool IsAlgorithmInited(); - AlgoResult IsNeedAlgoLoad(std::string bundleName); - AlgoResult IsNeedAlgoRelease(std::string bundleName); - int32_t GenerateEventId(); - void SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, int32_t dstType, pAuthMessage msg); - void Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, pAuthAttributes finalResult); - void HandleAlgorithmResult(const uint64_t &scheduleID, const FaceOperateType &type); - int32_t OpenCamera(sptr producer); - bool GetRandomNum(int32_t *randomNum); - std::pair> GetAlgorithmState(const int &requestId); - FIRetCode DoAlgorithmOperation(const uint64_t &scheduleID); -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACE_AUTH_MANAGER_H diff --git a/services/faceauth/include/face_auth_query_callback.h b/services/faceauth/include/face_auth_query_callback.h deleted file mode 100644 index 1acb18d..0000000 --- a/services/faceauth/include/face_auth_query_callback.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_QUERY_CALLBACK_H -#define FACE_AUTH_QUERY_CALLBACK_H - -#include "face_auth_defines.h" -#include "query_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthQueryCallback : public AuthResPool::QueryCallback { -public: - FaceAuthQueryCallback() = default; - virtual ~FaceAuthQueryCallback() = default; - void OnResult(uint32_t resultCode) override; -}; -} // namespace FaceAuth -} // namespace userIAM -} // namespace OHOS -#endif // FACE_AUTH_QUERY_CALLBACK_H diff --git a/services/faceauth/include/face_auth_req.h b/services/faceauth/include/face_auth_req.h deleted file mode 100644 index 280f842..0000000 --- a/services/faceauth/include/face_auth_req.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FACE_AUTH_REQ_H -#define FACE_AUTH_REQ_H - -#include -#include -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -class FaceAuthReq { -public: - FaceAuthReq(); - virtual ~FaceAuthReq(); - static std::shared_ptr GetInstance(); - bool IsReqNumReachedMax(FaceOperateType type); - void AddReqInfo(FaceReqType reqType, FaceInfo reqInfo); - void RemoveRequireInfo(FaceReqType reqType); - void RemoveAllRequireInfo(); - FaceOperateType GetOperateType(uint32_t eventId); - uint32_t GetEventId(FaceReqType reqType); - bool SetCancelFlagSuccess(FaceReqType reqType, int32_t uId); - bool isCanceled(uint32_t eventId, int32_t uId); - bool FindLocalAuth(); - void PrintReqInfoList(); - -private: - std::map reqInfoList_; - static std::mutex mutex_; - static std::shared_ptr instance_; -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS - -#endif // FACE_AUTH_REQ_H diff --git a/services/faceauth/src/face_auth_camera.cpp b/services/faceauth/src/face_auth_camera.cpp deleted file mode 100644 index d8e28d3..0000000 --- a/services/faceauth/src/face_auth_camera.cpp +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_camera.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_camera_buffer_listener.h" -#include "output/video_output.h" -#include "surface.h" -#include "surface_buffer.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -std::mutex FaceAuthCamera::mutex_; -std::shared_ptr FaceAuthCamera::instance_ = nullptr; -std::shared_ptr FaceAuthCamera::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -FaceAuthCamera::FaceAuthCamera() - : camInput_(nullptr), capSession_(nullptr), previewOutput_(nullptr), disPlayPreviewOutput_(nullptr), - isDisplay_(false) -{} - -FaceAuthCamera::~FaceAuthCamera() -{} - -sptr FaceAuthCamera::CreatePreviewOutput( - sptr &camManagerObj) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - sptr previewBuffer = Surface::CreateSurfaceAsConsumer(); - if (previewBuffer == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "previewBuffer is nullptr"); - return nullptr; - } - previewBuffer->SetDefaultWidthAndHeight(PREVIEW_DEFAULT_WIDTH, PREVIEW_DEFAULT_HEIGHT); - previewBuffer->SetUserData(CameraStandard::CameraManager::surfaceFormat, - std::to_string(OHOS_CAMERA_FORMAT_YCRCB_420_SP)); - sptr listener = new FaceAuthCameraBufferListener(); - listener->cameraBuffer_ = previewBuffer; - previewBuffer->RegisterConsumerListener((sptr &) listener); - sptr previewOutput = camManagerObj->CreatePreviewOutput(previewBuffer); - return previewOutput; -} - -int32_t FaceAuthCamera::CreateCamera(sptr producer) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - sptr camManagerObj = OHOS::CameraStandard::CameraManager::GetInstance(); - if (camManagerObj == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Get Camera Manager Failed"); - return FA_RET_ERROR; - } - std::vector> cameraObjList; - cameraObjList = camManagerObj->GetCameras(); - FACEAUTH_HILOGI(MODULE_SERVICE, "camera list length is %{public}zu", cameraObjList.size()); - if (cameraObjList.empty()) { - FACEAUTH_HILOGE(MODULE_SERVICE, "camera list is empty"); - return FA_RET_ERROR; - } - sptr camera = nullptr; - if (cameraObjList.size() > 0) { - for (unsigned int i = 0; i < cameraObjList.size(); i++) { - if (cameraObjList[i]->GetPosition() == OHOS_CAMERA_POSITION_FRONT && - cameraObjList[i]->GetConnectionType() == OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) { - camera = cameraObjList[i]; - } - } - } - if (camera == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "front facing camera is not found"); - return FA_RET_ERROR; - } - camInput_ = camManagerObj->CreateCameraInput(camera); - if (camInput_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Create Camera Input Failed"); - return FA_RET_ERROR; - } - int32_t intResult = PrepareCamera(producer); - if (intResult != 0) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Prepare Camera Failed"); - return FA_RET_ERROR; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "end"); - return FA_RET_OK; -} - -int32_t FaceAuthCamera::PrepareCamera(sptr producer) -{ - int32_t intResult = FA_RET_OK; - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - sptr camManagerObj = CameraStandard::CameraManager::GetInstance(); - if (camManagerObj == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "camManagerObj is null"); - return FA_RET_ERROR; - } - - capSession_ = camManagerObj->CreateCaptureSession(); - if ((capSession_ == nullptr) || (camInput_ == nullptr)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create capture session failed"); - return FA_RET_ERROR; - } - capSession_->BeginConfig(); - - intResult = capSession_->AddInput(camInput_); - if (intResult != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "AddInput Failed"); - return FA_RET_ERROR; - } - previewOutput_ = CreatePreviewOutput(camManagerObj); - if (previewOutput_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to create PreviewOutput"); - return FA_RET_ERROR; - } - intResult = capSession_->AddOutput(previewOutput_); - if (intResult != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to Add Preview Output"); - return FA_RET_ERROR; - } - - if (isDisplay_) { - if (CreateDisplayPreviewOutput(camManagerObj, producer) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to create display preview output"); - return FA_RET_ERROR; - } - FACEAUTH_HILOGE(MODULE_SERVICE, "CreateDisplayPreviewOutput ok"); - } - intResult = capSession_->CommitConfig(); - if (intResult != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to Commit config"); - return FA_RET_ERROR; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "end"); - return FA_RET_OK; -} - -int32_t FaceAuthCamera::OpenCamera(sptr producer) -{ - if (producer != nullptr) { - isDisplay_ = true; - } - if (CreateCamera(producer) != FA_RET_OK) { - return FA_RET_ERROR; - } - return Start(); -} - -void FaceAuthCamera::CloseCamera() -{ - Stop(); - Release(); - return; -} - -int32_t FaceAuthCamera::Start() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (capSession_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "capSession_ is nullptr"); - return FA_RET_ERROR; - } - int32_t intResult = capSession_->Start(); - if (intResult != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Start capture session failed"); - return FA_RET_ERROR; - } - return FA_RET_OK; -} - -void FaceAuthCamera::Stop() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (capSession_ != nullptr) { - capSession_->Stop(); - } - FACEAUTH_HILOGI(MODULE_SERVICE, "end"); - return; -} - -void FaceAuthCamera::Release() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (camInput_ != nullptr) { - camInput_->Release(); - camInput_ = nullptr; - } - if (previewOutput_ != nullptr) { - ((sptr &) previewOutput_)->Release(); - previewOutput_ = nullptr; - } - if (isDisplay_) { - if (disPlayPreviewOutput_ != nullptr) { - ((sptr &) disPlayPreviewOutput_)->Release(); - disPlayPreviewOutput_ = nullptr; - } - isDisplay_ = false; - } - if (capSession_ != nullptr) { - capSession_->Release(); - capSession_ = nullptr; - } - if (instance_ != nullptr) { - instance_.reset(); - } - FACEAUTH_HILOGI(MODULE_SERVICE, "end"); - return; -} - -int32_t FaceAuthCamera::CreateDisplayPreviewOutput(sptr &camManagerObj, - sptr producer) -{ - int32_t intResult = FA_RET_OK; - FACEAUTH_HILOGI(MODULE_SERVICE, "Use UI's producer"); - disPlayPreviewOutput_ = camManagerObj->CreatePreviewOutput(producer, OHOS_CAMERA_FORMAT_YCRCB_420_SP); - if (disPlayPreviewOutput_ == nullptr) { - isDisplay_ = false; - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to create PreviewOutput for UI"); - return FA_RET_ERROR; - } - intResult = capSession_->AddOutput(disPlayPreviewOutput_); - if (intResult != FA_RET_OK) { - isDisplay_ = false; - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to Add PreviewOutput For UI"); - return FA_RET_ERROR; - } - return FA_RET_OK; -} - -void FaceAuthCamera::SetZoomRatio(float zoom) -{ - if (camInput_ != nullptr) { - ((sptr &) camInput_)->LockForControl(); - ((sptr &) camInput_)->SetZoomRatio(zoom); - ((sptr &) camInput_)->UnlockForControl(); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "camInput is null, failed to set zoom ratio"); - } - return; -} - -void FaceAuthCamera::SetFlashMode(camera_flash_mode_enum_t flash) -{ - if (camInput_ != nullptr) { - ((sptr &) camInput_)->LockForControl(); - ((sptr &) camInput_)->SetFlashMode(flash); - ((sptr &) camInput_)->UnlockForControl(); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "camInput is null, failed to set flash mode"); - } - return; -} - -void FaceAuthCamera::SetFocusMode(camera_af_mode_t focus) -{ - if (camInput_ != nullptr) { - ((sptr &) camInput_)->LockForControl(); - ((sptr &) camInput_)->SetFocusMode(focus); - ((sptr &) camInput_)->UnlockForControl(); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "camInput is null, failed to set focus mode"); - } - return; -} - -void FaceAuthCamera::SetExposureMode(camera_ae_mode_t exposure) -{ - if (camInput_ != nullptr) { - ((sptr &) camInput_)->LockForControl(); - ((sptr &) camInput_)->SetExposureMode(exposure); - ((sptr &) camInput_)->UnlockForControl(); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "camInput is null, failed to set exposure mode"); - } - return; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_camera_buffer_listener.cpp b/services/faceauth/src/face_auth_camera_buffer_listener.cpp deleted file mode 100644 index 7284d89..0000000 --- a/services/faceauth/src/face_auth_camera_buffer_listener.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_camera_buffer_listener.h" -#include -#include "face_auth_log_wrapper.h" -#include "face_auth_ca.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -int32_t FaceAuthCameraBufferListener::SendCameraImage(OHOS::sptr buffer, int64_t timestamp) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "receive image from camera"); - if (buffer == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "buffer is nullptr"); - return -1; - } - CameraImage image = {}; - image.image = static_cast(buffer->GetVirAddr()); - image.imageSize = buffer->GetSize(); - BufferHandle *bufferHandle = buffer->GetBufferHandle(); - image.width = static_cast(bufferHandle->width); - image.height = static_cast(bufferHandle->height); - image.stride = static_cast(bufferHandle->stride); - image.timestamp = timestamp; - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA is nullptr"); - return -1; - } - faceAuthCA->TransferImageToAlgorithm(image); - return 0; -} - -void FaceAuthCameraBufferListener::OnBufferAvailable() -{ - if (cameraBuffer_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "cameraBuffer_ is nullptr"); - return; - } - int32_t flushFence = 0; - int64_t timestamp = 0; - OHOS::Rect damage; - OHOS::sptr buffer = nullptr; - cameraBuffer_->AcquireBuffer(buffer, flushFence, timestamp, damage); - if (buffer != nullptr) { - SendCameraImage(buffer, timestamp); - cameraBuffer_->ReleaseBuffer(buffer, -1); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "AcquireBuffer failed"); - } -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS \ No newline at end of file diff --git a/services/faceauth/src/face_auth_event.cpp b/services/faceauth/src/face_auth_event.cpp deleted file mode 100644 index 2a3678c..0000000 --- a/services/faceauth/src/face_auth_event.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_event.h" -#include -#include "face_auth_log_wrapper.h" -#include "face_auth_defines.h" -#include "face_auth_event_handler.h" -#include "face_auth_manager.h" -#include "face_auth_req.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -std::mutex FaceAuthEvent::mutex_; -std::shared_ptr FaceAuthEvent::instance_ = nullptr; - -FaceAuthEvent::FaceAuthEvent() -{} - -FaceAuthEvent::~FaceAuthEvent() -{} - -std::shared_ptr FaceAuthEvent::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -void FaceAuthEvent::HandleTask(const AppExecFwk::InnerEvent::Pointer &event) -{ - if (event == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "event is nullptr"); - return; - } - uint32_t event_id = event->GetInnerEventId(); - FACEAUTH_HILOGI(MODULE_SERVICE, "inner event id obtained: %{public}u", event_id); - FaceOperateType operateType = FaceAuthReq::GetInstance()->GetOperateType(event_id); - FACEAUTH_HILOGI(MODULE_SERVICE, "operateType is %{public}d", operateType); - switch (operateType) { - case FACE_OPERATE_TYPE_LOCAL_AUTH: { - ProcessAuthenticateTask(event); - break; - } - case FACE_OPERATE_TYPE_ENROLL: { - ProcessEnrollTask(event); - break; - } - case FACE_OPERATE_TYPE_DEL: { - ProcessRemoveTask(event); - break; - } - default: { - FACEAUTH_HILOGE(MODULE_SERVICE, "operateType is invalid, %{public}d", operateType); - break; - } - } - return; -} - -void FaceAuthEvent::ProcessEnrollTask(const AppExecFwk::InnerEvent::Pointer &event) -{ - auto object = event->GetUniqueObject(); - EnrollParam info = *object; - uint64_t uId = info.callerUID; - uint32_t event_id = event->GetInnerEventId(); - bool isCanceled = FaceAuthReq::GetInstance()->isCanceled(event_id, uId); - if (isCanceled) { - FaceReqType reqType; - reqType.reqId = info.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_ENROLL; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - return; - } - FaceAuthManager::GetInstance()->DoEnroll(info); - return; -} - -void FaceAuthEvent::ProcessAuthenticateTask(const AppExecFwk::InnerEvent::Pointer &event) -{ - auto object = event->GetUniqueObject(); - AuthParam info = *object; - uint64_t uId = info.callerUID; - uint32_t event_id = event->GetInnerEventId(); - bool isCanceled = FaceAuthReq::GetInstance()->isCanceled(event_id, uId); - if (isCanceled) { - FaceReqType reqType; - reqType.reqId = info.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_LOCAL_AUTH; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - return; - } - FaceAuthManager::GetInstance()->DoAuthenticate(info); - return; -} - -void FaceAuthEvent::ProcessRemoveTask(const AppExecFwk::InnerEvent::Pointer &event) -{ - auto object = event->GetUniqueObject(); - RemoveParam info = *object; - FaceAuthManager::GetInstance()->DoRemove(info); - return; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_event_handler.cpp b/services/faceauth/src/face_auth_event_handler.cpp deleted file mode 100644 index d3b37f3..0000000 --- a/services/faceauth/src/face_auth_event_handler.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_event_handler.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_defines.h" -#include "face_auth_event.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -FaceAuthEventHandler::FaceAuthEventHandler(const std::shared_ptr &runner) - : AppExecFwk::EventHandler(runner) -{} - -FaceAuthEventHandler::~FaceAuthEventHandler() -{} - -void FaceAuthEventHandler::RemoveEvent(const uint64_t reqId) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "remove event %{public}s", getMaskedString(reqId).c_str()); - EventHandler::RemoveEvent(reqId); - return; -} - -void FaceAuthEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) -{ - if (event == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "event is nullptr"); - return; - } - FaceAuthEvent::GetInstance()->HandleTask(event); - return; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_executor_callback.cpp b/services/faceauth/src/face_auth_executor_callback.cpp deleted file mode 100644 index 998d5a8..0000000 --- a/services/faceauth/src/face_auth_executor_callback.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_executor_callback.h" -#include "securec.h" -#include "face_auth_manager.h" -#include "face_auth_log_wrapper.h" -#include "co_auth_info_define.h" -#include "face_auth_ca.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -int32_t FaceAuthExecutorCallback::OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, - pAuthAttributes commandAttrs) -{ - (void)(publicKey); - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr manager = FaceAuthManager::GetInstance(); - if (manager == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "face auth manager is nullptr"); - return FA_RET_ERROR; - } - uint32_t command = 0; - commandAttrs->GetUint32Value(AUTH_SCHEDULE_MODE, command); - FACEAUTH_HILOGD(MODULE_SERVICE, "command = %{public}u", command); - uint64_t templateId = 0; - commandAttrs->GetUint64Value(AUTH_TEMPLATE_ID, templateId); - ResultCodeForCoAuth ret = ResultCodeForCoAuth::GENERAL_ERROR; - switch (command) { - case FACE_COMMAND_ENROLL: { - EnrollParam data = {}; - data.scheduleID = scheduleId; - data.templateID = templateId; - data.producer = manager->GetBufferProducer(); - ret = manager->AddEnrollmentRequest(data); - break; - } - case FACE_COMMAND_AUTH: { - AuthParam data = {}; - data.scheduleID = scheduleId; - data.templateID = templateId; - ret = manager->AddAuthenticationRequest(data); - break; - } - default: - FACEAUTH_HILOGE(MODULE_SERVICE, "Command id %{public}u is not supported", command); - break; - } - return static_cast(ret); -} -int32_t FaceAuthExecutorCallback::OnEndExecute(uint64_t scheduleId, pAuthAttributes consumerAttr) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr manager = FaceAuthManager::GetInstance(); - if (manager == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "face auth manager is nullptr"); - return FA_RET_ERROR; - } - uint32_t command = 0; - int32_t ret = FA_RET_OK; - consumerAttr->GetUint32Value(AUTH_SCHEDULE_MODE, command); - FACEAUTH_HILOGD(MODULE_SERVICE, "command = %{public}u", command); - switch (command) { - case FACE_COMMAND_CANCEL_ENROLL: { - EnrollParam data = {}; - data.scheduleID = scheduleId; - ret = manager->CancelEnrollment(data); - if (ret != FA_RET_OK) { - return FA_RET_GENERAL_ERROR; - } - break; - } - case FACE_COMMAND_CANCEL_AUTH: { - AuthParam data = {}; - data.scheduleID = scheduleId; - ret = manager->CancelAuth(data); - if (ret != FA_RET_OK) { - return FA_RET_GENERAL_ERROR; - } - break; - } - default: - FACEAUTH_HILOGE(MODULE_SERVICE, "Command id %{public}u is not supported", command); - break; - } - return ret; -} - -void FaceAuthExecutorCallback::OnMessengerReady(const sptr &messenger) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr manager = FaceAuthManager::GetInstance(); - if (manager == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "manager instance is null"); - return; - } - manager->SetExecutorMessenger(messenger); - manager->VerifyAuthInfo(); -} - -int32_t FaceAuthExecutorCallback::OnSetProperty(pAuthAttributes properties) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (properties == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "properties is nullptr"); - return FA_RET_ERROR; - } - std::shared_ptr manager = FaceAuthManager::GetInstance(); - if (manager == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "face auth manager is nullptr"); - return FA_RET_ERROR; - } - uint32_t command = 0; - properties->GetUint32Value(AUTH_PROPERTY_MODE, command); - FACEAUTH_HILOGD(MODULE_SERVICE, "command = %{public}u", command); - uint64_t scheduleID = 0; - properties->GetUint64Value(AUTH_SCHEDULE_ID, scheduleID); - uint64_t templateID = 0; - properties->GetUint64Value(AUTH_TEMPLATE_ID, templateID); - std::vector callerName; - properties->GetUint8ArrayValue(ALGORITHM_INFO, callerName); - std::vector templateIdList; - properties->GetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, templateIdList); - std::string bundleName = ""; - bundleName.assign(callerName.begin(), callerName.end()); - switch (command) { - case FACE_COMMAND_REMOVE: { - RemoveParam data = {}; - data.scheduleID = scheduleID; - data.templateID = templateID; - manager->AddRemoveRequest(data); - break; - } - case FACE_COMMAND_PROPERMODE_FREEZE: - manager->FreezeTemplates(templateIdList); - break; - case FACE_COMMAND_PROPERMODE_UNFREEZE: - manager->UnfreezeTemplates(templateIdList); - break; - case FACE_COMMAND_INIT_ALGORITHM: - manager->InitAlgorithm(bundleName); - break; - case FACE_COMMAND_RELEASE_ALGORITHM: - manager->ReleaseAlgorithm(bundleName); - break; - default: - FACEAUTH_HILOGI(MODULE_SERVICE, "other command.command = %{public}u", command); - break; - } - return FA_RET_OK; -} -int32_t FaceAuthExecutorCallback::OnGetProperty(std::shared_ptr conditions, - std::shared_ptr values) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - if (values == nullptr || conditions == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "bad param"); - return FA_RET_ERROR; - } - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA is nullptr"); - return FA_RET_ERROR; - } - - /* set command 0:delete 1:Query credential information */ - uint32_t command; - if (conditions->GetUint32Value(AUTH_PROPERTY_MODE, command) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetUint32Value fail"); - return FA_RET_ERROR; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "command is %{public}u", command); - if (command == FACE_COMMAND_QUERY_CREDENTIAL) { - /* get templateId */ - uint64_t templateId; - if (conditions->GetUint64Value(AUTH_TEMPLATE_ID, templateId) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthService::OnGetProperty GetUint64Value"); - return FA_RET_ERROR; - } - /* Query credential information */ - FaceCredentialInfo info; - faceAuthCA->GetFaceInfo(templateId, info); - if (values->SetUint64Value(AUTH_SUBTYPE, info.subType) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthService::OnGetProperty SetUint64Value"); - return FA_RET_ERROR; - } - /* send remainTimes FreezingTime */ - if (values->SetUint32Value(AUTH_REMAIN_TIME, info.freezingTime)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthService::OnGetProperty SetUint32ArrayValue"); - return FA_RET_ERROR; - } - if (values->SetUint32Value(AUTH_REMAIN_COUNT, info.remainTimes)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthService::OnGetProperty SetUint32ArrayValue"); - return FA_RET_ERROR; - } - } - return FA_RET_OK; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_get_info_callback.cpp b/services/faceauth/src/face_auth_get_info_callback.cpp deleted file mode 100644 index e36ec7b..0000000 --- a/services/faceauth/src/face_auth_get_info_callback.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_get_info_callback.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_ca.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -void FaceAuthGetInfoCallback::OnGetInfo(std::vector &info) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "credentialInfo length %{public}zu", info.size()); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA is null"); - return; - } - std::vector templateIdList; - for (auto infoItem : info) { - templateIdList.push_back(infoItem.templateId); - } - faceAuthCA->VerifyTemplateData(templateIdList); -}; -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_manager.cpp b/services/faceauth/src/face_auth_manager.cpp deleted file mode 100644 index 18f4292..0000000 --- a/services/faceauth/src/face_auth_manager.cpp +++ /dev/null @@ -1,742 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_manager.h" -#include -#include -#include -#include -#include -#include -#include "securec.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_event_handler.h" -#include "auth_executor_registry.h" -#include "useridm_client.h" -#include "useridm_info.h" -#include "face_auth_get_info_callback.h" -#include "auth_message.h" -#include "co_auth_info_define.h" -#include "face_auth_thread_pool.h" -#include "face_auth_req.h" -#include "face_auth_camera.h" -#include "face_auth_defines.h" -#include "return_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -const int RAND_NUM_BITS = 32; -const int TOP = -1; -const int BOTTOM = 0; -const int INVALID_EVENT_ID = -1; -static const std::string FACE_LOCAL_INIT_ALGO_NAME = "face_auth_local_init"; -std::shared_ptr FaceAuthManager::manager_ = nullptr; -std::mutex FaceAuthManager::mutex_; -sptr FaceAuthManager::executorMessenger_; -std::shared_ptr FaceAuthManager::handler_ = nullptr; -std::shared_ptr FaceAuthManager::runner_ = nullptr; -std::shared_ptr FaceAuthManager::queryCallback_ = nullptr; -std::shared_ptr FaceAuthManager::executorCallback_ = nullptr; -static void CheckSystemAbility(); - -std::shared_ptr FaceAuthManager::GetInstance() -{ - if (manager_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (manager_ == nullptr) { - manager_ = std::make_shared(); - } - } - return manager_; -} - -int32_t FaceAuthManager::Init() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::string threadName("FaceAuthEventRunner"); - runner_ = AppExecFwk::EventRunner::Create(threadName); - if (runner_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "failed to create runner"); - return FA_RET_ERROR; - } - runner_->Run(); - handler_ = std::make_shared(runner_); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create FaceAuthCA instance failed"); - return FA_RET_ERROR; - } - if (FA_RET_OK != faceAuthCA->Init()) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Init CA failed"); - return FA_RET_ERROR; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "init Success"); - std::thread checkThread(OHOS::UserIAM::FaceAuth::CheckSystemAbility); - checkThread.join(); - QueryRegStatus(); - return FA_RET_OK; -} - -void CheckSystemAbility() -{ - const int checkTimes = 3; - const int sleepTime = 1; - sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sam == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Failed to get system ability manager"); - return; - } - for (int i = 0; i < checkTimes; i++) { - bool isExist = false; - sam->CheckSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_AUTHEXECUTORMGR, isExist); - if (!isExist) { - FACEAUTH_HILOGI(MODULE_SERVICE, "AUTHEXECUTORMGR is not exist, start ability failed, to do next"); - } else { - FACEAUTH_HILOGI(MODULE_SERVICE, "AUTHEXECUTORMGR is exist, start AUTHEXECUTORMGR ability success"); - return; - } - if (i < checkTimes - 1) { - FACEAUTH_HILOGI(MODULE_SERVICE, "begin sleep"); - sleep(sleepTime); - FACEAUTH_HILOGI(MODULE_SERVICE, "end sleep"); - } - } - FACEAUTH_HILOGE(MODULE_SERVICE, "start AUTHEXECUTORMGR ability all failed"); -} - -void FaceAuthManager::QueryRegStatus() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr executorInfo = std::make_shared(); - std::vector pubKey; - uint32_t esl = 0; - uint64_t authAbility = 0; - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create FaceAuthCA instance failed"); - return; - } - int32_t ret = faceAuthCA->GetExecutorInfo(pubKey, esl, authAbility); - if (FA_RET_OK != ret) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetExecutorInfo failed"); - return; - } - executorInfo->SetAuthType(FACE); - executorInfo->SetExecutorType(TYPE_ALL_IN_ONE); - - if (queryCallback_== nullptr) { - queryCallback_ = std::make_shared(); - } - AuthResPool::AuthExecutorRegistry::GetInstance().QueryStatus(*executorInfo, queryCallback_); -} - -void FaceAuthManager::RegisterExecutor() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "create FaceAuthCA instance failed"); - return; - } - std::vector pubKey; - uint32_t esl = 0; - uint64_t authAbility = 0; - int32_t ret = faceAuthCA->GetExecutorInfo(pubKey, esl, authAbility); - if (FA_RET_OK != ret) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetExecutorInfo failed"); - return; - } - std::shared_ptr executorInfo = std::make_shared(); - executorInfo->SetPublicKey(pubKey); - executorInfo->SetExecutorSecLevel(static_cast(esl)); - executorInfo->SetAuthAbility(authAbility); - executorInfo->SetAuthType(FACE); - executorInfo->SetExecutorType(TYPE_ALL_IN_ONE); - executorCallback_ = std::make_shared(); - uint64_t regRet = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, executorCallback_); - if (regRet != 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "FaceAuthInitSeq::RegisterExecutor successful.executor id = %{public}s", - getMaskedString(regRet).c_str()); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthInitSeq::RegisterExecutor failed"); - } - return; -} - -void FaceAuthManager::VerifyAuthInfo() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - const int32_t ALL_INFO_GET_USER_ID = -1; - - std::shared_ptr getInfoCallback = std::make_shared(); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(ALL_INFO_GET_USER_ID, UserIDM::AuthType::FACE, - getInfoCallback); - if (ret == 0) { - FACEAUTH_HILOGI(MODULE_SERVICE, "get auth info success"); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "get auth info failed"); - } - return; -} - -int32_t FaceAuthManager::Release() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA != nullptr) { - faceAuthCA->Close(); - } - if (runner_ != nullptr) { - runner_.reset(); - } - return FA_RET_OK; -} - -ResultCodeForCoAuth FaceAuthManager::AddAuthenticationRequest(const AuthParam ¶m) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoAuth]scheduleID = %{public}s", getMaskedString(param.scheduleID).c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoAuth]templateID = %{public}s", getMaskedString(param.templateID).c_str()); - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_LOCAL_AUTH; - if (FaceAuthReq::GetInstance()->IsReqNumReachedMax(FACE_OPERATE_TYPE_LOCAL_AUTH)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Auth request num exceeds limit"); - return ResultCodeForCoAuth::BUSY; - } - FaceInfo faceInfo = {}; - faceInfo.eventId = GenerateEventId(); - if (faceInfo.eventId == INVALID_EVENT_ID) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceInfo.eventId is invalid"); - return ResultCodeForCoAuth::GENERAL_ERROR; - } - faceInfo.uId = param.callerUID; - FaceAuthReq::GetInstance()->AddReqInfo(reqType, faceInfo); - FaceAuthEventHandler::Priority priority = FaceAuthEventHandler::Priority::LOW; - auto authInfo = std::make_unique(param); - handler_->SendEvent(faceInfo.eventId, std::move(authInfo), priority); - return ResultCodeForCoAuth::SUCCESS; -} - -void FaceAuthManager::DoAuthenticate(const AuthParam ¶m) -{ - ReturnCallback removeRequireInfoWhenReturn([param]() { - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_LOCAL_AUTH; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - }); - this->InitAlgorithm(FACE_LOCAL_INIT_ALGO_NAME); - ReturnCallback releaseAlgorithmWhenReturn([this]() { - this->ReleaseAlgorithm(FACE_LOCAL_INIT_ALGO_NAME); - }); - if (OpenCamera(nullptr) != FA_RET_OK) { - // RK3568 no support camera, temporary ignore error - FACEAUTH_HILOGE(MODULE_SERVICE, "Ignore open camera fail"); - } - ReturnCallback closeCameraWhenReturn([]() { - std::shared_ptr faceAuthCamera = FaceAuthCamera::GetInstance(); - if (faceAuthCamera == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "face auth camera is null"); - return; - } - faceAuthCamera->CloseCamera(); - }); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA instance is null"); - return; - } - int32_t ret = faceAuthCA->StartAlgorithmOperation(Auth, AlgorithmParam { - .templateId = param.templateID, - .scheduleId = param.scheduleID - }); - if (ret != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "StartAlgorithmOperation failed"); - return; - } - ret = DoAlgorithmOperation(param.scheduleID); - if (ret != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "DoAlgorithmOperation failed"); - } - HandleAlgorithmResult(param.scheduleID, FACE_OPERATE_TYPE_LOCAL_AUTH); -} - -ResultCodeForCoAuth FaceAuthManager::AddEnrollmentRequest(const EnrollParam ¶m) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoEnroll]scheduleID = %{public}s", getMaskedString(param.scheduleID).c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoEnroll]templateID = %{public}s", getMaskedString(param.templateID).c_str()); - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_ENROLL; - if (FaceAuthReq::GetInstance()->IsReqNumReachedMax(FACE_OPERATE_TYPE_ENROLL)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Enroll request num exceeds limit"); - return ResultCodeForCoAuth::BUSY; - } - FaceInfo faceInfo = {}; - faceInfo.eventId = GenerateEventId(); - if (faceInfo.eventId == INVALID_EVENT_ID) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceInfo.eventId is invalid"); - return ResultCodeForCoAuth::GENERAL_ERROR; - } - faceInfo.uId = param.callerUID; - FaceAuthReq::GetInstance()->AddReqInfo(reqType, faceInfo); - FaceAuthEventHandler::Priority priority = FaceAuthEventHandler::Priority::HIGH; - auto authInfo = std::make_unique(param); - handler_->SendEvent(faceInfo.eventId, std::move(authInfo), priority); - return ResultCodeForCoAuth::SUCCESS; -} - -void FaceAuthManager::DoEnroll(const EnrollParam ¶m) -{ - ReturnCallback removeRequireInfoWhenReturn([param]() { - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_ENROLL; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - }); - this->InitAlgorithm(FACE_LOCAL_INIT_ALGO_NAME); - ReturnCallback releaseAlgorithmWhenReturn([this]() { - this->ReleaseAlgorithm(FACE_LOCAL_INIT_ALGO_NAME); - }); - if (OpenCamera(param.producer) != FA_RET_OK) { - // RK3568 no support camera, temporary ignore error - FACEAUTH_HILOGI(MODULE_SERVICE, "Ignore open camera fail"); - } - ReturnCallback closeCameraWhenReturn([]() { - std::shared_ptr faceAuthCamera = FaceAuthCamera::GetInstance(); - if (faceAuthCamera == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "face auth camera is null"); - return; - } - faceAuthCamera->CloseCamera(); - }); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA instance is null"); - return; - } - int32_t ret = faceAuthCA->StartAlgorithmOperation(Enroll, AlgorithmParam { - .templateId = param.templateID, - .scheduleId = param.scheduleID - }); - if (ret != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "StartAlgorithmOperation failed"); - return; - } - ret = DoAlgorithmOperation(param.scheduleID); - if (ret != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "DoAlgorithmOperation failed"); - } - HandleAlgorithmResult(param.scheduleID, FACE_OPERATE_TYPE_ENROLL); -} - -int32_t FaceAuthManager::AddRemoveRequest(const RemoveParam ¶m) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoRemove]scheduleID = %{public}s", getMaskedString(param.scheduleID).c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "[DoRemove]templateID = %{public}s", getMaskedString(param.templateID).c_str()); - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_DEL; - if (FaceAuthReq::GetInstance()->IsReqNumReachedMax(FACE_OPERATE_TYPE_DEL)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Remove request num exceeds limit"); - return FA_RET_ERROR; - } - FaceInfo faceInfo = {}; - faceInfo.eventId = GenerateEventId(); - if (faceInfo.eventId == INVALID_EVENT_ID) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceInfo.eventId is invalid"); - return FA_RET_ERROR; - } - faceInfo.uId = param.callerUID; - FaceAuthReq::GetInstance()->AddReqInfo(reqType, faceInfo); - FaceAuthEventHandler::Priority priority = FaceAuthEventHandler::Priority::IMMEDIATE; - auto authInfo = std::make_unique(param); - handler_->SendEvent(faceInfo.eventId, std::move(authInfo), priority); - return FA_RET_OK; -} - -void FaceAuthManager::DoRemove(const RemoveParam ¶m) -{ - ReturnCallback removeRequireInfoWhenReturn([param]() { - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_DEL; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - }); - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA instance is null"); - return; - } - int32_t ret = faceAuthCA->DeleteTemplate(param.templateID); - if (FA_RET_OK == ret) { - FACEAUTH_HILOGI(MODULE_SERVICE, "Remove success"); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "Remove failed"); - } -} - -void FaceAuthManager::HandleAlgorithmResult(const uint64_t &scheduleID, const FaceOperateType &type) -{ - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGI(MODULE_SERVICE, "faceAuthCA is null"); - return; - } - AlgorithmResult retResult; - faceAuthCA->FinishAlgorithmOperation(retResult); - FACEAUTH_HILOGI(MODULE_SERVICE, "Face auth result = %{public}d", retResult.result); - pAuthAttributes authAttributes = std::make_shared(); - authAttributes->SetUint32Value(AUTH_RESULT_CODE, 0); - authAttributes->SetUint8ArrayValue(AUTH_RESULT, retResult.coauthMsg); - FaceReqType reqType = {}; - reqType.reqId = scheduleID; - reqType.operateType = type; - FaceAuthReq::GetInstance()->RemoveRequireInfo(reqType); - Finish(scheduleID, TYPE_ALL_IN_ONE, retResult.result, authAttributes); -} - -int32_t FaceAuthManager::CancelAuth(const AuthParam ¶m) -{ - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_LOCAL_AUTH; - int32_t uId = param.callerUID; - bool isSuccess = FaceAuthReq::GetInstance()->SetCancelFlagSuccess(reqType, uId); - if (!isSuccess) { - FACEAUTH_HILOGE(MODULE_SERVICE, "CancelAuth failed, reqId: %{public}s,", - getMaskedString(reqType.reqId).c_str()); - return FA_RET_ERROR; - } - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthCA instance is null"); - return FA_RET_ERROR; - } - int32_t result = faceAuthCA->CancelAlgorithmOperation(); - if (result == FA_RET_OK) { - FACEAUTH_HILOGI(MODULE_SERVICE, "CancelAlgorithmOperation success"); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "CancelAlgorithmOperation failed"); - } - return result; -} - -int32_t FaceAuthManager::CancelEnrollment(const EnrollParam ¶m) -{ - FaceReqType reqType = {}; - reqType.reqId = param.scheduleID; - reqType.operateType = FACE_OPERATE_TYPE_ENROLL; - int32_t uId = param.callerUID; - bool isSuccess = FaceAuthReq::GetInstance()->SetCancelFlagSuccess(reqType, uId); - if (!isSuccess) { - FACEAUTH_HILOGE(MODULE_SERVICE, "CancelEnrollment failed, reqId: %{public}s", - getMaskedString(reqType.reqId).c_str()); - return FA_RET_ERROR; - } - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FaceAuthCA instance is null"); - return FA_RET_ERROR; - } - int32_t result = faceAuthCA->CancelAlgorithmOperation(); - if (result == FA_RET_OK) { - FACEAUTH_HILOGI(MODULE_SERVICE, "CancelAlgorithmOperation success"); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "CancelAlgorithmOperation failed"); - } - return result; -} - -void FaceAuthManager::SetExecutorMessenger(const sptr &messager) -{ - executorMessenger_ = messager; -} -void FaceAuthManager::SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, int32_t dstType, - pAuthMessage msg) -{ - if (executorMessenger_ != nullptr) { - executorMessenger_->SendData(scheduleId, transNum, srcType, dstType, msg); - } else { - FACEAUTH_HILOGI(MODULE_SERVICE, "executorMessenger_ is null"); - } -} - -void FaceAuthManager::Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, pAuthAttributes finalResult) -{ - if (executorMessenger_ != nullptr) { - executorMessenger_->Finish(scheduleId, srcType, resultCode, finalResult); - } else { - FACEAUTH_HILOGI(MODULE_SERVICE, "executorMessenger_ is null"); - } -} - -FIRetCode FaceAuthManager::InitAlgorithm(std::string bundleName) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "Init, bundleName:%{public}s", bundleName.c_str()); - AlgoResult result = IsNeedAlgoLoad(bundleName); - if (result == AR_EMPTY) { - auto promiseObj = std::make_shared>(); - auto futureObj = promiseObj->get_future(); - FaceAuthThreadPool::GetInstance()->AddTask([promiseObj]() { - promiseObj->set_value(FaceAuthCA::GetInstance()->LoadAlgorithm()); - }); - std::chrono::microseconds span(INIT_DYNAMIC_TIME_OUT); - if (futureObj.wait_for(span) == std::future_status::timeout) { - FACEAUTH_HILOGI(MODULE_SERVICE, "LoadAlgorithm TimeOut"); - return FI_RC_ERROR; - } - return static_cast(futureObj.get()); - } - FACEAUTH_HILOGE(MODULE_SERVICE, "Init Fail %{public}d", result); - return FI_RC_ERROR; -} - -FIRetCode FaceAuthManager::ReleaseAlgorithm(std::string bundleName) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "Release, bundleName:%{public}s", bundleName.c_str()); - AlgoResult result = IsNeedAlgoRelease(bundleName); - if (result == AR_EMPTY) { - auto promiseObj = std::make_shared>(); - auto futureObj = promiseObj->get_future(); - FaceAuthThreadPool::GetInstance()->AddTask([promiseObj]() { - promiseObj->set_value(FaceAuthCA::GetInstance()->ReleaseAlgorithm()); - }); - std::chrono::microseconds span(RELEASE_DYNAMIC_TIME_OUT); - if (futureObj.wait_for(span) == std::future_status::timeout) { - FACEAUTH_HILOGI(MODULE_SERVICE, "ReleaseAlgorithm TimeOut"); - return FI_RC_ERROR; - } - return static_cast(futureObj.get()); - } - FACEAUTH_HILOGE(MODULE_SERVICE, "Release Fail %{public}d", result); - return FI_RC_ERROR; -} - -bool FaceAuthManager::IsAlgorithmInited() -{ - if (bundleNameList_.empty()) { - FACEAUTH_HILOGI(MODULE_SERVICE, "algorithm is initted"); - return true; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "algorithm is not initted"); - return false; -} - -AlgoResult FaceAuthManager::IsNeedAlgoLoad(std::string bundleName) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - AlgoResult result = AR_SUCCESS; - - if (bundleNameList_.empty()) { - result = AR_EMPTY; - } - - if (bundleNameList_.find(bundleName) != bundleNameList_.end()) { - bundleNameList_[bundleName] = bundleNameList_[bundleName] + 1; - FACEAUTH_HILOGI(MODULE_SERVICE, - "Add same bundleName:%{public}s, num:%{public}d", bundleName.c_str(), bundleNameList_[bundleName]); - return AR_ADD_AGAIN; - } - - bundleNameList_.insert(std::pair(bundleName, 1)); - - FACEAUTH_HILOGI(MODULE_SERVICE, "Insert bundleName:%{public}s", bundleName.c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "Result:%{public}d", result); - return result; -} - -AlgoResult FaceAuthManager::IsNeedAlgoRelease(std::string bundleName) -{ - AlgoResult result = AR_SUCCESS; - if (bundleNameList_.erase(bundleName) != 0) { - if (bundleNameList_.empty()) { - result = AR_EMPTY; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "Remove Success bundleName:%{public}s", bundleName.c_str()); - } else { - result = AR_NOT_FOUND; - FACEAUTH_HILOGE(MODULE_SERVICE, "Remove Fail bundleName:%{public}s", bundleName.c_str()); - } - return result; -} - -int32_t FaceAuthManager::GenerateEventId() -{ - int32_t randomNum = INVALID_EVENT_ID; - if (!GetRandomNum(&randomNum)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetRandomNum error"); - return INVALID_EVENT_ID; - } - int32_t eventId = randomNum; - FACEAUTH_HILOGI(MODULE_SERVICE, "GenerateEventId generate eventId %{public}u", eventId); - return eventId; -} -int32_t FaceAuthManager::OpenCamera(sptr producer) -{ - auto promiseObj = std::make_shared>(); - auto futureObj = promiseObj->get_future(); - FaceAuthThreadPool::GetInstance()->AddTask([promiseObj, producer]() { - promiseObj->set_value(FaceAuthCamera::GetInstance()->OpenCamera(producer)); - }); - std::chrono::microseconds span(OPEN_CAMERA_TIME_OUT); - if (futureObj.wait_for(span) == std::future_status::timeout) { - FACEAUTH_HILOGI(MODULE_SERVICE, "Open Camera TimeOut"); - return FA_RET_ERROR; - } - if (futureObj.get() == FA_RET_ERROR) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Open Camera Fail"); - return FA_RET_ERROR; - } - return FA_RET_OK; -} - -std::pair> FaceAuthManager::GetAlgorithmState(const int &requestId) -{ - int retCode = FACE_ALGORITHM_OPERATION_BREAK; - std::vector retMsg; - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceAuthCA is null"); - return std::make_pair(FACE_ALGORITHM_OPERATION_BREAK, retMsg); - } - FACEAUTH_HILOGI(MODULE_SERVICE, "requestId %{public}d: start wait for algorithm state", requestId); - faceAuthCA->GetAlgorithmState(retCode, retMsg); - FACEAUTH_HILOGI(MODULE_SERVICE, "requestId %{public}d: algorithm state obtained code %{public}d " - "msg length %{public}zu", - requestId, retCode, retMsg.size()); - return std::make_pair(retCode, retMsg); -} - -FIRetCode FaceAuthManager::DoAlgorithmOperation(const uint64_t &scheduleID) -{ - const std::chrono::seconds GET_RESULT_TIME_LIMIT(GET_RESULT_TIME_LIMIT_IN_SEC); - using RetPair = std::pair>; - int requestId = 0; - std::chrono::steady_clock::time_point begin_time = std::chrono::steady_clock::now(); - while (std::chrono::duration_cast(std::chrono::steady_clock::now() - begin_time) - < GET_RESULT_TIME_LIMIT) { - auto promiseObj = std::make_shared>(); - auto futureObj = promiseObj->get_future(); - FaceAuthThreadPool::GetInstance()->AddTask([promiseObj, requestId]() { - promiseObj->set_value(FaceAuthManager::GetInstance()->GetAlgorithmState(requestId)); - }); - if (futureObj.wait_for(GET_RESULT_TIME_LIMIT) == std::future_status::timeout) { - FACEAUTH_HILOGE(MODULE_SERVICE, "GetAlgorithmState timeOut"); - return FI_RC_ERROR; - } - auto ret = futureObj.get(); - int32_t retCode = ret.first; - if (retCode == FACE_ALGORITHM_OPERATION_BREAK) { - FACEAUTH_HILOGI(MODULE_SERVICE, "GetAlgorithmState break"); - return FI_RC_OK; - } - - if (ret.second.size() > 0) { - std::shared_ptr msg = std::make_shared( - ret.second); - SendData(scheduleID, 0, TYPE_ALL_IN_ONE, TYPE_CO_AUTH, msg); - } - requestId++; - } - FACEAUTH_HILOGE(MODULE_SERVICE, "timeout"); - return FI_RC_ERROR; -} - -bool FaceAuthManager::GetRandomNum(int32_t *randomNum) -{ - if (randomNum == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "randomNum is nullptr"); - return false; - } - BIGNUM *bn = BN_new(); - if (bn == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "BN_new fail"); - return false; - } - if (BN_rand(bn, RAND_NUM_BITS, TOP, BOTTOM) == 0) { - FACEAUTH_HILOGE(MODULE_SERVICE, "BN_rand fail"); - BN_free(bn); - return false; - } - char *decVal = BN_bn2dec(bn); - if (decVal == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "BN_bn2dec is nullptr"); - BN_free(bn); - return false; - } - *randomNum = atoi(decVal); - BN_free(bn); - return true; -} - -FIRetCode FaceAuthManager::DoWaitInitAlgorithm(std::future futureObj) -{ - std::chrono::microseconds span(INIT_DYNAMIC_TIME_OUT); - if (futureObj.wait_for(span) == std::future_status::timeout) { - FACEAUTH_HILOGI(MODULE_SERVICE, "LoadAlgorithm TimeOut"); - return FI_RC_ERROR; - } - return static_cast(futureObj.get()); -} - -void FaceAuthManager::UnfreezeTemplates(std::vector templateIdList) -{ - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get FaceAuthCA instance failed"); - return; - } - - for (auto templateId : templateIdList) { - if (faceAuthCA->ResetRemainTimes(templateId) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "resetRemainTimes failed"); - } - } -} - -void FaceAuthManager::FreezeTemplates(std::vector templateIdList) -{ - std::shared_ptr faceAuthCA = FaceAuthCA::GetInstance(); - if (faceAuthCA == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "get FaceAuthCA instance failed"); - return; - } - - for (auto templateId : templateIdList) { - if (faceAuthCA->FreezeTemplate(templateId) != FA_RET_OK) { - FACEAUTH_HILOGE(MODULE_SERVICE, "FreezeTemplate failed"); - } - } -} - -void FaceAuthManager::SetBufferProducer(sptr &producer) -{ - this->producer_ = producer; - FACEAUTH_HILOGI(MODULE_SERVICE, "set buffer producer %{public}s", getPointerNullString(this->producer_).c_str()); -} - -sptr FaceAuthManager::GetBufferProducer() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "get buffer producer %{public}s", getPointerNullString(this->producer_).c_str()); - return this->producer_; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_query_callback.cpp b/services/faceauth/src/face_auth_query_callback.cpp deleted file mode 100644 index a846fde..0000000 --- a/services/faceauth/src/face_auth_query_callback.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_manager.h" -#include "face_auth_log_wrapper.h" -#include "co_auth_info_define.h" -#include "face_auth_ca.h" -#include "face_auth_query_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -void FaceAuthQueryCallback::OnResult(uint32_t resultCode) -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "start"); - FACEAUTH_HILOGI(MODULE_SERVICE, "resultCode = %{public}u", resultCode); - std::shared_ptr manager = FaceAuthManager::GetInstance(); - if (manager != nullptr) { - if (resultCode != EXECUTOR_REGISTER) { - manager->RegisterExecutor(); - } - } -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_req.cpp b/services/faceauth/src/face_auth_req.cpp deleted file mode 100644 index 711812d..0000000 --- a/services/faceauth/src/face_auth_req.cpp +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_req.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -std::mutex FaceAuthReq::mutex_; -std::shared_ptr FaceAuthReq::instance_ = nullptr; -FaceAuthReq::FaceAuthReq() -{} - -FaceAuthReq::~FaceAuthReq() -{ - reqInfoList_.clear(); -} - -std::shared_ptr FaceAuthReq::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -bool FaceAuthReq::IsReqNumReachedMax(FaceOperateType type) -{ - std::lock_guard lock_l(mutex_); - PrintReqInfoList(); - FACEAUTH_HILOGI(MODULE_SERVICE, "type is %{public}d", type); - if (type > FACE_OPERATE_TYPE_MAX || type <= FACE_INVALID_OPERATE_TYPE) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Parameter check error.type is %{public}d", type); - return true; - } - if (type == FACE_OPERATE_TYPE_CO_AUTH) { - int32_t count = 0; - for (auto iter = reqInfoList_.begin(); iter != reqInfoList_.end(); ++iter) { - if (iter->first.operateType == type) { - count++; - } - } - if (count >= CO_AUTH_MAX_NUM - 1) { - return true; - } - } else { - for (auto iter = reqInfoList_.begin(); iter != reqInfoList_.end(); ++iter) { - FACEAUTH_HILOGI(MODULE_SERVICE, "operateType is %{public}d", iter->first.operateType); - if (iter->first.operateType == type) { - FACEAUTH_HILOGI(MODULE_SERVICE, "reqId is %{public}s", getMaskedString(iter->first.reqId).c_str()); - return true; - } - } - } - return false; -} - -void FaceAuthReq::AddReqInfo(FaceReqType reqType, FaceInfo reqInfo) -{ - std::lock_guard lock_l(mutex_); - FACEAUTH_HILOGI(MODULE_SERVICE, - "reqType.reqId is %{public}s," - "reqType.operateType is %{public}d," - "reqInfo.eventId is %{public}u," - "reqInfo.uId is %{public}d," - "reqInfo.isCanceled is %{public}d", - getMaskedString(reqType.reqId).c_str(), reqType.operateType, reqInfo.eventId, reqInfo.uId, reqInfo.isCanceled); - if (reqType.operateType <= FACE_INVALID_OPERATE_TYPE || reqType.operateType > FACE_OPERATE_TYPE_MAX) { - FACEAUTH_HILOGE(MODULE_SERVICE, "Parameter check error.reqInfo.operateType is %{public}d", reqType.operateType); - return; - } - auto req2Rm = reqInfoList_.find(reqType); - if (req2Rm != reqInfoList_.end()) { - FACEAUTH_HILOGI(MODULE_SERVICE, "same key was found"); - } else { - reqInfoList_.insert(std::pair(reqType, reqInfo)); - } - return; -} - -void FaceAuthReq::RemoveRequireInfo(FaceReqType reqType) -{ - std::lock_guard lock_l(mutex_); - PrintReqInfoList(); - FACEAUTH_HILOGI(MODULE_SERVICE, - "Remove reqType.reqId is %{public}s," - "Remove reqType.operateType is %{public}d", - getMaskedString(reqType.reqId).c_str(), reqType.operateType); - auto req2Rm = reqInfoList_.find(reqType); - if (req2Rm != reqInfoList_.end()) { - reqInfoList_.erase(reqType); - } else { - FACEAUTH_HILOGI(MODULE_SERVICE, "no require info found"); - } - return; -} - -void FaceAuthReq::RemoveAllRequireInfo() -{ - std::lock_guard lock_l(mutex_); - reqInfoList_.clear(); -} - -FaceOperateType FaceAuthReq::GetOperateType(uint32_t eventId) -{ - std::lock_guard lock_l(mutex_); - FACEAUTH_HILOGI(MODULE_SERVICE, "eventId is %{public}u", eventId); - for (auto iter = reqInfoList_.begin(); iter != reqInfoList_.end(); ++iter) { - if ((uint32_t)iter->second.eventId == eventId) { - return iter->first.operateType; - } - } - return FACE_INVALID_OPERATE_TYPE; -} - -uint32_t FaceAuthReq::GetEventId(FaceReqType reqType) -{ - std::lock_guard lock_l(mutex_); - FACEAUTH_HILOGI(MODULE_SERVICE, "reqType.reqId is %{public}s", getMaskedString(reqType.reqId).c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "reqType.operateType is %{public}d", reqType.operateType); - auto iter = reqInfoList_.find(reqType); - if (iter != reqInfoList_.end()) { - return reqInfoList_[reqType].eventId; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "reqType is not in reqInfoList, reqType.reqId is %{public}s", - getMaskedString(reqType.reqId).c_str()); - return 0; -} - -bool FaceAuthReq::SetCancelFlagSuccess(FaceReqType reqType, int32_t uId) -{ - std::lock_guard lock_l(mutex_); - FACEAUTH_HILOGI(MODULE_SERVICE, "reqType.reqId is %{public}s", getMaskedString(reqType.reqId).c_str()); - FACEAUTH_HILOGI(MODULE_SERVICE, "reqType.operateType is %{public}d", reqType.operateType); - FACEAUTH_HILOGI(MODULE_SERVICE, "uId is %{public}d", uId); - auto iter = reqInfoList_.find(reqType); - if (iter != reqInfoList_.end() && uId == reqInfoList_[reqType].uId) { - reqInfoList_[reqType].isCanceled = true; - return true; - } - FACEAUTH_HILOGI(MODULE_SERVICE, - "reqType or uId is not in reqInfoList, reqType.reqId is %{public}s, uid is%{public}d", - getMaskedString(reqType.reqId).c_str(), uId); - return false; -} - -bool FaceAuthReq::isCanceled(uint32_t eventId, int32_t uId) -{ - std::lock_guard lock_l(mutex_); - FACEAUTH_HILOGI(MODULE_SERVICE, "eventId is %{public}u", eventId); - FACEAUTH_HILOGI(MODULE_SERVICE, "uId is %{public}d", uId); - for (auto iter = reqInfoList_.begin(); iter != reqInfoList_.end(); ++iter) { - if ((uint32_t)iter->second.eventId == eventId && uId == iter->second.uId) { - return iter->second.isCanceled; - } - } - FACEAUTH_HILOGI(MODULE_SERVICE, "eventId or uId is not in reqInfoList, eventId is %{public}u, uid is%{public}d", - eventId, uId); - return false; -} - -bool FaceAuthReq::FindLocalAuth() -{ - std::lock_guard lock_l(mutex_); - bool result = false; - for (auto iter = reqInfoList_.begin(); iter != reqInfoList_.end(); ++iter) { - if (iter->first.operateType == FACE_OPERATE_TYPE_LOCAL_AUTH) { - result = true; - } - } - return result; -} - -void FaceAuthReq::PrintReqInfoList() -{ - for (auto iterinfo = reqInfoList_.begin(); iterinfo != reqInfoList_.end(); ++iterinfo) { - FACEAUTH_HILOGI(MODULE_SERVICE, - "ListInfo:reqId:%{public}s, Type:%{public}d, eventId:%{public}u, uId:%{public}d, isCanceled:%{public}d", - getMaskedString(iterinfo->first.reqId).c_str(), iterinfo->first.operateType, iterinfo->second.eventId, - iterinfo->second.uId, iterinfo->second.isCanceled); - } -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_service.cpp b/services/faceauth/src/face_auth_service.cpp deleted file mode 100644 index da30669..0000000 --- a/services/faceauth/src/face_auth_service.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_service.h" -#include "parameter.h" -#include "singleton.h" -#include "face_auth_log_wrapper.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -const std::string REGISTER_NOTIFICATION = "EXECUTOR_REGISTER_NOTIFICATION"; -std::shared_ptr FaceAuthService::instance_ = nullptr; -std::shared_ptr FaceAuthService::manager_ = nullptr; -std::mutex FaceAuthService::mutex_; -const bool REGISTER_RESULT = - SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); -static const char IAM_EVENT_KEY[] = "bootevent.useriam.fwkready"; - -FaceAuthService::FaceAuthService() - : SystemAbility(SUBSYS_USERIAM_SYS_ABILITY_FACEAUTH, true) -{ -} - -FaceAuthService::~FaceAuthService() -{ -} - -std::shared_ptr FaceAuthService::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} - -static void UserIamBootEventCallback(const char *key, const char *value, void *context) -{ - FACEAUTH_HILOGD(MODULE_SERVICE, "UserIam is ready"); - if (key == nullptr || value == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "param is null"); - return; - } - if (strcmp(key, IAM_EVENT_KEY) || strcmp(value, "true")) { - FACEAUTH_HILOGE(MODULE_SERVICE, "event is mismatch"); - return; - } - auto faceService = FaceAuthService::GetInstance(); - if (faceService == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "faceService is null"); - return; - } - faceService->ReRegister(); -} - -void FaceAuthService::OnStart() -{ - FACEAUTH_HILOGI(MODULE_SERVICE, "Start"); - WatchParameter(IAM_EVENT_KEY, UserIamBootEventCallback, nullptr); - - if (!Publish(this)) { - FACEAUTH_HILOGE(MODULE_SERVICE, "failed to publish the service."); - return; - } - FACEAUTH_HILOGI(MODULE_SERVICE, "publish service ok"); - Start(); -} - -void FaceAuthService::OnStop() -{ - if (manager_ != nullptr) { - manager_->Release(); - manager_.reset(); - } -} -void FaceAuthService::Start() -{ - int32_t ret = FA_RET_ERROR; - manager_ = FaceAuthManager::GetInstance(); - if (manager_ != nullptr) { - ret = manager_->Init(); - } - if (FA_RET_OK == ret) { - FACEAUTH_HILOGI(MODULE_SERVICE, "Init result success"); - } else { - FACEAUTH_HILOGE(MODULE_SERVICE, "Init result failed"); - } -} - -void FaceAuthService::ReRegister() -{ - if (manager_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "manager_ is null"); - return; - } - manager_->QueryRegStatus(); -} - -int32_t FaceAuthService::SetBufferProducer(sptr &producer) -{ - if (manager_ == nullptr) { - FACEAUTH_HILOGE(MODULE_SERVICE, "manager_ is null"); - return FA_RET_ERROR; - } - manager_->SetBufferProducer(producer); - return FA_RET_OK; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/services/faceauth/src/face_auth_thread_pool.cpp b/services/faceauth/src/face_auth_thread_pool.cpp deleted file mode 100644 index 6e066fc..0000000 --- a/services/faceauth/src/face_auth_thread_pool.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "face_auth_thread_pool.h" -#include "face_auth_defines.h" - -namespace OHOS { -namespace UserIAM { -namespace FaceAuth { -std::mutex FaceAuthThreadPool::mutex_; -std::shared_ptr FaceAuthThreadPool::instance_ = nullptr; -FaceAuthThreadPool::FaceAuthThreadPool() -{ - Start(FACE_AUTH_THREAD_NUM); -} - -FaceAuthThreadPool::~FaceAuthThreadPool() -{ - Stop(); -} - -std::shared_ptr FaceAuthThreadPool::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard lock_l(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; -} -} // namespace FaceAuth -} // namespace UserIAM -} // namespace OHOS \ No newline at end of file diff --git a/services/faceauth/include/face_auth_thread_pool.h b/services/include/face_auth_driver_hdi.h similarity index 58% rename from services/faceauth/include/face_auth_thread_pool.h rename to services/include/face_auth_driver_hdi.h index e02bbe9..bcf5de1 100644 --- a/services/faceauth/include/face_auth_thread_pool.h +++ b/services/include/face_auth_driver_hdi.h @@ -13,28 +13,30 @@ * limitations under the License. */ -#ifndef FACE_AUTH_THREAD_POOL_H -#define FACE_AUTH_THREAD_POOL_H +#ifndef FACE_AUTH_DRIVER_HDI +#define FACE_AUTH_DRIVER_HDI -#include +#include +#include + +#include "iauth_executor_hdi.h" +#include "iremote_broker.h" #include "nocopyable.h" -#include "thread_pool.h" +#include "v1_0/face_auth_interface_proxy.h" namespace OHOS { namespace UserIAM { namespace FaceAuth { -class FaceAuthThreadPool : public ThreadPool { +namespace FaceHdi = OHOS::HDI::FaceAuth::V1_0; +class FaceAuthDriverHdi : public UserAuth::IAuthDriverHdi, public NoCopyable { public: - FaceAuthThreadPool(); - virtual ~FaceAuthThreadPool(); - static std::shared_ptr GetInstance(); + FaceAuthDriverHdi() = default; + virtual ~FaceAuthDriverHdi() = default; -private: - static std::mutex mutex_; - static std::shared_ptr instance_; + void GetExecutorList(std::vector> &executorList); }; } // namespace FaceAuth } // namespace UserIAM } // namespace OHOS -#endif // FACE_AUTH_THREAD_POOL_H +#endif // FACE_AUTH_DRIVER_HDI \ No newline at end of file diff --git a/services/include/face_auth_executor_callback_hdi.h b/services/include/face_auth_executor_callback_hdi.h new file mode 100644 index 0000000..50d676d --- /dev/null +++ b/services/include/face_auth_executor_callback_hdi.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FACE_AUTH_EXECUTOR_CALLBACK_HDI +#define FACE_AUTH_EXECUTOR_CALLBACK_HDI + +#include +#include + +#include "iexecute_callback.h" +#include "iexecutor_messenger.h" +#include "nocopyable.h" +#include "v1_0/executor_callback_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +namespace FaceHdi = OHOS::HDI::FaceAuth::V1_0; +class FaceAuthExecutorCallbackHdi : public FaceHdi::IExecutorCallback, public NoCopyable { +public: + explicit FaceAuthExecutorCallbackHdi(std::shared_ptr frameworkCallback); + virtual ~FaceAuthExecutorCallbackHdi() = default; + + // FaceHdi::IExecutorCallback + int32_t OnResult(int32_t result, const std::vector &extraInfo) override; + int32_t OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; + +private: + UserIAM::ResultCode ConvertResultCode(const int32_t in); + + std::shared_ptr frameworkCallback_; +}; +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // FACE_AUTH_EXECUTOR_CALLBACK_HDI diff --git a/services/include/face_auth_executor_hdi.h b/services/include/face_auth_executor_hdi.h new file mode 100644 index 0000000..d6a9c66 --- /dev/null +++ b/services/include/face_auth_executor_hdi.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FACE_AUTH_EXECUTOR_HDI_H +#define FACE_AUTH_EXECUTOR_HDI_H + +#include +#include +#include +#include + +#include "framework_types.h" +#include "iauth_executor_hdi.h" +#include "iexecute_callback.h" +#include "nocopyable.h" + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +namespace FaceHdi = OHOS::HDI::FaceAuth::V1_0; +class FaceAuthExecutorHdi : public UserAuth::IAuthExecutorHdi, public NoCopyable { +public: + explicit FaceAuthExecutorHdi(sptr executorProxy); + virtual ~FaceAuthExecutorHdi() = default; + + UserIAM::ResultCode GetExecutorInfo(UserIAM::ExecutorInfo &info); + UserIAM::ResultCode GetTemplateInfo(uint64_t templateId, UserAuth::TemplateInfo &info); + UserIAM::ResultCode OnRegisterFinish(const std::vector &templateIdList, + const std::vector &frameworkPublicKey, const std::vector &extraInfo); + UserIAM::ResultCode Enroll(uint64_t scheduleId, uint64_t callerUid, const std::vector &extraInfo, + const std::shared_ptr &callbackObj); + UserIAM::ResultCode Authenticate(uint64_t scheduleId, uint64_t callerUid, + const std::vector &templateIdList, const std::vector &extraInfo, + const std::shared_ptr &callbackObj); + UserIAM::ResultCode Identify(uint64_t scheduleId, uint64_t callerUid, const std::vector &extraInfo, + const std::shared_ptr &callbackObj); + UserIAM::ResultCode Delete(const std::vector &templateIdList); + UserIAM::ResultCode Cancel(uint64_t scheduleId); + UserIAM::ResultCode SendCommand(UserAuth::AuthPropertyMode commandId, const std::vector &extraInfo, + const std::shared_ptr &callbackObj); + +private: + UserIAM::ResultCode MoveHdiExecutorInfo(FaceHdi::ExecutorInfo &in, UserIAM::ExecutorInfo &out); + UserIAM::ResultCode MoveHdiTemplateInfo(FaceHdi::TemplateInfo &in, UserAuth::TemplateInfo &out); + UserIAM::ResultCode ConvertCommandId(const UserAuth::AuthPropertyMode in, FaceHdi::CommandId &out); + UserIAM::ResultCode ConvertAuthType(const FaceHdi::AuthType in, UserIAM::AuthType &out); + UserIAM::ResultCode ConvertExecutorRole(const FaceHdi::ExecutorRole in, UserIAM::ExecutorRole &out); + UserIAM::ResultCode ConvertExecutorSecureLevel( + const FaceHdi::ExecutorSecureLevel in, UserIAM::ExecutorSecureLevel &out); + UserIAM::ResultCode ConvertResultCode(const int32_t in); + + sptr executorProxy_; +}; +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // FACE_AUTH_EXECUTOR_HDI_H \ No newline at end of file diff --git a/services/faceauth/include/face_auth_service.h b/services/include/face_auth_service.h similarity index 76% rename from services/faceauth/include/face_auth_service.h rename to services/include/face_auth_service.h index 7defe37..de19ee2 100644 --- a/services/faceauth/include/face_auth_service.h +++ b/services/include/face_auth_service.h @@ -12,40 +12,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef FACE_AUTH_SERVICE_H #define FACE_AUTH_SERVICE_H -#include -#include "system_ability.h" -#include "system_ability_definition.h" -#include "face_auth_manager.h" #include "face_auth_stub.h" +#include +#include + +#include "nocopyable.h" +#include "system_ability.h" +#include "surface.h" + namespace OHOS { namespace UserIAM { namespace FaceAuth { -class FaceAuthService : public SystemAbility, public FaceAuthStub { -public: +class FaceAuthService : public SystemAbility, public FaceAuthStub, public NoCopyable { DECLEAR_SYSTEM_ABILITY(FaceAuthService); - static std::shared_ptr GetInstance(); - FaceAuthService(); - virtual ~FaceAuthService() override; - virtual int32_t SetBufferProducer(sptr &producer) override; + public: + FaceAuthService(); + virtual ~FaceAuthService() = default; + static std::shared_ptr GetInstance(); + void OnStart() override; void OnStop() override; - void Start(); - void ReRegister(); + int32_t SetBufferProducer(sptr &producer) override; + + sptr GetBufferProducer(); private: + void StartDriverManager(); + static std::mutex mutex_; static std::shared_ptr instance_; - static std::shared_ptr manager_; - DISALLOW_COPY_AND_MOVE(FaceAuthService); + sptr bufferProducer_; }; } // namespace FaceAuth } // namespace UserIAM } // namespace OHOS -#endif // FACE_AUTH_SERVICE_H \ No newline at end of file +#endif // FACE_AUTH_SERVICE_H diff --git a/services/src/face_auth_driver_hdi.cpp b/services/src/face_auth_driver_hdi.cpp new file mode 100644 index 0000000..e529ec2 --- /dev/null +++ b/services/src/face_auth_driver_hdi.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "face_auth_driver_hdi.h" + +#include + +#include "face_auth_defines.h" +#include "face_auth_executor_hdi.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "iauth_executor_hdi.h" +#include "v1_0/iface_auth_interface.h" + +#define LOG_LABEL UserIAM::Common::LABEL_FACE_AUTH_SA + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +void FaceAuthDriverHdi::GetExecutorList(std::vector> &executorList) +{ + auto faceIf = FaceHdi::IFaceAuthInterface::Get(); + if (faceIf == nullptr) { + IAM_LOGE("IFaceAuthInterface is null"); + return; + } + + std::vector> iExecutorList; + faceIf->GetExecutorList(iExecutorList); + for (const auto &iExecutor : iExecutorList) { + auto executor = Common::MakeShared(iExecutor); + if (executor == nullptr) { + IAM_LOGE("make share failed"); + continue; + } + executorList.push_back(executor); + } +} +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/services/src/face_auth_executor_callback_hdi.cpp b/services/src/face_auth_executor_callback_hdi.cpp new file mode 100644 index 0000000..ac16177 --- /dev/null +++ b/services/src/face_auth_executor_callback_hdi.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "face_auth_executor_callback_hdi.h" + +#include "face_auth_defines.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "v1_0/face_auth_types.h" + +#define LOG_LABEL UserIAM::Common::LABEL_FACE_AUTH_SA + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +FaceAuthExecutorCallbackHdi::FaceAuthExecutorCallbackHdi(std::shared_ptr frameworkCallback) + : frameworkCallback_(frameworkCallback) +{ +} + +int32_t FaceAuthExecutorCallbackHdi::OnResult(int32_t code, const std::vector &extraInfo) +{ + IAM_LOGI("OnResult %{public}d", code); + UserIAM::ResultCode retCode = ConvertResultCode(code); + IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE); + frameworkCallback_->OnResult(retCode, extraInfo); + return HDF_SUCCESS; +} + +int32_t FaceAuthExecutorCallbackHdi::OnAcquireInfo(int32_t code, const std::vector &extraInfo) +{ + IAM_LOGI("OnAcquireInfo %{public}d", code); + IF_FALSE_LOGE_AND_RETURN_VAL(frameworkCallback_ != nullptr, HDF_FAILURE); + frameworkCallback_->OnAcquireInfo(code, extraInfo); + return HDF_SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorCallbackHdi::ConvertResultCode(const int32_t in) +{ + ResultCode hdiIn = static_cast(in); + if (hdiIn > ResultCode::VENDOR_RESULT_CODE_BEGIN) { + IAM_LOGI("vendor hdi result code %{public}d, no covert", hdiIn); + return static_cast(in); + } + + static const std::map data = { + {ResultCode::SUCCESS, UserIAM::ResultCode::SUCCESS}, + {ResultCode::FAIL, UserIAM::ResultCode::FAIL}, + {ResultCode::GENERAL_ERROR, UserIAM::ResultCode::GENERAL_ERROR}, + {ResultCode::CANCELED, UserIAM::ResultCode::CANCELED}, + {ResultCode::TIMEOUT, UserIAM::ResultCode::TIMEOUT}, + {ResultCode::BUSY, UserIAM::ResultCode::BUSY}, + {ResultCode::INVALID_PARAMETERS, UserIAM::ResultCode::INVALID_PARAMETERS}, + {ResultCode::LOCKED, UserIAM::ResultCode::LOCKED}, + {ResultCode::NOT_ENROLLED, UserIAM::ResultCode::NOT_ENROLLED}, + // should be UserIAM::ResultCode::OPERATION_NOT_SUPPORT + {ResultCode::OPERATION_NOT_SUPPORT, UserIAM::ResultCode::GENERAL_ERROR}, + }; + + UserIAM::ResultCode out; + if (data.count(hdiIn) == 0) { + out = UserIAM::ResultCode::GENERAL_ERROR; + IAM_LOGE("convert hdi undefined result code %{public}d to framework result code %{public}d", in, out); + return out; + } + out = data.at(hdiIn); + IAM_LOGI("covert hdi result code %{public}d to framework result code %{public}d", hdiIn, out); + return out; +} +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/services/src/face_auth_executor_hdi.cpp b/services/src/face_auth_executor_hdi.cpp new file mode 100644 index 0000000..6f2f4c0 --- /dev/null +++ b/services/src/face_auth_executor_hdi.cpp @@ -0,0 +1,289 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "face_auth_executor_hdi.h" + +#include "face_auth_defines.h" +#include "face_auth_executor_callback_hdi.h" +#include "hdf_base.h" +#include "iam_check.h" +#include "iam_logger.h" + +#define LOG_LABEL UserIAM::Common::LABEL_FACE_AUTH_SA + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +FaceAuthExecutorHdi::FaceAuthExecutorHdi(sptr executorProxy) : executorProxy_(executorProxy) {}; + +UserIAM::ResultCode FaceAuthExecutorHdi::GetExecutorInfo(UserIAM::ExecutorInfo &info) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + FaceHdi::ExecutorInfo localInfo = {}; + int32_t status = executorProxy_->GetExecutorInfo(localInfo); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("GetExecutorInfo fail ret=%{public}d", result); + return result; + } + int32_t ret = MoveHdiExecutorInfo(localInfo, info); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("MoveHdiExecutorInfo fail ret=%{public}d", ret); + return UserIAM::ResultCode::GENERAL_ERROR; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::GetTemplateInfo(uint64_t templateId, UserAuth::TemplateInfo &info) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + FaceHdi::TemplateInfo localInfo = {}; + int32_t status = executorProxy_->GetTemplateInfo(templateId, localInfo); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("GetTemplateInfo fail ret=%{public}d", result); + return result; + } + int32_t ret = MoveHdiTemplateInfo(localInfo, info); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("MoveHdiTemplateInfo fail ret=%{public}d", ret); + return UserIAM::ResultCode::GENERAL_ERROR; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::OnRegisterFinish(const std::vector &templateIdList, + const std::vector &frameworkPublicKey, const std::vector &extraInfo) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("OnRegisterFinish fail ret=%{public}d", status); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::Enroll(uint64_t scheduleId, uint64_t callerUid, + const std::vector &extraInfo, const std::shared_ptr &callbackObj) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + auto callback = sptr(new (std::nothrow) FaceAuthExecutorCallbackHdi(callbackObj)); + IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->Enroll(scheduleId, extraInfo, callback); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("Enroll fail ret=%{public}d", result); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::Authenticate(uint64_t scheduleId, uint64_t callerUid, + const std::vector &templateIdList, const std::vector &extraInfo, + const std::shared_ptr &callbackObj) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + auto callback = sptr(new (std::nothrow) FaceAuthExecutorCallbackHdi(callbackObj)); + IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->Authenticate(scheduleId, templateIdList, extraInfo, callback); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("Authenticate fail ret=%{public}d", result); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::Identify(uint64_t scheduleId, uint64_t callerUid, + const std::vector &extraInfo, const std::shared_ptr &callbackObj) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + auto callback = sptr(new (std::nothrow) FaceAuthExecutorCallbackHdi(callbackObj)); + IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->Identify(scheduleId, extraInfo, callback); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("Identify fail ret=%{public}d", result); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::Delete(const std::vector &templateIdList) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->Delete(templateIdList); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("Delete fail ret=%{public}d", result); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::Cancel(uint64_t scheduleId) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + int32_t status = executorProxy_->Cancel(scheduleId); + UserIAM::ResultCode result = ConvertResultCode(status); + if (result != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("Cancel fail ret=%{public}d", result); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::SendCommand(UserAuth::AuthPropertyMode commandId, + const std::vector &extraInfo, const std::shared_ptr &callbackObj) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executorProxy_ != nullptr, UserIAM::ResultCode::GENERAL_ERROR); + FaceHdi::CommandId hdiCommandId; + int32_t ret = ConvertCommandId(commandId, hdiCommandId); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("ConvertCommandId fail ret=%{public}d", ret); + return UserIAM::ResultCode::GENERAL_ERROR; + } + int32_t status = executorProxy_->SendCommand(hdiCommandId, extraInfo, + sptr(new (std::nothrow) FaceAuthExecutorCallbackHdi(callbackObj))); + UserIAM::ResultCode result = ConvertResultCode(status); + if (status != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("SendCommand fail ret=%{public}d", status); + return result; + } + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::MoveHdiExecutorInfo(FaceHdi::ExecutorInfo &in, UserIAM::ExecutorInfo &out) +{ + out.executorId = static_cast(in.sensorId); + out.executorType = in.executorType; + int32_t ret = ConvertExecutorRole(in.executorRole, out.role); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("executorProxy is null"); + return UserIAM::ResultCode::GENERAL_ERROR; + } + ret = ConvertAuthType(in.authType, out.authType); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("ConvertAuthType fail ret=%{public}d", ret); + return UserIAM::ResultCode::GENERAL_ERROR; + } + ret = ConvertExecutorSecureLevel(in.esl, out.esl); + if (ret != UserIAM::ResultCode::SUCCESS) { + IAM_LOGE("ConvertExecutorSecureLevel fail ret=%{public}d", ret); + return UserIAM::ResultCode::GENERAL_ERROR; + } + in.publicKey.swap(out.publicKey); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::MoveHdiTemplateInfo(FaceHdi::TemplateInfo &in, UserAuth::TemplateInfo &out) +{ + out.executorType = in.executorType; + out.freezingTime = in.freezingTime; + out.remainTimes = in.remainTimes; + in.extraInfo.swap(out.extraInfo); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::ConvertCommandId(const UserAuth::AuthPropertyMode in, FaceHdi::CommandId &out) +{ + if (static_cast(in) > UserAuth::VENDOR_COMMAND_BEGIN) { + out = static_cast(in); + IAM_LOGI("vendor command id %{public}d, no covert", out); + return UserIAM::ResultCode::SUCCESS; + } + + static const std::map data = { + {UserAuth::AuthPropertyMode::PROPERMODE_FREEZE, FaceHdi::CommandId::LOCK_TEMPLATE}, + {UserAuth::AuthPropertyMode::PROPERMODE_UNFREEZE, FaceHdi::CommandId::UNLOCK_TEMPLATE}}; + if (data.count(in) == 0) { + IAM_LOGE("command id %{public}d is invalid", in); + return UserIAM::ResultCode::GENERAL_ERROR; + } + out = data.at(in); + IAM_LOGI("covert command id %{public}d to idl command is %{public}d", in, out); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::ConvertAuthType(const FaceHdi::AuthType in, UserIAM::AuthType &out) +{ + static const std::map data = { + {FaceHdi::FACE, UserIAM::AuthType::FACE}, + }; + if (data.count(in) == 0) { + IAM_LOGE("authType %{public}d is invalid", in); + return UserIAM::ResultCode::GENERAL_ERROR; + } + out = data.at(in); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::ConvertExecutorRole(const FaceHdi::ExecutorRole in, UserIAM::ExecutorRole &out) +{ + static const std::map data = { + {FaceHdi::ExecutorRole::COLLECTOR, UserIAM::ExecutorRole::COLLECTOR}, + {FaceHdi::ExecutorRole::VERIFIER, UserIAM::ExecutorRole::VERIFIER}, + {FaceHdi::ExecutorRole::ALL_IN_ONE, UserIAM::ExecutorRole::ALL_IN_ONE}, + }; + if (data.count(in) == 0) { + IAM_LOGE("executorRole %{public}d is invalid", in); + return UserIAM::ResultCode::GENERAL_ERROR; + } + out = data.at(in); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::ConvertExecutorSecureLevel( + const FaceHdi::ExecutorSecureLevel in, UserIAM::ExecutorSecureLevel &out) +{ + static const std::map data = { + {FaceHdi::ExecutorSecureLevel::ESL0, UserIAM::ExecutorSecureLevel::ESL0}, + {FaceHdi::ExecutorSecureLevel::ESL1, UserIAM::ExecutorSecureLevel::ESL1}, + {FaceHdi::ExecutorSecureLevel::ESL2, UserIAM::ExecutorSecureLevel::ESL2}, + {FaceHdi::ExecutorSecureLevel::ESL3, UserIAM::ExecutorSecureLevel::ESL3}, + }; + if (data.count(in) == 0) { + IAM_LOGE("executorSecureLevel %{public}d is invalid", in); + return UserIAM::ResultCode::GENERAL_ERROR; + } + out = data.at(in); + return UserIAM::ResultCode::SUCCESS; +} + +UserIAM::ResultCode FaceAuthExecutorHdi::ConvertResultCode(const int32_t in) +{ + HDF_STATUS hdfIn = static_cast(in); + static const std::map data = { + {HDF_SUCCESS, UserIAM::ResultCode::SUCCESS}, + {HDF_FAILURE, UserIAM::ResultCode::FAIL}, + {HDF_ERR_TIMEOUT, UserIAM::ResultCode::TIMEOUT}, + {HDF_ERR_QUEUE_FULL, UserIAM::ResultCode::BUSY}, + {HDF_ERR_DEVICE_BUSY, UserIAM::ResultCode::BUSY}, + }; + + UserIAM::ResultCode out; + if (data.count(hdfIn) == 0) { + out = UserIAM::ResultCode::GENERAL_ERROR; + } else { + out = data.at(hdfIn); + } + IAM_LOGE("covert hdi result code %{public}d to framework result code %{public}d", in, out); + return out; +} +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/services/src/face_auth_service.cpp b/services/src/face_auth_service.cpp new file mode 100644 index 0000000..ddfa8b0 --- /dev/null +++ b/services/src/face_auth_service.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "face_auth_service.h" + +#include + +#include "face_auth_defines.h" +#include "face_auth_driver_hdi.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_para2str.h" +#include "iam_ptr.h" +#include "idriver_manager.h" +#include "system_ability_definition.h" + +#define LOG_LABEL UserIAM::Common::LABEL_FACE_AUTH_SA + +namespace OHOS { +namespace UserIAM { +namespace FaceAuth { +const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(FaceAuthService::GetInstance().get()); +std::mutex FaceAuthService::mutex_; +std::shared_ptr FaceAuthService::instance_ = nullptr; + +FaceAuthService::FaceAuthService() : SystemAbility(SUBSYS_USERIAM_SYS_ABILITY_FACEAUTH, true) +{ +} + +std::shared_ptr FaceAuthService::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard gurard(mutex_); + if (instance_ == nullptr) { + instance_ = Common::MakeShared(); + if (instance_ == nullptr) { + IAM_LOGE("make share failed"); + } + } + } + return instance_; +} + +void FaceAuthService::OnStart() +{ + IAM_LOGI("start"); + StartDriverManager(); + Publish(this); + IAM_LOGI("success"); +} + +void FaceAuthService::OnStop() +{ + IAM_LOGE("service is persistent, OnStop is not implemented"); +} + +int32_t FaceAuthService::SetBufferProducer(sptr &producer) +{ + std::lock_guard gurard(mutex_); + IAM_LOGI("set buffer producer %{public}s", Common::GetPointerNullStateString(producer).c_str()); + bufferProducer_ = producer; + return FACEAUTH_SUCCESS; +} + +sptr FaceAuthService::FaceAuthService::GetBufferProducer() +{ + std::lock_guard gurard(mutex_); + IAM_LOGI("get buffer producer %{public}s", Common::GetPointerNullStateString(bufferProducer_).c_str()); + return bufferProducer_; +} + +void FaceAuthService::StartDriverManager() +{ + IAM_LOGI("start"); + auto faceAuthDefaultHdi = Common::MakeShared(); + IF_FALSE_LOGE_AND_RETURN(faceAuthDefaultHdi != nullptr); + const uint16_t faceAuthDefaultHdiId = 1; + // serviceName and HdiConfig.id must be globally unique + const std::map hdiName2Config = { + {"face_auth_interface_service", {faceAuthDefaultHdiId, faceAuthDefaultHdi}}, + }; + int32_t ret = UserIAM::UserAuth::IDriverManager::Start(hdiName2Config); + if (ret != FACEAUTH_SUCCESS) { + IAM_LOGE("start driver manager failed"); + } +} +} // namespace FaceAuth +} // namespace UserIAM +} // namespace OHOS