From 5fe2a812da43f6a730b63c2d2042099383ab3cdd Mon Sep 17 00:00:00 2001 From: shenkang Date: Thu, 8 May 2025 11:38:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:arkts1.2=20taihe=20=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shenkang --- bundle.json | 4 +- frameworks/ets/taihe/BUILD.gn | 19 +++ frameworks/ets/taihe/usb_manager/BUILD.gn | 76 ++++++++++++ .../idl/ohos.usbManager.usbManager.taihe | 74 +++++++++++ .../taihe/usb_manager/src/ani_constructor.cpp | 29 +++++ .../src/ohos.usbManager.usbManager.impl.cpp | 117 ++++++++++++++++++ 6 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 frameworks/ets/taihe/BUILD.gn create mode 100644 frameworks/ets/taihe/usb_manager/BUILD.gn create mode 100644 frameworks/ets/taihe/usb_manager/idl/ohos.usbManager.usbManager.taihe create mode 100644 frameworks/ets/taihe/usb_manager/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/usb_manager/src/ohos.usbManager.usbManager.impl.cpp diff --git a/bundle.json b/bundle.json index bfada034..932dbee4 100644 --- a/bundle.json +++ b/bundle.json @@ -57,7 +57,8 @@ "hisysevent", "cJSON", "bounds_checking_function", - "hitrace" + "hitrace", + "runtime_core" ] }, "build": { @@ -68,6 +69,7 @@ "//base/usb/usb_manager/interfaces/kits/js/napi:usbmanager", "//base/usb/usb_manager/interfaces/kits/js/napi:usb", "//base/usb/usb_manager/frameworks/dialog/dialog_ui/usb_right_dialog:dialog_hap", + "//base/usb/usb_manager/frameworks/ets/taihe:usb_taihe", "//base/usb/usb_manager/interfaces/kits/js/napi:serial" ], "service_group": [ diff --git a/frameworks/ets/taihe/BUILD.gn b/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 00000000..3e49595f --- /dev/null +++ b/frameworks/ets/taihe/BUILD.gn @@ -0,0 +1,19 @@ +# 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("//base/usb/usb_manager/usbmgr.gni") +import("//build/ohos.gni") + +group("usb_taihe") { + deps = [ "usb_manager:usbManager_taihe" ] +} diff --git a/frameworks/ets/taihe/usb_manager/BUILD.gn b/frameworks/ets/taihe/usb_manager/BUILD.gn new file mode 100644 index 00000000..31070785 --- /dev/null +++ b/frameworks/ets/taihe/usb_manager/BUILD.gn @@ -0,0 +1,76 @@ +# 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("//base/usb/usb_manager/usbmgr.gni") +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") + +copy_taihe_idl("copy_usbManager") { + sources = [ "idl/ohos.usbManager.usbManager.taihe" ] +} + +subsystem_name = "usb" +part_name = "usb_manager" +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 = [ ":copy_usbManager" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.usbManager.usbManager.ani.cpp", + "$taihe_generated_file_path/src/ohos.usbManager.usbManager.abi.c", + ] +} + +taihe_shared_library("usb_manager_taihe_native") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + taihe_generated_file_path = "$taihe_generated_file_path" + part_name = "$part_name" + subsystem_name = "$subsystem_name" + sources = get_target_outputs(":run_taihe") + sources += [ + "src/ani_constructor.cpp", + "src/ohos.usbManager.usbManager.impl.cpp", + ] + deps = [ + ":run_taihe", + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + ] +} + +generate_static_abc("usb_manager_abc") { + base_url = "$taihe_generated_file_path" + files = [ "$taihe_generated_file_path/@ohos.usbManager.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/usb_manager_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("usb_manager_etc") { + source = "$target_out_dir/usb_manager_abc.abc" + part_name = $part_name + subsystem_name = $subsystem_name + deps = [ ":usb_manager_abc" ] +} + +group("usbManager_taihe") { + deps = [ + ":usb_manager_etc", + ":usb_manager_taihe_native", + ] +} diff --git a/frameworks/ets/taihe/usb_manager/idl/ohos.usbManager.usbManager.taihe b/frameworks/ets/taihe/usb_manager/idl/ohos.usbManager.usbManager.taihe new file mode 100644 index 00000000..4748c224 --- /dev/null +++ b/frameworks/ets/taihe/usb_manager/idl/ohos.usbManager.usbManager.taihe @@ -0,0 +1,74 @@ +/* + * 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.usbManager", "usbManager") + +@!sts_inject(""" +static { loadLibrary("usb_manager_taihe_native.z") } +""") + +enum USBRequestDirection: i32 { + USB_REQUEST_DIR_TO_DEVICE = 0, + USB_REQUEST_DIR_FROM_DEVICE = 0x80 +} + +struct USBEndpoint { + address: i32; + attributes: i32; + interval: i32; + maxPacketSize: i32; + direction: USBRequestDirection; + number: i32; + type: i32; + interfaceId: i32; +} + +struct USBInterface { + id: i32; + protocol: i32; + clazz: i32; + subClass: i32; + alternateSetting: i32; + name: String; + endpoints: Array; +} + +struct USBConfiguration { + id: i32; + attributes: i32; + maxPower: i32; + name: String; + isRemoteWakeup: bool; + isSelfPowered: bool; + interfaces: Array; +} + +struct USBDevice { + busNum: i32; + devAddress: i32; + serial: String; + name: String; + manufacturerName: String; + productName: String; + version: String; + vendorId: i32; + productId: i32; + clazz: i32; + subClass: i32; + protocol: i32; + configs: Array; +} + +function getDevices(): Array; diff --git a/frameworks/ets/taihe/usb_manager/src/ani_constructor.cpp b/frameworks/ets/taihe/usb_manager/src/ani_constructor.cpp new file mode 100644 index 00000000..e14b885e --- /dev/null +++ b/frameworks/ets/taihe/usb_manager/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.usbManager.usbManager.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::usbManager::usbManager::ANIRegister(env)) { + std::cerr << "Error from ohos::usbManager::usbManager::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/usb_manager/src/ohos.usbManager.usbManager.impl.cpp b/frameworks/ets/taihe/usb_manager/src/ohos.usbManager.usbManager.impl.cpp new file mode 100644 index 00000000..ccd1bb74 --- /dev/null +++ b/frameworks/ets/taihe/usb_manager/src/ohos.usbManager.usbManager.impl.cpp @@ -0,0 +1,117 @@ +/* + * 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.usbManager.usbManager.proj.hpp" +#include "ohos.usbManager.usbManager.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "usb_srv_client.h" + +using namespace taihe; +using namespace ohos::usbManager::usbManager; + +namespace { +static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); + +static ohos::usbManager::usbManager::USBEndpoint ParseToUSBEndpoint(const OHOS::USB::USBEndpoint &usbEndpoint) +{ + auto directionKey = usbEndpoint.GetDirection() == OHOS::USB::USB_ENDPOINT_DIR_OUT + ? ohos::usbManager::usbManager::USBRequestDirection::key_t::USB_REQUEST_DIR_TO_DEVICE + : ohos::usbManager::usbManager::USBRequestDirection::key_t::USB_REQUEST_DIR_FROM_DEVICE; + return { + .address = usbEndpoint.GetAddress(), + .attributes = usbEndpoint.GetAttributes(), + .interval = usbEndpoint.GetInterval(), + .maxPacketSize = usbEndpoint.GetMaxPacketSize(), + .direction = ohos::usbManager::usbManager::USBRequestDirection(directionKey), + .number = usbEndpoint.GetEndpointNumber(), + .type = usbEndpoint.GetType(), + .interfaceId = usbEndpoint.GetInterfaceId() + }; +} + +static ohos::usbManager::usbManager::USBInterface ParseToUSBInterface(OHOS::USB::UsbInterface &usbInterface) +{ + std::vector endpoints; + for (const auto &endpoint : usbInterface.GetEndpoints()) { + endpoints.push_back(ParseToUSBEndpoint(endpoint)); + } + return { + .id = usbInterface.GetId(), + .protocol = usbInterface.GetProtocol(), + .clazz = usbInterface.GetClass(), + .subClass = usbInterface.GetSubClass(), + .alternateSetting = usbInterface.GetAlternateSetting(), + .name = usbInterface.GetName(), + .endpoints = array(endpoints) + }; +} + +static ohos::usbManager::usbManager::USBConfiguration ParseToUSBConfiguration(OHOS::USB::USBConfig &usbConfig) +{ + std::vector interfaces; + for (auto &interface : usbConfig.GetInterfaces()) { + interfaces.push_back(ParseToUSBInterface(interface)); + } + return { + .id = usbConfig.GetId(), + .attributes = usbConfig.GetAttributes(), + .maxPower = usbConfig.GetMaxPower(), + .name = usbConfig.GetName(), + .isRemoteWakeup = usbConfig.IsRemoteWakeup(), + .isSelfPowered = usbConfig.IsSelfPowered(), + .interfaces = array(interfaces) + }; +} + +static ohos::usbManager::usbManager::USBDevice ParseToUSBDevice(OHOS::USB::UsbDevice &usbDevice) +{ + std::vector configs; + for (auto &config : usbDevice.GetConfigs()) { + configs.push_back(ParseToUSBConfiguration(config)); + } + return { + .busNum = usbDevice.GetBusNum(), + .devAddress = usbDevice.GetDevAddr(), + .serial = usbDevice.GetmSerial(), + .name = usbDevice.GetName(), + .manufacturerName = usbDevice.GetManufacturerName(), + .productName = usbDevice.GetProductName(), + .version = usbDevice.GetVersion(), + .vendorId = usbDevice.GetVendorId(), + .productId = usbDevice.GetProductId(), + .clazz = usbDevice.GetClass(), + .subClass = usbDevice.GetSubclass(), + .protocol = usbDevice.GetProtocol(), + .configs = array(configs) + }; +} + +array getDevices() +{ + std::vector res; + std::vector deviceList; + auto ret = g_usbClient.GetDeviceList(deviceList); + if (ret != 0) { + return array(res); + } + for (auto &usbDevice : deviceList) { + res.push_back(ParseToUSBDevice(usbDevice)); + } + return array(res); +} +} // namespace + +TH_EXPORT_CPP_API_getDevices(getDevices); -- Gitee