diff --git a/interfaces/kits/taihe/include/ohos.distributedDeviceManager.h b/interfaces/kits/taihe/include/ohos.distributedDeviceManager.h index 0242f0efac793acaf0310cd2d55f74d5156f12d2..63b29f1d0b610f32b082846e7e2de6d8d5d0ba48 100644 --- a/interfaces/kits/taihe/include/ohos.distributedDeviceManager.h +++ b/interfaces/kits/taihe/include/ohos.distributedDeviceManager.h @@ -19,6 +19,7 @@ #include #include "device_manager_callback.h" #include "dm_device_info.h" +#include "dm_error_type.h" #include "ohos.distributedDeviceManager.proj.hpp" #include "ohos.distributedDeviceManager.impl.hpp" #include "taihe/callback.hpp" @@ -26,9 +27,25 @@ namespace ANI::distributedDeviceManager { -enum DMBussinessErrorCode { +enum ANIBussinessErrorCode { + // OK + ERR_OK = 0, + // Permission verify failed. ERR_NO_PERMISSION = 201, + // The caller is not a system application. + ERR_NOT_SYSTEM_APP = 202, + // Input parameter error. + ERR_INVALID_PARAMS = 401, + // Failed to execute the function. DM_ERR_FAILED = 11600101, + // Failed to obtain the service. + DM_ERR_OBTAIN_SERVICE = 11600102, + // Authentication invalid. + DM_ERR_AUTHENTICALTION_INVALID = 11600103, + // Discovery invalid. + DM_ERR_DISCOVERY_INVALID = 11600104, + // Publish invalid. + DM_ERR_PUBLISH_INVALID = 11600105, }; class DeviceManagerImpl { @@ -37,11 +54,11 @@ public: explicit DeviceManagerImpl(std::shared_ptr impl); explicit DeviceManagerImpl(const std::string& bundleName); ~DeviceManagerImpl() = default; - std::string GetLocalDeviceId(); + taihe::string GetLocalDeviceId(); void UnbindTarget(taihe::string_view deviceId); - double GetDeviceType(taihe::string_view networkId); - std::string GetDeviceName(taihe::string_view networkId); - std::string GetLocalDeviceNetworkId(); + int32_t GetDeviceType(taihe::string_view networkId); + taihe::string GetDeviceName(taihe::string_view networkId); + taihe::string GetLocalDeviceNetworkId(); void OnDeviceNameChange(taihe::callback_view onDeviceNameChangecb); diff --git a/interfaces/kits/taihe/src/ohos.distributedDeviceManager.cpp b/interfaces/kits/taihe/src/ohos.distributedDeviceManager.cpp index a05aeefca620d465229e789884b10896d8a478df..e9f8c869aba4ccd87a7ce17428b698958777cff2 100644 --- a/interfaces/kits/taihe/src/ohos.distributedDeviceManager.cpp +++ b/interfaces/kits/taihe/src/ohos.distributedDeviceManager.cpp @@ -25,7 +25,8 @@ namespace ANI::distributedDeviceManager { namespace { -constexpr double DEVICE_TYPE_UNKNOWN = -1.0; +constexpr int32_t DEVICE_TYPE_UNKNOWN = -1; +const int32_t DM_STR_BUF_LENGTH = 256; constexpr const char *DEVICE_TYPE_EMPTY_STR = ""; constexpr const char *ERROR_DEVICE_ID = "error deviceId"; constexpr const char *ERROR_NETWORK_ID = "error networkId"; @@ -45,6 +46,32 @@ std::map> g_discover std::map> g_dmUiCallbackMap; } //namespace +inline int32_t StringCheck(const taihe::string_view &str) +{ + auto strPara = std::string(str); + LOGI("*****strPara = %{public}s", strPara.c_str()); + if (strPara.size() == 0 || strPara.size() >= DM_STR_BUF_LENGTH) { + return ANIBussinessErrorCode::ERR_INVALID_PARAMS; + } + return ANIBussinessErrorCode::ERR_OK; +} + +int32_t TransformErrCode(const int32_t errCode) +{ + switch (errCode) { + case OHOS::DistributedHardware::ERR_DM_NO_PERMISSION: + return ANIBussinessErrorCode::ERR_NO_PERMISSION; + case OHOS::DistributedHardware::ERR_DM_INPUT_PARA_INVALID: + case OHOS::DistributedHardware::ERR_DM_UNSUPPORTED_AUTH_TYPE: + return ANIBussinessErrorCode::ERR_INVALID_PARAMS; + case ANIBussinessErrorCode::ERR_NOT_SYSTEM_APP: + return ANIBussinessErrorCode::ERR_NOT_SYSTEM_APP; + default: + return ANIBussinessErrorCode::DM_ERR_FAILED; + } + return 0; +} + DeviceManagerImpl::DeviceManagerImpl(const std::string& bundleName) : bundleName_(bundleName) { @@ -68,7 +95,7 @@ ohos::distributedDeviceManager::DeviceManager CreateDeviceManager(taihe::string_ std::string(bundleName), initCallback); if (ret != 0) { LOGE("CreateDeviceManager for bundleName %{public}s failed, ret %{public}d.", bundleName.c_str(), ret); - taihe::set_business_error(DM_ERR_FAILED, "CreateDeviceManager for failed"); + taihe::set_business_error(DM_ERR_FAILED, "Failed to execute the function"); return taihe::make_holder(); } { @@ -113,72 +140,98 @@ ohos::distributedDeviceManager::DeviceStateChangeResult MakeDeviceStateChangeRes return {deviceStateChange, deviceBasicInfo}; } -std::string DeviceManagerImpl::GetLocalDeviceId() +taihe::string DeviceManagerImpl::GetLocalDeviceId() { if (OHOS::DistributedHardware::DeviceManager::GetInstance().CheckNewAPIAccessPermission() != 0) { taihe::set_business_error(OHOS::DistributedHardware::ERR_DM_NO_PERMISSION, "GetLocalDeviceId for failed1"); return DEVICE_TYPE_EMPTY_STR; } - std::string deviceId; + std::string deviceId = ""; int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetLocalDeviceId(bundleName_, deviceId); if (ret != 0) { - taihe::set_business_error(DM_ERR_FAILED, "GetLocalDeviceId for failed2"); + taihe::set_business_error(DM_ERR_FAILED, "Failed to execute the function"); return ERROR_DEVICE_ID; } - return std::string(deviceId); + return taihe::string(deviceId.c_str()); } void DeviceManagerImpl::UnbindTarget(taihe::string_view deviceId) { + LOGI("*********UnbindTarget start"); + int32_t checkRet = StringCheck(deviceId); + if (checkRet != 0) { + taihe::set_business_error(checkRet, "Input parameter error"); + return; + } int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().UnBindDevice( bundleName_, std::string(deviceId)); + LOGI("*********error %{public}d", ret); if (ret != 0) { + ret = TransformErrCode(ret); LOGE("UnBindDevice for bundleName %{public}s failed, ret %{public}d", bundleName_.c_str(), ret); - taihe::set_business_error(DM_ERR_FAILED, "UnbindTarget for failed"); + taihe::set_business_error(ret, "Failed to execute the function"); return; } } -double DeviceManagerImpl::GetDeviceType(taihe::string_view networkId) +int32_t DeviceManagerImpl::GetDeviceType(taihe::string_view networkId) { + LOGI("*********GetDeviceType start"); + int32_t checkRet = StringCheck(networkId); + if (checkRet != 0) { + taihe::set_business_error(checkRet, "Input parameter error"); + return DEVICE_TYPE_UNKNOWN; + } int32_t deviceType; int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetDeviceType( bundleName_, std::string(networkId), deviceType); + LOGI("*********error %{public}d", ret); if (ret != 0) { - taihe::set_business_error(DM_ERR_FAILED, "GetDeviceType for failed"); + ret = TransformErrCode(ret); + taihe::set_business_error(ret, "Failed to execute the function"); return DEVICE_TYPE_UNKNOWN; } - return static_cast(deviceType); + return deviceType; } -std::string DeviceManagerImpl::GetDeviceName(taihe::string_view networkId) +taihe::string DeviceManagerImpl::GetDeviceName(taihe::string_view networkId) { - std::string deviceName; + int32_t checkRet = StringCheck(networkId); + if (checkRet != 0) { + taihe::set_business_error(checkRet, "Input parameter error"); + return DEVICE_TYPE_EMPTY_STR; + } + LOGI("*********GetDeviceName start"); + std::string deviceName = ""; int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetDeviceName( bundleName_, std::string(networkId), deviceName); + LOGI("*********error %{public}d", ret); if (ret != 0) { - taihe::set_business_error(DM_ERR_FAILED, "GetDeviceName for failed"); + ret = TransformErrCode(ret); + taihe::set_business_error(ret, "Failed to execute the function"); return DEVICE_TYPE_EMPTY_STR; } - return std::string(deviceName); + return taihe::string(deviceName.c_str()); } -std::string DeviceManagerImpl::GetLocalDeviceNetworkId() +taihe::string DeviceManagerImpl::GetLocalDeviceNetworkId() { if (OHOS::DistributedHardware::DeviceManager::GetInstance().CheckNewAPIAccessPermission() != 0) { taihe::set_business_error(OHOS::DistributedHardware::ERR_DM_NO_PERMISSION, "GetLocalDeviceNetworkId failed"); return DEVICE_TYPE_EMPTY_STR; } - std::string networkId; + std::string networkId = ""; int32_t ret = OHOS::DistributedHardware::DeviceManager::GetInstance().GetLocalDeviceNetWorkId( bundleName_, networkId); + LOGI("*********error %{public}d", ret); if (ret != 0) { + ret = TransformErrCode(ret); LOGE("GetLocalDeviceNetworkId for failed, ret %{public}d", ret); - taihe::set_business_error(DM_ERR_FAILED, "GetLocalDeviceNetworkId failed"); + taihe::set_business_error(ret, "Failed to execute the function"); return ERROR_NETWORK_ID; } - return std::string(networkId); + return taihe::string(networkId.c_str()); } void DeviceManagerImpl::OnDeviceNameChange(taihe::callback_view onServiceDiecb { if (OHOS::DistributedHardware::DeviceManager::GetInstance().CheckNewAPIAccessPermission() != 0) { taihe::set_business_error(OHOS::DistributedHardware::ERR_DM_NO_PERMISSION, - "OnReplyResult check permission failed"); + "OnServiceDie check permission failed"); return; } @@ -473,7 +526,7 @@ void DeviceManagerImpl::OffServiceDie(taihe::optional_view