From dd02056d89acc36d8a178a2bf48911d1dc64cc68 Mon Sep 17 00:00:00 2001 From: luo-wei246 Date: Thu, 1 Jun 2023 19:10:44 -0700 Subject: [PATCH] feat:Disable HDC Settings Signed-off-by: luo-wei246 --- interfaces/kits/js/napi/src/usb_info.cpp | 1 + services/BUILD.gn | 1 + services/native/include/usb_right_manager.h | 2 ++ services/native/src/usb_right_manager.cpp | 28 +++++++++++++++++++++ services/native/src/usb_service.cpp | 9 ++++--- utils/native/include/usb_errors.h | 1 + utils/native/include/usb_napi_errors.h | 2 ++ 7 files changed, 40 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/napi/src/usb_info.cpp b/interfaces/kits/js/napi/src/usb_info.cpp index 57d11e89..027b455f 100644 --- a/interfaces/kits/js/napi/src/usb_info.cpp +++ b/interfaces/kits/js/napi/src/usb_info.cpp @@ -696,6 +696,7 @@ static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info) napi_value result; USB_HILOGI(MODULE_JS_NAPI, "get current functions failed ret = %{public}d", ret); USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_DENIED_SYSAPI), USB_SYSAPI_PERMISSION_DENIED, ""); + USB_ASSERT_RETURN_UNDEF(env, (ret != UEC_SERVICE_PERMISSION_CHECK_HDC), USB_HDC_PERMISSION_DENIED, ""); if (ret != UEC_OK) { napi_get_undefined(env, &result); diff --git a/services/BUILD.gn b/services/BUILD.gn index 2e0f835a..c28a196e 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -69,6 +69,7 @@ ohos_shared_library("usbservice") { "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", "init:libbegetutil", + "napi:ace_napi", "os_account:os_account_innerkits", "relational_store:native_rdb", "safwk:system_ability_fwk", diff --git a/services/native/include/usb_right_manager.h b/services/native/include/usb_right_manager.h index 064c7bbc..2cbd73cd 100644 --- a/services/native/include/usb_right_manager.h +++ b/services/native/include/usb_right_manager.h @@ -26,6 +26,7 @@ #include "ability_connect_callback_stub.h" #include "bundle_mgr_interface.h" #include "usb_common.h" +#include "parameter.h" namespace OHOS { namespace USB { @@ -46,6 +47,7 @@ public: static int32_t CleanUpRightUserDeleted(int32_t &totalUsers, int32_t &deleteUsers); static int32_t CleanUpRightAppUninstalled(int32_t uid, const std::string &bundleName); static int32_t IsOsAccountExists(int32_t id, bool &isAccountExists); + int32_t HasSetFuncRight(int32_t functions); private: bool GetUserAgreementByDiag( diff --git a/services/native/src/usb_right_manager.cpp b/services/native/src/usb_right_manager.cpp index 4a6ca2c1..b89db1c0 100644 --- a/services/native/src/usb_right_manager.cpp +++ b/services/native/src/usb_right_manager.cpp @@ -33,11 +33,16 @@ #include "tokenid_kit.h" #include "usb_errors.h" #include "usb_right_db_helper.h" +#include "usb_napi_errors.h" +#include "usb_srv_support.h" using namespace OHOS::AppExecFwk; using namespace OHOS::EventFwk; using namespace OHOS::Security::AccessToken; + +#define PARAM_BUF_LEN 128 + namespace OHOS { namespace USB { @@ -377,6 +382,29 @@ int32_t UsbRightManager::IsOsAccountExists(int32_t id, bool &isAccountExists) return USB_RIGHT_OK; } +int32_t UsbRightManager::HasSetFuncRight(int32_t functions) +{ + if (!IsSystemHap()) { + USB_HILOGW(MODULE_USB_SERVICE, "is not system app"); + return UEC_SERVICE_PERMISSION_DENIED_SYSAPI; + } + if (!(functions & UsbSrvSupport::FUNCTION_HDC)) { + return UEC_OK; + } + USB_HILOGI(MODULE_USB_SERVICE, "Set up function permission validation"); + char paramValue[PARAM_BUF_LEN] = { 0 }; + int32_t ret = GetParameter("persist.hdc.control", "true", paramValue, sizeof(paramValue)); + if (ret < 0) { + USB_HILOGW(MODULE_USB_SERVICE, "GetParameter fail"); + } + ret = strcmp(paramValue, "true"); + if (ret != 0) { + USB_HILOGE(MODULE_USB_SERVICE, "HDC setup failed"); + return UEC_SERVICE_PERMISSION_CHECK_HDC; + } + return UEC_OK; +} + int32_t UsbRightManager::CleanUpRightExpired(std::vector &devices) { USB_HILOGD(MODULE_USB_SERVICE, "clean up expired right: size=%{public}zu", devices.size()); diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index af164bec..0d776213 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -401,11 +401,12 @@ int32_t UsbService::SetCurrentFunctions(int32_t functions) USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_"); return UEC_SERVICE_INVALID_VALUE; } - if (!(usbRightManager_->IsSystemHap())) { - USB_HILOGW(MODULE_USB_SERVICE, "is not system app"); - return UEC_SERVICE_PERMISSION_DENIED_SYSAPI; - } + int32_t ret = usbRightManager_->HasSetFuncRight(functions); + if (ret != 0) { + USB_HILOGE(MODULE_USB_SERVICE, "HasSetFuncRight fail"); + return ret; + } if (usbDeviceManager_ == nullptr) { USB_HILOGE(MODULE_USB_SERVICE, "invalid usbDeviceManager_"); return UEC_SERVICE_INVALID_VALUE; diff --git a/utils/native/include/usb_errors.h b/utils/native/include/usb_errors.h index 5416751e..e2b74187 100644 --- a/utils/native/include/usb_errors.h +++ b/utils/native/include/usb_errors.h @@ -74,6 +74,7 @@ enum UsbErrCode { UEC_SERVICE_ADD_DEATH_RECIPIENT_FAILED, UEC_SERVICE_INNER_ERR, UEC_SERVICE_PERMISSION_DENIED_SYSAPI, + UEC_SERVICE_PERMISSION_CHECK_HDC, UEC_SERVICE_END, }; diff --git a/utils/native/include/usb_napi_errors.h b/utils/native/include/usb_napi_errors.h index 82c4497f..ed64b136 100644 --- a/utils/native/include/usb_napi_errors.h +++ b/utils/native/include/usb_napi_errors.h @@ -26,12 +26,14 @@ enum UsbJsErrCode : int32_t { SYSPARAM_INVALID_INPUT = 401, USB_DEVICE_PERMISSION_DENIED = 14400001, USB_SYSAPI_PERMISSION_DENIED = 202, + USB_HDC_PERMISSION_DENIED = 14400002, }; const std::map ERRCODE_MSG_MAP = { {SYSPARAM_INVALID_INPUT, "BusinessError 401:Parameter error." }, {USB_DEVICE_PERMISSION_DENIED, "BusinessError 14400001:Permission denied." }, {USB_SYSAPI_PERMISSION_DENIED, "BusinessError 202:Permission denied. Normal application uses system api."}, + {USB_HDC_PERMISSION_DENIED, "BusinessError 14400002:Permission denied.The HDC is disabled by the system."} }; void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); -- Gitee