From df23beb692f229ea8d649b269ec0731de5f76268 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Fri, 25 Apr 2025 14:52:44 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[add]=20js=E6=8E=A5=E5=8F=A3IDL=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- bundle.json | 10 +- framework/js/taihe/common/include/ani_utils.h | 36 ++++++ framework/js/taihe/system_datetime/BUILD.gn | 93 +++++++++++++++ .../ohos.time_service.systemDateTime.taihe | 33 ++++++ .../system_datetime/src/ani_constructor.cpp | 29 +++++ .../ohos.time_service.systemDateTime.impl.cpp | 111 ++++++++++++++++++ framework/js/taihe/system_timer/BUILD.gn | 94 +++++++++++++++ .../idl/ohos.time_service.systemtimer.taihe | 43 +++++++ .../system_timer/include/ani_system_timer.h | 43 +++++++ .../system_timer/src/ani_constructor.cpp | 31 +++++ .../system_timer/src/ani_system_timer.cpp | 55 +++++++++ .../ohos.time_service.systemtimer.impl.cpp | 78 ++++++++++++ 12 files changed, 655 insertions(+), 1 deletion(-) create mode 100644 framework/js/taihe/common/include/ani_utils.h create mode 100644 framework/js/taihe/system_datetime/BUILD.gn create mode 100644 framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe create mode 100644 framework/js/taihe/system_datetime/src/ani_constructor.cpp create mode 100644 framework/js/taihe/system_datetime/src/ohos.time_service.systemDateTime.impl.cpp create mode 100644 framework/js/taihe/system_timer/BUILD.gn create mode 100644 framework/js/taihe/system_timer/idl/ohos.time_service.systemtimer.taihe create mode 100644 framework/js/taihe/system_timer/include/ani_system_timer.h create mode 100644 framework/js/taihe/system_timer/src/ani_constructor.cpp create mode 100644 framework/js/taihe/system_timer/src/ani_system_timer.cpp create mode 100644 framework/js/taihe/system_timer/src/ohos.time_service.systemtimer.impl.cpp diff --git a/bundle.json b/bundle.json index ee9eb119..2d24d7fb 100644 --- a/bundle.json +++ b/bundle.json @@ -63,7 +63,9 @@ "//base/time/time_service/framework/js/napi/system_date_time:systemdatetime", "//base/time/time_service/interfaces/inner_api:time_client", "//base/time/time_service/interfaces/kits/c:time_service_ndk", - "//base/time/time_service/framework/js/ani:systemdatetime_ani_package" + "//base/time/time_service/framework/js/ani:systemdatetime_ani_package", + "//base/time/time_service/framework/js/taihe/system_datetime:systemdatetime_taihe", + "//base/time/time_service/framework/js/taihe/system_timer:systemtimer_taihe" ], "service_group":[ "//base/time/time_service/services/etc/init:timeservice.cfg", @@ -90,6 +92,12 @@ "header_files": [], "header_base":"//base/time/time_service/framework/cj/include" } + }, + { + "name": "//base/time/time_service/framework/js/taihe/system_datetime:copy_systemdatetime" + }, + { + "name": "//base/time/time_service/framework/js/taihe/system_timer:copy_systemtimer" } ], "test": [ diff --git a/framework/js/taihe/common/include/ani_utils.h b/framework/js/taihe/common/include/ani_utils.h new file mode 100644 index 00000000..f6121cb6 --- /dev/null +++ b/framework/js/taihe/common/include/ani_utils.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2025 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 ANI_UTILS_H +#define ANI_UTILS_H + +namespace OHOS { +namespace MiscServices { +namespace Time { + +enum JsErrorCode : int32_t { + ERROR_OK = 0, + ERROR = -1, + PERMISSION_ERROR = 201, + SYSTEM_APP_ERROR = 202, + PARAMETER_ERROR = 401, + NTP_UPDATE_ERROR = 13000001, + NTP_NOT_UPDATE_ERROR = 13000002, +}; +} // namespace Time +} // namespace MiscServices +} // namespace OHOS + +#endif // NAPI_UTILS_H \ No newline at end of file diff --git a/framework/js/taihe/system_datetime/BUILD.gn b/framework/js/taihe/system_datetime/BUILD.gn new file mode 100644 index 00000000..7a4813b4 --- /dev/null +++ b/framework/js/taihe/system_datetime/BUILD.gn @@ -0,0 +1,93 @@ +# Copyright (C) 2025 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/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("//base/time/time_service/time.gni") + +subsystem_name = "time" +part_name = "time_service" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +copy_taihe_idl("copy_systemdatetime") { + sources = [ "idl/ohos.time_service.systemDateTime.taihe" ] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_systemdatetime" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.time_service.systemDateTime.ani.cpp", + "$taihe_generated_file_path/src/ohos.time_service.systemDateTime.abi.c", + ] +} + +taihe_shared_library("systemdatetime_taihe_native") { + taihe_generated_file_path = "$taihe_generated_file_path" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + sources = get_target_outputs(":run_taihe") + include_dirs = [ + "../common/include", + "../../../interfaces/inner_api/include", + "${time_utils_path}/native/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.time_service.systemDateTime.impl.cpp", + ] + deps = [ + ":run_taihe", + "//base/time/time_service/interfaces/inner_api:time_client" + ] + external_deps = [ + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = time_sanitize_debug + } +} + +generate_static_abc("systemdatetime_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.systemDateTime.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/systemdatetime_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("systemdatetime_etc") { + source = "$target_out_dir/systemdatetime_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":systemdatetime_abc" ] +} +group("systemdatetime_taihe") { + deps = [ + ":systemdatetime_etc", + ":systemdatetime_taihe_native", + ] +} \ No newline at end of file diff --git a/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe b/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe new file mode 100644 index 00000000..7e9c1102 --- /dev/null +++ b/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025-2025 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. + */ + +@!namespace("@ohos.systemDateTime", "systemDateTime") + +enum TimeType: i32 { + STARTUP, + ACTIVE +} + +@gen_promise("getTimezone") +@gen_async("getTimezone") +function GetTimezoneSync(): String; + +function GetUptime(timeType: TimeType, isNanoseconds: Optional): i64; + +function GetTime(isNanoseconds: Optional): i64; + +@gen_promise("setTime") +@gen_async("setTime") +function SetTimeSync(time: i64); \ No newline at end of file diff --git a/framework/js/taihe/system_datetime/src/ani_constructor.cpp b/framework/js/taihe/system_datetime/src/ani_constructor.cpp new file mode 100644 index 00000000..13d812bc --- /dev/null +++ b/framework/js/taihe/system_datetime/src/ani_constructor.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2025 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 "ohos.time_service.systemDateTime.ani.hpp" +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ +ani_env *env; +if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { +return ANI_ERROR; +} +if (ANI_OK != ohos::time_service::systemDateTime::ANIRegister(env)) { +std::cerr << "Error from ohos::time_service::systemdatetime::ANIRegister" << std::endl; +return ANI_ERROR; +} +*result = ANI_VERSION_1; +return ANI_OK; +} \ No newline at end of file diff --git a/framework/js/taihe/system_datetime/src/ohos.time_service.systemDateTime.impl.cpp b/framework/js/taihe/system_datetime/src/ohos.time_service.systemDateTime.impl.cpp new file mode 100644 index 00000000..9aeff2e4 --- /dev/null +++ b/framework/js/taihe/system_datetime/src/ohos.time_service.systemDateTime.impl.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2025 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 "ohos.time_service.systemDateTime.proj.hpp" +#include "ohos.time_service.systemDateTime.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "time_service_client.h" +#include "ani_utils.h" +#include "parameters.h" + +using namespace taihe; +using namespace ohos::time_service::systemDateTime; +using namespace OHOS::MiscServices; +using namespace OHOS::MiscServices::Time; + +namespace { + +constexpr int64_t SECONDS_TO_NANO = 1000000000; +constexpr int64_t SECONDS_TO_MILLI = 1000; +constexpr int64_t NANO_TO_MILLI = SECONDS_TO_NANO / SECONDS_TO_MILLI; +constexpr int32_t STARTUP = 0; +constexpr int32_t ACTIVE = 1; +constexpr const char *TIMEZONE_KEY = "persist.time.timezone"; + +int32_t GetDeviceTime(clockid_t clockId, bool isNano, int64_t &time) +{ + struct timespec tv {}; + if (clock_gettime(clockId, &tv) < 0) { + return JsErrorCode::ERROR; + } + + if (isNano) { + time = tv.tv_sec * SECONDS_TO_NANO + tv.tv_nsec; + } else { + time = tv.tv_sec * SECONDS_TO_MILLI + tv.tv_nsec / NANO_TO_MILLI; + } + return JsErrorCode::ERROR_OK; +} + + +string GetTimezoneSync() +{ + auto timeZone = OHOS::system::GetParameter(TIMEZONE_KEY, "Asia/Shanghai"); + if (timeZone.empty()) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return ""; + } + return timeZone; +} + +int64_t GetUptime(TimeType timeType, optional_view isNanoseconds) +{ + bool isNanosecondsValue = isNanoseconds.value_or(false); + int innerCode; + int64_t time = 0; + if (timeType == STARTUP) { + innerCode = GetDeviceTime(CLOCK_BOOTTIME, isNanosecondsValue, time); + } else if (timeType == ACTIVE) { + innerCode = GetDeviceTime(CLOCK_MONOTONIC, isNanosecondsValue, time); + } else { + set_business_error(JsErrorCode::PARAMETER_ERROR, "The 'timeType' must be 'STARTUP' or 'ACTIVE' or 0 or 1"); + return 0; + } + + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return 0; + } + return time; +} + +int64_t GetTime(optional_view isNanoseconds) +{ + bool isNanosecondsValue = isNanoseconds.value_or(false); + int64_t time = 0; + int32_t innerCode = GetDeviceTime(CLOCK_REALTIME, isNanosecondsValue, time); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return 0; + } + return time; +} + +void SetTimeSync(int64_t time) +{ + auto innerCode = TimeServiceClient::GetInstance()->SetTimeV9(time); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return; + } + return; +} +} // namespace + +TH_EXPORT_CPP_API_GetTimezoneSync(GetTimezoneSync); +TH_EXPORT_CPP_API_GetUptime(GetUptime); +TH_EXPORT_CPP_API_GetTime(GetTime); +TH_EXPORT_CPP_API_SetTimeSync(SetTimeSync); \ No newline at end of file diff --git a/framework/js/taihe/system_timer/BUILD.gn b/framework/js/taihe/system_timer/BUILD.gn new file mode 100644 index 00000000..eba1df97 --- /dev/null +++ b/framework/js/taihe/system_timer/BUILD.gn @@ -0,0 +1,94 @@ +# Copyright (C) 2025 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/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("//base/time/time_service/time.gni") + +subsystem_name = "time" +part_name = "time_service" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +copy_taihe_idl("copy_systemtimer") { + sources = [ "idl/ohos.time_service.systemtimer.taihe" ] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":copy_systemtimer" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.time_service.systemtimer.ani.cpp", + "$taihe_generated_file_path/src/ohos.time_service.systemtimer.abi.c", + ] +} +taihe_shared_library("systemtimer_taihe_native") { + taihe_generated_file_path = "$taihe_generated_file_path" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + sources = get_target_outputs(":run_taihe") + include_dirs = [ + "include", + "../common/include", + "../../../interfaces/inner_api/include", + "${time_utils_path}/native/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ani_system_timer.cpp", + "src/ohos.time_service.systemtimer.impl.cpp", + ] + deps = [ + ":run_taihe", + "//base/time/time_service/interfaces/inner_api:time_client" + ] + external_deps = [ + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = time_sanitize_debug + } +} + +generate_static_abc("systemtimer_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.systemTimer.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/systemtimer_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("systemtimer_etc") { + source = "$target_out_dir/systemtimer_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":systemtimer_abc" ] +} +group("systemtimer_taihe") { + deps = [ + ":systemtimer_etc", + ":systemtimer_taihe_native", + ] +} \ No newline at end of file diff --git a/framework/js/taihe/system_timer/idl/ohos.time_service.systemtimer.taihe b/framework/js/taihe/system_timer/idl/ohos.time_service.systemtimer.taihe new file mode 100644 index 00000000..6c185d0e --- /dev/null +++ b/framework/js/taihe/system_timer/idl/ohos.time_service.systemtimer.taihe @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025-2025 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. + */ + +@!namespace("@ohos.systemTimer", "systemTimer") +@!sts_inject(""" +static { loadLibrary("systemtimer_taihe_native.z") } +""") +struct TimerOptions {type: i64; repeat: bool; interval: i64; name: String;} + +@const +enum TimerType: i32 { + TIMER_TYPE_REALTIME = 1, + TIMER_TYPE_WAKEUP = 2, + TIMER_TYPE_EXACT = 4 +} + +@gen_promise("createTimer") +@gen_async("createTimer") +function CreateTimerSync(options: TimerOptions): i64; + +@gen_promise("startTimer") +@gen_async("startTimer") +function StartTimerSync(timer: i64, triggerTime: i64); + +@gen_promise("stopTimer") +@gen_async("stopTimer") +function StopTimerSync(timer: i64); + +@gen_promise("destroyTimer") +@gen_async("destroyTimer") +function DestroyTimerSync(timer: i64); \ No newline at end of file diff --git a/framework/js/taihe/system_timer/include/ani_system_timer.h b/framework/js/taihe/system_timer/include/ani_system_timer.h new file mode 100644 index 00000000..9911d397 --- /dev/null +++ b/framework/js/taihe/system_timer/include/ani_system_timer.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2025 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 ANI_SYSTEM_TIMER_H +#define ANI_SYSTEM_TIMER_H + +#include "time_service_client.h" + +namespace OHOS { +namespace MiscServices { +namespace Time { + +constexpr int TIMER_TYPE_REALTIME = 0; +constexpr int TIMER_TYPE_WAKEUP = 1; +constexpr int TIMER_TYPE_EXACT = 2; +constexpr int TIMER_TYPE_IDLE = 3; + +class ITimerInfoInstance : public OHOS::MiscServices::ITimerInfo { +public: + ITimerInfoInstance(); + virtual ~ITimerInfoInstance(); + virtual void OnTrigger() override; + virtual void SetType(const int &type) override; + virtual void SetRepeat(bool repeat) override; + virtual void SetInterval(const uint64_t &interval) override; + virtual void SetWantAgent(std::shared_ptr wantAgent) override; +}; +} +} +} +#endif // ANI_SYSTEM_TIMER_H \ No newline at end of file diff --git a/framework/js/taihe/system_timer/src/ani_constructor.cpp b/framework/js/taihe/system_timer/src/ani_constructor.cpp new file mode 100644 index 00000000..65161e86 --- /dev/null +++ b/framework/js/taihe/system_timer/src/ani_constructor.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 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 "ohos.time_service.systemtimer.ani.hpp" +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != ohos::time_service::systemtimer::ANIRegister(env)) { + std::cerr << "Error from ohos::time_service::systemtimer::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} + \ No newline at end of file diff --git a/framework/js/taihe/system_timer/src/ani_system_timer.cpp b/framework/js/taihe/system_timer/src/ani_system_timer.cpp new file mode 100644 index 00000000..46feac90 --- /dev/null +++ b/framework/js/taihe/system_timer/src/ani_system_timer.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2025 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 "ani_system_timer.h" +using namespace OHOS::MiscServices; + +namespace OHOS { +namespace MiscServices { +namespace Time { + +ITimerInfoInstance::ITimerInfoInstance() +{ +} + +ITimerInfoInstance::~ITimerInfoInstance() +{ +} + +void ITimerInfoInstance::OnTrigger() +{ + return; +} + +void ITimerInfoInstance::SetType(const int &_type) +{ + type = _type; +} + +void ITimerInfoInstance::SetRepeat(bool _repeat) +{ + repeat = _repeat; +} +void ITimerInfoInstance::SetInterval(const uint64_t &_interval) +{ + interval = _interval; +} +void ITimerInfoInstance::SetWantAgent(std::shared_ptr _wantAgent) +{ + wantAgent = _wantAgent; +} +} // namespace Time +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/framework/js/taihe/system_timer/src/ohos.time_service.systemtimer.impl.cpp b/framework/js/taihe/system_timer/src/ohos.time_service.systemtimer.impl.cpp new file mode 100644 index 00000000..0c14824a --- /dev/null +++ b/framework/js/taihe/system_timer/src/ohos.time_service.systemtimer.impl.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2025 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 "ohos.time_service.systemtimer.proj.hpp" +#include "ohos.time_service.systemtimer.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "ani_system_timer.h" +#include "ani_utils.h" + +using namespace taihe; +using namespace ohos::time_service::systemtimer; +using namespace OHOS::MiscServices; +using namespace OHOS::MiscServices::Time; + +namespace { +// To be implemented. + +int64_t CreateTimerSync(TimerOptions const& options) +{ + std::shared_ptr iTimerInfoInstance = std::make_shared(); + iTimerInfoInstance->SetType(options.type); + iTimerInfoInstance->SetRepeat(options.repeat); + iTimerInfoInstance->SetInterval(options.interval); + iTimerInfoInstance->SetName(std::string(options.name)); + uint64_t timerId = 0; + auto innerCode = TimeServiceClient::GetInstance()->CreateTimerV9(iTimerInfoInstance, timerId); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return 0; + } + return timerId; +} + +void StartTimerSync(int64_t timer, int64_t triggerTime) +{ + auto innerCode = TimeServiceClient::GetInstance()->StartTimerV9(timer, triggerTime); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return; + } +} + +void StopTimerSync(int64_t timer) +{ + auto innerCode = TimeServiceClient::GetInstance()->StopTimerV9(timer); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return; + } +} + +void DestroyTimerSync(int64_t timer) +{ + auto innerCode = TimeServiceClient::GetInstance()->DestroyTimerV9(timer); + if (innerCode != JsErrorCode::ERROR_OK) { + set_business_error(JsErrorCode::ERROR, "convert native object to javascript object failed"); + return; + } +} +} // namespace + +TH_EXPORT_CPP_API_CreateTimerSync(CreateTimerSync); +TH_EXPORT_CPP_API_StartTimerSync(StartTimerSync); +TH_EXPORT_CPP_API_StopTimerSync(StopTimerSync); +TH_EXPORT_CPP_API_DestroyTimerSync(DestroyTimerSync); \ No newline at end of file -- Gitee From 9b7484c31fc8fc9c7eb21405b5461198c38f2ac2 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Thu, 8 May 2025 15:22:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?[fix]=20IDL=E5=8C=96=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E5=85=A5so?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- .../system_datetime/idl/ohos.time_service.systemDateTime.taihe | 3 +++ 1 file changed, 3 insertions(+) diff --git a/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe b/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe index 7e9c1102..fe1e165d 100644 --- a/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe +++ b/framework/js/taihe/system_datetime/idl/ohos.time_service.systemDateTime.taihe @@ -14,6 +14,9 @@ */ @!namespace("@ohos.systemDateTime", "systemDateTime") +@!sts_inject(""" +static { loadLibrary("systemdatetime_taihe_native.z") } +""") enum TimeType: i32 { STARTUP, -- Gitee From 9c60e39aaa917b08da6b612b20843e32b987728a Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Fri, 30 May 2025 10:31:56 +0800 Subject: [PATCH 3/4] =?UTF-8?q?[add]=20=E5=8F=96=E6=B6=88=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=89=8B=E5=86=99ani=E9=83=A8=E5=88=86=20=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=B8=8Etaihe=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- bundle.json | 1 - 1 file changed, 1 deletion(-) diff --git a/bundle.json b/bundle.json index 2d24d7fb..3150b61d 100644 --- a/bundle.json +++ b/bundle.json @@ -63,7 +63,6 @@ "//base/time/time_service/framework/js/napi/system_date_time:systemdatetime", "//base/time/time_service/interfaces/inner_api:time_client", "//base/time/time_service/interfaces/kits/c:time_service_ndk", - "//base/time/time_service/framework/js/ani:systemdatetime_ani_package", "//base/time/time_service/framework/js/taihe/system_datetime:systemdatetime_taihe", "//base/time/time_service/framework/js/taihe/system_timer:systemtimer_taihe" ], -- Gitee From 73aab7601fb808c65de7f74a0f057137de2693d8 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Tue, 8 Jul 2025 15:21:57 +0800 Subject: [PATCH 4/4] [Taihe] Fix gn output for taihe stdlib Signed-off-by: gaozhichao --- framework/js/taihe/system_datetime/BUILD.gn | 11 ++++++----- framework/js/taihe/system_timer/BUILD.gn | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/framework/js/taihe/system_datetime/BUILD.gn b/framework/js/taihe/system_datetime/BUILD.gn index 7a4813b4..2f2141ab 100644 --- a/framework/js/taihe/system_datetime/BUILD.gn +++ b/framework/js/taihe/system_datetime/BUILD.gn @@ -18,14 +18,15 @@ import("//base/time/time_service/time.gni") subsystem_name = "time" part_name = "time_service" -taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" +taihe_generated_file_path_datetime = + "$taihe_file_path/out/$subsystem_name/$part_name/systemDateTime" copy_taihe_idl("copy_systemdatetime") { sources = [ "idl/ohos.time_service.systemDateTime.taihe" ] } ohos_taihe("run_taihe") { - taihe_generated_file_path = "$taihe_generated_file_path" + taihe_generated_file_path = "${taihe_generated_file_path_datetime}" deps = [ ":copy_systemdatetime" ] outputs = [ "$taihe_generated_file_path/src/ohos.time_service.systemDateTime.ani.cpp", @@ -34,7 +35,7 @@ ohos_taihe("run_taihe") { } taihe_shared_library("systemdatetime_taihe_native") { - taihe_generated_file_path = "$taihe_generated_file_path" + taihe_generated_file_path = "${taihe_generated_file_path_datetime}" part_name = "$part_name" subsystem_name = "$subsystem_name" sources = get_target_outputs(":run_taihe") @@ -71,8 +72,8 @@ taihe_shared_library("systemdatetime_taihe_native") { } generate_static_abc("systemdatetime_abc") { - base_url = "$taihe_generated_file_path" - files = [ "$taihe_generated_file_path/@ohos.systemDateTime.ets" ] + base_url = "${taihe_generated_file_path_datetime}" + files = [ "${taihe_generated_file_path_datetime}/@ohos.systemDateTime.ets" ] is_boot_abc = "True" device_dst_file = "/system/framework/systemdatetime_abc.abc" dependencies = [ ":run_taihe" ] diff --git a/framework/js/taihe/system_timer/BUILD.gn b/framework/js/taihe/system_timer/BUILD.gn index eba1df97..85c5a996 100644 --- a/framework/js/taihe/system_timer/BUILD.gn +++ b/framework/js/taihe/system_timer/BUILD.gn @@ -18,14 +18,15 @@ import("//base/time/time_service/time.gni") subsystem_name = "time" part_name = "time_service" -taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" +taihe_generated_file_path_systemtimer = + "$taihe_file_path/out/$subsystem_name/$part_name/systemtimer" copy_taihe_idl("copy_systemtimer") { sources = [ "idl/ohos.time_service.systemtimer.taihe" ] } ohos_taihe("run_taihe") { - taihe_generated_file_path = "$taihe_generated_file_path" + taihe_generated_file_path = "${taihe_generated_file_path_systemtimer}" deps = [ ":copy_systemtimer" ] outputs = [ "$taihe_generated_file_path/src/ohos.time_service.systemtimer.ani.cpp", @@ -33,7 +34,7 @@ ohos_taihe("run_taihe") { ] } taihe_shared_library("systemtimer_taihe_native") { - taihe_generated_file_path = "$taihe_generated_file_path" + taihe_generated_file_path = "${taihe_generated_file_path_systemtimer}" part_name = "$part_name" subsystem_name = "$subsystem_name" sources = get_target_outputs(":run_taihe") @@ -72,8 +73,8 @@ taihe_shared_library("systemtimer_taihe_native") { } generate_static_abc("systemtimer_abc") { - base_url = "$taihe_generated_file_path" - files = [ "$taihe_generated_file_path/@ohos.systemTimer.ets" ] + base_url = "$taihe_generated_file_path_systemtimer" + files = [ "$taihe_generated_file_path_systemtimer/@ohos.systemTimer.ets" ] is_boot_abc = "True" device_dst_file = "/system/framework/systemtimer_abc.abc" dependencies = [ ":run_taihe" ] -- Gitee