1 /* 2 * Copyright (c) 2022 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 "single_instance.h" 33 34 namespace OHOS { 35 namespace DistributedHardware { 36 struct GroupInfo { 37 std::string groupName; 38 std::string groupId; 39 std::string groupOwner; 40 int32_t groupType; 41 int32_t groupVisibility; 42 std::string userId; 43 GroupInfoGroupInfo44 GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId("") 45 { 46 } 47 }; 48 49 enum class AuthFormPriority { 50 PRIORITY_PEER_TO_PEER = 0, 51 PRIORITY_ACROSS_ACCOUNT = 1, 52 PRIORITY_IDENTICAL_ACCOUNT = 2, 53 }; 54 55 static const std::unordered_map<int32_t, AuthFormPriority> g_authFormPriorityMap = { 56 {GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP, AuthFormPriority::PRIORITY_IDENTICAL_ACCOUNT}, 57 {GROUP_TYPE_ACROSS_ACCOUNT_GROUP, AuthFormPriority::PRIORITY_ACROSS_ACCOUNT}, 58 {GROUP_TYPE_PEER_TO_PEER_GROUP, AuthFormPriority::PRIORITY_PEER_TO_PEER} 59 }; 60 61 void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo); 62 63 class HiChainConnector { 64 public: 65 static bool onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); 66 static void onFinish(int64_t requestId, int operationCode, const char *returnData); 67 static void onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn); 68 static char *onRequest(int64_t requestId, int operationCode, const char *reqParams); 69 70 public: 71 HiChainConnector(); 72 ~HiChainConnector(); 73 74 /** 75 * @tc.name: HiChainConnector::RegisterHiChainCallback 76 * @tc.desc: Register HiChain Callback of the HiChain Connector 77 * @tc.type: FUNC 78 */ 79 int32_t RegisterHiChainCallback(std::shared_ptr<IHiChainConnectorCallback> callback); 80 81 /** 82 * @tc.name: HiChainConnector::UnRegisterHiChainCallback 83 * @tc.desc: Un Register HiChain Callback of the HiChain Connector 84 * @tc.type: FUNC 85 */ 86 int32_t UnRegisterHiChainCallback(); 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, const std::string &groupName); 94 95 /** 96 * @tc.name: HiChainConnector::CreateGroup 97 * @tc.desc: Create Group of the HiChain Connector 98 * @tc.type: FUNC 99 */ 100 int32_t CreateGroup(int64_t requestId, int32_t authType, const std::string &userId, 101 nlohmann::json &jsonOutObj); 102 103 /** 104 * @tc.name: HiChainConnector::AddMember 105 * @tc.desc: Add Member of the HiChain Connector 106 * @tc.type: FUNC 107 */ 108 int32_t AddMember(const std::string &deviceId, const std::string &connectInfo); 109 110 /** 111 * @tc.name: HiChainConnector::DelMemberFromGroup 112 * @tc.desc: Delete Member From Group of the HiChain Connector 113 * @tc.type: FUNC 114 */ 115 int32_t DelMemberFromGroup(const std::string &groupId, const std::string &deviceId); 116 117 /** 118 * @tc.name: HiChainConnector::DeleteGroup 119 * @tc.desc: Delete Group of the HiChain Connector 120 * @tc.type: FUNC 121 */ 122 int32_t DeleteGroup(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(const int32_t userId, std::string &groupId); 130 131 /** 132 * @tc.name: HiChainConnector::DeleteGroup 133 * @tc.desc: DeleteGroup of the HiChain Connector 134 * @tc.type: FUNC 135 */ 136 int32_t DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType); 137 138 /** 139 * @tc.name: HiChainConnector::IsDevicesInGroup 140 * @tc.desc: IsDevicesInGroup of the HiChain Connector 141 * @tc.type: FUNC 142 */ 143 bool IsDevicesInGroup(const std::string &hostDevice, const std::string &peerDevice); 144 145 /** 146 * @tc.name: HiChainConnector::GetRelatedGroups 147 * @tc.desc: Get Related Groups of the HiChain Connector 148 * @tc.type: FUNC 149 */ 150 int32_t GetRelatedGroups(const std::string &deviceId, 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 std::string &queryParams, std::vector<GroupInfo> &groupList); 158 159 /** 160 * @tc.name: HiChainConnector::GetGroupInfo 161 * @tc.desc: Get GroupInfo of the HiChain Connector 162 * @tc.type: FUNC 163 */ 164 int32_t GetGroupInfo(const int32_t userId, const std::string &queryParams, std::vector<GroupInfo> &groupList); 165 166 /** 167 * @tc.name: HiChainConnector::GetGroupType 168 * @tc.desc: Get GroupType of the HiChain Connector 169 * @tc.type: FUNC 170 */ 171 DmAuthForm GetGroupType(const std::string &deviceId); 172 173 /** 174 * @tc.name: HiChainConnector::DeleteTimeOutGroup 175 * @tc.desc: Delete TimeOut Group of the HiChain Connector 176 * @tc.type: FUNC 177 */ 178 int32_t DeleteTimeOutGroup(const char* deviceId); 179 180 /** 181 * @tc.name: HiChainConnector::RegisterHiChainCallback 182 * @tc.desc: Register HiChain Callback of the HiChain Connector 183 * @tc.type: FUNC 184 */ 185 int32_t RegisterHiChainGroupCallback(const std::shared_ptr<IDmGroupResCallback> &callback); 186 187 /** 188 * @tc.name: HiChainConnector::UnRegisterHiChainCallback 189 * @tc.desc: Un Register HiChain Callback of the HiChain Connector 190 * @tc.type: FUNC 191 */ 192 int32_t UnRegisterHiChainGroupCallback(); 193 194 /** 195 * @tc.name: HiChainConnector::getRegisterInfo 196 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 197 * @tc.type: FUNC 198 */ 199 int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr); 200 201 /** 202 * @tc.name: HiChainConnector::addMultiMembers 203 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 204 * @tc.type: FUNC 205 */ 206 int32_t addMultiMembers(const int32_t groupType, const std::string &userId, 207 const nlohmann::json &jsonDeviceList); 208 /** 209 * @tc.name: HiChainConnector::deleteMultiMembers 210 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 211 * @tc.type: FUNC 212 */ 213 int32_t deleteMultiMembers(const int32_t groupType, const std::string &userId, 214 const nlohmann::json &jsonDeviceList); 215 216 /** 217 * @tc.name: HiChainConnector::GetTrustedDevices 218 * @tc.desc: Get TrustDevicesUdid Info of the HiChain Connector 219 * @tc.type: FUNC 220 */ 221 std::vector<std::string> GetTrustedDevices(const std::string &localDeviceUdid); 222 private: 223 int64_t GenRequestId(); 224 int32_t SyncGroups(std::string deviceId, std::vector<std::string> &remoteGroupIdList); 225 int32_t GetSyncGroupList(std::vector<GroupInfo> &groupList, std::vector<std::string> &syncGroupList); 226 std::string GetConnectPara(std::string deviceId, std::string reqDeviceId); 227 bool IsGroupCreated(std::string groupName, GroupInfo &groupInfo); 228 bool IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector<GroupInfo> &groupList); 229 void DealRedundanceGroup(const std::string &userId, int32_t authType); 230 void DeleteRedundanceGroup(std::string &userId); 231 bool IsGroupInfoInvalid(GroupInfo &group); 232 int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type); 233 int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type); 234 int32_t GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId); 235 int32_t ParseRemoteCredential(const int32_t groupType, const std::string &userId, 236 const nlohmann::json &jsonDeviceList, std::string ¶ms, int32_t &osAccountUserId); 237 int32_t GetTrustedDevicesUdid(const char* jsonStr, std::vector<std::string> &udidList); 238 private: 239 const DeviceGroupManager *deviceGroupManager_ = nullptr; 240 DeviceAuthCallback deviceAuthCallback_; 241 static std::shared_ptr<IHiChainConnectorCallback> hiChainConnectorCallback_; 242 static std::shared_ptr<IDmGroupResCallback> hiChainResCallback_; 243 static int32_t networkStyle_; 244 }; 245 } // namespace DistributedHardware 246 } // namespace OHOS 247 #endif // OHOS_HICHAIN_CONNECTOR_H 248