From eff742f9450ba553aa2743aa4091ff3227a0aa97 Mon Sep 17 00:00:00 2001 From: Tianshi Liu Date: Sun, 24 Apr 2022 17:28:17 +0800 Subject: [PATCH] add userauth executor Change-Id: Ieffd57c3e485abeabc0232618336f99d3e167805 Signed-off-by: Tianshi Liu --- .gitignore | 4 +- bundle.json | 1 + common/BUILD.gn | 23 ++ common/executors/BUILD.gn | 66 +++++ .../async_command/async_command_base.h | 61 +++++ .../include/async_command/auth_command.h | 44 ++++ .../include/async_command/custom_command.h | 47 ++++ .../include/async_command/enroll_command.h | 44 ++++ .../include/async_command/iasync_command.h | 37 +++ .../include/async_command/identify_command.h | 44 ++++ common/executors/include/driver.h | 47 ++++ common/executors/include/driver_manager.h | 58 +++++ common/executors/include/executor.h | 62 +++++ .../framework/framework_executor_callback.h | 66 +++++ .../include/public/framework_types.h | 57 +++++ .../include/public/iauth_driver_hdi.h | 37 +++ .../include/public/iauth_executor_hdi.h | 54 ++++ .../include/public/idriver_manager.h | 42 ++++ .../include/public/iexecute_callback.h | 39 +++ .../include/system_ability_status_listener.h | 47 ++++ .../src/async_command/async_command_base.cpp | 96 +++++++ .../src/async_command/auth_command.cpp | 97 +++++++ .../src/async_command/custom_command.cpp | 87 +++++++ .../src/async_command/enroll_command.cpp | 95 +++++++ .../src/async_command/identify_command.cpp | 95 +++++++ common/executors/src/driver.cpp | 77 ++++++ common/executors/src/driver_manager.cpp | 213 ++++++++++++++++ common/executors/src/executor.cpp | 148 +++++++++++ .../framework/framework_executor_callback.cpp | 237 ++++++++++++++++++ common/executors/src/idriver_manager.cpp | 31 +++ .../src/system_ability_status_listener.cpp | 64 +++++ .../logs/co_auth/inc/coauth_hilog_wrapper.h | 4 +- common/logs/iam_logger.h | 2 + .../user_auth/inc/userauth_hilog_wrapper.h | 4 +- common/logs/user_idm/useridm_hilog_wrapper.h | 4 +- common/thread_pool/thread_groups.cpp | 4 +- common/thread_pool/thread_groups.h | 4 +- common/utils/iam_check.h | 44 ++++ common/utils/iam_mem.cpp | 37 +++ common/utils/iam_mem.h | 42 ++++ common/utils/iam_para2str.h | 49 ++++ common/utils/iam_ptr.h | 4 +- 42 files changed, 2305 insertions(+), 13 deletions(-) create mode 100644 common/executors/BUILD.gn create mode 100644 common/executors/include/async_command/async_command_base.h create mode 100644 common/executors/include/async_command/auth_command.h create mode 100644 common/executors/include/async_command/custom_command.h create mode 100644 common/executors/include/async_command/enroll_command.h create mode 100644 common/executors/include/async_command/iasync_command.h create mode 100644 common/executors/include/async_command/identify_command.h create mode 100644 common/executors/include/driver.h create mode 100644 common/executors/include/driver_manager.h create mode 100644 common/executors/include/executor.h create mode 100644 common/executors/include/framework/framework_executor_callback.h create mode 100644 common/executors/include/public/framework_types.h create mode 100644 common/executors/include/public/iauth_driver_hdi.h create mode 100644 common/executors/include/public/iauth_executor_hdi.h create mode 100644 common/executors/include/public/idriver_manager.h create mode 100644 common/executors/include/public/iexecute_callback.h create mode 100644 common/executors/include/system_ability_status_listener.h create mode 100644 common/executors/src/async_command/async_command_base.cpp create mode 100644 common/executors/src/async_command/auth_command.cpp create mode 100644 common/executors/src/async_command/custom_command.cpp create mode 100644 common/executors/src/async_command/enroll_command.cpp create mode 100644 common/executors/src/async_command/identify_command.cpp create mode 100644 common/executors/src/driver.cpp create mode 100644 common/executors/src/driver_manager.cpp create mode 100644 common/executors/src/executor.cpp create mode 100644 common/executors/src/framework/framework_executor_callback.cpp create mode 100644 common/executors/src/idriver_manager.cpp create mode 100644 common/executors/src/system_ability_status_listener.cpp create mode 100644 common/utils/iam_check.h create mode 100644 common/utils/iam_mem.cpp create mode 100644 common/utils/iam_mem.h create mode 100644 common/utils/iam_para2str.h diff --git a/.gitignore b/.gitignore index 8047e0a..b12ade2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,6 @@ *.out *.app - +# Hidden +.* +!/.gitignore diff --git a/bundle.json b/bundle.json index 71da276..70cd0b2 100644 --- a/bundle.json +++ b/bundle.json @@ -38,6 +38,7 @@ "//base/user_iam/user_auth/sa_profile:userauth_sa_profile", "//base/user_iam/user_auth/sa_profile:useriam.init", "//base/user_iam/user_auth/services:userauthservice", + "//base/user_iam/user_auth/common/executors:userauth_executors", "//base/user_iam/user_auth/services/co_auth/common:useriam_common_lib", "//base/user_iam/user_auth/frameworks/js/napi/user_idm:useridm" ], diff --git a/common/BUILD.gn b/common/BUILD.gn index 78ba89b..47ed9f7 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -43,3 +43,26 @@ ohos_source_set("iam_thread_pool") { part_name = "user_auth" subsystem_name = "useriam" } + +config("iam_utils_config") { + include_dirs = [ "utils" ] +} + +ohos_source_set("iam_utils") { + include_dirs = [ + "utils", + "logs", + ] + + sources = [ "utils/iam_mem.cpp" ] + + external_deps = [ + "hilog_native:libhilog", + "utils_base:utils", + ] + + public_configs = [ ":iam_utils_config" ] + + part_name = "user_auth" + subsystem_name = "useriam" +} diff --git a/common/executors/BUILD.gn b/common/executors/BUILD.gn new file mode 100644 index 0000000..7bddfb2 --- /dev/null +++ b/common/executors/BUILD.gn @@ -0,0 +1,66 @@ +# 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. + +import("//build/ohos.gni") + +config("userauth_executors_public_config") { + include_dirs = [ "include/public" ] +} + +ohos_shared_library("userauth_executors") { + sources = [ + "src/async_command/async_command_base.cpp", + "src/async_command/auth_command.cpp", + "src/async_command/custom_command.cpp", + "src/async_command/enroll_command.cpp", + "src/async_command/identify_command.cpp", + "src/driver.cpp", + "src/driver_manager.cpp", + "src/executor.cpp", + "src/framework/framework_executor_callback.cpp", + "src/idriver_manager.cpp", + "src/system_ability_status_listener.cpp", + ] + include_dirs = [ + "include", + "include/async_command", + "include/framework", + "//base/user_iam/user_auth/interfaces/inner_api/user_auth/", + "//base/user_iam/user_auth/interfaces/inner_api/common/", + "//base/user_iam/user_auth/common/utils", + "//base/user_iam/user_auth/common/logs", + "//base/user_iam/user_auth/services/co_auth/common/coauth/inc/", + "//base/user_iam/user_auth/services/co_auth/common/common/inc/", + ] + + public_configs = [ ":userauth_executors_public_config" ] + + remove_configs = [ "//build/config/compiler:no_exceptions" ] + + deps = [ "//base/user_iam/user_auth/common:iam_utils" ] + + external_deps = [ + "device_driver_framework:libhdf_host", + "device_driver_framework:libhdf_ipc_adapter", + "device_driver_framework:libhdf_utils", + "device_driver_framework:libhdi", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "samgr_standard:samgr_proxy", + "startup_l2:syspara_watchagent", + "user_auth:coauth_framework", + "utils_base:utils", + ] + + part_name = "user_auth" +} diff --git a/common/executors/include/async_command/async_command_base.h b/common/executors/include/async_command/async_command_base.h new file mode 100644 index 0000000..6475d17 --- /dev/null +++ b/common/executors/include/async_command/async_command_base.h @@ -0,0 +1,61 @@ +/* + * 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 ASYNC_COMMAND_BASE_H +#define ASYNC_COMMAND_BASE_H + +#include +#include +#include "co_auth_defines.h" +#include "executor.h" +#include "nocopyable.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class AsyncCommandBase : public std::enable_shared_from_this, + public IAsyncCommand, + public IExecuteCallback, + public NoCopyable { +public: + AsyncCommandBase(std::string type, uint64_t scheduleId, std::shared_ptr executor); + virtual ~AsyncCommandBase() = default; + + void OnHdiDisconnect() override; + ResultCode StartProcess() override; + void OnResult(ResultCode result) override; + void OnResult(ResultCode result, const std::vector &extraInfo) override; + virtual void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override = 0; + +protected: + static int32_t GenerateCommandId(); + virtual ResultCode SendRequest() = 0; + virtual void OnResultInner(ResultCode result, const std::vector &extraInfo) = 0; + const char *GetDescription(); + uint64_t scheduleId_; + std::shared_ptr executor_; + uint32_t commandId_; + +private: + void EndProcess(); + std::string str_; + std::promise promise_; + std::future future_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // ASYNC_COMMAND_BASE_H \ No newline at end of file diff --git a/common/executors/include/async_command/auth_command.h b/common/executors/include/async_command/auth_command.h new file mode 100644 index 0000000..00af533 --- /dev/null +++ b/common/executors/include/async_command/auth_command.h @@ -0,0 +1,44 @@ +/* + * 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 AUTH_COMMAND_H +#define AUTH_COMMAND_H + +#include "async_command_base.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class AuthCommand : public AsyncCommandBase { +public: + AuthCommand(std::shared_ptr executor, uint64_t scheduleId, + std::shared_ptr commandAttrs); + virtual ~AuthCommand() = default; + + void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; + +protected: + void OnResultInner(ResultCode result, const std::vector &extraInfo) override; + ResultCode SendRequest() override; + +private: + uint32_t transNum_ = 1; + std::shared_ptr attributes_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // AUTH_COMMAND_H \ No newline at end of file diff --git a/common/executors/include/async_command/custom_command.h b/common/executors/include/async_command/custom_command.h new file mode 100644 index 0000000..6c71508 --- /dev/null +++ b/common/executors/include/async_command/custom_command.h @@ -0,0 +1,47 @@ +/* + * 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 CUSTOM_COMMAND_H +#define CUSTOM_COMMAND_H + +#include "async_command_base.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class CustomCommand : public AsyncCommandBase { +public: + CustomCommand(std::shared_ptr executor, std::shared_ptr attributes); + virtual ~CustomCommand() = default; + + void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; + + ResultCode GetResult(); + +protected: + void OnResultInner(ResultCode result, const std::vector &extraInfo) override; + ResultCode SendRequest() override; + +private: + std::shared_ptr attributes_; + ResultCode result_; + std::promise promise_; + std::future future_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // CUSTOM_COMMAND_H \ No newline at end of file diff --git a/common/executors/include/async_command/enroll_command.h b/common/executors/include/async_command/enroll_command.h new file mode 100644 index 0000000..2990e41 --- /dev/null +++ b/common/executors/include/async_command/enroll_command.h @@ -0,0 +1,44 @@ +/* + * 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 ENROLL_COMMAND_H +#define ENROLL_COMMAND_H + +#include "async_command_base.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class EnrollCommand : public AsyncCommandBase { +public: + EnrollCommand(std::shared_ptr executor, uint64_t scheduleId, + std::shared_ptr commandAttrs); + virtual ~EnrollCommand() = default; + + void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; + +protected: + void OnResultInner(ResultCode result, const std::vector &extraInfo) override; + ResultCode SendRequest() override; + +private: + uint32_t transNum_ = 1; + std::shared_ptr attributes_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // ENROLL_COMMAND_H \ No newline at end of file diff --git a/common/executors/include/async_command/iasync_command.h b/common/executors/include/async_command/iasync_command.h new file mode 100644 index 0000000..5f9ac53 --- /dev/null +++ b/common/executors/include/async_command/iasync_command.h @@ -0,0 +1,37 @@ +/* + * 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 IASYNC_COMMAND_H +#define IASYNC_COMMAND_H + +#include +#include "co_auth_defines.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class IAsyncCommand { +public: + IAsyncCommand() = default; + virtual ~IAsyncCommand() = default; + + virtual void OnHdiDisconnect() = 0; + virtual ResultCode StartProcess() = 0; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // IASYNC_COMMAND_H \ No newline at end of file diff --git a/common/executors/include/async_command/identify_command.h b/common/executors/include/async_command/identify_command.h new file mode 100644 index 0000000..e737548 --- /dev/null +++ b/common/executors/include/async_command/identify_command.h @@ -0,0 +1,44 @@ +/* + * 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 IDENTIFY_COMMAND_H +#define IDENTIFY_COMMAND_H + +#include "async_command_base.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class IdentifyCommand : public AsyncCommandBase { +public: + IdentifyCommand(std::shared_ptr executor, uint64_t scheduleId, + std::shared_ptr commandAttrs); + virtual ~IdentifyCommand() = default; + + void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) override; + +protected: + void OnResultInner(ResultCode result, const std::vector &extraInfo) override; + ResultCode SendRequest() override; + +private: + uint32_t transNum_ = 1; + std::shared_ptr attributes_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // IDENTIFY_COMMAND_H \ No newline at end of file diff --git a/common/executors/include/driver.h b/common/executors/include/driver.h new file mode 100644 index 0000000..2dcf9aa --- /dev/null +++ b/common/executors/include/driver.h @@ -0,0 +1,47 @@ +/* + * 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 EXECUTOR_DRIVER_H +#define EXECUTOR_DRIVER_H + +#include +#include +#include "executor.h" +#include "idriver_manager.h" +#include "nocopyable.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class Driver : public NoCopyable { +public: + Driver(const std::string &serviceName, HdiConfig hdiConfig); + virtual ~Driver() = default; + + void OnHdiConnect(); + void OnHdiDisconnect(); + void OnFrameworkReady(); + +private: + std::recursive_mutex mutex_; + std::string serviceName_; + HdiConfig hdiConfig_; + std::vector> executorList_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // EXECUTOR_DRIVER_H \ No newline at end of file diff --git a/common/executors/include/driver_manager.h b/common/executors/include/driver_manager.h new file mode 100644 index 0000000..91a3d32 --- /dev/null +++ b/common/executors/include/driver_manager.h @@ -0,0 +1,58 @@ +/* + * 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 DRIVER_MANAGER_H +#define DRIVER_MANAGER_H + +#include +#include +#include "driver.h" +#include "iauth_driver_hdi.h" +#include "idriver_manager.h" +#include "iservstat_listener_hdi.h" +#include "singleton.h" +#include "system_ability_status_listener.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +using namespace HDI::ServiceManager::V1_0; + +class DriverManager : public ServStatListenerStub, public Singleton { +public: + friend SystemAbilityStatusListener; + + void OnReceive(const ServiceStatus &status) override; + int32_t Start(const std::map &hdiName2Config); + void OnFrameworkReady(); + +private: + bool HdiConfigIsValid(const std::map &hdiName2Config); + bool HdiDriverIsRunning(const std::string &serviceName); + void SubscribeHdiDriverStatus(); + void SubscribeHdiManagerServiceStatus(); + void SubscribeFrameworkRedayEvent(); + void OnAllHdiDisconnect(); + void OnAllHdiConnect(); + + std::recursive_mutex mutex_; + std::map> serviceName2Driver_; + std::map hdiName2Config_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // DRIVER_MANAGER_H \ No newline at end of file diff --git a/common/executors/include/executor.h b/common/executors/include/executor.h new file mode 100644 index 0000000..6f0e496 --- /dev/null +++ b/common/executors/include/executor.h @@ -0,0 +1,62 @@ +/* + * 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 EXCECUTOR_H +#define EXCECUTOR_H + +#include +#include +#include +#include +#include "iasync_command.h" +#include "iauth_executor_hdi.h" +#include "iexecute_callback.h" +#include "iexecutor_messenger.h" +#include "nocopyable.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class Executor : public std::enable_shared_from_this, public NoCopyable { +public: + Executor(std::shared_ptr executorHdi, uint16_t hdiId); + virtual ~Executor() = default; + + void OnHdiConnect(); + void OnHdiDisconnect(); + void OnFrameworkReady(); + void SetExecutorMessenger(const sptr &messenger); + sptr GetExecutorMessenger(); + void AddCommand(std::shared_ptr command); + void RemoveCommand(std::shared_ptr command); + std::shared_ptr GetExecutorHdi(); + const char *GetDescription(); + +private: + void RegisterExecutorCallback(ExecutorInfo &executorInfo); + void RespondCallbackOnDisconnect(); + void SetStr(const int32_t executorId); + sptr executorMessenger_; + std::recursive_mutex mutex_; + std::set> command2Respond_; + std::shared_ptr executorHdi_; + std::string str_; + uint16_t hdiId_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // EXCECUTOR_H \ No newline at end of file diff --git a/common/executors/include/framework/framework_executor_callback.h b/common/executors/include/framework/framework_executor_callback.h new file mode 100644 index 0000000..cfe107e --- /dev/null +++ b/common/executors/include/framework/framework_executor_callback.h @@ -0,0 +1,66 @@ +/* + * 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 FRAMEWORK_EXECUTOR_CALLBACK_H +#define FRAMEWORK_EXECUTOR_CALLBACK_H + +#include +#include "auth_attributes.h" +#include "auth_executor.h" +#include "co_auth_defines.h" +#include "coauth.h" +#include "executor.h" +#include "executor_callback.h" +#include "nocopyable.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +using pAuthAttributes = std::shared_ptr; +class FrameworkExecutorCallback : public AuthResPool::ExecutorCallback, public NoCopyable { +public: + explicit FrameworkExecutorCallback(std::shared_ptr executor); + virtual ~FrameworkExecutorCallback() = 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, std::vector &publicKey, + std::vector &templateIds) override; + int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) override; + +private: + ResultCode OnBeginExecuteInner(uint64_t scheduleId, std::vector &publicKey, pAuthAttributes commandAttrs); + ResultCode OnEndExecuteInner(uint64_t scheduleId, pAuthAttributes consumerAttr); + ResultCode OnSetPropertyInner(pAuthAttributes properties); + ResultCode OnGetPropertyInner( + std::shared_ptr conditions, std::shared_ptr values); + ResultCode ProcessEnrollCommand(uint64_t scheduleId, pAuthAttributes properties); + ResultCode ProcessAuthCommand(uint64_t scheduleId, pAuthAttributes properties); + ResultCode ProcessIdentifyCommand(uint64_t scheduleId, pAuthAttributes properties); + ResultCode ProcessCancelCommand(uint64_t scheduleId); + ResultCode ProcessDeleteTemplateCommand(pAuthAttributes properties); + ResultCode ProcessCustomCommand(pAuthAttributes properties); + ResultCode ProcessGetTemplateCommand( + std::shared_ptr conditions, std::shared_ptr values); + + std::shared_ptr executor_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // FRAMEWORK_EXECUTOR_CALLBACK_H \ No newline at end of file diff --git a/common/executors/include/public/framework_types.h b/common/executors/include/public/framework_types.h new file mode 100644 index 0000000..2e2a7f2 --- /dev/null +++ b/common/executors/include/public/framework_types.h @@ -0,0 +1,57 @@ +/* + * 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 FRAMEWORK_TYPES_H +#define FRAMEWORK_TYPES_H + +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +enum UserAuthResult : int32_t { + USERAUTH_SUCCESS = 0, + USERAUTH_ERROR = 1, +}; + +enum CommandId : int32_t { + LOCK_TEMPLATE = 0, + UNLOCK_TEMPLATE = 1, + VENDOR_COMMAND_BEGIN = 10000, +}; + +struct TemplateInfo { + uint32_t executorType; + int32_t freezingTime; + int32_t remainTimes; + std::vector extraInfo; +}; + +enum AuthPropertyMode : int32_t { + PROPERMODE_DELETE = 0, + PROPERMODE_GET = 1, + PROPERMODE_SET = 2, + PROPERMODE_FREEZE = 3, + PROPERMODE_UNFREEZE = 4, + PROPERMODE_INIT_ALGORITHM = 5, + PROPERMODE_RELEASE_ALGORITHM = 6, + PROPERMODE_SET_SURFACE_ID = 100, +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // FRAMEWORK_TYPES_H diff --git a/common/executors/include/public/iauth_driver_hdi.h b/common/executors/include/public/iauth_driver_hdi.h new file mode 100644 index 0000000..86c604b --- /dev/null +++ b/common/executors/include/public/iauth_driver_hdi.h @@ -0,0 +1,37 @@ +/* + * 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 IAUTH_DRIVER_HDI_H +#define IAUTH_DRIVER_HDI_H + +#include +#include "iauth_executor_hdi.h" +#include "iremote_broker.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class IAuthDriverHdi { +public: + IAuthDriverHdi() = default; + virtual ~IAuthDriverHdi() = default; + + virtual void GetExecutorList(std::vector> &executorList) = 0; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // IAUTH_DRIVER_HDI_H \ No newline at end of file diff --git a/common/executors/include/public/iauth_executor_hdi.h b/common/executors/include/public/iauth_executor_hdi.h new file mode 100644 index 0000000..0132562 --- /dev/null +++ b/common/executors/include/public/iauth_executor_hdi.h @@ -0,0 +1,54 @@ +/* + * 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 IAUTH_EXECUTOR_HDI_H +#define IAUTH_EXECUTOR_HDI_H + +#include +#include +#include "co_auth_defines.h" +#include "framework_types.h" +#include "iexecute_callback.h" +#include "iauth_executor_hdi.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class IAuthExecutorHdi { +public: + IAuthExecutorHdi() = default; + virtual ~IAuthExecutorHdi() = default; + + virtual UserIAM::ResultCode GetExecutorInfo(ExecutorInfo &info) = 0; + virtual UserIAM::ResultCode GetTemplateInfo(uint64_t templateId, UserAuth::TemplateInfo &info) = 0; + virtual UserIAM::ResultCode OnRegisterFinish(const std::vector &templateIdList, + const std::vector &frameworkPublicKey, const std::vector &extraInfo) = 0; + virtual UserIAM::ResultCode Enroll(uint64_t scheduleId, uint64_t callerUid, const std::vector &extraInfo, + const std::shared_ptr &callbackObj) = 0; + virtual UserIAM::ResultCode Authenticate(uint64_t scheduleId, uint64_t callerUid, + const std::vector &templateIdList, const std::vector &extraInfo, + const std::shared_ptr &callbackObj) = 0; + virtual UserIAM::ResultCode Identify(uint64_t scheduleId, uint64_t callerUid, const std::vector &extraInfo, + const std::shared_ptr &callbackObj) = 0; + virtual UserIAM::ResultCode Delete(const std::vector &templateIdList) = 0; + virtual UserIAM::ResultCode Cancel(uint64_t scheduleId) = 0; + virtual UserIAM::ResultCode SendCommand(UserAuth::AuthPropertyMode commandId, const std::vector &extraInfo, + const std::shared_ptr &callbackObj) = 0; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // IAUTH_EXECUTOR_HDI_H \ No newline at end of file diff --git a/common/executors/include/public/idriver_manager.h b/common/executors/include/public/idriver_manager.h new file mode 100644 index 0000000..c4d0cc0 --- /dev/null +++ b/common/executors/include/public/idriver_manager.h @@ -0,0 +1,42 @@ +/* + * 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 IDRIVER_MANAGER_H +#define IDRIVER_MANAGER_H + +#include +#include +#include "iauth_driver_hdi.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +struct HdiConfig { + uint16_t id; // non-zero + std::shared_ptr driver; +}; + +class IDriverManager { +public: + IDriverManager() = default; + virtual ~IDriverManager() = default; + + static int32_t Start(const std::map &hdiName2Config); +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // IDRIVER_MANAGER_H \ No newline at end of file diff --git a/common/executors/include/public/iexecute_callback.h b/common/executors/include/public/iexecute_callback.h new file mode 100644 index 0000000..2ec001e --- /dev/null +++ b/common/executors/include/public/iexecute_callback.h @@ -0,0 +1,39 @@ +/* + * 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 IEXECUTE_CALLBACK_H +#define IEXECUTE_CALLBACK_H + +#include +#include +#include "co_auth_defines.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class IExecuteCallback { +public: + IExecuteCallback() = default; + virtual ~IExecuteCallback() = default; + + virtual void OnResult(ResultCode result, const std::vector &extraInfo) = 0; + virtual void OnResult(ResultCode result) = 0; + virtual void OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) = 0; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // EXECUTE_CALLBACK_H \ No newline at end of file diff --git a/common/executors/include/system_ability_status_listener.h b/common/executors/include/system_ability_status_listener.h new file mode 100644 index 0000000..747ad3f --- /dev/null +++ b/common/executors/include/system_ability_status_listener.h @@ -0,0 +1,47 @@ +/* + * 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 SYSTEM_ABILITY_STATUS_LISTENER +#define SYSTEM_ABILITY_STATUS_LISTENER + +#include +#include +#include +#include "nocopyable.h" +#include "refbase.h" +#include "system_ability_status_change_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class SystemAbilityStatusListener : public OHOS::SystemAbilityStatusChangeStub, public NoCopyable { +public: + static sptr GetInstance(); + + virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + +private: + SystemAbilityStatusListener() = default; + virtual ~SystemAbilityStatusListener() = default; + + static sptr instance_; + static std::mutex mutex_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // SYSTEM_ABILITY_STATUS_LISTENER \ No newline at end of file diff --git a/common/executors/src/async_command/async_command_base.cpp b/common/executors/src/async_command/async_command_base.cpp new file mode 100644 index 0000000..84cb34b --- /dev/null +++ b/common/executors/src/async_command/async_command_base.cpp @@ -0,0 +1,96 @@ +/* + * 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 "async_command_base.h" +#include +#include +#include +#include +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_para2str.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +AsyncCommandBase::AsyncCommandBase(std::string type, uint64_t scheduleId, std::shared_ptr executor) + : scheduleId_(scheduleId), + executor_(executor) +{ + commandId_ = GenerateCommandId(); + std::ostringstream ss; + ss << "Command(type:" << type << ", id:" << commandId_ << ", scheduleId:" << Common::GetMaskedString(scheduleId_) + << ")"; + str_ = ss.str(); +} + +void AsyncCommandBase::OnHdiDisconnect() +{ + IAM_LOGE("driver disconnect, %{public}s end process", GetDescription()); + // Need new result code: hal invalid + OnResult(ResultCode::GENERAL_ERROR); +} + +ResultCode AsyncCommandBase::StartProcess() +{ + IAM_LOGI("%{public}s start process", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + + executor_->AddCommand(shared_from_this()); + ResultCode ret = SendRequest(); + if (ret != ResultCode::SUCCESS) { + IAM_LOGE("send request failed"); + return ret; + } + return ResultCode::SUCCESS; +} + +void AsyncCommandBase::OnResult(ResultCode result) +{ + std::vector extraInfo; + OnResult(result, extraInfo); +} + +void AsyncCommandBase::OnResult(ResultCode result, const std::vector &extraInfo) +{ + OnResultInner(result, extraInfo); + EndProcess(); +} + +void AsyncCommandBase::EndProcess() +{ + IAM_LOGI("%{public}s end process", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + executor_->RemoveCommand(shared_from_this()); +} + +const char *AsyncCommandBase::GetDescription() +{ + return str_.c_str(); +} + +int32_t AsyncCommandBase::GenerateCommandId() +{ + static std::mutex mutex; + static uint32_t commandId = 0; + std::lock_guard guard(mutex); + commandId++; + return commandId; +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/async_command/auth_command.cpp b/common/executors/src/async_command/auth_command.cpp new file mode 100644 index 0000000..99bf191 --- /dev/null +++ b/common/executors/src/async_command/auth_command.cpp @@ -0,0 +1,97 @@ +/* + * 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 "auth_command.h" +#include "framework_types.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_para2str.h" +#include "iam_ptr.h" +#include "pool.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +AuthCommand::AuthCommand( + std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr attributes) + : AsyncCommandBase("AUTH", scheduleId, executor), + attributes_(attributes) +{ +} + +ResultCode AuthCommand::SendRequest() +{ + IAM_LOGI("%{public}s send request start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + + std::vector tempalteIdList; + std::vector extraInfo; + uint64_t templateId = 0; + attributes_->GetUint64Value(AUTH_TEMPLATE_ID, templateId); + uint64_t callerUid; + attributes_->GetUint64Value(AUTH_CALLER_UID, callerUid); + tempalteIdList.push_back(templateId); + + ResultCode ret = hdi->Authenticate(scheduleId_, callerUid, tempalteIdList, extraInfo, shared_from_this()); + IAM_LOGI("%{public}s authenticate result %{public}d", GetDescription(), ret); + return ret; +} + +void AuthCommand::OnResultInner(ResultCode result, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on result start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr authAttributes = Common::MakeShared(); + IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr); + authAttributes->SetUint32Value(AUTH_RESULT_CODE, result); + authAttributes->SetUint8ArrayValue(AUTH_RESULT, nonConstExtraInfo); + int32_t ret = executorMessenger->Finish(scheduleId_, ALL_IN_ONE, result, authAttributes); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call fininsh fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call fininsh success result=%{public}d", GetDescription(), result); +} + +void AuthCommand::OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on acquire info start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr msg = Common::MakeShared(nonConstExtraInfo); + IF_FALSE_LOGE_AND_RETURN(msg != nullptr); + int32_t ret = executorMessenger->SendData(scheduleId_, transNum_, TYPE_ALL_IN_ONE, TYPE_CO_AUTH, msg); + transNum_++; + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call SendData fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call SendData success acquire=%{public}d", GetDescription(), acquire); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/async_command/custom_command.cpp b/common/executors/src/async_command/custom_command.cpp new file mode 100644 index 0000000..4b92aed --- /dev/null +++ b/common/executors/src/async_command/custom_command.cpp @@ -0,0 +1,87 @@ +/* + * 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 "custom_command.h" +#include +#include "iam_check.h" +#include "iam_logger.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +CustomCommand::CustomCommand( + std::shared_ptr executor, std::shared_ptr attributes) + : AsyncCommandBase("CUSTOM", 0, executor), + attributes_(attributes) +{ +} + +ResultCode CustomCommand::SendRequest() +{ + static const size_t MAX_TEMPLATE_LIST_LEN = 1000; + IAM_LOGI("%{public}s send request start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + + future_ = promise_.get_future(); + + uint32_t commandId = 0; + attributes_->GetUint32Value(AUTH_PROPERTY_MODE, commandId); + std::vector templateIdList; + attributes_->GetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, templateIdList); + IF_FALSE_LOGE_AND_RETURN_VAL(templateIdList.size() < MAX_TEMPLATE_LIST_LEN, ResultCode::GENERAL_ERROR); + const uint8_t *src = static_cast(static_cast(&templateIdList[0])); + std::vector extraInfo(src, src + templateIdList.size() * sizeof(uint64_t) / sizeof(uint8_t)); + ResultCode ret = + hdi->SendCommand(static_cast(commandId), extraInfo, shared_from_this()); + IAM_LOGI("%{public}s send command result %{public}d", GetDescription(), ret); + return ret; +} + +void CustomCommand::OnResultInner(ResultCode result, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on result start", GetDescription()); + result_ = result; + promise_.set_value(); +} + +void CustomCommand::OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) +{ + IAM_LOGE("%{public}s not support", GetDescription()); +} + +ResultCode CustomCommand::GetResult() +{ + if (!future_.valid()) { + IAM_LOGE("%{public}s get result before request send, error", GetDescription()); + return ResultCode::GENERAL_ERROR; + } + IAM_LOGI("%{public}s begin wait future", GetDescription()); + const std::chrono::seconds maxWaitTime(1); + auto ret = future_.wait_for(maxWaitTime); + if (ret != std::future_status::ready) { + IAM_LOGE("%{public}s future timeout", GetDescription()); + return ResultCode::TIMEOUT; + } + IAM_LOGI("%{public}s get result %{public}d", GetDescription(), result_); + return result_; +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/async_command/enroll_command.cpp b/common/executors/src/async_command/enroll_command.cpp new file mode 100644 index 0000000..9c28d74 --- /dev/null +++ b/common/executors/src/async_command/enroll_command.cpp @@ -0,0 +1,95 @@ +/* + * 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 "enroll_command.h" +#include "framework_types.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_mem.h" +#include "iam_para2str.h" +#include "iam_ptr.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +EnrollCommand::EnrollCommand( + std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr attributes) + : AsyncCommandBase("ENROLL", scheduleId, executor), + attributes_(attributes) +{ +} + +ResultCode EnrollCommand::SendRequest() +{ + IAM_LOGI("%{public}s send request start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + + std::vector extraInfo; + uint64_t templateId = 0; + attributes_->GetUint64Value(AUTH_TEMPLATE_ID, templateId); + uint64_t callerUid; + attributes_->GetUint64Value(AUTH_CALLER_UID, callerUid); + Common::Pack(extraInfo, templateId); + ResultCode ret = hdi->Enroll(scheduleId_, callerUid, extraInfo, shared_from_this()); + IAM_LOGI("%{public}s enroll result %{public}d", GetDescription(), ret); + return ret; +} + +void EnrollCommand::OnResultInner(ResultCode result, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on result start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr authAttributes = Common::MakeShared(); + IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr); + authAttributes->SetUint32Value(AUTH_RESULT_CODE, result); + authAttributes->SetUint8ArrayValue(AUTH_RESULT, nonConstExtraInfo); + int32_t ret = executorMessenger->Finish(scheduleId_, ALL_IN_ONE, result, authAttributes); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call fininsh fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call fininsh success result=%{public}d", GetDescription(), result); +} + +void EnrollCommand::OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on acquire info start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr msg = Common::MakeShared(nonConstExtraInfo); + IF_FALSE_LOGE_AND_RETURN(msg != nullptr); + int32_t ret = executorMessenger->SendData(scheduleId_, transNum_, TYPE_ALL_IN_ONE, TYPE_CO_AUTH, msg); + transNum_++; + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call SendData fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call SendData success acquire=%{public}d", GetDescription(), acquire); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/async_command/identify_command.cpp b/common/executors/src/async_command/identify_command.cpp new file mode 100644 index 0000000..4fe4fc9 --- /dev/null +++ b/common/executors/src/async_command/identify_command.cpp @@ -0,0 +1,95 @@ +/* + * 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 "identify_command.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_para2str.h" +#include "iam_ptr.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +IdentifyCommand::IdentifyCommand( + std::shared_ptr executor, uint64_t scheduleId, std::shared_ptr attributes) + : AsyncCommandBase("IDENTIFY", scheduleId, executor), + attributes_(attributes) +{ +} + +ResultCode IdentifyCommand::SendRequest() +{ + IAM_LOGI("%{public}s send request start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + + std::vector tempalteIdList; + std::vector extraInfo; + uint64_t templateId = 0; + attributes_->GetUint64Value(AUTH_TEMPLATE_ID, templateId); + uint64_t callerUid; + attributes_->GetUint64Value(AUTH_CALLER_UID, callerUid); + tempalteIdList.push_back(templateId); + + ResultCode ret = hdi->Identify(scheduleId_, callerUid, extraInfo, shared_from_this()); + IAM_LOGI("%{public}s identify result %{public}d", GetDescription(), ret); + return ret; +} + +void IdentifyCommand::OnResultInner(ResultCode result, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on result start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr authAttributes = Common::MakeShared(); + IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr); + authAttributes->SetUint32Value(AUTH_RESULT_CODE, result); + authAttributes->SetUint8ArrayValue(AUTH_RESULT, nonConstExtraInfo); + int32_t ret = executorMessenger->Finish(scheduleId_, ALL_IN_ONE, result, authAttributes); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call fininsh fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call fininsh success result=%{public}d", GetDescription(), result); +} + +void IdentifyCommand::OnAcquireInfo(int32_t acquire, const std::vector &extraInfo) +{ + IAM_LOGI("%{public}s on acquire info start", GetDescription()); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto executorMessenger = executor_->GetExecutorMessenger(); + IF_FALSE_LOGE_AND_RETURN(executorMessenger != nullptr); + + std::vector nonConstExtraInfo(extraInfo.begin(), extraInfo.end()); + std::shared_ptr msg = Common::MakeShared(nonConstExtraInfo); + IF_FALSE_LOGE_AND_RETURN(msg != nullptr); + int32_t ret = executorMessenger->SendData(scheduleId_, transNum_, TYPE_ALL_IN_ONE, TYPE_CO_AUTH, msg); + transNum_++; + if (ret != USERAUTH_SUCCESS) { + IAM_LOGI("%{public}s call SendData fail", GetDescription()); + return; + } + IAM_LOGI("%{public}s call SendData success acquire=%{public}d", GetDescription(), acquire); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/driver.cpp b/common/executors/src/driver.cpp new file mode 100644 index 0000000..36023bf --- /dev/null +++ b/common/executors/src/driver.cpp @@ -0,0 +1,77 @@ +/* + * 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 "driver.h" +#include +#include "iam_logger.h" +#include "iam_ptr.h" +#include "iauth_driver_hdi.h" +#include "iauth_executor_hdi.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +Driver::Driver(const std::string &serviceName, HdiConfig hdiConfig) : serviceName_(serviceName), hdiConfig_(hdiConfig) +{ +} + +void Driver::OnHdiConnect() +{ + IAM_LOGI("start"); + OnHdiDisconnect(); + std::lock_guard lock(mutex_); + std::vector> executorHdiList; + hdiConfig_.driver->GetExecutorList(executorHdiList); + IAM_LOGI("executorHdiList length is %{public}zu", executorHdiList.size()); + for (const auto &executorHdi : executorHdiList) { + auto executor = Common::MakeShared(executorHdi, hdiConfig_.id); + if (executor == nullptr) { + IAM_LOGE("make_shared failed"); + continue; + } + executorList_.push_back(executor); + executor->OnHdiConnect(); + IAM_LOGI("add executor %{public}s success", executor->GetDescription()); + } + IAM_LOGI("success"); +} + +void Driver::OnHdiDisconnect() +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + for (const auto &executor : executorList_) { + // executor is non-null + executor->OnHdiDisconnect(); + } + executorList_.clear(); + IAM_LOGI("success"); +} + +void Driver::OnFrameworkReady() +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + for (const auto &executor : executorList_) { + // executor is non-null + executor->OnFrameworkReady(); + } + IAM_LOGI("success"); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/driver_manager.cpp b/common/executors/src/driver_manager.cpp new file mode 100644 index 0000000..e92083f --- /dev/null +++ b/common/executors/src/driver_manager.cpp @@ -0,0 +1,213 @@ +/* + * 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 "driver_manager.h" +#include +#include "devsvc_manager_stub.h" +#include "hdf_device_desc.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_ptr.h" +#include "iservice_registry.h" +#include "iservmgr_hdi.h" +#include "parameter.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +const std::string GROUP_SCHEDULE = "GROUP_SCHEDULE"; +const char IAM_EVENT_KEY[] = "bootevent.useriam.fwkready"; +int32_t DriverManager::Start(const std::map &hdiName2Config) +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + if (!HdiConfigIsValid(hdiName2Config)) { + IAM_LOGE("service config is not valid"); + return USERAUTH_ERROR; + } + hdiName2Config_ = hdiName2Config; + serviceName2Driver_.clear(); + for (auto const &pair : hdiName2Config) { + auto driver = Common::MakeShared(pair.first, pair.second); + if (driver == nullptr) { + IAM_LOGE("make_shared for driver %{public}s failed", pair.first.c_str()); + continue; + } + serviceName2Driver_[pair.first] = driver; + IAM_LOGI("add driver %{public}s", pair.first.c_str()); + } + SubscribeHdiManagerServiceStatus(); + SubscribeHdiDriverStatus(); + SubscribeFrameworkRedayEvent(); + OnAllHdiConnect(); + IAM_LOGI("success"); + return USERAUTH_SUCCESS; +} + +bool DriverManager::HdiConfigIsValid(const std::map &hdiName2Config) +{ + std::set idSet; + for (auto const &pair : hdiName2Config) { + uint16_t id = pair.second.id; + if (idSet.count(id) != 0) { + IAM_LOGE("duplicate hdi id %{public}d", id); + return false; + } + idSet.insert(id); + } + return true; +} + +void DriverManager::SubscribeHdiDriverStatus() +{ + IAM_LOGI("start"); + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + IAM_LOGE("failed to get IServiceManager"); + return; + } + + int32_t ret = servMgr->RegisterServiceStatusListener(this, DEVICE_CLASS_USERAUTH); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGE("failed to register service status listener"); + return; + } + IAM_LOGI("success"); +} + +bool DriverManager::HdiDriverIsRunning(const std::string &serviceName) +{ + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + IAM_LOGE("failed to get IServiceManager"); + return false; + } + + auto service = servMgr->GetService(serviceName.c_str()); + if (service == nullptr) { + IAM_LOGE("hdi driver is not running"); + return false; + } + + IAM_LOGI("hdi driver is running"); + return true; +} + +void DriverManager::OnReceive(const ServiceStatus &status) +{ + IAM_LOGI("service %{public}s receive status", status.serviceName.c_str()); + std::lock_guard lock(mutex_); + if (serviceName2Driver_.count(status.serviceName) == 0) { + IAM_LOGI("service name not match"); + return; + } + + switch (status.status) { + case SERVIE_STATUS_START: + IAM_LOGI("service %{public}s status change to start", status.serviceName.c_str()); + if (!HdiDriverIsRunning(status.serviceName)) { + IAM_LOGE("service %{public}s is not running, ignore this message", status.serviceName.c_str()); + break; + } + serviceName2Driver_[status.serviceName]->OnHdiConnect(); + break; + case SERVIE_STATUS_STOP: + IAM_LOGI("service %{public}s status change to stop", status.serviceName.c_str()); + if (HdiDriverIsRunning(status.serviceName)) { + IAM_LOGE("service %{public}s is running, ignore this message", status.serviceName.c_str()); + break; + } + serviceName2Driver_[status.serviceName]->OnHdiDisconnect(); + break; + default: + IAM_LOGE("service %{public}s status invalid", status.serviceName.c_str()); + } +} + +void DriverManager::SubscribeHdiManagerServiceStatus() +{ + IAM_LOGI("start"); + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + IAM_LOGE("failed to get SA manager"); + return; + } + auto listener = SystemAbilityStatusListener::GetInstance(); + IF_FALSE_LOGE_AND_RETURN(listener != nullptr); + int32_t ret = sam->SubscribeSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID, listener); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGE("failed to subscribe status"); + return; + } + IAM_LOGI("success"); +} + +void DriverManager::SubscribeFrameworkRedayEvent() +{ + auto eventCallback = [](const char *key, const char *value, void *context) { + IAM_LOGI("receive useriam.fwkready event"); + IF_FALSE_LOGE_AND_RETURN(key != nullptr); + IF_FALSE_LOGE_AND_RETURN(value != nullptr); + if (strcmp(key, IAM_EVENT_KEY) != 0) { + IAM_LOGE("event key mismatch"); + return; + } + if (strcmp(value, "true")) { + IAM_LOGI("event value is not true"); + return; + } + DriverManager::GetInstance().OnFrameworkReady(); + }; + int32_t ret = WatchParameter(IAM_EVENT_KEY, eventCallback, nullptr); + if (ret != USERAUTH_SUCCESS) { + IAM_LOGE("WatchParameter fail"); + return; + } + IAM_LOGI("success"); +} + +void DriverManager::OnAllHdiDisconnect() +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + for (auto const &pair : serviceName2Driver_) { + pair.second->OnHdiDisconnect(); + } + IAM_LOGI("success"); +} + +void DriverManager::OnAllHdiConnect() +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + for (auto const &pair : serviceName2Driver_) { + pair.second->OnHdiConnect(); + } + IAM_LOGI("success"); +} + +void DriverManager::OnFrameworkReady() +{ + IAM_LOGI("start"); + std::lock_guard lock(mutex_); + for (auto const &pair : serviceName2Driver_) { + pair.second->OnFrameworkReady(); + } +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/executor.cpp b/common/executors/src/executor.cpp new file mode 100644 index 0000000..46e5c12 --- /dev/null +++ b/common/executors/src/executor.cpp @@ -0,0 +1,148 @@ +/* + * 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 "executor.h" +#include +#include "co_auth_defines.h" +#include "executor_mgr.h" +#include "framework_executor_callback.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_mem.h" +#include "iam_para2str.h" +#include "iam_ptr.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +const int32_t INVALID_EXECUTOR_ID = -1; + +Executor::Executor(std::shared_ptr executorHdi, uint16_t hdiId) + : executorHdi_(executorHdi), + hdiId_(hdiId) +{ + SetStr(INVALID_EXECUTOR_ID); +} + +void Executor::OnHdiConnect() +{ + IAM_LOGI("%{public}s start", GetDescription()); + // register resource pool depends on hdi start, after hid connect re-register resource pool + OnFrameworkReady(); +} + +void Executor::OnHdiDisconnect() +{ + IAM_LOGI("%{public}s start", GetDescription()); + executorHdi_ = nullptr; +} + +void Executor::OnFrameworkReady() +{ + IAM_LOGI("%{public}s start", GetDescription()); + ExecutorInfo executorInfo = {}; + auto hdi = executorHdi_; + if (hdi == nullptr) { + IAM_LOGI("executorHdi_ is disconnected, skip framework ready process"); + return; + } + ResultCode ret = hdi->GetExecutorInfo(executorInfo); + if (ret != ResultCode::SUCCESS) { + IAM_LOGE("Get executor info failed"); + return; + } + SetStr(executorInfo.executorId); + RegisterExecutorCallback(executorInfo); +} + +void Executor::RegisterExecutorCallback(ExecutorInfo &executorInfo) +{ + IAM_LOGI("%{public}s start", GetDescription()); + executorInfo.executorId = Common::CombineShortToInt(hdiId_, executorInfo.executorId); + auto executorCallback = Common::MakeShared(shared_from_this()); + IF_FALSE_LOGE_AND_RETURN(executorCallback != nullptr); + UserIAM::AuthResPool::ExecutorMgr::GetInstance().Register(executorInfo, executorCallback); + IAM_LOGI("register executor callback ok, executor id=%{public}s", + Common::GetMaskedString(executorInfo.executorId).c_str()); +} + +void Executor::SetExecutorMessenger(const sptr &messenger) +{ + IAM_LOGI("%{public}s start", GetDescription()); + executorMessenger_ = messenger; + return; +} + +sptr Executor::GetExecutorMessenger() +{ + IAM_LOGI("%{public}s start", GetDescription()); + return executorMessenger_; +} + +void Executor::AddCommand(std::shared_ptr command) +{ + IAM_LOGI("%{public}s start", GetDescription()); + std::lock_guard lock(mutex_); + IF_FALSE_LOGE_AND_RETURN(command != nullptr); + command2Respond_.insert(command); +} + +void Executor::RemoveCommand(std::shared_ptr command) +{ + IAM_LOGI("%{public}s start", GetDescription()); + std::lock_guard lock(mutex_); + command2Respond_.erase(command); +} + +void Executor::RespondCallbackOnDisconnect() +{ + IAM_LOGI("%{public}s start", GetDescription()); + std::lock_guard lock(mutex_); + for (auto it = command2Respond_.begin(); it != command2Respond_.end();) { + auto cmdToProc = it; + it++; + (*cmdToProc)->OnHdiDisconnect(); + } + command2Respond_.clear(); + IAM_LOGI("success"); +} + +std::shared_ptr Executor::GetExecutorHdi() +{ + return executorHdi_; +} + +const char *Executor::GetDescription() +{ + std::lock_guard lock(mutex_); + return str_.c_str(); +} + +void Executor::SetStr(const int32_t executorId) +{ + std::lock_guard lock(mutex_); + std::ostringstream ss; + if (executorId == INVALID_EXECUTOR_ID) { + ss << "Executor(hdiId:" << hdiId_ << ", executorId: not set)"; + } else { + ss << "Executor(hdiId:" << hdiId_ << ", executorId:" << executorId << ")"; + } + str_ = ss.str(); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/framework/framework_executor_callback.cpp b/common/executors/src/framework/framework_executor_callback.cpp new file mode 100644 index 0000000..37900e5 --- /dev/null +++ b/common/executors/src/framework/framework_executor_callback.cpp @@ -0,0 +1,237 @@ +/* + * 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 "framework_executor_callback.h" +#include "auth_command.h" +#include "custom_command.h" +#include "enroll_command.h" +#include "iam_check.h" +#include "iam_logger.h" +#include "iam_mem.h" +#include "iam_ptr.h" +#include "identify_command.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +FrameworkExecutorCallback::FrameworkExecutorCallback(std::shared_ptr executor) : executor_(executor) +{ +} + +int32_t FrameworkExecutorCallback::OnBeginExecute( + uint64_t scheduleId, std::vector &publicKey, std::shared_ptr commandAttrs) +{ + return OnBeginExecuteInner(scheduleId, publicKey, commandAttrs); +} + +ResultCode FrameworkExecutorCallback::OnBeginExecuteInner( + uint64_t scheduleId, std::vector &publicKey, std::shared_ptr commandAttrs) +{ + static_cast(publicKey); + IF_FALSE_LOGE_AND_RETURN_VAL(commandAttrs != nullptr, ResultCode::GENERAL_ERROR); + uint32_t commandId = 0; + IF_FALSE_LOGE_AND_RETURN_VAL( + commandAttrs->GetUint32Value(AUTH_SCHEDULE_MODE, commandId) == USERAUTH_SUCCESS, ResultCode::GENERAL_ERROR); + + IAM_LOGI("start process cmd %{public}u", commandId); + ResultCode ret = ResultCode::GENERAL_ERROR; + switch (commandId) { + case SCHEDULE_MODE_ENROLL: + ret = ProcessEnrollCommand(scheduleId, commandAttrs); + break; + case SCHEDULE_MODE_AUTH: + ret = ProcessAuthCommand(scheduleId, commandAttrs); + break; + default: + IAM_LOGE("command id %{public}u is not supported", commandId); + } + + IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret); + return ret; +} + +int32_t FrameworkExecutorCallback::OnEndExecute( + uint64_t scheduleId, std::shared_ptr consumerAttr) +{ + return OnEndExecuteInner(scheduleId, consumerAttr); +} + +ResultCode FrameworkExecutorCallback::OnEndExecuteInner( + uint64_t scheduleId, std::shared_ptr consumerAttr) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(consumerAttr != nullptr, ResultCode::GENERAL_ERROR); + uint32_t commandId = 0; + IF_FALSE_LOGE_AND_RETURN_VAL( + consumerAttr->GetUint32Value(AUTH_SCHEDULE_MODE, commandId) == USERAUTH_SUCCESS, ResultCode::GENERAL_ERROR); + + IAM_LOGI("start process cmd %{public}u", commandId); + ResultCode ret = ResultCode::GENERAL_ERROR; + switch (commandId) { + case SCHEDULE_MODE_AUTH: + __attribute__((fallthrough)); + case SCHEDULE_MODE_ENROLL: + ret = ProcessCancelCommand(scheduleId); + break; + default: + IAM_LOGE("Command id %{public}u is not supported", commandId); + break; + } + IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret); + return ret; +} + +void FrameworkExecutorCallback::OnMessengerReady(const sptr &messenger, + std::vector &publicKey, std::vector &templateIds) +{ + IAM_LOGI("start"); + IF_FALSE_LOGE_AND_RETURN(executor_ != nullptr); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN(hdi != nullptr); + executor_->SetExecutorMessenger(messenger); + std::vector extraInfo; + hdi->OnRegisterFinish(templateIds, publicKey, extraInfo); +} + +int32_t FrameworkExecutorCallback::OnSetProperty(std::shared_ptr properties) +{ + return OnSetPropertyInner(properties); +} +ResultCode FrameworkExecutorCallback::OnSetPropertyInner(std::shared_ptr properties) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(properties != nullptr, ResultCode::GENERAL_ERROR); + uint32_t commandId = 0; + IF_FALSE_LOGE_AND_RETURN_VAL( + properties->GetUint32Value(AUTH_PROPERTY_MODE, commandId) == USERAUTH_SUCCESS, ResultCode::GENERAL_ERROR); + IAM_LOGI("start process cmd %{public}u", commandId); + ResultCode ret = ResultCode::GENERAL_ERROR; + if (commandId == PROPERMODE_DELETE) { + ret = ProcessDeleteTemplateCommand(properties); + } else { + ret = ProcessCustomCommand(properties); + } + IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret); + return ret; +} + +int32_t FrameworkExecutorCallback::OnGetProperty( + std::shared_ptr conditions, std::shared_ptr values) +{ + return OnGetPropertyInner(conditions, values); +} +ResultCode FrameworkExecutorCallback::OnGetPropertyInner( + std::shared_ptr conditions, std::shared_ptr values) +{ + IAM_LOGI("start"); + IF_FALSE_LOGE_AND_RETURN_VAL(conditions != nullptr, ResultCode::GENERAL_ERROR); + uint32_t commandId = 0; + IF_FALSE_LOGE_AND_RETURN_VAL( + conditions->GetUint32Value(AUTH_PROPERTY_MODE, commandId) == USERAUTH_SUCCESS, ResultCode::GENERAL_ERROR); + if (commandId != PROPERMODE_GET) { + IAM_LOGI("command id not recognised"); + return ResultCode::GENERAL_ERROR; + } + ResultCode ret = ProcessGetTemplateCommand(conditions, values); + IAM_LOGI("command id = %{public}u ret = %{public}d", commandId, ret); + return ret; +} + +ResultCode FrameworkExecutorCallback::ProcessEnrollCommand( + uint64_t scheduleId, std::shared_ptr properties) +{ + auto command = Common::MakeShared(executor_, scheduleId, properties); + IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR); + return command->StartProcess(); +} + +ResultCode FrameworkExecutorCallback::ProcessAuthCommand( + uint64_t scheduleId, std::shared_ptr properties) +{ + auto command = Common::MakeShared(executor_, scheduleId, properties); + IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR); + return command->StartProcess(); +} + +ResultCode FrameworkExecutorCallback::ProcessIdentifyCommand( + uint64_t scheduleId, std::shared_ptr properties) +{ + auto command = Common::MakeShared(executor_, scheduleId, properties); + IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR); + return command->StartProcess(); +} + +ResultCode FrameworkExecutorCallback::ProcessCancelCommand(uint64_t scheduleId) +{ + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + hdi->Cancel(scheduleId); + return ResultCode::SUCCESS; +} + +ResultCode FrameworkExecutorCallback::ProcessDeleteTemplateCommand( + std::shared_ptr properties) +{ + IAM_LOGI("start"); + IF_FALSE_LOGE_AND_RETURN_VAL(properties != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + uint64_t templateId = 0; + properties->GetUint64Value(AUTH_TEMPLATE_ID, templateId); + std::vector tempalteIdList; + tempalteIdList.push_back(templateId); + hdi->Delete(tempalteIdList); + return ResultCode::SUCCESS; +} + +ResultCode FrameworkExecutorCallback::ProcessCustomCommand(std::shared_ptr properties) +{ + auto command = Common::MakeShared(executor_, properties); + IF_FALSE_LOGE_AND_RETURN_VAL(command != nullptr, ResultCode::GENERAL_ERROR); + ResultCode ret = command->StartProcess(); + if (ret != ResultCode::SUCCESS) { + IAM_LOGI("start process command fail ret = %{public}d", ret); + return ret; + } + + ret = command->GetResult(); + return ret; +} + +ResultCode FrameworkExecutorCallback::ProcessGetTemplateCommand( + std::shared_ptr conditions, std::shared_ptr values) +{ + IAM_LOGI("start"); + IF_FALSE_LOGE_AND_RETURN_VAL(conditions != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(values != nullptr, ResultCode::GENERAL_ERROR); + IF_FALSE_LOGE_AND_RETURN_VAL(executor_ != nullptr, ResultCode::GENERAL_ERROR); + auto hdi = executor_->GetExecutorHdi(); + IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR); + uint64_t templateId = 0; + conditions->GetUint64Value(AUTH_TEMPLATE_ID, templateId); + TemplateInfo templateInfo = {}; + hdi->GetTemplateInfo(templateId, templateInfo); + uint64_t subType = 0; + Common::UnpackUint64(templateInfo.extraInfo, 0, subType); + values->SetUint64Value(AUTH_SUBTYPE, subType); + values->SetUint32Value(AUTH_REMAIN_TIME, templateInfo.freezingTime); + values->SetUint32Value(AUTH_REMAIN_COUNT, templateInfo.remainTimes); + return ResultCode::SUCCESS; +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/idriver_manager.cpp b/common/executors/src/idriver_manager.cpp new file mode 100644 index 0000000..3848f24 --- /dev/null +++ b/common/executors/src/idriver_manager.cpp @@ -0,0 +1,31 @@ +/* + * 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 "idriver_manager.h" +#include "driver_manager.h" +#include "iam_logger.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +int32_t IDriverManager::Start(const std::map &hdiName2Config) +{ + return Singleton::GetInstance().Start(hdiName2Config); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/executors/src/system_ability_status_listener.cpp b/common/executors/src/system_ability_status_listener.cpp new file mode 100644 index 0000000..fc01669 --- /dev/null +++ b/common/executors/src/system_ability_status_listener.cpp @@ -0,0 +1,64 @@ +/* + * 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 "system_ability_status_listener.h" +#include "driver_manager.h" +#include "iam_logger.h" +#include "system_ability_definition.h" + +#define LOG_LABEL Common::LABEL_USER_AUTH_EXECUTOR + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +std::mutex SystemAbilityStatusListener::mutex_; +sptr SystemAbilityStatusListener::instance_ = nullptr; +sptr SystemAbilityStatusListener::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard gurard(mutex_); + if (instance_ == nullptr) { + instance_ = new (std::nothrow) SystemAbilityStatusListener(); + if (instance_ == nullptr) { + IAM_LOGE("create instance failed"); + } + } + } + return instance_; +} + +void SystemAbilityStatusListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + if (systemAbilityId != DEVICE_SERVICE_MANAGER_SA_ID) { + return; + } + + IAM_LOGI("device service manager SA added"); + Singleton::GetInstance().SubscribeHdiDriverStatus(); +} + +void SystemAbilityStatusListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + if (systemAbilityId != DEVICE_SERVICE_MANAGER_SA_ID) { + return; + } + + IAM_LOGI("device service manager SA removed"); + // when hdi device manager die, hdi driver status is not reliable, disconnect all + Singleton::GetInstance().OnAllHdiDisconnect(); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/common/logs/co_auth/inc/coauth_hilog_wrapper.h b/common/logs/co_auth/inc/coauth_hilog_wrapper.h index 32ac3ff..28d2b7f 100644 --- a/common/logs/co_auth/inc/coauth_hilog_wrapper.h +++ b/common/logs/co_auth/inc/coauth_hilog_wrapper.h @@ -19,8 +19,8 @@ #include "hilog/log.h" namespace OHOS { namespace UserIAM { -#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) -#define FORMATTED(fmt, ...) "[%{public}s] %{public}s# " fmt, FILENAME, __FUNCTION__, ##__VA_ARGS__ +#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define FORMATTED(fmt, ...) "[%{public}s] %{public}s# " fmt, FILENAME, __FUNCTION__, ##__VA_ARGS__ #ifdef COAUTH_HILOGF #undef COAUTH_HILOGF diff --git a/common/logs/iam_logger.h b/common/logs/iam_logger.h index 60e4be6..955d2fe 100644 --- a/common/logs/iam_logger.h +++ b/common/logs/iam_logger.h @@ -46,6 +46,8 @@ static constexpr unsigned int IAM_DOMAIN_ID_USER = 0xD002421; static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_USER_AUTH_NAPI = {LOG_CORE, IAM_DOMAIN_ID_USER, "USER_AUTH_NAPI"}; static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_USER_AUTH_SDK = {LOG_CORE, IAM_DOMAIN_ID_USER, "USER_AUTH_SDK"}; static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_USER_AUTH_SA = {LOG_CORE, IAM_DOMAIN_ID_USER, "USER_AUTH_SA"}; +static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_USER_AUTH_EXECUTOR = { + LOG_CORE, IAM_DOMAIN_ID_USER, "USER_AUTH_EXECUTOR"}; // pin static constexpr unsigned int IAM_DOMAIN_ID_PIN = 0xD002432; diff --git a/common/logs/user_auth/inc/userauth_hilog_wrapper.h b/common/logs/user_auth/inc/userauth_hilog_wrapper.h index cba8c10..24e642f 100644 --- a/common/logs/user_auth/inc/userauth_hilog_wrapper.h +++ b/common/logs/user_auth/inc/userauth_hilog_wrapper.h @@ -20,8 +20,8 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) -#define FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, FILENAME, __FUNCTION__, ##__VA_ARGS__ +#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, FILENAME, __FUNCTION__, ##__VA_ARGS__ #ifdef USERAUTH_HILOGF #undef USERAUTH_HILOGF diff --git a/common/logs/user_idm/useridm_hilog_wrapper.h b/common/logs/user_idm/useridm_hilog_wrapper.h index 2a7e5cd..5b5878f 100644 --- a/common/logs/user_idm/useridm_hilog_wrapper.h +++ b/common/logs/user_idm/useridm_hilog_wrapper.h @@ -21,8 +21,8 @@ namespace OHOS { namespace UserIAM { namespace UserIDM { -#define filename__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) -#define formated__(fmt, ...) "[%{public}s] %{public}s# " fmt, filename__, __FUNCTION__, ##__VA_ARGS__ +#define filename__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define formated__(fmt, ...) "[%{public}s] %{public}s# " fmt, filename__, __FUNCTION__, ##__VA_ARGS__ #ifdef USERIDM_HILOGF #undef USERIDM_HILOGF diff --git a/common/thread_pool/thread_groups.cpp b/common/thread_pool/thread_groups.cpp index 783bd09..32e57dd 100644 --- a/common/thread_pool/thread_groups.cpp +++ b/common/thread_pool/thread_groups.cpp @@ -214,8 +214,8 @@ bool ThreadGroups::ThreadGroup::PostTask(uint64_t transaction, const Task &task) return true; } -void ThreadGroups::ThreadGroup::EnsureTransaction(const ThreadPoolPtr &poolPtr, uint64_t transaction, - const std::string &name) +void ThreadGroups::ThreadGroup::EnsureTransaction( + const ThreadPoolPtr &poolPtr, uint64_t transaction, const std::string &name) { auto task = [transaction, name]() { std::stringstream sstream; diff --git a/common/thread_pool/thread_groups.h b/common/thread_pool/thread_groups.h index b983345..48784ff 100644 --- a/common/thread_pool/thread_groups.h +++ b/common/thread_pool/thread_groups.h @@ -47,12 +47,12 @@ public: bool PostTask(const std::string &name, uint64_t transaction, const Task &task); template && !std::is_class_v>> - bool PostTask(const std::string &name, uint64_t transaction, const T &task, Args &&...args) + bool PostTask(const std::string &name, uint64_t transaction, const T &task, Args &&... args) { static_assert(std::is_invocable::type, typename std::decay::type...>::value, "arguments cannot invocable"); auto invoker = [task, tuple = std::make_tuple(std::forward(args)...)]() mutable { - std::apply([task](auto &&...args) { std::invoke(task, std::forward(args)...); }, tuple); + std::apply([task](auto &&... args) { std::invoke(task, std::forward(args)...); }, tuple); }; return PostTask(name, transaction, invoker); } diff --git a/common/utils/iam_check.h b/common/utils/iam_check.h new file mode 100644 index 0000000..5669a52 --- /dev/null +++ b/common/utils/iam_check.h @@ -0,0 +1,44 @@ +/* + * 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 IAM_CHECK_H +#define IAM_CHECK_H + +#include "iam_logger.h" + +namespace OHOS { +namespace UserIAM { +namespace Common { +// these macros are used to check condition that should never fail +#define IF_FALSE_LOGE_AND_RETURN(cond) \ + do { \ + if (!(cond)) { \ + IAM_LOGE("(" #cond ") check fail, return"); \ + return; \ + } \ + } while (0) + +#define IF_FALSE_LOGE_AND_RETURN_VAL(cond, retVal) \ + do { \ + if (!(cond)) { \ + IAM_LOGE("(" #cond ") check fail, return"); \ + return (retVal); \ + } \ + } while (0) +} // namespace Common +} // namespace UserIAM +} // namespace OHOS + +#endif // IAM_CHECK_H \ No newline at end of file diff --git a/common/utils/iam_mem.cpp b/common/utils/iam_mem.cpp new file mode 100644 index 0000000..e9e3028 --- /dev/null +++ b/common/utils/iam_mem.cpp @@ -0,0 +1,37 @@ + +/* + * 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 "iam_mem.h" +#include "securec.h" + +#define LOG_LABEL LABEL_IAM_COMMON + +namespace OHOS { +namespace UserIAM { +namespace Common { +int32_t UnpackUint64(const std::vector &src, size_t index, uint64_t &data) +{ + if ((src.size() < index) || (src.size() - index < sizeof(uint64_t))) { + return 1; + } + if (memcpy_s(static_cast(&data), sizeof(uint64_t), &src[index], sizeof(uint64_t)) != 0) { + return 1; + } + return 0; +} +} // namespace Common +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/common/utils/iam_mem.h b/common/utils/iam_mem.h new file mode 100644 index 0000000..b4eabf6 --- /dev/null +++ b/common/utils/iam_mem.h @@ -0,0 +1,42 @@ +/* + * 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 IAM_MEM_H +#define IAM_MEM_H + +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace Common { +template +inline void Pack(std::vector &dst, const T &data) +{ + const uint8_t *src = static_cast(static_cast(&data)); + dst.insert(dst.end(), src, src + sizeof(T)); +} + +int32_t UnpackUint64(const std::vector &src, size_t index, uint64_t &data); + +inline int32_t CombineShortToInt(int16_t upper, int16_t lower) +{ + return (static_cast(upper) << 16) | lower; +} +} // namespace Common +} // namespace UserIAM +} // namespace OHOS + +#endif // IAM_MEM_H \ No newline at end of file diff --git a/common/utils/iam_para2str.h b/common/utils/iam_para2str.h new file mode 100644 index 0000000..28069f2 --- /dev/null +++ b/common/utils/iam_para2str.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 IAM_PARA2STR_H +#define IAM_PARA2STR_H + +#include +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace Common { +using namespace std; +const uint64_t UINT64_MASK = 0xffff; +const size_t MASKED_STRING_LEN = 11; +static inline std::string GetMaskedString(uint64_t val) +{ + char bytes[MASKED_STRING_LEN] = {0}; + if (std::snprintf(bytes, sizeof(bytes), "0xXXXX%04" PRIx64, val & UINT64_MASK) == 0) { + return "(snprintf fail)"; + } + return std::string(bytes); +} + +static inline std::string GetPointerNullStateString(void *p) +{ + if (p == nullptr) { + return "null"; + } + return "non-null"; +} +} // namespace Common +} // namespace UserIAM +} // namespace OHOS + +#endif // IAM_PARA2STR_H \ No newline at end of file diff --git a/common/utils/iam_ptr.h b/common/utils/iam_ptr.h index 5d08957..e92136a 100644 --- a/common/utils/iam_ptr.h +++ b/common/utils/iam_ptr.h @@ -30,7 +30,7 @@ static inline std::shared_ptr SptrToStdSharedPtr(sptr &other) } template -static inline std::shared_ptr MakeShared(Args &&...args) +static inline std::shared_ptr MakeShared(Args &&... args) { try { return std::make_shared(std::forward(args)...); @@ -40,7 +40,7 @@ static inline std::shared_ptr MakeShared(Args &&...args) } template -static inline std::unique_ptr MakeUnique(Args &&...args) +static inline std::unique_ptr MakeUnique(Args &&... args) { try { return std::make_unique(std::forward(args)...);