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 GroupInfoOHOS::DistributedHardware::GroupInfo49 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 bool GetGroupInfo(const int32_t userId, const std::string &queryParams, std::vector<GroupInfo> &groupList); 170 171 bool GetGroupInfoExt(const int32_t userId, const std::string &queryParams, std::vector<GroupInfo> &groupList); 172 173 bool GetGroupInfoCommon(const int32_t userId, const std::string &queryParams, const char* pkgName, 174 std::vector<GroupInfo> &groupList); 175 /** 176 * @tc.name: HiChainConnector::GetGroupType 177 * @tc.desc: Get GroupType of the HiChain Connector 178 * @tc.type: FUNC 179 */ 180 DmAuthForm GetGroupType(const std::string &deviceId); 181 182 /** 183 * @tc.name: HiChainConnector::DeleteTimeOutGroup 184 * @tc.desc: Delete TimeOut Group of the HiChain Connector 185 * @tc.type: FUNC 186 */ 187 int32_t DeleteTimeOutGroup(const char* deviceId); 188 189 /** 190 * @tc.name: HiChainConnector::RegisterHiChainCallback 191 * @tc.desc: Register HiChain Callback of the HiChain Connector 192 * @tc.type: FUNC 193 */ 194 int32_t RegisterHiChainGroupCallback(const std::shared_ptr<IDmGroupResCallback> &callback); 195 196 /** 197 * @tc.name: HiChainConnector::UnRegisterHiChainCallback 198 * @tc.desc: Un Register HiChain Callback of the HiChain Connector 199 * @tc.type: FUNC 200 */ 201 int32_t UnRegisterHiChainGroupCallback(); 202 203 /** 204 * @tc.name: HiChainConnector::getRegisterInfo 205 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 206 * @tc.type: FUNC 207 */ 208 int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr); 209 210 /** 211 * @tc.name: HiChainConnector::addMultiMembers 212 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 213 * @tc.type: FUNC 214 */ 215 int32_t addMultiMembers(const int32_t groupType, const std::string &userId, 216 const nlohmann::json &jsonDeviceList); 217 /** 218 * @tc.name: HiChainConnector::deleteMultiMembers 219 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 220 * @tc.type: FUNC 221 */ 222 int32_t deleteMultiMembers(const int32_t groupType, const std::string &userId, 223 const nlohmann::json &jsonDeviceList); 224 225 /** 226 * @tc.name: HiChainConnector::GetTrustedDevices 227 * @tc.desc: Get TrustDevicesUdid Info of the HiChain Connector 228 * @tc.type: FUNC 229 */ 230 std::vector<std::string> GetTrustedDevices(const std::string &localDeviceUdid); 231 232 int32_t addMultiMembersExt(const std::string &credentialInfo); 233 234 void DeleteAllGroup(int32_t userId); 235 236 int32_t GetRelatedGroupsExt(const std::string &deviceId, std::vector<GroupInfo> &groupList); 237 238 int32_t DeleteGroupExt(std::string &groupId); 239 240 int32_t GetRelatedGroupsCommon(const std::string &deviceId, const char* pkgName, 241 std::vector<GroupInfo> &groupList); 242 void DeleteAllGroupByUdid(const std::string &udid); 243 void DeleteP2PGroup(int32_t switchUserId); 244 int32_t DeleteGroupByACL(std::vector<std::pair<int32_t, std::string>> &delACLInfoVec, 245 std::vector<int32_t> &userIdVec); 246 bool IsNeedDelete(std::string &groupName, int32_t userId, 247 std::vector<std::pair<int32_t, std::string>> &delACLInfoVec); 248 249 private: 250 int64_t GenRequestId(); 251 int32_t SyncGroups(std::string deviceId, std::vector<std::string> &remoteGroupIdList); 252 int32_t GetSyncGroupList(std::vector<GroupInfo> &groupList, std::vector<std::string> &syncGroupList); 253 std::string GetConnectPara(std::string deviceId, std::string reqDeviceId); 254 bool IsGroupCreated(std::string groupName, GroupInfo &groupInfo); 255 bool IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector<GroupInfo> &groupList); 256 void DealRedundanceGroup(const std::string &userId, int32_t authType); 257 void DeleteRedundanceGroup(std::string &userId); 258 bool IsGroupInfoInvalid(GroupInfo &group); 259 int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type); 260 int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type); 261 int32_t GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId); 262 int32_t ParseRemoteCredential(const int32_t groupType, const std::string &userId, 263 const nlohmann::json &jsonDeviceList, std::string ¶ms, int32_t &osAccountUserId); 264 int32_t GetTrustedDevicesUdid(const char* jsonStr, std::vector<std::string> &udidList); 265 int32_t GetGroupIdExt(const std::string &userId, const int32_t groupType, 266 std::string &groupId, std::string &groupOwner); 267 int32_t ParseRemoteCredentialExt(const std::string &credentialInfo, std::string ¶ms, std::string &groupOwner); 268 int32_t GetJsonInt(const nlohmann::json &jsonObj, const std::string &key); 269 std::string GetJsonStr(const nlohmann::json &jsonObj, const std::string &key); 270 271 private: 272 const DeviceGroupManager *deviceGroupManager_ = nullptr; 273 DeviceAuthCallback deviceAuthCallback_; 274 static std::shared_ptr<IHiChainConnectorCallback> hiChainConnectorCallback_; 275 static std::shared_ptr<IDmGroupResCallback> hiChainResCallback_; 276 static int32_t networkStyle_; 277 }; 278 } // namespace DistributedHardware 279 } // namespace OHOS 280 #endif // OHOS_HICHAIN_CONNECTOR_H 281