1 /* 2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef OHOS_DM_DEVICEPROFILE_CONNECTOR_H 16 #define OHOS_DM_DEVICEPROFILE_CONNECTOR_H 17 #include <algorithm> 18 #include <string> 19 #include <unordered_set> 20 #include "access_control_profile.h" 21 #include "dm_device_info.h" 22 #include "dm_single_instance.h" 23 #include "i_dp_inited_callback.h" 24 #include "local_service_info.h" 25 #include "trusted_device_info.h" 26 27 enum AllowAuthType { 28 ALLOW_AUTH_ONCE = 1, 29 ALLOW_AUTH_ALWAYS = 2 30 }; 31 32 extern const uint32_t INVALIED_TYPE; 33 extern const uint32_t APP_PEER_TO_PEER_TYPE; 34 extern const uint32_t APP_ACROSS_ACCOUNT_TYPE; 35 extern const uint32_t DEVICE_PEER_TO_PEER_TYPE; 36 extern const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE; 37 extern const uint32_t IDENTICAL_ACCOUNT_TYPE; 38 39 extern const uint32_t DM_IDENTICAL_ACCOUNT; 40 extern const uint32_t DM_POINT_TO_POINT; 41 extern const uint32_t DM_ACROSS_ACCOUNT; 42 extern const uint32_t DM_INVALIED_BINDTYPE; 43 extern const uint32_t DEVICE; 44 extern const uint32_t SERVICE; 45 extern const uint32_t APP; 46 47 enum ProfileState { 48 INACTIVE = 0, 49 ACTIVE = 1 50 }; 51 52 typedef struct DmDiscoveryInfo { 53 std::string pkgname; 54 std::string localDeviceId; 55 int32_t userId; 56 std::string remoteDeviceIdHash; 57 } DmDiscoveryInfo; 58 59 typedef struct DmAclInfo { 60 std::string sessionKey; 61 int32_t bindType; 62 int32_t state; 63 std::string trustDeviceId; 64 int32_t bindLevel; 65 int32_t authenticationType; 66 std::string deviceIdHash; 67 } DmAclInfo; 68 69 typedef struct DmAccesser { 70 uint64_t requestTokenId; 71 std::string requestBundleName; 72 int32_t requestUserId; 73 std::string requestAccountId; 74 std::string requestDeviceId; 75 int32_t requestTargetClass; 76 std::string requestDeviceName; 77 } DmAccesser; 78 79 typedef struct DmAccessee { 80 uint64_t trustTokenId; 81 std::string trustBundleName; 82 int32_t trustUserId; 83 std::string trustAccountId; 84 std::string trustDeviceId; 85 int32_t trustTargetClass; 86 std::string trustDeviceName; 87 } DmAccessee; 88 89 typedef struct DmOfflineParam { 90 uint32_t bindType; 91 std::vector<OHOS::DistributedHardware::ProcessInfo> processVec; 92 int32_t leftAclNumber; 93 } DmOfflineParam; 94 95 namespace OHOS { 96 namespace DistributedHardware { 97 class IDeviceProfileConnector { 98 public: ~IDeviceProfileConnector()99 virtual ~IDeviceProfileConnector() {} 100 virtual int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) = 0; 101 virtual std::map<std::string, int32_t> GetDeviceIdAndBindLevel(std::vector<int32_t> userIds, 102 const std::string &localUdid) = 0; 103 virtual int32_t HandleUserSwitched(const std::string &localUdid, const std::vector<std::string> &deviceVec, 104 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds) = 0; 105 virtual bool CheckAclStatusAndForegroundNotMatch(const std::string &localUdid, 106 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds) = 0; 107 virtual int32_t HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid) = 0; 108 virtual int32_t HandleUserStop(int32_t stopUserId, const std::string &localUdid, 109 const std::vector<std::string> &acceptEventUdids) = 0; 110 }; 111 112 class DeviceProfileConnector : public IDeviceProfileConnector { 113 DM_DECLARE_SINGLE_INSTANCE(DeviceProfileConnector); 114 public: 115 std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfile(); 116 std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfileByUserId(int32_t userId); 117 std::vector<DistributedDeviceProfile::AccessControlProfile> GetAclProfileByDeviceIdAndUserId( 118 const std::string &deviceId, int32_t userId); 119 uint32_t CheckBindType(std::string peerUdid, std::string localUdid); 120 int32_t PutAccessControlList(DmAclInfo aclInfo, DmAccesser dmAccesser, DmAccessee dmAccessee); 121 int32_t UpdateAccessControlList(int32_t userId, std::string &oldAccountId, std::string &newAccountId); 122 std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceList(const std::string &pkgName, 123 const std::string &deviceId); 124 std::vector<int32_t> GetBindTypeByPkgName(std::string pkgName, std::string requestDeviceId, 125 std::string trustUdid); 126 uint64_t GetTokenIdByNameAndDeviceId(std::string pkgName, std::string requestDeviceId); 127 std::vector<int32_t> SyncAclByBindType(std::string pkgName, std::vector<int32_t> bindTypeVec, 128 std::string localDeviceId, std::string targetDeviceId); 129 int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm); 130 bool DeleteAclForAccountLogOut(const std::string &localUdid, int32_t localUserId, 131 const std::string &peerUdid, int32_t peerUserId); 132 void DeleteAclForUserRemoved(std::string localUdid, int32_t userId); 133 void DeleteAclForRemoteUserRemoved(std::string peerUdid, int32_t peerUserId, std::vector<int32_t> &userIds); 134 DmOfflineParam DeleteAccessControlList(const std::string &pkgName, const std::string &localDeviceId, 135 const std::string &remoteDeviceId, int32_t bindLevel, const std::string &extra); 136 std::vector<OHOS::DistributedHardware::ProcessInfo> GetProcessInfoFromAclByUserId(const std::string &localDeviceId, 137 const std::string &targetDeviceId, int32_t userId); 138 bool CheckIdenticalAccount(int32_t userId, const std::string &accountId); 139 bool CheckSrcDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 140 bool CheckSinkDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 141 uint32_t DeleteTimeOutAcl(const std::string &deviceId); 142 int32_t GetTrustNumber(const std::string &deviceId); 143 bool CheckDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 144 std::vector<int32_t> CompareBindType(std::vector<DistributedDeviceProfile::AccessControlProfile> profiles, 145 std::string pkgName, std::vector<int32_t> &sinkBindType, std::string localDeviceId, std::string targetDeviceId); 146 int32_t IsSameAccount(const std::string &udid); 147 int32_t CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid, 148 const DmAccessCallee &callee, const std::string &sinkUdid); 149 int32_t CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, 150 const DmAccessCallee &callee, const std::string &sinkUdid); 151 void DeleteAccessControlList(const std::string &udid); 152 int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid, 153 const std::string &udid, uint64_t &tokenId); 154 std::map<std::string, int32_t> GetDeviceIdAndBindLevel(std::vector<int32_t> userIds, const std::string &localUdid); 155 std::multimap<std::string, int32_t> GetDeviceIdAndUserId(int32_t userId, const std::string &accountId, 156 const std::string &localUdid); 157 int32_t HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, 158 const std::string &remoteUdid, const std::string &localUdid); 159 int32_t HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, const std::string &localUdid); 160 OHOS::DistributedHardware::ProcessInfo HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, 161 int32_t tokenId, const std::string &localUdid); 162 OHOS::DistributedHardware::ProcessInfo HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, 163 int32_t tokenId, const std::string &localUdid, int32_t peerTokenId); 164 std::vector<DistributedDeviceProfile::AccessControlProfile> GetAllAccessControlProfile(); 165 void DeleteAccessControlById(int64_t accessControlId); 166 int32_t HandleUserSwitched(const std::string &localUdid, const std::vector<std::string> &deviceVec, 167 int32_t currentUserId, int32_t beforeUserId); 168 int32_t HandleUserSwitched(const std::string &localUdid, const std::vector<std::string> &deviceVec, 169 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds); 170 bool CheckAclStatusAndForegroundNotMatch(const std::string &localUdid, 171 const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds); 172 void HandleUserSwitched(const std::vector<DistributedDeviceProfile::AccessControlProfile> &activeProfiles, 173 const std::vector<DistributedDeviceProfile::AccessControlProfile> &inActiveProfiles, 174 const std::vector<DistributedDeviceProfile::AccessControlProfile> &delActiveProfiles); 175 void HandleSyncForegroundUserIdEvent(const std::vector<int32_t> &remoteUserIds, const std::string &remoteUdid, 176 const std::vector<int32_t> &localUserIds, std::string &localUdid); 177 std::vector<ProcessInfo> GetOfflineProcessInfo(std::string &localUdid, const std::vector<int32_t> &localUserIds, 178 const std::string &remoteUdid, const std::vector<int32_t> &remoteUserIds); 179 std::map<int32_t, int32_t> GetUserIdAndBindLevel(const std::string &localUdid, const std::string &peerUdid); 180 void UpdateACL(std::string &localUdid, const std::vector<int32_t> &localUserIds, 181 const std::string &remoteUdid, const std::vector<int32_t> &remoteFrontUserIds, 182 const std::vector<int32_t> &remoteBackUserIds); 183 std::multimap<std::string, int32_t> GetDevIdAndUserIdByActHash(const std::string &localUdid, 184 const std::string &peerUdid, int32_t peerUserId, const std::string &peerAccountHash); 185 std::multimap<std::string, int32_t> GetDeviceIdAndUserId(const std::string &localUdid, int32_t localUserId); 186 void HandleSyncBackgroundUserIdEvent(const std::vector<int32_t> &remoteUserIds, const std::string &remoteUdid, 187 const std::vector<int32_t> &localUserIds, std::string &localUdid); 188 void HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid, 189 const std::string &localUdid, int32_t localUserId, const std::string &localAccountId); 190 int32_t SubscribeDeviceProfileInited(sptr<DistributedDeviceProfile::IDpInitedCallback> dpInitedCallback); 191 int32_t UnSubscribeDeviceProfileInited(); 192 int32_t PutAllTrustedDevices(const std::vector<DistributedDeviceProfile::TrustedDeviceInfo> &deviceInfos); 193 int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); 194 int32_t UpdateAclDeviceName(const std::string &udid, const std::string &newDeviceName); 195 int32_t PutLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo); 196 int32_t DeleteLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType); 197 int32_t UpdateLocalServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &localServiceInfo); 198 int32_t GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string &bundleName, 199 int32_t pinExchangeType, DistributedDeviceProfile::LocalServiceInfo &localServiceInfo); 200 int32_t PutSessionKey(const std::vector<unsigned char> &sessionKeyArray, int32_t &sessionKeyId); 201 int32_t HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); 202 int32_t HandleUserStop(int32_t stopUserId, const std::string &localUdid, 203 const std::vector<std::string> &acceptEventUdids); 204 205 private: 206 int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); 207 void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, 208 std::vector<int32_t> &bindTypeVec, std::string trustUdid); 209 void ProcessBindType(DistributedDeviceProfile::AccessControlProfile profiles, std::string localDeviceId, 210 std::vector<int32_t> &sinkBindType, std::vector<int32_t> &bindTypeIndex, 211 uint32_t index, std::string targetDeviceId); 212 bool CheckAppLevelAccess(const DistributedDeviceProfile::AccessControlProfile &profile, 213 const DmAccessCaller &caller, const DmAccessCallee &callee); 214 int32_t GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, const std::string &trustDev, 215 const std::string &reqDev); 216 int32_t CheckAuthForm(DmAuthForm form, DistributedDeviceProfile::AccessControlProfile profiles, 217 DmDiscoveryInfo discoveryInfo); 218 bool SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller, 219 const DmAccessCallee &callee); 220 void DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName, 221 const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 222 const std::string &remoteUdid); 223 void DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName, 224 const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 225 const std::string &remoteUdid, const std::string &extra); 226 void DeleteDeviceBindLevel(DmOfflineParam &offlineParam, 227 const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 228 const std::string &remoteUdid); 229 void DeleteServiceBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName, 230 const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 231 const std::string &remoteUdid); 232 void UpdateBindType(const std::string &udid, int32_t compareParam, std::map<std::string, int32_t> &deviceMap); 233 std::vector<DistributedDeviceProfile::AccessControlProfile> GetAclProfileByUserId(const std::string &localUdid, 234 int32_t userId, const std::string &remoteUdid); 235 void DeleteSigTrustACL(DistributedDeviceProfile::AccessControlProfile profile, const std::string &remoteUdid, 236 const std::vector<int32_t> &remoteFrontUserIds, const std::vector<int32_t> &remoteBackUserIds); 237 void UpdatePeerUserId(DistributedDeviceProfile::AccessControlProfile profile, std::string &localUdid, 238 const std::vector<int32_t> &localUserIds, const std::string &remoteUdid, 239 const std::vector<int32_t> &remoteFrontUserIds); 240 bool CheckAclStatusNotMatch(const DistributedDeviceProfile::AccessControlProfile &profile, 241 const std::string &localUdid, const std::vector<int32_t> &foregroundUserIds, 242 const std::vector<int32_t> &backgroundUserIds); 243 }; 244 245 extern "C" IDeviceProfileConnector *CreateDpConnectorInstance(); 246 using CreateDpConnectorFuncPtr = IDeviceProfileConnector *(*)(void); 247 } // namespace DistributedHardware 248 } // namespace OHOS 249 #endif // OHOS_DM_DEVICEPROFILE_CONNECTOR_H 250