diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index d932e8c7c8081a6b02f3da7f5fa91078bdd31137..085cf49329b1ac273c1597110380760517b55ee2 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -102,6 +102,12 @@ enum DMIpcCmdInterfaceCode { GET_DEVICE_PROFILE_INFO_LIST_RESULT, GET_DEVICE_ICON_INFO, GET_DEVICE_ICON_INFO_RESULT, + GEN_SERVICEID, + REG_SERVICE_INFO, + UNREG_SERVICE_INFO, + UPDATE_SERVICE_INFO, + GET_SERVICEINFO_BYID, + GET_SERVICEINFO_CALLER, // Add ipc msg here IPC_MSG_BUTT }; diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index ece2184ff497c8f10dbde04f4ad388775a328f94..4eb9150506acc4cd9fc0b1cf259db30495d82681 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -50,6 +50,7 @@ enum { ERR_DM_BIND_USER_CANCEL_PIN_CODE_DISPLAY = -20056, ERR_DM_SYNC_DELETE_DEVICE_REPEATED = -20058, ERR_DM_VERSION_INCOMPATIBLE = -20059, + ERR_DM_SERVICEINFO_NOTVALID = -20060, ERR_DM_FAILED = 96929744, ERR_DM_NOT_INIT = 96929746, @@ -128,7 +129,8 @@ enum { ERR_DM_HILINKSVC_REPLY_FAILED = 96929827, ERR_DM_HILINKSVC_ICON_URL_EMPTY = 96929828, ERR_DM_HILINKSVC_DISCONNECT = 96929829, - ERR_DM_WISE_NEED_LOGIN = 96929830 + ERR_DM_WISE_NEED_LOGIN = 96929830, + ERR_DM_HICHAIN_PROOFMISMATCH = 96929831, }; constexpr const char* TAG_GROUP_ID = "groupId"; @@ -261,6 +263,13 @@ constexpr const char* PROCESS_NAME = "PROCESS_NAME"; constexpr const char* PARAM_CLOSE_SESSION_DELAY_SECONDS = "DM_CLOSE_SESSION_DELAY_SECONDS"; constexpr const char* DM_AUTHENTICATION_TYPE = "DM_AUTHENTICATION_TYPE"; +constexpr const char* PARAM_KEY_CONN_SESSIONTYPE = "connSessionType"; +constexpr const char* PARAM_KEY_HML_RELEASETIME = "hmlReleaseTime"; +constexpr const char* PARAM_KEY_HML_ENABLE_160M = "hmlEnable160M"; +constexpr const char* PARAM_KEY_HML_ACTIONID = "hmlActionId"; + +const std::string CONN_SESSION_TYPE_HML = "HML"; +const std::string CONN_SESSION_TYPE_BLE = "BLE"; // screen state constexpr int32_t DM_SCREEN_UNKNOWN = -1; constexpr int32_t DM_SCREEN_ON = 0; diff --git a/common/include/ipc/model/ipc_gen_serviceid_rsp.h b/common/include/ipc/model/ipc_gen_serviceid_rsp.h new file mode 100644 index 0000000000000000000000000000000000000000..74840316c2ab33b3396ea8c64df35e54c84a1897 --- /dev/null +++ b/common/include/ipc/model/ipc_gen_serviceid_rsp.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef OHOS_DM_IPC_GEN_SERVICEID_RSP_H +#define OHOS_DM_IPC_GEN_SERVICEID_RSP_H + +#include "ipc_rsp.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGenServiceIdRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGenServiceIdRsp); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(const int64_t &serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GEN_SERVICEID_RSP_H diff --git a/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h b/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h new file mode 100644 index 0000000000000000000000000000000000000000..c0090a8a1198acfe5870bfff8784ed166b00ba0e --- /dev/null +++ b/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h @@ -0,0 +1,45 @@ +/* + * 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. + */ + +#ifndef OHOS_DM_IPC_GET_SERVICEINFO_BYCALLER_RSP_H +#define OHOS_DM_IPC_GET_SERVICEINFO_BYCALLER_RSP_H + +#include + +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetServiceInfoByCallerRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetServiceInfoByCallerRsp); + +public: + const std::vector& GetServiceInfos() const + { + return serviceInfos_; + } + + void SetServiceInfos(const std::vector &infos) + { + serviceInfos_ = infos; + } + +private: + std::vector serviceInfos_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_SERVICEINFO_BYCALLER_RSP_H diff --git a/common/include/ipc/model/ipc_get_serviceinfo_byid_req.h b/common/include/ipc/model/ipc_get_serviceinfo_byid_req.h new file mode 100644 index 0000000000000000000000000000000000000000..a99e2490ccc8ca792d13714b933fccd4aaff4f94 --- /dev/null +++ b/common/include/ipc/model/ipc_get_serviceinfo_byid_req.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef OHOS_DM_IPC_GET_SERVICE_INFO_BYID_REQ_H +#define OHOS_DM_IPC_GET_SERVICE_INFO_BYID_REQ_H + +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetServiceInfoByIdReq : public IpcReq { + DECLARE_IPC_MODEL(IpcGetServiceInfoByIdReq); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(const int64_t &serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_SERVICE_INFO_BYID_REQ_H diff --git a/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h b/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h new file mode 100644 index 0000000000000000000000000000000000000000..cd3cd1396fc71c4c743af945d0e8e047aba70b4c --- /dev/null +++ b/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef OHOS_DM_IPC_GET_SERVICEINFO_BYID_RSP_H +#define OHOS_DM_IPC_GET_SERVICEINFO_BYID_RSP_H + +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetServiceInfoByIdRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetServiceInfoByIdRsp); + +public: + const DMServiceInfo &GetServiceInfo() const + { + return serviceInfo_; + } + + void SetServiceInfo(const DMServiceInfo &info) + { + serviceInfo_ = info; + } +private: + DMServiceInfo serviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_SERVICEINFO_BYID_RSP_H diff --git a/common/include/ipc/model/ipc_register_serviceinfo_req.h b/common/include/ipc/model/ipc_register_serviceinfo_req.h new file mode 100644 index 0000000000000000000000000000000000000000..a869907b1f802cda115c173427c6dfbf1cd08a66 --- /dev/null +++ b/common/include/ipc/model/ipc_register_serviceinfo_req.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef OHOS_DM_IPC_REGISTER_SERVICE_INFO_REQ_H +#define OHOS_DM_IPC_REGISTER_SERVICE_INFO_REQ_H + +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRegServiceInfoReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegServiceInfoReq); + +public: + const DMServiceInfo &GetServiceInfo() const + { + return serviceInfo_; + } + + void SetServiceInfo(const DMServiceInfo &info) + { + serviceInfo_ = info; + } + +private: + DMServiceInfo serviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_REGISTER_SERVICE_INFO_REQ_H diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index d0bb054cee7b6b3fd346a475676c667a09371b0a..e1a1122fddaf970be1bd334cccfc75838de885fd 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -21,6 +21,8 @@ #include "dm_device_info.h" #include "dm_single_instance.h" #include "i_dp_inited_callback.h" +#include "service_info_profile.h" +#include "service_info_unique_key.h" #include "trusted_device_info.h" constexpr uint32_t ALLOW_AUTH_ONCE = 1; @@ -167,6 +169,13 @@ public: int32_t SubscribeDeviceProfileInited(sptr dpInitedCallback); int32_t UnSubscribeDeviceProfileInited(); int32_t PutAllTrustedDevices(const std::vector &deviceInfos); + int32_t PutServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile); + int32_t DeleteServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoUniqueKey& key); + int32_t UpdateServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile); + int32_t GetServiceInfoProfileByUniqueKey(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, + DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile); + int32_t GetServiceInfoProfileListByTokenId(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, + std::vector& serviceInfoProfiles); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 6d57f99bc7785a08d306bda9d9362f08727bad26..6f5309208030f357706c34dc41621421aad63d45 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1631,6 +1631,69 @@ int32_t DeviceProfileConnector::PutAllTrustedDevices( return DM_OK; } +int32_t DeviceProfileConnector::PutServiceInfoProfile( + const DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::PutServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoUniqueKey& key) +{ + LOGI("[rm003] DeviceProfileConnector::DeleteServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteServiceInfoProfile(key); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::UpdateServiceInfoProfile( + const DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::UpdateServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().UpdateServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileByUniqueKey( + const DistributedDeviceProfile::ServiceInfoUniqueKey& key, + DistributedDeviceProfile::ServiceInfoProfile& serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::GetServiceInfoProfileByUniqueKey"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByUniqueKey( + key, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileListByTokenId( + const DistributedDeviceProfile::ServiceInfoUniqueKey& key, + std::vector& serviceInfoProfiles) +{ + LOGI("[rm003] DeviceProfileConnector::GetServiceInfoProfileListByTokenId"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId( + key, serviceInfoProfiles); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index d0550aa0e5f0de373f3df072b9fb30c1dff7eb57..c8388fad473e7c98ad30e21aa596567101fdc30f 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -649,6 +649,12 @@ public: virtual int32_t GetDeviceIconInfo(const std::string &pkgName, const DmDeviceIconInfoFilterOptions &filterOptions, std::shared_ptr callback) = 0; + virtual int32_t GenerateServiceId(int64_t &serviceId) = 0; + virtual int32_t RegisterServiceInfo(const DMServiceInfo &info) = 0; + virtual int32_t UnRegisterServiceInfo(const DMServiceInfo &info) = 0; + virtual int32_t UpdateServiceInfo(const DMServiceInfo &info) = 0; + virtual int32_t GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) = 0; + virtual int32_t GetCallerServiceInfos(std::vector &serviceInfos) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index ba274c1bfa0149724a11ca03178ca0fd10a4195e..eb6ddf6be0b9b51f63ad77d511da65697972e50c 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -416,6 +416,12 @@ public: virtual int32_t GetDeviceIconInfo(const std::string &pkgName, const DmDeviceIconInfoFilterOptions &filterOptions, std::shared_ptr callback) override; + virtual int32_t GenerateServiceId(int64_t &serviceId) override; + virtual int32_t RegisterServiceInfo(const DMServiceInfo &info) override; + virtual int32_t UnRegisterServiceInfo(const DMServiceInfo &info) override; + virtual int32_t UpdateServiceInfo(const DMServiceInfo &info) override; + virtual int32_t GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) override; + virtual int32_t GetCallerServiceInfos(std::vector &serviceInfos) override; private: DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index 00d087fcbd26773ccd73e26acac0b5785ad89e6a..7ac41782b20bc4a2fdd0fd4dd65781f89d71351e 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -413,6 +413,42 @@ typedef struct DmNotifyKey { notifyUserId == other.notifyUserId && udid < other.udid); } } DmNotifyKey; + +typedef enum : int32_t { + DMServiceInfoAuthType_TrustOneTime = 0, + DMServiceInfoAuthType_TrustAlways = 6, + DMServiceInfoAuthType_Cancel = 1 +} DMServiceInfoAuthType; + +typedef enum : int32_t { + DMServiceInfoPinExchangeType_PinBox = 1, + DMServiceInfoPinExchangeType_UltraSound = 2, + DMServiceInfoPinExchangeType_FromDp = 3 +} DMServiceInfoExchangeType; + +typedef enum : int32_t { + DMServiceInfoAuthBoxType_3State = 1, + DMServiceInfoAuthBoxType_SkipConfirm = 2, + DMServiceInfoAuthBoxType_2In1 = 3 +} DMServiceInfoAuthBoxType; + +typedef struct DMServiceInfo { + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + std::string bundleName; + std::string moduleName; + std::string abilityName; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDiscoveryScope; +} DMServiceInfo; + } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 61228cb0f8fb85a48507be051055081f31a6a3fa..21fc1289a541f6b32b3fc547b991cdd8da6c0b7e 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -36,6 +36,8 @@ #include "ipc_destroy_pin_holder_req.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_get_serviceinfo_byid_req.h" +#include "ipc_gen_serviceid_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -48,10 +50,13 @@ #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" +#include "ipc_get_serviceinfo_bycaller_rsp.h" +#include "ipc_get_serviceinfo_byid_rsp.h" #include "ipc_import_auth_code_req.h" #include "ipc_notify_event_req.h" #include "ipc_permission_req.h" #include "ipc_publish_req.h" +#include "ipc_register_serviceinfo_req.h" #include "ipc_req.h" #include "ipc_rsp.h" #include "ipc_set_credential_req.h" @@ -2647,5 +2652,132 @@ int32_t DeviceManagerImpl::GetDeviceIconInfo(const std::string &pkgName, #endif return DM_OK; } + +int32_t DeviceManagerImpl::GenerateServiceId(int64_t &serviceId) +{ + LOGI("Start"); + LOGI("[rm003] GenerateServiceId"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GEN_SERVICEID, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + serviceId = rsp->GetServiceId(); + LOGI("Completed, serviceId %{public}" PRId64, serviceId); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterServiceInfo(const DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] RegisterServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceInfo(info); + int32_t ret = ipcClientProxy_->SendRequest(REG_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceInfo(const DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] UnRegisterServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceInfo(info); + int32_t ret = ipcClientProxy_->SendRequest(UNREG_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UpdateServiceInfo(const DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] UpdateServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceInfo(info); + int32_t ret = ipcClientProxy_->SendRequest(UPDATE_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] GetServiceInfoByServiceId, %{public}" PRId64, serviceId); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceId(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFO_BYID, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + info = rsp->GetServiceInfo(); + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetCallerServiceInfos(std::vector &serviceInfos) +{ + LOGI("Start"); + LOGI("[rm003] GetCallerServiceInfos"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFO_CALLER, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + serviceInfos = rsp->GetServiceInfos(); + LOGI("Completed, serviceInfos size %{public}zu", serviceInfos.size()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index 30a47da9206aedae9bc4de48106a141974dd8d63..90fe9a2f8bfddf00f37ea39059da5591f1a71537 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -32,6 +32,7 @@ #include "ipc_def.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_gen_serviceid_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -42,12 +43,16 @@ #include "ipc_get_info_by_network_rsp.h" #include "ipc_get_info_by_network_req.h" #include "ipc_get_local_device_info_rsp.h" +#include "ipc_get_serviceinfo_byid_req.h" +#include "ipc_get_serviceinfo_byid_rsp.h" +#include "ipc_get_serviceinfo_bycaller_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" #include "ipc_import_auth_code_req.h" #include "ipc_model_codec.h" #include "ipc_notify_event_req.h" #include "ipc_register_listener_req.h" +#include "ipc_register_serviceinfo_req.h" #include "ipc_req.h" #include "ipc_rsp.h" #include "ipc_set_credential_req.h" @@ -68,6 +73,15 @@ namespace { const int32_t DM_MAX_TRUST_DEVICE_NUM = 200; } +#define READ_HELPER_RET(parcel, type, out, failRet) \ + do { \ + bool ret = (parcel).Read##type((out)); \ + if (!ret) { \ + LOGE("read value failed!"); \ + return failRet; \ + } \ + } while (0) + void DecodeDmDeviceBasicInfo(MessageParcel &parcel, DmDeviceBasicInfo &devInfo) { std::string deviceIdStr = parcel.ReadString(); @@ -131,6 +145,70 @@ bool EncodeDmAccessCallee(const DmAccessCallee &callee, MessageParcel &parcel) return bRet; } +bool EncodeServiceInfo(const DMServiceInfo& serviceInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDisplayName)); + bRet = (bRet && parcel.WriteString(serviceInfo.customData)); + bRet = (bRet && parcel.WriteString(serviceInfo.bundleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.moduleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.abilityName)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authBoxType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.pinExchangeType)); + bRet = (bRet && parcel.WriteString(serviceInfo.pinCode)); + bRet = (bRet && parcel.WriteString(serviceInfo.description)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDiscoveryScope)); + return bRet; +} + +bool DecodeServiceInfo(MessageParcel &parcel, DMServiceInfo &serviceInfo) +{ + READ_HELPER_RET(parcel, Int64, serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDisplayName, false); + READ_HELPER_RET(parcel, String, serviceInfo.customData, false); + READ_HELPER_RET(parcel, String, serviceInfo.bundleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.moduleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.abilityName, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authBoxType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.pinExchangeType, false); + READ_HELPER_RET(parcel, String, serviceInfo.pinCode, false); + READ_HELPER_RET(parcel, String, serviceInfo.description, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDiscoveryScope, false); + return true; +} + +bool DecodeServiceInfos(MessageParcel &parcel, std::vector &serviceInfos) +{ + uint32_t num = 0; + READ_HELPER_RET(parcel, Uint32, num, false); + for (uint32_t k = 0; k < num; k++) { + DMServiceInfo serviceInfo; + READ_HELPER_RET(parcel, Int64, serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDisplayName, false); + READ_HELPER_RET(parcel, String, serviceInfo.customData, false); + READ_HELPER_RET(parcel, String, serviceInfo.bundleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.moduleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.abilityName, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authBoxType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.pinExchangeType, false); + READ_HELPER_RET(parcel, String, serviceInfo.pinCode, false); + READ_HELPER_RET(parcel, String, serviceInfo.description, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDiscoveryScope, false); + serviceInfos.emplace_back(serviceInfo); + } + return true; +} + ON_IPC_SET_REQUEST(REGISTER_DEVICE_MANAGER_LISTENER, std::shared_ptr pBaseReq, MessageParcel &data) { if (pBaseReq == nullptr) { @@ -2164,5 +2242,179 @@ ON_IPC_CMD(GET_DEVICE_ICON_INFO_RESULT, MessageParcel &data, MessageParcel &repl reply.WriteInt32(DM_OK); return DM_OK; } + +ON_IPC_SET_REQUEST(GEN_SERVICEID, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GEN_SERVICEID"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GEN_SERVICEID, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GEN_SERVICEID"); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + if (pRsp == nullptr) { + LOGE("pRsp is null"); + return ERR_DM_FAILED; + } + pRsp->SetErrCode(reply.ReadInt32()); + if (pRsp->GetErrCode() == DM_OK) { + pRsp->SetServiceId(reply.ReadInt64()); + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(REG_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + LOGI("[rm003] ON_IPC_SET_REQUEST(REG_SERVICE_INFO"); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + const DMServiceInfo& info = pReq->GetServiceInfo(); + if (!EncodeServiceInfo(info, data)) { + LOGE("EncodeServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(REG_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + const DMServiceInfo& info = pReq->GetServiceInfo(); + if (!EncodeServiceInfo(info, data)) { + LOGE("EncodeServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + const DMServiceInfo& info = pReq->GetServiceInfo(); + if (!EncodeServiceInfo(info, data)) { + LOGE("EncodeServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetServiceId(); + if (!data.WriteInt64(serviceId)) { + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + if (pBaseRsp->GetErrCode() == DM_OK) { + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + DMServiceInfo info; + bool ret = DecodeServiceInfo(reply, info); + if (!ret) { + LOGE("[rm003] DecodeServiceInfo failed"); + pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); + } + pRsp->SetServiceInfo(info); + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(GET_SERVICEINFO_CALLER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFO_CALLER"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_SERVICEINFO_CALLER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFO_CALLER"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + if (pBaseRsp->GetErrCode() == DM_OK) { + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + std::vector infos; + bool ret = DecodeServiceInfos(reply, infos); + if (!ret) { + LOGE("[rm003] DecodeServiceInfo failed"); + pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); + } + pRsp->SetServiceInfos(infos); + } + return DM_OK; +} + } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js4.0/src/dm_native_util.cpp b/interfaces/kits/js4.0/src/dm_native_util.cpp index 1d8a814dd64b09682fed3524b6bd94fb21b33259..e60d56435e709b68b43dedb15430634b8601f179 100644 --- a/interfaces/kits/js4.0/src/dm_native_util.cpp +++ b/interfaces/kits/js4.0/src/dm_native_util.cpp @@ -42,6 +42,7 @@ const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_ONE = -1; const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_TWO = -2; const int32_t DM_NAPI_DESCRIPTION_BUF_LENGTH = 16384; const int32_t DM_NAPI_BUF_LENGTH = 256; +const int32_t DM_NAPI_CONN_SESSIONTYPE_LENGTH = 16; const int32_t MAX_OBJECT_LEN = 4096; void JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr, @@ -336,6 +337,15 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b int32_t bindLevel = 0; JsObjectToInt(env, object, "bindLevel", bindLevel); + char connSessionType[DM_NAPI_CONN_SESSIONTYPE_LENGTH] = ""; + JsObjectToString(env, object, "connSessionType", connSessionType, sizeof(connSessionType)); + int32_t hmlReleaseTime = 0; + JsObjectToInt(env, object, "hmlReleaseTime", hmlReleaseTime); + int32_t hmlActionId = 0; + JsObjectToInt(env, object, "hmlActionId", hmlActionId); + bool hmlEnable160M = false; + JsObjectToBool(env, object, "hmlEnable160M", hmlEnable160M); + nlohmann::json jsonObj; jsonObj[AUTH_TYPE] = bindType; jsonObj[APP_OPERATION] = std::string(appOperation); @@ -351,6 +361,11 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b jsonObj[PARAM_KEY_WIFI_IP] = std::string(wifiIP); jsonObj[PARAM_KEY_WIFI_PORT] = wifiPort; jsonObj[BIND_LEVEL] = bindLevel; + + jsonObj[PARAM_KEY_CONN_SESSIONTYPE] = std::string(connSessionType); + jsonObj[PARAM_KEY_HML_RELEASETIME] = hmlReleaseTime; + jsonObj[PARAM_KEY_HML_ENABLE_160M] = hmlEnable160M; + jsonObj[PARAM_KEY_HML_ACTIONID] = hmlActionId; bindParam = SafetyDump(jsonObj); } diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index bb13e8d1397fbea933050be4157c04440508285b..0ca0774bda36b05a49857faa21b2ddbf6dd86aa3 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1658,6 +1658,52 @@ napi_value DeviceManagerNapi::UnPublishDeviceDiscoverySync(napi_env env, napi_ca return result; } +bool xProcessRegisterServiceInfo(std::string bindParam) +{ + nlohmann::json bindParamObj = nlohmann::json::parse(bindParam, nullptr, false); + if (bindParamObj.is_discarded()) { + return false; + } + if (IsInt32(bindParamObj, AUTH_TYPE)) { + int32_t op = bindParamObj[AUTH_TYPE].get(); + if (op == 11) { + LOGI("[temp003] xProcessRegisterServiceInfo"); + std::string pincode; + if (IsString(bindParamObj, PARAM_KEY_PIN_CODE)) { + pincode = bindParamObj[PARAM_KEY_PIN_CODE].get(); + LOGI("[temp003] pincode = %{public}s", pincode.c_str()); + } + if (pincode.empty()) { + pincode = "100000"; + LOGI("[temp003] set pincode to 100000"); + } + DMServiceInfo serviceInfo; + serviceInfo.serviceType = "profile.serviceType"; + serviceInfo.serviceName = "profile.serviceName"; + serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; + serviceInfo.customData = "profile.customData"; + serviceInfo.bundleName = "profile.bundleName"; + serviceInfo.moduleName = "profile.moduleName"; + serviceInfo.abilityName = "profile.abilityName"; + serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; + serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; + serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; + serviceInfo.pinCode = pincode; + serviceInfo.description = "profile.description"; + serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; + int32_t err = DeviceManager::GetInstance().GenerateServiceId(serviceInfo.serviceId); + if (err == DM_OK) { + DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo); + } else { + LOGI("[temp003] DeviceManager::GenerateServiceId failed"); + } + LOGI("[temp003] DeviceManager::GetInstance().RegisterServiceInfo complete"); + return true; + } + } + return false; +} + void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, const std::string &deviceId, napi_env env, napi_value &object) { @@ -1666,6 +1712,10 @@ void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, bool isMetaType = false; JsToBindParam(env, object, bindParam, authAsyncCallbackInfo_.authType, isMetaType); + bool processRet = xProcessRegisterServiceInfo(bindParam); + if (processRet) { + return; + } if (isMetaType) { std::shared_ptr bindTargetCallback = nullptr; { diff --git a/sa_profile/device_manager.cfg b/sa_profile/device_manager.cfg index 4a495502fd274812e3b69b1b2725dabc1bf0279c..de736fd0aece8418c29f8bdc0a6d6f1a37725455 100644 --- a/sa_profile/device_manager.cfg +++ b/sa_profile/device_manager.cfg @@ -30,6 +30,7 @@ "ohos.permission.ACCESS_BLUETOOTH", "ohos.permission.MANAGE_BLUETOOTH", "ohos.permission.MANAGE_SETTINGS", + "ohos.permission.MANAGE_SOFTBUS_NETWORK", "ohos.permission.START_SYSTEM_DIALOG", "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", "ohos.permission.GET_BUNDLE_RESOURCES", diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index d21279a34ad47be10783315c8473819e23a254e9..b97818a49e71ec905465894b8d15611291295b0c 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -43,6 +43,7 @@ constexpr const char* TAG_INDEX = "INDEX"; constexpr const char* TAG_SLICE_NUM = "SLICE"; constexpr const char* TAG_IS_AUTH_CODE_READY = "IS_AUTH_CODE_READY"; constexpr const char* TAG_IS_SHOW_DIALOG = "IS_SHOW_DIALOG"; +constexpr const char* TAG_PINCODE_SETTED = "TAG_PINCODE_SETTED"; constexpr const char* TAG_TOKEN = "TOKEN"; constexpr const char* TAG_CRYPTO_NAME = "CRYPTONAME"; constexpr const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION"; diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 760000cb4fc6e5f1de7aa61e1e6c6b2395a184f9..9a51c6a53e32c0c1d643b9d919e178098ae499e8 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -33,6 +33,7 @@ #include "dm_timer.h" #include "hichain_auth_connector.h" #include "hichain_connector.h" +#include "service_info_unique_key.h" #include "softbus_connector.h" #include "softbus_session.h" @@ -134,8 +135,27 @@ typedef struct DmAuthRequestContext { std::string addr; std::string hostPkgLabel; int32_t closeSessionDelaySeconds = 0; + int32_t presetPinCode = 0; + std::string connSessionType; + int32_t hmlReleaseTime = 0; + bool hmlEnable160M = false; + int32_t hmlActionId = 0; } DmAuthRequestContext; +typedef struct DMRecheckData { + std::string edition; + std::string localDeviceId; + int32_t localUserId; + std::string localAccountId; + int64_t tokenId; + std::string bundleName; + int32_t localBindLevel; + std::mutex recheckDataMutex; + bool isDataReceived = false; + bool isDataParsed = false; + bool isDataVerified = false; +} DMRecheckData; + typedef struct DmAuthResponseContext { int32_t authType; std::string deviceId; @@ -172,6 +192,7 @@ typedef struct DmAuthResponseContext { std::vector syncGroupList; std::string accountGroupIdHash; std::string publicKey; + std::string remotePublicKey; bool isOnline; int32_t bindLevel; bool haveCredential; @@ -191,6 +212,8 @@ typedef struct DmAuthResponseContext { bool isFinish = false; std::string edition; int32_t localBindLevel; + DMRecheckData recheckData; + bool isPincodeSetted = false; } DmAuthResponseContext; class AuthMessageProcessor; @@ -403,7 +426,7 @@ public: * @tc.desc: Show AuthInfo Dialog of the DeviceManager Authenticate Manager * @tc.type: FUNC */ - void ShowAuthInfoDialog(); + void ShowAuthInfoDialog(bool authDeviceError = false); /** * @tc.name: DmAuthManager::ShowStartAuthDialog @@ -472,8 +495,13 @@ public: void HandleSessionHeartbeat(std::string name); int32_t RegisterAuthenticationType(int32_t authenticationType); + static bool IsPinCodeValid(std::string strpin); + static bool IsPinCodeValid(int32_t numpin); private: + bool IsHmlSessionType(); + bool IsUsePinFromDp(); + void InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra); int32_t CheckAuthParamVaildExtra(const std::string &extra); @@ -508,6 +536,8 @@ public: void GenerateCredential(std::string &publicKey); void RequestCredentialDone(); void ResponseCredential(); + bool ResponseNewProcessCredential(); + void ResponseNewSendCredential(); bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); void AuthDeviceFinish(int64_t requestId); void AuthDeviceError(int64_t requestId, int32_t errorCode); @@ -519,9 +549,15 @@ public: int32_t DeleteGroup(const std::string &pkgName, const std::string &deviceId); int32_t DeleteGroup(const std::string &pkgName, int32_t userId, const std::string &deviceId); int32_t StopAuthenticateDevice(const std::string &pkgName); + bool RemoteSupportRecheck(); + bool IsRecheckDataReceived(); + bool IsRecheckDataParsed(); + bool IsRecheckDataVerified(); void RequestReCheckMsg(); void ResponseReCheckMsg(); void RequestReCheckMsgDone(); + void ResponseReCheckMsgDone(); + void ParseSavedFailedRecheckMsg(); private: int32_t ImportCredential(std::string &deviceId, std::string &publicKey); void GetAuthParam(const std::string &pkgName, int32_t authType, const std::string &deviceId, @@ -552,6 +588,7 @@ private: int32_t GetBindLevel(int32_t bindLevel); std::string GetBundleName(nlohmann::json &jsonObject); int32_t GetBinderInfo(); + int64_t GetBinderTokenId(); void SetProcessInfo(); int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr); void ConverToFinish(); @@ -597,6 +634,13 @@ private: bool isNeedProcCachedSrcReqMsg_ = false; std::string srcReqMsg_ = ""; int32_t authenticationType_ = USER_OPERATION_TYPE_ALLOW_AUTH; + + std::mutex recheckPublicKeyProcessMutex_; + std::string savedParseFailedRecheckMsg_; + std::mutex savedParseFailedRecheckMsgMutex_; + + DistributedDeviceProfile::ServiceInfoProfile serviceInfoProfile_; + bool pincodeDialogEverShown_ = false; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/softbus/softbus_connector.h b/services/implementation/include/dependency/softbus/softbus_connector.h index 2536eeaccc594b880040c6311129e99ec569325d..30d44b6b1fd3b78f7067a86aea4d5e13266ddee9 100644 --- a/services/implementation/include/dependency/softbus/softbus_connector.h +++ b/services/implementation/include/dependency/softbus/softbus_connector.h @@ -77,6 +77,13 @@ public: * @tc.type: FUNC */ static void JoinLnn(const std::string &deviceId); + + /** + * @tc.name: SoftbusConnector::JoinLnnByHml + * @tc.desc: join lnn by session id + * @tc.type: FUNC + */ + static void JoinLnnByHml(const int32_t sessionId); public: SoftbusConnector(); ~SoftbusConnector(); diff --git a/services/implementation/include/dependency/softbus/softbus_session.h b/services/implementation/include/dependency/softbus/softbus_session.h index 99387bb7618119b0596fd902370fec8c7c563128..2e7be8f5712f2045cbaced3dbb18fd382056820d 100644 --- a/services/implementation/include/dependency/softbus/softbus_session.h +++ b/services/implementation/include/dependency/softbus/softbus_session.h @@ -60,6 +60,13 @@ public: */ int32_t OpenAuthSession(const std::string &deviceId); + /** + * @tc.name: SoftbusSession::OpenAuthSessionWithPara + * @tc.desc: Open HML AuthSession of the Softbus Session + * @tc.type: FUNC + */ + int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool enable160m); + /** * @tc.name: SoftbusSession::CloseAuthSession * @tc.desc: Close AuthSession of the Softbus Session diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index b67246a0b4afb00059c8509d13377ccd84db51e2..a27cacd02e3b859e9fd53a2b17f28a749cbab763 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -71,6 +71,13 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } else { jsonObj[TAG_IS_SHOW_DIALOG] = true; } + if (DmAuthManager::IsPinCodeValid(authRequestContext_->presetPinCode)) { + LOGI("[rm003-nopincode] TAG_PINCODE_SETTED"); + jsonObj[TAG_PINCODE_SETTED] = true; + } else { + LOGI("[rm003-nopincode] TAG_PINCODE_SETTED not set"); + jsonObj[TAG_PINCODE_SETTED] = false; + } jsonObj[TAG_APP_OPERATION] = authRequestContext_->appOperation; jsonObj[TAG_CUSTOM_DESCRIPTION] = authRequestContext_->customDesc; jsonObj[TAG_APP_NAME] = authRequestContext_->appName; @@ -167,6 +174,7 @@ void AuthMessageProcessor::CreatePublicKeyMessageExt(nlohmann::json &json) return; } else { nlohmann::json jsonTemp; + LOGI("rm003] TAG_PUBLICKEY %{public}s", authResponseContext_->publicKey.c_str()); jsonTemp[TAG_PUBLICKEY] = authResponseContext_->publicKey; std::string strTemp = SafetyDump(jsonTemp); std::string encryptStr = ""; @@ -306,7 +314,7 @@ int32_t AuthMessageProcessor::ParseMessage(const std::string &message) } int32_t msgType = jsonObject[TAG_MSG_TYPE].get(); authResponseContext_->msgType = msgType; - LOGI("AuthMessageProcessor::ParseMessage message type %{public}d", authResponseContext_->msgType); + LOGI("AuthMessageProcessor::ParseMessage msgType message type %{public}d", authResponseContext_->msgType); switch (msgType) { case MSG_TYPE_NEGOTIATE: ParseNegotiateMessage(jsonObject); @@ -348,13 +356,13 @@ void AuthMessageProcessor::ParsePublicKeyMessageExt(nlohmann::json &json) encryptFlag = encryptFlag_; } if (!encryptFlag && IsString(json, TAG_PUBLICKEY)) { - authResponseContext_->publicKey = json[TAG_PUBLICKEY].get(); + authResponseContext_->remotePublicKey = json[TAG_PUBLICKEY].get(); return; } if (encryptFlag && IsString(json, TAG_CRYPTIC_MSG)) { std::string encryptStr = json[TAG_CRYPTIC_MSG].get(); std::string decryptStr = ""; - authResponseContext_->publicKey = ""; + authResponseContext_->remotePublicKey = ""; CHECK_NULL_VOID(cryptoMgr_); if (cryptoMgr_->DecryptMessage(encryptStr, decryptStr) != DM_OK) { LOGE("DecryptMessage failed."); @@ -366,7 +374,7 @@ void AuthMessageProcessor::ParsePublicKeyMessageExt(nlohmann::json &json) return; } if (IsString(jsonObject, TAG_PUBLICKEY)) { - authResponseContext_->publicKey = jsonObject[TAG_PUBLICKEY].get(); + authResponseContext_->remotePublicKey = jsonObject[TAG_PUBLICKEY].get(); } return; } @@ -457,6 +465,12 @@ int32_t AuthMessageProcessor::ParseAuthRequestMessage(nlohmann::json &json) } else { authResponseContext_->isShowDialog = true; } + if (IsBool(json, TAG_PINCODE_SETTED)) { + authResponseContext_->isPincodeSetted = json[TAG_PINCODE_SETTED].get(); + } else { + authResponseContext_->isPincodeSetted = false; + } + LOGI("[rm003-nopincode] isPincodeSetted %{public}d", authResponseContext_->isPincodeSetted); if (IsInt32(json, TAG_BIND_TYPE_SIZE)) { int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].get(); if (bindTypeSize > MAX_BINDTYPE_SIZE) { @@ -687,6 +701,9 @@ void AuthMessageProcessor::CreateReqReCheckMessage(nlohmann::json &jsonObj) void AuthMessageProcessor::ParseReqReCheckMessage(nlohmann::json &json) { + std::lock_guard guard(authResponseContext_->recheckData.recheckDataMutex); + authResponseContext_->recheckData.isDataReceived = true; + LOGE("[rm003] isDataReceived = true"); std::string encryptStr = ""; if (IsString(json, TAG_CRYPTIC_MSG)) { encryptStr = json[TAG_CRYPTIC_MSG].get(); @@ -730,6 +747,16 @@ void AuthMessageProcessor::ParseReqReCheckMessage(nlohmann::json &json) if (IsInt32(jsonObject, TAG_BIND_LEVEL)) { authResponseContext_->localBindLevel = jsonObject[TAG_BIND_LEVEL].get(); } + + LOGE("[rm003] isDataParsed = true"); + authResponseContext_->recheckData.isDataParsed = true; + authResponseContext_->recheckData.edition = authResponseContext_->edition; + authResponseContext_->recheckData.localDeviceId = authResponseContext_->localDeviceId; + authResponseContext_->recheckData.localUserId = authResponseContext_->localUserId; + authResponseContext_->recheckData.localAccountId = authResponseContext_->localAccountId; + authResponseContext_->recheckData.tokenId = authResponseContext_->tokenId; + authResponseContext_->recheckData.bundleName = authResponseContext_->bundleName; + authResponseContext_->recheckData.localBindLevel = authResponseContext_->localBindLevel; } int32_t AuthMessageProcessor::SaveSessionKey(const uint8_t *sessionKey, const uint32_t keyLen) diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 1d284dab319c86f50b0c62a9c70aea1ca5dca597..0033b9e4facdbdcc7be215d41939ef9f3132b77d 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -63,6 +63,7 @@ const int32_t CLONE_ADD_TIMEOUT = 10; const int32_t CLONE_WAIT_NEGOTIATE_TIMEOUT = 10; const int32_t CLONE_WAIT_REQUEST_TIMEOUT = 10; const int32_t CLONE_SESSION_HEARTBEAT_TIMEOUT = 20; +const int32_t HML_SESSION_TIMEOUT = 10; const int32_t CANCEL_PIN_CODE_DISPLAY = 1; const int32_t DEVICE_ID_HALF = 2; const int32_t MAX_AUTH_TIMES = 3; @@ -127,6 +128,12 @@ DmAuthManager::~DmAuthManager() LOGI("DmAuthManager destructor"); } +bool DmAuthManager::IsHmlSessionType() +{ + CHECK_NULL_RETURN(authRequestContext_, false); + return authRequestContext_->connSessionType == CONN_SESSION_TYPE_HML; +} + int32_t DmAuthManager::CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { @@ -179,6 +186,25 @@ int32_t DmAuthManager::CheckAuthParamVaild(const std::string &pkgName, int32_t a int32_t DmAuthManager::CheckAuthParamVaildExtra(const std::string &extra) { nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false); + if (jsonObject.is_discarded()) { + return DM_OK; + } + std::string connSessionType; + if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { + connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); + } + if (connSessionType == CONN_SESSION_TYPE_HML) { + if (!IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { + LOGE("[rm003] CONN_SESSION_TYPE_HML, input actionId is !Int32"); + return ERR_DM_INPUT_PARA_INVALID; + } + int32_t actionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); + if (actionId <= 0) { + LOGE("[rm003] CONN_SESSION_TYPE_HML, input actionId is <=0"); + return ERR_DM_INPUT_PARA_INVALID; + } + } + if (jsonObject.is_discarded() || jsonObject.find(TAG_BIND_LEVEL) == jsonObject.end() || !IsInt32(jsonObject, TAG_BIND_LEVEL)) { return DM_OK; @@ -251,6 +277,9 @@ void DmAuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, void DmAuthManager::ParseJsonObject(nlohmann::json jsonObject) { + std::string debug = SafetyDump(jsonObject); + LOGI("[rm003] debug %{public}s", debug.c_str()); + if (!jsonObject.is_discarded()) { if (IsString(jsonObject, TARGET_PKG_NAME_KEY)) { authRequestContext_->targetPkgName = jsonObject[TARGET_PKG_NAME_KEY].get(); @@ -281,6 +310,37 @@ void DmAuthManager::ParseJsonObject(nlohmann::json jsonObject) } else { authRequestContext_->peerBundleName = authRequestContext_->hostPkgName; } + if (IsString(jsonObject, PARAM_KEY_PIN_CODE)) { + std::string strpin = jsonObject[PARAM_KEY_PIN_CODE].get(); + if (DmAuthManager::IsPinCodeValid(strpin)) { + authRequestContext_->presetPinCode = std::stoi(strpin); + LOGI("[rm003-nopincode] input pincode %{public}s", strpin.c_str()); + } + } + + if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { + authRequestContext_->connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); + LOGI("[rm003-nopincode] input connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); + } + if (IsInt32(jsonObject, PARAM_KEY_HML_RELEASETIME)) { + authRequestContext_->hmlReleaseTime = jsonObject[PARAM_KEY_HML_RELEASETIME].get(); + LOGI("[rm003-nopincode] input hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + } + if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { + authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); + LOGI("[rm003-nopincode] input hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + } + if (IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { + authRequestContext_->hmlActionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); + if (authRequestContext_->hmlActionId <= 0) { + authRequestContext_->hmlActionId = 0; + } + LOGI("[rm003-nopincode] input hmlActionId %{public}d", authRequestContext_->hmlActionId); + } + if(IsHmlSessionType()) { + LOGI("[rm003] set closeSessionDelaySeconds = HML_SESSION_TIMEOUT"); + authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; + } } authRequestContext_->bundleName = GetBundleName(jsonObject); } @@ -572,15 +632,34 @@ void DmAuthManager::ProcessSourceMsg() } break; case MSG_TYPE_RESP_PUBLICKEY: - if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_CREDENTIAL) { - authRequestState_->TransitionTo(std::make_shared()); + { + std::lock_guard guard(recheckPublicKeyProcessMutex_); + if (RemoteSupportRecheck()) { + if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_RECHECK_MSG) { + authRequestState_->TransitionTo(std::make_shared()); + } else { + LOGE("[rm003] not in state, skip MSG_TYPE_RESP_PUBLICKEY"); + } + } else { + if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_CREDENTIAL) { + authRequestState_->TransitionTo(std::make_shared()); + } else { + LOGE("[rm003] not in state, skip MSG_TYPE_RESP_PUBLICKEY"); + } } break; + } case MSG_TYPE_RESP_RECHECK_MSG: - if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_RECHECK_MSG) { - authRequestState_->TransitionTo(std::make_shared()); + { + std::lock_guard guard(recheckPublicKeyProcessMutex_); + if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_AUTH_FINISH || + authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_RECHECK_MSG) { + RequestReCheckMsgDone(); + } else { + LOGE("[rm003] not in state, skip MSG_TYPE_RESP_RECHECK_MSG"); } break; + } default: break; } @@ -619,15 +698,18 @@ void DmAuthManager::ProcessSinkMsg() ProcessReqPublicKey(); break; case MSG_TYPE_REQ_RECHECK_MSG: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_AUTH_FINISH) { - authResponseState_->TransitionTo(std::make_shared()); - break; + { + std::lock_guard guard(recheckPublicKeyProcessMutex_); + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_AUTH_FINISH || + authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_RECHECK_MSG) { + ResponseReCheckMsgDone(); } if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_SHOW) { std::lock_guard lock(srcReqMsgLock_); isNeedProcCachedSrcReqMsg_ = true; } break; + } default: break; } @@ -649,6 +731,18 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me return; } + if (authResponseContext_->msgType == MSG_TYPE_RESP_RECHECK_MSG || + authResponseContext_->msgType == MSG_TYPE_REQ_RECHECK_MSG) { + std::lock_guard guard(savedParseFailedRecheckMsgMutex_); + if (!IsRecheckDataParsed()) { + LOGE("[rm003] parse failed, save RECHECK_MSG"); + savedParseFailedRecheckMsg_ = message; + return; + } else { + LOGI("[rm003] savedParseFailedRecheckMsg_.clear"); + savedParseFailedRecheckMsg_.clear(); + } + } if ((authRequestState_ != nullptr) && (authResponseState_ == nullptr)) { // source device auth process ProcessSourceMsg(); @@ -702,6 +796,7 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); authResponseContext_->isFinish = true; authResponseState_->TransitionTo(std::make_shared()); + LOGI("[rm003] OnGroupCreated pincode %{public}d", authResponseContext_->code); } void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) @@ -800,7 +895,16 @@ void DmAuthManager::HandleAuthenticateTimeout(std::string name) int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) { - int32_t sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); + int32_t sessionId = 0; + if (IsHmlSessionType()) { + LOGI("[rm003] EstablishAuthChannel, try OpenAuthSessionWithPara"); + LOGI("[rm003] hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + LOGI("[rm003] hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + LOGI("[rm003] hmlActionId %{public}d", authRequestContext_->hmlActionId); + sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSessionWithPara(deviceId, authRequestContext_->hmlActionId, authRequestContext_->hmlEnable160M); + } else { + sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); + } struct RadarInfo info = { .funcName = "EstablishAuthChannel", .stageRes = (sessionId > 0) ? @@ -1091,7 +1195,11 @@ bool DmAuthManager::IsAuthFinish() if ((authResponseContext_->isIdenticalAccount && !authResponseContext_->authed) || (authResponseContext_->authed && !authResponseContext_->isOnline)) { - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } authRequestContext_->reason = DM_OK; authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authResponseContext_->reply = DM_OK; @@ -1140,7 +1248,10 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) authMessageProcessor_->SetResponseContext(authResponseContext_); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { - if (!authResponseContext_->isShowDialog) { + if (IsUsePinFromDp() && authResponseContext_->isPincodeSetted) { + authResponseContext_->code = std::stoi(serviceInfoProfile_.GetPinCode()); + LOGI("[rm003-nopincode] import from dp, pin %{public}d", authResponseContext_->code); + } else if (!authResponseContext_->isShowDialog) { GetAuthCode(authResponseContext_->hostPkgName, authResponseContext_->code); } else { authResponseContext_->code = GeneratePincode(); @@ -1149,6 +1260,7 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) authResponseState_->TransitionTo(std::make_shared()); } authMessageProcessor_->SetResponseContext(authResponseContext_); + LOGI("[rm003-nopincode] authResponseContext_->code %{public}d", authResponseContext_->code); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_AUTH_EXT); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); return DM_OK; @@ -1375,6 +1487,10 @@ void DmAuthManager::AuthenticateFinish() std::lock_guard lock(srcReqMsgLock_); srcReqMsg_ = ""; isNeedProcCachedSrcReqMsg_ = false; + + pincodeDialogEverShown_ = false; + std::lock_guard guard(savedParseFailedRecheckMsgMutex_); + savedParseFailedRecheckMsg_.clear(); } authType_ = AUTH_TYPE_UNKNOW; std::lock_guard autoLock(g_authFinishLock); @@ -1440,6 +1556,43 @@ int32_t DmAuthManager::GeneratePincode() return GenRandInt(MIN_PIN_CODE, MAX_PIN_CODE); } +bool DmAuthManager::IsPinCodeValid(std::string strpin) +{ + if (strpin.empty()) { + return false; + } + int pinnum = std::stoi(strpin); + return IsPinCodeValid(pinnum); +} + +bool DmAuthManager::IsPinCodeValid(int32_t numpin) +{ + if (numpin < MIN_PIN_CODE || numpin > MAX_PIN_CODE) { + return false; + } + return true; +} + +bool DmAuthManager::IsUsePinFromDp() +{ + return (serviceInfoProfile_.GetPinExchangeType() == DMServiceInfoPinExchangeType_FromDp && + DmAuthManager::IsPinCodeValid(serviceInfoProfile_.GetPinCode())); +} + +void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + Crypto::GetUdidHash(localDeviceId, reinterpret_cast(udidHash)); + key.SetDeviceId(std::string(udidHash)); + int64_t tokenId = GetBinderTokenId(); + key.SetTokenId(std::to_string(tokenId)); + int32_t userId = MultipleUserConnector::GetFirstForegroundUserId(); + key.SetUserId(userId); + LOGI("[rm003] localDeviceId %{public}s, userId %{public}d, tokenId %{public}" PRId64, udidHash, userId, tokenId); +} + std::string DmAuthManager::GenerateGroupName() { if (authResponseContext_ == nullptr) { @@ -1510,6 +1663,7 @@ int32_t DmAuthManager::GetPinCode(int32_t &code) } LOGI("ShowConfigDialog start add member pin code."); code = authResponseContext_->code; + LOGI("[rm003] hichain get pin code %{public}d", code); return DM_OK; } @@ -1519,6 +1673,11 @@ void DmAuthManager::ShowConfigDialog() LOGE("failed to ShowConfigDialog because authResponseContext_ is nullptr"); return; } + if (serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { + LOGI("[rm003-nopincode] no need confirm dialog"); + StartAuthProcess(serviceInfoProfile_.GetAuthType()); + return; + } if (!authResponseContext_->isShowDialog) { LOGI("start auth process"); StartAuthProcess(authenticationType_); @@ -1551,13 +1710,20 @@ void DmAuthManager::ShowConfigDialog() LOGI("ShowConfigDialog end"); } -void DmAuthManager::ShowAuthInfoDialog() +void DmAuthManager::ShowAuthInfoDialog(bool authDeviceError) { if (authResponseContext_ == nullptr) { LOGE("failed to ShowAuthInfoDialog because authResponseContext_ is nullptr"); return; } LOGI("DmAuthManager::ShowAuthInfoDialog start"); + if (!authDeviceError && IsUsePinFromDp() && authResponseContext_->isPincodeSetted) { + LOGI("[rm003-nopincode] pin import from dp, not show dialog"); + return; + } + if (pincodeDialogEverShown_) { + return; + } if (!authResponseContext_->isShowDialog) { LOGI("not show dialog."); return; @@ -1572,6 +1738,7 @@ void DmAuthManager::ShowAuthInfoDialog() nlohmann::json jsonObj; jsonObj[PIN_CODE_KEY] = authResponseContext_->code; std::string authParam = SafetyDump(jsonObj); + pincodeDialogEverShown_ = true; DmDialogManager::GetInstance().ShowPinDialog(std::to_string(authResponseContext_->code)); } @@ -1606,7 +1773,14 @@ void DmAuthManager::ShowStartAuthDialog() return; } LOGI("DmAuthManager::ShowStartAuthDialog start"); - DmDialogManager::GetInstance().ShowInputDialog(authResponseContext_->targetDeviceName); + LOGI("[rm003] presetPinCode %{public}d", authRequestContext_->presetPinCode); + if (authRequestContext_->presetPinCode > 0) { + LOGI("[rm003] already has pin code"); + ProcessPincode(authRequestContext_->presetPinCode); + } else { + pincodeDialogEverShown_ = true; + DmDialogManager::GetInstance().ShowInputDialog(authResponseContext_->targetDeviceName); + } } int32_t DmAuthManager::ProcessPincode(int32_t pinCode) @@ -1618,6 +1792,7 @@ int32_t DmAuthManager::ProcessPincode(int32_t pinCode) if (timer_ != nullptr) { timer_->DeleteTimer(std::string(INPUT_TIMEOUT_TASK)); } + LOGI("[rm003] ProcessPincode %{public}d", pinCode); if (CompareVersion(remoteVersion_, std::string(DM_VERSION_4_1_5_1)) && (static_cast(authResponseContext_->bindLevel) >= DEVICE && static_cast(authResponseContext_->bindLevel) <= APP)) { @@ -1995,6 +2170,7 @@ void DmAuthManager::RequestCredential() LOGI("DmAuthManager::RequestCredential start."); std::string publicKey = ""; GenerateCredential(publicKey); + LOGI("[rm003] RequestCredential pk %{public}s", publicKey.c_str()); authResponseContext_->publicKey = publicKey; std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_PUBLICKEY); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); @@ -2017,14 +2193,18 @@ void DmAuthManager::RequestCredentialDone() { LOGI("DmAuthManager ExchangeCredentailDone start"); CHECK_NULL_VOID(authResponseContext_); - if (authResponseContext_->publicKey == "" || - ImportCredential(remoteDeviceId_, authResponseContext_->publicKey) != DM_OK) { + if (authResponseContext_->remotePublicKey == "" || + ImportCredential(remoteDeviceId_, authResponseContext_->remotePublicKey) != DM_OK) { LOGE("RequestCredentialDone import credential failed."); } if (timer_ != nullptr) { timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); } - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authResponseContext_->reply = DM_OK; @@ -2034,6 +2214,7 @@ void DmAuthManager::RequestCredentialDone() int32_t DmAuthManager::ImportCredential(std::string &deviceId, std::string &publicKey) { LOGI("DmAuthManager::ImportCredential"); + LOGI("[rm003] ImportCredential, pk %{public}s", publicKey.c_str()); int32_t osAccountId = MultipleUserConnector::GetCurrentAccountUserID(); return hiChainAuthConnector_->ImportCredential(osAccountId, deviceId, publicKey); } @@ -2042,8 +2223,8 @@ void DmAuthManager::ResponseCredential() { LOGI("DmAuthManager::ResponseCredential start."); CHECK_NULL_VOID(authResponseContext_); - if (authResponseContext_->publicKey == "") { - LOGE("authResponseContext_->publicKey is empty."); + if (authResponseContext_->remotePublicKey == "") { + LOGE("authResponseContext_->remotePublicKey is empty."); authResponseContext_->isFinish = false; isFinishOfLocal_ = false; authMessageProcessor_->SetEncryptFlag(false); @@ -2052,9 +2233,11 @@ void DmAuthManager::ResponseCredential() } std::string publicKey = ""; GenerateCredential(publicKey); - if (ImportCredential(remoteDeviceId_, authResponseContext_->publicKey) != DM_OK) { + LOGI("rm003] ResponseCredential pk %{public}s", publicKey.c_str()); + if (ImportCredential(remoteDeviceId_, authResponseContext_->remotePublicKey) != DM_OK) { LOGE("ResponseCredential import credential failed."); authResponseState_->TransitionTo(std::make_shared()); + softbusConnector_->GetSoftbusSession()->CloseAuthSession(authResponseContext_->sessionId); return; } authResponseContext_->publicKey = publicKey; @@ -2062,6 +2245,40 @@ void DmAuthManager::ResponseCredential() softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); } +bool DmAuthManager::ResponseNewProcessCredential() +{ + LOGI("[rm003] ResponseNewProcessCredential, pk %{public}s", authResponseContext_->remotePublicKey.c_str()); + CHECK_NULL_RETURN(authResponseContext_, false); + if (authResponseContext_->remotePublicKey == "") { + LOGE("authResponseContext_->remotePublicKey is empty."); + authResponseContext_->isFinish = false; + isFinishOfLocal_ = false; + authMessageProcessor_->SetEncryptFlag(false); + authResponseState_->TransitionTo(std::make_shared()); + softbusConnector_->GetSoftbusSession()->CloseAuthSession(authResponseContext_->sessionId); + return false; + } + if (ImportCredential(remoteDeviceId_, authResponseContext_->remotePublicKey) != DM_OK) { + LOGE("ResponseCredential import credential failed."); + authResponseState_->TransitionTo(std::make_shared()); + softbusConnector_->GetSoftbusSession()->CloseAuthSession(authResponseContext_->sessionId); + return false; + } + return true; +} + +void DmAuthManager::ResponseNewSendCredential() +{ + LOGI("DmAuthManager::ResponseCredential start."); + CHECK_NULL_VOID(authResponseContext_); + std::string publicKey = ""; + GenerateCredential(publicKey); + LOGI("[rm003] ResponseNewSendCredential, pk %{public}s", publicKey.c_str()); + authResponseContext_->publicKey = publicKey; + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_PUBLICKEY); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); +} + bool DmAuthManager::AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) { LOGI("DmAuthManager::onTransmit start."); @@ -2086,6 +2303,8 @@ bool DmAuthManager::AuthDeviceTransmit(int64_t requestId, const uint8_t *data, u void DmAuthManager::SrcAuthDeviceFinish() { + LOGI("[rm003] SrcAuthDeviceFinish"); + std::lock_guard guard(recheckPublicKeyProcessMutex_); CHECK_NULL_VOID(authRequestState_); authRequestState_->TransitionTo(std::make_shared()); if (authResponseContext_->confirmOperation != USER_OPERATION_TYPE_ALLOW_AUTH && @@ -2112,13 +2331,22 @@ void DmAuthManager::SrcAuthDeviceFinish() softbusConnector_->HandleDeviceOnline(remoteDeviceId_, authForm_); if (CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { authRequestState_->TransitionTo(std::make_shared()); + if (IsRecheckDataReceived()) { + ParseSavedFailedRecheckMsg(); + LOGI("[rm003] IsRecheckDataReceived true, call RequestReCheckMsgDone"); + RequestReCheckMsgDone(); + } } else { authRequestState_->TransitionTo(std::make_shared()); } return; } if (!authResponseContext_->isOnline && authResponseContext_->haveCredential) { - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); ConverToFinish(); return; @@ -2127,6 +2355,11 @@ void DmAuthManager::SrcAuthDeviceFinish() authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_PIN_CODE_INPUT); if (CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { authRequestState_->TransitionTo(std::make_shared()); + if (IsRecheckDataReceived()) { + ParseSavedFailedRecheckMsg(); + LOGI("[rm003] IsRecheckDataReceived true, call RequestReCheckMsgDone"); + RequestReCheckMsgDone(); + } } else { authRequestState_->TransitionTo(std::make_shared()); } @@ -2137,6 +2370,8 @@ void DmAuthManager::SrcAuthDeviceFinish() void DmAuthManager::SinkAuthDeviceFinish() { LOGI("isNeedProcCachedSrcReqMsg %{public}d.", isNeedProcCachedSrcReqMsg_); + LOGI("[rm003] SinkAuthDeviceFinish"); + std::lock_guard guard(recheckPublicKeyProcessMutex_); CHECK_NULL_VOID(authResponseState_); authResponseState_->TransitionTo(std::make_shared()); if (!authResponseContext_->haveCredential) { @@ -2147,6 +2382,17 @@ void DmAuthManager::SinkAuthDeviceFinish() SetProcessInfo(); softbusConnector_->HandleDeviceOnline(remoteDeviceId_, authForm_); } + if (RemoteSupportRecheck()) { + LOGI("[rm003] SinkAuthDeviceFinish enter AuthResponseReCheckMsg"); + authResponseState_->TransitionTo(std::make_shared()); + if (IsRecheckDataReceived()) { + ParseSavedFailedRecheckMsg(); + LOGI("[rm003] IsRecheckDataReceived true, call ResponseReCheckMsgDone"); + ResponseReCheckMsgDone(); + } + return; + } + std::string srcReqMsg = ""; bool isNeedProcCachedSrcReqMsg = false; { @@ -2167,8 +2413,6 @@ void DmAuthManager::SinkAuthDeviceFinish() } if (!CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { authResponseState_->TransitionTo(std::make_shared()); - } else { - authResponseState_->TransitionTo(std::make_shared()); } } @@ -2201,8 +2445,13 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) LOGI("AuthDeviceError start."); isAuthDevice_ = false; if (authRequestState_ == nullptr || authResponseState_ != nullptr) { - authTimes_++; LOGI("AuthDeviceError sink authTimes %{public}d.", authTimes_); + if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && !pincodeDialogEverShown_ && IsUsePinFromDp()) { + LOGI("[rm003-nopincode] AuthDeviceError, ShowAuthInfoDialog"); + ShowAuthInfoDialog(true); + return; + } + authTimes_++; if (authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->isFinish = false; authResponseContext_->reply = ERR_DM_AUTH_CODE_INCORRECT; @@ -2241,7 +2490,14 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) DmAuthManager::HandleAuthenticateTimeout(name); }); } - authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + + authRequestContext_->presetPinCode = 0; + if (!pincodeDialogEverShown_ && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { + LOGI("[rm003] auth error, ShowStartAuthDialog"); + ShowStartAuthDialog(); + } else { + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + } } } } @@ -2353,6 +2609,20 @@ void DmAuthManager::ProcRespNegotiateExt(const int32_t &sessionId) } std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_NEGOTIATE); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); + + DistributedDeviceProfile::ServiceInfoUniqueKey key; + InitServiceInfoUniqueKey(key); + std::vector profiles; + int32_t result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (result != DM_OK) { + LOGI("[rm003-nopincode] GetServiceInfoProfileListByTokenId failed"); + } else if(profiles.size() > 0) { + serviceInfoProfile_ = profiles[0]; + LOGI("[rm003] authBoxType %{public}d", serviceInfoProfile_.GetAuthBoxType()); + LOGI("[rm003] authType %{public}d", serviceInfoProfile_.GetAuthType()); + LOGI("[rm003] pinExchangeType %{public}d", serviceInfoProfile_.GetPinExchangeType()); + LOGI("[rm003] pinCode %{public}s", serviceInfoProfile_.GetPinCode().c_str()); + } } void DmAuthManager::ProcRespNegotiate(const int32_t &sessionId) @@ -2471,6 +2741,7 @@ int32_t DmAuthManager::DeleteGroup(const std::string &pkgName, int32_t userId, c void DmAuthManager::PutAccessControlList() { + LOGI("[rm003] PutAccessControlList"); char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); std::string localUdid = static_cast(localDeviceId); @@ -2787,6 +3058,32 @@ int32_t DmAuthManager::GetBinderInfo() return ret; } +int64_t DmAuthManager::GetBinderTokenId() +{ + LOGI("[rm003-nopincode] GetBinderTokenId bundleName is %{public}s", authResponseContext_->bundleName.c_str()); + CHECK_NULL_RETURN(authResponseContext_, 0); + if (authResponseContext_->bundleName.empty()) { + LOGI("[rm003-nopincode] bundleName is empty"); + return authResponseContext_->remoteTokenId; + + } + int32_t localUserId = MultipleUserConnector::GetFirstForegroundUserId(); + std::string localAccountId = MultipleUserConnector::GetOhosAccountIdByUserId(localUserId); + int32_t ret = AppManager::GetInstance(). + GetNativeTokenIdByName(authResponseContext_->bundleName, authResponseContext_->tokenId); + if (ret == DM_OK) { + LOGI("[rm003-nopincode] bundleName is sa"); + return authResponseContext_->tokenId; + } + ret = AppManager::GetInstance().GetHapTokenIdByName(localUserId, + authResponseContext_->peerBundleName, 0, authResponseContext_->tokenId); + if (ret != DM_OK) { + LOGI("[rm003-nopincode] get tokenId by bundleName failed %{public}s", GetAnonyString(authResponseContext_->bundleName).c_str()); + return authResponseContext_->remoteTokenId; + } + return authResponseContext_->tokenId; +} + void DmAuthManager::SetProcessInfo() { CHECK_NULL_VOID(authResponseContext_); @@ -2822,6 +3119,11 @@ void DmAuthManager::ConverToFinish() void DmAuthManager::RequestReCheckMsg() { LOGI("dmVersion %{public}s.", DM_VERSION_5_0_3); + if (!CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { + LOGI("[rm003] remote version < 502, skip"); + return; + } + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); uint32_t tokenId = 0; @@ -2844,15 +3146,6 @@ void DmAuthManager::ResponseReCheckMsg() { LOGI("remoteVersion %{public}s, authResponseContext_->edition %{public}s.", remoteVersion_.c_str(), authResponseContext_->edition.c_str()); - if (!IsSinkMsgValid()) { - LOGE("peer deviceId not trust."); - authResponseContext_->isFinish = false; - isFinishOfLocal_ = false; - authMessageProcessor_->SetEncryptFlag(false); - int32_t sessionId = authResponseContext_->sessionId; - authResponseState_->TransitionTo(std::make_shared()); - return; - } char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); authResponseContext_->edition = DM_VERSION_5_0_3; @@ -2868,13 +3161,20 @@ void DmAuthManager::ResponseReCheckMsg() authMessageProcessor_->SetEncryptFlag(true); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_RECHECK_MSG); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); - PutAccessControlList(); } void DmAuthManager::RequestReCheckMsgDone() { LOGI("remoteVersion %{public}s, authResponseContext_->edition %{public}s.", remoteVersion_.c_str(), authResponseContext_->edition.c_str()); + LOGI("[rm003] RequestReCheckMsgDone"); + CHECK_NULL_VOID(authResponseContext_); + CHECK_NULL_VOID(authMessageProcessor_); + CHECK_NULL_VOID(authRequestState_); + if (IsRecheckDataVerified()) { + LOGI("[rm003] recheckData already verified, return"); + return; + } if (!IsSourceMsgValid()) { LOGE("peer deviceId not trust."); authResponseContext_->isFinish = false; @@ -2883,17 +3183,45 @@ void DmAuthManager::RequestReCheckMsgDone() authRequestState_->TransitionTo(std::make_shared()); return; } + RequestCredential(); + PutAccessControlList(); +} + +void DmAuthManager::ResponseReCheckMsgDone() +{ + LOGI("[rm003] ResponseReCheckMsgDone"); + CHECK_NULL_VOID(authResponseContext_); + CHECK_NULL_VOID(authMessageProcessor_); + if (IsRecheckDataVerified()) { + LOGI("[rm003] recheckData already verified, return"); + return; + } + if (!IsSinkMsgValid()) { + LOGE("peer deviceId not trust."); + authResponseContext_->isFinish = false; + isFinishOfLocal_ = false; + authMessageProcessor_->SetEncryptFlag(false); + int32_t sessionId = authResponseContext_->sessionId; + authResponseState_->TransitionTo(std::make_shared()); + CHECK_NULL_VOID(softbusConnector_->GetSoftbusSession()); + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; + } + ResponseNewSendCredential(); PutAccessControlList(); - authRequestState_->TransitionTo(std::make_shared()); } bool DmAuthManager::IsSinkMsgValid() { - if (authResponseContext_->edition != remoteVersion_ || - authResponseContext_->localDeviceId != remoteDeviceId_ || - authResponseContext_->localUserId != authResponseContext_->remoteUserId || - authResponseContext_->bundleName != authResponseContext_->hostPkgName || - authResponseContext_->localBindLevel != authResponseContext_->bindLevel) { + LOGI("[rm003] IsSinkMsgValid"); + std::lock_guard guard(authResponseContext_->recheckData.recheckDataMutex); + CHECK_NULL_RETURN(authResponseContext_, false); + authResponseContext_->recheckData.isDataVerified = true; + if (authResponseContext_->recheckData.edition != remoteVersion_ || + authResponseContext_->recheckData.localDeviceId != remoteDeviceId_ || + authResponseContext_->recheckData.localUserId != authResponseContext_->remoteUserId || + authResponseContext_->recheckData.bundleName != authResponseContext_->hostPkgName || + authResponseContext_->recheckData.localBindLevel != authResponseContext_->bindLevel) { return false; } return true; @@ -2901,11 +3229,16 @@ bool DmAuthManager::IsSinkMsgValid() bool DmAuthManager::IsSourceMsgValid() { - if (authResponseContext_->edition != remoteVersion_ || - authResponseContext_->localDeviceId != remoteDeviceId_ || - authResponseContext_->localUserId != authRequestContext_->remoteUserId || - authResponseContext_->bundleName != authResponseContext_->peerBundleName || - authResponseContext_->localBindLevel != authResponseContext_->bindLevel) { + LOGI("[rm003] IsSourceMsgValid"); + std::lock_guard guard(authResponseContext_->recheckData.recheckDataMutex); + CHECK_NULL_RETURN(authResponseContext_, false); + CHECK_NULL_RETURN(authRequestContext_, false); + authResponseContext_->recheckData.isDataVerified = true; + if (authResponseContext_->recheckData.edition != remoteVersion_ || + authResponseContext_->recheckData.localDeviceId != remoteDeviceId_ || + authResponseContext_->recheckData.localUserId != authRequestContext_->remoteUserId || + authResponseContext_->recheckData.bundleName != authResponseContext_->peerBundleName || + authResponseContext_->recheckData.localBindLevel != authResponseContext_->bindLevel) { return false; } authResponseContext_->localAccountId = authRequestContext_->localAccountId; @@ -2924,16 +3257,69 @@ int32_t DmAuthManager::RegisterAuthenticationType(int32_t authenticationType) } void DmAuthManager::ProcessReqPublicKey() -{ - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_AUTH_FINISH || - authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_RECHECK_MSG) { - authResponseState_->TransitionTo(std::make_shared()); - return; +{ + std::lock_guard guard(recheckPublicKeyProcessMutex_); + if (RemoteSupportRecheck()) { + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_RECHECK_MSG) { + ResponseNewProcessCredential(); + } else { + LOGE("[rm003] not in state, skip MSG_TYPE_REQ_PUBLICKEY"); + } + } else { + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_AUTH_FINISH || + authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_RECHECK_MSG) { + authResponseState_->TransitionTo(std::make_shared()); + } else { + LOGE("[rm003] not in state, skip MSG_TYPE_REQ_PUBLICKEY"); + } } if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_SHOW) { std::lock_guard lock(srcReqMsgLock_); isNeedProcCachedSrcReqMsg_ = true; } } + +bool DmAuthManager::RemoteSupportRecheck() +{ + if (CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { + return true; + } + return false; +} + +bool DmAuthManager::IsRecheckDataReceived() +{ + LOGI("[rm003] IsRecheckDataReceived"); + CHECK_NULL_RETURN(authResponseContext_, false); + return authResponseContext_->recheckData.isDataReceived; +} + +bool DmAuthManager::IsRecheckDataParsed() +{ + LOGI("[rm003] IsRecheckDataParsed"); + CHECK_NULL_RETURN(authResponseContext_, false); + return authResponseContext_->recheckData.isDataParsed; +} + +bool DmAuthManager::IsRecheckDataVerified() +{ + LOGI("[rm003] IsRecheckDataVerified"); + CHECK_NULL_RETURN(authResponseContext_, false); + return authResponseContext_->recheckData.isDataVerified; +} + +void DmAuthManager::ParseSavedFailedRecheckMsg() +{ + std::lock_guard guard(savedParseFailedRecheckMsgMutex_); + if (savedParseFailedRecheckMsg_.empty()) { + return; + } + LOGI("[rm003] process savedParseFailedRecheckMsg_"); + CHECK_NULL_VOID(authResponseContext_); + CHECK_NULL_VOID(authMessageProcessor_); + authMessageProcessor_->SetResponseContext(authResponseContext_); + authMessageProcessor_->ParseMessage(savedParseFailedRecheckMsg_); + savedParseFailedRecheckMsg_.clear(); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index a723bb322d5398de698a6362a1b376ba4bdd2cbd..b0c3894abc7b119948ee5e02feae883427b43e8b 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -145,7 +145,11 @@ void HiChainAuthConnector::onError(int64_t requestId, int operationCode, int err LOGE("HiChainAuthConnector::onError dmDeviceAuthCallback_ is nullptr."); return; } - dmDeviceAuthCallback_->AuthDeviceError(requestId, ERR_DM_FAILED); + if (errorCode == PROOF_MISMATCH) { + dmDeviceAuthCallback_->AuthDeviceError(requestId, ERR_DM_HICHAIN_PROOFMISMATCH); + } else { + dmDeviceAuthCallback_->AuthDeviceError(requestId, ERR_DM_FAILED); + } } void HiChainAuthConnector::onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index cf4eeb5d3ff0fe4dc802afef8bb3f3e02baeb52c..a65aab3bc4daf39a4de430bc75a5334926a9eebd 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -96,6 +96,18 @@ void SoftbusConnector::JoinLnn(const std::string &deviceId) return; } +void SoftbusConnector::JoinLnnByHml(const int32_t sessionId) +{ + LOGI("start, JoinLnnByHml sessionId: %{public}d.", sessionId); + ConnectionAddr addrInfo; + addrInfo.type = CONNECTION_ADDR_SESSION; + addrInfo.info.session.sessionId = sessionId; + int32_t ret = ::JoinLNN(DM_PKG_NAME, &addrInfo, OnSoftbusJoinLNNResult); + if (ret != DM_OK) { + LOGE("[SOFTBUS]JoinLNN failed, ret: %{public}d.", ret); + } +} + int32_t SoftbusConnector::GetUdidByNetworkId(const char *networkId, std::string &udid) { LOGI("start, networkId: %{public}s.", GetAnonyString(std::string(networkId)).c_str()); diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 9e579ba80efa912f3935780bd5b7d936c79afa07..fcb81abf1d614505b4638ad2a14147376faf6640 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -22,6 +22,10 @@ #include "dm_log.h" #include "nlohmann/json.hpp" #include "softbus_connector.h" +#include "softbus_error_code.h" +#ifdef SOFTBUS_ENHANCE_FLAG +#include "trans_auth_lane_pending_ctl.h" +#endif namespace OHOS { namespace DistributedHardware { @@ -110,6 +114,28 @@ int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId) return sessionId; } +int32_t SoftbusSession::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool enable160m) +{ +#ifndef SOFTBUS_ENHANCE_FLAG + LOGE("[SOFTBUS] OpenAuthSessionWithPara no implement"); + return SOFTBUS_NOT_IMPLEMENT; +#else + DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION)); + LinkPara para; + para.type = PARA_ACTION; + para.action = actionId; + para.enable160M = enable160m; + int32_t sessionId = OpenAuthSessionWithPara(DM_SESSION_NAME, ¶); + if (sessionId < 0) { + LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId); + return sessionId; + } + DmTraceEnd(); + LOGI("OpenAuthSessionWithPara success. sessionId: %{public}d.", sessionId); + return sessionId; +#endif +} + int32_t SoftbusSession::CloseAuthSession(int32_t sessionId) { LOGI("CloseAuthSession."); diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 18e144a9ee797bc32e13829fd79b3d554546fa70..c46b449f11b1dd09ab564c1057b33ac92a7f72a4 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -144,6 +144,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency/include", "${devicemanager_path}/radar/include", "${innerkits_path}/native_cpp/include", + "${servicesimpl_path}/include/dependency/deviceprofile", "${softbuscache_parh}/include", "${utils_path}/include/appInfo/standard", "${utils_path}/include/crypto", diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 7a91f0a7e829f2400f3c79c7d1a6c12fb3028540..045bc66c1a7254b6cb85757c30db17490a545b9e 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -44,6 +44,7 @@ #include "dm_publish_common_event.h" #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI #endif +#include "service_info_profile.h" namespace OHOS { namespace DistributedHardware { @@ -184,6 +185,19 @@ public: int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &unbindParam); + bool IsServiceInfoValid(const DMServiceInfo &serviceInfo); + int64_t GenerateSerivceId(); + void InitServiceInfoProfile(const DMServiceInfo &serviceInfo, DistributedDeviceProfile::ServiceInfoProfile &profile); + void InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, DMServiceInfo& serviceInfo); + void InitServiceInfos(const std::vector &profiles, + std::vector &serviceInfos); + bool InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); + int32_t RegisterServiceInfo(DMServiceInfo &serviceInfo); + int32_t UnRegisterServiceInfo(DMServiceInfo &serviceInfo); + int32_t UpdateServiceInfo(DMServiceInfo &serviceInfo); + int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); + int32_t GetCallerServiceInfos(std::vector &serviceInfos); + int32_t DpAclAdd(const std::string &udid); int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index f86e86851fd69836f4103c7d2c6985af486d3f95..ce5507f8c5d6b44b7f784b4dd07c814b204482e8 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -20,11 +20,13 @@ #include "app_manager.h" #include "nlohmann/json.hpp" +#include "deviceprofile_connector.h" #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_crypto.h" #include "dm_hidumper.h" #include "dm_log.h" +#include "dm_random.h" #include "dm_softbus_cache.h" #include "dm_radar_helper.h" #include "parameter.h" @@ -34,10 +36,12 @@ #include "datetime_ex.h" #include "dm_comm_tool.h" #include "dm_transport_msg.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" #include "kv_adapter_manager.h" #include "multiple_user_connector.h" #include "relationship_sync_mgr.h" +#include "openssl/sha.h" #if defined(SUPPORT_POWER_MANAGER) #include "power_mgr_client.h" #endif // SUPPORT_POWER_MANAGER @@ -1442,6 +1446,202 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParam); } +bool DeviceManagerService::IsServiceInfoValid(const DMServiceInfo &serviceInfo) +{ + //todo + return true; +} + +int64_t DeviceManagerService::GenerateSerivceId() +{ + DmDeviceInfo devInfo; + int32_t ret = GetLocalDeviceInfo(devInfo); + if (ret != DM_OK) { + LOGE("[rm003] GetLocalDeviceInfo failed"); + return 0; + } + std::string udid = devInfo.deviceId; + if (udid.empty()) { + LOGE("[rm003] udid empty"); + return 0; + } + int64_t tokenId = IPCSkeleton::GetCallingTokenID(); + const uint32_t dataLength = sizeof(int64_t) + sizeof(int64_t) + udid.length(); + std::unique_ptr data = std::make_unique(dataLength); + int64_t randomSeed = GenRandLongLong(0, std::numeric_limits::max()); + unsigned char* dataPtr = data.get(); + *((int64_t*)dataPtr) = randomSeed; + dataPtr += sizeof(int64_t); + *((int64_t*)dataPtr) = tokenId; + dataPtr += sizeof(int64_t); + if (memcpy_s(dataPtr, udid.length(), udid.c_str(), udid.length()) != EOK) { + LOGE("[rm003] memcpy udid failed"); + return 0; + } + + unsigned char out[SHA256_DIGEST_LENGTH] = {0}; + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, data.get(), dataLength); + SHA256_Final(out, &ctx); + + int64_t serviceId = 0; + if (memcpy_s(&serviceId, sizeof(int32_t), out, sizeof(int32_t)) != EOK) { + serviceId = 0; + LOGE("[rm003] memcpy serviceId failed"); + } + serviceId = std::abs((int32_t)serviceId); + LOGI("[rm003] GenerateSeivceId %{public}" PRId64, serviceId); + return serviceId; +} + +void DeviceManagerService::InitServiceInfoProfile(const DMServiceInfo &serviceInfo, + DistributedDeviceProfile::ServiceInfoProfile &profile) +{ + DmDeviceInfo devInfo; + int32_t ret = GetLocalDeviceInfo(devInfo); + if (ret != DM_OK) { + LOGE("[rm003] GetLocalDeviceInfo failed"); + } + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + Crypto::GetUdidHash(localDeviceId, reinterpret_cast(udidHash)); + profile.SetDeviceId(std::string(udidHash)); + profile.SetNetworkId(devInfo.networkId); + profile.SetUserId(MultipleUserConnector::GetFirstForegroundUserId()); + profile.SetTokenId(std::to_string(IPCSkeleton::GetCallingTokenID())); + profile.SetServiceId(serviceInfo.serviceId); + profile.SetServiceType(serviceInfo.serviceType); + profile.SetServiceName(serviceInfo.serviceName); + profile.SetServiceDisplayName(serviceInfo.serviceDisplayName); + profile.SetCustomData(serviceInfo.customData); + profile.SetCustomDataLen(serviceInfo.customData.size()); + profile.SetBundleName(serviceInfo.bundleName); + profile.SetModuleName(serviceInfo.moduleName); + profile.SetAbilityName(serviceInfo.abilityName); + profile.SetAuthBoxType(serviceInfo.authBoxType); + profile.SetAuthType(serviceInfo.authType); + profile.SetPinExchangeType(serviceInfo.pinExchangeType); + profile.SetPinCode(serviceInfo.pinCode); + profile.SetDescription(serviceInfo.description); + profile.SetServiceDicoveryScope(serviceInfo.serviceDiscoveryScope); +} + +void DeviceManagerService::InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, + DMServiceInfo &serviceInfo) +{ + serviceInfo.serviceId = profile.GetServiceId(); + serviceInfo.serviceType = profile.GetServiceType(); + serviceInfo.serviceName = profile.GetServiceName(); + serviceInfo.serviceDisplayName = profile.GetServiceDisplayName(); + serviceInfo.customData = profile.GetCustomData(); + serviceInfo.bundleName = profile.GetBundleName(); + serviceInfo.moduleName = profile.GetModuleName(); + serviceInfo.abilityName = profile.GetAbilityName(); + serviceInfo.authBoxType = profile.GetAuthBoxType(); + serviceInfo.authType = profile.GetAuthType(); + serviceInfo.pinExchangeType = profile.GetPinExchangeType(); + serviceInfo.pinCode = profile.GetPinCode(); + serviceInfo.description = profile.GetDescription(); + serviceInfo.serviceDiscoveryScope = profile.GetServiceDicoveryScope(); +} + +void DeviceManagerService::InitServiceInfos(const std::vector &profiles, + std::vector &serviceInfos) +{ + for (const auto &profileItem : profiles) { + DMServiceInfo infoItem; + InitServiceInfo(profileItem, infoItem); + serviceInfos.emplace_back(infoItem); + } +} + +bool DeviceManagerService::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + Crypto::GetUdidHash(localDeviceId, reinterpret_cast(udidHash)); + key.SetDeviceId(std::string(udidHash)); + key.SetUserId(MultipleUserConnector::GetFirstForegroundUserId()); + key.SetTokenId(std::to_string(IPCSkeleton::GetCallingTokenID())); + return true; +} + +int32_t DeviceManagerService::RegisterServiceInfo(DMServiceInfo &serviceInfo) +{ + //todo permission verifier + if (!IsServiceInfoValid(serviceInfo)) { + LOGE("[rm003] IsServiceInfoValid failed"); + return ERR_DM_SERVICEINFO_NOTVALID; + } + LOGI("[rm003] DeviceManagerService::RegisterServiceInfo"); + DistributedDeviceProfile::ServiceInfoProfile profile; + InitServiceInfoProfile(serviceInfo, profile); + int32_t result = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(profile); + return result; +} + +int32_t DeviceManagerService::UnRegisterServiceInfo(DMServiceInfo &serviceInfo) +{ + LOGI("[rm003] DeviceManagerService::UnRegisterServiceInfo"); + DistributedDeviceProfile::ServiceInfoProfile profile; + InitServiceInfoProfile(serviceInfo, profile); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + key.SetServiceId(serviceInfo.serviceId); + return DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(key); +} + +int32_t DeviceManagerService::UpdateServiceInfo(DMServiceInfo &serviceInfo) +{ + LOGI("[rm003] DeviceManagerService::UpdateServiceInfo"); + DistributedDeviceProfile::ServiceInfoProfile profile; + InitServiceInfoProfile(serviceInfo, profile); + int32_t result = DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(profile); + return result; +} + +int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo) +{ + LOGI("[rm003] DeviceManagerService::GetServiceInfoById, %{public}" PRId64, serviceId); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + key.SetServiceId(serviceId); + DistributedDeviceProfile::ServiceInfoProfile profile; + result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(key, profile); + if (result == DM_OK) { + InitServiceInfo(profile, serviceInfo); + } + return result; +} + +int32_t DeviceManagerService::GetCallerServiceInfos(std::vector &serviceInfos) +{ + LOGI("[rm003] DeviceManagerService::GetCallerServiceInfos"); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + std::vector profiles; + result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (result == DM_OK) { + InitServiceInfos(profiles, serviceInfos); + } + return result; +} + int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName) { if (!PermissionManager::GetInstance().CheckPermission()) { diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index df9ff60cd52efba984d9bf82d641477b0d39130e..7753a3ced2a38221339f253c2a6119e0402b8afb 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -126,6 +126,63 @@ void DecodeDmAccessCallee(MessageParcel &parcel, DmAccessCallee &callee) callee.extra = parcel.ReadString(); } +bool EncodeServiceInfo(const DMServiceInfo& serviceInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDisplayName)); + bRet = (bRet && parcel.WriteString(serviceInfo.customData)); + bRet = (bRet && parcel.WriteString(serviceInfo.bundleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.moduleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.abilityName)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authBoxType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.pinExchangeType)); + bRet = (bRet && parcel.WriteString(serviceInfo.pinCode)); + bRet = (bRet && parcel.WriteString(serviceInfo.description)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDiscoveryScope)); + return bRet; +} + +bool EncodeServiceInfos(const std::vector& serviceInfos, MessageParcel &parcel) +{ + uint32_t num = serviceInfos.size(); + if (!parcel.WriteUint32(num)){ + LOGE("[rm003] WriteInt32 num failed"); + return false; + } + bool bRet = true; + for (uint32_t k = 0; k < num; k++) { + const DMServiceInfo& serviceInfo = serviceInfos[k]; + bRet = EncodeServiceInfo(serviceInfo, parcel); + if (!bRet) { + LOGE("[rm003] encode serviceinfos failed"); + break; + } + } + return bRet; +} + +void DecodeServiceInfo(MessageParcel &parcel, DMServiceInfo &serviceInfo) +{ + serviceInfo.serviceId = parcel.ReadInt64(); + serviceInfo.serviceType = parcel.ReadString(); + serviceInfo.serviceName = parcel.ReadString(); + serviceInfo.serviceDisplayName = parcel.ReadString(); + serviceInfo.customData = parcel.ReadString(); + serviceInfo.bundleName = parcel.ReadString(); + serviceInfo.moduleName = parcel.ReadString(); + serviceInfo.abilityName = parcel.ReadString(); + serviceInfo.authBoxType = parcel.ReadInt32(); + serviceInfo.authType = parcel.ReadInt32(); + serviceInfo.pinExchangeType = parcel.ReadInt32(); + serviceInfo.pinCode = parcel.ReadString(); + serviceInfo.description = parcel.ReadString(); + serviceInfo.serviceDiscoveryScope = parcel.ReadString(); +} + ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr pBaseReq, MessageParcel &data) { if (pBaseReq == nullptr) { @@ -820,6 +877,7 @@ ON_IPC_CMD(BIND_DEVICE, MessageParcel &data, MessageParcel &reply) std::string bindParam = data.ReadString(); std::string deviceId = data.ReadString(); int32_t bindType = data.ReadInt32(); + LOGE("[rm003] bindParam %{public}s", bindParam.c_str()); int32_t result = DM_OK; result = DeviceManagerService::GetInstance().BindDevice(pkgName, bindType, deviceId, bindParam); if (!reply.WriteInt32(result)) { @@ -1794,5 +1852,97 @@ ON_IPC_READ_RESPONSE(GET_DEVICE_ICON_INFO_RESULT, MessageParcel &reply, std::sha pBaseRsp->SetErrCode(reply.ReadInt32()); return DM_OK; } + +ON_IPC_CMD(GEN_SERVICEID, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GEN_SERVICEID"); + int64_t serviceId = DeviceManagerService::GetInstance().GenerateSerivceId(); + int32_t result = DM_OK; + if (serviceId == 0) { + result = ERR_DM_FAILED; + } + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!reply.WriteInt64(serviceId)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(REG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(REG_SERVICE_INFO"); + DMServiceInfo serviceInfo; + DecodeServiceInfo(data, serviceInfo); + int32_t result = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(UNREG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(UNREG_SERVICE_INFO"); + DMServiceInfo serviceInfo; + DecodeServiceInfo(data, serviceInfo); + int32_t result = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(UPDATE_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(UPDATE_SERVICE_INFO"); + DMServiceInfo serviceInfo; + DecodeServiceInfo(data, serviceInfo); + int32_t result = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(GET_SERVICEINFO_BYID, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFO_BYID"); + int64_t serviceId = data.ReadInt64(); + DMServiceInfo serviceInfo; + int32_t result = DeviceManagerService::GetInstance().GetServiceInfoById(serviceId, serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (result == DM_OK && !EncodeServiceInfo(serviceInfo, reply)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(GET_SERVICEINFO_CALLER, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFO_CALLER"); + std::vector serviceInfos; + int32_t result = DeviceManagerService::GetInstance().GetCallerServiceInfos(serviceInfos); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (result == DM_OK && !EncodeServiceInfos(serviceInfos, reply)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_auth_message_processor.cpp b/test/unittest/UTTest_auth_message_processor.cpp index ecb4e0a70dabc19d45f1e3b1ba8b984c16f4edb4..74edd0999313a4647cac8e593ceb1ffd0290d7cd 100644 --- a/test/unittest/UTTest_auth_message_processor.cpp +++ b/test/unittest/UTTest_auth_message_processor.cpp @@ -1501,7 +1501,7 @@ HWTEST_F(AuthMessageProcessorTest, ParsePublicKeyMessageExt_001, testing::ext::T nlohmann::json jsonObj; jsonObj[TAG_PUBLICKEY] = 2121; authMessageProcessor->ParsePublicKeyMessageExt(jsonObj); - ASSERT_EQ(authMessageProcessor->authResponseContext_->publicKey.empty(), true); + ASSERT_EQ(authMessageProcessor->authResponseContext_->remotePublicKey.empty(), true); } HWTEST_F(AuthMessageProcessorTest, ParsePublicKeyMessageExt_002, testing::ext::TestSize.Level0) @@ -1515,7 +1515,7 @@ HWTEST_F(AuthMessageProcessorTest, ParsePublicKeyMessageExt_002, testing::ext::T nlohmann::json jsonObj; jsonObj[TAG_PUBLICKEY] = "2121"; authMessageProcessor->ParsePublicKeyMessageExt(jsonObj); - ASSERT_EQ(authMessageProcessor->authResponseContext_->publicKey.empty(), false); + ASSERT_EQ(authMessageProcessor->authResponseContext_->remotePublicKey.empty(), false); } HWTEST_F(AuthMessageProcessorTest, ParseAuthResponseMessageExt_001, testing::ext::TestSize.Level0) diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index c846464ec9859163997373d02fb3af6abae90ca6..5d84fe62461144ded9e8d24fa031fc459d907d16 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -77,6 +77,23 @@ void DeviceManagerImplTest::TearDownTestCase() { } +void DeviceManagerImplTest::InitServiceInfo(DMServiceInfo &serviceInfo) +{ + serviceInfo.serviceType = "profile.serviceType"; + serviceInfo.serviceName = "profile.serviceName"; + serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; + serviceInfo.customData = "profile.customData"; + serviceInfo.bundleName = "profile.bundleName"; + serviceInfo.moduleName = "profile.moduleName"; + serviceInfo.abilityName = "profile.abilityName"; + serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; + serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; + serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; + serviceInfo.pinCode = "123456"; + serviceInfo.description = "profile.description"; + serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; +} + namespace { /** * @tc.name: InitDeviceManager_001 @@ -2551,6 +2568,94 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceSecurityLevel_001, testing::ext::TestSi ret = DeviceManager::GetInstance().GetDeviceSecurityLevel(pkgName, netWorkId, securityLevel); ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } + +HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_101, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.test"; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); + ASSERT_EQ(ret, DM_OK); + + DMServiceInfo serviceInfoOld; + InitServiceInfo(serviceInfoOld); + + int64_t serviceId = 0; + ret = DeviceManager::GetInstance().GenerateServiceId(serviceId); + ASSERT_EQ(ret, DM_OK); + DMServiceInfo serviceInfo; + InitServiceInfo(serviceInfo); + serviceInfo.serviceId = serviceId; + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo); + ASSERT_EQ(ret, DM_OK); + DMServiceInfo retInfo; + LOGI("[rm003] serviceId %{public}" PRId64, serviceId); + ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(serviceId == retInfo.serviceId); + ASSERT_TRUE(serviceInfoOld.serviceType == retInfo.serviceType); + ASSERT_TRUE(serviceInfoOld.abilityName == retInfo.abilityName); + ASSERT_TRUE(serviceInfoOld.serviceDiscoveryScope == retInfo.serviceDiscoveryScope); + + retInfo.abilityName = "kkk"; + ret = DeviceManager::GetInstance().UpdateServiceInfo(retInfo); + ASSERT_EQ(ret, DM_OK); + + ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(retInfo.abilityName == "kkk"); + + std::vector infos; + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_EQ(ret, DM_OK); + for (const auto &item : infos) { + DeviceManager::GetInstance().UnRegisterServiceInfo(item); + } + infos.clear(); + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_NE(ret, DM_OK); + ASSERT_EQ(infos.size(), 0); + DeviceManager::GetInstance().UnInitDeviceManager(packName); +} + +HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_102, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.test"; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); + ASSERT_EQ(ret, DM_OK); + + std::vector infos; + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + for (const auto &item : infos) { + DeviceManager::GetInstance().UnRegisterServiceInfo(item); + } + infos.clear(); + + DMServiceInfo serviceInfo1; + InitServiceInfo(serviceInfo1); + ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo1.serviceId); + ASSERT_EQ(ret, DM_OK); + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo1); + ASSERT_EQ(ret, DM_OK); + + DMServiceInfo serviceInfo2; + InitServiceInfo(serviceInfo2); + ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo2.serviceId); + ASSERT_EQ(ret, DM_OK); + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo2); + ASSERT_EQ(ret, DM_OK); + + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(infos.size() >= 2); + for (const auto &item : infos) { + LOGI("[rm003] UnRegisterServiceInfo, serviceId %{public}" PRId64, item.serviceId); + DeviceManager::GetInstance().UnRegisterServiceInfo(item); + } + infos.clear(); + DeviceManager::GetInstance().UnInitDeviceManager(packName); +} + } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_impl.h b/test/unittest/UTTest_device_manager_impl.h index bb1de8da259588fa180ca6a9e60ca465d189194a..2b1647745d54f1e38d3e75f1cf2c219db4c15970 100644 --- a/test/unittest/UTTest_device_manager_impl.h +++ b/test/unittest/UTTest_device_manager_impl.h @@ -35,6 +35,7 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + void InitServiceInfo(DMServiceInfo &serviceInfo); }; class DeviceDiscoveryCallbackTest : public DiscoveryCallback {