From 7de266e681eaa37cda7a6392deb842513e511a04 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Tue, 11 Feb 2025 11:07:32 +0800 Subject: [PATCH] =?UTF-8?q?sink=E7=AB=AF=E6=B7=BB=E5=8A=A0callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../device_manager_ipc_interface_code.h | 1 + .../native_cpp/include/device_manager.h | 3 ++ .../native_cpp/include/device_manager_impl.h | 3 ++ .../include/notify/device_manager_notify.h | 5 +++ .../native_cpp/src/device_manager_impl.cpp | 23 ++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 14 ++++++ .../src/notify/device_manager_notify.cpp | 45 +++++++++++++++++++ .../src/authentication/dm_auth_manager.cpp | 16 +++++++ .../include/device_manager_service_listener.h | 2 + .../idevice_manager_service_listener.h | 2 + .../src/device_manager_service_listener.cpp | 25 +++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 45 +++++++++++++++++++ test/unittest/UTTest_dm_pin_holder.h | 10 +++++ 13 files changed, 194 insertions(+) diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index ad592b650..4d834a4ca 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -97,6 +97,7 @@ enum DMIpcCmdInterfaceCode { GET_DEVICE_SCREEN_STATUS, SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, SYNC_CALLBACK, + SINK_BIND_TARGET_RESULT, IPC_MSG_BUTT }; } // namespace DistributedHardware diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index dc7f7b360..bb97916c1 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -625,6 +625,9 @@ public: virtual int32_t RegisterCredentialAuthStatusCallback(const std::string &pkgName, std::shared_ptr callback) = 0; virtual int32_t UnRegisterCredentialAuthStatusCallback(const std::string &pkgName) = 0; + virtual int32_t RegisterSinkBindCallback(const std::string &pkgName, + std::shared_ptr callback) = 0; + virtual int32_t UnRegisterSinkBindCallback(const std::string &pkgName) = 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 d7d373df1..23fe6ff26 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -400,6 +400,9 @@ public: std::shared_ptr callback) override; virtual int32_t UnRegisterCredentialAuthStatusCallback(const std::string &pkgName) override; void SyncCallbacksToService(std::map> &callbackMap); + virtual int32_t RegisterSinkBindCallback(const std::string &pkgName, + std::shared_ptr callback) override; + virtual int32_t UnRegisterSinkBindCallback(const std::string &pkgName) override; private: DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 0f183ecc3..971ea9688 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -73,6 +73,8 @@ public: void RegisterCredentialAuthStatusCallback(const std::string &pkgName, std::shared_ptr callback); void UnRegisterCredentialAuthStatusCallback(const std::string &pkgName); + void RegisterSinkBindCallback(const std::string &pkgName, std::shared_ptr callback); + void UnRegisterSinkBindCallback(const std::string &pkgName); public: static void DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); @@ -118,6 +120,8 @@ public: void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode); void GetCallBack(std::map> &callbackMap); + void OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, int32_t status, + std::string content); private: #if !defined(__LITEOS_M__) @@ -136,6 +140,7 @@ private: std::map> pinHolderCallback_; std::map> deviceScreenStatusCallback_; std::map> credentialAuthStatusCallback_; + std::map> sinkBindTargetCallback_; std::mutex bindLock_; }; } // namespace DistributedHardware 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 0b701dbd7..3b792c586 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -2358,5 +2358,28 @@ void DeviceManagerImpl::SyncCallbacksToService(std::map callback) +{ + if (pkgName.empty()) { + LOGE("Error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + DeviceManagerNotify::GetInstance().RegisterSinkBindCallback(pkgName, callback); + LOGI("Completed, pkgName: %{public}s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterSinkBindCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("Error: Invalid para"); + return ERR_DM_INPUT_PARA_INVALID; + } + DeviceManagerNotify::GetInstance().UnRegisterSinkBindCallback(pkgName); + LOGI("Completed, pkgName: %{public}s", pkgName.c_str()); + 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 c1b939b36..8b64b7024 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 @@ -1751,5 +1751,19 @@ ON_IPC_READ_RESPONSE(SYNC_CALLBACK, MessageParcel &reply, std::shared_ptrSetErrCode(reply.ReadInt32()); return DM_OK; } + +ON_IPC_CMD(SINK_BIND_TARGET_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + PeerTargetId targetId; + DecodePeerTargetId(data, targetId); + int32_t result = data.ReadInt32(); + int32_t status = data.ReadInt32(); + std::string content = data.ReadString(); + + DeviceManagerNotify::GetInstance().OnSinkBindResult(pkgName, targetId, result, status, content); + reply.WriteInt32(DM_OK); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 00ab9b31d..5604636c8 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -1175,5 +1175,50 @@ void DeviceManagerNotify::GetCallBack(std::map callback) +{ + if (pkgName.empty() || callback == nullptr) { + LOGE("Invalid parameter, pkgName is empty or callback is nullptr."); + return; + } + std::lock_guard autoLock(lock_); + sinkBindTargetCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterSinkBindCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return; + } + std::lock_guard autoLock(lock_); + sinkBindTargetCallback_.erase(pkgName); +} + +void DeviceManagerNotify::OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, + int32_t result, int32_t status, std::string content) +{ + if (pkgName.empty()) { + LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str()); + return; + } + LOGI("DeviceManagerNotify::OnSinkBindResult in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result); + std::shared_ptr tempCbk; + { + std::lock_guard autoLock(lock_); + if (sinkBindTargetCallback_.find(pkgName) == sinkBindTargetCallback_.end()) { + LOGE("error, sink bind callback not register."); + return; + } + tempCbk = sinkBindTargetCallback_[pkgName]; + } + if (tempCbk == nullptr) { + LOGE("error, registered sink bind callback is nullptr."); + return; + } + tempCbk->OnBindResult(targetId, result, status, content); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 400f6194a..b547e56ca 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -301,6 +301,7 @@ int32_t DmAuthManager::StopAuthenticateDevice(const std::string &pkgName) LOGI("Stop previous AuthenticateDevice."); authRequestContext_->reason = STOP_BIND; authResponseContext_->state = authRequestState_->GetStateType(); + authResponseContext_->reply = STOP_BIND; authRequestState_->TransitionTo(std::make_shared()); } return DM_OK; @@ -612,6 +613,7 @@ void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) if (status != DM_OK || authResponseContext_->requestId != requestId) { if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; + authResponseContext_->reply = ERR_DM_BIND_PIN_CODE_ERROR; authRequestContext_->reason = ERR_DM_BIND_PIN_CODE_ERROR; authRequestState_->TransitionTo(std::make_shared()); } else { @@ -643,6 +645,7 @@ void DmAuthManager::HandleMemberJoinImportAuthCode(const int64_t requestId, cons { if (status != DM_OK || authResponseContext_->requestId != requestId) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; + authResponseContext_->reply = ERR_DM_AUTH_CODE_INCORRECT; authRequestContext_->reason = ERR_DM_AUTH_CODE_INCORRECT; authRequestState_->TransitionTo(std::make_shared()); } else { @@ -658,6 +661,7 @@ void DmAuthManager::HandleAuthenticateTimeout(std::string name) authResponseContext_ = std::make_shared(); } authResponseContext_->state = authRequestState_->GetStateType(); + authResponseContext_->reply = ERR_DM_TIME_OUT; authRequestContext_->reason = ERR_DM_TIME_OUT; authRequestState_->TransitionTo(std::make_shared()); } @@ -697,6 +701,7 @@ int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) } authResponseContext_->state = AuthState::AUTH_REQUEST_NEGOTIATE; authRequestContext_->reason = sessionId; + authResponseContext_->reply = sessionId; if (authRequestState_ != nullptr) { authRequestState_->TransitionTo(std::make_shared()); } @@ -1104,6 +1109,7 @@ void DmAuthManager::StartRespAuthProcess() LOGE("do not accept"); authResponseContext_->state = AuthState::AUTH_REQUEST_REPLY; authRequestContext_->reason = ERR_DM_AUTH_PEER_REJECT; + authResponseContext_->reply = ERR_DM_AUTH_PEER_REJECT; authRequestState_->TransitionTo(std::make_shared()); } } @@ -1195,6 +1201,7 @@ int32_t DmAuthManager::JoinNetwork() authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authResponseContext_->isFinish = true; authRequestContext_->reason = DM_OK; + authResponseContext_->reply = DM_OK; authRequestState_->TransitionTo(std::make_shared()); return DM_OK; } @@ -1202,6 +1209,8 @@ int32_t DmAuthManager::JoinNetwork() void DmAuthManager::SinkAuthenticateFinish() { LOGI("DmAuthManager::SinkAuthenticateFinish, isFinishOfLocal: %{public}d", isFinishOfLocal_); + listener_->OnSinkBindResult(authResponseContext_->hostPkgName, peerTargetId_, authResponseContext_->reply, + authResponseContext_->state, GenerateBindResultContent()); if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_FINISH && authPtr_ != nullptr) { authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_PIN_CODE_SHOW); authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_CONFIRM_SHOW); @@ -1906,6 +1915,7 @@ void DmAuthManager::RequestCredentialDone() softbusConnector_->JoinLnn(authRequestContext_->ip); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; + authResponseContext_->reply = DM_OK; authRequestState_->TransitionTo(std::make_shared()); } @@ -2074,6 +2084,7 @@ void DmAuthManager::SrcAuthDeviceFinish() timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); } authRequestContext_->reason = DM_OK; + authResponseContext_->reply = DM_OK; authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestState_->TransitionTo(std::make_shared()); return; @@ -2095,6 +2106,7 @@ void DmAuthManager::SrcAuthDeviceFinish() timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); } authRequestContext_->reason = DM_OK; + authResponseContext_->reply = DM_OK; authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestState_->TransitionTo(std::make_shared()); return; @@ -2152,6 +2164,7 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) if (authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; authRequestContext_->reason = ERR_DM_AUTH_CODE_INCORRECT; + authResponseContext_->reply = ERR_DM_AUTH_CODE_INCORRECT; authRequestState_->TransitionTo(std::make_shared()); return; } @@ -2164,6 +2177,7 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; authRequestContext_->reason = ERR_DM_INPUT_PARA_INVALID; + authResponseContext_->reply = ERR_DM_INPUT_PARA_INVALID; authRequestState_->TransitionTo(std::make_shared()); } else { if (timer_ != nullptr) { @@ -2617,6 +2631,7 @@ int32_t DmAuthManager::CheckTrustState() softbusConnector_->JoinLnn(authResponseContext_->deviceId); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; + authResponseContext_->reply = DM_OK; authRequestState_->TransitionTo(std::make_shared()); return ALREADY_BIND; } @@ -2639,6 +2654,7 @@ int32_t DmAuthManager::CheckTrustState() authResponseContext_->isAuthCodeReady == false)) { authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = ERR_DM_BIND_PEER_UNSUPPORTED; + authResponseContext_->reply = ERR_DM_BIND_PEER_UNSUPPORTED; authRequestState_->TransitionTo(std::make_shared()); return ERR_DM_BIND_PEER_UNSUPPORTED; } diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 6bbcb8f4c..a1caf69e4 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -76,6 +76,8 @@ public: void OnDeviceScreenStateChange(const std::string &pkgName, DmDeviceInfo &devInfo) override; void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode) override; + void OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, + int32_t status, std::string content) override; private: void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo); diff --git a/services/service/include/idevice_manager_service_listener.h b/services/service/include/idevice_manager_service_listener.h index 9f1a5d032..c8977db25 100644 --- a/services/service/include/idevice_manager_service_listener.h +++ b/services/service/include/idevice_manager_service_listener.h @@ -159,6 +159,8 @@ public: */ virtual void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode) = 0; + virtual void OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, + int32_t status, std::string content) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index 3c2b68d97..4575d82ea 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -584,5 +584,30 @@ std::vector DeviceManagerServiceListener::GetNotifyPkgName( } return pkgNamesTemp; } + +void DeviceManagerServiceListener::OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, + int32_t result, int32_t status, std::string content) +{ + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) { + status = STATUS_DM_AUTH_DEFAULT; + } + PeerTargetId returnTargetId = targetId; +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::string deviceIdTemp = ""; + DmKVValue kvValue; + if (ConvertUdidHashToAnoyDeviceId(pkgName, targetId.deviceId, deviceIdTemp) == DM_OK && + KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) { + returnTargetId.deviceId = deviceIdTemp; + } +#endif + pReq->SetPkgName(pkgName); + pReq->SetPeerTargetId(returnTargetId); + pReq->SetResult(result); + pReq->SetStatus(status); + pReq->SetContent(content); + ipcServerListener_.SendRequest(SINK_BIND_TARGET_RESULT, pReq, pRsp); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index 796773020..28eea7080 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -1102,6 +1102,51 @@ ON_IPC_READ_RESPONSE(BIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + PeerTargetId targetId = pReq->GetPeerTargetId(); + int32_t result = pReq->GetResult(); + int32_t status = pReq->GetStatus(); + std::string content = pReq->GetContent(); + + if (!data.WriteString(pkgName)) { + LOGE("write bind pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!EncodePeerTargetId(targetId, data)) { + LOGE("write bind peer target id failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(result)) { + LOGE("write bind result code failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(status)) { + LOGE("write bind result status failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(content)) { + LOGE("write bind result content failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(SINK_BIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + ON_IPC_SET_REQUEST(UNBIND_TARGET_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) { if (pBaseReq == nullptr) { diff --git a/test/unittest/UTTest_dm_pin_holder.h b/test/unittest/UTTest_dm_pin_holder.h index 71eae95a2..85daf8506 100644 --- a/test/unittest/UTTest_dm_pin_holder.h +++ b/test/unittest/UTTest_dm_pin_holder.h @@ -194,6 +194,16 @@ public: (void)deviceTypeId; (void)errcode; } + + void OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, + int32_t status, std::string content) override + { + (void)pkgName; + (void)targetId; + (void)result; + (void)status; + (void)content; + } }; } // namespace DistributedHardware } // namespace OHOS -- Gitee