diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index 80a875a0ce3e758c3926dd6ac6bf83b8f6422272..d4e404d2181f341921ea445f4e5b55fead5a58f9 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 { SINK_BIND_TARGET_RESULT, SYNC_CALLBACK, GET_ANONY_LOCAL_UDID, + GET_ALL_TRUST_DEVICE_LIST, REG_AUTHENTICATION_TYPE, // Add ipc msg here IPC_MSG_BUTT diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 5dc128a3c0eadad9e93f592d607cb8994e170919..6c9935a832d39f2ec3a45cccba8072c0369fd62d 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -633,6 +633,8 @@ public: virtual int32_t RegisterSinkBindCallback(const std::string &pkgName, std::shared_ptr callback) = 0; virtual int32_t UnRegisterSinkBindCallback(const std::string &pkgName) = 0; + virtual int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + bool isRefresh, std::vector &deviceList) = 0; virtual int32_t RegisterAuthenticationType(const std::string &pkgName, const std::map &authParam) = 0; }; 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 324f721b93baddae7d768360d8135fc1889a057f..5e13f0aa347e7d06834117aa1034d449881c5979 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -406,6 +406,8 @@ public: virtual int32_t RegisterSinkBindCallback(const std::string &pkgName, std::shared_ptr callback) override; virtual int32_t UnRegisterSinkBindCallback(const std::string &pkgName) override; + virtual int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + bool isRefresh, std::vector &deviceList) override; void SyncCallbacksToService(std::map> &callbackMap); virtual int32_t RegisterAuthenticationType(const std::string &pkgName, const std::map &authParam) override; 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..62d1ec34b0103f0d0ed510f1490739ebf25b6c73 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -185,6 +185,10 @@ typedef struct DmDeviceInfo { * include json keys: "CONN_ADDR_TYPE", "BR_MAC_", "BLE_MAC", "WIFI_IP", "WIFI_PORT", "CUSTOM_DATA" */ std::string extraData; + /** + * Device authentication bindLevel. + */ + uint32_t bindLevel; } DmDeviceInfo; /** 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 3da3d1d080a49b119a0f051fc3f6d4e4d937b9d3..19bd8c1f23f7b4861a5410855d8a1612d1fec309 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -2538,6 +2538,44 @@ void DeviceManagerImpl::SyncCallbackToService(DmCommonNotifyEvent dmCommonNotify } } +int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + if (pkgName.empty()) { + DmRadarHelper::GetInstance().ReportGetTrustDeviceList( + pkgName, "GetAllTrustedDeviceList", deviceList, ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_); + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGD("Start, pkgName: %{public}s, extra: %{public}s", GetAnonyString(pkgName).c_str(), extra.c_str()); + + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetExtra(extra); + int32_t ret = ipcClientProxy_->SendRequest(GET_ALL_TRUST_DEVICE_LIST, req, rsp); + if (ret != DM_OK) { + DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList", + deviceList, ret, anonyLocalUdid_); + LOGE("DeviceManagerImpl::GetAllTrustedDeviceList error, Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList", + deviceList, ret, anonyLocalUdid_); + LOGI("GetAllTrustedDeviceList error, failed ret: %{public}d", ret); + return ret; + } + + deviceList = rsp->GetDeviceVec(); + LOGI("Completed, device size %{public}zu", deviceList.size()); + DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList", + deviceList, DM_OK, anonyLocalUdid_); + return DM_OK; +} + void DeviceManagerImpl::SyncCallbacksToService(std::map> &callbackMap) { if (callbackMap.size() == 0) { 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 e5bbebb130c8d428878be5c4c4122056ddc03226..b63beff4a5d45824ebfc13839020ece40498c3ec 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 @@ -86,6 +86,7 @@ void DecodeDmDeviceInfo(MessageParcel &parcel, DmDeviceInfo &devInfo) devInfo.networkType = parcel.ReadInt32(); devInfo.authForm = static_cast(parcel.ReadInt32()); devInfo.extraData = parcel.ReadString(); + devInfo.bindLevel = parcel.ReadUint32(); } void DecodeDmDeviceBasicInfo(MessageParcel &parcel, DmDeviceBasicInfo &devInfo) @@ -251,6 +252,47 @@ ON_IPC_READ_RESPONSE(GET_TRUST_DEVICE_LIST, MessageParcel &reply, std::shared_pt return DM_OK; } +ON_IPC_SET_REQUEST(GET_ALL_TRUST_DEVICE_LIST, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string extra = pReq->GetExtra(); + if (!data.WriteString(pkgName)) { + LOGE("write pkg failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(extra)) { + LOGE("write extra failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_ALL_TRUST_DEVICE_LIST, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + int32_t deviceNum = reply.ReadInt32(); + if (deviceNum > 0 && deviceNum <= DM_MAX_TRUST_DEVICE_NUM) { + std::vector deviceInfoVec; + for (int32_t i = 0; i < deviceNum; ++i) { + DmDeviceInfo deviceInfo; + DecodeDmDeviceInfo(reply, deviceInfo); + deviceInfoVec.emplace_back(deviceInfo); + } + pRsp->SetDeviceVec(deviceInfoVec); + } + pRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + ON_IPC_SET_REQUEST(GET_DEVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) { if (pBaseReq == nullptr) { diff --git a/services/service/include/softbus/softbus_listener.h b/services/service/include/softbus/softbus_listener.h index 7a916e4350f557eb5b7dcec8b5259505eda5cbf5..42a4afdf9555b087b7e4a56ddf819d5972a5b292 100644 --- a/services/service/include/softbus/softbus_listener.h +++ b/services/service/include/softbus/softbus_listener.h @@ -104,9 +104,12 @@ public: void SendAclChangedBroadcast(const std::string &msg); int32_t GetDeviceScreenStatus(const char *networkId, int32_t &screenStatus); static int32_t GetNetworkIdByUdid(const std::string &udid, std::string &networkId); + static int32_t GetDeviceNameByUdid(const std::string &udid, std::string &deviceName); int32_t SetLocalDeviceName(const std::string &localDeviceName, const std::string &localDisplayName); int32_t SetForegroundUserIdsToDSoftBus(const std::string &remoteUserId, const std::vector &userIds); void DeleteCacheDeviceInfo(); + int32_t GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList); private: static int32_t FillDeviceInfo(const DeviceInfo &device, DmDeviceInfo &dmDevice); static void ParseConnAddrInfo(const ConnectionAddr *addrInfo, nlohmann::json &jsonObj); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index e8fa03cef0d30d1810db89a76ef20530b999b2ff..7a82579607aae4c1838491320fc4805feefcf66a 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -321,6 +321,27 @@ int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, c return DM_OK; } +int32_t DeviceManagerService::GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + LOGI("Begin for pkgName = %{public}s.", pkgName.c_str()); + if (pkgName.empty() || extra.empty()) { + LOGE("Invalid parameter, pkgName or extra is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (!PermissionManager::GetInstance().CheckNewPermission()) { + LOGE("The caller: %{public}s does not have permission to call GetAllTrustedDeviceList.", pkgName.c_str()); + return ERR_DM_NO_PERMISSION; + } + CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL); + int32_t ret = softbusListener_->GetAllTrustedDeviceList(pkgName, extra, deviceList); + if (ret != DM_OK) { + LOGE("GetAllTrustedDeviceList failed"); + return ret; + } + return DM_OK; +} + int32_t DeviceManagerService::ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp) { diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index 3877ee4d7734c661bc566cb192f00bbb697e4733..9fc8ebca84c9ee9461edd87ceb12754f7e5bb04e 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -67,6 +67,7 @@ bool EncodeDmDeviceInfo(const DmDeviceInfo &devInfo, MessageParcel &parcel) bRet = (bRet && parcel.WriteInt32(devInfo.networkType)); bRet = (bRet && parcel.WriteInt32(devInfo.authForm)); bRet = (bRet && parcel.WriteString(devInfo.extraData)); + bRet = (bRet && WriteUint32(&reply, devInfo.bindLevel)); return bRet; } @@ -406,6 +407,29 @@ ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply) return DM_OK; } +ON_IPC_CMD(GET_ALL_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + std::string extra = data.ReadString(); + std::vector deviceList; + int32_t result = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); + if (!reply.WriteInt32((int32_t)deviceList.size())) { + LOGE("write device list size failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + for (const auto &devInfo : deviceList) { + if (!EncodeDmDeviceInfo(devInfo, reply)) { + LOGE("write dm device info failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + } + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply) { int32_t id = OHOS::HiviewDFX::XCollie::GetInstance().SetTimer("RegisterDeviceManagerListener", XCOLLIE_TIMEOUT_S, diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 84fd34e21a7dd58766dfa3f0d49d6b0acccd2d7f..1596e5b926f97fa29ff58257a91af491049f51ee 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -713,6 +713,11 @@ int32_t SoftbusListener::GetNetworkIdByUdid(const std::string &udid, std::string return SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, networkId); } +int32_t SoftbusListener::GetDeviceNameByUdid(const std::string &udid, std::string &deviceName) +{ + return SoftbusCache::GetInstance().GetDeviceNameFromCache(udid, deviceName); +} + int32_t SoftbusListener::SetLocalDeviceName(const std::string &localDeviceName, const std::string &localDisplayName) { @@ -1181,5 +1186,54 @@ void SoftbusListener::DeleteCacheDeviceInfo() DeviceOffLine(it); } } + +int32_t SoftbusListener::GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra, + std::vector &deviceList) +{ + int64_t tokenId = 0; + nlohmann::json extraJson = nlohmann::json::parse(extra, nullptr, false); + if (!extraJson.is_discarded()) { + if (IsInt64(extraJson, TOKENID)) { + tokenId = json[TOKENID].get(); + } + } + std::vector allProfile = DeviceProfileConnector::GetInstance().GetAllAccessControlProfile(); + for (AccessControlProfile profile : allProfile) { + Accessee acee = profile.GetAccessee(); + Accesser acer = profile.GetAccesser(); + if (pkgName == acee.GetAccesseeBundleName() && pkgName == acer.GetAccesserBundleName() + && tokenId == acee.GetAccesseeTokenId() && tokenId == acer.GetAccesserTokenId()) { + DmDeviceInfo deviceInfo; + deviceInfo.bindLevel = profile.GetBindLevel(); + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + if (Crypto::GetUdidHash(profile.GetTrustDeviceId(), reinterpret_cast(udidHash)) != DM_OK) { + LOGE("get udidhash by udid: %{public}s failed.", GetAnonyString(profile.GetTrustDeviceId()).c_str()); + continue; + } + if (memcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId), udidHash, + std::min(sizeof(deviceInfo.deviceId), sizeof(udidHash))) != DM_OK) { + LOGE("GetAllTrustedDeviceList copy deviceId failed."); + continue; + } + + std::string networkId = ""; + if (GetNetworkIdByUdid(profile.GetTrustDeviceId(), networkId) == DM_OK) { + if (memcpy_s(deviceInfo.networkId, sizeof(deviceInfo.networkId), networkId.c_str(), + std::min(sizeof(deviceInfo.networkId), sizeof(networkId.c_str()))) != DM_OK) { + LOGE("GetAllTrustedDeviceList copy networkId data failed."); + } + } + + std::string deviceName = ""; + if (GetDeviceNameByUdid(profile.GetTrustDeviceId(), deviceName) == DM_OK) { + if (memcpy_s(deviceInfo.deviceName, sizeof(deviceInfo.deviceName), deviceName.c_str(), + std::min(sizeof(deviceInfo.deviceName), sizeof(deviceName.c_str()))) != DM_OK) { + LOGE("GetAllTrustedDeviceList copy deviceName data failed."); + } + } + deviceList.push_back(deviceInfo); + } + } +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/softbuscache/include/dm_softbus_cache.h b/services/softbuscache/include/dm_softbus_cache.h index 5a174234d46f38729c66964bcdd445e88b24d794..8721e01dc6a888dcb5b7bb314c750003e73ec13b 100644 --- a/services/softbuscache/include/dm_softbus_cache.h +++ b/services/softbuscache/include/dm_softbus_cache.h @@ -49,6 +49,7 @@ public: int32_t GetUdidByUdidHash(const std::string &udidHash, std::string &udid); int32_t GetUuidByUdid(const std::string &udid, std::string &uuid); int32_t GetNetworkIdFromCache(const std::string &udid, std::string &networkId); + int32_t GetDeviceNameFromCache(const std::string &udid, std::string &deviceName); bool CheckIsOnline(const std::string &udidHash); private: int32_t GetUdidByNetworkId(const char *networkId, std::string &udid); diff --git a/services/softbuscache/src/dm_softbus_cache.cpp b/services/softbuscache/src/dm_softbus_cache.cpp index a97bad57b0c680e37695f7ccb14e12666e83bcb8..0ca777aa8f267581a6cb6bc60fa55868eb58225f 100644 --- a/services/softbuscache/src/dm_softbus_cache.cpp +++ b/services/softbuscache/src/dm_softbus_cache.cpp @@ -446,6 +446,21 @@ int32_t SoftbusCache::GetNetworkIdFromCache(const std::string &udid, std::string return ERR_DM_FAILED; } +int32_t SoftbusCache::GetDeviceNameFromCache(const std::string &udid, std::string &deviceName) +{ + LOGI("udid %{public}s.", GetAnonyString(udid).c_str()); + { + std::lock_guard mutexLock(deviceInfosMutex_); + if (deviceInfo_.find(udid) != deviceInfo_.end()) { + deviceName = deviceInfo_[udid].second.deviceName; + LOGI("GetDeviceNameFromCache success deviceName %{public}s, udid %{public}s.", + GetAnonyString(networkId).c_str(), GetAnonyString(udid).c_str()); + return DM_OK; + } + } + return ERR_DM_FAILED; +} + bool SoftbusCache::CheckIsOnline(const std::string &deviceId) { LOGI("deviceId %{public}s.", GetAnonyString(deviceId).c_str());