1 /*
2  * Copyright (c) 2022-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 
16 #ifndef OHOS_HICHAIN_CONNECTOR_H
17 #define OHOS_HICHAIN_CONNECTOR_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "device_auth.h"
27 #include "device_auth_defines.h"
28 #include "dm_constants.h"
29 #include "dm_device_info.h"
30 #include "hichain_connector_callback.h"
31 #include "nlohmann/json.hpp"
32 #include "dm_single_instance.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 
37 constexpr const char* FIELD_TYPE = "TType";
38 constexpr const char* FIELD_OPERATION_CODE = "operationCode";
39 constexpr const char* FIELD_META_NODE_TYPE = "metaNodeType";
40 
41 struct GroupInfo {
42     std::string groupName;
43     std::string groupId;
44     std::string groupOwner;
45     int32_t groupType;
46     int32_t groupVisibility;
47     std::string userId;
48 
GroupInfoGroupInfo49     GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId("")
50     {
51     }
52 };
53 
54 enum class AuthFormPriority {
55     PRIORITY_PEER_TO_PEER = 0,
56     PRIORITY_ACROSS_ACCOUNT = 1,
57     PRIORITY_IDENTICAL_ACCOUNT = 2,
58 };
59 
60 static const std::unordered_map<int32_t, AuthFormPriority> g_authFormPriorityMap = {
61     {GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP, AuthFormPriority::PRIORITY_IDENTICAL_ACCOUNT},
62     {GROUP_TYPE_ACROSS_ACCOUNT_GROUP, AuthFormPriority::PRIORITY_ACROSS_ACCOUNT},
63     {GROUP_TYPE_PEER_TO_PEER_GROUP, AuthFormPriority::PRIORITY_PEER_TO_PEER}
64 };
65 
66 void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo);
67 
68 class HiChainConnector {
69 public:
70     static bool onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen);
71     static void onFinish(int64_t requestId, int operationCode, const char *returnData);
72     static void onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn);
73     static char *onRequest(int64_t requestId, int operationCode, const char *reqParams);
74 
75 public:
76     HiChainConnector();
77     ~HiChainConnector();
78 
79     /**
80      * @tc.name: HiChainConnector::RegisterHiChainCallback
81      * @tc.desc: Register HiChain Callback of the HiChain Connector
82      * @tc.type: FUNC
83      */
84     int32_t RegisterHiChainCallback(std::shared_ptr<IHiChainConnectorCallback> callback);
85 
86     /**
87      * @tc.name: HiChainConnector::UnRegisterHiChainCallback
88      * @tc.desc: Un Register HiChain Callback of the HiChain Connector
89      * @tc.type: FUNC
90      */
91     int32_t UnRegisterHiChainCallback();
92 
93     /**
94      * @tc.name: HiChainConnector::CreateGroup
95      * @tc.desc: Create Group of the HiChain Connector
96      * @tc.type: FUNC
97      */
98     int32_t CreateGroup(int64_t requestId, const std::string &groupName);
99 
100     /**
101      * @tc.name: HiChainConnector::CreateGroup
102      * @tc.desc: Create Group of the HiChain Connector
103      * @tc.type: FUNC
104      */
105     int32_t CreateGroup(int64_t requestId, int32_t authType, const std::string &userId,
106         nlohmann::json &jsonOutObj);
107 
108     /**
109      * @tc.name: HiChainConnector::AddMember
110      * @tc.desc: Add Member of the HiChain Connector
111      * @tc.type: FUNC
112      */
113     int32_t AddMember(const std::string &deviceId, const std::string &connectInfo);
114 
115     /**
116      * @tc.name: HiChainConnector::DelMemberFromGroup
117      * @tc.desc: Delete Member From Group of the HiChain Connector
118      * @tc.type: FUNC
119      */
120     int32_t DelMemberFromGroup(const std::string &groupId, const std::string &deviceId);
121 
122     /**
123      * @tc.name: HiChainConnector::DeleteGroup
124      * @tc.desc: Delete Group of the HiChain Connector
125      * @tc.type: FUNC
126      */
127     int32_t DeleteGroup(std::string &groupId);
128 
129     /**
130      * @tc.name: HiChainConnector::DeleteGroup
131      * @tc.desc: DeleteGroup of the HiChain Connector
132      * @tc.type: FUNC
133      */
134     int32_t DeleteGroup(const int32_t userId, std::string &groupId);
135 
136     /**
137      * @tc.name: HiChainConnector::DeleteGroup
138      * @tc.desc: DeleteGroup of the HiChain Connector
139      * @tc.type: FUNC
140      */
141     int32_t DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType);
142 
143     /**
144      * @tc.name: HiChainConnector::IsDevicesInP2PGroup
145      * @tc.desc: IsDevicesInP2PGroup of the HiChain Connector
146      * @tc.type: FUNC
147      */
148     bool IsDevicesInP2PGroup(const std::string &hostDevice, const std::string &peerDevice);
149 
150     /**
151      * @tc.name: HiChainConnector::GetRelatedGroups
152      * @tc.desc: Get Related Groups of the HiChain Connector
153      * @tc.type: FUNC
154      */
155     int32_t GetRelatedGroups(const std::string &deviceId, std::vector<GroupInfo> &groupList);
156 
157     /**
158      * @tc.name: HiChainConnector::GetGroupInfo
159      * @tc.desc: Get GroupInfo of the HiChain Connector
160      * @tc.type: FUNC
161      */
162     bool GetGroupInfo(const std::string &queryParams, std::vector<GroupInfo> &groupList);
163 
164     /**
165      * @tc.name: HiChainConnector::GetGroupInfo
166      * @tc.desc: Get GroupInfo of the HiChain Connector
167      * @tc.type: FUNC
168      */
169     int32_t GetGroupInfo(const int32_t userId, const std::string &queryParams, std::vector<GroupInfo> &groupList);
170 
171     /**
172      * @tc.name: HiChainConnector::GetGroupType
173      * @tc.desc: Get GroupType of the HiChain Connector
174      * @tc.type: FUNC
175      */
176     DmAuthForm GetGroupType(const std::string &deviceId);
177 
178     /**
179      * @tc.name: HiChainConnector::DeleteTimeOutGroup
180      * @tc.desc: Delete TimeOut Group of the HiChain Connector
181      * @tc.type: FUNC
182      */
183     int32_t DeleteTimeOutGroup(const char* deviceId);
184 
185     /**
186      * @tc.name: HiChainConnector::RegisterHiChainCallback
187      * @tc.desc: Register HiChain Callback of the HiChain Connector
188      * @tc.type: FUNC
189      */
190     int32_t RegisterHiChainGroupCallback(const std::shared_ptr<IDmGroupResCallback> &callback);
191 
192     /**
193      * @tc.name: HiChainConnector::UnRegisterHiChainCallback
194      * @tc.desc: Un Register HiChain Callback of the HiChain Connector
195      * @tc.type: FUNC
196      */
197     int32_t UnRegisterHiChainGroupCallback();
198 
199     /**
200      * @tc.name: HiChainConnector::getRegisterInfo
201      * @tc.desc: Get RegisterInfo Info of the HiChain Connector
202      * @tc.type: FUNC
203      */
204     int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr);
205 
206     /**
207      * @tc.name: HiChainConnector::addMultiMembers
208      * @tc.desc: Get RegisterInfo Info of the HiChain Connector
209      * @tc.type: FUNC
210      */
211     int32_t addMultiMembers(const int32_t groupType, const std::string &userId,
212                             const nlohmann::json &jsonDeviceList);
213     /**
214      * @tc.name: HiChainConnector::deleteMultiMembers
215      * @tc.desc: Get RegisterInfo Info of the HiChain Connector
216      * @tc.type: FUNC
217      */
218     int32_t deleteMultiMembers(const int32_t groupType, const std::string &userId,
219                             const nlohmann::json &jsonDeviceList);
220 
221     /**
222      * @tc.name: HiChainConnector::GetTrustedDevices
223      * @tc.desc: Get TrustDevicesUdid Info of the HiChain Connector
224      * @tc.type: FUNC
225      */
226     std::vector<std::string> GetTrustedDevices(const std::string &localDeviceUdid);
227 
228     int32_t addMultiMembersExt(const std::string &credentialInfo);
229 
230     void DeleteAllGroup(int32_t userId);
231 
232     void DeleteP2PGroup(int32_t userId);
233 
234     int32_t GetRelatedGroupsExt(const std::string &deviceId, std::vector<GroupInfo> &groupList);
235 
236     int32_t DeleteGroupExt(std::string &groupId);
237 
238     int32_t GetRelatedGroupsCommon(const std::string &deviceId, const char* pkgName,
239         std::vector<GroupInfo> &groupList);
240     void DeleteAllGroupByUdid(const std::string &udid);
241 private:
242     int64_t GenRequestId();
243     int32_t SyncGroups(std::string deviceId, std::vector<std::string> &remoteGroupIdList);
244     int32_t GetSyncGroupList(std::vector<GroupInfo> &groupList, std::vector<std::string> &syncGroupList);
245     std::string GetConnectPara(std::string deviceId, std::string reqDeviceId);
246     bool IsGroupCreated(std::string groupName, GroupInfo &groupInfo);
247     bool IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector<GroupInfo> &groupList);
248     void DealRedundanceGroup(const std::string &userId, int32_t authType);
249     void DeleteRedundanceGroup(std::string &userId);
250     bool IsGroupInfoInvalid(GroupInfo &group);
251     int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type);
252     int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type);
253     int32_t GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId);
254     int32_t ParseRemoteCredential(const int32_t groupType, const std::string &userId,
255     const nlohmann::json &jsonDeviceList, std::string &params, int32_t &osAccountUserId);
256     int32_t GetTrustedDevicesUdid(const char* jsonStr, std::vector<std::string> &udidList);
257     int32_t GetGroupIdExt(const std::string &userId, const int32_t groupType,
258         std::string &groupId, std::string &groupOwner);
259     int32_t ParseRemoteCredentialExt(const std::string &credentialInfo, std::string &params, std::string &groupOwner);
260     int32_t GetJsonInt(const nlohmann::json &jsonObj, const std::string &key);
261     std::string GetJsonStr(const nlohmann::json &jsonObj, const std::string &key);
262 
263 private:
264     const DeviceGroupManager *deviceGroupManager_ = nullptr;
265     DeviceAuthCallback deviceAuthCallback_;
266     static std::shared_ptr<IHiChainConnectorCallback> hiChainConnectorCallback_;
267     static std::shared_ptr<IDmGroupResCallback> hiChainResCallback_;
268     static int32_t networkStyle_;
269 };
270 } // namespace DistributedHardware
271 } // namespace OHOS
272 #endif // OHOS_HICHAIN_CONNECTOR_H
273