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 <vector> 24 25 #include "device_auth.h" 26 #include "hichain_connector_callback.h" 27 #include "nlohmann/json.hpp" 28 #include "single_instance.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 struct GroupInfo { 33 std::string groupName; 34 std::string groupId; 35 std::string groupOwner; 36 int32_t groupType; 37 int32_t groupVisibility; 38 std::string userId; 39 GroupInfoGroupInfo40 GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId("") 41 { 42 } 43 }; 44 45 void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo); 46 47 class HiChainConnector { 48 public: 49 static bool onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); 50 static void onFinish(int64_t requestId, int operationCode, const char *returnData); 51 static void onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn); 52 static char *onRequest(int64_t requestId, int operationCode, const char *reqParams); 53 54 public: 55 HiChainConnector(); 56 ~HiChainConnector(); 57 58 /** 59 * @tc.name: HiChainConnector::RegisterHiChainCallback 60 * @tc.desc: Register HiChain Callback of the HiChain Connector 61 * @tc.type: FUNC 62 */ 63 int32_t RegisterHiChainCallback(std::shared_ptr<IHiChainConnectorCallback> callback); 64 65 /** 66 * @tc.name: HiChainConnector::UnRegisterHiChainCallback 67 * @tc.desc: Un Register HiChain Callback of the HiChain Connector 68 * @tc.type: FUNC 69 */ 70 int32_t UnRegisterHiChainCallback(); 71 72 /** 73 * @tc.name: HiChainConnector::CreateGroup 74 * @tc.desc: Create Group of the HiChain Connector 75 * @tc.type: FUNC 76 */ 77 int32_t CreateGroup(int64_t requestId, const std::string &groupName); 78 79 /** 80 * @tc.name: HiChainConnector::CreateGroup 81 * @tc.desc: Create Group of the HiChain Connector 82 * @tc.type: FUNC 83 */ 84 int32_t CreateGroup(int64_t requestId, int32_t authType, const std::string &userId, 85 nlohmann::json &jsonOutObj); 86 87 /** 88 * @tc.name: HiChainConnector::AddMember 89 * @tc.desc: Add Member of the HiChain Connector 90 * @tc.type: FUNC 91 */ 92 int32_t AddMember(const std::string &deviceId, const std::string &connectInfo); 93 94 /** 95 * @tc.name: HiChainConnector::DelMemberFromGroup 96 * @tc.desc: Delete Member From Group of the HiChain Connector 97 * @tc.type: FUNC 98 */ 99 int32_t DelMemberFromGroup(const std::string &groupId, const std::string &deviceId); 100 101 /** 102 * @tc.name: HiChainConnector::DeleteGroup 103 * @tc.desc: Delete Group of the HiChain Connector 104 * @tc.type: FUNC 105 */ 106 int32_t DeleteGroup(std::string &groupId); 107 108 /** 109 * @tc.name: HiChainConnector::DeleteGroup 110 * @tc.desc: DeleteGroup of the HiChain Connector 111 * @tc.type: FUNC 112 */ 113 int32_t DeleteGroup(const int32_t userId, std::string &groupId); 114 115 /** 116 * @tc.name: HiChainConnector::DeleteGroup 117 * @tc.desc: DeleteGroup of the HiChain Connector 118 * @tc.type: FUNC 119 */ 120 int32_t DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType); 121 122 /** 123 * @tc.name: HiChainConnector::IsDevicesInGroup 124 * @tc.desc: IsDevicesInGroup of the HiChain Connector 125 * @tc.type: FUNC 126 */ 127 bool IsDevicesInGroup(const std::string &hostDevice, const std::string &peerDevice); 128 129 /** 130 * @tc.name: HiChainConnector::GetRelatedGroups 131 * @tc.desc: Get Related Groups of the HiChain Connector 132 * @tc.type: FUNC 133 */ 134 int32_t GetRelatedGroups(const std::string &DeviceId, std::vector<GroupInfo> &groupList); 135 136 /** 137 * @tc.name: HiChainConnector::GetGroupInfo 138 * @tc.desc: Get GroupInfo of the HiChain Connector 139 * @tc.type: FUNC 140 */ 141 bool GetGroupInfo(const std::string &queryParams, std::vector<GroupInfo> &groupList); 142 143 /** 144 * @tc.name: HiChainConnector::GetGroupInfo 145 * @tc.desc: Get GroupInfo of the HiChain Connector 146 * @tc.type: FUNC 147 */ 148 int32_t GetGroupInfo(const int32_t userId, const std::string &queryParams, std::vector<GroupInfo> &groupList); 149 150 /** 151 * @tc.name: HiChainConnector::DeleteTimeOutGroup 152 * @tc.desc: Delete TimeOut Group of the HiChain Connector 153 * @tc.type: FUNC 154 */ 155 int32_t DeleteTimeOutGroup(const char* deviceId); 156 157 /** 158 * @tc.name: HiChainConnector::RegisterHiChainCallback 159 * @tc.desc: Register HiChain Callback of the HiChain Connector 160 * @tc.type: FUNC 161 */ 162 int32_t RegisterHiChainGroupCallback(const std::shared_ptr<IDmGroupResCallback> &callback); 163 164 /** 165 * @tc.name: HiChainConnector::UnRegisterHiChainCallback 166 * @tc.desc: Un Register HiChain Callback of the HiChain Connector 167 * @tc.type: FUNC 168 */ 169 int32_t UnRegisterHiChainGroupCallback(); 170 171 /** 172 * @tc.name: HiChainConnector::getRegisterInfo 173 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 174 * @tc.type: FUNC 175 */ 176 int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr); 177 178 /** 179 * @tc.name: HiChainConnector::addMultiMembers 180 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 181 * @tc.type: FUNC 182 */ 183 int32_t addMultiMembers(const int32_t groupType, const std::string &userId, 184 const nlohmann::json &jsonDeviceList); 185 /** 186 * @tc.name: HiChainConnector::deleteMultiMembers 187 * @tc.desc: Get RegisterInfo Info of the HiChain Connector 188 * @tc.type: FUNC 189 */ 190 int32_t deleteMultiMembers(const int32_t groupType, const std::string &userId, 191 const nlohmann::json &jsonDeviceList); 192 private: 193 int64_t GenRequestId(); 194 int32_t SyncGroups(std::string deviceId, std::vector<std::string> &remoteGroupIdList); 195 int32_t GetSyncGroupList(std::vector<GroupInfo> &groupList, std::vector<std::string> &syncGroupList); 196 std::string GetConnectPara(std::string deviceId, std::string reqDeviceId); 197 bool IsGroupCreated(std::string groupName, GroupInfo &groupInfo); 198 bool IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector<GroupInfo> &groupList); 199 void DealRedundanceGroup(const std::string &userId, int32_t authType); 200 void DeleteRedundanceGroup(std::string &userId); 201 bool IsGroupInfoInvalid(GroupInfo &group); 202 int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type); 203 int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type); 204 int32_t GetGroupId(const std::string &userId, const int32_t groupType, std::string &groupId); 205 int32_t ParseRemoteCredential(const int32_t groupType, const std::string &userId, 206 const nlohmann::json &jsonDeviceList, std::string ¶ms, int32_t &osAccountUserId); 207 private: 208 const DeviceGroupManager *deviceGroupManager_ = nullptr; 209 DeviceAuthCallback deviceAuthCallback_; 210 static std::shared_ptr<IHiChainConnectorCallback> hiChainConnectorCallback_; 211 static std::shared_ptr<IDmGroupResCallback> hiChainResCallback_; 212 static int32_t networkStyle_; 213 }; 214 } // namespace DistributedHardware 215 } // namespace OHOS 216 #endif // OHOS_HICHAIN_CONNECTOR_H 217