diff --git a/interfaces/kits/js/napi/src/usb_info.cpp b/interfaces/kits/js/napi/src/usb_info.cpp index 57d11e893ba873ed538e41e28bd15e44b559a82b..027b455f3ecc230e059eba62237dd72540efdc79 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 2e0f835a53d77b14af3f05e55e13f9644a9e8560..c28a196ed591e585a19df12d0ffd97fef98ee6a8 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 064c7bbcc88722339e0bcab4bc49fb8b006e4c0a..2cbd73cdf335141b659e22969c0e586bcc1b4e84 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 4a6ca2c1a65ee539cc2f1e14be2832d4242ab568..b89db1c06b3d211da99e6080033476ae38e53da8 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 af164bec14faf224df813273a15ec27cab242c27..0d776213e440ee7efa74b5feab2b3df2af3b7749 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 5416751e76aeb91b08e75b5982e45f43ef649a26..e2b741879be89a8646475e3e21b7dcd4e3f9a80f 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 82c4497fe3ac3abbb348a7145e4ecf5b7d4a02fe..ed64b136cdaff4bee5d2e7059a7fa580b94d2d81 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);