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