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