From b949e355b99546bf50adcfcb4b71c49f58a6485a Mon Sep 17 00:00:00 2001 From: zili Date: Thu, 17 Jun 2021 09:17:05 +0800 Subject: [PATCH 1/2] refactor powermgr and add running lock feature --- BUILD.gn | 21 ++ services/config.gni => config.gni | 2 +- frameworks/BUILD.gn | 63 ++++++ frameworks/include/hilog_wrapper.h | 67 ++++++ .../include/mini/running_lock_interface.h | 35 +++ frameworks/include/power_mgr.h | 30 +++ frameworks/include/running_lock_entry.h | 55 +++++ frameworks/include/running_lock_inner.h | 42 ++++ frameworks/include/running_lock_intf_define.h | 36 +++ frameworks/include/screen_saver_intf_define.h | 33 +++ .../include/small/running_lock_interface.h | 48 ++++ .../include/small/screen_saver_interface.h | 47 ++++ frameworks/src/mini/BUILD.gn | 40 ++++ frameworks/src/mini/power_screen_saver.c | 23 ++ frameworks/src/mini/running_lock_inner.c | 82 +++++++ frameworks/src/running_lock.c | 149 ++++++++++++ frameworks/src/small/BUILD.gn | 38 ++++ frameworks/src/small/power_screen_saver.c | 135 +++++++++++ frameworks/src/small/running_lock_inner.c | 182 +++++++++++++++ interfaces/innerkits/power_screen_saver.h | 30 +++ interfaces/kits/running_lock.h | 68 ++++++ powermgr.gni | 45 ++++ services/BUILD.gn | 77 +++---- services/include/power_manage_feature.h | 99 -------- services/include/running_lock_feature.h | 49 ++++ services/include/running_lock_handler.h | 29 +++ services/include/running_lock_mgr.h | 38 ++++ services/include/screen_saver_feature.h | 47 ++++ services/include/screen_saver_mgr.h | 29 +++ .../screen_saver_handler.h} | 61 ++--- services/source/power_manage_feature.cpp | 213 ------------------ services/src/mini/BUILD.gn | 37 +++ services/src/mini/running_lock_feature_impl.c | 31 +++ services/src/mini/running_lock_handler.c | 32 +++ .../power_manage_service.c} | 78 ++++--- services/src/running_lock_feature.c | 100 ++++++++ services/src/running_lock_mgr.c | 162 +++++++++++++ services/src/screen_saver_feature.c | 94 ++++++++ services/src/small/BUILD.gn | 71 ++++++ .../src/small/running_lock_feature_impl.c | 77 +++++++ services/src/small/running_lock_handler.c | 26 +++ .../src/small/screen_saver_feature_impl.c | 70 ++++++ services/src/small/screen_saver_handler.cpp | 181 +++++++++++++++ services/src/small/screen_saver_mgr.cpp | 36 +++ utils/BUILD.gn | 55 +++++ utils/include/power_mgr_time_util.h | 119 ++++++++++ utils/include/power_mgr_timer_util.h | 63 ++++++ utils/src/power_mgr_time_util.c | 23 ++ utils/src/power_mgr_timer_util.c | 169 ++++++++++++++ 49 files changed, 2923 insertions(+), 414 deletions(-) create mode 100644 BUILD.gn rename services/config.gni => config.gni (99%) mode change 100755 => 100644 create mode 100644 frameworks/BUILD.gn create mode 100644 frameworks/include/hilog_wrapper.h create mode 100644 frameworks/include/mini/running_lock_interface.h create mode 100644 frameworks/include/power_mgr.h create mode 100644 frameworks/include/running_lock_entry.h create mode 100644 frameworks/include/running_lock_inner.h create mode 100644 frameworks/include/running_lock_intf_define.h create mode 100644 frameworks/include/screen_saver_intf_define.h create mode 100644 frameworks/include/small/running_lock_interface.h create mode 100644 frameworks/include/small/screen_saver_interface.h create mode 100644 frameworks/src/mini/BUILD.gn create mode 100644 frameworks/src/mini/power_screen_saver.c create mode 100644 frameworks/src/mini/running_lock_inner.c create mode 100644 frameworks/src/running_lock.c create mode 100644 frameworks/src/small/BUILD.gn create mode 100644 frameworks/src/small/power_screen_saver.c create mode 100644 frameworks/src/small/running_lock_inner.c create mode 100644 interfaces/innerkits/power_screen_saver.h create mode 100644 interfaces/kits/running_lock.h create mode 100644 powermgr.gni mode change 100755 => 100644 services/BUILD.gn delete mode 100755 services/include/power_manage_feature.h create mode 100644 services/include/running_lock_feature.h create mode 100644 services/include/running_lock_handler.h create mode 100644 services/include/running_lock_mgr.h create mode 100644 services/include/screen_saver_feature.h create mode 100644 services/include/screen_saver_mgr.h rename services/include/{power_manage_service.h => small/screen_saver_handler.h} (38%) delete mode 100755 services/source/power_manage_feature.cpp create mode 100644 services/src/mini/BUILD.gn create mode 100644 services/src/mini/running_lock_feature_impl.c create mode 100644 services/src/mini/running_lock_handler.c rename services/{source/power_manage_service.cpp => src/power_manage_service.c} (33%) mode change 100755 => 100644 create mode 100644 services/src/running_lock_feature.c create mode 100644 services/src/running_lock_mgr.c create mode 100644 services/src/screen_saver_feature.c create mode 100644 services/src/small/BUILD.gn create mode 100644 services/src/small/running_lock_feature_impl.c create mode 100644 services/src/small/running_lock_handler.c create mode 100644 services/src/small/screen_saver_feature_impl.c create mode 100644 services/src/small/screen_saver_handler.cpp create mode 100644 services/src/small/screen_saver_mgr.cpp create mode 100644 utils/BUILD.gn create mode 100644 utils/include/power_mgr_time_util.h create mode 100644 utils/include/power_mgr_timer_util.h create mode 100644 utils/src/power_mgr_time_util.c create mode 100644 utils/src/power_mgr_timer_util.c diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000..b0fa50a --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2021 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/lite/config/component/lite_component.gni") + +lite_component("powermgr_lite") { + features = [ + "frameworks:powermgr", + "services:powermgrservice", + ] +} diff --git a/services/config.gni b/config.gni old mode 100755 new mode 100644 similarity index 99% rename from services/config.gni rename to config.gni index 4b2c6e4..cf5ad14 --- a/services/config.gni +++ b/config.gni @@ -10,7 +10,7 @@ # 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. -# +# declare_args() { enable_screensaver = false diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn new file mode 100644 index 0000000..1c8a676 --- /dev/null +++ b/frameworks/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +local_include_dirs = [] +local_deps = [] + +if (is_mini_system) { + local_include_dirs += [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//kernel/liteos_m/kal/posix/include", + ] +} else { + local_deps += [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//third_party/bounds_checking_function:libsec_shared", + ] +} + +config("powermgr_public_config") { + include_dirs = [ + "${powermgr_innerkits_path}", + "${powermgr_kits_path}", + ] +} + +lite_library("powermgr") { + target_type = lite_library_type + + sources = [ "src/running_lock.c" ] + + include_dirs = [ + "include", + "include/${system_type}", + "//utils/native/lite/include", + ] + include_dirs += local_include_dirs + + public_configs = [ ":powermgr_public_config" ] + + deps = [ + "src/${system_type}:runninglock_impl", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + ] + deps += local_deps + + if (enable_screensaver) { + deps += [ "src/${system_type}:screensaver_impl" ] + } +} diff --git a/frameworks/include/hilog_wrapper.h b/frameworks/include/hilog_wrapper.h new file mode 100644 index 0000000..8007eee --- /dev/null +++ b/frameworks/include/hilog_wrapper.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 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 POWERMGR_HILOG_WRAPPER_H +#define POWERMGR_HILOG_WRAPPER_H + +#define LOG_TAG "PowerMgr" + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define __FORMATED__(fmt, ...) "[%s] %s# " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__ + +#ifdef POWER_HILOGD +#undef POWER_HILOGD +#endif + +#ifdef POWER_HILOGI +#undef POWER_HILOGI +#endif + +#ifdef POWER_HILOGW +#undef POWER_HILOGW +#endif + +#ifdef POWER_HILOGE +#undef POWER_HILOGE +#endif + +#ifdef POWER_HILOGF +#undef POWER_HILOGF +#endif + +#ifdef __LITEOS_M__ +#define POWER_HILOGD(...) HILOG_DEBUG(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGI(...) HILOG_INFO(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGW(...) HILOG_WARN(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGE(...) HILOG_ERROR(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGF(...) HILOG_FATAL(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#else // !__LITEOS_M__ +#define POWER_HILOGD(...) HILOG_DEBUG(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGI(...) HILOG_INFO(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGW(...) HILOG_WARN(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGE(...) HILOG_ERROR(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGF(...) HILOG_FATAL(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#endif // __LITEOS_M__ + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_HILOG_WRAPPER_H diff --git a/frameworks/include/mini/running_lock_interface.h b/frameworks/include/mini/running_lock_interface.h new file mode 100644 index 0000000..56db47a --- /dev/null +++ b/frameworks/include/mini/running_lock_interface.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTERFACE_H +#define POWERMGR_RUNNING_LOCK_INTERFACE_H + +#include + +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct { + INHERIT_IUNKNOWN; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTERFACE_H diff --git a/frameworks/include/power_mgr.h b/frameworks/include/power_mgr.h new file mode 100644 index 0000000..046eb4a --- /dev/null +++ b/frameworks/include/power_mgr.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_H +#define POWERMGR_POWER_MGR_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_MANAGE_SERVICE "powermgr" + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_H diff --git a/frameworks/include/running_lock_entry.h b/frameworks/include/running_lock_entry.h new file mode 100644 index 0000000..381a55a --- /dev/null +++ b/frameworks/include/running_lock_entry.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_ENTRY_H +#define POWERMGR_RUNNING_LOCK_ENTRY_H + +#include + +#include "running_lock.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct { + uint32_t pid; + uint64_t token; +} RunningLockIdentity; + +typedef struct { + RunningLock lock; + RunningLockIdentity identity; +} RunningLockEntry; + +static inline RunningLockEntry *GetRunningLockEntry(const RunningLock *lock) +{ + return GET_OBJECT(lock, RunningLockEntry, lock); +} + +static inline BOOL IsValidRunningLockEntry(RunningLockEntry *entry) +{ + return (entry != NULL) && ((entry->lock.type >= 0) && (entry->lock.type < RUNNINGLOCK_BUTT)) ? TRUE : FALSE; +} + +static inline BOOL IsSameRunningLockIdentity(const RunningLockIdentity *a, const RunningLockIdentity *b) +{ + return ((a->pid == b->pid) && (a->token == b->token)) ? TRUE : FALSE; +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_ENTRY_H diff --git a/frameworks/include/running_lock_inner.h b/frameworks/include/running_lock_inner.h new file mode 100644 index 0000000..dca92c5 --- /dev/null +++ b/frameworks/include/running_lock_inner.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INNER_H +#define POWERMGR_RUNNING_LOCK_INNER_H + +#include +#include + +#include "power_mgr.h" +#include "running_lock_entry.h" +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +static inline IUnknown *GetRunningLockIUnknown(void) +{ + return SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE); +} + +void InitIdentity(RunningLockEntry *entry); +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs); +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INNER_H diff --git a/frameworks/include/running_lock_intf_define.h b/frameworks/include/running_lock_intf_define.h new file mode 100644 index 0000000..f24a356 --- /dev/null +++ b/frameworks/include/running_lock_intf_define.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTF_DEFINE_H +#define POWERMGR_RUNNING_LOCK_INTF_DEFINE_H + +#include + +#include "running_lock_entry.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_RUNNING_LOCK_FEATURE "runninglock" + +#define INHERIT_RUNNINGLOCK_INTERFACE \ + int32_t (*AcquireRunningLockEntryFunc)(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); \ + int32_t (*ReleaseRunningLockEntryFunc)(IUnknown *iUnknown, RunningLockEntry *entry) + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTF_DEFINE_H diff --git a/frameworks/include/screen_saver_intf_define.h b/frameworks/include/screen_saver_intf_define.h new file mode 100644 index 0000000..0bb1d9d --- /dev/null +++ b/frameworks/include/screen_saver_intf_define.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_INTF_DEFINE_H +#define POWERMGR_SCREEN_SAVER_INTF_DEFINE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_SCREEN_SAVER_FEATURE "screensaver" + +#define INHERIT_SCREENSAVER_INTERFACE \ + int32_t (*SetScreenSaverStateFunc)(IUnknown *iUnknown, BOOL enable) + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_INTF_DEFINE_H diff --git a/frameworks/include/small/running_lock_interface.h b/frameworks/include/small/running_lock_interface.h new file mode 100644 index 0000000..85640fe --- /dev/null +++ b/frameworks/include/small/running_lock_interface.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTERFACE_H +#define POWERMGR_RUNNING_LOCK_INTERFACE_H + +#include +#include +#include + +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + RUNNINGLOCK_FUNCID_ACQUIRE = 0, + RUNNINGLOCK_FUNCID_RELEASE, + RUNNINGLOCK_FUNCID_BUTT, +} RunningLockFuncId; + +typedef struct { + INHERIT_SERVER_IPROXY; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockInterface; + +typedef struct { + INHERIT_CLIENT_IPROXY; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockProxyInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTERFACE_H diff --git a/frameworks/include/small/screen_saver_interface.h b/frameworks/include/small/screen_saver_interface.h new file mode 100644 index 0000000..4a9878d --- /dev/null +++ b/frameworks/include/small/screen_saver_interface.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_INTERFACE_H +#define POWERMGR_SCREEN_SAVER_INTERFACE_H + +#include +#include +#include + +#include "screen_saver_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + SCREENSAVER_FUNCID_SETSTATE = 0, + SCREENSAVER_FUNCID_BUTT, +} ScreenSaverFuncId; + +typedef struct { + INHERIT_SERVER_IPROXY; + INHERIT_SCREENSAVER_INTERFACE; +} ScreenSaverInterface; + +typedef struct { + INHERIT_CLIENT_IPROXY; + INHERIT_SCREENSAVER_INTERFACE; +} ScreenSaverProxyInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_INTERFACE_H diff --git a/frameworks/src/mini/BUILD.gn b/frameworks/src/mini/BUILD.gn new file mode 100644 index 0000000..d567d40 --- /dev/null +++ b/frameworks/src/mini/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +static_library("runninglock_impl") { + sources = [ "running_lock_inner.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", + "//kernel/liteos_m/kal/posix/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +static_library("screensaver_impl") { + sources = [ "power_screen_saver.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//utils/native/lite/include", + ] +} diff --git a/frameworks/src/mini/power_screen_saver.c b/frameworks/src/mini/power_screen_saver.c new file mode 100644 index 0000000..f1b0e8b --- /dev/null +++ b/frameworks/src/mini/power_screen_saver.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 "hilog_wrapper.h" + +BOOL SetScreenSaverState(BOOL enable) +{ + (void)enable; + POWER_HILOGE("Not support screen saver"); + return FALSE; +} diff --git a/frameworks/src/mini/running_lock_inner.c b/frameworks/src/mini/running_lock_inner.c new file mode 100644 index 0000000..569cdd9 --- /dev/null +++ b/frameworks/src/mini/running_lock_inner.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2021 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 "running_lock_inner.h" + +#include + +#include + +#include "hilog_wrapper.h" +#include "running_lock_interface.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static RunningLockInterface *g_intf = NULL; + +static RunningLockInterface *GetRunningLockInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + IUnknown *iUnknown = GetRunningLockIUnknown(); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get running lock iUnknown"); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query running lock interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + + return g_intf; +} + +void InitIdentity(RunningLockEntry *entry) +{ + if (entry == NULL) { + return; + } + entry->identity.pid = 0; + entry->identity.token = (uint64_t)(uintptr_t)entry; +} + +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + int32_t ret = EC_FAILURE; + RunningLockInterface *intf = GetRunningLockInterface(); + if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) { + ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} + +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry) +{ + int32_t ret = EC_FAILURE; + RunningLockInterface *intf = GetRunningLockInterface(); + if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) { + ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/frameworks/src/running_lock.c b/frameworks/src/running_lock.c new file mode 100644 index 0000000..11d23c2 --- /dev/null +++ b/frameworks/src/running_lock.c @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2021 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 "running_lock.h" + +#include +#include + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_entry.h" +#include "running_lock_inner.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static BOOL g_inited = FALSE; +static Vector g_runningLocks; + +static BOOL AddRunningLock(RunningLock *lock) +{ + pthread_mutex_lock(&g_mutex); + if (g_inited == FALSE) { + g_runningLocks = VECTOR_Make(NULL, NULL); + g_inited = TRUE; + } + int16_t pos = VECTOR_Add(&g_runningLocks, (void *)lock); + if (pos == INVALID_INDEX) { + POWER_HILOGE("Failed to add lock to vector for lock: %s", lock->name); + pthread_mutex_unlock(&g_mutex); + return FALSE; + } + POWER_HILOGD("Add running lock, name: %s, type: %d", lock->name, lock->type); + pthread_mutex_unlock(&g_mutex); + return TRUE; +} + +static BOOL RemoveRunningLock(const RunningLock *lock) +{ + pthread_mutex_lock(&g_mutex); + int16_t pos = VECTOR_Find(&g_runningLocks, (void *)lock); + if (pos < 0) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + pthread_mutex_unlock(&g_mutex); + return FALSE; + } + VECTOR_Swap(&g_runningLocks, pos, NULL); + POWER_HILOGD("Remove running lock, name: %s, type: %d", lock->name, lock->type); + pthread_mutex_unlock(&g_mutex); + return TRUE; +} + +static BOOL IsRunningLockExisted(const RunningLock *lock) +{ + if (lock == NULL) { + POWER_HILOGE("Invalid running lock"); + return FALSE; + } + pthread_mutex_lock(&g_mutex); + BOOL ret = (VECTOR_Find(&g_runningLocks, (void *)lock) >= 0) ? TRUE : FALSE; + pthread_mutex_unlock(&g_mutex); + return ret; +} + +static RunningLockEntry *CreateRunningLockEntry(const char *name, RunningLockType type, RunningLockFlag flag) +{ + RunningLockEntry *entry = (RunningLockEntry *)malloc(sizeof(RunningLockEntry)); + if (entry == NULL) { + POWER_HILOGE("Failed allocate running lock entry"); + return NULL; + } + (void)memset_s(entry, sizeof(RunningLockEntry), 0, sizeof(RunningLockEntry)); + InitIdentity(entry); + entry->lock.type = type; + entry->lock.flag = flag; + if(strcpy_s( entry->lock.name, sizeof(entry->lock.name), name) != EOK) { + free(entry); + return NULL; + } + return entry; +} + +static void DestroyRunningLockEntry(RunningLockEntry *entry) +{ + if (entry != NULL) { + POWER_HILOGD("Free entry: %p", entry); + free(entry); + } +} + +const RunningLock *CreateRunningLock(const char *name, RunningLockType type, RunningLockFlag flag) +{ + if ((name == NULL) || (type >= RUNNINGLOCK_BUTT)) { + POWER_HILOGE("Invalid running lock name"); + return NULL; + } + + RunningLockEntry *entry = CreateRunningLockEntry(name, type, flag); + if (entry == NULL) { + POWER_HILOGE("Failed create running lock entry"); + return NULL; + } + AddRunningLock(&entry->lock); + POWER_HILOGI("Create %s, pid: %u, token: %llu", name, entry->identity.pid, (long long)entry->identity.token); + return &entry->lock; +} + +void DestroyRunningLock(const RunningLock *lock) +{ + if (lock == NULL) { + POWER_HILOGE("Invalid running lock"); + return; + } + if (RemoveRunningLock(lock) == TRUE) { + DestroyRunningLockEntry(GetRunningLockEntry(lock)); + } +} + +BOOL AcquireRunningLock(const RunningLock *lock) +{ + if (IsRunningLockExisted(lock) == FALSE) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + return FALSE; + } + return AcquireRunningLockEntry(GetRunningLockEntry(lock), -1); +} + +BOOL ReleaseRunningLock(const RunningLock *lock) +{ + if (IsRunningLockExisted(lock) == FALSE) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + return FALSE; + } + return ReleaseRunningLockEntry(GetRunningLockEntry(lock)); +} diff --git a/frameworks/src/small/BUILD.gn b/frameworks/src/small/BUILD.gn new file mode 100644 index 0000000..43b3a2f --- /dev/null +++ b/frameworks/src/small/BUILD.gn @@ -0,0 +1,38 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +source_set("runninglock_impl") { + sources = [ "running_lock_inner.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +source_set("screensaver_impl") { + sources = [ "power_screen_saver.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} diff --git a/frameworks/src/small/power_screen_saver.c b/frameworks/src/small/power_screen_saver.c new file mode 100644 index 0000000..eb068eb --- /dev/null +++ b/frameworks/src/small/power_screen_saver.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "screen_saver_interface.h" + +#define MAX_DATA_LEN 1024 + +typedef struct { + INHERIT_IUNKNOWNENTRY(ScreenSaverProxyInterface); +} ScreenSaverProxyEntry; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static ScreenSaverProxyInterface *g_intf = NULL; + +static int32_t SetScreenSaverStateProxy(IUnknown *iUnknown, BOOL enable); + +static void *CreatClient(const char *service, const char *feature, uint32_t size) +{ + (void)service; + (void)feature; + uint32_t len = size + sizeof(ScreenSaverProxyEntry); + uint8_t *client = malloc(len); + if (client == NULL) { + POWER_HILOGE("Failed to allocate memory for screen saver proxy entry"); + return NULL; + } + (void)memset_s(client, len, 0, len); + ScreenSaverProxyEntry *entry = (ScreenSaverProxyEntry *)&client[size]; + entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION); + entry->ref = 1; + entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface; + entry->iUnknown.AddRef = IUNKNOWN_AddRef; + entry->iUnknown.Release = IUNKNOWN_Release; + entry->iUnknown.Invoke = NULL; + entry->iUnknown.SetScreenSaverStateFunc = SetScreenSaverStateProxy; + return client; +} + +static void DestroyClient(const char *service, const char *feature, void *iproxy) +{ + free(iproxy); +} + +static ScreenSaverProxyInterface *GetScreenSaverProxyInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + + SAMGR_RegisterFactory(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE, CreatClient, DestroyClient); + + IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get screen saver iUnknown"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query screen saver interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + POWER_HILOGI("Succeed to get screen saver proxy interface"); + return g_intf; +} + +static int32_t Callback(IOwner owner, int32_t code, IpcIo *reply) +{ + if ((reply == NULL) || (owner == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + + int32_t *ret = (int32_t *)owner; + *ret = IpcIoPopInt32(reply); + POWER_HILOGD("Power screen saver callback: %d, code: %d", *ret, code); + return EC_SUCCESS; +} + +static int32_t SetScreenSaverStateProxy(IUnknown *iUnknown, BOOL enable) +{ + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushBool(&request, enable == TRUE); + + int32_t ret; + ScreenSaverProxyInterface *proxy = (ScreenSaverProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, SCREENSAVER_FUNCID_SETSTATE, &request, &ret, Callback); + POWER_HILOGD("Set screen saver state done, enable: %d", (int32_t)enable); + + return ret; +} + +BOOL SetScreenSaverState(BOOL enable) +{ + int32_t ret = EC_FAILURE; + ScreenSaverProxyInterface *intf = GetScreenSaverProxyInterface(); + if ((intf != NULL) && (intf->SetScreenSaverStateFunc != NULL)) { + ret = intf->SetScreenSaverStateFunc((IUnknown *)intf, enable); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/frameworks/src/small/running_lock_inner.c b/frameworks/src/small/running_lock_inner.c new file mode 100644 index 0000000..3a8d858 --- /dev/null +++ b/frameworks/src/small/running_lock_inner.c @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2021 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 "running_lock_inner.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_interface.h" + +#define MAX_DATA_LEN 1024 + +typedef struct { + INHERIT_IUNKNOWNENTRY(RunningLockProxyInterface); +} RunningLockProxyEntry; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static RunningLockProxyInterface *g_intf = NULL; + +static int32_t AcquireRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); +static int32_t ReleaseRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry); + +static void *CreatClient(const char *service, const char *feature, uint32_t size) +{ + (void)service; + (void)feature; + uint32_t len = size + sizeof(RunningLockProxyEntry); + uint8_t *client = malloc(len); + if (client == NULL) { + POWER_HILOGE("Failed to allocate memory for running lock proxy entry"); + return NULL; + } + (void)memset_s(client, len, 0, len); + RunningLockProxyEntry *entry = (RunningLockProxyEntry *)&client[size]; + entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION); + entry->ref = 1; + entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface; + entry->iUnknown.AddRef = IUNKNOWN_AddRef; + entry->iUnknown.Release = IUNKNOWN_Release; + entry->iUnknown.Invoke = NULL; + entry->iUnknown.AcquireRunningLockEntryFunc = AcquireRunningLockEntryProxy; + entry->iUnknown.ReleaseRunningLockEntryFunc = ReleaseRunningLockEntryProxy; + return client; +} + +static void DestroyClient(const char *service, const char *feature, void *iproxy) +{ + free(iproxy); +} + +static RunningLockProxyInterface *GetRunningLockProxyInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + + SAMGR_RegisterFactory(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE, CreatClient, DestroyClient); + + IUnknown *iUnknown = GetRunningLockIUnknown(); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get running lock iUnknown"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query running lock interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + POWER_HILOGI("Succeed to get running lock proxy interface"); + return g_intf; +} + +static int32_t Callback(IOwner owner, int32_t code, IpcIo *reply) +{ + if ((reply == NULL) || (owner == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + + int32_t *ret = (int32_t *)owner; + *ret = IpcIoPopInt32(reply); + POWER_HILOGD("Running lock callback: %d, code: %d", *ret, code); + return EC_SUCCESS; +} + +static int32_t AcquireRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushFlatObj(&request, entry, sizeof(RunningLockEntry)); + IpcIoPushInt32(&request, timeoutMs); + + int32_t ret; + RunningLockProxyInterface *proxy = (RunningLockProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, RUNNINGLOCK_FUNCID_ACQUIRE, &request, &ret, Callback); + POWER_HILOGD("Acquire running lock done, name: %s, type: %d", entry->lock.name, entry->lock.type); + + return ret; +} + +static int32_t ReleaseRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushFlatObj(&request, entry, sizeof(RunningLockEntry)); + + int32_t ret; + RunningLockProxyInterface *proxy = (RunningLockProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, RUNNINGLOCK_FUNCID_RELEASE, &request, &ret, Callback); + POWER_HILOGD("Release running lock done, name: %s, type: %d", entry->lock.name, entry->lock.type); + + return ret; +} + +void InitIdentity(RunningLockEntry *entry) +{ + if (entry == NULL) { + return; + } + entry->identity.pid = getpid(); + entry->identity.token = (uint64_t)(uintptr_t)entry; +} + +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + int32_t ret = EC_FAILURE; + RunningLockProxyInterface *intf = GetRunningLockProxyInterface(); + if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) { + ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} + +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry) +{ + int32_t ret = EC_FAILURE; + RunningLockProxyInterface *intf = GetRunningLockProxyInterface(); + if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) { + ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/interfaces/innerkits/power_screen_saver.h b/interfaces/innerkits/power_screen_saver.h new file mode 100644 index 0000000..d108186 --- /dev/null +++ b/interfaces/innerkits/power_screen_saver.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_SCREEN_SAVER_H +#define POWERMGR_POWER_SCREEN_SAVER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +BOOL SetScreenSaverState(BOOL enable); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_SCREEN_SAVER_H diff --git a/interfaces/kits/running_lock.h b/interfaces/kits/running_lock.h new file mode 100644 index 0000000..2868dc5 --- /dev/null +++ b/interfaces/kits/running_lock.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_H +#define POWERMGR_RUNNING_LOCK_H + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + /** + * RunningLock type: used to keep screen on. + */ + RUNNINGLOCK_SCREEN, + /** + * RunningLock type: used to keep cpu running. + */ + RUNNINGLOCK_BACKGROUND, + /** + * RunningLock type: used to keep the screen on/off when the proximity sensor is active. + */ + RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, + RUNNINGLOCK_BUTT +} RunningLockType; + +typedef enum { + RUNNINGLOCK_FLAG_NONE = 0, + + /** + * Wakeup device when running lock is acquired. + */ + RUNNINGLOCK_FLAG_WAKEUP_WHEN_ACQUIRED = 1 << 0, +} RunningLockFlag; + +#define RUNNING_LOCK_NAME_LEN 64 + +typedef struct { + char name[RUNNING_LOCK_NAME_LEN]; + RunningLockType type; + RunningLockFlag flag; +} RunningLock; + +const RunningLock *CreateRunningLock(const char *name, RunningLockType type, RunningLockFlag flag); +void DestroyRunningLock(const RunningLock *lock); +BOOL AcquireRunningLock(const RunningLock *lock); +BOOL ReleaseRunningLock(const RunningLock *lock); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_H diff --git a/powermgr.gni b/powermgr.gni new file mode 100644 index 0000000..25a4f01 --- /dev/null +++ b/powermgr.gni @@ -0,0 +1,45 @@ +# Copyright (c) 2021 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/lite/config/component/lite_component.gni") +import("config.gni") + +powermgr_path = "//base/powermgr/powermgr_lite" + +powermgr_frameworks_path = "${powermgr_path}/frameworks" + +powermgr_interfaces_path = "${powermgr_path}/interfaces" + +powermgr_innerkits_path = "${powermgr_interfaces_path}/innerkits" + +powermgr_kits_path = "${powermgr_interfaces_path}/kits" + +powermgr_services_path = "${powermgr_path}/services" + +powermgr_utils_path = "${powermgr_path}/utils" + +declare_args() { + is_mini_system = false + is_small_system = false +} + +if (ohos_kernel_type == "liteos_m") { + is_mini_system = true + lite_library_type = "static_library" + system_type = "mini" +} else { + is_small_system = true + lite_library_type = "shared_library" + system_type = "small" +} diff --git a/services/BUILD.gn b/services/BUILD.gn old mode 100755 new mode 100644 index 43c135d..9935f0a --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -11,58 +11,51 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//base/powermgr/powermgr_lite/powermgr.gni") import("//build/lite/config/component/lite_component.gni") -shared_library("pmslite") { - cflags = [ "-Wall" ] - cflags_cc = cflags + +local_include_dirs = [] +local_deps = [] +local_sources = [] + +if (is_mini_system) { + local_include_dirs += [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//kernel/liteos_m/kal/posix/include", + ] +} else { + local_deps += + [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" ] + + if (enable_screensaver) { + local_sources += [ "src/screen_saver_feature.c" ] + local_deps += [ "src/${system_type}:screensaver_featrue_impl" ] + } +} + +lite_library("powermgrservice") { + target_type = lite_library_type + sources = [ - "source/power_manage_feature.cpp", - "source/power_manage_service.cpp", + "src/power_manage_service.c", + "src/running_lock_feature.c", + "src/running_lock_mgr.c", ] + sources += local_sources include_dirs = [ "include", - "//drivers/peripheral/input/interfaces/include", - "//third_party/bounds_checking_function/include", + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", "//utils/native/lite/include", - "//foundation/distributedschedule/samgr_lite/samgr/adapter/", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/communication/broadcast", - "//foundation/distributedschedule/samgr_lite/samgr_endpoint/source", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", - "//foundation/communication/ipc_lite/interfaces/kits", - "${aafwk_lite_path}/interfaces/kits/want_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite/include", - "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite", - "${aafwk_lite_path}/frameworks/ability_lite/include", - "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite", - "${aafwk_lite_path}/interfaces/kits/want_lite", - "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", - "${appexecfwk_lite_path}/interfaces/innerkits/bundlemgr_lite", - "//foundation/graphic/ui/interfaces/innerkits", - "//foundation/graphic/ui/interfaces/kits", - ] - defines = [ - "ENABLE_WINDOW=1", - "ABILITY_WINDOW_SUPPORT", - "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", ] + include_dirs += local_include_dirs deps = [ - "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite", - "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//foundation/communication/ipc_lite:liteipc_adapter", + "src/${system_type}:runninglock_featrue_impl", "//foundation/distributedschedule/samgr_lite/samgr:samgr", - "//foundation/graphic/wms:lite_wms", - "//third_party/bounds_checking_function:libsec_shared", ] -} - -lite_component("power_manage_service") { - features = [ ":pmslite" ] + deps += local_deps } diff --git a/services/include/power_manage_feature.h b/services/include/power_manage_feature.h deleted file mode 100755 index 48e505f..0000000 --- a/services/include/power_manage_feature.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2021 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 POWER_MANAGE_FEATURE_H -#define POWER_MANAGE_FEATURE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "feature.h" -#include "iunknown.h" -#include "ohos_init.h" -#include "samgr_lite.h" -#include "iproxy_server.h" -#include "iproxy_client.h" -#include "hilog/log.h" -#include "app_info.h" -#include "ability_context.h" -#include "ability_service_interface.h" -#include "input_event_listener_proxy.h" -#include "power_manage_service.h" - -static const char * const PERM_INNER = "power_feature"; - -namespace OHOS::PowerMgr { -typedef int32 (*InvokeFunc)(const void *origin, IpcIo *req); - -enum PowerApi { - LOCK_INVOKE, - UNLOCK_INVOKE, - IS_ALWAYS_ON_INVOKE, - FUNC_SIZE -}; - -class PowerManageFeature : public Feature, public InputEventListenerProxy::RawEventListener { -public: - static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply); - static void AcqurieLock(); - static void ReleaseLock(); - static int32 IsAlwaysOn(); - - ~PowerManageFeature() = default; - static const char *GetFeatureName(Feature *feature); - static void OnFeatureInitialize(Feature *feature, Service *parent, Identity identity); - static void OnFeatureStop(Feature *feature, Identity identity); - static BOOL OnFeatureMessage(Feature *feature, Request *request); - - class UserActivity : public InputEventListenerProxy::RawEventListener { - public: - void OnRawEvent(const RawEvent& event)override; - static UserActivity* GetInstance() - { - static UserActivity activity; - return &activity; - } - }; - -private: - static int32 LockInvoke(const void *origin, IpcIo *req); - static int32 UnLockInvoke(const void *origin, IpcIo *req); - static int32 IsAlwaysOnInvoke(const void *origin, IpcIo *req); - static InvokeFunc invokeFuncList[FUNC_SIZE]; - PowerManageFeature(); -}; - -struct PmsInterface { - INHERIT_SERVER_IPROXY; - void (*Lock)(); - void (*UnLock)(); - int32 (*IsStayOn)(); -}; - -typedef struct { - INHERIT_FEATURE; - INHERIT_IUNKNOWNENTRY(PmsInterface); - Identity identity_; -} PowerMgrFeatureImpl; -} -#endif diff --git a/services/include/running_lock_feature.h b/services/include/running_lock_feature.h new file mode 100644 index 0000000..c2b3a25 --- /dev/null +++ b/services/include/running_lock_feature.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_FEATURE_H +#define POWERMGR_RUNNING_LOCK_FEATURE_H + +#include + +#include "running_lock_interface.h" +#include "running_lock_mgr.h" + +typedef struct { + INHERIT_FEATURE; + INHERIT_IUNKNOWNENTRY(RunningLockInterface); + Identity identity; +} RunningLockFeature; + +const char *GetRunningLockFeatureName(Feature *feature); +void OnRunningLockFeatureInitialize(Feature *feature, Service *parent, Identity identity); +void OnRunningLockFeatureStop(Feature *feature, Identity identity); +BOOL OnRunningLockFeatureMessage(Feature *feature, Request *request); +int32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); +int32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry); + +#define RUNNING_LOCK_FEATURE_INTERFACE_IMPL \ + .GetName = GetRunningLockFeatureName, \ + .OnInitialize = OnRunningLockFeatureInitialize, \ + .OnStop = OnRunningLockFeatureStop, \ + .OnMessage = OnRunningLockFeatureMessage + +#define RUNNING_LOCK_INTERFACE_IMPL \ + .AcquireRunningLockEntryFunc = OnAcquireRunningLockEntry, \ + .ReleaseRunningLockEntryFunc = OnReleaseRunningLockEntry + +RunningLockFeature *GetRunningLockFeatureImpl(void); + +#endif // POWERMGR_RUNNING_LOCK_FEATURE_H diff --git a/services/include/running_lock_handler.h b/services/include/running_lock_handler.h new file mode 100644 index 0000000..714b0b9 --- /dev/null +++ b/services/include/running_lock_handler.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_HANDLER_H +#define POWERMGR_RUNNING_LOCK_HANDLER_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void RunningLockHandlerLock(const char *name); +void RunningLockHandlerUnlock(const char *name); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_HANDLER_H diff --git a/services/include/running_lock_mgr.h b/services/include/running_lock_mgr.h new file mode 100644 index 0000000..c26d567 --- /dev/null +++ b/services/include/running_lock_mgr.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_MGR_H +#define POWERMGR_RUNNING_LOCK_MGR_H + +#include "running_lock_entry.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void RunningLockMgrInit(void); +int32_t RunningLockMgrAcquireEntry(RunningLockEntry *entry, int32_t timeoutMs); +int32_t RunningLockMgrReleaseEntry(RunningLockEntry *entry); +int32_t RunningLockMgrGetLockCount(RunningLockType type); + +static inline BOOL RunningLockMgrIsLockHolding(RunningLockType type) +{ + return (RunningLockMgrGetLockCount(type) != 0) ? TRUE : FALSE; +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_MGR_H diff --git a/services/include/screen_saver_feature.h b/services/include/screen_saver_feature.h new file mode 100644 index 0000000..d3fe2c1 --- /dev/null +++ b/services/include/screen_saver_feature.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_FEATURE_H +#define POWERMGR_SCREEN_SAVER_FEATURE_H + +#include + +#include "screen_saver_interface.h" +#include "screen_saver_mgr.h" + +typedef struct { + INHERIT_FEATURE; + INHERIT_IUNKNOWNENTRY(ScreenSaverInterface); + Identity identity; +} ScreenSaverFeature; + +const char *GetScreenSaverFeatureName(Feature *feature); +void OnScreenSaverFeatureInitialize(Feature *feature, Service *parent, Identity identity); +void OnScreenSaverFeatureStop(Feature *feature, Identity identity); +BOOL OnScreenSaverFeatureMessage(Feature *feature, Request *request); +int32_t OnSetScreenSaverState(IUnknown *iUnknown, BOOL enable); + +#define SCREEN_SAVER_FEATURE_INTERFACE_IMPL \ + .GetName = GetScreenSaverFeatureName, \ + .OnInitialize = OnScreenSaverFeatureInitialize, \ + .OnStop = OnScreenSaverFeatureStop, \ + .OnMessage = OnScreenSaverFeatureMessage + +#define SCREEN_SAVER_INTERFACE_IMPL \ + .SetScreenSaverStateFunc = OnSetScreenSaverState + +ScreenSaverFeature *GetScreenSaverFeatureImpl(void); + +#endif // POWERMGR_SCREEN_SAVER_FEATURE_H diff --git a/services/include/screen_saver_mgr.h b/services/include/screen_saver_mgr.h new file mode 100644 index 0000000..51d503f --- /dev/null +++ b/services/include/screen_saver_mgr.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_MGR_H +#define POWERMGR_SCREEN_SAVER_MGR_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void ScreenSaverMgrInit(void); +int32_t ScreenSaverMgrSetState(BOOL enable); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_MGR_H diff --git a/services/include/power_manage_service.h b/services/include/small/screen_saver_handler.h similarity index 38% rename from services/include/power_manage_service.h rename to services/include/small/screen_saver_handler.h index 529f391..ccb8a9a 100755 --- a/services/include/power_manage_service.h +++ b/services/include/small/screen_saver_handler.h @@ -13,38 +13,45 @@ * limitations under the License. */ -#ifndef POWER_MANAGE_SERVICE_H -#define POWER_MANAGE_SERVICE_H +#ifndef POWERMGR_SCREEN_SAVER_HANDLER_H +#define POWERMGR_SCREEN_SAVER_HANDLER_H -#include "service.h" -#include "ohos_init.h" -#include "samgr_lite.h" -#include "hilog/log.h" +#include -static const char * const POWER_MANAGE_SERVICE = "power_service"; +#include +#include -namespace OHOS::PowerMgr { -class PowerMgrService : public Service { +#include "power_mgr_time_util.h" +#include "power_mgr_timer_util.h" + +namespace OHOS { +class ScreenSaverHandler : public InputEventListenerProxy::RawEventListener { public: - static PowerMgrService *GetInstance() - { - static PowerMgrService instance; - return &instance; - } - PowerMgrService(const PowerMgrService&) = delete; - PowerMgrService& operator=(const PowerMgrService&) = delete; - PowerMgrService(PowerMgrService &&) = delete; - PowerMgrService& operator=(PowerMgrService &&) = delete; - ~PowerMgrService() = default; -private: - PowerMgrService(); - static const char *GetServiceName(Service *service); - static BOOL ServiceInitialize(Service *service, Identity identity); - static TaskConfig GetServiceTaskConfig(Service *service); - static BOOL ServiceMessageHandle(Service *service, Request *request); + ScreenSaverHandler() = default; + ~ScreenSaverHandler() override; + + void Init(); + void SetInterval(int64_t intervalMsec); + int32_t SetState(bool enable); + void OnRawEvent(const RawEvent &event) override; private: - Identity identity_; + friend void PowerTimerCallback(void *data); + + static AmsInterface *GetAmsInterface(); + + bool SetEnableLocked(); + bool SetDisableLocked(); + void StartScreenSaver(); + bool StartScreenSaverLocked(); + + static constexpr int64_t DEFAULT_INTERVAL_MSECS = 5 * MSEC_PER_MIN; + + std::mutex mutex_; + bool enabled_{false}; + bool screenSaverStarted_{false}; + int64_t intervalMsec_{DEFAULT_INTERVAL_MSECS}; + PowerTimer *timer_{nullptr}; }; } // namespace OHOS -#endif +#endif // POWERMGR_SCREEN_SAVER_HANDLER_H diff --git a/services/source/power_manage_feature.cpp b/services/source/power_manage_feature.cpp deleted file mode 100755 index 0d6c3e1..0000000 --- a/services/source/power_manage_feature.cpp +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2021 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 "power_manage_feature.h" - -using namespace OHOS; -using namespace OHOS::PowerMgr; -using namespace OHOS::HiviewDFX; - -namespace { -static const int SLEEP_OUT_TIME_SEC = 300; -static const int REGISTER_WAIT_TIME_SEC = 20; -static const int POWER_ERROR = -1; -static const int PM_INVOKE_SUCCESS = 1; -} - -static int32 g_lockNum = 0; -static bool g_isSleep = false; -static int g_count = 0; -static int g_targetTime = SLEEP_OUT_TIME_SEC; -static pthread_mutex_t g_numMutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_t g_powerThread = -1; - -static void *SleepListen(void *args); -static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "PowerMgrService"}; - -PowerManageFeature::PowerManageFeature() : Feature() {} - -void PowerManageFeature::AcqurieLock() -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "AcqurieLock."); - g_lockNum++; - pthread_mutex_unlock(&g_numMutex); -} - -void PowerManageFeature::ReleaseLock() -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "ReleaseLock."); - g_lockNum--; - g_count = 0; - pthread_mutex_unlock(&g_numMutex); -} - -int32 PowerManageFeature::IsAlwaysOn() -{ - HiLog::Debug(LABEL, "IsAlwaysOn."); - return g_lockNum; -} - -void PowerManageFeature::UserActivity::OnRawEvent(const RawEvent& event) -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "User touch the screen, refresh time now."); - g_count = 0; - if (g_isSleep) { - g_isSleep = false; - } - pthread_mutex_unlock(&g_numMutex); -} - -PowerMgrFeatureImpl g_pmsImpl = { - GetName : PowerManageFeature::GetFeatureName, - OnInitialize : PowerManageFeature::OnFeatureInitialize, - OnStop : PowerManageFeature::OnFeatureStop, - OnMessage : PowerManageFeature::OnFeatureMessage, - SERVER_IPROXY_IMPL_BEGIN, - Invoke : PowerManageFeature::Invoke, - Lock : PowerManageFeature::AcqurieLock, - UnLock : PowerManageFeature::ReleaseLock, - IsStayOn : PowerManageFeature::IsAlwaysOn, - IPROXY_END, - identity_ : { -1, -1, NULL }, -}; - -InvokeFunc PowerManageFeature::invokeFuncList[FUNC_SIZE] { - PowerManageFeature::LockInvoke, - PowerManageFeature::UnLockInvoke, - PowerManageFeature::IsAlwaysOnInvoke, -}; - -int32 PowerManageFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply) -{ - HiLog::Debug(LABEL, "IPC Invoke called."); - if (req == nullptr) { - return POWER_ERROR; - } - if (funcId >= 0 && funcId < FUNC_SIZE) { - if (funcId == IS_ALWAYS_ON_INVOKE) { - IpcIoPushInt32(reply, g_lockNum); - } - invokeFuncList[funcId](origin, req); - } - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::LockInvoke(const void *origin, IpcIo *req) -{ - PowerManageFeature::AcqurieLock(); - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::UnLockInvoke(const void *origin, IpcIo *req) -{ - PowerManageFeature::ReleaseLock(); - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::IsAlwaysOnInvoke(const void *origin, IpcIo *req) -{ - return PowerManageFeature::IsAlwaysOn(); -} - -const char *PowerManageFeature::GetFeatureName(Feature *feature) -{ - return PERM_INNER; -} - -void PowerManageFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity) -{ - g_pmsImpl.identity_ = identity; - HiLog::Debug(LABEL, "OnInitialize called."); - pthread_create(&g_powerThread, nullptr, SleepListen, nullptr); - g_lockNum = 0; - g_isSleep = false; - g_count = 0; - g_targetTime = SLEEP_OUT_TIME_SEC; -} - -static bool GetAmsInterface(struct AmsInterface **amsInterface) -{ - IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE); - if (iUnknown == NULL) { - HiLog::Debug(LABEL, "GetFeatureApi failed."); - return false; - } - - int32_t errCode = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)amsInterface); - if (errCode != EC_SUCCESS) { - HiLog::Debug(LABEL, "QueryInterface failed."); - return false; - } - return true; -} - -static void *SleepListen(void *args) -{ - sleep(REGISTER_WAIT_TIME_SEC); - HiLog::Debug(LABEL, "Register ims touch event now."); - InputEventListenerProxy::GetInstance()->RegisterInputEventListener(PowerManageFeature::UserActivity::GetInstance()); - while (true) { - pthread_mutex_lock(&g_numMutex); - if (g_lockNum == 0 && g_count == g_targetTime) { - HiLog::Debug(LABEL, "Five minutes passed, It is time to call screen saver."); - Want want = { nullptr }; - ElementName element = { nullptr }; - SetElementBundleName(&element, "com.huawei.screensaver"); - SetElementAbilityName(&element, "ScreensaverAbility"); - SetWantElement(&want, element); - SetWantData(&want, "WantData", strlen("WantData") + 1); - struct AmsInterface *amsInterface = nullptr; - if (!GetAmsInterface(&amsInterface)) { - ClearWant(&want); - } - amsInterface->StartAbility(&want); - ClearElement(&element); - ClearWant(&want); - } - pthread_mutex_unlock(&g_numMutex); - g_count++; - sleep(1); - } -} - -void PowerManageFeature::OnFeatureStop(Feature *feature, Identity identity) -{ - (void)feature; - (void)identity; -} - -BOOL PowerManageFeature::OnFeatureMessage(Feature *feature, Request *request) -{ - return ((feature != nullptr) && (request != nullptr)); -} - -static void GInit() -{ - HiLog::Debug(LABEL, "SYS_FEATURE_INIT called."); - BOOL result = SAMGR_GetInstance()->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)&g_pmsImpl); - if (result == FALSE) { - HiLog::Debug(LABEL, "RegisterFeature failed."); - return; - } - BOOL apiResult = SAMGR_GetInstance()->RegisterFeatureApi(POWER_MANAGE_SERVICE, PERM_INNER, GET_IUNKNOWN(g_pmsImpl)); - if (apiResult == FALSE) { - HiLog::Debug(LABEL, "RegisterFeatureApi failed."); - return; - } -} -SYSEX_FEATURE_INIT(GInit); diff --git a/services/src/mini/BUILD.gn b/services/src/mini/BUILD.gn new file mode 100644 index 0000000..3d0037d --- /dev/null +++ b/services/src/mini/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +static_library("runninglock_featrue_impl") { + sources = [ + "running_lock_feature_impl.c", + "running_lock_handler.c", + ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_kits_path}", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//kernel/liteos_m/kernel/include", + "//kernel/liteos_m/utils", + ] + + deps = [ + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//kernel/liteos_m/kernel:kernel", + ] +} diff --git a/services/src/mini/running_lock_feature_impl.c b/services/src/mini/running_lock_feature_impl.c new file mode 100644 index 0000000..ba95cf3 --- /dev/null +++ b/services/src/mini/running_lock_feature_impl.c @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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 + +#include "running_lock_feature.h" + +static RunningLockFeature g_feature = { + RUNNING_LOCK_FEATURE_INTERFACE_IMPL, + DEFAULT_IUNKNOWN_ENTRY_BEGIN, + RUNNING_LOCK_INTERFACE_IMPL, + DEFAULT_IUNKNOWN_ENTRY_END, + .identity = { -1, -1, NULL }, +}; + +RunningLockFeature *GetRunningLockFeatureImpl(void) +{ + return &g_feature; +} diff --git a/services/src/mini/running_lock_handler.c b/services/src/mini/running_lock_handler.c new file mode 100644 index 0000000..60d81d2 --- /dev/null +++ b/services/src/mini/running_lock_handler.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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 + +#include + +#include "hilog_wrapper.h" + +void RunningLockHandlerLock(const char *name) +{ + uint32_t ret = LOS_PmLockRequest(name); + POWER_HILOGD("Lock request, name: %s, ret: %u", name, ret); +} + +void RunningLockHandlerUnlock(const char *name) +{ + uint32_t ret = LOS_PmLockRelease(name); + POWER_HILOGD("Lock release, name: %s, ret: %u", name, ret); +} diff --git a/services/source/power_manage_service.cpp b/services/src/power_manage_service.c old mode 100755 new mode 100644 similarity index 33% rename from services/source/power_manage_service.cpp rename to services/src/power_manage_service.c index 142ca0e..56c26c2 --- a/services/source/power_manage_service.cpp +++ b/services/src/power_manage_service.c @@ -13,56 +13,70 @@ * limitations under the License. */ -#include "power_manage_service.h" +#include +#include +#include -using namespace OHOS; -using namespace OHOS::PowerMgr; +#include +#include -namespace { -static const int STACK_SIZE = 0x800; -static const int QUEUE_SIZE = 20; -} +#include "hilog_wrapper.h" +#include "power_mgr.h" -PowerMgrService::PowerMgrService() : Service(), identity_() -{ - this->Service::GetName = PowerMgrService::GetServiceName; - this->Service::Initialize = PowerMgrService::ServiceInitialize; - this->Service::MessageHandle = PowerMgrService::ServiceMessageHandle; - this->Service::GetTaskConfig = PowerMgrService::GetServiceTaskConfig; -} +#define STACK_SIZE 0x800 +#define QUEUE_SIZE 20 -static void GInit() -{ - SamgrLite *sm = SAMGR_GetInstance(); - if (sm == nullptr) { - return; - } - sm->RegisterService(PowerMgrService::GetInstance()); -} -SYSEX_SERVICE_INIT(GInit); +typedef struct { + INHERIT_SERVICE; + Identity identity; +} PowerMgrService; -const char *PowerMgrService::GetServiceName(__attribute__((unused))Service *service) +static const char *GetName(Service *service); +static BOOL Initialize(Service *service, Identity identity); +static BOOL MessageHandle(Service *service, Request *request); +static TaskConfig GetTaskConfig(Service *service); + +static PowerMgrService g_service = { + .GetName = GetName, + .Initialize = Initialize, + .MessageHandle = MessageHandle, + .GetTaskConfig = GetTaskConfig, + .identity = { -1, -1, NULL } +}; + +static const char *GetName(Service *service) { + (void)service; return POWER_MANAGE_SERVICE; } -BOOL PowerMgrService::ServiceInitialize(Service *service, Identity identity) +static BOOL Initialize(Service *service, Identity identity) { - if (service == nullptr) { + if (service == NULL) { + POWER_HILOGE("Invalid service"); return FALSE; } - PowerMgrService *powerService = static_cast(service); - powerService->identity_ = identity; + PowerMgrService *pms = (PowerMgrService *)(service); + pms->identity = identity; return TRUE; } -BOOL PowerMgrService::ServiceMessageHandle(Service *service, Request *request) +static BOOL MessageHandle(Service *service, Request *request) { - return (request != nullptr); + (void)service; + return request != NULL; } -TaskConfig PowerMgrService::GetServiceTaskConfig(Service *service) +static TaskConfig GetTaskConfig(Service *service) { - TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK}; + (void)service; + TaskConfig config = { LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK }; return config; } + +static void Init(void) +{ + SAMGR_GetInstance()->RegisterService((Service *)&g_service); + POWER_HILOGI("Succeed to init power manager service"); +} +SYS_SERVICE_INIT(Init); diff --git a/services/src/running_lock_feature.c b/services/src/running_lock_feature.c new file mode 100644 index 0000000..63dd7ba --- /dev/null +++ b/services/src/running_lock_feature.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 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 "running_lock_feature.h" + +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "running_lock_inner.h" +#include "running_lock_mgr.h" + +const char *GetRunningLockFeatureName(Feature *feature) +{ + (void)feature; + return POWER_RUNNING_LOCK_FEATURE; +} + +void OnRunningLockFeatureInitialize(Feature *feature, Service *parent, Identity identity) +{ + if (feature == NULL) { + POWER_HILOGE("Invalid feature"); + return; + } + + RunningLockFeature *f = (RunningLockFeature *)feature; + f->identity = identity; + POWER_HILOGI("Init running lock feature done"); +} + +void OnRunningLockFeatureStop(Feature *feature, Identity identity) +{ + (void)feature; + (void)identity; + RunningLockFeature *f = GetRunningLockFeatureImpl(); + if (f != NULL) { + f->identity.queueId = NULL; + f->identity.featureId = -1; + f->identity.serviceId = -1; + } +} + +BOOL OnRunningLockFeatureMessage(Feature *feature, Request *request) +{ + return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE; +} + +int32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs) +{ + (void)iUnknown; + return RunningLockMgrAcquireEntry(entry, timeoutMs); +} + +int32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry) +{ + (void)iUnknown; + return RunningLockMgrReleaseEntry(entry); +} + +static void Init(void) +{ + SamgrLite *sam = SAMGR_GetInstance(); + if (sam == NULL) { + POWER_HILOGE("Failed to get samgr"); + return; + } + + RunningLockFeature *feature = GetRunningLockFeatureImpl(); + if (feature == NULL) { + POWER_HILOGE("Failed to get running lock feature"); + return; + } + BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature); + if (result == FALSE) { + POWER_HILOGE("Failed to register running lock feature"); + return; + } + result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE, GET_IUNKNOWN(*feature)); + if (result == FALSE) { + POWER_HILOGE("Failed to register running lock feature api"); + return; + } + RunningLockMgrInit(); + POWER_HILOGI("Succeed to register running lock feature"); +} +SYS_FEATURE_INIT(Init); diff --git a/services/src/running_lock_mgr.c b/services/src/running_lock_mgr.c new file mode 100644 index 0000000..7ea99e5 --- /dev/null +++ b/services/src/running_lock_mgr.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2021 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 + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_entry.h" +#include "running_lock_handler.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static Vector g_runningLocks[RUNNINGLOCK_BUTT]; +static const char * const g_runningLockNames[RUNNINGLOCK_BUTT] = { + "OHOSPowerMgr.Screen", + "OHOSPowerMgr.Background", + "OHOSPowerMgr.Proximity", +}; + +static RunningLockEntry *DupRunningLockEntry(RunningLockEntry *entry) +{ + RunningLockEntry *e = (RunningLockEntry *)malloc(sizeof(RunningLockEntry)); + if (e == NULL) { + POWER_HILOGE("Failed allocate running lock entry"); + return NULL; + } + if (memcpy_s(e, sizeof(RunningLockEntry), entry, sizeof(RunningLockEntry)) != EOK) { + POWER_HILOGE("Failed copy running lock entry"); + free(e); + return NULL; + } + return e; +} + +static BOOL AddRunningLockEntryLocked(Vector *vec, RunningLockEntry *entry) +{ + int16_t pos = VECTOR_FindByKey(vec, (void *)&entry->identity); + if (pos >= 0) { + POWER_HILOGI("Already acquired: %s", entry->lock.name); + return TRUE; + } + RunningLockEntry *e = DupRunningLockEntry(entry); + if (e == NULL) { + POWER_HILOGE("Failed duplicate running lock entry"); + return FALSE; + } + pos = VECTOR_Add(vec, (void *)e); + if (pos == INVALID_INDEX) { + POWER_HILOGE("Failed to add entry to vector for lock: %s", e->lock.name); + return FALSE; + } + POWER_HILOGD("Add running lock entry, name: %s, type: %d", e->lock.name, e->lock.type); + if (VECTOR_Num(vec) == 1) { + RunningLockHandlerLock(g_runningLockNames[entry->lock.type]); + } + return TRUE; +} + +static BOOL RemoveRunningLockEntryLocked(Vector *vec, RunningLockEntry *entry) +{ + int16_t pos = VECTOR_FindByKey(vec, (void *)&entry->identity); + if (pos < 0) { + POWER_HILOGE("Non-existent running lock: %s", entry->lock.name); + return TRUE; + } + VECTOR_Swap(vec, pos, NULL); + RunningLockEntry *e = (RunningLockEntry *)VECTOR_At(vec, pos); + free(e); + POWER_HILOGD("Remove running lock entry, name: %s, type: %d", entry->lock.name, entry->lock.type); + if (VECTOR_Num(vec) == 0) { + RunningLockHandlerUnlock(g_runningLockNames[entry->lock.type]); + } + return TRUE; +} + +static void ShowLocks() +{ +#ifdef OHOS_DEBUG + for (int t = RUNNINGLOCK_SCREEN; t < RUNNINGLOCK_BUTT; t++) { + Vector *vec = &g_runningLocks[t]; + int size = VECTOR_Size(vec); + POWER_HILOGD("type: %d, lock num: %d", t, VECTOR_Num(vec)); + for (int i = 0; i < size; i++) { + RunningLockEntry* e = VECTOR_At(vec, i); + if (e == NULL) continue; + POWER_HILOGD("No.%d, name: %s, pid: %u, token: %llu", i, e->lock.name, e->identity.pid, (long long)e->identity.token); + } + } +#endif +} + +int32_t RunningLockMgrAcquireEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + pthread_mutex_lock(&g_mutex); + Vector *vec = &g_runningLocks[entry->lock.type]; + BOOL ret = AddRunningLockEntryLocked(vec, entry); + ShowLocks(); + pthread_mutex_unlock(&g_mutex); + return (ret == TRUE) ? EC_SUCCESS : EC_FAILURE; +} + +int32_t RunningLockMgrReleaseEntry(RunningLockEntry *entry) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + pthread_mutex_lock(&g_mutex); + Vector *vec = &g_runningLocks[entry->lock.type]; + BOOL ret = RemoveRunningLockEntryLocked(vec, entry); + ShowLocks(); + pthread_mutex_unlock(&g_mutex); + return (ret == TRUE) ? EC_SUCCESS : EC_FAILURE; +} + +int32_t RunningLockMgrGetLockCount(RunningLockType type) +{ + int32_t cnt = -1; + if ((type >= 0) && (type < RUNNINGLOCK_BUTT)) { + pthread_mutex_lock(&g_mutex); + cnt = VECTOR_Num(&g_runningLocks[type]); + pthread_mutex_unlock(&g_mutex); + } + return cnt; +} + +static RunningLockIdentity *GetRunningLockIdentity(const RunningLockEntry *entry) +{ + return &entry->identity; +} + +static int32_t RunningLockIdentityCmp(const RunningLockIdentity *a, const RunningLockIdentity *b) +{ + return (IsSameRunningLockIdentity(a, b) == TRUE) ? 0 : -1; +} + +void RunningLockMgrInit(void) +{ + for (int32_t i = 0; i < RUNNINGLOCK_BUTT; i++) { + g_runningLocks[i] = VECTOR_Make((VECTOR_Key)GetRunningLockIdentity, (VECTOR_Compare)RunningLockIdentityCmp); + } +} diff --git a/services/src/screen_saver_feature.c b/services/src/screen_saver_feature.c new file mode 100644 index 0000000..7bcf58e --- /dev/null +++ b/services/src/screen_saver_feature.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021 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 "screen_saver_feature.h" + +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "screen_saver_intf_define.h" +#include "screen_saver_mgr.h" + +const char *GetScreenSaverFeatureName(Feature *feature) +{ + (void)feature; + return POWER_SCREEN_SAVER_FEATURE; +} + +void OnScreenSaverFeatureInitialize(Feature *feature, Service *parent, Identity identity) +{ + if (feature == NULL) { + POWER_HILOGE("Invalid feature"); + return; + } + + ScreenSaverFeature *f = (ScreenSaverFeature *)feature; + f->identity = identity; + ScreenSaverMgrInit(); + POWER_HILOGI("Init screen saver feature done"); +} + +void OnScreenSaverFeatureStop(Feature *feature, Identity identity) +{ + (void)feature; + (void)identity; + ScreenSaverFeature *f = GetScreenSaverFeatureImpl(); + if (f != NULL) { + f->identity.queueId = NULL; + f->identity.featureId = -1; + f->identity.serviceId = -1; + } +} + +BOOL OnScreenSaverFeatureMessage(Feature *feature, Request *request) +{ + return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE; +} + +int32_t OnSetScreenSaverState(IUnknown *iUnknown, BOOL enable) +{ + (void)iUnknown; + return ScreenSaverMgrSetState(enable); +} + +static void Init(void) +{ + SamgrLite *sam = SAMGR_GetInstance(); + if (sam == NULL) { + POWER_HILOGE("Failed to get samgr"); + return; + } + + ScreenSaverFeature *feature = GetScreenSaverFeatureImpl(); + if (feature == NULL) { + POWER_HILOGE("Failed to get screen saver feature"); + return; + } + BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature); + if (result == FALSE) { + POWER_HILOGE("Failed to register screen saver feature"); + return; + } + result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE, GET_IUNKNOWN(*feature)); + if (result == FALSE) { + POWER_HILOGE("Failed to register screen saver feature api"); + return; + } + POWER_HILOGI("Succeed to register screen saver feature"); +} +SYS_FEATURE_INIT(Init); diff --git a/services/src/small/BUILD.gn b/services/src/small/BUILD.gn new file mode 100644 index 0000000..e4647ff --- /dev/null +++ b/services/src/small/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +source_set("runninglock_featrue_impl") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + sources = [ + "running_lock_feature_impl.c", + "running_lock_handler.c", + ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_kits_path}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +source_set("screensaver_featrue_impl") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] + + sources = [ + "screen_saver_feature_impl.c", + "screen_saver_handler.cpp", + "screen_saver_mgr.cpp", + ] + + include_dirs = [ + "${powermgr_kits_path}", + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_services_path}/include/${system_type}", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/abilitymgr_lite:abilitymanager", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "${powermgr_utils_path}:powermgr_utils", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//foundation/graphic/wms:lite_wms", + ] +} diff --git a/services/src/small/running_lock_feature_impl.c b/services/src/small/running_lock_feature_impl.c new file mode 100644 index 0000000..5d5bcc2 --- /dev/null +++ b/services/src/small/running_lock_feature_impl.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_feature.h" + +typedef int32_t (*InvokeFunc)(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply); + +static RunningLockFeature g_feature = { + RUNNING_LOCK_FEATURE_INTERFACE_IMPL, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = FeatureInvoke, + RUNNING_LOCK_INTERFACE_IMPL, + IPROXY_END, + .identity = { -1, -1, NULL }, +}; + +static InvokeFunc g_invokeFuncs[RUNNINGLOCK_FUNCID_BUTT] = { + AcquireInvoke, + ReleaseInvoke, +}; + +RunningLockFeature *GetRunningLockFeatureImpl(void) +{ + return &g_feature; +} + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + if ((iProxy == NULL) || (origin == NULL) || (req == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + POWER_HILOGD("Running lock feature invoke function id: %d", funcId); + return (funcId >= 0 && funcId < RUNNINGLOCK_FUNCID_BUTT) ? g_invokeFuncs[funcId](iProxy, origin, req, reply) : + EC_FAILURE; +} + +static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + size_t len = 0; + void *data = (void *)IpcIoPopFlatObj(req, &len); + int32_t timeoutMs = IpcIoPopInt32(req); + int32_t ret = OnAcquireRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data, timeoutMs); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} + +static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + size_t len = 0; + void *data = (void *)IpcIoPopFlatObj(req, &len); + int32_t ret = OnReleaseRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} diff --git a/services/src/small/running_lock_handler.c b/services/src/small/running_lock_handler.c new file mode 100644 index 0000000..ce2be50 --- /dev/null +++ b/services/src/small/running_lock_handler.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 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 "hilog_wrapper.h" + +void RunningLockHandlerLock(const char *name) +{ + POWER_HILOGD("To be locked in kernel, name: %s", name); +} + +void RunningLockHandlerUnlock(const char *name) +{ + POWER_HILOGD("To be unlocked in kernel, name: %s", name); +} diff --git a/services/src/small/screen_saver_feature_impl.c b/services/src/small/screen_saver_feature_impl.c new file mode 100644 index 0000000..8b5b856 --- /dev/null +++ b/services/src/small/screen_saver_feature_impl.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include + +#include "hilog_wrapper.h" +#include "screen_saver_feature.h" + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply); + +static ScreenSaverFeature g_feature = { + SCREEN_SAVER_FEATURE_INTERFACE_IMPL, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = FeatureInvoke, + SCREEN_SAVER_INTERFACE_IMPL, + IPROXY_END, + .identity = { -1, -1, NULL }, +}; + +ScreenSaverFeature *GetScreenSaverFeatureImpl(void) +{ + return &g_feature; +} + +static int32_t SetStateInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + if (reply == NULL) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + bool enable = IpcIoPopBool(req); + int32_t ret = OnSetScreenSaverState((IUnknown *)iProxy, enable ? TRUE : FALSE); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + if ((iProxy == NULL) || (origin == NULL) || (req == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + POWER_HILOGD("Screen saver feature invoke function id: %d", funcId); + int32_t ret = EC_SUCCESS; + switch (funcId) { + case SCREENSAVER_FUNCID_SETSTATE: + ret = SetStateInvoke(iProxy, origin, req, reply); + break; + default: + POWER_HILOGE("Invalid function id: %d", funcId); + break; + } + return ret; +} diff --git a/services/src/small/screen_saver_handler.cpp b/services/src/small/screen_saver_handler.cpp new file mode 100644 index 0000000..7f86dd0 --- /dev/null +++ b/services/src/small/screen_saver_handler.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2021 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 "screen_saver_handler.h" + +#include + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_mgr.h" + +namespace OHOS { +ScreenSaverHandler::~ScreenSaverHandler() +{ + if (timer_ != nullptr) { + PowerMgrDestroyTimer(timer_); + timer_ = nullptr; + } +} + +AmsInterface *ScreenSaverHandler::GetAmsInterface() +{ + static struct AmsInterface *ams = nullptr; + if (ams != nullptr) { + return ams; + } + + IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE); + if (iUnknown == nullptr) { + POWER_HILOGE("Failed to get ams iUnknown"); + return nullptr; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&ams); + if ((ret != EC_SUCCESS) || (ams == nullptr)) { + POWER_HILOGE("Failed to query ams interface"); + return nullptr; + } + POWER_HILOGI("Succeed to get ams interface"); + return ams; +} + +bool ScreenSaverHandler::StartScreenSaverLocked() +{ + AmsInterface *ams = GetAmsInterface(); + if (ams == nullptr) { + POWER_HILOGE("Failed to get ams interface"); + return false; + } + + Want want = { nullptr }; + ElementName element = { nullptr }; + SetElementBundleName(&element, "com.huawei.screensaver"); + SetElementAbilityName(&element, "ScreensaverAbility"); + SetWantElement(&want, element); + SetWantData(&want, "WantData", strlen("WantData") + 1); + int32_t ret = ams->StartAbility(&want); + ClearElement(&element); + ClearWant(&want); + return ret == EC_SUCCESS; +} + +void ScreenSaverHandler::StartScreenSaver() +{ + POWER_HILOGI("Time to start screen saver"); + std::lock_guard lock(mutex_); + if (!enabled_ || screenSaverStarted_ || (RunningLockMgrIsLockHolding(RUNNINGLOCK_SCREEN) == TRUE)) { + return; + } + screenSaverStarted_ = StartScreenSaverLocked(); + POWER_HILOGI("Start screen saver: %d", static_cast(screenSaverStarted_)); +} + +void PowerTimerCallback(void *data) +{ + if (data == nullptr) { + POWER_HILOGE("Invalid timer data"); + return; + } + ScreenSaverHandler *handler = static_cast(data); + handler->StartScreenSaver(); +} + +void ScreenSaverHandler::Init() +{ + if (timer_ == nullptr) { + timer_ = PowerMgrCreateTimer(intervalMsec_, intervalMsec_, PowerTimerCallback); + } + std::thread lateInit([this] { + sleep(20); // set delay to avoid registing input evnet listener failure + SetState(true); + }); + lateInit.detach(); +} + +void ScreenSaverHandler::SetInterval(int64_t intervalMsec) +{ + std::lock_guard lock(mutex_); + if (intervalMsec_ == intervalMsec) { + return; + } + if (timer_ != nullptr) { + PowerMgrResetTimer(timer_, intervalMsec, intervalMsec); + if (enabled_) { + SetEnableLocked(); + } + } + intervalMsec_ = intervalMsec; +} + +bool ScreenSaverHandler::SetEnableLocked() +{ + if (PowerMgrStartTimer(timer_, (void *)this) == FALSE) { + POWER_HILOGE("Failed to start timer"); + return false; + } + if (!InputEventListenerProxy::GetInstance()->RegisterInputEventListener(this)) { + PowerMgrStopTimer(timer_); + POWER_HILOGE("Failed to register input event listener"); + return false; + } + POWER_HILOGI("Succeed to enable screen saver"); + return true; +} + +bool ScreenSaverHandler::SetDisableLocked() +{ + InputEventListenerProxy::GetInstance()->UnregisterInputEventListener(); + PowerMgrStopTimer(timer_); + POWER_HILOGI("Succeed to disable screen saver"); + return true; +} + +int32_t ScreenSaverHandler::SetState(bool enable) +{ + std::lock_guard lock(mutex_); + if (timer_ == nullptr) { + return EC_FAILURE; + } + if (enabled_ == enable) { + return EC_SUCCESS; + } + bool ret = enable ? SetEnableLocked() : SetDisableLocked(); + if (!ret) { + POWER_HILOGE("Failed to set state: %d", static_cast(enable)); + return EC_FAILURE; + } + enabled_ = enable; + return EC_SUCCESS; +} + +void ScreenSaverHandler::OnRawEvent(const RawEvent &event) +{ + static int64_t lastUpdateTime = 0; + std::lock_guard lock(mutex_); + POWER_HILOGI("User activity refreshed, restart the timer"); + screenSaverStarted_ = false; + + int64_t currentTime = GetCurrentTimeMsec(CLOCK_MONOTONIC); + if ((currentTime - lastUpdateTime) > MSEC_PER_SEC) { + PowerMgrRestartTimer(timer_, (void *)this); + lastUpdateTime = currentTime; + } +} +} // namespace OHOS diff --git a/services/src/small/screen_saver_mgr.cpp b/services/src/small/screen_saver_mgr.cpp new file mode 100644 index 0000000..69ddab5 --- /dev/null +++ b/services/src/small/screen_saver_mgr.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 "screen_saver_handler.h" + +static OHOS::ScreenSaverHandler g_handler; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void ScreenSaverMgrInit(void) +{ + g_handler.Init(); +} + +int32_t ScreenSaverMgrSetState(BOOL enable) +{ + return g_handler.SetState(enable == TRUE); +} + +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100644 index 0000000..5640d21 --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +config("powermgr_utils_public_config") { + include_dirs = [ "include" ] +} + +utils_sources = [ + "src/power_mgr_time_util.c", + "src/power_mgr_timer_util.c", +] + +if (is_mini_system) { + static_library("powermgr_utils") { + sources = utils_sources + + public_configs = [ ":powermgr_utils_public_config" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//kernel/liteos_m/kal/posix/include", + ] + + deps = [ + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//kernel/liteos_m/kernel:kernel", + ] + } +} else { + source_set("powermgr_utils") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + sources = utils_sources + + public_configs = [ ":powermgr_utils_public_config" ] + include_dirs = [ "${powermgr_frameworks_path}/include" ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] + } +} diff --git a/utils/include/power_mgr_time_util.h b/utils/include/power_mgr_time_util.h new file mode 100644 index 0000000..0a170b6 --- /dev/null +++ b/utils/include/power_mgr_time_util.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_TIME_UTIL_H +#define POWERMGR_POWER_MGR_TIME_UTIL_H + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +enum { + NSEC_PER_MIN = 60000000000LL, + NSEC_PER_SEC = 1000000000LL, + NSEC_PER_MSEC = 1000000LL, + NSEC_PER_USEC = 1000LL, + USEC_PER_SEC = 1000000LL, + USEC_PER_MSEC = 1000LL, + MSEC_PER_DAY = 86400000LL, + MSEC_PER_HOUR = 3600000LL, + MSEC_PER_MIN = 60000LL, + MSEC_PER_SEC = 1000LL, + SEC_PER_MIN = 60LL, + SEC_PER_HOUR = 3600LL, + SEC_PER_DAY = 86400LL, + MIN_PER_HOUR = 60LL, + HOUR_PER_DAY = 24LL, +}; + +static inline int64_t NsecToUsec(int64_t nsec) +{ + return nsec / NSEC_PER_USEC; +} + +static inline int64_t NsecToMsec(int64_t nsec) +{ + return nsec / NSEC_PER_MSEC; +} + +static inline int64_t UsecToMsec(int64_t usec) +{ + return usec / USEC_PER_MSEC; +} + +static inline int64_t MsecToNsec(int64_t msec) +{ + return msec * NSEC_PER_MSEC; +} + +static inline int64_t MsecToUsec(int64_t msec) +{ + return msec * USEC_PER_MSEC; +} + +static inline int64_t MsecToSec(int64_t msec) +{ + return msec / MSEC_PER_SEC; +} + +static inline int64_t MsecToMin(int64_t msec) +{ + return msec / MSEC_PER_MIN; +} + +static inline int64_t MsecToHour(int64_t msec) +{ + return msec / MSEC_PER_HOUR; +} + +static inline int64_t SecToNsec(int64_t sec) +{ + return sec * NSEC_PER_SEC; +} + +static inline int64_t SecToMsec(int64_t sec) +{ + return sec * MSEC_PER_SEC; +} + +static inline int64_t MinToNsec(int64_t mins) +{ + return mins * NSEC_PER_MIN; +} + +static inline int64_t MinToMsec(int64_t mins) +{ + return mins * MSEC_PER_MIN; +} + +static inline int64_t HourToMsec(int64_t hour) +{ + return hour * MSEC_PER_HOUR; +} + +static inline int64_t DayToMsec(int64_t day) +{ + return day * MSEC_PER_DAY; +} + +int64_t GetCurrentTimeMsec(clockid_t clkId); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_TIME_UTIL_H diff --git a/utils/include/power_mgr_timer_util.h b/utils/include/power_mgr_timer_util.h new file mode 100644 index 0000000..3e99ef7 --- /dev/null +++ b/utils/include/power_mgr_timer_util.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_TIMER_UTIL_H +#define POWERMGR_POWER_MGR_TIMER_UTIL_H + +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef timer_t PowerTimer; +typedef void (*PowerTimerCallback)(void *privateData); + +/* + * Create timer. Must call PowerMgrDestroyTimer to release memory after timer is not needed + */ +PowerTimer *PowerMgrCreateTimer(int64_t whenMsec, int64_t intervalMsec, PowerTimerCallback cb); + +/* + * Reset timer attributes and stop timer + */ +BOOL PowerMgrResetTimer(PowerTimer *timer, int64_t whenMsec, int64_t intervalMsec); + +/* + * Start timer. The privateData will set to PowerTimerCallback function when timer timeout + */ +BOOL PowerMgrStartTimer(PowerTimer *timer, void *privateData); + +/* + * Restart timer. The privateData will set to PowerTimerCallback function when timer timeout + */ +BOOL PowerMgrRestartTimer(PowerTimer *timer, void *privateData); + +/* + * Stop timer + */ +BOOL PowerMgrStopTimer(PowerTimer *timer); + +/* + * Destory timer + */ +void PowerMgrDestroyTimer(PowerTimer *timer); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_TIMER_UTIL_H diff --git a/utils/src/power_mgr_time_util.c b/utils/src/power_mgr_time_util.c new file mode 100644 index 0000000..9ec544d --- /dev/null +++ b/utils/src/power_mgr_time_util.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 "power_mgr_time_util.h" + +int64_t GetCurrentTimeMsec(clockid_t clkId) +{ + struct timespec ts; + clock_gettime(clkId, &ts); + return SecToMsec(ts.tv_sec) + NsecToMsec(ts.tv_nsec); +} diff --git a/utils/src/power_mgr_timer_util.c b/utils/src/power_mgr_timer_util.c new file mode 100644 index 0000000..5f53454 --- /dev/null +++ b/utils/src/power_mgr_timer_util.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2021 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 "power_mgr_timer_util.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr_time_util.h" + +typedef struct { + PowerTimer timerId; + BOOL isRunning; + int64_t whenMsec; + int64_t intervalMsec; + PowerTimerCallback timerCb; + void *data; +} PowerTimerInfo; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static BOOL g_inited = FALSE; +static Vector g_timers; + +static inline PowerTimerInfo *GetPowerTimerInfo(PowerTimer *timer) +{ + return GET_OBJECT(timer, PowerTimerInfo, timerId); +} + +static void SetTimeSpec(struct timespec *ts, int64_t msec) +{ + ts->tv_sec = MsecToSec(msec); + msec -= SecToMsec(ts->tv_sec); + ts->tv_nsec = MsecToNsec(msec); +} + +static BOOL StartTimer(PowerTimer timer, int64_t whenMsec, int64_t intervalMsec) +{ + struct itimerspec ts; + SetTimeSpec(&ts.it_value, whenMsec); + SetTimeSpec(&ts.it_interval, intervalMsec); + int32_t ret = timer_settime(timer, 0, &ts, NULL); + if (ret < 0) { + POWER_HILOGE("Failed to start timer: %s", strerror(errno)); + return FALSE; + } + POWER_HILOGD("Succeed to start timer, id: %p", timer); + return TRUE; +} + +static void TimerHandle(union sigval v) +{ + PowerTimerInfo *info = (PowerTimerInfo *)v.sival_ptr; + if (info == NULL) { + POWER_HILOGE("Invalid timer info"); + return; + } + POWER_HILOGD("Timer trigger, id: %p", info->timerId); + if (info->timerCb != NULL) { + info->timerCb(info->data); + } +} + +PowerTimer *PowerMgrCreateTimer(int64_t whenMsec, int64_t intervalMsec, PowerTimerCallback cb) +{ + PowerTimerInfo *info = (PowerTimerInfo *)malloc(sizeof(PowerTimerInfo)); + if (info == NULL) { + POWER_HILOGE("Failed allocate timer info"); + return NULL; + } + (void)memset_s(info, sizeof(PowerTimerInfo), 0, sizeof(PowerTimerInfo)); + info->isRunning = FALSE; + info->whenMsec = whenMsec; + info->intervalMsec = intervalMsec; + info->timerCb = cb; + + struct sigevent evp; + memset_s (&evp, sizeof(evp), 0, sizeof(evp)); + evp.sigev_value.sival_ptr = info; + evp.sigev_notify = SIGEV_THREAD; + evp.sigev_notify_function = TimerHandle; + int32_t ret = timer_create(CLOCK_REALTIME, &evp, &info->timerId); + if (ret < 0) { + POWER_HILOGE("Failed to create timer: %s", strerror(errno)); + free(info); + return NULL; + } + POWER_HILOGD("Succeed to create timer, id: %p", info->timerId); + + return &info->timerId; +} + +BOOL PowerMgrResetTimer(PowerTimer *timer, int64_t whenMsec, int64_t intervalMsec) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + PowerMgrStopTimer(timer); + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->whenMsec = whenMsec; + info->intervalMsec = intervalMsec; + return TRUE; +} + +BOOL PowerMgrStartTimer(PowerTimer *timer, void *privateData) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->data = privateData; + info->isRunning = TRUE; + return StartTimer(info->timerId, info->whenMsec, info->intervalMsec); +} + +BOOL PowerMgrRestartTimer(PowerTimer *timer, void *privateData) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + return PowerMgrStartTimer(timer, privateData); +} + +BOOL PowerMgrStopTimer(PowerTimer *timer) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->isRunning = FALSE; + return StartTimer(info->timerId, 0, 0); +} + +void PowerMgrDestroyTimer(PowerTimer *timer) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + int32_t ret = timer_delete(info->timerId); + POWER_HILOGD("Destory timer: %d", ret); + free(info); +} -- Gitee From a37a5ff0cfe9dec862a812fcafe764806751f60e Mon Sep 17 00:00:00 2001 From: zili Date: Thu, 17 Jun 2021 09:17:05 +0800 Subject: [PATCH 2/2] refactor powermgr and add running lock feature Signed-off-by: zili --- BUILD.gn | 21 ++ services/config.gni => config.gni | 2 +- frameworks/BUILD.gn | 63 ++++++ frameworks/include/hilog_wrapper.h | 67 ++++++ .../include/mini/running_lock_interface.h | 35 +++ frameworks/include/power_mgr.h | 30 +++ frameworks/include/running_lock_entry.h | 55 +++++ frameworks/include/running_lock_inner.h | 42 ++++ frameworks/include/running_lock_intf_define.h | 36 +++ frameworks/include/screen_saver_intf_define.h | 33 +++ .../include/small/running_lock_interface.h | 48 ++++ .../include/small/screen_saver_interface.h | 47 ++++ frameworks/src/mini/BUILD.gn | 40 ++++ frameworks/src/mini/power_screen_saver.c | 23 ++ frameworks/src/mini/running_lock_inner.c | 82 +++++++ frameworks/src/running_lock.c | 149 ++++++++++++ frameworks/src/small/BUILD.gn | 38 ++++ frameworks/src/small/power_screen_saver.c | 135 +++++++++++ frameworks/src/small/running_lock_inner.c | 182 +++++++++++++++ interfaces/innerkits/power_screen_saver.h | 30 +++ interfaces/kits/running_lock.h | 68 ++++++ powermgr.gni | 45 ++++ services/BUILD.gn | 77 +++---- services/include/power_manage_feature.h | 99 -------- services/include/running_lock_feature.h | 49 ++++ services/include/running_lock_handler.h | 29 +++ services/include/running_lock_mgr.h | 38 ++++ services/include/screen_saver_feature.h | 47 ++++ services/include/screen_saver_mgr.h | 29 +++ .../screen_saver_handler.h} | 61 ++--- services/source/power_manage_feature.cpp | 213 ------------------ services/src/mini/BUILD.gn | 37 +++ services/src/mini/running_lock_feature_impl.c | 31 +++ services/src/mini/running_lock_handler.c | 32 +++ .../power_manage_service.c} | 78 ++++--- services/src/running_lock_feature.c | 100 ++++++++ services/src/running_lock_mgr.c | 162 +++++++++++++ services/src/screen_saver_feature.c | 94 ++++++++ services/src/small/BUILD.gn | 71 ++++++ .../src/small/running_lock_feature_impl.c | 77 +++++++ services/src/small/running_lock_handler.c | 26 +++ .../src/small/screen_saver_feature_impl.c | 70 ++++++ services/src/small/screen_saver_handler.cpp | 181 +++++++++++++++ services/src/small/screen_saver_mgr.cpp | 36 +++ utils/BUILD.gn | 55 +++++ utils/include/power_mgr_time_util.h | 119 ++++++++++ utils/include/power_mgr_timer_util.h | 63 ++++++ utils/src/power_mgr_time_util.c | 23 ++ utils/src/power_mgr_timer_util.c | 169 ++++++++++++++ 49 files changed, 2923 insertions(+), 414 deletions(-) create mode 100644 BUILD.gn rename services/config.gni => config.gni (99%) mode change 100755 => 100644 create mode 100644 frameworks/BUILD.gn create mode 100644 frameworks/include/hilog_wrapper.h create mode 100644 frameworks/include/mini/running_lock_interface.h create mode 100644 frameworks/include/power_mgr.h create mode 100644 frameworks/include/running_lock_entry.h create mode 100644 frameworks/include/running_lock_inner.h create mode 100644 frameworks/include/running_lock_intf_define.h create mode 100644 frameworks/include/screen_saver_intf_define.h create mode 100644 frameworks/include/small/running_lock_interface.h create mode 100644 frameworks/include/small/screen_saver_interface.h create mode 100644 frameworks/src/mini/BUILD.gn create mode 100644 frameworks/src/mini/power_screen_saver.c create mode 100644 frameworks/src/mini/running_lock_inner.c create mode 100644 frameworks/src/running_lock.c create mode 100644 frameworks/src/small/BUILD.gn create mode 100644 frameworks/src/small/power_screen_saver.c create mode 100644 frameworks/src/small/running_lock_inner.c create mode 100644 interfaces/innerkits/power_screen_saver.h create mode 100644 interfaces/kits/running_lock.h create mode 100644 powermgr.gni mode change 100755 => 100644 services/BUILD.gn delete mode 100755 services/include/power_manage_feature.h create mode 100644 services/include/running_lock_feature.h create mode 100644 services/include/running_lock_handler.h create mode 100644 services/include/running_lock_mgr.h create mode 100644 services/include/screen_saver_feature.h create mode 100644 services/include/screen_saver_mgr.h rename services/include/{power_manage_service.h => small/screen_saver_handler.h} (38%) delete mode 100755 services/source/power_manage_feature.cpp create mode 100644 services/src/mini/BUILD.gn create mode 100644 services/src/mini/running_lock_feature_impl.c create mode 100644 services/src/mini/running_lock_handler.c rename services/{source/power_manage_service.cpp => src/power_manage_service.c} (33%) mode change 100755 => 100644 create mode 100644 services/src/running_lock_feature.c create mode 100644 services/src/running_lock_mgr.c create mode 100644 services/src/screen_saver_feature.c create mode 100644 services/src/small/BUILD.gn create mode 100644 services/src/small/running_lock_feature_impl.c create mode 100644 services/src/small/running_lock_handler.c create mode 100644 services/src/small/screen_saver_feature_impl.c create mode 100644 services/src/small/screen_saver_handler.cpp create mode 100644 services/src/small/screen_saver_mgr.cpp create mode 100644 utils/BUILD.gn create mode 100644 utils/include/power_mgr_time_util.h create mode 100644 utils/include/power_mgr_timer_util.h create mode 100644 utils/src/power_mgr_time_util.c create mode 100644 utils/src/power_mgr_timer_util.c diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000..b0fa50a --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2021 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/lite/config/component/lite_component.gni") + +lite_component("powermgr_lite") { + features = [ + "frameworks:powermgr", + "services:powermgrservice", + ] +} diff --git a/services/config.gni b/config.gni old mode 100755 new mode 100644 similarity index 99% rename from services/config.gni rename to config.gni index 4b2c6e4..cf5ad14 --- a/services/config.gni +++ b/config.gni @@ -10,7 +10,7 @@ # 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. -# +# declare_args() { enable_screensaver = false diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn new file mode 100644 index 0000000..1c8a676 --- /dev/null +++ b/frameworks/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +local_include_dirs = [] +local_deps = [] + +if (is_mini_system) { + local_include_dirs += [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//kernel/liteos_m/kal/posix/include", + ] +} else { + local_deps += [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//third_party/bounds_checking_function:libsec_shared", + ] +} + +config("powermgr_public_config") { + include_dirs = [ + "${powermgr_innerkits_path}", + "${powermgr_kits_path}", + ] +} + +lite_library("powermgr") { + target_type = lite_library_type + + sources = [ "src/running_lock.c" ] + + include_dirs = [ + "include", + "include/${system_type}", + "//utils/native/lite/include", + ] + include_dirs += local_include_dirs + + public_configs = [ ":powermgr_public_config" ] + + deps = [ + "src/${system_type}:runninglock_impl", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + ] + deps += local_deps + + if (enable_screensaver) { + deps += [ "src/${system_type}:screensaver_impl" ] + } +} diff --git a/frameworks/include/hilog_wrapper.h b/frameworks/include/hilog_wrapper.h new file mode 100644 index 0000000..8007eee --- /dev/null +++ b/frameworks/include/hilog_wrapper.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 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 POWERMGR_HILOG_WRAPPER_H +#define POWERMGR_HILOG_WRAPPER_H + +#define LOG_TAG "PowerMgr" + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define __FORMATED__(fmt, ...) "[%s] %s# " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__ + +#ifdef POWER_HILOGD +#undef POWER_HILOGD +#endif + +#ifdef POWER_HILOGI +#undef POWER_HILOGI +#endif + +#ifdef POWER_HILOGW +#undef POWER_HILOGW +#endif + +#ifdef POWER_HILOGE +#undef POWER_HILOGE +#endif + +#ifdef POWER_HILOGF +#undef POWER_HILOGF +#endif + +#ifdef __LITEOS_M__ +#define POWER_HILOGD(...) HILOG_DEBUG(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGI(...) HILOG_INFO(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGW(...) HILOG_WARN(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGE(...) HILOG_ERROR(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#define POWER_HILOGF(...) HILOG_FATAL(HILOG_MODULE_POWERMGR, __VA_ARGS__) +#else // !__LITEOS_M__ +#define POWER_HILOGD(...) HILOG_DEBUG(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGI(...) HILOG_INFO(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGW(...) HILOG_WARN(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGE(...) HILOG_ERROR(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#define POWER_HILOGF(...) HILOG_FATAL(LOG_CORE, __FORMATED__(__VA_ARGS__)) +#endif // __LITEOS_M__ + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_HILOG_WRAPPER_H diff --git a/frameworks/include/mini/running_lock_interface.h b/frameworks/include/mini/running_lock_interface.h new file mode 100644 index 0000000..56db47a --- /dev/null +++ b/frameworks/include/mini/running_lock_interface.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTERFACE_H +#define POWERMGR_RUNNING_LOCK_INTERFACE_H + +#include + +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct { + INHERIT_IUNKNOWN; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTERFACE_H diff --git a/frameworks/include/power_mgr.h b/frameworks/include/power_mgr.h new file mode 100644 index 0000000..046eb4a --- /dev/null +++ b/frameworks/include/power_mgr.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_H +#define POWERMGR_POWER_MGR_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_MANAGE_SERVICE "powermgr" + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_H diff --git a/frameworks/include/running_lock_entry.h b/frameworks/include/running_lock_entry.h new file mode 100644 index 0000000..381a55a --- /dev/null +++ b/frameworks/include/running_lock_entry.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_ENTRY_H +#define POWERMGR_RUNNING_LOCK_ENTRY_H + +#include + +#include "running_lock.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct { + uint32_t pid; + uint64_t token; +} RunningLockIdentity; + +typedef struct { + RunningLock lock; + RunningLockIdentity identity; +} RunningLockEntry; + +static inline RunningLockEntry *GetRunningLockEntry(const RunningLock *lock) +{ + return GET_OBJECT(lock, RunningLockEntry, lock); +} + +static inline BOOL IsValidRunningLockEntry(RunningLockEntry *entry) +{ + return (entry != NULL) && ((entry->lock.type >= 0) && (entry->lock.type < RUNNINGLOCK_BUTT)) ? TRUE : FALSE; +} + +static inline BOOL IsSameRunningLockIdentity(const RunningLockIdentity *a, const RunningLockIdentity *b) +{ + return ((a->pid == b->pid) && (a->token == b->token)) ? TRUE : FALSE; +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_ENTRY_H diff --git a/frameworks/include/running_lock_inner.h b/frameworks/include/running_lock_inner.h new file mode 100644 index 0000000..dca92c5 --- /dev/null +++ b/frameworks/include/running_lock_inner.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INNER_H +#define POWERMGR_RUNNING_LOCK_INNER_H + +#include +#include + +#include "power_mgr.h" +#include "running_lock_entry.h" +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +static inline IUnknown *GetRunningLockIUnknown(void) +{ + return SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE); +} + +void InitIdentity(RunningLockEntry *entry); +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs); +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INNER_H diff --git a/frameworks/include/running_lock_intf_define.h b/frameworks/include/running_lock_intf_define.h new file mode 100644 index 0000000..f24a356 --- /dev/null +++ b/frameworks/include/running_lock_intf_define.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTF_DEFINE_H +#define POWERMGR_RUNNING_LOCK_INTF_DEFINE_H + +#include + +#include "running_lock_entry.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_RUNNING_LOCK_FEATURE "runninglock" + +#define INHERIT_RUNNINGLOCK_INTERFACE \ + int32_t (*AcquireRunningLockEntryFunc)(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); \ + int32_t (*ReleaseRunningLockEntryFunc)(IUnknown *iUnknown, RunningLockEntry *entry) + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTF_DEFINE_H diff --git a/frameworks/include/screen_saver_intf_define.h b/frameworks/include/screen_saver_intf_define.h new file mode 100644 index 0000000..0bb1d9d --- /dev/null +++ b/frameworks/include/screen_saver_intf_define.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_INTF_DEFINE_H +#define POWERMGR_SCREEN_SAVER_INTF_DEFINE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#define POWER_SCREEN_SAVER_FEATURE "screensaver" + +#define INHERIT_SCREENSAVER_INTERFACE \ + int32_t (*SetScreenSaverStateFunc)(IUnknown *iUnknown, BOOL enable) + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_INTF_DEFINE_H diff --git a/frameworks/include/small/running_lock_interface.h b/frameworks/include/small/running_lock_interface.h new file mode 100644 index 0000000..85640fe --- /dev/null +++ b/frameworks/include/small/running_lock_interface.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_INTERFACE_H +#define POWERMGR_RUNNING_LOCK_INTERFACE_H + +#include +#include +#include + +#include "running_lock_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + RUNNINGLOCK_FUNCID_ACQUIRE = 0, + RUNNINGLOCK_FUNCID_RELEASE, + RUNNINGLOCK_FUNCID_BUTT, +} RunningLockFuncId; + +typedef struct { + INHERIT_SERVER_IPROXY; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockInterface; + +typedef struct { + INHERIT_CLIENT_IPROXY; + INHERIT_RUNNINGLOCK_INTERFACE; +} RunningLockProxyInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_INTERFACE_H diff --git a/frameworks/include/small/screen_saver_interface.h b/frameworks/include/small/screen_saver_interface.h new file mode 100644 index 0000000..4a9878d --- /dev/null +++ b/frameworks/include/small/screen_saver_interface.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_INTERFACE_H +#define POWERMGR_SCREEN_SAVER_INTERFACE_H + +#include +#include +#include + +#include "screen_saver_intf_define.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + SCREENSAVER_FUNCID_SETSTATE = 0, + SCREENSAVER_FUNCID_BUTT, +} ScreenSaverFuncId; + +typedef struct { + INHERIT_SERVER_IPROXY; + INHERIT_SCREENSAVER_INTERFACE; +} ScreenSaverInterface; + +typedef struct { + INHERIT_CLIENT_IPROXY; + INHERIT_SCREENSAVER_INTERFACE; +} ScreenSaverProxyInterface; + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_INTERFACE_H diff --git a/frameworks/src/mini/BUILD.gn b/frameworks/src/mini/BUILD.gn new file mode 100644 index 0000000..d567d40 --- /dev/null +++ b/frameworks/src/mini/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +static_library("runninglock_impl") { + sources = [ "running_lock_inner.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", + "//kernel/liteos_m/kal/posix/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +static_library("screensaver_impl") { + sources = [ "power_screen_saver.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//utils/native/lite/include", + ] +} diff --git a/frameworks/src/mini/power_screen_saver.c b/frameworks/src/mini/power_screen_saver.c new file mode 100644 index 0000000..f1b0e8b --- /dev/null +++ b/frameworks/src/mini/power_screen_saver.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 "hilog_wrapper.h" + +BOOL SetScreenSaverState(BOOL enable) +{ + (void)enable; + POWER_HILOGE("Not support screen saver"); + return FALSE; +} diff --git a/frameworks/src/mini/running_lock_inner.c b/frameworks/src/mini/running_lock_inner.c new file mode 100644 index 0000000..569cdd9 --- /dev/null +++ b/frameworks/src/mini/running_lock_inner.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2021 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 "running_lock_inner.h" + +#include + +#include + +#include "hilog_wrapper.h" +#include "running_lock_interface.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static RunningLockInterface *g_intf = NULL; + +static RunningLockInterface *GetRunningLockInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + IUnknown *iUnknown = GetRunningLockIUnknown(); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get running lock iUnknown"); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query running lock interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + + return g_intf; +} + +void InitIdentity(RunningLockEntry *entry) +{ + if (entry == NULL) { + return; + } + entry->identity.pid = 0; + entry->identity.token = (uint64_t)(uintptr_t)entry; +} + +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + int32_t ret = EC_FAILURE; + RunningLockInterface *intf = GetRunningLockInterface(); + if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) { + ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} + +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry) +{ + int32_t ret = EC_FAILURE; + RunningLockInterface *intf = GetRunningLockInterface(); + if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) { + ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/frameworks/src/running_lock.c b/frameworks/src/running_lock.c new file mode 100644 index 0000000..11d23c2 --- /dev/null +++ b/frameworks/src/running_lock.c @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2021 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 "running_lock.h" + +#include +#include + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_entry.h" +#include "running_lock_inner.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static BOOL g_inited = FALSE; +static Vector g_runningLocks; + +static BOOL AddRunningLock(RunningLock *lock) +{ + pthread_mutex_lock(&g_mutex); + if (g_inited == FALSE) { + g_runningLocks = VECTOR_Make(NULL, NULL); + g_inited = TRUE; + } + int16_t pos = VECTOR_Add(&g_runningLocks, (void *)lock); + if (pos == INVALID_INDEX) { + POWER_HILOGE("Failed to add lock to vector for lock: %s", lock->name); + pthread_mutex_unlock(&g_mutex); + return FALSE; + } + POWER_HILOGD("Add running lock, name: %s, type: %d", lock->name, lock->type); + pthread_mutex_unlock(&g_mutex); + return TRUE; +} + +static BOOL RemoveRunningLock(const RunningLock *lock) +{ + pthread_mutex_lock(&g_mutex); + int16_t pos = VECTOR_Find(&g_runningLocks, (void *)lock); + if (pos < 0) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + pthread_mutex_unlock(&g_mutex); + return FALSE; + } + VECTOR_Swap(&g_runningLocks, pos, NULL); + POWER_HILOGD("Remove running lock, name: %s, type: %d", lock->name, lock->type); + pthread_mutex_unlock(&g_mutex); + return TRUE; +} + +static BOOL IsRunningLockExisted(const RunningLock *lock) +{ + if (lock == NULL) { + POWER_HILOGE("Invalid running lock"); + return FALSE; + } + pthread_mutex_lock(&g_mutex); + BOOL ret = (VECTOR_Find(&g_runningLocks, (void *)lock) >= 0) ? TRUE : FALSE; + pthread_mutex_unlock(&g_mutex); + return ret; +} + +static RunningLockEntry *CreateRunningLockEntry(const char *name, RunningLockType type, RunningLockFlag flag) +{ + RunningLockEntry *entry = (RunningLockEntry *)malloc(sizeof(RunningLockEntry)); + if (entry == NULL) { + POWER_HILOGE("Failed allocate running lock entry"); + return NULL; + } + (void)memset_s(entry, sizeof(RunningLockEntry), 0, sizeof(RunningLockEntry)); + InitIdentity(entry); + entry->lock.type = type; + entry->lock.flag = flag; + if(strcpy_s( entry->lock.name, sizeof(entry->lock.name), name) != EOK) { + free(entry); + return NULL; + } + return entry; +} + +static void DestroyRunningLockEntry(RunningLockEntry *entry) +{ + if (entry != NULL) { + POWER_HILOGD("Free entry: %p", entry); + free(entry); + } +} + +const RunningLock *CreateRunningLock(const char *name, RunningLockType type, RunningLockFlag flag) +{ + if ((name == NULL) || (type >= RUNNINGLOCK_BUTT)) { + POWER_HILOGE("Invalid running lock name"); + return NULL; + } + + RunningLockEntry *entry = CreateRunningLockEntry(name, type, flag); + if (entry == NULL) { + POWER_HILOGE("Failed create running lock entry"); + return NULL; + } + AddRunningLock(&entry->lock); + POWER_HILOGI("Create %s, pid: %u, token: %llu", name, entry->identity.pid, (long long)entry->identity.token); + return &entry->lock; +} + +void DestroyRunningLock(const RunningLock *lock) +{ + if (lock == NULL) { + POWER_HILOGE("Invalid running lock"); + return; + } + if (RemoveRunningLock(lock) == TRUE) { + DestroyRunningLockEntry(GetRunningLockEntry(lock)); + } +} + +BOOL AcquireRunningLock(const RunningLock *lock) +{ + if (IsRunningLockExisted(lock) == FALSE) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + return FALSE; + } + return AcquireRunningLockEntry(GetRunningLockEntry(lock), -1); +} + +BOOL ReleaseRunningLock(const RunningLock *lock) +{ + if (IsRunningLockExisted(lock) == FALSE) { + POWER_HILOGE("Non-existent running lock: %s", lock->name); + return FALSE; + } + return ReleaseRunningLockEntry(GetRunningLockEntry(lock)); +} diff --git a/frameworks/src/small/BUILD.gn b/frameworks/src/small/BUILD.gn new file mode 100644 index 0000000..43b3a2f --- /dev/null +++ b/frameworks/src/small/BUILD.gn @@ -0,0 +1,38 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +source_set("runninglock_impl") { + sources = [ "running_lock_inner.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +source_set("screensaver_impl") { + sources = [ "power_screen_saver.c" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} diff --git a/frameworks/src/small/power_screen_saver.c b/frameworks/src/small/power_screen_saver.c new file mode 100644 index 0000000..eb068eb --- /dev/null +++ b/frameworks/src/small/power_screen_saver.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "screen_saver_interface.h" + +#define MAX_DATA_LEN 1024 + +typedef struct { + INHERIT_IUNKNOWNENTRY(ScreenSaverProxyInterface); +} ScreenSaverProxyEntry; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static ScreenSaverProxyInterface *g_intf = NULL; + +static int32_t SetScreenSaverStateProxy(IUnknown *iUnknown, BOOL enable); + +static void *CreatClient(const char *service, const char *feature, uint32_t size) +{ + (void)service; + (void)feature; + uint32_t len = size + sizeof(ScreenSaverProxyEntry); + uint8_t *client = malloc(len); + if (client == NULL) { + POWER_HILOGE("Failed to allocate memory for screen saver proxy entry"); + return NULL; + } + (void)memset_s(client, len, 0, len); + ScreenSaverProxyEntry *entry = (ScreenSaverProxyEntry *)&client[size]; + entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION); + entry->ref = 1; + entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface; + entry->iUnknown.AddRef = IUNKNOWN_AddRef; + entry->iUnknown.Release = IUNKNOWN_Release; + entry->iUnknown.Invoke = NULL; + entry->iUnknown.SetScreenSaverStateFunc = SetScreenSaverStateProxy; + return client; +} + +static void DestroyClient(const char *service, const char *feature, void *iproxy) +{ + free(iproxy); +} + +static ScreenSaverProxyInterface *GetScreenSaverProxyInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + + SAMGR_RegisterFactory(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE, CreatClient, DestroyClient); + + IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get screen saver iUnknown"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query screen saver interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + POWER_HILOGI("Succeed to get screen saver proxy interface"); + return g_intf; +} + +static int32_t Callback(IOwner owner, int32_t code, IpcIo *reply) +{ + if ((reply == NULL) || (owner == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + + int32_t *ret = (int32_t *)owner; + *ret = IpcIoPopInt32(reply); + POWER_HILOGD("Power screen saver callback: %d, code: %d", *ret, code); + return EC_SUCCESS; +} + +static int32_t SetScreenSaverStateProxy(IUnknown *iUnknown, BOOL enable) +{ + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushBool(&request, enable == TRUE); + + int32_t ret; + ScreenSaverProxyInterface *proxy = (ScreenSaverProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, SCREENSAVER_FUNCID_SETSTATE, &request, &ret, Callback); + POWER_HILOGD("Set screen saver state done, enable: %d", (int32_t)enable); + + return ret; +} + +BOOL SetScreenSaverState(BOOL enable) +{ + int32_t ret = EC_FAILURE; + ScreenSaverProxyInterface *intf = GetScreenSaverProxyInterface(); + if ((intf != NULL) && (intf->SetScreenSaverStateFunc != NULL)) { + ret = intf->SetScreenSaverStateFunc((IUnknown *)intf, enable); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/frameworks/src/small/running_lock_inner.c b/frameworks/src/small/running_lock_inner.c new file mode 100644 index 0000000..3a8d858 --- /dev/null +++ b/frameworks/src/small/running_lock_inner.c @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2021 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 "running_lock_inner.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_interface.h" + +#define MAX_DATA_LEN 1024 + +typedef struct { + INHERIT_IUNKNOWNENTRY(RunningLockProxyInterface); +} RunningLockProxyEntry; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static RunningLockProxyInterface *g_intf = NULL; + +static int32_t AcquireRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); +static int32_t ReleaseRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry); + +static void *CreatClient(const char *service, const char *feature, uint32_t size) +{ + (void)service; + (void)feature; + uint32_t len = size + sizeof(RunningLockProxyEntry); + uint8_t *client = malloc(len); + if (client == NULL) { + POWER_HILOGE("Failed to allocate memory for running lock proxy entry"); + return NULL; + } + (void)memset_s(client, len, 0, len); + RunningLockProxyEntry *entry = (RunningLockProxyEntry *)&client[size]; + entry->ver = ((uint16)CLIENT_PROXY_VER | (uint16)DEFAULT_VERSION); + entry->ref = 1; + entry->iUnknown.QueryInterface = IUNKNOWN_QueryInterface; + entry->iUnknown.AddRef = IUNKNOWN_AddRef; + entry->iUnknown.Release = IUNKNOWN_Release; + entry->iUnknown.Invoke = NULL; + entry->iUnknown.AcquireRunningLockEntryFunc = AcquireRunningLockEntryProxy; + entry->iUnknown.ReleaseRunningLockEntryFunc = ReleaseRunningLockEntryProxy; + return client; +} + +static void DestroyClient(const char *service, const char *feature, void *iproxy) +{ + free(iproxy); +} + +static RunningLockProxyInterface *GetRunningLockProxyInterface(void) +{ + if (g_intf != NULL) { + return g_intf; + } + pthread_mutex_lock(&g_mutex); + if (g_intf != NULL) { + pthread_mutex_unlock(&g_mutex); + return g_intf; + } + + SAMGR_RegisterFactory(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE, CreatClient, DestroyClient); + + IUnknown *iUnknown = GetRunningLockIUnknown(); + if (iUnknown == NULL) { + POWER_HILOGE("Failed to get running lock iUnknown"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); + if ((ret != EC_SUCCESS) || (g_intf == NULL)) { + POWER_HILOGE("Failed to query running lock interface"); + pthread_mutex_unlock(&g_mutex); + return NULL; + } + pthread_mutex_unlock(&g_mutex); + POWER_HILOGI("Succeed to get running lock proxy interface"); + return g_intf; +} + +static int32_t Callback(IOwner owner, int32_t code, IpcIo *reply) +{ + if ((reply == NULL) || (owner == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + + int32_t *ret = (int32_t *)owner; + *ret = IpcIoPopInt32(reply); + POWER_HILOGD("Running lock callback: %d, code: %d", *ret, code); + return EC_SUCCESS; +} + +static int32_t AcquireRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushFlatObj(&request, entry, sizeof(RunningLockEntry)); + IpcIoPushInt32(&request, timeoutMs); + + int32_t ret; + RunningLockProxyInterface *proxy = (RunningLockProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, RUNNINGLOCK_FUNCID_ACQUIRE, &request, &ret, Callback); + POWER_HILOGD("Acquire running lock done, name: %s, type: %d", entry->lock.name, entry->lock.type); + + return ret; +} + +static int32_t ReleaseRunningLockEntryProxy(IUnknown *iUnknown, RunningLockEntry *entry) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + IpcIo request; + char buffer[MAX_DATA_LEN]; + IpcIoInit(&request, buffer, MAX_DATA_LEN, 0); + IpcIoPushFlatObj(&request, entry, sizeof(RunningLockEntry)); + + int32_t ret; + RunningLockProxyInterface *proxy = (RunningLockProxyInterface *)iUnknown; + proxy->Invoke((IClientProxy *)proxy, RUNNINGLOCK_FUNCID_RELEASE, &request, &ret, Callback); + POWER_HILOGD("Release running lock done, name: %s, type: %d", entry->lock.name, entry->lock.type); + + return ret; +} + +void InitIdentity(RunningLockEntry *entry) +{ + if (entry == NULL) { + return; + } + entry->identity.pid = getpid(); + entry->identity.token = (uint64_t)(uintptr_t)entry; +} + +BOOL AcquireRunningLockEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + int32_t ret = EC_FAILURE; + RunningLockProxyInterface *intf = GetRunningLockProxyInterface(); + if ((intf != NULL) && (intf->AcquireRunningLockEntryFunc != NULL)) { + ret = intf->AcquireRunningLockEntryFunc((IUnknown *)intf, entry, timeoutMs); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} + +BOOL ReleaseRunningLockEntry(RunningLockEntry *entry) +{ + int32_t ret = EC_FAILURE; + RunningLockProxyInterface *intf = GetRunningLockProxyInterface(); + if ((intf != NULL) && (intf->ReleaseRunningLockEntryFunc != NULL)) { + ret = intf->ReleaseRunningLockEntryFunc((IUnknown *)intf, entry); + } + return (ret == EC_SUCCESS) ? TRUE : FALSE; +} diff --git a/interfaces/innerkits/power_screen_saver.h b/interfaces/innerkits/power_screen_saver.h new file mode 100644 index 0000000..d108186 --- /dev/null +++ b/interfaces/innerkits/power_screen_saver.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_SCREEN_SAVER_H +#define POWERMGR_POWER_SCREEN_SAVER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +BOOL SetScreenSaverState(BOOL enable); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_SCREEN_SAVER_H diff --git a/interfaces/kits/running_lock.h b/interfaces/kits/running_lock.h new file mode 100644 index 0000000..2868dc5 --- /dev/null +++ b/interfaces/kits/running_lock.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_H +#define POWERMGR_RUNNING_LOCK_H + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef enum { + /** + * RunningLock type: used to keep screen on. + */ + RUNNINGLOCK_SCREEN, + /** + * RunningLock type: used to keep cpu running. + */ + RUNNINGLOCK_BACKGROUND, + /** + * RunningLock type: used to keep the screen on/off when the proximity sensor is active. + */ + RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, + RUNNINGLOCK_BUTT +} RunningLockType; + +typedef enum { + RUNNINGLOCK_FLAG_NONE = 0, + + /** + * Wakeup device when running lock is acquired. + */ + RUNNINGLOCK_FLAG_WAKEUP_WHEN_ACQUIRED = 1 << 0, +} RunningLockFlag; + +#define RUNNING_LOCK_NAME_LEN 64 + +typedef struct { + char name[RUNNING_LOCK_NAME_LEN]; + RunningLockType type; + RunningLockFlag flag; +} RunningLock; + +const RunningLock *CreateRunningLock(const char *name, RunningLockType type, RunningLockFlag flag); +void DestroyRunningLock(const RunningLock *lock); +BOOL AcquireRunningLock(const RunningLock *lock); +BOOL ReleaseRunningLock(const RunningLock *lock); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_H diff --git a/powermgr.gni b/powermgr.gni new file mode 100644 index 0000000..25a4f01 --- /dev/null +++ b/powermgr.gni @@ -0,0 +1,45 @@ +# Copyright (c) 2021 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/lite/config/component/lite_component.gni") +import("config.gni") + +powermgr_path = "//base/powermgr/powermgr_lite" + +powermgr_frameworks_path = "${powermgr_path}/frameworks" + +powermgr_interfaces_path = "${powermgr_path}/interfaces" + +powermgr_innerkits_path = "${powermgr_interfaces_path}/innerkits" + +powermgr_kits_path = "${powermgr_interfaces_path}/kits" + +powermgr_services_path = "${powermgr_path}/services" + +powermgr_utils_path = "${powermgr_path}/utils" + +declare_args() { + is_mini_system = false + is_small_system = false +} + +if (ohos_kernel_type == "liteos_m") { + is_mini_system = true + lite_library_type = "static_library" + system_type = "mini" +} else { + is_small_system = true + lite_library_type = "shared_library" + system_type = "small" +} diff --git a/services/BUILD.gn b/services/BUILD.gn old mode 100755 new mode 100644 index 43c135d..9935f0a --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -11,58 +11,51 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//base/powermgr/powermgr_lite/powermgr.gni") import("//build/lite/config/component/lite_component.gni") -shared_library("pmslite") { - cflags = [ "-Wall" ] - cflags_cc = cflags + +local_include_dirs = [] +local_deps = [] +local_sources = [] + +if (is_mini_system) { + local_include_dirs += [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", + "//kernel/liteos_m/kal/posix/include", + ] +} else { + local_deps += + [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" ] + + if (enable_screensaver) { + local_sources += [ "src/screen_saver_feature.c" ] + local_deps += [ "src/${system_type}:screensaver_featrue_impl" ] + } +} + +lite_library("powermgrservice") { + target_type = lite_library_type + sources = [ - "source/power_manage_feature.cpp", - "source/power_manage_service.cpp", + "src/power_manage_service.c", + "src/running_lock_feature.c", + "src/running_lock_mgr.c", ] + sources += local_sources include_dirs = [ "include", - "//drivers/peripheral/input/interfaces/include", - "//third_party/bounds_checking_function/include", + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_kits_path}", "//utils/native/lite/include", - "//foundation/distributedschedule/samgr_lite/samgr/adapter/", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/communication/broadcast", - "//foundation/distributedschedule/samgr_lite/samgr_endpoint/source", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/registry", - "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", - "//foundation/communication/ipc_lite/interfaces/kits", - "${aafwk_lite_path}/interfaces/kits/want_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite/include", - "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite", - "${aafwk_lite_path}/frameworks/ability_lite/include", - "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", - "${aafwk_lite_path}/interfaces/kits/ability_lite", - "${aafwk_lite_path}/interfaces/kits/want_lite", - "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", - "${appexecfwk_lite_path}/interfaces/innerkits/bundlemgr_lite", - "//foundation/graphic/ui/interfaces/innerkits", - "//foundation/graphic/ui/interfaces/kits", - ] - defines = [ - "ENABLE_WINDOW=1", - "ABILITY_WINDOW_SUPPORT", - "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", ] + include_dirs += local_include_dirs deps = [ - "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite", - "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", - "//foundation/communication/ipc_lite:liteipc_adapter", + "src/${system_type}:runninglock_featrue_impl", "//foundation/distributedschedule/samgr_lite/samgr:samgr", - "//foundation/graphic/wms:lite_wms", - "//third_party/bounds_checking_function:libsec_shared", ] -} - -lite_component("power_manage_service") { - features = [ ":pmslite" ] + deps += local_deps } diff --git a/services/include/power_manage_feature.h b/services/include/power_manage_feature.h deleted file mode 100755 index 48e505f..0000000 --- a/services/include/power_manage_feature.h +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2021 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 POWER_MANAGE_FEATURE_H -#define POWER_MANAGE_FEATURE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "feature.h" -#include "iunknown.h" -#include "ohos_init.h" -#include "samgr_lite.h" -#include "iproxy_server.h" -#include "iproxy_client.h" -#include "hilog/log.h" -#include "app_info.h" -#include "ability_context.h" -#include "ability_service_interface.h" -#include "input_event_listener_proxy.h" -#include "power_manage_service.h" - -static const char * const PERM_INNER = "power_feature"; - -namespace OHOS::PowerMgr { -typedef int32 (*InvokeFunc)(const void *origin, IpcIo *req); - -enum PowerApi { - LOCK_INVOKE, - UNLOCK_INVOKE, - IS_ALWAYS_ON_INVOKE, - FUNC_SIZE -}; - -class PowerManageFeature : public Feature, public InputEventListenerProxy::RawEventListener { -public: - static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply); - static void AcqurieLock(); - static void ReleaseLock(); - static int32 IsAlwaysOn(); - - ~PowerManageFeature() = default; - static const char *GetFeatureName(Feature *feature); - static void OnFeatureInitialize(Feature *feature, Service *parent, Identity identity); - static void OnFeatureStop(Feature *feature, Identity identity); - static BOOL OnFeatureMessage(Feature *feature, Request *request); - - class UserActivity : public InputEventListenerProxy::RawEventListener { - public: - void OnRawEvent(const RawEvent& event)override; - static UserActivity* GetInstance() - { - static UserActivity activity; - return &activity; - } - }; - -private: - static int32 LockInvoke(const void *origin, IpcIo *req); - static int32 UnLockInvoke(const void *origin, IpcIo *req); - static int32 IsAlwaysOnInvoke(const void *origin, IpcIo *req); - static InvokeFunc invokeFuncList[FUNC_SIZE]; - PowerManageFeature(); -}; - -struct PmsInterface { - INHERIT_SERVER_IPROXY; - void (*Lock)(); - void (*UnLock)(); - int32 (*IsStayOn)(); -}; - -typedef struct { - INHERIT_FEATURE; - INHERIT_IUNKNOWNENTRY(PmsInterface); - Identity identity_; -} PowerMgrFeatureImpl; -} -#endif diff --git a/services/include/running_lock_feature.h b/services/include/running_lock_feature.h new file mode 100644 index 0000000..c2b3a25 --- /dev/null +++ b/services/include/running_lock_feature.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_FEATURE_H +#define POWERMGR_RUNNING_LOCK_FEATURE_H + +#include + +#include "running_lock_interface.h" +#include "running_lock_mgr.h" + +typedef struct { + INHERIT_FEATURE; + INHERIT_IUNKNOWNENTRY(RunningLockInterface); + Identity identity; +} RunningLockFeature; + +const char *GetRunningLockFeatureName(Feature *feature); +void OnRunningLockFeatureInitialize(Feature *feature, Service *parent, Identity identity); +void OnRunningLockFeatureStop(Feature *feature, Identity identity); +BOOL OnRunningLockFeatureMessage(Feature *feature, Request *request); +int32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs); +int32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry); + +#define RUNNING_LOCK_FEATURE_INTERFACE_IMPL \ + .GetName = GetRunningLockFeatureName, \ + .OnInitialize = OnRunningLockFeatureInitialize, \ + .OnStop = OnRunningLockFeatureStop, \ + .OnMessage = OnRunningLockFeatureMessage + +#define RUNNING_LOCK_INTERFACE_IMPL \ + .AcquireRunningLockEntryFunc = OnAcquireRunningLockEntry, \ + .ReleaseRunningLockEntryFunc = OnReleaseRunningLockEntry + +RunningLockFeature *GetRunningLockFeatureImpl(void); + +#endif // POWERMGR_RUNNING_LOCK_FEATURE_H diff --git a/services/include/running_lock_handler.h b/services/include/running_lock_handler.h new file mode 100644 index 0000000..714b0b9 --- /dev/null +++ b/services/include/running_lock_handler.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_HANDLER_H +#define POWERMGR_RUNNING_LOCK_HANDLER_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void RunningLockHandlerLock(const char *name); +void RunningLockHandlerUnlock(const char *name); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_HANDLER_H diff --git a/services/include/running_lock_mgr.h b/services/include/running_lock_mgr.h new file mode 100644 index 0000000..c26d567 --- /dev/null +++ b/services/include/running_lock_mgr.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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 POWERMGR_RUNNING_LOCK_MGR_H +#define POWERMGR_RUNNING_LOCK_MGR_H + +#include "running_lock_entry.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void RunningLockMgrInit(void); +int32_t RunningLockMgrAcquireEntry(RunningLockEntry *entry, int32_t timeoutMs); +int32_t RunningLockMgrReleaseEntry(RunningLockEntry *entry); +int32_t RunningLockMgrGetLockCount(RunningLockType type); + +static inline BOOL RunningLockMgrIsLockHolding(RunningLockType type) +{ + return (RunningLockMgrGetLockCount(type) != 0) ? TRUE : FALSE; +} + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_RUNNING_LOCK_MGR_H diff --git a/services/include/screen_saver_feature.h b/services/include/screen_saver_feature.h new file mode 100644 index 0000000..d3fe2c1 --- /dev/null +++ b/services/include/screen_saver_feature.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_FEATURE_H +#define POWERMGR_SCREEN_SAVER_FEATURE_H + +#include + +#include "screen_saver_interface.h" +#include "screen_saver_mgr.h" + +typedef struct { + INHERIT_FEATURE; + INHERIT_IUNKNOWNENTRY(ScreenSaverInterface); + Identity identity; +} ScreenSaverFeature; + +const char *GetScreenSaverFeatureName(Feature *feature); +void OnScreenSaverFeatureInitialize(Feature *feature, Service *parent, Identity identity); +void OnScreenSaverFeatureStop(Feature *feature, Identity identity); +BOOL OnScreenSaverFeatureMessage(Feature *feature, Request *request); +int32_t OnSetScreenSaverState(IUnknown *iUnknown, BOOL enable); + +#define SCREEN_SAVER_FEATURE_INTERFACE_IMPL \ + .GetName = GetScreenSaverFeatureName, \ + .OnInitialize = OnScreenSaverFeatureInitialize, \ + .OnStop = OnScreenSaverFeatureStop, \ + .OnMessage = OnScreenSaverFeatureMessage + +#define SCREEN_SAVER_INTERFACE_IMPL \ + .SetScreenSaverStateFunc = OnSetScreenSaverState + +ScreenSaverFeature *GetScreenSaverFeatureImpl(void); + +#endif // POWERMGR_SCREEN_SAVER_FEATURE_H diff --git a/services/include/screen_saver_mgr.h b/services/include/screen_saver_mgr.h new file mode 100644 index 0000000..51d503f --- /dev/null +++ b/services/include/screen_saver_mgr.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SCREEN_SAVER_MGR_H +#define POWERMGR_SCREEN_SAVER_MGR_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void ScreenSaverMgrInit(void); +int32_t ScreenSaverMgrSetState(BOOL enable); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_SCREEN_SAVER_MGR_H diff --git a/services/include/power_manage_service.h b/services/include/small/screen_saver_handler.h similarity index 38% rename from services/include/power_manage_service.h rename to services/include/small/screen_saver_handler.h index 529f391..ccb8a9a 100755 --- a/services/include/power_manage_service.h +++ b/services/include/small/screen_saver_handler.h @@ -13,38 +13,45 @@ * limitations under the License. */ -#ifndef POWER_MANAGE_SERVICE_H -#define POWER_MANAGE_SERVICE_H +#ifndef POWERMGR_SCREEN_SAVER_HANDLER_H +#define POWERMGR_SCREEN_SAVER_HANDLER_H -#include "service.h" -#include "ohos_init.h" -#include "samgr_lite.h" -#include "hilog/log.h" +#include -static const char * const POWER_MANAGE_SERVICE = "power_service"; +#include +#include -namespace OHOS::PowerMgr { -class PowerMgrService : public Service { +#include "power_mgr_time_util.h" +#include "power_mgr_timer_util.h" + +namespace OHOS { +class ScreenSaverHandler : public InputEventListenerProxy::RawEventListener { public: - static PowerMgrService *GetInstance() - { - static PowerMgrService instance; - return &instance; - } - PowerMgrService(const PowerMgrService&) = delete; - PowerMgrService& operator=(const PowerMgrService&) = delete; - PowerMgrService(PowerMgrService &&) = delete; - PowerMgrService& operator=(PowerMgrService &&) = delete; - ~PowerMgrService() = default; -private: - PowerMgrService(); - static const char *GetServiceName(Service *service); - static BOOL ServiceInitialize(Service *service, Identity identity); - static TaskConfig GetServiceTaskConfig(Service *service); - static BOOL ServiceMessageHandle(Service *service, Request *request); + ScreenSaverHandler() = default; + ~ScreenSaverHandler() override; + + void Init(); + void SetInterval(int64_t intervalMsec); + int32_t SetState(bool enable); + void OnRawEvent(const RawEvent &event) override; private: - Identity identity_; + friend void PowerTimerCallback(void *data); + + static AmsInterface *GetAmsInterface(); + + bool SetEnableLocked(); + bool SetDisableLocked(); + void StartScreenSaver(); + bool StartScreenSaverLocked(); + + static constexpr int64_t DEFAULT_INTERVAL_MSECS = 5 * MSEC_PER_MIN; + + std::mutex mutex_; + bool enabled_{false}; + bool screenSaverStarted_{false}; + int64_t intervalMsec_{DEFAULT_INTERVAL_MSECS}; + PowerTimer *timer_{nullptr}; }; } // namespace OHOS -#endif +#endif // POWERMGR_SCREEN_SAVER_HANDLER_H diff --git a/services/source/power_manage_feature.cpp b/services/source/power_manage_feature.cpp deleted file mode 100755 index 0d6c3e1..0000000 --- a/services/source/power_manage_feature.cpp +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2021 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 "power_manage_feature.h" - -using namespace OHOS; -using namespace OHOS::PowerMgr; -using namespace OHOS::HiviewDFX; - -namespace { -static const int SLEEP_OUT_TIME_SEC = 300; -static const int REGISTER_WAIT_TIME_SEC = 20; -static const int POWER_ERROR = -1; -static const int PM_INVOKE_SUCCESS = 1; -} - -static int32 g_lockNum = 0; -static bool g_isSleep = false; -static int g_count = 0; -static int g_targetTime = SLEEP_OUT_TIME_SEC; -static pthread_mutex_t g_numMutex = PTHREAD_MUTEX_INITIALIZER; -static pthread_t g_powerThread = -1; - -static void *SleepListen(void *args); -static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "PowerMgrService"}; - -PowerManageFeature::PowerManageFeature() : Feature() {} - -void PowerManageFeature::AcqurieLock() -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "AcqurieLock."); - g_lockNum++; - pthread_mutex_unlock(&g_numMutex); -} - -void PowerManageFeature::ReleaseLock() -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "ReleaseLock."); - g_lockNum--; - g_count = 0; - pthread_mutex_unlock(&g_numMutex); -} - -int32 PowerManageFeature::IsAlwaysOn() -{ - HiLog::Debug(LABEL, "IsAlwaysOn."); - return g_lockNum; -} - -void PowerManageFeature::UserActivity::OnRawEvent(const RawEvent& event) -{ - pthread_mutex_lock(&g_numMutex); - HiLog::Debug(LABEL, "User touch the screen, refresh time now."); - g_count = 0; - if (g_isSleep) { - g_isSleep = false; - } - pthread_mutex_unlock(&g_numMutex); -} - -PowerMgrFeatureImpl g_pmsImpl = { - GetName : PowerManageFeature::GetFeatureName, - OnInitialize : PowerManageFeature::OnFeatureInitialize, - OnStop : PowerManageFeature::OnFeatureStop, - OnMessage : PowerManageFeature::OnFeatureMessage, - SERVER_IPROXY_IMPL_BEGIN, - Invoke : PowerManageFeature::Invoke, - Lock : PowerManageFeature::AcqurieLock, - UnLock : PowerManageFeature::ReleaseLock, - IsStayOn : PowerManageFeature::IsAlwaysOn, - IPROXY_END, - identity_ : { -1, -1, NULL }, -}; - -InvokeFunc PowerManageFeature::invokeFuncList[FUNC_SIZE] { - PowerManageFeature::LockInvoke, - PowerManageFeature::UnLockInvoke, - PowerManageFeature::IsAlwaysOnInvoke, -}; - -int32 PowerManageFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply) -{ - HiLog::Debug(LABEL, "IPC Invoke called."); - if (req == nullptr) { - return POWER_ERROR; - } - if (funcId >= 0 && funcId < FUNC_SIZE) { - if (funcId == IS_ALWAYS_ON_INVOKE) { - IpcIoPushInt32(reply, g_lockNum); - } - invokeFuncList[funcId](origin, req); - } - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::LockInvoke(const void *origin, IpcIo *req) -{ - PowerManageFeature::AcqurieLock(); - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::UnLockInvoke(const void *origin, IpcIo *req) -{ - PowerManageFeature::ReleaseLock(); - return PM_INVOKE_SUCCESS; -} - -int32 PowerManageFeature::IsAlwaysOnInvoke(const void *origin, IpcIo *req) -{ - return PowerManageFeature::IsAlwaysOn(); -} - -const char *PowerManageFeature::GetFeatureName(Feature *feature) -{ - return PERM_INNER; -} - -void PowerManageFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity) -{ - g_pmsImpl.identity_ = identity; - HiLog::Debug(LABEL, "OnInitialize called."); - pthread_create(&g_powerThread, nullptr, SleepListen, nullptr); - g_lockNum = 0; - g_isSleep = false; - g_count = 0; - g_targetTime = SLEEP_OUT_TIME_SEC; -} - -static bool GetAmsInterface(struct AmsInterface **amsInterface) -{ - IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE); - if (iUnknown == NULL) { - HiLog::Debug(LABEL, "GetFeatureApi failed."); - return false; - } - - int32_t errCode = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)amsInterface); - if (errCode != EC_SUCCESS) { - HiLog::Debug(LABEL, "QueryInterface failed."); - return false; - } - return true; -} - -static void *SleepListen(void *args) -{ - sleep(REGISTER_WAIT_TIME_SEC); - HiLog::Debug(LABEL, "Register ims touch event now."); - InputEventListenerProxy::GetInstance()->RegisterInputEventListener(PowerManageFeature::UserActivity::GetInstance()); - while (true) { - pthread_mutex_lock(&g_numMutex); - if (g_lockNum == 0 && g_count == g_targetTime) { - HiLog::Debug(LABEL, "Five minutes passed, It is time to call screen saver."); - Want want = { nullptr }; - ElementName element = { nullptr }; - SetElementBundleName(&element, "com.huawei.screensaver"); - SetElementAbilityName(&element, "ScreensaverAbility"); - SetWantElement(&want, element); - SetWantData(&want, "WantData", strlen("WantData") + 1); - struct AmsInterface *amsInterface = nullptr; - if (!GetAmsInterface(&amsInterface)) { - ClearWant(&want); - } - amsInterface->StartAbility(&want); - ClearElement(&element); - ClearWant(&want); - } - pthread_mutex_unlock(&g_numMutex); - g_count++; - sleep(1); - } -} - -void PowerManageFeature::OnFeatureStop(Feature *feature, Identity identity) -{ - (void)feature; - (void)identity; -} - -BOOL PowerManageFeature::OnFeatureMessage(Feature *feature, Request *request) -{ - return ((feature != nullptr) && (request != nullptr)); -} - -static void GInit() -{ - HiLog::Debug(LABEL, "SYS_FEATURE_INIT called."); - BOOL result = SAMGR_GetInstance()->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)&g_pmsImpl); - if (result == FALSE) { - HiLog::Debug(LABEL, "RegisterFeature failed."); - return; - } - BOOL apiResult = SAMGR_GetInstance()->RegisterFeatureApi(POWER_MANAGE_SERVICE, PERM_INNER, GET_IUNKNOWN(g_pmsImpl)); - if (apiResult == FALSE) { - HiLog::Debug(LABEL, "RegisterFeatureApi failed."); - return; - } -} -SYSEX_FEATURE_INIT(GInit); diff --git a/services/src/mini/BUILD.gn b/services/src/mini/BUILD.gn new file mode 100644 index 0000000..3d0037d --- /dev/null +++ b/services/src/mini/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +static_library("runninglock_featrue_impl") { + sources = [ + "running_lock_feature_impl.c", + "running_lock_handler.c", + ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_kits_path}", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//kernel/liteos_m/kernel/include", + "//kernel/liteos_m/utils", + ] + + deps = [ + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//kernel/liteos_m/kernel:kernel", + ] +} diff --git a/services/src/mini/running_lock_feature_impl.c b/services/src/mini/running_lock_feature_impl.c new file mode 100644 index 0000000..ba95cf3 --- /dev/null +++ b/services/src/mini/running_lock_feature_impl.c @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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 + +#include "running_lock_feature.h" + +static RunningLockFeature g_feature = { + RUNNING_LOCK_FEATURE_INTERFACE_IMPL, + DEFAULT_IUNKNOWN_ENTRY_BEGIN, + RUNNING_LOCK_INTERFACE_IMPL, + DEFAULT_IUNKNOWN_ENTRY_END, + .identity = { -1, -1, NULL }, +}; + +RunningLockFeature *GetRunningLockFeatureImpl(void) +{ + return &g_feature; +} diff --git a/services/src/mini/running_lock_handler.c b/services/src/mini/running_lock_handler.c new file mode 100644 index 0000000..60d81d2 --- /dev/null +++ b/services/src/mini/running_lock_handler.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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 + +#include + +#include "hilog_wrapper.h" + +void RunningLockHandlerLock(const char *name) +{ + uint32_t ret = LOS_PmLockRequest(name); + POWER_HILOGD("Lock request, name: %s, ret: %u", name, ret); +} + +void RunningLockHandlerUnlock(const char *name) +{ + uint32_t ret = LOS_PmLockRelease(name); + POWER_HILOGD("Lock release, name: %s, ret: %u", name, ret); +} diff --git a/services/source/power_manage_service.cpp b/services/src/power_manage_service.c old mode 100755 new mode 100644 similarity index 33% rename from services/source/power_manage_service.cpp rename to services/src/power_manage_service.c index 142ca0e..56c26c2 --- a/services/source/power_manage_service.cpp +++ b/services/src/power_manage_service.c @@ -13,56 +13,70 @@ * limitations under the License. */ -#include "power_manage_service.h" +#include +#include +#include -using namespace OHOS; -using namespace OHOS::PowerMgr; +#include +#include -namespace { -static const int STACK_SIZE = 0x800; -static const int QUEUE_SIZE = 20; -} +#include "hilog_wrapper.h" +#include "power_mgr.h" -PowerMgrService::PowerMgrService() : Service(), identity_() -{ - this->Service::GetName = PowerMgrService::GetServiceName; - this->Service::Initialize = PowerMgrService::ServiceInitialize; - this->Service::MessageHandle = PowerMgrService::ServiceMessageHandle; - this->Service::GetTaskConfig = PowerMgrService::GetServiceTaskConfig; -} +#define STACK_SIZE 0x800 +#define QUEUE_SIZE 20 -static void GInit() -{ - SamgrLite *sm = SAMGR_GetInstance(); - if (sm == nullptr) { - return; - } - sm->RegisterService(PowerMgrService::GetInstance()); -} -SYSEX_SERVICE_INIT(GInit); +typedef struct { + INHERIT_SERVICE; + Identity identity; +} PowerMgrService; -const char *PowerMgrService::GetServiceName(__attribute__((unused))Service *service) +static const char *GetName(Service *service); +static BOOL Initialize(Service *service, Identity identity); +static BOOL MessageHandle(Service *service, Request *request); +static TaskConfig GetTaskConfig(Service *service); + +static PowerMgrService g_service = { + .GetName = GetName, + .Initialize = Initialize, + .MessageHandle = MessageHandle, + .GetTaskConfig = GetTaskConfig, + .identity = { -1, -1, NULL } +}; + +static const char *GetName(Service *service) { + (void)service; return POWER_MANAGE_SERVICE; } -BOOL PowerMgrService::ServiceInitialize(Service *service, Identity identity) +static BOOL Initialize(Service *service, Identity identity) { - if (service == nullptr) { + if (service == NULL) { + POWER_HILOGE("Invalid service"); return FALSE; } - PowerMgrService *powerService = static_cast(service); - powerService->identity_ = identity; + PowerMgrService *pms = (PowerMgrService *)(service); + pms->identity = identity; return TRUE; } -BOOL PowerMgrService::ServiceMessageHandle(Service *service, Request *request) +static BOOL MessageHandle(Service *service, Request *request) { - return (request != nullptr); + (void)service; + return request != NULL; } -TaskConfig PowerMgrService::GetServiceTaskConfig(Service *service) +static TaskConfig GetTaskConfig(Service *service) { - TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK}; + (void)service; + TaskConfig config = { LEVEL_HIGH, PRI_NORMAL, STACK_SIZE, QUEUE_SIZE, SINGLE_TASK }; return config; } + +static void Init(void) +{ + SAMGR_GetInstance()->RegisterService((Service *)&g_service); + POWER_HILOGI("Succeed to init power manager service"); +} +SYS_SERVICE_INIT(Init); diff --git a/services/src/running_lock_feature.c b/services/src/running_lock_feature.c new file mode 100644 index 0000000..63dd7ba --- /dev/null +++ b/services/src/running_lock_feature.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 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 "running_lock_feature.h" + +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "running_lock_inner.h" +#include "running_lock_mgr.h" + +const char *GetRunningLockFeatureName(Feature *feature) +{ + (void)feature; + return POWER_RUNNING_LOCK_FEATURE; +} + +void OnRunningLockFeatureInitialize(Feature *feature, Service *parent, Identity identity) +{ + if (feature == NULL) { + POWER_HILOGE("Invalid feature"); + return; + } + + RunningLockFeature *f = (RunningLockFeature *)feature; + f->identity = identity; + POWER_HILOGI("Init running lock feature done"); +} + +void OnRunningLockFeatureStop(Feature *feature, Identity identity) +{ + (void)feature; + (void)identity; + RunningLockFeature *f = GetRunningLockFeatureImpl(); + if (f != NULL) { + f->identity.queueId = NULL; + f->identity.featureId = -1; + f->identity.serviceId = -1; + } +} + +BOOL OnRunningLockFeatureMessage(Feature *feature, Request *request) +{ + return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE; +} + +int32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs) +{ + (void)iUnknown; + return RunningLockMgrAcquireEntry(entry, timeoutMs); +} + +int32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry) +{ + (void)iUnknown; + return RunningLockMgrReleaseEntry(entry); +} + +static void Init(void) +{ + SamgrLite *sam = SAMGR_GetInstance(); + if (sam == NULL) { + POWER_HILOGE("Failed to get samgr"); + return; + } + + RunningLockFeature *feature = GetRunningLockFeatureImpl(); + if (feature == NULL) { + POWER_HILOGE("Failed to get running lock feature"); + return; + } + BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature); + if (result == FALSE) { + POWER_HILOGE("Failed to register running lock feature"); + return; + } + result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_RUNNING_LOCK_FEATURE, GET_IUNKNOWN(*feature)); + if (result == FALSE) { + POWER_HILOGE("Failed to register running lock feature api"); + return; + } + RunningLockMgrInit(); + POWER_HILOGI("Succeed to register running lock feature"); +} +SYS_FEATURE_INIT(Init); diff --git a/services/src/running_lock_mgr.c b/services/src/running_lock_mgr.c new file mode 100644 index 0000000..7ea99e5 --- /dev/null +++ b/services/src/running_lock_mgr.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2021 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 + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_entry.h" +#include "running_lock_handler.h" + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static Vector g_runningLocks[RUNNINGLOCK_BUTT]; +static const char * const g_runningLockNames[RUNNINGLOCK_BUTT] = { + "OHOSPowerMgr.Screen", + "OHOSPowerMgr.Background", + "OHOSPowerMgr.Proximity", +}; + +static RunningLockEntry *DupRunningLockEntry(RunningLockEntry *entry) +{ + RunningLockEntry *e = (RunningLockEntry *)malloc(sizeof(RunningLockEntry)); + if (e == NULL) { + POWER_HILOGE("Failed allocate running lock entry"); + return NULL; + } + if (memcpy_s(e, sizeof(RunningLockEntry), entry, sizeof(RunningLockEntry)) != EOK) { + POWER_HILOGE("Failed copy running lock entry"); + free(e); + return NULL; + } + return e; +} + +static BOOL AddRunningLockEntryLocked(Vector *vec, RunningLockEntry *entry) +{ + int16_t pos = VECTOR_FindByKey(vec, (void *)&entry->identity); + if (pos >= 0) { + POWER_HILOGI("Already acquired: %s", entry->lock.name); + return TRUE; + } + RunningLockEntry *e = DupRunningLockEntry(entry); + if (e == NULL) { + POWER_HILOGE("Failed duplicate running lock entry"); + return FALSE; + } + pos = VECTOR_Add(vec, (void *)e); + if (pos == INVALID_INDEX) { + POWER_HILOGE("Failed to add entry to vector for lock: %s", e->lock.name); + return FALSE; + } + POWER_HILOGD("Add running lock entry, name: %s, type: %d", e->lock.name, e->lock.type); + if (VECTOR_Num(vec) == 1) { + RunningLockHandlerLock(g_runningLockNames[entry->lock.type]); + } + return TRUE; +} + +static BOOL RemoveRunningLockEntryLocked(Vector *vec, RunningLockEntry *entry) +{ + int16_t pos = VECTOR_FindByKey(vec, (void *)&entry->identity); + if (pos < 0) { + POWER_HILOGE("Non-existent running lock: %s", entry->lock.name); + return TRUE; + } + VECTOR_Swap(vec, pos, NULL); + RunningLockEntry *e = (RunningLockEntry *)VECTOR_At(vec, pos); + free(e); + POWER_HILOGD("Remove running lock entry, name: %s, type: %d", entry->lock.name, entry->lock.type); + if (VECTOR_Num(vec) == 0) { + RunningLockHandlerUnlock(g_runningLockNames[entry->lock.type]); + } + return TRUE; +} + +static void ShowLocks() +{ +#ifdef OHOS_DEBUG + for (int t = RUNNINGLOCK_SCREEN; t < RUNNINGLOCK_BUTT; t++) { + Vector *vec = &g_runningLocks[t]; + int size = VECTOR_Size(vec); + POWER_HILOGD("type: %d, lock num: %d", t, VECTOR_Num(vec)); + for (int i = 0; i < size; i++) { + RunningLockEntry* e = VECTOR_At(vec, i); + if (e == NULL) continue; + POWER_HILOGD("No.%d, name: %s, pid: %u, token: %llu", i, e->lock.name, e->identity.pid, (long long)e->identity.token); + } + } +#endif +} + +int32_t RunningLockMgrAcquireEntry(RunningLockEntry *entry, int32_t timeoutMs) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + pthread_mutex_lock(&g_mutex); + Vector *vec = &g_runningLocks[entry->lock.type]; + BOOL ret = AddRunningLockEntryLocked(vec, entry); + ShowLocks(); + pthread_mutex_unlock(&g_mutex); + return (ret == TRUE) ? EC_SUCCESS : EC_FAILURE; +} + +int32_t RunningLockMgrReleaseEntry(RunningLockEntry *entry) +{ + if (IsValidRunningLockEntry(entry) == FALSE) { + POWER_HILOGE("Invalid running lock entry"); + return EC_INVALID; + } + + pthread_mutex_lock(&g_mutex); + Vector *vec = &g_runningLocks[entry->lock.type]; + BOOL ret = RemoveRunningLockEntryLocked(vec, entry); + ShowLocks(); + pthread_mutex_unlock(&g_mutex); + return (ret == TRUE) ? EC_SUCCESS : EC_FAILURE; +} + +int32_t RunningLockMgrGetLockCount(RunningLockType type) +{ + int32_t cnt = -1; + if ((type >= 0) && (type < RUNNINGLOCK_BUTT)) { + pthread_mutex_lock(&g_mutex); + cnt = VECTOR_Num(&g_runningLocks[type]); + pthread_mutex_unlock(&g_mutex); + } + return cnt; +} + +static RunningLockIdentity *GetRunningLockIdentity(const RunningLockEntry *entry) +{ + return &entry->identity; +} + +static int32_t RunningLockIdentityCmp(const RunningLockIdentity *a, const RunningLockIdentity *b) +{ + return (IsSameRunningLockIdentity(a, b) == TRUE) ? 0 : -1; +} + +void RunningLockMgrInit(void) +{ + for (int32_t i = 0; i < RUNNINGLOCK_BUTT; i++) { + g_runningLocks[i] = VECTOR_Make((VECTOR_Key)GetRunningLockIdentity, (VECTOR_Compare)RunningLockIdentityCmp); + } +} diff --git a/services/src/screen_saver_feature.c b/services/src/screen_saver_feature.c new file mode 100644 index 0000000..7bcf58e --- /dev/null +++ b/services/src/screen_saver_feature.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021 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 "screen_saver_feature.h" + +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr.h" +#include "screen_saver_intf_define.h" +#include "screen_saver_mgr.h" + +const char *GetScreenSaverFeatureName(Feature *feature) +{ + (void)feature; + return POWER_SCREEN_SAVER_FEATURE; +} + +void OnScreenSaverFeatureInitialize(Feature *feature, Service *parent, Identity identity) +{ + if (feature == NULL) { + POWER_HILOGE("Invalid feature"); + return; + } + + ScreenSaverFeature *f = (ScreenSaverFeature *)feature; + f->identity = identity; + ScreenSaverMgrInit(); + POWER_HILOGI("Init screen saver feature done"); +} + +void OnScreenSaverFeatureStop(Feature *feature, Identity identity) +{ + (void)feature; + (void)identity; + ScreenSaverFeature *f = GetScreenSaverFeatureImpl(); + if (f != NULL) { + f->identity.queueId = NULL; + f->identity.featureId = -1; + f->identity.serviceId = -1; + } +} + +BOOL OnScreenSaverFeatureMessage(Feature *feature, Request *request) +{ + return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE; +} + +int32_t OnSetScreenSaverState(IUnknown *iUnknown, BOOL enable) +{ + (void)iUnknown; + return ScreenSaverMgrSetState(enable); +} + +static void Init(void) +{ + SamgrLite *sam = SAMGR_GetInstance(); + if (sam == NULL) { + POWER_HILOGE("Failed to get samgr"); + return; + } + + ScreenSaverFeature *feature = GetScreenSaverFeatureImpl(); + if (feature == NULL) { + POWER_HILOGE("Failed to get screen saver feature"); + return; + } + BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature); + if (result == FALSE) { + POWER_HILOGE("Failed to register screen saver feature"); + return; + } + result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE, GET_IUNKNOWN(*feature)); + if (result == FALSE) { + POWER_HILOGE("Failed to register screen saver feature api"); + return; + } + POWER_HILOGI("Succeed to register screen saver feature"); +} +SYS_FEATURE_INIT(Init); diff --git a/services/src/small/BUILD.gn b/services/src/small/BUILD.gn new file mode 100644 index 0000000..e4647ff --- /dev/null +++ b/services/src/small/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +source_set("runninglock_featrue_impl") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + sources = [ + "running_lock_feature_impl.c", + "running_lock_handler.c", + ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_kits_path}", + ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] +} + +source_set("screensaver_featrue_impl") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] + + sources = [ + "screen_saver_feature_impl.c", + "screen_saver_handler.cpp", + "screen_saver_mgr.cpp", + ] + + include_dirs = [ + "${powermgr_kits_path}", + "${powermgr_frameworks_path}/include", + "${powermgr_frameworks_path}/include/${system_type}", + "${powermgr_services_path}/include", + "${powermgr_services_path}/include/${system_type}", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite", + "${aafwk_lite_path}/interfaces/kits/want_lite", + "${appexecfwk_lite_path}/interfaces/kits/bundle_lite", + ] + + deps = [ + "${aafwk_lite_path}/frameworks/abilitymgr_lite:abilitymanager", + "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle", + "${powermgr_utils_path}:powermgr_utils", + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//foundation/graphic/wms:lite_wms", + ] +} diff --git a/services/src/small/running_lock_feature_impl.c b/services/src/small/running_lock_feature_impl.c new file mode 100644 index 0000000..5d5bcc2 --- /dev/null +++ b/services/src/small/running_lock_feature_impl.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_feature.h" + +typedef int32_t (*InvokeFunc)(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply); +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply); + +static RunningLockFeature g_feature = { + RUNNING_LOCK_FEATURE_INTERFACE_IMPL, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = FeatureInvoke, + RUNNING_LOCK_INTERFACE_IMPL, + IPROXY_END, + .identity = { -1, -1, NULL }, +}; + +static InvokeFunc g_invokeFuncs[RUNNINGLOCK_FUNCID_BUTT] = { + AcquireInvoke, + ReleaseInvoke, +}; + +RunningLockFeature *GetRunningLockFeatureImpl(void) +{ + return &g_feature; +} + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + if ((iProxy == NULL) || (origin == NULL) || (req == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + POWER_HILOGD("Running lock feature invoke function id: %d", funcId); + return (funcId >= 0 && funcId < RUNNINGLOCK_FUNCID_BUTT) ? g_invokeFuncs[funcId](iProxy, origin, req, reply) : + EC_FAILURE; +} + +static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + size_t len = 0; + void *data = (void *)IpcIoPopFlatObj(req, &len); + int32_t timeoutMs = IpcIoPopInt32(req); + int32_t ret = OnAcquireRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data, timeoutMs); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} + +static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + size_t len = 0; + void *data = (void *)IpcIoPopFlatObj(req, &len); + int32_t ret = OnReleaseRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} diff --git a/services/src/small/running_lock_handler.c b/services/src/small/running_lock_handler.c new file mode 100644 index 0000000..ce2be50 --- /dev/null +++ b/services/src/small/running_lock_handler.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 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 "hilog_wrapper.h" + +void RunningLockHandlerLock(const char *name) +{ + POWER_HILOGD("To be locked in kernel, name: %s", name); +} + +void RunningLockHandlerUnlock(const char *name) +{ + POWER_HILOGD("To be unlocked in kernel, name: %s", name); +} diff --git a/services/src/small/screen_saver_feature_impl.c b/services/src/small/screen_saver_feature_impl.c new file mode 100644 index 0000000..8b5b856 --- /dev/null +++ b/services/src/small/screen_saver_feature_impl.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 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 +#include + +#include +#include + +#include "hilog_wrapper.h" +#include "screen_saver_feature.h" + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply); + +static ScreenSaverFeature g_feature = { + SCREEN_SAVER_FEATURE_INTERFACE_IMPL, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = FeatureInvoke, + SCREEN_SAVER_INTERFACE_IMPL, + IPROXY_END, + .identity = { -1, -1, NULL }, +}; + +ScreenSaverFeature *GetScreenSaverFeatureImpl(void) +{ + return &g_feature; +} + +static int32_t SetStateInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply) +{ + if (reply == NULL) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + bool enable = IpcIoPopBool(req); + int32_t ret = OnSetScreenSaverState((IUnknown *)iProxy, enable ? TRUE : FALSE); + IpcIoPushInt32(reply, ret); + return EC_SUCCESS; +} + +static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + if ((iProxy == NULL) || (origin == NULL) || (req == NULL)) { + POWER_HILOGE("Invalid parameter"); + return EC_INVALID; + } + POWER_HILOGD("Screen saver feature invoke function id: %d", funcId); + int32_t ret = EC_SUCCESS; + switch (funcId) { + case SCREENSAVER_FUNCID_SETSTATE: + ret = SetStateInvoke(iProxy, origin, req, reply); + break; + default: + POWER_HILOGE("Invalid function id: %d", funcId); + break; + } + return ret; +} diff --git a/services/src/small/screen_saver_handler.cpp b/services/src/small/screen_saver_handler.cpp new file mode 100644 index 0000000..7f86dd0 --- /dev/null +++ b/services/src/small/screen_saver_handler.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2021 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 "screen_saver_handler.h" + +#include + +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "running_lock_mgr.h" + +namespace OHOS { +ScreenSaverHandler::~ScreenSaverHandler() +{ + if (timer_ != nullptr) { + PowerMgrDestroyTimer(timer_); + timer_ = nullptr; + } +} + +AmsInterface *ScreenSaverHandler::GetAmsInterface() +{ + static struct AmsInterface *ams = nullptr; + if (ams != nullptr) { + return ams; + } + + IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE); + if (iUnknown == nullptr) { + POWER_HILOGE("Failed to get ams iUnknown"); + return nullptr; + } + + int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&ams); + if ((ret != EC_SUCCESS) || (ams == nullptr)) { + POWER_HILOGE("Failed to query ams interface"); + return nullptr; + } + POWER_HILOGI("Succeed to get ams interface"); + return ams; +} + +bool ScreenSaverHandler::StartScreenSaverLocked() +{ + AmsInterface *ams = GetAmsInterface(); + if (ams == nullptr) { + POWER_HILOGE("Failed to get ams interface"); + return false; + } + + Want want = { nullptr }; + ElementName element = { nullptr }; + SetElementBundleName(&element, "com.huawei.screensaver"); + SetElementAbilityName(&element, "ScreensaverAbility"); + SetWantElement(&want, element); + SetWantData(&want, "WantData", strlen("WantData") + 1); + int32_t ret = ams->StartAbility(&want); + ClearElement(&element); + ClearWant(&want); + return ret == EC_SUCCESS; +} + +void ScreenSaverHandler::StartScreenSaver() +{ + POWER_HILOGI("Time to start screen saver"); + std::lock_guard lock(mutex_); + if (!enabled_ || screenSaverStarted_ || (RunningLockMgrIsLockHolding(RUNNINGLOCK_SCREEN) == TRUE)) { + return; + } + screenSaverStarted_ = StartScreenSaverLocked(); + POWER_HILOGI("Start screen saver: %d", static_cast(screenSaverStarted_)); +} + +void PowerTimerCallback(void *data) +{ + if (data == nullptr) { + POWER_HILOGE("Invalid timer data"); + return; + } + ScreenSaverHandler *handler = static_cast(data); + handler->StartScreenSaver(); +} + +void ScreenSaverHandler::Init() +{ + if (timer_ == nullptr) { + timer_ = PowerMgrCreateTimer(intervalMsec_, intervalMsec_, PowerTimerCallback); + } + std::thread lateInit([this] { + sleep(20); // set delay to avoid registing input evnet listener failure + SetState(true); + }); + lateInit.detach(); +} + +void ScreenSaverHandler::SetInterval(int64_t intervalMsec) +{ + std::lock_guard lock(mutex_); + if (intervalMsec_ == intervalMsec) { + return; + } + if (timer_ != nullptr) { + PowerMgrResetTimer(timer_, intervalMsec, intervalMsec); + if (enabled_) { + SetEnableLocked(); + } + } + intervalMsec_ = intervalMsec; +} + +bool ScreenSaverHandler::SetEnableLocked() +{ + if (PowerMgrStartTimer(timer_, (void *)this) == FALSE) { + POWER_HILOGE("Failed to start timer"); + return false; + } + if (!InputEventListenerProxy::GetInstance()->RegisterInputEventListener(this)) { + PowerMgrStopTimer(timer_); + POWER_HILOGE("Failed to register input event listener"); + return false; + } + POWER_HILOGI("Succeed to enable screen saver"); + return true; +} + +bool ScreenSaverHandler::SetDisableLocked() +{ + InputEventListenerProxy::GetInstance()->UnregisterInputEventListener(); + PowerMgrStopTimer(timer_); + POWER_HILOGI("Succeed to disable screen saver"); + return true; +} + +int32_t ScreenSaverHandler::SetState(bool enable) +{ + std::lock_guard lock(mutex_); + if (timer_ == nullptr) { + return EC_FAILURE; + } + if (enabled_ == enable) { + return EC_SUCCESS; + } + bool ret = enable ? SetEnableLocked() : SetDisableLocked(); + if (!ret) { + POWER_HILOGE("Failed to set state: %d", static_cast(enable)); + return EC_FAILURE; + } + enabled_ = enable; + return EC_SUCCESS; +} + +void ScreenSaverHandler::OnRawEvent(const RawEvent &event) +{ + static int64_t lastUpdateTime = 0; + std::lock_guard lock(mutex_); + POWER_HILOGI("User activity refreshed, restart the timer"); + screenSaverStarted_ = false; + + int64_t currentTime = GetCurrentTimeMsec(CLOCK_MONOTONIC); + if ((currentTime - lastUpdateTime) > MSEC_PER_SEC) { + PowerMgrRestartTimer(timer_, (void *)this); + lastUpdateTime = currentTime; + } +} +} // namespace OHOS diff --git a/services/src/small/screen_saver_mgr.cpp b/services/src/small/screen_saver_mgr.cpp new file mode 100644 index 0000000..69ddab5 --- /dev/null +++ b/services/src/small/screen_saver_mgr.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 "screen_saver_handler.h" + +static OHOS::ScreenSaverHandler g_handler; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void ScreenSaverMgrInit(void) +{ + g_handler.Init(); +} + +int32_t ScreenSaverMgrSetState(BOOL enable) +{ + return g_handler.SetState(enable == TRUE); +} + +#ifdef __cplusplus +} +#endif // __cplusplus diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100644 index 0000000..5640d21 --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2021 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("//base/powermgr/powermgr_lite/powermgr.gni") +import("//build/lite/config/component/lite_component.gni") + +config("powermgr_utils_public_config") { + include_dirs = [ "include" ] +} + +utils_sources = [ + "src/power_mgr_time_util.c", + "src/power_mgr_timer_util.c", +] + +if (is_mini_system) { + static_library("powermgr_utils") { + sources = utils_sources + + public_configs = [ ":powermgr_utils_public_config" ] + + include_dirs = [ + "${powermgr_frameworks_path}/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + "//kernel/liteos_m/kal/posix/include", + ] + + deps = [ + "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//kernel/liteos_m/kernel:kernel", + ] + } +} else { + source_set("powermgr_utils") { + cflags = [ "-fPIC" ] + cflags_cc = cflags + + sources = utils_sources + + public_configs = [ ":powermgr_utils_public_config" ] + include_dirs = [ "${powermgr_frameworks_path}/include" ] + + deps = [ "//foundation/distributedschedule/samgr_lite/samgr:samgr" ] + } +} diff --git a/utils/include/power_mgr_time_util.h b/utils/include/power_mgr_time_util.h new file mode 100644 index 0000000..0a170b6 --- /dev/null +++ b/utils/include/power_mgr_time_util.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_TIME_UTIL_H +#define POWERMGR_POWER_MGR_TIME_UTIL_H + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +enum { + NSEC_PER_MIN = 60000000000LL, + NSEC_PER_SEC = 1000000000LL, + NSEC_PER_MSEC = 1000000LL, + NSEC_PER_USEC = 1000LL, + USEC_PER_SEC = 1000000LL, + USEC_PER_MSEC = 1000LL, + MSEC_PER_DAY = 86400000LL, + MSEC_PER_HOUR = 3600000LL, + MSEC_PER_MIN = 60000LL, + MSEC_PER_SEC = 1000LL, + SEC_PER_MIN = 60LL, + SEC_PER_HOUR = 3600LL, + SEC_PER_DAY = 86400LL, + MIN_PER_HOUR = 60LL, + HOUR_PER_DAY = 24LL, +}; + +static inline int64_t NsecToUsec(int64_t nsec) +{ + return nsec / NSEC_PER_USEC; +} + +static inline int64_t NsecToMsec(int64_t nsec) +{ + return nsec / NSEC_PER_MSEC; +} + +static inline int64_t UsecToMsec(int64_t usec) +{ + return usec / USEC_PER_MSEC; +} + +static inline int64_t MsecToNsec(int64_t msec) +{ + return msec * NSEC_PER_MSEC; +} + +static inline int64_t MsecToUsec(int64_t msec) +{ + return msec * USEC_PER_MSEC; +} + +static inline int64_t MsecToSec(int64_t msec) +{ + return msec / MSEC_PER_SEC; +} + +static inline int64_t MsecToMin(int64_t msec) +{ + return msec / MSEC_PER_MIN; +} + +static inline int64_t MsecToHour(int64_t msec) +{ + return msec / MSEC_PER_HOUR; +} + +static inline int64_t SecToNsec(int64_t sec) +{ + return sec * NSEC_PER_SEC; +} + +static inline int64_t SecToMsec(int64_t sec) +{ + return sec * MSEC_PER_SEC; +} + +static inline int64_t MinToNsec(int64_t mins) +{ + return mins * NSEC_PER_MIN; +} + +static inline int64_t MinToMsec(int64_t mins) +{ + return mins * MSEC_PER_MIN; +} + +static inline int64_t HourToMsec(int64_t hour) +{ + return hour * MSEC_PER_HOUR; +} + +static inline int64_t DayToMsec(int64_t day) +{ + return day * MSEC_PER_DAY; +} + +int64_t GetCurrentTimeMsec(clockid_t clkId); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_TIME_UTIL_H diff --git a/utils/include/power_mgr_timer_util.h b/utils/include/power_mgr_timer_util.h new file mode 100644 index 0000000..3e99ef7 --- /dev/null +++ b/utils/include/power_mgr_timer_util.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 POWERMGR_POWER_MGR_TIMER_UTIL_H +#define POWERMGR_POWER_MGR_TIMER_UTIL_H + +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef timer_t PowerTimer; +typedef void (*PowerTimerCallback)(void *privateData); + +/* + * Create timer. Must call PowerMgrDestroyTimer to release memory after timer is not needed + */ +PowerTimer *PowerMgrCreateTimer(int64_t whenMsec, int64_t intervalMsec, PowerTimerCallback cb); + +/* + * Reset timer attributes and stop timer + */ +BOOL PowerMgrResetTimer(PowerTimer *timer, int64_t whenMsec, int64_t intervalMsec); + +/* + * Start timer. The privateData will set to PowerTimerCallback function when timer timeout + */ +BOOL PowerMgrStartTimer(PowerTimer *timer, void *privateData); + +/* + * Restart timer. The privateData will set to PowerTimerCallback function when timer timeout + */ +BOOL PowerMgrRestartTimer(PowerTimer *timer, void *privateData); + +/* + * Stop timer + */ +BOOL PowerMgrStopTimer(PowerTimer *timer); + +/* + * Destory timer + */ +void PowerMgrDestroyTimer(PowerTimer *timer); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // POWERMGR_POWER_MGR_TIMER_UTIL_H diff --git a/utils/src/power_mgr_time_util.c b/utils/src/power_mgr_time_util.c new file mode 100644 index 0000000..9ec544d --- /dev/null +++ b/utils/src/power_mgr_time_util.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 "power_mgr_time_util.h" + +int64_t GetCurrentTimeMsec(clockid_t clkId) +{ + struct timespec ts; + clock_gettime(clkId, &ts); + return SecToMsec(ts.tv_sec) + NsecToMsec(ts.tv_nsec); +} diff --git a/utils/src/power_mgr_timer_util.c b/utils/src/power_mgr_timer_util.c new file mode 100644 index 0000000..5f53454 --- /dev/null +++ b/utils/src/power_mgr_timer_util.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2021 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 "power_mgr_timer_util.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "hilog_wrapper.h" +#include "power_mgr_time_util.h" + +typedef struct { + PowerTimer timerId; + BOOL isRunning; + int64_t whenMsec; + int64_t intervalMsec; + PowerTimerCallback timerCb; + void *data; +} PowerTimerInfo; + +static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +static BOOL g_inited = FALSE; +static Vector g_timers; + +static inline PowerTimerInfo *GetPowerTimerInfo(PowerTimer *timer) +{ + return GET_OBJECT(timer, PowerTimerInfo, timerId); +} + +static void SetTimeSpec(struct timespec *ts, int64_t msec) +{ + ts->tv_sec = MsecToSec(msec); + msec -= SecToMsec(ts->tv_sec); + ts->tv_nsec = MsecToNsec(msec); +} + +static BOOL StartTimer(PowerTimer timer, int64_t whenMsec, int64_t intervalMsec) +{ + struct itimerspec ts; + SetTimeSpec(&ts.it_value, whenMsec); + SetTimeSpec(&ts.it_interval, intervalMsec); + int32_t ret = timer_settime(timer, 0, &ts, NULL); + if (ret < 0) { + POWER_HILOGE("Failed to start timer: %s", strerror(errno)); + return FALSE; + } + POWER_HILOGD("Succeed to start timer, id: %p", timer); + return TRUE; +} + +static void TimerHandle(union sigval v) +{ + PowerTimerInfo *info = (PowerTimerInfo *)v.sival_ptr; + if (info == NULL) { + POWER_HILOGE("Invalid timer info"); + return; + } + POWER_HILOGD("Timer trigger, id: %p", info->timerId); + if (info->timerCb != NULL) { + info->timerCb(info->data); + } +} + +PowerTimer *PowerMgrCreateTimer(int64_t whenMsec, int64_t intervalMsec, PowerTimerCallback cb) +{ + PowerTimerInfo *info = (PowerTimerInfo *)malloc(sizeof(PowerTimerInfo)); + if (info == NULL) { + POWER_HILOGE("Failed allocate timer info"); + return NULL; + } + (void)memset_s(info, sizeof(PowerTimerInfo), 0, sizeof(PowerTimerInfo)); + info->isRunning = FALSE; + info->whenMsec = whenMsec; + info->intervalMsec = intervalMsec; + info->timerCb = cb; + + struct sigevent evp; + memset_s (&evp, sizeof(evp), 0, sizeof(evp)); + evp.sigev_value.sival_ptr = info; + evp.sigev_notify = SIGEV_THREAD; + evp.sigev_notify_function = TimerHandle; + int32_t ret = timer_create(CLOCK_REALTIME, &evp, &info->timerId); + if (ret < 0) { + POWER_HILOGE("Failed to create timer: %s", strerror(errno)); + free(info); + return NULL; + } + POWER_HILOGD("Succeed to create timer, id: %p", info->timerId); + + return &info->timerId; +} + +BOOL PowerMgrResetTimer(PowerTimer *timer, int64_t whenMsec, int64_t intervalMsec) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + PowerMgrStopTimer(timer); + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->whenMsec = whenMsec; + info->intervalMsec = intervalMsec; + return TRUE; +} + +BOOL PowerMgrStartTimer(PowerTimer *timer, void *privateData) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->data = privateData; + info->isRunning = TRUE; + return StartTimer(info->timerId, info->whenMsec, info->intervalMsec); +} + +BOOL PowerMgrRestartTimer(PowerTimer *timer, void *privateData) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + return PowerMgrStartTimer(timer, privateData); +} + +BOOL PowerMgrStopTimer(PowerTimer *timer) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return FALSE; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + info->isRunning = FALSE; + return StartTimer(info->timerId, 0, 0); +} + +void PowerMgrDestroyTimer(PowerTimer *timer) +{ + if (timer == NULL) { + POWER_HILOGE("Invalid timer"); + return; + } + + PowerTimerInfo *info = GetPowerTimerInfo(timer); + int32_t ret = timer_delete(info->timerId); + POWER_HILOGD("Destory timer: %d", ret); + free(info); +} -- Gitee