From 417bc4e4ce9841e64417775c2c576ae481bc8c8b Mon Sep 17 00:00:00 2001 From: zhangruixue Date: Fri, 19 Apr 2024 17:53:59 +0800 Subject: [PATCH] add system_date_time ffi Signed-off-by: zhangruixue --- bundle.json | 3 +- framework/cj/BUILD.gn | 55 +++++++ framework/cj/include/system_date_time.h | 50 ++++++ framework/cj/include/system_date_time_ffi.h | 35 ++++ framework/cj/include/utils.h | 34 ++++ framework/cj/src/system_date_mock.cpp | 27 ++++ framework/cj/src/system_date_time.cpp | 167 ++++++++++++++++++++ framework/cj/src/system_date_time_ffi.cpp | 130 +++++++++++++++ 8 files changed, 500 insertions(+), 1 deletion(-) create mode 100644 framework/cj/BUILD.gn create mode 100644 framework/cj/include/system_date_time.h create mode 100644 framework/cj/include/system_date_time_ffi.h create mode 100644 framework/cj/include/utils.h create mode 100644 framework/cj/src/system_date_mock.cpp create mode 100644 framework/cj/src/system_date_time.cpp create mode 100644 framework/cj/src/system_date_time_ffi.cpp diff --git a/bundle.json b/bundle.json index 3cf25c1..8551bc3 100644 --- a/bundle.json +++ b/bundle.json @@ -54,6 +54,7 @@ "//base/time/time_service/framework/js/napi/system_time:systemtime", "//base/time/time_service/framework/js/napi/system_timer:systemtimer", "//base/time/time_service/framework/js/napi/system_date_time:systemdatetime", + "//base/time/time_service/framework/cj:cj_system_date_time_ffi", "//base/time/time_service/interfaces/inner_api:time_client" ], "service_group":[ @@ -83,4 +84,4 @@ ] } } -} \ No newline at end of file +} diff --git a/framework/cj/BUILD.gn b/framework/cj/BUILD.gn new file mode 100644 index 0000000..ef777ba --- /dev/null +++ b/framework/cj/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (C) 2024 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("../../time.gni") + +ohos_shared_library("cj_system_date_time_ffi") { + defines = [] + include_dirs = [ + "include/", + "../../interfaces/inner_api/include", + "../../utils/native/include", + ] + + if (!build_ohos_sdk) { + defines += [ "PREVIEWER" ] + sources = [ "src/system_date_mock.cpp" ] + } else { + sources = [ + "src/system_date_time.cpp", + "src/system_date_time_ffi.cpp", + ] + } + + deps = [ "${api_path}:time_client" ] + + external_deps = [ + "ability_runtime:abilitykit_native", + "ability_runtime:wantagent_innerkits", + "hilog:libhilog", + "napi:cj_bind_ffi", + ] + + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = time_sanitize_debug + } + innerapi_tags = [ "platformsdk" ] + subsystem_name = "time" + part_name = "time_service" +} diff --git a/framework/cj/include/system_date_time.h b/framework/cj/include/system_date_time.h new file mode 100644 index 0000000..a449bce --- /dev/null +++ b/framework/cj/include/system_date_time.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 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 OHOS_SYSTEM_DATE_TIME_H +#define OHOS_SYSTEM_DATE_TIME_H + +#include +#include + +namespace OHOS { +namespace CJSystemapi { +namespace SystemDateTime { + +constexpr int32_t STARTUP = 0; +constexpr int32_t ACTIVE = 1; +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; +class SystemDateTimeImpl { +public: + static int SetTime(int64_t time); + static std::tuple getCurrentTime(bool isNano); + static std::tuple getRealActiveTime(bool isNano); + static std::tuple getRealTime(bool isNano); + static std::tuple getTime(bool isNano); + static std::tuple getUpTime(int32_t timeType, bool isNano); + static int SetTimeZone(char* timezone); + static std::tuple getTimezone(); +private: + static int32_t GetDeviceTime(clockid_t clockId, bool isNano, int64_t &time); +}; + +char* MallocCString(const std::string& origin); +} // SystemDateTime +} // CJSystemapi +} // OHOS + +#endif // OHOS_SYSTEM_DATE_TIME_H \ No newline at end of file diff --git a/framework/cj/include/system_date_time_ffi.h b/framework/cj/include/system_date_time_ffi.h new file mode 100644 index 0000000..58d6a99 --- /dev/null +++ b/framework/cj/include/system_date_time_ffi.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 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 OHOS_SYSTEM_DATE_TIME_FFI_H +#define OHOS_SYSTEM_DATE_TIME_FFI_H + +#include "native/ffi_remote_data.h" +#include "cj_ffi/cj_common_ffi.h" + +#include + +extern "C" { + FFI_EXPORT RetCode FfiOHOSSysDateTimeSetTime(int64_t time); + FFI_EXPORT RetDataI64 FfiOHOSSysDateTimegetCurrentTime(bool isNano); + FFI_EXPORT RetDataI64 FfiOHOSSysDateTimegetRealActiveTime(bool isNano); + FFI_EXPORT RetDataI64 FfiOHOSSysDateTimegetRealTime(bool isNano); + FFI_EXPORT RetDataI64 FfiOHOSSysDateTimeGetTime(bool isNano); + FFI_EXPORT RetDataI64 FfiOHOSSysDateTimeGetUptime(int32_t timeType, bool isNano); + FFI_EXPORT RetCode FfiOHOSSysSetTimezone(char* timezone); + FFI_EXPORT RetDataCString FfiOHOSSysGetTimezone(); +} + +#endif // OHOS_SYSTEM_DATE_TIME_FFI_H \ No newline at end of file diff --git a/framework/cj/include/utils.h b/framework/cj/include/utils.h new file mode 100644 index 0000000..c9b7cb6 --- /dev/null +++ b/framework/cj/include/utils.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 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 OHOS_SYSTEM_DATE_TIME_UTILS_H +#define OHOS_SYSTEM_DATE_TIME_UTILS_H + +namespace OHOS { +namespace CJSystemapi { +namespace SystemDateTime { + +enum CjErrorCode : int { + ERROR_OK = 0, + ERROR = -1, + PERMISSION_ERROR = 201, + SYSTEM_APP_ERROR = 202, + PARAMETER_ERROR = 401, +}; + +} //SystemDateTime +} //CJSystemapi +} //OHOS + +#endif // OHOS_SYSTEM_DATE_TIME_UTILS_H \ No newline at end of file diff --git a/framework/cj/src/system_date_mock.cpp b/framework/cj/src/system_date_mock.cpp new file mode 100644 index 0000000..79a2542 --- /dev/null +++ b/framework/cj/src/system_date_mock.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 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 "cj_ffi/cj_common_ffi.h" + +extern "C" { +FFI_EXPORT int FfiOHOSSysDateTimeSetTime = 0; +FFI_EXPORT int FfiOHOSSysDateTimegetCurrentTime = 0; +FFI_EXPORT int FfiOHOSSysDateTimegetRealActiveTime = 0; +FFI_EXPORT int FfiOHOSSysDateTimegetRealTime = 0; +FFI_EXPORT int FfiOHOSSysDateTimeGetTime = 0; +FFI_EXPORT int FfiOHOSSysDateTimeGetUptime = 0; +FFI_EXPORT int FfiOHOSSysSetTimezone = 0; +FFI_EXPORT int FfiOHOSSysGetTimezone = 0; +} \ No newline at end of file diff --git a/framework/cj/src/system_date_time.cpp b/framework/cj/src/system_date_time.cpp new file mode 100644 index 0000000..bb0e72b --- /dev/null +++ b/framework/cj/src/system_date_time.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2024 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 "time_hilog.h" +#include "system_date_time.h" +#include "cj_ffi/cj_common_ffi.h" +#include "time_service_client.h" +#include "utils.h" + +namespace OHOS { +namespace CJSystemapi { +namespace SystemDateTime { + +using namespace MiscServices; + +int SystemDateTimeImpl::SetTime(int64_t time) +{ + auto innerCode = TimeServiceClient::GetInstance()->SetTimeV9(time); + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return ERROR; + } + + return SUCCESS_CODE; +} + +std::tuple SystemDateTimeImpl::getCurrentTime(bool isNano) +{ + int32_t innerCode; + int64_t time = 0; + if (isNano) { + innerCode = TimeServiceClient::GetInstance()->GetWallTimeNs(time); + } else { + innerCode = TimeServiceClient::GetInstance()->GetWallTimeMs(time); + } + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, ERROR}; + } + return {SUCCESS_CODE, time}; +} + +std::tuple SystemDateTimeImpl::getRealActiveTime(bool isNano) +{ + int32_t innerCode; + int64_t time = 0; + if (isNano) { + innerCode = TimeServiceClient::GetInstance()->GetMonotonicTimeNs(time); + } else { + innerCode = TimeServiceClient::GetInstance()->GetMonotonicTimeMs(time); + } + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, ERROR}; + } + return {SUCCESS_CODE, time}; +} + +std::tuple SystemDateTimeImpl::getRealTime(bool isNano) +{ + int32_t innerCode; + int64_t time = 0; + if (isNano) { + innerCode = TimeServiceClient::GetInstance()->GetBootTimeNs(time); + } else { + innerCode = TimeServiceClient::GetInstance()->GetBootTimeMs(time); + } + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, ERROR}; + } + return {SUCCESS_CODE, time}; +} + +int32_t SystemDateTimeImpl::GetDeviceTime(clockid_t clockId, bool isNano, int64_t &time) +{ + struct timespec tv {}; + if (clock_gettime(clockId, &tv) < 0) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed clock_gettime"); + return 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 0; +} + +std::tuple SystemDateTimeImpl::getTime(bool isNano) +{ + int32_t innerCode; + int64_t time = 0; + innerCode = GetDeviceTime(CLOCK_REALTIME, isNano, time); + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, ERROR}; + } + return {SUCCESS_CODE, time}; +} + +std::tuple SystemDateTimeImpl::getUpTime(int32_t timeType, bool isNano) +{ + int32_t innerCode; + int64_t time = 0; + if (timeType == STARTUP) { + innerCode = GetDeviceTime(CLOCK_BOOTTIME, isNano, time); + } else { + innerCode = GetDeviceTime(CLOCK_MONOTONIC, isNano, time); + } + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, ERROR}; + } + return {SUCCESS_CODE, time}; +} + +int SystemDateTimeImpl::SetTimeZone(char* timezone) +{ + auto innerCode = TimeServiceClient::GetInstance()->SetTimeZoneV9(timezone); + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return ERROR; + } + return SUCCESS_CODE; +} + +char* MallocCString(const std::string& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto len = origin.length() + 1; + char* res = (char*)malloc(sizeof(char) * len); + if (res == nullptr) { + return nullptr; + } + return std::char_traits::copy(res, origin.c_str(), len); +} + +std::tuple SystemDateTimeImpl::getTimezone() +{ + int32_t innerCode; + std::string time; + innerCode = TimeServiceClient::GetInstance()->GetTimeZone(time); + if (innerCode != CjErrorCode::ERROR_OK) { + TIME_HILOGE(TIME_MODULE_CLIENT, "failed innerCode is %{public}d", innerCode); + return {innerCode, nullptr}; + } + return {SUCCESS_CODE, MallocCString(time)}; +} +} // SystemDateTime +} // CJSystemapi +} // OHOS \ No newline at end of file diff --git a/framework/cj/src/system_date_time_ffi.cpp b/framework/cj/src/system_date_time_ffi.cpp new file mode 100644 index 0000000..2b295f0 --- /dev/null +++ b/framework/cj/src/system_date_time_ffi.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024 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 "time_hilog.h" +#include "cj_ffi/cj_common_ffi.h" +#include "system_date_time.h" +#include "system_date_time_ffi.h" + +namespace OHOS { +namespace CJSystemapi { +namespace SystemDateTime { + +extern "C" { +RetCode FfiOHOSSysDateTimeSetTime(int64_t time) +{ + RetCode ret = SystemDateTimeImpl::SetTime(time); + if (ret != SUCCESS_CODE) { + return ret; + } + return ret; +} + +RetDataI64 FfiOHOSSysDateTimegetCurrentTime(bool isNano) +{ + RetDataI64 ret = { .code = INVALID_DATA_ID, .data = 0 }; + auto [state, time] = SystemDateTimeImpl::getCurrentTime(isNano); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = 0; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} + +RetDataI64 FfiOHOSSysDateTimegetRealActiveTime(bool isNano) +{ + RetDataI64 ret = { .code = INVALID_DATA_ID, .data = 0 }; + auto [state, time] = SystemDateTimeImpl::getRealActiveTime(isNano); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = 0; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} + +RetDataI64 FfiOHOSSysDateTimegetRealTime(bool isNano) +{ + RetDataI64 ret = { .code = INVALID_DATA_ID, .data = 0 }; + auto [state, time] = SystemDateTimeImpl::getRealTime(isNano); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = 0; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} + +RetDataI64 FfiOHOSSysDateTimeGetTime(bool isNano) +{ + RetDataI64 ret = { .code = INVALID_DATA_ID, .data = 0 }; + auto [state, time] = SystemDateTimeImpl::getTime(isNano); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = 0; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} + +RetDataI64 FfiOHOSSysDateTimeGetUptime(int32_t timeType, bool isNano) +{ + RetDataI64 ret = { .code = INVALID_DATA_ID, .data = 0 }; + auto [state, time] = SystemDateTimeImpl::getUpTime(timeType, isNano); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = 0; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} + +RetCode FfiOHOSSysSetTimezone(char* timezone) +{ + RetCode ret = SystemDateTimeImpl::SetTimeZone(timezone); + if (ret != SUCCESS_CODE) { + return ret; + } + return ret; +} + +RetDataCString FfiOHOSSysGetTimezone() +{ + RetDataCString ret = { .code = INVALID_DATA_ID, .data = nullptr }; + auto [state, time] = SystemDateTimeImpl::getTimezone(); + if (state != SUCCESS_CODE) { + ret.code = state; + ret.data = nullptr; + return ret; + } + ret.code = state; + ret.data = time; + return ret; +} +} +} // SystemDateTime +} // CJSystemapi +} // OHOS \ No newline at end of file -- Gitee