diff --git a/frameworks/ets/taihe/usb_manager/BUILD.gn b/frameworks/ets/taihe/usb_manager/BUILD.gn index 9eab86e7e529991bae7228c0285221f4d80d6969..64366f0d24943951ebdb54f07bbb15cd33c441c8 100644 --- a/frameworks/ets/taihe/usb_manager/BUILD.gn +++ b/frameworks/ets/taihe/usb_manager/BUILD.gn @@ -80,6 +80,7 @@ taihe_shared_library("usb_manager_taihe_native") { "hitrace:hitrace_meter", "ipc:ipc_single", "eventhandler:libeventhandler", + "samgr:samgr_proxy", ] } @@ -122,4 +123,4 @@ group("usbManager_taihe") { ":usb_manager_taihe_native", ":usbmanager_serial_etc", ] -} \ No newline at end of file +} diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index 102f2b15f796503641942e83a98b7014da80f47c..0548156909080e64d533b7d7dcfbf14242636479 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -31,6 +31,7 @@ #include "usb_interface_type.h" #include "serial_death_monitor.h" #include "usb_server_types.h" +#include "system_ability_load_callback_stub.h" namespace OHOS { namespace USB { constexpr uint8_t CLAIM_FORCE_1 = 1; @@ -126,6 +127,7 @@ public: private: UsbSrvClient(); ~UsbSrvClient(); + int32_t LoadService(); void UsbDeviceTypeChange(const std::vector &disableType, std::vector &deviceTypes); void UsbCtrlTransferChange(const HDI::Usb::V1_0::UsbCtrlTransfer ¶m, UsbCtlSetUp &ctlSetup); diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index a9c5c4d8f2b029f00798e3cd6adce04186e4a639..44a6ceeba58cf61141cf2120d95ac88ea8937c3c 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -36,6 +36,26 @@ constexpr int32_t READ_BUF_SIZE = 8192; constexpr int32_t PARAM_ERROR = 401; #endif // USB_MANAGER_FEATURE_HOST [[ maybe_unused ]] constexpr int32_t CAPABILITY_NOT_SUPPORT = 801; + +class UsbSrvClientCallback : public SystemAbilityLoadCallbackStub { +public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override + { + if (systemAbilityId != USB_SYSTEM_ABILITY_ID) { + USB_HILOGE(MODULE_USB_INNERKIT, "Start systemAbility is not Usb service: %{public}d.", systemAbilityId); + return; + } + } + + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override + { + if (systemAbilityId != USB_SYSTEM_ABILITY_ID) { + USB_HILOGE(MODULE_USB_INNERKIT, "Start systemAbility is not Usb service: %{public}d.", systemAbilityId); + return; + } + } +}; + UsbSrvClient::UsbSrvClient() { Connect(); @@ -52,6 +72,29 @@ UsbSrvClient& UsbSrvClient::GetInstance() return instance; } +int32_t UsbSrvClient::LoadService() +{ + if (Connect() == UEC_INTERFACE_GET_USB_SERVICE_FAILED) { + sptr callback = new (std::nothrow) UsbSrvClientCallback(); + if (callback == nullptr) { + USB_HILOGE(MODULE_USB_INNERKIT, "LoadCallback new error."); + return UEC_INTERFACE_NO_INIT; + } + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + USB_HILOGE(MODULE_USB_INNERKIT, "fail to get SystemAbilityManager"); + return UEC_INTERFACE_GET_SYSTEM_ABILITY_MANAGER_FAILED; + } + int32_t result = sm->LoadSystemAbility(USB_SYSTEM_ABILITY_ID, callback); + if (result != ERR_OK) { + USB_HILOGE( + MODULE_USB_INNERKIT, "LoadSystemAbility UsbSaLoad failed, result: %{public}d.", result); + return UEC_INTERFACE_NO_INIT; + } + } + return UEC_OK; +} + int32_t UsbSrvClient::Connect() { std::lock_guard lock(mutex_); @@ -816,7 +859,8 @@ int32_t UsbSrvClient::GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInt #ifdef USB_MANAGER_FEATURE_DEVICE int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs) { - RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT); + USB_HILOGI(MODULE_USB_INNERKIT, "SetCurrentFunctions funcs Enter"); + RETURN_IF_WITH_RET(LoadService() != UEC_OK, UEC_INTERFACE_NO_INIT); int32_t ret = proxy_->GetCurrentFunctions(funcs); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret); @@ -830,7 +874,7 @@ int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs) int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs) { USB_HILOGI(MODULE_USB_INNERKIT, "SetCurrentFunctions funcs = %{public}d!", funcs); - RETURN_IF_WITH_RET(Connect() != UEC_OK, false); + RETURN_IF_WITH_RET(LoadService() != UEC_OK, UEC_INTERFACE_NO_INIT); int32_t ret = proxy_->SetCurrentFunctions(funcs); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret); @@ -1005,8 +1049,8 @@ int32_t UsbSrvClient::CloseAccessory(const int32_t fd) #ifdef USB_MANAGER_FEATURE_PORT int32_t UsbSrvClient::GetPorts(std::vector &usbports) { - RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT); USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetPorts"); + RETURN_IF_WITH_RET(LoadService() != UEC_OK, UEC_INTERFACE_NO_INIT); int32_t ret = proxy_->GetPorts(usbports); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret); @@ -1016,8 +1060,8 @@ int32_t UsbSrvClient::GetPorts(std::vector &usbports) int32_t UsbSrvClient::GetSupportedModes(int32_t portId, int32_t &result) { - RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT); USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetSupportedModes"); + RETURN_IF_WITH_RET(LoadService() != UEC_OK, UEC_INTERFACE_NO_INIT); int32_t ret = proxy_->GetSupportedModes(portId, result); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret); @@ -1027,8 +1071,8 @@ int32_t UsbSrvClient::GetSupportedModes(int32_t portId, int32_t &result) int32_t UsbSrvClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) { - RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT); USB_HILOGI(MODULE_USB_INNERKIT, "Calling SetPortRole"); + RETURN_IF_WITH_RET(LoadService() != UEC_OK, UEC_INTERFACE_NO_INIT); int32_t ret = proxy_->SetPortRole(portId, powerRole, dataRole); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret); diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index 7d4606fb89f0858771111945723b27ceb6de990c..b0ac1b8d8c5f5396121fdf469fa13f5332b77026 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -82,6 +82,7 @@ ohos_shared_library("usbmanager") { "hitrace:hitrace_meter", "ipc:ipc_single", "napi:ace_napi", + "samgr:samgr_proxy", ] relative_install_dir = "module"