1 /* 2 * Copyright (c) 2025 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_DISTRIBUTED_EXTENSION_TYPES_H 17 #define OHOS_DISTRIBUTED_EXTENSION_TYPES_H 18 19 #include <string> 20 21 #include "iremote_object.h" 22 #include "parcel.h" 23 24 namespace OHOS { 25 namespace DistributedSchedule { 26 struct DExtSourceInfo : public Parcelable { 27 std::string deviceId; 28 std::string networkId; 29 std::string bundleName; 30 std::string moduleName; 31 std::string abilityName; 32 33 DExtSourceInfo() = default; DExtSourceInfoDExtSourceInfo34 DExtSourceInfo(std::string deviceId, std::string networkId, std::string bundleName, std::string moduleName, 35 std::string abilityName) 36 : deviceId(deviceId), networkId(networkId), bundleName(bundleName), moduleName(moduleName), 37 abilityName(abilityName) {} 38 ReadFromParcelDExtSourceInfo39 bool ReadFromParcel(Parcel &parcel) 40 { 41 deviceId = parcel.ReadString(); 42 networkId = parcel.ReadString(); 43 bundleName = parcel.ReadString(); 44 moduleName = parcel.ReadString(); 45 abilityName = parcel.ReadString(); 46 return true; 47 } 48 MarshallingDExtSourceInfo49 virtual bool Marshalling(Parcel &parcel) const override 50 { 51 if (!parcel.WriteString(deviceId)) { 52 return false; 53 } 54 if (!parcel.WriteString(networkId)) { 55 return false; 56 } 57 if (!parcel.WriteString(bundleName)) { 58 return false; 59 } 60 if (!parcel.WriteString(moduleName)) { 61 return false; 62 } 63 if (!parcel.WriteString(abilityName)) { 64 return false; 65 } 66 return true; 67 } 68 UnmarshallingDExtSourceInfo69 static DExtSourceInfo *Unmarshalling(Parcel &parcel) 70 { 71 DExtSourceInfo *info = new (std::nothrow) DExtSourceInfo(); 72 if (info == nullptr) { 73 return nullptr; 74 } 75 76 if (!info->ReadFromParcel(parcel)) { 77 delete info; 78 info = nullptr; 79 } 80 return info; 81 } 82 }; 83 84 struct DExtSinkInfo : public Parcelable { 85 int32_t userId = 100; 86 int32_t pid = -1; 87 std::string bundleName; 88 std::string moduleName; 89 std::string abilityName; 90 std::string serviceName; 91 92 DExtSinkInfo() = default; DExtSinkInfoDExtSinkInfo93 DExtSinkInfo(int32_t userId, int32_t pid, std::string bundleName, std::string moduleName, 94 std::string abilityName, std::string serviceName) 95 : userId(userId), pid(pid), bundleName(bundleName), moduleName(moduleName), 96 abilityName(abilityName), serviceName(serviceName) {} 97 IsEmptyDExtSinkInfo98 bool IsEmpty() const 99 { 100 return bundleName.empty() && moduleName.empty() && abilityName.empty(); 101 } ReadFromParcelDExtSinkInfo102 bool ReadFromParcel(Parcel &parcel) 103 { 104 pid = parcel.ReadInt32(); 105 userId = parcel.ReadInt32(); 106 bundleName = parcel.ReadString(); 107 moduleName = parcel.ReadString(); 108 abilityName = parcel.ReadString(); 109 serviceName = parcel.ReadString(); 110 return true; 111 } 112 MarshallingDExtSinkInfo113 virtual bool Marshalling(Parcel &parcel) const override 114 { 115 if (!parcel.WriteInt32(pid)) { 116 return false; 117 } 118 if (!parcel.WriteInt32(userId)) { 119 return false; 120 } 121 if (!parcel.WriteString(bundleName)) { 122 return false; 123 } 124 if (!parcel.WriteString(moduleName)) { 125 return false; 126 } 127 if (!parcel.WriteString(abilityName)) { 128 return false; 129 } 130 if (!parcel.WriteString(serviceName)) { 131 return false; 132 } 133 return true; 134 } 135 UnmarshallingDExtSinkInfo136 static DExtSinkInfo *Unmarshalling(Parcel &parcel) 137 { 138 DExtSinkInfo *info = new (std::nothrow) DExtSinkInfo(); 139 if (info == nullptr) { 140 return nullptr; 141 } 142 143 if (!info->ReadFromParcel(parcel)) { 144 delete info; 145 info = nullptr; 146 } 147 return info; 148 } 149 }; 150 151 enum class DExtConnectResult : int32_t { 152 SUCCESS = 0, 153 FAILED = 1, 154 PERMISSION_DENIED = 2, 155 TIMEOUT = 3, 156 }; 157 158 struct DExtConnectInfo : public Parcelable { 159 DExtSourceInfo sourceInfo; 160 DExtSinkInfo sinkInfo; 161 std::string tokenId; 162 std::string delegatee; 163 164 DExtConnectInfo() = default; DExtConnectInfoDExtConnectInfo165 DExtConnectInfo(DExtSourceInfo sourceInfo, DExtSinkInfo sinkInfo, std::string tokenId, std::string delegatee) 166 : sourceInfo(sourceInfo), sinkInfo(sinkInfo), tokenId(tokenId), delegatee(delegatee) {} 167 ReadFromParcelDExtConnectInfo168 bool ReadFromParcel(Parcel &parcel) 169 { 170 if (!sourceInfo.ReadFromParcel(parcel)) { 171 return false; 172 } 173 if (!sinkInfo.ReadFromParcel(parcel)) { 174 return false; 175 } 176 tokenId = parcel.ReadString(); 177 delegatee = parcel.ReadString(); 178 return true; 179 } 180 MarshallingDExtConnectInfo181 virtual bool Marshalling(Parcel &parcel) const override 182 { 183 if (!sourceInfo.Marshalling(parcel)) { 184 return false; 185 } 186 if (!sinkInfo.Marshalling(parcel)) { 187 return false; 188 } 189 if (!parcel.WriteString(tokenId)) { 190 return false; 191 } 192 if (!parcel.WriteString(delegatee)) { 193 return false; 194 } 195 return true; 196 } 197 UnmarshallingDExtConnectInfo198 static DExtConnectInfo *Unmarshalling(Parcel &parcel) 199 { 200 DExtConnectInfo *info = new (std::nothrow) DExtConnectInfo(); 201 if (info == nullptr) { 202 return nullptr; 203 } 204 205 if (!info->ReadFromParcel(parcel)) { 206 delete info; 207 info = nullptr; 208 } 209 return info; 210 } 211 }; 212 213 struct DExtConnectResultInfo : public Parcelable { 214 DExtConnectInfo connectInfo; 215 DExtConnectResult result { OHOS::DistributedSchedule::DExtConnectResult::FAILED }; 216 int32_t errCode { 0 }; 217 218 DExtConnectResultInfo() = default; DExtConnectResultInfoDExtConnectResultInfo219 DExtConnectResultInfo(DExtConnectInfo connectInfo, DExtConnectResult result, int32_t errCode) 220 : connectInfo(connectInfo), result(result), errCode(errCode) {} 221 ReadFromParcelDExtConnectResultInfo222 bool ReadFromParcel(Parcel &parcel) 223 { 224 if (!connectInfo.ReadFromParcel(parcel)) { 225 return false; 226 } 227 result = static_cast<DExtConnectResult>(parcel.ReadInt32()); 228 errCode = parcel.ReadInt32(); 229 return true; 230 } 231 MarshallingDExtConnectResultInfo232 virtual bool Marshalling(Parcel &parcel) const override 233 { 234 if (!connectInfo.Marshalling(parcel)) { 235 return false; 236 } 237 if (!parcel.WriteInt32(static_cast<int32_t>(result))) { 238 return false; 239 } 240 if (!parcel.WriteInt32(errCode)) { 241 return false; 242 } 243 return true; 244 } 245 UnmarshallingDExtConnectResultInfo246 static DExtConnectResultInfo *Unmarshalling(Parcel &parcel) 247 { 248 DExtConnectResultInfo *info = new (std::nothrow) DExtConnectResultInfo(); 249 if (info == nullptr) { 250 return nullptr; 251 } 252 253 if (!info->ReadFromParcel(parcel)) { 254 delete info; 255 info = nullptr; 256 } 257 return info; 258 } 259 }; 260 } // namespace DistributedSchedule 261 } // namespace OHOS 262 #endif // OHOS_DISTRIBUTED_EXTENSION_TYPES_H