179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci#ifndef OHOS_DM_DEVICEPROFILE_CONNECTOR_H 1679a732c7Sopenharmony_ci#define OHOS_DM_DEVICEPROFILE_CONNECTOR_H 1779a732c7Sopenharmony_ci#include <algorithm> 1879a732c7Sopenharmony_ci#include <string> 1979a732c7Sopenharmony_ci#include <unordered_set> 2079a732c7Sopenharmony_ci#include "access_control_profile.h" 2179a732c7Sopenharmony_ci#include "dm_device_info.h" 2279a732c7Sopenharmony_ci#include "dm_single_instance.h" 2379a732c7Sopenharmony_ci 2479a732c7Sopenharmony_ciconstexpr uint32_t ALLOW_AUTH_ONCE = 1; 2579a732c7Sopenharmony_ciconstexpr uint32_t ALLOW_AUTH_ALWAYS = 2; 2679a732c7Sopenharmony_ci 2779a732c7Sopenharmony_ciconstexpr uint32_t INVALIED_TYPE = 0; 2879a732c7Sopenharmony_ciconstexpr uint32_t APP_PEER_TO_PEER_TYPE = 1; 2979a732c7Sopenharmony_ciconstexpr uint32_t APP_ACROSS_ACCOUNT_TYPE = 2; 3079a732c7Sopenharmony_ciconstexpr uint32_t DEVICE_PEER_TO_PEER_TYPE = 3; 3179a732c7Sopenharmony_ciconstexpr uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 4; 3279a732c7Sopenharmony_ciconstexpr uint32_t IDENTICAL_ACCOUNT_TYPE = 5; 3379a732c7Sopenharmony_ci 3479a732c7Sopenharmony_ciconstexpr uint32_t DM_IDENTICAL_ACCOUNT = 1; 3579a732c7Sopenharmony_ciconstexpr uint32_t DM_POINT_TO_POINT = 256; 3679a732c7Sopenharmony_ciconstexpr uint32_t DM_ACROSS_ACCOUNT = 1282; 3779a732c7Sopenharmony_ciconstexpr uint32_t DM_INVALIED_BINDTYPE = 2048; 3879a732c7Sopenharmony_ciconstexpr uint32_t DEVICE = 1; 3979a732c7Sopenharmony_ciconstexpr uint32_t SERVICE = 2; 4079a732c7Sopenharmony_ciconstexpr uint32_t APP = 3; 4179a732c7Sopenharmony_ci 4279a732c7Sopenharmony_ciconstexpr uint32_t INACTIVE = 0; 4379a732c7Sopenharmony_ciconstexpr uint32_t ACTIVE = 1; 4479a732c7Sopenharmony_ci 4579a732c7Sopenharmony_citypedef struct DmDiscoveryInfo { 4679a732c7Sopenharmony_ci std::string pkgname; 4779a732c7Sopenharmony_ci std::string localDeviceId; 4879a732c7Sopenharmony_ci std::string remoteDeviceIdHash; 4979a732c7Sopenharmony_ci} DmDiscoveryInfo; 5079a732c7Sopenharmony_ci 5179a732c7Sopenharmony_citypedef struct DmAclInfo { 5279a732c7Sopenharmony_ci std::string sessionKey; 5379a732c7Sopenharmony_ci int32_t bindType; 5479a732c7Sopenharmony_ci int32_t state; 5579a732c7Sopenharmony_ci std::string trustDeviceId; 5679a732c7Sopenharmony_ci int32_t bindLevel; 5779a732c7Sopenharmony_ci int32_t authenticationType; 5879a732c7Sopenharmony_ci std::string deviceIdHash; 5979a732c7Sopenharmony_ci} DmAclInfo; 6079a732c7Sopenharmony_ci 6179a732c7Sopenharmony_citypedef struct DmAccesser { 6279a732c7Sopenharmony_ci uint64_t requestTokenId; 6379a732c7Sopenharmony_ci std::string requestBundleName; 6479a732c7Sopenharmony_ci int32_t requestUserId; 6579a732c7Sopenharmony_ci std::string requestAccountId; 6679a732c7Sopenharmony_ci std::string requestDeviceId; 6779a732c7Sopenharmony_ci int32_t requestTargetClass; 6879a732c7Sopenharmony_ci} DmAccesser; 6979a732c7Sopenharmony_ci 7079a732c7Sopenharmony_citypedef struct DmAccessee { 7179a732c7Sopenharmony_ci uint64_t trustTokenId; 7279a732c7Sopenharmony_ci std::string trustBundleName; 7379a732c7Sopenharmony_ci int32_t trustUserId; 7479a732c7Sopenharmony_ci std::string trustAccountId; 7579a732c7Sopenharmony_ci std::string trustDeviceId; 7679a732c7Sopenharmony_ci int32_t trustTargetClass; 7779a732c7Sopenharmony_ci} DmAccessee; 7879a732c7Sopenharmony_ci 7979a732c7Sopenharmony_citypedef struct DmOfflineParam { 8079a732c7Sopenharmony_ci uint32_t bindType; 8179a732c7Sopenharmony_ci std::vector<std::string> pkgNameVec; 8279a732c7Sopenharmony_ci int32_t leftAclNumber; 8379a732c7Sopenharmony_ci} DmOfflineParam; 8479a732c7Sopenharmony_ci 8579a732c7Sopenharmony_cinamespace OHOS { 8679a732c7Sopenharmony_cinamespace DistributedHardware { 8779a732c7Sopenharmony_ciclass IDeviceProfileConnector { 8879a732c7Sopenharmony_cipublic: 8979a732c7Sopenharmony_ci virtual ~IDeviceProfileConnector() {} 9079a732c7Sopenharmony_ci virtual int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) = 0; 9179a732c7Sopenharmony_ci}; 9279a732c7Sopenharmony_ci 9379a732c7Sopenharmony_ciclass DeviceProfileConnector : public IDeviceProfileConnector { 9479a732c7Sopenharmony_ci DM_DECLARE_SINGLE_INSTANCE(DeviceProfileConnector); 9579a732c7Sopenharmony_cipublic: 9679a732c7Sopenharmony_ci std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfile(); 9779a732c7Sopenharmony_ci std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfileByUserId(int32_t userId); 9879a732c7Sopenharmony_ci uint32_t CheckBindType(std::string trustDeviceId, std::string requestDeviceId); 9979a732c7Sopenharmony_ci int32_t PutAccessControlList(DmAclInfo aclInfo, DmAccesser dmAccesser, DmAccessee dmAccessee); 10079a732c7Sopenharmony_ci int32_t UpdateAccessControlList(int32_t userId, std::string &oldAccountId, std::string &newAccountId); 10179a732c7Sopenharmony_ci std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceList(const std::string &pkgName, 10279a732c7Sopenharmony_ci const std::string &deviceId); 10379a732c7Sopenharmony_ci DmOfflineParam GetOfflineParamFromAcl(std::string trustDeviceId, std::string requestDeviceId); 10479a732c7Sopenharmony_ci std::vector<int32_t> GetBindTypeByPkgName(std::string pkgName, std::string requestDeviceId, 10579a732c7Sopenharmony_ci std::string trustUdid); 10679a732c7Sopenharmony_ci std::vector<int32_t> SyncAclByBindType(std::string pkgName, std::vector<int32_t> bindTypeVec, 10779a732c7Sopenharmony_ci std::string localDeviceId, std::string targetDeviceId); 10879a732c7Sopenharmony_ci int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm); 10979a732c7Sopenharmony_ci void DeleteAclForAccountLogOut(const std::string &localUdid, int32_t userId, const std::string &remoteUdid); 11079a732c7Sopenharmony_ci void DeleteAclForUserRemoved(int32_t userId); 11179a732c7Sopenharmony_ci DmOfflineParam DeleteAccessControlList(const std::string &pkgName, const std::string &localDeviceId, 11279a732c7Sopenharmony_ci const std::string &remoteDeviceId, int32_t bindLevel); 11379a732c7Sopenharmony_ci std::vector<std::string> GetPkgNameFromAcl(std::string &localDeviceId, std::string &targetDeviceId); 11479a732c7Sopenharmony_ci bool CheckIdenticalAccount(int32_t userId, const std::string &accountId); 11579a732c7Sopenharmony_ci bool CheckSrcDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 11679a732c7Sopenharmony_ci bool CheckSinkDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 11779a732c7Sopenharmony_ci uint32_t DeleteTimeOutAcl(const std::string &deviceId); 11879a732c7Sopenharmony_ci int32_t GetTrustNumber(const std::string &deviceId); 11979a732c7Sopenharmony_ci bool CheckDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); 12079a732c7Sopenharmony_ci std::vector<int32_t> CompareBindType(std::vector<DistributedDeviceProfile::AccessControlProfile> profiles, 12179a732c7Sopenharmony_ci std::string pkgName, std::vector<int32_t> &sinkBindType, std::string localDeviceId, std::string targetDeviceId); 12279a732c7Sopenharmony_ci int32_t IsSameAccount(const std::string &udid); 12379a732c7Sopenharmony_ci int32_t CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid, 12479a732c7Sopenharmony_ci const DmAccessCallee &callee, const std::string &sinkUdid); 12579a732c7Sopenharmony_ci int32_t CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid, 12679a732c7Sopenharmony_ci const DmAccessCallee &callee, const std::string &sinkUdid); 12779a732c7Sopenharmony_ci void DeleteAccessControlList(const std::string &udid); 12879a732c7Sopenharmony_ci int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid, 12979a732c7Sopenharmony_ci const std::string &udid, uint64_t &tokenId); 13079a732c7Sopenharmony_ci std::map<std::string, int32_t> GetDeviceIdAndBindType(int32_t userId, const std::string &accountId, 13179a732c7Sopenharmony_ci const std::string &localUdid); 13279a732c7Sopenharmony_ci int32_t HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, 13379a732c7Sopenharmony_ci const std::string &remoteUdid, const std::string &localUdid); 13479a732c7Sopenharmony_ci int32_t HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, const std::string &localUdid); 13579a732c7Sopenharmony_ci std::string HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId, 13679a732c7Sopenharmony_ci const std::string &localUdid); 13779a732c7Sopenharmony_ci std::vector<DistributedDeviceProfile::AccessControlProfile> GetAllAccessControlProfile(); 13879a732c7Sopenharmony_ci void DeleteAccessControlById(int64_t accessControlId); 13979a732c7Sopenharmony_ci 14079a732c7Sopenharmony_ciprivate: 14179a732c7Sopenharmony_ci int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); 14279a732c7Sopenharmony_ci void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string pkgName, 14379a732c7Sopenharmony_ci std::string requestDeviceId, std::vector<int32_t> &bindTypeVec); 14479a732c7Sopenharmony_ci void ProcessBindType(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo paramInfo, 14579a732c7Sopenharmony_ci std::vector<int32_t> &sinkBindType, std::vector<int32_t> &bindTypeIndex, uint32_t index); 14679a732c7Sopenharmony_ci int32_t GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, const std::string &trustDev, 14779a732c7Sopenharmony_ci const std::string &reqDev); 14879a732c7Sopenharmony_ci int32_t CheckAuthForm(DmAuthForm form, DistributedDeviceProfile::AccessControlProfile profiles, 14979a732c7Sopenharmony_ci DmDiscoveryInfo discoveryInfo); 15079a732c7Sopenharmony_ci bool SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller, 15179a732c7Sopenharmony_ci const DmAccessCallee &callee); 15279a732c7Sopenharmony_ci void DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName, 15379a732c7Sopenharmony_ci const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 15479a732c7Sopenharmony_ci const std::string &remoteUdid); 15579a732c7Sopenharmony_ci void DeleteDeviceBindLevel(DmOfflineParam &offlineParam, 15679a732c7Sopenharmony_ci const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 15779a732c7Sopenharmony_ci const std::string &remoteUdid); 15879a732c7Sopenharmony_ci void DeleteServiceBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName, 15979a732c7Sopenharmony_ci const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid, 16079a732c7Sopenharmony_ci const std::string &remoteUdid); 16179a732c7Sopenharmony_ci void UpdateBindType(const std::string &udid, int32_t bindType, std::map<std::string, int32_t> &deviceMap); 16279a732c7Sopenharmony_ci}; 16379a732c7Sopenharmony_ci 16479a732c7Sopenharmony_ciextern "C" IDeviceProfileConnector *CreateDpConnectorInstance(); 16579a732c7Sopenharmony_ciusing CreateDpConnectorFuncPtr = IDeviceProfileConnector *(*)(void); 16679a732c7Sopenharmony_ci} // namespace DistributedHardware 16779a732c7Sopenharmony_ci} // namespace OHOS 16879a732c7Sopenharmony_ci#endif // OHOS_DM_DEVICEPROFILE_CONNECTOR_H 169