1 /*
2  * Copyright (c) 2023-2024 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 
24 constexpr uint32_t ALLOW_AUTH_ONCE = 1;
25 constexpr uint32_t ALLOW_AUTH_ALWAYS = 2;
26 
27 constexpr uint32_t INVALIED_TYPE = 0;
28 constexpr uint32_t APP_PEER_TO_PEER_TYPE = 1;
29 constexpr uint32_t APP_ACROSS_ACCOUNT_TYPE = 2;
30 constexpr uint32_t DEVICE_PEER_TO_PEER_TYPE = 3;
31 constexpr uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 4;
32 constexpr uint32_t IDENTICAL_ACCOUNT_TYPE = 5;
33 
34 constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1;
35 constexpr uint32_t DM_POINT_TO_POINT = 256;
36 constexpr uint32_t DM_ACROSS_ACCOUNT = 1282;
37 constexpr uint32_t DM_INVALIED_BINDTYPE = 2048;
38 constexpr uint32_t DEVICE = 1;
39 constexpr uint32_t SERVICE = 2;
40 constexpr uint32_t APP = 3;
41 
42 constexpr uint32_t INACTIVE = 0;
43 constexpr uint32_t ACTIVE = 1;
44 
45 typedef struct DmDiscoveryInfo {
46     std::string pkgname;
47     std::string localDeviceId;
48     std::string remoteDeviceIdHash;
49 } DmDiscoveryInfo;
50 
51 typedef struct DmAclInfo {
52     std::string sessionKey;
53     int32_t bindType;
54     int32_t state;
55     std::string trustDeviceId;
56     int32_t bindLevel;
57     int32_t authenticationType;
58     std::string deviceIdHash;
59 } DmAclInfo;
60 
61 typedef struct DmAccesser {
62     uint64_t requestTokenId;
63     std::string requestBundleName;
64     int32_t requestUserId;
65     std::string requestAccountId;
66     std::string requestDeviceId;
67     int32_t requestTargetClass;
68 } DmAccesser;
69 
70 typedef struct DmAccessee {
71     uint64_t trustTokenId;
72     std::string trustBundleName;
73     int32_t trustUserId;
74     std::string trustAccountId;
75     std::string trustDeviceId;
76     int32_t trustTargetClass;
77 } DmAccessee;
78 
79 typedef struct DmOfflineParam {
80     uint32_t bindType;
81     std::vector<std::string> pkgNameVec;
82     int32_t leftAclNumber;
83 } DmOfflineParam;
84 
85 namespace OHOS {
86 namespace DistributedHardware {
87 class IDeviceProfileConnector {
88 public:
~IDeviceProfileConnector()89     virtual ~IDeviceProfileConnector() {}
90     virtual int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) = 0;
91 };
92 
93 class DeviceProfileConnector : public IDeviceProfileConnector {
94     DM_DECLARE_SINGLE_INSTANCE(DeviceProfileConnector);
95 public:
96     std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfile();
97     std::vector<DistributedDeviceProfile::AccessControlProfile> GetAccessControlProfileByUserId(int32_t userId);
98     uint32_t CheckBindType(std::string trustDeviceId, std::string requestDeviceId);
99     int32_t PutAccessControlList(DmAclInfo aclInfo, DmAccesser dmAccesser, DmAccessee dmAccessee);
100     int32_t UpdateAccessControlList(int32_t userId, std::string &oldAccountId, std::string &newAccountId);
101     std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceList(const std::string &pkgName,
102         const std::string &deviceId);
103     DmOfflineParam GetOfflineParamFromAcl(std::string trustDeviceId, std::string requestDeviceId);
104     std::vector<int32_t> GetBindTypeByPkgName(std::string pkgName, std::string requestDeviceId,
105         std::string trustUdid);
106     std::vector<int32_t> SyncAclByBindType(std::string pkgName, std::vector<int32_t> bindTypeVec,
107         std::string localDeviceId, std::string targetDeviceId);
108     int32_t GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm);
109     void DeleteAclForAccountLogOut(const std::string &localUdid, int32_t userId, const std::string &remoteUdid);
110     void DeleteAclForUserRemoved(int32_t userId);
111     DmOfflineParam DeleteAccessControlList(const std::string &pkgName, const std::string &localDeviceId,
112         const std::string &remoteDeviceId, int32_t bindLevel);
113     std::vector<std::string> GetPkgNameFromAcl(std::string &localDeviceId, std::string &targetDeviceId);
114     bool CheckIdenticalAccount(int32_t userId, const std::string &accountId);
115     bool CheckSrcDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId);
116     bool CheckSinkDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId);
117     uint32_t DeleteTimeOutAcl(const std::string &deviceId);
118     int32_t GetTrustNumber(const std::string &deviceId);
119     bool CheckDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId);
120     std::vector<int32_t> CompareBindType(std::vector<DistributedDeviceProfile::AccessControlProfile> profiles,
121         std::string pkgName, std::vector<int32_t> &sinkBindType, std::string localDeviceId, std::string targetDeviceId);
122     int32_t IsSameAccount(const std::string &udid);
123     int32_t CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
124         const DmAccessCallee &callee, const std::string &sinkUdid);
125     int32_t CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
126         const DmAccessCallee &callee, const std::string &sinkUdid);
127     void DeleteAccessControlList(const std::string &udid);
128     int32_t GetBindLevel(const std::string &pkgName, const std::string &localUdid,
129         const std::string &udid, uint64_t &tokenId);
130     std::map<std::string, int32_t> GetDeviceIdAndBindType(int32_t userId, const std::string &accountId,
131         const std::string &localUdid);
132     int32_t HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash,
133         const std::string &remoteUdid, const std::string &localUdid);
134     int32_t HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, const std::string &localUdid);
135     std::string HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid, int32_t tokenId,
136         const std::string &localUdid);
137     std::vector<DistributedDeviceProfile::AccessControlProfile> GetAllAccessControlProfile();
138     void DeleteAccessControlById(int64_t accessControlId);
139 
140 private:
141     int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo);
142     void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string pkgName,
143         std::string requestDeviceId, std::vector<int32_t> &bindTypeVec);
144     void ProcessBindType(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo paramInfo,
145         std::vector<int32_t> &sinkBindType, std::vector<int32_t> &bindTypeIndex, uint32_t index);
146     int32_t GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, const std::string &trustDev,
147         const std::string &reqDev);
148     int32_t CheckAuthForm(DmAuthForm form, DistributedDeviceProfile::AccessControlProfile profiles,
149         DmDiscoveryInfo discoveryInfo);
150     bool SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller,
151         const DmAccessCallee &callee);
152     void DeleteAppBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName,
153         const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid,
154         const std::string &remoteUdid);
155     void DeleteDeviceBindLevel(DmOfflineParam &offlineParam,
156         const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid,
157         const std::string &remoteUdid);
158     void DeleteServiceBindLevel(DmOfflineParam &offlineParam, const std::string &pkgName,
159         const std::vector<DistributedDeviceProfile::AccessControlProfile> &profiles, const std::string &localUdid,
160         const std::string &remoteUdid);
161     void UpdateBindType(const std::string &udid, int32_t bindType, std::map<std::string, int32_t> &deviceMap);
162 };
163 
164 extern "C" IDeviceProfileConnector *CreateDpConnectorInstance();
165 using CreateDpConnectorFuncPtr = IDeviceProfileConnector *(*)(void);
166 } // namespace DistributedHardware
167 } // namespace OHOS
168 #endif // OHOS_DM_DEVICEPROFILE_CONNECTOR_H
169