From 3d7fd8ed7c97949d3da9a4bd9e042e94c95c59bf Mon Sep 17 00:00:00 2001 From: chenming Date: Mon, 10 Jan 2022 21:25:45 +0800 Subject: [PATCH] Add constant napi Signed-off-by: chenming --- .../innerkits/native/include/work_condition.h | 91 +++++++ interfaces/kits/js/BUILD.gn | 67 +++++ .../kits/js/napi/include/workscheduler_napi.h | 58 +++++ .../kits/js/napi/src/workscheduler_napi.cpp | 243 ++++++++++++++++++ ohos.build | 4 +- 5 files changed, 462 insertions(+), 1 deletion(-) create mode 100644 interfaces/innerkits/native/include/work_condition.h create mode 100644 interfaces/kits/js/BUILD.gn create mode 100644 interfaces/kits/js/napi/include/workscheduler_napi.h create mode 100644 interfaces/kits/js/napi/src/workscheduler_napi.cpp diff --git a/interfaces/innerkits/native/include/work_condition.h b/interfaces/innerkits/native/include/work_condition.h new file mode 100644 index 0000000..0491c41 --- /dev/null +++ b/interfaces/innerkits/native/include/work_condition.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WORKSCHED_INNERKITS_WORK_CONDITION_H +#define WORKSCHED_INNERKITS_WORK_CONDITION_H + +#include +#include +#include + +namespace OHOS { +namespace WorkScheduler { +class WorkCondition { +public: + virtual ~WorkCondition() = default; + enum Type { + WORK_ID = 0, + BUNDLE_NAME, + ABILITY_NAME, + NETWORK, + CHARGER, + POWER, + BATTERY_STATUS, + BATTERY_LEVEL, + STORAGE, + TIMER, // uint32_t intervalTime, int32_t cycle, bool repeat + TIMER_ROUND, // int32_t passedRound + UNKNOWN, + }; + enum Network { + NETWORK_TYPE_ANY = 0, + NETWORK_TYPE_MOBILE, + NETWORK_TYPE_WIFI, + NETWORK_TYPE_BLUETOOTH, + NETWORK_TYPE_WIFI_P2P, + NETWORK_TYPE_ETHERNET, + NETWORK_TYPE_DISCONNECT, + NETWORK_UNKNOWN + }; + enum Charger { + CHARGER_PLUGGED_ANY = 0, + CHARGER_PLUGGED_AC, + CHARGER_PLUGGED_USB, + CHARGER_PLUGGED_WIRELESS, + CHARGER_UNPLUGGED, + CHARGER_UNKNOWN, + }; + enum Power { + POWER_UNKNOWN = 30, + POWER_CONNECTED, + POWER_DISCONNECTED + }; + enum BatteryStatus { + BATTERY_STATUS_LOW = 0, + BATTERY_STATUS_OKAY, + BATTERY_STATUS_LOW_OR_OKAY, + BATTERY_UNKNOWN + }; + enum Storage { + STORAGE_LEVEL_LOW = 0, + STORAGE_LEVEL_OKAY, + STORAGE_LEVEL_LOW_OR_OKAY, + STORAGE_UNKNOWN + }; +}; + +struct Condition { + int enumVal; + int32_t intVal; + int64_t longVal; + uint32_t uintVal; + time_t timeVal; + bool boolVal; + std::string strVal; +}; +} // namespace WorkScheduler +} // namespace OHOS + +#endif // WORKSCHED_INNERKITS_WORK_CONDITION_H diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn new file mode 100644 index 0000000..76b188e --- /dev/null +++ b/interfaces/kits/js/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") +import("//foundation/resourceschedule/work_scheduler/workscheduler.gni") + +config("worksched_private_config") { + include_dirs = [ + "//utils/system/safwk/native/include", + "${worksched_native_innerkits_path}/native/include", + ] +} + +config("worksched_public_config") { + include_dirs = [ + "${worksched_root_path}/interfaces/kits/js/napi/include", + ] +} + +js_declaration("workscheduler_js") { + part_name = "${worksched_native_part_name}" + sources = [ + "./@ohos.workscheduler.d.ts", + ] +} + +ohos_copy("worksched_declaration") { + sources = [ + "./@ohos.workscheduler.d.ts", + ] + outputs = [ target_out_dir + "/$target_name/" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} + +ohos_shared_library("workscheduler") { + configs = [ + ":worksched_private_config", + ] + + public_configs = [ ":worksched_public_config" ] + + sources = [ + "${worksched_root_path}/interfaces/kits/js/napi/src/workscheduler_napi.cpp", + ] + + deps = [ + "//foundation/ace/napi:ace_napi", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "${worksched_utils_path}:workschedutils", + ] + + relative_install_dir = "module" + part_name = "${worksched_native_part_name}" + subsystem_name = "resourceschedule" +} diff --git a/interfaces/kits/js/napi/include/workscheduler_napi.h b/interfaces/kits/js/napi/include/workscheduler_napi.h new file mode 100644 index 0000000..69be5cf --- /dev/null +++ b/interfaces/kits/js/napi/include/workscheduler_napi.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WORKSCHED_NAPI_H +#define WORKSCHED_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace WorkScheduler { +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((constructor)) void RegisterModule(void); +static napi_value Init(napi_env env, napi_value exports); + +static napi_value InitNetworkType(napi_env env, napi_value exports); +static napi_value EnumNetworkTypeConstructor(napi_env env, napi_callback_info info); +static napi_value InitChargingType(napi_env env, napi_value exports); +static napi_value EnumChargingTypeConstructor(napi_env env, napi_callback_info info); +static napi_value InitBatteryStatus(napi_env env, napi_value exports); +static napi_value EnumBatteryStatusConstructor(napi_env env, napi_callback_info info); +static napi_value InitStorageRequest(napi_env env, napi_value exports); +static napi_value EnumStorageRequestConstructor(napi_env env, napi_callback_info info); + +#ifdef __cplusplus +} +#endif + +/* + * Module define + */ +napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "workscheduler", + .nm_priv = ((void *)0), + .reserved = {0} +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/kits/js/napi/src/workscheduler_napi.cpp b/interfaces/kits/js/napi/src/workscheduler_napi.cpp new file mode 100644 index 0000000..0427dd9 --- /dev/null +++ b/interfaces/kits/js/napi/src/workscheduler_napi.cpp @@ -0,0 +1,243 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "workscheduler_napi.h" +#include "work_condition.h" +#include "work_sched_hilog.h" + +namespace OHOS { +namespace WorkScheduler { + +EXTERN_C_START + +static const uint8_t ARG_FIRST = 1; + +napi_ref networkTypeConstructor_ = nullptr; +napi_ref chargingTypeConstructor_ = nullptr; +napi_ref batteryStatusConstructor_ = nullptr; +napi_ref storageRequestConstructor_ = nullptr; + +napi_value Init(napi_env env, napi_value exports) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + + // Init constant value. + InitNetworkType(env, exports); + InitChargingType(env, exports); + InitBatteryStatus(env, exports); + InitStorageRequest(env, exports); + + return exports; +} + +napi_value InitNetworkType(napi_env env, napi_value exports) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + napi_value network_type_any; + napi_value network_type_mobile; + napi_value network_type_wifi; + napi_value network_type_bluetooth; + napi_value network_type_wifi_p2p; + napi_value network_type_ethernet; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_ANY), &network_type_any); + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_MOBILE), &network_type_mobile); + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_WIFI), &network_type_wifi); + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_BLUETOOTH), &network_type_bluetooth); + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_WIFI_P2P), &network_type_wifi_p2p); + napi_create_uint32(env, static_cast(WorkCondition::Network::NETWORK_TYPE_ETHERNET), &network_type_ethernet); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_ANY", network_type_any), + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_MOBILE", network_type_mobile), + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_WIFI", network_type_wifi), + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_BLUETOOTH", network_type_bluetooth), + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_WIFI_P2P", network_type_wifi_p2p), + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_TYPE_ETHERNET", network_type_ethernet), + }; + + napi_value result = nullptr; + napi_define_class(env, "NetworkType", NAPI_AUTO_LENGTH, EnumNetworkTypeConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &networkTypeConstructor_); + napi_define_class(env, "NetworkType", NAPI_AUTO_LENGTH, EnumNetworkTypeConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &networkTypeConstructor_); + napi_set_named_property(env, exports, "NetworkType", result); + return exports; + + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: return", __func__); +} + +napi_value EnumNetworkTypeConstructor(napi_env env, napi_callback_info info) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + size_t argc = 0; + napi_value args[ARG_FIRST] = { 0 }; + napi_value jsthis = nullptr; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + + WS_HILOGD(MODULE_JS_NAPI, "EnumNetworkTypeConstructor %{public}d", status); + if (status != napi_ok) { + return nullptr; + } + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: return", __func__); + return jsthis; +} + +napi_value InitChargingType(napi_env env, napi_value exports) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + napi_value charging_plugged_any; + napi_value charging_plugged_ac; + napi_value charging_plugged_usb; + napi_value charging_plugged_wireless; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(WorkCondition::Charger::CHARGER_PLUGGED_ANY), &charging_plugged_any); + napi_create_uint32(env, static_cast(WorkCondition::Charger::CHARGER_PLUGGED_AC), &charging_plugged_ac); + napi_create_uint32(env, static_cast(WorkCondition::Charger::CHARGER_PLUGGED_USB), &charging_plugged_usb); + napi_create_uint32(env, static_cast(WorkCondition::Charger::CHARGER_PLUGGED_WIRELESS), &charging_plugged_wireless); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("CHARGING_PLUGGED_ANY", charging_plugged_any), + DECLARE_NAPI_STATIC_PROPERTY("CHARGING_PLUGGED_AC", charging_plugged_ac), + DECLARE_NAPI_STATIC_PROPERTY("CHARGING_PLUGGED_USB", charging_plugged_usb), + DECLARE_NAPI_STATIC_PROPERTY("CHARGING_PLUGGED_WIRELESS", charging_plugged_wireless), + }; + + napi_value result = nullptr; + napi_define_class(env, "ChargingType", NAPI_AUTO_LENGTH, EnumChargingTypeConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &chargingTypeConstructor_); + napi_set_named_property(env, exports, "ChargingType", result); + return exports; +} + +napi_value EnumChargingTypeConstructor(napi_env env, napi_callback_info info) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + size_t argc = 0; + napi_value args[ARG_FIRST] = { 0 }; + napi_value jsthis = nullptr; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + + WS_HILOGD(MODULE_JS_NAPI, "EnumChargingTypeConstructor %{public}d", status); + if (status != napi_ok) { + return nullptr; + } + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: return", __func__); + return jsthis; +} + +napi_value InitBatteryStatus(napi_env env, napi_value exports) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + napi_value battery_status_low; + napi_value battery_status_okay; + napi_value battery_status_low_or_okay; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW), &battery_status_low); + napi_create_uint32(env, static_cast(WorkCondition::BatteryStatus::BATTERY_STATUS_OKAY), &battery_status_okay); + napi_create_uint32(env, static_cast(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW_OR_OKAY), &battery_status_low_or_okay); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("BATTERY_STATUS_LOW", battery_status_low), + DECLARE_NAPI_STATIC_PROPERTY("BATTERY_STATUS_OKAY", battery_status_okay), + DECLARE_NAPI_STATIC_PROPERTY("BATTERY_STATUS_LOW_OR_OKAY", battery_status_low_or_okay), + }; + + napi_value result = nullptr; + napi_define_class(env, "BatteryStatus", NAPI_AUTO_LENGTH, EnumBatteryStatusConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &batteryStatusConstructor_); + napi_set_named_property(env, exports, "BatteryStatus", result); + return exports; +} + +napi_value EnumBatteryStatusConstructor(napi_env env, napi_callback_info info) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + size_t argc = 0; + napi_value args[ARG_FIRST] = { 0 }; + napi_value jsthis = nullptr; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + + WS_HILOGD(MODULE_JS_NAPI, "EnumBatteryStatusConstructor %{public}d", status); + if (status != napi_ok) { + return nullptr; + } + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: return", __func__); + return jsthis; +} + +napi_value InitStorageRequest(napi_env env, napi_value exports) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + napi_value storage_level_low; + napi_value storage_level_okay; + napi_value storage_level_low_or_okay; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(WorkCondition::Storage::STORAGE_LEVEL_LOW), &storage_level_low); + napi_create_uint32(env, static_cast(WorkCondition::Storage::STORAGE_LEVEL_OKAY), &storage_level_okay); + napi_create_uint32(env, static_cast(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY), &storage_level_low_or_okay); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("STORAGE_LEVEL_LOW", storage_level_low), + DECLARE_NAPI_STATIC_PROPERTY("STORAGE_LEVEL_OKAY", storage_level_okay), + DECLARE_NAPI_STATIC_PROPERTY("STORAGE_LEVEL_LOW_OR_OKAY", storage_level_low_or_okay), + }; + + napi_value result = nullptr; + napi_define_class(env, "StorageRequest", NAPI_AUTO_LENGTH, EnumStorageRequestConstructor, nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &storageRequestConstructor_); + napi_set_named_property(env, exports, "StorageRequest", result); + return exports; +} + +napi_value EnumStorageRequestConstructor(napi_env env, napi_callback_info info) +{ + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__); + size_t argc = 0; + napi_value args[ARG_FIRST] = { 0 }; + napi_value jsthis = nullptr; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + + WS_HILOGD(MODULE_JS_NAPI, "EnumStorageRequestConstructor %{public}d", status); + if (status != napi_ok) { + return nullptr; + } + WS_HILOGD(MODULE_JS_NAPI, "%{public}s: return", __func__); + return jsthis; +} + +/* + * Module register function + */ +__attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +EXTERN_C_END + +} +} \ No newline at end of file diff --git a/ohos.build b/ohos.build index f1bc4e6..30f9760 100644 --- a/ohos.build +++ b/ohos.build @@ -5,7 +5,9 @@ "module_list": [ "//foundation/resourceschedule/work_scheduler/services:workschedservice", "//foundation/resourceschedule/work_scheduler/sa_profile:worksched_sa_profile", - "//foundation/resourceschedule/work_scheduler/sa_profile:work_scheduler_service_init" + "//foundation/resourceschedule/work_scheduler/sa_profile:work_scheduler_service_init", + "//foundation/resourceschedule/work_scheduler/interfaces/kits/js:workscheduler", + "//foundation/resourceschedule/work_scheduler/interfaces/kits/js:workscheduler_js" ] } } -- Gitee