From f716bd51ace7c628e3034c79e21143745468591d Mon Sep 17 00:00:00 2001 From: guoxiadi Date: Wed, 21 May 2025 10:15:11 +0800 Subject: [PATCH] bluetooth fwk taihe Signed-off-by: guoxiadi --- bundle.json | 13 +- frameworks/ets/taihe/bluetooth_a2dp/BUILD.gn | 94 +++++ .../idl/ohos.bluetooth.a2dp.taihe | 25 ++ .../bluetooth_a2dp/src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.a2dp.impl.cpp | 52 +++ .../ets/taihe/bluetooth_access/BUILD.gn | 93 +++++ .../idl/ohos.bluetooth.access.taihe | 44 +++ .../bluetooth_access/src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.access.impl.cpp | 83 +++++ .../ets/taihe/bluetooth_baseProfile/BUILD.gn | 93 +++++ .../idl/ohos.bluetooth.baseProfile.taihe | 41 +++ .../src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.baseProfile.impl.cpp | 43 +++ frameworks/ets/taihe/bluetooth_ble/BUILD.gn | 93 +++++ .../idl/ohos.bluetooth.ble.taihe | 330 ++++++++++++++++++ .../bluetooth_ble/src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.ble.impl.cpp | 261 ++++++++++++++ .../ets/taihe/bluetooth_connection/BUILD.gn | 93 +++++ .../idl/ohos.bluetooth.connection.taihe | 168 +++++++++ .../src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.connection.impl.cpp | 185 ++++++++++ .../ets/taihe/bluetooth_constant/BUILD.gn | 88 +++++ .../idl/ohos.bluetooth.constant.taihe | 129 +++++++ .../src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.constant.impl.cpp | 30 ++ frameworks/ets/taihe/bluetooth_hfp/BUILD.gn | 94 +++++ .../idl/ohos.bluetooth.hfp.taihe | 21 ++ .../bluetooth_hfp/src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.hfp.impl.cpp | 52 +++ frameworks/ets/taihe/bluetooth_hid/BUILD.gn | 94 +++++ .../idl/ohos.bluetooth.hid.taihe | 20 ++ .../bluetooth_hid/src/ani_constructor.cpp | 29 ++ .../src/ohos.bluetooth.hid.impl.cpp | 52 +++ 33 files changed, 2521 insertions(+), 2 deletions(-) create mode 100644 frameworks/ets/taihe/bluetooth_a2dp/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_a2dp/idl/ohos.bluetooth.a2dp.taihe create mode 100644 frameworks/ets/taihe/bluetooth_a2dp/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_a2dp/src/ohos.bluetooth.a2dp.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_access/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_access/idl/ohos.bluetooth.access.taihe create mode 100644 frameworks/ets/taihe/bluetooth_access/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_access/src/ohos.bluetooth.access.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_baseProfile/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_baseProfile/idl/ohos.bluetooth.baseProfile.taihe create mode 100644 frameworks/ets/taihe/bluetooth_baseProfile/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_baseProfile/src/ohos.bluetooth.baseProfile.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_ble/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe create mode 100644 frameworks/ets/taihe/bluetooth_ble/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_connection/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_connection/idl/ohos.bluetooth.connection.taihe create mode 100644 frameworks/ets/taihe/bluetooth_connection/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_connection/src/ohos.bluetooth.connection.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_constant/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_constant/idl/ohos.bluetooth.constant.taihe create mode 100644 frameworks/ets/taihe/bluetooth_constant/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_constant/src/ohos.bluetooth.constant.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_hfp/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_hfp/idl/ohos.bluetooth.hfp.taihe create mode 100644 frameworks/ets/taihe/bluetooth_hfp/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_hfp/src/ohos.bluetooth.hfp.impl.cpp create mode 100644 frameworks/ets/taihe/bluetooth_hid/BUILD.gn create mode 100644 frameworks/ets/taihe/bluetooth_hid/idl/ohos.bluetooth.hid.taihe create mode 100644 frameworks/ets/taihe/bluetooth_hid/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/bluetooth_hid/src/ohos.bluetooth.hid.impl.cpp diff --git a/bundle.json b/bundle.json index b77272fd..d0014761 100644 --- a/bundle.json +++ b/bundle.json @@ -69,7 +69,8 @@ "libuv", "napi", "samgr", - "resource_schedule_service" + "resource_schedule_service", + "runtime_core" ], "third_party": [] }, @@ -81,7 +82,15 @@ "//foundation/communication/bluetooth/frameworks/inner:btcommon", "//foundation/communication/bluetooth/frameworks/js/napi:bluetooth_napi", "//foundation/communication/bluetooth/frameworks/c_api:bluetooth_ndk", - "//foundation/communication/bluetooth/frameworks/cj:bluetooth_ffi" + "//foundation/communication/bluetooth/frameworks/cj:bluetooth_ffi", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_a2dp:bluetoothA2dp_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_access:bluetoothAccess_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_baseProfile:bluetoothBaseProfile_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_ble:bluetoothBle_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_connection:bluetoothConnection_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_constant:bluetoothConstant_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_hfp:bluetoothHfp_taihe", + "//foundation/communication/bluetooth/frameworks/ets/taihe/bluetooth_hid:bluetoothHid_taihe" ], "service_group": [ ] diff --git a/frameworks/ets/taihe/bluetooth_a2dp/BUILD.gn b/frameworks/ets/taihe/bluetooth_a2dp/BUILD.gn new file mode 100644 index 00000000..67e6c1aa --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_a2dp/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_a2dp_taihe") { + sources = [ + "idl/ohos.bluetooth.a2dp.taihe", + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_baseProfile:bluetooth_baseProfile_taihe" + ] +} + +subsystem_name = "communication" +part_name = "bluetooth" +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_a2dp_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.a2dp.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.a2dp.abi.c", + ] +} + +taihe_shared_library("bluetoothA2dp_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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.a2dp.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + "../bluetooth_baseProfile:bluetoothBaseProfile_taihe_native" + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothA2dp_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.bluetooth.a2dp.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothA2dp_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("bluetoothA2dp_etc") { + source = "$target_out_dir/bluetoothA2dp_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothA2dp_abc" ] +} + +group("bluetoothA2dp_taihe") { + deps = [ + ":bluetoothA2dp_etc", + ":bluetoothA2dp_taihe_native" + ] + # deps = [":run_taihe"] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_a2dp/idl/ohos.bluetooth.a2dp.taihe b/frameworks/ets/taihe/bluetooth_a2dp/idl/ohos.bluetooth.a2dp.taihe new file mode 100644 index 00000000..48399c4b --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_a2dp/idl/ohos.bluetooth.a2dp.taihe @@ -0,0 +1,25 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.a2dp", "a2dp") +@!sts_inject(""" +static { loadLibrary("bluetoothA2dp_taihe_native.z") } +""") + +from ohos.bluetooth.baseProfile use BaseProfile; +interface A2dpSourceProfile : BaseProfile { +} + +function CreateA2dpSrcProfile(): A2dpSourceProfile; diff --git a/frameworks/ets/taihe/bluetooth_a2dp/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_a2dp/src/ani_constructor.cpp new file mode 100644 index 00000000..a431d8ca --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_a2dp/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.bluetooth.a2dp.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::bluetooth::a2dp::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::a2dp::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_a2dp/src/ohos.bluetooth.a2dp.impl.cpp b/frameworks/ets/taihe/bluetooth_a2dp/src/ohos.bluetooth.a2dp.impl.cpp new file mode 100644 index 00000000..a72c5346 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_a2dp/src/ohos.bluetooth.a2dp.impl.cpp @@ -0,0 +1,52 @@ +/* + * 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.bluetooth.a2dp.proj.hpp" +#include "ohos.bluetooth.a2dp.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +using namespace taihe; +using namespace ohos::bluetooth::a2dp; + +namespace { +// To be implemented. + +class A2dpSourceProfileImpl { +public: + A2dpSourceProfileImpl() + { + // Don't forget to implement the constructor. + } + + void OnConnectionStateChange( + callback_view callback) + { + std::cout << "OnConnectionStateChange" << std::endl; + } +}; + +A2dpSourceProfile CreateA2dpSrcProfile() +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_CreateA2dpSrcProfile(CreateA2dpSrcProfile); +// NOLINTEND diff --git a/frameworks/ets/taihe/bluetooth_access/BUILD.gn b/frameworks/ets/taihe/bluetooth_access/BUILD.gn new file mode 100644 index 00000000..e9afb927 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_access/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_access_taihe") { + sources = [ + "idl/ohos.bluetooth.access.taihe" + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_baseProfile:bluetooth_baseProfile_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_access_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.access.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.access.abi.c", + ] +} + +taihe_shared_library("bluetoothAccess_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.access.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothAccess_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.access.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothAccess_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothAccess_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothAccess_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothAccess_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothAccess_taihe") { + deps = [ + ":bluetoothAccess_etc", + ":bluetoothAccess_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_access/idl/ohos.bluetooth.access.taihe b/frameworks/ets/taihe/bluetooth_access/idl/ohos.bluetooth.access.taihe new file mode 100644 index 00000000..fd5de130 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_access/idl/ohos.bluetooth.access.taihe @@ -0,0 +1,44 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.access", "access") +@!sts_inject(""" +static { loadLibrary("bluetoothAccess_taihe_native.z") } +""") + +from ohos.bluetooth.baseProfile use BaseProfile; + +enum BluetoothState: i32 { + STATE_OFF = 0, + STATE_TURNING_ON = 1, + STATE_ON = 2, + STATE_TURNING_OFF = 3, + STATE_BLE_TURNING_ON = 4, + STATE_BLE_ON = 5, + STATE_BLE_TURNING_OFF = 6 +} + +@on_off("stateChange") +function OnStateChange(callback: (a: BluetoothState) => void): void; +@on_off("stateChange") +function OffStateChange(callback:Optional<(a: BluetoothState)=> void>): void; + +@gen_promise("restrictBluetooth") +function RestrictBluetoothSync(): void; + +function GetState(): BluetoothState; + +function EnableBluetooth(): void; +function DisableBluetooth(): void; \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_access/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_access/src/ani_constructor.cpp new file mode 100644 index 00000000..481b00a9 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_access/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.bluetooth.access.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::bluetooth::access::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::access::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_access/src/ohos.bluetooth.access.impl.cpp b/frameworks/ets/taihe/bluetooth_access/src/ohos.bluetooth.access.impl.cpp new file mode 100644 index 00000000..c851a756 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_access/src/ohos.bluetooth.access.impl.cpp @@ -0,0 +1,83 @@ +/* + * 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.bluetooth.access.proj.hpp" +#include "ohos.bluetooth.access.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "bluetooth_remote_device.h" +#include "bluetooth_host.h" +#include "bluetooth_log.h" +#include "bluetooth_errorcode.h" + +using namespace taihe; +using namespace ohos::bluetooth::access; + +namespace { +// To be implemented. + +void OnStateChange(callback_view callback) +{ + std::cout << "OnStateChange" << std::endl; +} + +void OffStateChange(optional_view> callback) +{ + std::cout << "OffStateChange" << std::endl; +} + +void RestrictBluetoothSync() +{ + std::cout << "RestrictBluetoothSync" << std::endl; + int32_t ret = OHOS::Bluetooth::BluetoothHost::GetDefaultHost().RestrictBluetooth(); + if (ret == OHOS::Bluetooth::BT_ERR_INTERNAL_ERROR) { + std::cerr << "RestrictBluetoothSync failed." << std::endl; + } +} + +BluetoothState GetState() +{ + int32_t state = static_cast(OHOS::Bluetooth::BluetoothHost::GetDefaultHost().GetBluetoothState()); + return static_cast(state); +} + +void EnableBluetooth() +{ + OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost(); + int32_t ret = host->EnableBle(); + if (ret != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr << "enableBluetooth failed." << std::endl; + } +} + +void DisableBluetooth() +{ + OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost(); + int ret = host->DisableBt(); + if (ret != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr << "disableBluetooth failed." << std::endl; + } +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_OnStateChange(OnStateChange); +TH_EXPORT_CPP_API_OffStateChange(OffStateChange); +TH_EXPORT_CPP_API_RestrictBluetoothSync(RestrictBluetoothSync); +TH_EXPORT_CPP_API_GetState(GetState); +TH_EXPORT_CPP_API_EnableBluetooth(EnableBluetooth); +TH_EXPORT_CPP_API_DisableBluetooth(DisableBluetooth); +// NOLINTEND diff --git a/frameworks/ets/taihe/bluetooth_baseProfile/BUILD.gn b/frameworks/ets/taihe/bluetooth_baseProfile/BUILD.gn new file mode 100644 index 00000000..69a23c50 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_baseProfile/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_baseProfile_taihe") { + sources = [ + "idl/ohos.bluetooth.baseProfile.taihe", + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_constant:bluetooth_constant_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_baseProfile_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.baseProfile.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.baseProfile.abi.c", + ] +} + +taihe_shared_library("bluetoothBaseProfile_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.baseProfile.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothBaseProfile_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.baseProfile.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothBaseProfile_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothBaseProfile_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothBaseProfile_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothBaseProfile_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothBaseProfile_taihe") { + deps = [ + ":bluetoothBaseProfile_etc", + ":bluetoothBaseProfile_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_baseProfile/idl/ohos.bluetooth.baseProfile.taihe b/frameworks/ets/taihe/bluetooth_baseProfile/idl/ohos.bluetooth.baseProfile.taihe new file mode 100644 index 00000000..a99925de --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_baseProfile/idl/ohos.bluetooth.baseProfile.taihe @@ -0,0 +1,41 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.baseProfile", "baseProfile") +@!sts_inject(""" +static { loadLibrary("bluetoothBaseProfile_taihe_native.z") } +""") + +from ohos.bluetooth.constant use ProfileConnectionState; + +struct StateChangeParam { + deviceId: String; + state: ProfileConnectionState; + cause: DisconnectCause; +} + +enum DisconnectCause: i32{ + USER_DISCONNECT = 0, + CONNECT_FROM_KEYBOARD = 1, + CONNECT_FROM_MOUSE = 2, + CONNECT_FROM_CAR = 3, + TOO_MANY_CONNECTED_DEVICES = 4, + CONNECT_FAIL_INTERNAL = 5 +} + +interface BaseProfile { + @on_off("connectionStateChange") + OnConnectionStateChange(callback: (a: StateChangeParam) => void): void; +} diff --git a/frameworks/ets/taihe/bluetooth_baseProfile/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_baseProfile/src/ani_constructor.cpp new file mode 100644 index 00000000..3069a5ae --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_baseProfile/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.bluetooth.baseProfile.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::bluetooth::baseProfile::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::baseProfile::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_baseProfile/src/ohos.bluetooth.baseProfile.impl.cpp b/frameworks/ets/taihe/bluetooth_baseProfile/src/ohos.bluetooth.baseProfile.impl.cpp new file mode 100644 index 00000000..ab6ba147 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_baseProfile/src/ohos.bluetooth.baseProfile.impl.cpp @@ -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. + */ + +#include "ohos.bluetooth.baseProfile.proj.hpp" +#include "ohos.bluetooth.baseProfile.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +using namespace taihe; +using namespace ohos::bluetooth::baseProfile; + +namespace { +// To be implemented. + +class BaseProfileImpl { +public: + BaseProfileImpl() + { + // Don't forget to implement the constructor. + } + + void OnConnectionStateChange(callback_view callback) + { + std::cout<< "OnConnectionStateChange" << std::endl; + } +}; +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +// NOLINTEND diff --git a/frameworks/ets/taihe/bluetooth_ble/BUILD.gn b/frameworks/ets/taihe/bluetooth_ble/BUILD.gn new file mode 100644 index 00000000..6c899a87 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_ble/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_ble_taihe") { + sources = [ + "idl/ohos.bluetooth.ble.taihe", + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_constant:bluetooth_constant_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_ble_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.ble.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.ble.abi.c", + ] +} + +taihe_shared_library("bluetoothBle_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.ble.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothBle_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.ble.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothBle_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothBle_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothBle_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothBle_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothBle_taihe") { + deps = [ + ":bluetoothBle_etc", + ":bluetoothBle_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe b/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe new file mode 100644 index 00000000..6fc6d94c --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe @@ -0,0 +1,330 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.ble", "ble") +@!sts_inject(""" +static { loadLibrary("bluetoothBle_taihe_native.z") } +""") +from ohos.bluetooth.constant use ProfileConnectionState; + +struct ScanReport { + reportType: ScanReportType; + scanResult: Array; +} +enum ScanReportType: i32 { + ON_FOUND = 1, + ON_LOST = 2, +} +struct ScanResult { + deviceId: String; + rssi: i32; + data: @arraybuffer Array; + deviceName: String; + connectable: bool; +} +@on_off("BLEDeviceFind") +function OnBLEDeviceFind(callback: (a: ScanReport) => void): void; +@on_off("BLEDeviceFind") +function OffBLEDeviceFind(callback:Optional<(a: ScanReport)=> void>): void; + +@on_off("advertisingStateChange") +function OnAdvertisingStateChange(callback: (a: AdvertisingStateChangeInfo) => void): void; +@on_off("advertisingStateChange") +function OffAdvertisingStateChange(callback:Optional<(a: AdvertisingStateChangeInfo)=> void>): void; + + +enum MatchMode: i32 { + MATCH_MODE_AGGRESSIVE = 1, + MATCH_MODE_STICKY = 2 +} + +struct AdvertisingEnableParams { + advertisingId: i32; + duration: Optional; +} + + +struct DescriptorWriteRequest { + deviceId: String; + transId: i32; + offset: i32; + isPrepared: bool; + needRsp: bool; + value: @arraybuffer Array; + descriptorUuid: String; + characteristicUuid: String; + serviceUuid: String; +} + +struct DescriptorReadRequest { + deviceId: String; + transId: i32; + offset: i32; + descriptorUuid: String; + characteristicUuid: String; + serviceUuid: String; +} + +struct AdvertiseSetting { + interval: Optional; + txPower: Optional; + connectable: Optional; +} + +struct NotifyCharacteristic { + serviceUuid: String; + characteristicUuid: String; + characteristicValue: @arraybuffer Array; + confirm: bool; +} + +struct ScanFilter { + deviceId: Optional; + name: Optional; + serviceUuid: Optional; + serviceUuidMask: Optional; + serviceSolicitationUuid: Optional; + serviceSolicitationUuidMask: Optional; + serviceData: Optional<@arraybuffer Array>; + serviceDataMask: Optional<@arraybuffer Array>; + manufactureId: Optional; + manufactureData: Optional<@arraybuffer Array>; + manufactureDataMask: Optional<@arraybuffer Array>; +} + +struct CharacteristicReadRequest { + deviceId: String; + transId: i32; + offset: i32; + characteristicUuid: String; + serviceUuid: String; +} + +struct CharacteristicWriteRequest { + deviceId: String; + transId: i32; + offset: i32; + isPrepared: bool; + needRsp: bool; + value: @arraybuffer Array; + characteristicUuid: String; + serviceUuid: String; +} + + +function CreateGattClientDevice(deviceId: String): GattClientDevice; + +function StopBLEScan(): void; + +struct ServerResponse { + deviceId: String; + transId: i32; + status: i32; + offset: i32; + value: @arraybuffer Array; +} + +struct ScanOptions { + interval: Optional; + dutyMode: Optional; + matchMode: Optional; + phyType: Optional; + reportMode: Optional; +} + +enum ScanDuty: i32 { + SCAN_MODE_LOW_POWER = 0, + SCAN_MODE_BALANCED = 1, + SCAN_MODE_LOW_LATENCY = 2 +} + +enum PhyType: i32 { + PHY_LE_1M = 1, + PHY_LE_ALL_SUPPORTED = 255 +} + +enum ScanReportMode: i32 { + NORMAL = 1, + FENCE_SENSITIVITY_LOW = 10, + FENCE_SENSITIVITY_HIGH = 11, +} + +struct BLECharacteristic { + serviceUuid: String; + characteristicUuid: String; + characteristicValue: @arraybuffer Array; + descriptors: Array; + properties: Optional; + characteristicValueHandle: Optional; +} + +struct AdvertisingStateChangeInfo { + advertisingId: i32; + state: AdvertisingState; +} + +enum AdvertisingState: i32 { + STARTED = 1, + ENABLED = 2, + DISABLED = 3, + STOPPED = 4 +} + +struct BLEDescriptor { + serviceUuid: String; + characteristicUuid: String; + descriptorUuid: String; + descriptorValue: @arraybuffer Array; + descriptorHandle: Optional; +} + +struct GattProperties { + write: Optional; + writeNoResponse: Optional; + read: Optional; + notify: Optional; + indicate: Optional; +} + +enum GattWriteType: i32 { + WRITE = 1, + WRITE_NO_RESPONSE = 2 +} + +function StopAdvertising(): void; + +@gen_async("stopAdvertising") +@gen_promise("stopAdvertising") +function StopAdvertisingSync(advertisingId: f64): void; + +interface GattClientDevice { + @on_off("BLECharacteristicChange") + OnBLECharacteristicChange(callback: (a: BLECharacteristic) => void): void; + @on_off("BLECharacteristicChange") + OffBLECharacteristicChange(callback:Optional<(a: BLECharacteristic)=> void>): void; + + @gen_async("setCharacteristicChangeNotification") + @gen_promise("setCharacteristicChangeNotification") + SetCharacteristicChangeNotificationSync(characteristic: BLECharacteristic, enable: bool): void; + + @gen_async("getRssiValue") + @gen_promise("getRssiValue") + GetRssiValueSync(): f64; + + @gen_async("writeCharacteristicValue") + @gen_promise("writeCharacteristicValue") + WriteCharacteristicValueSync(characteristic: BLECharacteristic, writeType: GattWriteType): void; + + @gen_async("getServices") + @gen_promise("getServices") + GetServicesSync(): void; + + @gen_async("writeDescriptorValue") + @gen_promise("writeDescriptorValue") + WriteDescriptorValueSync(descriptor: BLEDescriptor): void; + + @on_off("BLEMtuChange") + OnBLEMtuChange(callback: (a: i32) => void): void; + @on_off("BLEMtuChange") + OffBLEMtuChange(callback:Optional<(a: i32)=> void>): void; + + @on_off("BLEConnectionStateChange") + OnBLEConnectionStateChange(callback: (a: BLEConnectionChangeState) => void): void; + @on_off("BLEConnectionStateChange") + OffBLEConnectionStateChange(callback:Optional<(a: BLEConnectionChangeState)=> void>): void; + + SetBLEMtuSize(mtu: f64): void; + Connect(): void; + Disconnect(): void; + + Close(): void; +} + +function CreateGattServer(): GattServer; + +interface GattServer { + @on_off("characteristicRead") + OnCharacteristicRead(callback: (a: CharacteristicReadRequest) => void): void; + @on_off("characteristicRead") + OffCharacteristicRead(callback:Optional<(a: CharacteristicReadRequest)=> void>): void; + + @on_off("BLEMtuChange") + OnBLEMtuChange(callback: (a: i32) => void): void; + @on_off("BLEMtuChange") + OffBLEMtuChange(callback:Optional<(a: i32)=> void>): void; + + @on_off("descriptorRead") + OnDescriptorRead(callback: (a: DescriptorReadRequest) => void): void; + @on_off("descriptorRead") + OffDescriptorRead(callback:Optional<(a: DescriptorReadRequest)=> void>): void; + + @on_off("descriptorWrite") + OnDescriptorWrite(callback: (a: DescriptorWriteRequest) => void): void; + @on_off("descriptorWrite") + OffDescriptorWrite(callback:Optional<(a: DescriptorWriteRequest)=> void>): void; + + @on_off("connectionStateChange") + OnConnectionStateChange(callback: (a: BLEConnectionChangeState) => void): void; + @on_off("connectionStateChange") + OffConnectionStateChange(callback:Optional<(a: BLEConnectionChangeState)=> void>): void; + + Close(): void; +} + +struct AdvertiseData { + serviceUuids: Array; + manufactureData: Array; + serviceData: Array; + includeDeviceName: Optional; + includeTxPower: Optional; +} + +struct AdvertisingParams { + advertisingSettings: AdvertiseSetting; + advertisingData: AdvertiseData; + advertisingResponse: Optional; + duration: Optional; +} +struct ManufactureData { + manufactureId: i16; + manufactureValue: @arraybuffer Array; +} + +struct ServiceData { + serviceUuid: String; + serviceValue: @arraybuffer Array; +} + +struct GattService { + serviceUuid: String; + isPrimary: bool; + characteristics: Array; + includeServices: Optional>; +} + +struct BLEConnectionChangeState { + deviceId: String; + state: ProfileConnectionState; +} + +function CreateBleScanner(): BleScanner; + +interface BleScanner { + @on_off("BLEDeviceFind") + OnBLEDeviceFind(callback: (a: ScanReport) => void): void; + @on_off("BLEDeviceFind") + OffBLEDeviceFind(callback:Optional<(a: ScanReport)=> void>): void; +} + diff --git a/frameworks/ets/taihe/bluetooth_ble/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_ble/src/ani_constructor.cpp new file mode 100644 index 00000000..47d1641f --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_ble/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.bluetooth.ble.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::bluetooth::ble::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::ble::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp b/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp new file mode 100644 index 00000000..659331e8 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp @@ -0,0 +1,261 @@ +/* + * 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.bluetooth.ble.proj.hpp" +#include "ohos.bluetooth.ble.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "bluetooth_ble_advertiser.h" +#include "bluetooth_ble_central_manager.h" +#include "bluetooth_errorcode.h" +#include "bluetooth_utils.h" + +using namespace taihe; +using namespace ohos::bluetooth::ble; + +namespace { +// To be implemented. + +class GattClientDeviceImpl { +public: + void OnBLECharacteristicChange(callback_view callback) + { + std::cout << "OnBLECharacteristicChange" << std::endl; + } + + void OffBLECharacteristicChange(optional_view> callback) + { + std::cout << "OffBLECharacteristicChange" << std::endl; + } + + void SetCharacteristicChangeNotificationSync(BLECharacteristic const& characteristic, bool enable) + { + std::cout << "SetCharacteristicChangeNotificationSync" << std::endl; + } + + double GetRssiValueSync() + { + std::cout << "GetRssiValueSync" << std::endl; + return 1; + } + + void WriteCharacteristicValueSync(BLECharacteristic const& characteristic, GattWriteType writeType) + { + std::cout << "WriteCharacteristicValueSync" << std::endl; + } + + void GetServicesSync() + { + std::cout << "GetServicesSync" << std::endl; + } + + void WriteDescriptorValueSync(BLEDescriptor const& descriptor) + { + std::cout << "WriteDescriptorValueSync" << std::endl; + } + + void OnBLEMtuChange(callback_view callback) + { + std::cout << "OnBLEMtuChange" << std::endl; + } + + void OffBLEMtuChange(optional_view> callback) + { + std::cout << "OffBLEMtuChange" << std::endl; + } + + void OnBLEConnectionStateChange(callback_view callback) + { + std::cout << "OnBLEConnectionStateChange" << std::endl; + } + + void OffBLEConnectionStateChange(optional_view> callback) + { + std::cout << "OffBLEConnectionStateChange" << std::endl; + } + + void SetBLEMtuSize(double mtu) + { + std::cout << "SetBLEMtuSize" << std::endl; + } + + void Connect() + { + std::cout << "Connect" << std::endl; + } + + void Disconnect() + { + std::cout << "Disconnect" << std::endl; + } + + void Close() + { + std::cout << "Close GattClientDevice" << std::endl; + } +}; + +class GattServerImpl { +public: + GattServerImpl() + { + // Don't forget to implement the constructor. + } + + void OnCharacteristicRead(callback_view callback) + { + std::cout << "OnCharacteristicRead GattServer" << std::endl; + } + + void OffCharacteristicRead(optional_view> callback) + { + std::cout << "OnCharacteristicRead GattServer" << std::endl; + } + + void OnBLEMtuChange(callback_view callback) + { + std::cout << "OnBLEMtuChange GattServer" << std::endl; + } + + void OffBLEMtuChange(optional_view> callback) + { + std::cout << "OffBLEMtuChange GattServer" << std::endl; + } + + void OnDescriptorRead(callback_view callback) + { + std::cout << "OnDescriptorRead GattServer" << std::endl; + } + + void OffDescriptorRead(optional_view> callback) + { + std::cout << "OffDescriptorRead GattServer" << std::endl; + } + + void OnDescriptorWrite(callback_view callback) + { + std::cout << "OnDescriptorWrite GattServer" << std::endl; + } + + void OffDescriptorWrite(optional_view> callback) + { + std::cout << "OffDescriptorWrite GattServer" << std::endl; + } + + void OnConnectionStateChange(callback_view callback) + { + std::cout << "OnConnectionStateChange GattServer" << std::endl; + } + + void OffConnectionStateChange(optional_view> callback) + { + std::cout << "OffConnectionStateChange GattServer" << std::endl; + } + + void Close() + { + std::cout << "Close GattServer" << std::endl; + } +}; + +class BleScannerImpl { +public: + BleScannerImpl() + { + // Don't forget to implement the constructor. + } + + void OnBLEDeviceFind(callback_view callback) + { + std::cout << "BleScanner onBLEDeviceFind" << std::endl; + } + + void OffBLEDeviceFind(optional_view> callback) + { + std::cout << "BleScanner offBLEDeviceFind" << std::endl; + } +}; + +void OnBLEDeviceFind(callback_view callback) +{ + std::cout << "OnBLEDeviceFind" << std::endl; +} + +void OffBLEDeviceFind(optional_view> callback) +{ + std::cout << "OffBLEDeviceFind" << std::endl; +} + +void OnAdvertisingStateChange(callback_view callback) +{ + std::cout << "OnAdvertisingStateChange" << std::endl; +} + +void OffAdvertisingStateChange(optional_view> callback) +{ + std::cout << "OffAdvertisingStateChange" << std::endl; +} + +void StopAdvertising() +{ + +} + +void StopAdvertisingSync(double advertisingId) +{ + std::cout << "StopAdvertisingSync" << std::endl; +} + +void StopBLEScan() +{ + std::cout << "StopBLEScan" << std::endl; +} + +GattClientDevice CreateGattClientDevice(string_view deviceId) +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} + +GattServer CreateGattServer() +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} + +BleScanner CreateBleScanner() +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_OnBLEDeviceFind(OnBLEDeviceFind); +TH_EXPORT_CPP_API_OffBLEDeviceFind(OffBLEDeviceFind); +TH_EXPORT_CPP_API_OnAdvertisingStateChange(OnAdvertisingStateChange); +TH_EXPORT_CPP_API_OffAdvertisingStateChange(OffAdvertisingStateChange); +TH_EXPORT_CPP_API_CreateGattClientDevice(CreateGattClientDevice); +TH_EXPORT_CPP_API_CreateGattServer(CreateGattServer); +TH_EXPORT_CPP_API_CreateBleScanner(CreateBleScanner); +TH_EXPORT_CPP_API_StopAdvertising(StopAdvertising); +TH_EXPORT_CPP_API_StopAdvertisingSync(StopAdvertisingSync); +TH_EXPORT_CPP_API_StopBLEScan(StopBLEScan); +// NOLINTEND + diff --git a/frameworks/ets/taihe/bluetooth_connection/BUILD.gn b/frameworks/ets/taihe/bluetooth_connection/BUILD.gn new file mode 100644 index 00000000..accf9648 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_connection/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_connection_taihe") { + sources = [ + "idl/ohos.bluetooth.connection.taihe" + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_constant:bluetooth_constant_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_connection_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.connection.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.connection.abi.c", + ] +} + +taihe_shared_library("bluetoothConnection_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.connection.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothConnection_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.connection.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothConnection_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothConnection_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothConnection_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothConnection_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothConnection_taihe") { + deps = [ + ":bluetoothConnection_etc", + ":bluetoothConnection_taihe_native", + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_connection/idl/ohos.bluetooth.connection.taihe b/frameworks/ets/taihe/bluetooth_connection/idl/ohos.bluetooth.connection.taihe new file mode 100644 index 00000000..0e82edea --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_connection/idl/ohos.bluetooth.connection.taihe @@ -0,0 +1,168 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.connection", "connection") +@!sts_inject(""" +static { loadLibrary("bluetoothConnection_taihe_native.z") } +""") +from ohos.bluetooth.constant use MajorClass, MajorMinorClass; + +enum BluetoothTransport: i32 { + TRANSPORT_BR_EDR = 0, + TRANSPORT_LE = 1 +} + +enum ScanMode: i32 { + SCAN_MODE_NONE = 0, + SCAN_MODE_CONNECTABLE = 1, + SCAN_MODE_GENERAL_DISCOVERABLE = 2, + SCAN_MODE_LIMITED_DISCOVERABLE = 3, + SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE = 4, + SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE = 5 +} + +struct DeviceClass { + majorClass: MajorClass; + majorMinorClass: MajorMinorClass; + classOfDevice: i32; +} + +struct DiscoveryResult { + deviceId: String; + rssi: i32; + deviceName: String; + deviceClass: DeviceClass; +} + +struct PinRequiredParam { + deviceId: String; + pinCode: String; + pinType: PinType; +} + +enum PinType: i32 { + PIN_TYPE_ENTER_PIN_CODE = 0, + PIN_TYPE_ENTER_PASSKEY = 1, + PIN_TYPE_CONFIRM_PASSKEY = 2, + PIN_TYPE_NO_PASSKEY_CONSENT = 3, + PIN_TYPE_NOTIFY_PASSKEY = 4, + PIN_TYPE_DISPLAY_PIN_CODE = 5, + PIN_TYPE_OOB_CONSENT = 6, + PIN_TYPE_PIN_16_DIGITS = 7 +} + +struct BatteryInfo { + deviceId: String; + batteryLevel: i32; + leftEarBatteryLevel: i32; + leftEarChargeState: DeviceChargeState; + rightEarBatteryLevel: i32; + deviceClass: DeviceClass; + rightEarChargeState: DeviceChargeState; + boxBatteryLevel: i32; + boxChargeState: DeviceChargeState; +} + +enum DeviceChargeState: i32 { + DEVICE_NORMAL_CHARGE_NOT_CHARGED = 0, + DEVICE_NORMAL_CHARGE_IN_CHARGING = 1, + DEVICE_SUPER_CHARGE_NOT_CHARGED = 2, + DEVICE_SUPER_CHARGE_IN_CHARGING = 3 +} + +struct BondStateParam { + deviceId: String; + state: BondState; + cause: UnbondCause; +} + +enum BondState: i32 { + BOND_STATE_INVALID = 0, + BOND_STATE_BONDING = 1, + BOND_STATE_BONDED = 2 +} + +enum UnbondCause: i32 { + USER_REMOVED = 0, + REMOTE_DEVICE_DOWN = 1, + AUTH_FAILURE = 2, + AUTH_REJECTED = 3, + INTERNAL_ERROR = 4 +} + +struct ControlDeviceActionParams { + deviceId: String; + type: ControlType; + typeValue: ControlTypeValue; + controlObject: ControlObject; +} + +enum ControlType: i32 { + PLAY = 0, + VIBRATE = 1, + FLASH = 2, + LOCK = 3, + ERASE = 4, +} + +enum ControlTypeValue: i32 { + DISABLE = 0, + ENABLE = 1, + QUERY = 2, +} + +enum ControlObject: i32 { + LEFT_EAR = 0, + RIGHT_EAR = 1, + LEFT_RIGHT_EAR = 2, +} + +function GetRemoteProductId(deviceId: String): String; + +@overload("getRemoteDeviceName") +function GetRemoteDeviceName_two(deviceId: String, alias: Optional): String; +@overload("getRemoteDeviceName") +function GetRemoteDeviceName_one(deviceId: String): String; + +function GetPairedDevices(): Array; + +function SetBluetoothScanMode(mode: ScanMode, duration: f64): void; + +@on_off("discoveryResult") +function OnDiscoveryResult(callback: (a: Array) => void): void; +@on_off("discoveryResult") +function OffDiscoveryResult(callback:Optional<(a: Array)=> void>): void; + +@on_off("batteryChange") +function OnBatteryChange(callback: (a: BatteryInfo) => void): void; +@on_off("batteryChange") +function OffBatteryChange(callback:Optional<(a: BatteryInfo)=> void>): void; + +@on_off("bluetoothDeviceFind") +function OnBluetoothDeviceFind(callback: (a: Array) => void): void; +@on_off("bluetoothDeviceFind") +function OffBluetoothDeviceFind(callback:Optional<(a: Array)=> void>): void; + +@on_off("bondStateChange") +function OnBondStateChange(callback: (a: BondStateParam) => void): void; +@on_off("bondStateChange") +function OffBondStateChange(callback:Optional<(a: BondStateParam)=> void>): void; + +@on_off("pinRequired") +function OnPinRequired(callback: (a: PinRequiredParam) => void): void; +@on_off("pinRequired") +function OffPinRequired(callback:Optional<(a: PinRequiredParam)=> void>): void; + +function GetRemoteDeviceClass(deviceId: String): DeviceClass; diff --git a/frameworks/ets/taihe/bluetooth_connection/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_connection/src/ani_constructor.cpp new file mode 100644 index 00000000..b29f0783 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_connection/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.bluetooth.connection.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::bluetooth::connection::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::connection::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_connection/src/ohos.bluetooth.connection.impl.cpp b/frameworks/ets/taihe/bluetooth_connection/src/ohos.bluetooth.connection.impl.cpp new file mode 100644 index 00000000..dd4427a2 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_connection/src/ohos.bluetooth.connection.impl.cpp @@ -0,0 +1,185 @@ +/* + * 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.bluetooth.connection.proj.hpp" +#include "ohos.bluetooth.connection.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "bluetooth_remote_device.h" +#include "bluetooth_host.h" +#include "bluetooth_log.h" +#include "bluetooth_errorcode.h" + +using namespace taihe; +using namespace ohos::bluetooth::connection; + +namespace { + +string GetRemoteProductId(string_view deviceId) +{ + // 数据类型转换 + std::string remoteAddr = std::string(deviceId); + // 业务逻辑实现 + OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr); + std::string productId = "invalid"; + int32_t err = remoteDevice.GetDeviceProductId(productId); + if (err != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "getRemoteProductId failed." << std::endl; + } + // 返回值 + return productId; +} + +string GetRemoteDeviceName_one(string_view deviceId) +{ + std::string remoteAddr = std::string(deviceId); + std::string name = OHOS::Bluetooth::INVALID_NAME; + bool deviceAlias = false; + + OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr); + int32_t err = remoteDevice.GetDeviceName(name, deviceAlias); + if (err != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "getRemoteDeviceName failed." << std::endl; + } + return name; +} + +string GetRemoteDeviceName_two(string_view deviceId, optional_view alias) +{ + std::string remoteAddr = std::string(deviceId); + std::string name = OHOS::Bluetooth::INVALID_NAME; + bool deviceAlias = bool(alias); + + OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr); + int32_t err = remoteDevice.GetDeviceName(name, deviceAlias); + if (err != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "getRemoteDeviceName failed." << std::endl; + } + return name; +} + +array GetPairedDevices() +{ + OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost(); + std::vector remoteDeviceLists; + std::vector dstDevicesvec; + int32_t ret = host->GetPairedDevices(OHOS::Bluetooth::BT_TRANSPORT_BREDR, remoteDeviceLists); + if (ret != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "getPairedDevices failed." << std::endl; + } + for (auto vec : remoteDeviceLists) { + dstDevicesvec.push_back(vec.GetDeviceAddr().c_str()); + } + std::cout<< "getPairedDevices" << std::endl; + array result(taihe::copy_data_t{}, dstDevicesvec.data(), dstDevicesvec.size()); + return result; +} + +void SetBluetoothScanMode(ScanMode mode, double duration) +{ + OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost(); + int32_t ret = host->SetBtScanMode(mode, duration); + if (ret != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "setBluetoothScanMode failed." << std::endl; + return; + } + host->SetBondableMode(OHOS::Bluetooth::BT_TRANSPORT_BREDR, 1); +} + +void OnDiscoveryResult(callback_view)> callback) +{ + std::cout<< "onDiscoveryResult" << std::endl; +} + +void OffDiscoveryResult(optional_view)>> callback) +{ + std::cout<< "offDiscoveryResult" << std::endl; +} + +void OnBatteryChange(callback_view callback) +{ + std::cout<< "onBatteryChange" << std::endl; +} + +void OffBatteryChange(optional_view> callback) +{ + std::cout<< "offBatteryChange" << std::endl; +} + +void OnBluetoothDeviceFind(callback_view)> callback) +{ + std::cout<< "onBluetoothDeviceFind" << std::endl; +} + +void OffBluetoothDeviceFind(optional_view)>> callback) +{ + std::cout<< "offBluetoothDeviceFind" << std::endl; +} + +void OnBondStateChange(callback_view callback) +{ + std::cout<< "onBondStateChange" << std::endl; +} + +void OffBondStateChange(optional_view> callback) +{ + std::cout<< "offBondStateChange" << std::endl; +} + +void OnPinRequired(callback_view callback) +{ + std::cout<< "onPinRequired" << std::endl; +} + +void OffPinRequired(optional_view> callback) +{ + std::cout<< "offPinRequired" << std::endl; +} + +DeviceClass GetRemoteDeviceClass(string_view deviceId) +{ + std::string remoteAddr = std::string(deviceId); + OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr); + int tmpCod = int(ohos::bluetooth::constant::MajorClass::key_t::MAJOR_UNCATEGORIZED); + int tmpMajorClass = int(ohos::bluetooth::constant::MajorClass::key_t::MAJOR_UNCATEGORIZED); + int tmpMajorMinorClass = int(ohos::bluetooth::constant::MajorClass::key_t::MAJOR_UNCATEGORIZED); + int32_t err = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass); + if (err != OHOS::Bluetooth::BT_NO_ERROR) { + std::cerr<< "getRemoteDeviceClass failed." << std::endl; + } + return {static_cast(tmpMajorClass), + static_cast(tmpMajorMinorClass), tmpCod}; +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_GetRemoteProductId(GetRemoteProductId); +TH_EXPORT_CPP_API_GetRemoteDeviceName_one(GetRemoteDeviceName_one); +TH_EXPORT_CPP_API_GetRemoteDeviceName_two(GetRemoteDeviceName_two); +TH_EXPORT_CPP_API_GetPairedDevices(GetPairedDevices); +TH_EXPORT_CPP_API_SetBluetoothScanMode(SetBluetoothScanMode); +TH_EXPORT_CPP_API_OnDiscoveryResult(OnDiscoveryResult); +TH_EXPORT_CPP_API_OffDiscoveryResult(OffDiscoveryResult); +TH_EXPORT_CPP_API_OnBatteryChange(OnBatteryChange); +TH_EXPORT_CPP_API_OffBatteryChange(OffBatteryChange); +TH_EXPORT_CPP_API_OnBluetoothDeviceFind(OnBluetoothDeviceFind); +TH_EXPORT_CPP_API_OffBluetoothDeviceFind(OffBluetoothDeviceFind); +TH_EXPORT_CPP_API_OnBondStateChange(OnBondStateChange); +TH_EXPORT_CPP_API_OffBondStateChange(OffBondStateChange); +TH_EXPORT_CPP_API_OnPinRequired(OnPinRequired); +TH_EXPORT_CPP_API_OffPinRequired(OffPinRequired); +TH_EXPORT_CPP_API_GetRemoteDeviceClass(GetRemoteDeviceClass); +// NOLINTEND \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_constant/BUILD.gn b/frameworks/ets/taihe/bluetooth_constant/BUILD.gn new file mode 100644 index 00000000..296eca7a --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_constant/BUILD.gn @@ -0,0 +1,88 @@ +# 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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_constant_taihe") { + sources = [ "idl/ohos.bluetooth.constant.taihe" ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_constant_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.constant.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.constant.abi.c", + ] +} + +taihe_shared_library("bluetoothConstant_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.constant.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothConstant_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.constant.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothConstant_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothConstant_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothConstant_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothConstant_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothConstant_taihe") { + deps = [ + ":bluetoothConstant_etc", + ":bluetoothConstant_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_constant/idl/ohos.bluetooth.constant.taihe b/frameworks/ets/taihe/bluetooth_constant/idl/ohos.bluetooth.constant.taihe new file mode 100644 index 00000000..1b33e638 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_constant/idl/ohos.bluetooth.constant.taihe @@ -0,0 +1,129 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.constant", "constant") +@!sts_inject(""" +static { loadLibrary("bluetoothConstant_taihe_native.z") } +""") + +enum ProfileConnectionState: i32 { + STATE_DISCONNECTED = 0, + STATE_CONNECTING = 1, + STATE_CONNECTED = 2, + STATE_DISCONNECTING = 3 +} + +enum MajorClass: i32 { + MAJOR_MISC = 0x0000, + MAJOR_COMPUTER = 0x0100, + MAJOR_PHONE = 0x0200, + MAJOR_NETWORKING = 0x0300, + MAJOR_AUDIO_VIDEO = 0x0400, + MAJOR_PERIPHERAL = 0x0500, + MAJOR_IMAGING = 0x0600, + MAJOR_WEARABLE = 0x0700, + MAJOR_TOY = 0x0800, + MAJOR_HEALTH = 0x0900, + MAJOR_UNCATEGORIZED = 0x1F00 +} + +enum MajorMinorClass: i32 { + COMPUTER_UNCATEGORIZED = 0x0100, + COMPUTER_DESKTOP = 0x0104, + COMPUTER_SERVER = 0x0108, + COMPUTER_LAPTOP = 0x010C, + COMPUTER_HANDHELD_PC_PDA = 0x0110, + COMPUTER_PALM_SIZE_PC_PDA = 0x0114, + COMPUTER_WEARABLE = 0x0118, + COMPUTER_TABLET = 0x011C, + PHONE_UNCATEGORIZED = 0x0200, + PHONE_CELLULAR = 0x0204, + PHONE_CORDLESS = 0x0208, + PHONE_SMART = 0x020C, + PHONE_MODEM_OR_GATEWAY = 0x0210, + PHONE_ISDN = 0x0214, + NETWORK_FULLY_AVAILABLE = 0x0300, + NETWORK_1_TO_17_UTILIZED = 0x0320, + NETWORK_17_TO_33_UTILIZED = 0x0340, + NETWORK_33_TO_50_UTILIZED = 0x0360, + NETWORK_60_TO_67_UTILIZED = 0x0380, + NETWORK_67_TO_83_UTILIZED = 0x03A0, + NETWORK_83_TO_99_UTILIZED = 0x03C0, + NETWORK_NO_SERVICE = 0x03E0, + AUDIO_VIDEO_UNCATEGORIZED = 0x0400, + AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404, + AUDIO_VIDEO_HANDSFREE = 0x0408, + AUDIO_VIDEO_MICROPHONE = 0x0410, + AUDIO_VIDEO_LOUDSPEAKER = 0x0414, + AUDIO_VIDEO_HEADPHONES = 0x0418, + AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C, + AUDIO_VIDEO_CAR_AUDIO = 0x0420, + AUDIO_VIDEO_SET_TOP_BOX = 0x0424, + AUDIO_VIDEO_HIFI_AUDIO = 0x0428, + AUDIO_VIDEO_VCR = 0x042C, + AUDIO_VIDEO_VIDEO_CAMERA = 0x0430, + AUDIO_VIDEO_CAMCORDER = 0x0434, + AUDIO_VIDEO_VIDEO_MONITOR = 0x0438, + AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C, + AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440, + AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448, + PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500, + PERIPHERAL_KEYBOARD = 0x0540, + PERIPHERAL_POINTING_DEVICE = 0x0580, + PERIPHERAL_KEYBOARD_POINTING = 0x05C0, + PERIPHERAL_UNCATEGORIZED = 0x0500, + PERIPHERAL_JOYSTICK = 0x0504, + PERIPHERAL_GAMEPAD = 0x0508, + PERIPHERAL_REMOTE_CONTROL = 0x05C0, + PERIPHERAL_SENSING_DEVICE = 0x0510, + PERIPHERAL_DIGITIZER_TABLET = 0x0514, + PERIPHERAL_CARD_READER = 0x0518, + PERIPHERAL_DIGITAL_PEN = 0x051C, + PERIPHERAL_SCANNER_RFID = 0x0520, + PERIPHERAL_GESTURAL_INPUT = 0x0522, + IMAGING_UNCATEGORIZED = 0x0600, + IMAGING_DISPLAY = 0x0610, + IMAGING_CAMERA = 0x0620, + IMAGING_SCANNER = 0x0640, + IMAGING_PRINTER = 0x0680, + WEARABLE_UNCATEGORIZED = 0x0700, + WEARABLE_WRIST_WATCH = 0x0704, + WEARABLE_PAGER = 0x0708, + WEARABLE_JACKET = 0x070C, + WEARABLE_HELMET = 0x0710, + WEARABLE_GLASSES = 0x0714, + TOY_UNCATEGORIZED = 0x0800, + TOY_ROBOT = 0x0804, + TOY_VEHICLE = 0x0808, + TOY_DOLL_ACTION_FIGURE = 0x080C, + TOY_CONTROLLER = 0x0810, + TOY_GAME = 0x0814, + HEALTH_UNCATEGORIZED = 0x0900, + HEALTH_BLOOD_PRESSURE = 0x0904, + HEALTH_THERMOMETER = 0x0908, + HEALTH_WEIGHING = 0x090C, + HEALTH_GLUCOSE = 0x0910, + HEALTH_PULSE_OXIMETER = 0x0914, + HEALTH_PULSE_RATE = 0x0918, + HEALTH_DATA_DISPLAY = 0x091C, + HEALTH_STEP_COUNTER = 0x0920, + HEALTH_BODY_COMPOSITION_ANALYZER = 0x0924, + HEALTH_PEAK_FLOW_MONITOR = 0x0928, + HEALTH_MEDICATION_MONITOR = 0x092C, + HEALTH_KNEE_PROSTHESIS = 0x0930, + HEALTH_ANKLE_PROSTHESIS = 0x0934, + HEALTH_GENERIC_HEALTH_MANAGER = 0x0938, + HEALTH_PERSONAL_MOBILITY_DEVICE = 0x093C +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_constant/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_constant/src/ani_constructor.cpp new file mode 100644 index 00000000..fe01bb6b --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_constant/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.bluetooth.constant.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::bluetooth::constant::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::constant::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_constant/src/ohos.bluetooth.constant.impl.cpp b/frameworks/ets/taihe/bluetooth_constant/src/ohos.bluetooth.constant.impl.cpp new file mode 100644 index 00000000..5e5519ef --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_constant/src/ohos.bluetooth.constant.impl.cpp @@ -0,0 +1,30 @@ +/* + * 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.bluetooth.constant.proj.hpp" +#include "ohos.bluetooth.constant.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +using namespace taihe; +using namespace ohos::bluetooth::constant; + +namespace { +// To be implemented. +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +// NOLINTEND diff --git a/frameworks/ets/taihe/bluetooth_hfp/BUILD.gn b/frameworks/ets/taihe/bluetooth_hfp/BUILD.gn new file mode 100644 index 00000000..ddf40553 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hfp/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_hfp_taihe") { + sources = [ + "idl/ohos.bluetooth.hfp.taihe" + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_baseProfile:bluetooth_baseProfile_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_hfp_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.hfp.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.hfp.abi.c", + ] +} + +taihe_shared_library("bluetoothHfp_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.hfp.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + "../bluetooth_baseProfile:bluetoothBaseProfile_taihe_native" + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothHfp_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.hfp.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothHfp_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothHfp_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothHfp_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothHfp_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothHfp_taihe") { + deps = [ + ":bluetoothHfp_etc", + ":bluetoothHfp_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_hfp/idl/ohos.bluetooth.hfp.taihe b/frameworks/ets/taihe/bluetooth_hfp/idl/ohos.bluetooth.hfp.taihe new file mode 100644 index 00000000..eb023a8c --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hfp/idl/ohos.bluetooth.hfp.taihe @@ -0,0 +1,21 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.hfp", "hfp") +from ohos.bluetooth.baseProfile use BaseProfile; +interface HandsFreeAudioGatewayProfile : BaseProfile { +} + +function CreateHfpAgProfile(): HandsFreeAudioGatewayProfile; \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_hfp/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_hfp/src/ani_constructor.cpp new file mode 100644 index 00000000..7113155a --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hfp/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.bluetooth.hfp.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::bluetooth::hfp::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::hfp::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_hfp/src/ohos.bluetooth.hfp.impl.cpp b/frameworks/ets/taihe/bluetooth_hfp/src/ohos.bluetooth.hfp.impl.cpp new file mode 100644 index 00000000..391f819d --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hfp/src/ohos.bluetooth.hfp.impl.cpp @@ -0,0 +1,52 @@ +/* + * 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.bluetooth.hfp.proj.hpp" +#include "ohos.bluetooth.hfp.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +using namespace taihe; +using namespace ohos::bluetooth::hfp; + +namespace { +// To be implemented. + +class HandsFreeAudioGatewayProfileImpl { +public: + HandsFreeAudioGatewayProfileImpl() + { + // Don't forget to implement the constructor. + } + + void OnConnectionStateChange( + callback_view callback) + { + std::cout<< "onConnectionStateChange" << std::endl; + } +}; + +HandsFreeAudioGatewayProfile CreateHfpAgProfile() +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_CreateHfpAgProfile(CreateHfpAgProfile); +// NOLINTEND diff --git a/frameworks/ets/taihe/bluetooth_hid/BUILD.gn b/frameworks/ets/taihe/bluetooth_hid/BUILD.gn new file mode 100644 index 00000000..2c51684a --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hid/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") + +SUBSYSTEM_DIR = "//foundation/communication" + +copy_taihe_idl("bluetooth_hid_taihe") { + sources = [ + "idl/ohos.bluetooth.hid.taihe" + ] + deps = [ + "$SUBSYSTEM_DIR/bluetooth/frameworks/ets/taihe/bluetooth_baseProfile:bluetooth_baseProfile_taihe" + ] +} + +subsystem_name = "communication" #子系统名 +part_name = "bluetooth" #部件名 +taihe_generated_file_path = "$taihe_file_path/out/$subsystem_name/$part_name" + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "$taihe_generated_file_path" + deps = [ ":bluetooth_hid_taihe" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.bluetooth.hid.ani.cpp", + "$taihe_generated_file_path/src/ohos.bluetooth.hid.abi.c", + ] +} + +taihe_shared_library("bluetoothHid_taihe_native") { #调用taihe_shared_library模板编译so + 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", + "src", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner/include", + "$SUBSYSTEM_DIR/bluetooth/interfaces/inner_api/include" + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.bluetooth.hid.impl.cpp", + ] + deps = [ + ":run_taihe", + "$SUBSYSTEM_DIR/bluetooth/frameworks/inner:btframework", + "../bluetooth_baseProfile:bluetoothBaseProfile_taihe_native" + ] + sanitize = { + cfi = true # Enable/disable control flow integrity detection + boundary_sanitize = true # Enable boundary san detection + cfi_cross_dso = true # Cross-SO CFI Checks + integer_overflow = true # Enable integer overflow detection + ubsan = true # Enable some Ubsan options + debug = false + } +} + +generate_static_abc("bluetoothHid_abc") { #调用generate_static_abc编译abc + base_url = "$taihe_generated_file_path" #base_url直接设置为taihe_generated_file_path + files = [ "$taihe_generated_file_path/@ohos.bluetooth.hid.ets" ] #files传入生成的ets文件 + is_boot_abc = "True" + device_dst_file = "/system/framework/bluetoothHid_abc.abc" + dependencies = [ ":run_taihe" ] #编译abc依赖太和工具链调用 +} + +ohos_prebuilt_etc("bluetoothHid_etc") { #调用ohos_prebuilt_etc将abc拷贝到/system/framework下 + source = "$target_out_dir/bluetoothHid_abc.abc" + module_install_dir = "framework" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + deps = [ ":bluetoothHid_abc" ] #拷贝abc依赖编译abc +} + +group("bluetoothHid_taihe") { + deps = [ + ":bluetoothHid_etc", + ":bluetoothHid_taihe_native" + ] + # deps = [":run_taihe"] #单独运行run_taihe目标即可只调用工具链并生成对应代码 +} \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_hid/idl/ohos.bluetooth.hid.taihe b/frameworks/ets/taihe/bluetooth_hid/idl/ohos.bluetooth.hid.taihe new file mode 100644 index 00000000..f5e1d9b1 --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hid/idl/ohos.bluetooth.hid.taihe @@ -0,0 +1,20 @@ +/* + * 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. + */ + +@!namespace("@ohos.bluetooth.hid", "hid") +from ohos.bluetooth.baseProfile use BaseProfile; +interface HidHostProfile : BaseProfile { +} +function CreateHidHostProfile(): HidHostProfile; \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_hid/src/ani_constructor.cpp b/frameworks/ets/taihe/bluetooth_hid/src/ani_constructor.cpp new file mode 100644 index 00000000..3181ec4c --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hid/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.bluetooth.hid.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::bluetooth::hid::ANIRegister(env)) { + std::cerr << "Error from ohos::bluetooth::hid::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/bluetooth_hid/src/ohos.bluetooth.hid.impl.cpp b/frameworks/ets/taihe/bluetooth_hid/src/ohos.bluetooth.hid.impl.cpp new file mode 100644 index 00000000..55ce492d --- /dev/null +++ b/frameworks/ets/taihe/bluetooth_hid/src/ohos.bluetooth.hid.impl.cpp @@ -0,0 +1,52 @@ +/* + * 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.bluetooth.hid.proj.hpp" +#include "ohos.bluetooth.hid.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" + +using namespace taihe; +using namespace ohos::bluetooth::hid; + +namespace { +// To be implemented. + +class HidHostProfileImpl { +public: + HidHostProfileImpl() + { + // Don't forget to implement the constructor. + } + + void OnConnectionStateChange( + callback_view callback) + { + std::cout<< "onConnectionStateChange" << std::endl; + } +}; + +HidHostProfile CreateHidHostProfile() +{ + // The parameters in the make_holder function should be of the same type + // as the parameters in the constructor of the actual implementation class. + return make_holder(); +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_CreateHidHostProfile(CreateHidHostProfile); +// NOLINTEND -- Gitee