1 /*
2 * Copyright (c) 2021-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 #include "sync_options.h"
17
18 #include <iosfwd>
19 #include <list>
20 #include <string>
21
22 #include "device_profile_log.h"
23 #include "parcel.h"
24 #include "parcel_helper.h"
25
26 namespace OHOS {
27 namespace DeviceProfile {
28 namespace {
29 const std::string TAG = "SyncOptions";
30 constexpr int32_t MAX_DEVICE_LEN = 1000000;
31 }
32
GetDeviceList() const33 const std::list<std::string>& SyncOptions::GetDeviceList() const
34 {
35 return syncDevIds_;
36 }
37
AddDevice(const std::string & deviceId)38 void SyncOptions::AddDevice(const std::string& deviceId)
39 {
40 syncDevIds_.emplace_back(deviceId);
41 }
42
GetSyncMode() const43 SyncMode SyncOptions::GetSyncMode() const
44 {
45 return syncMode_;
46 }
47
SetSyncMode(SyncMode mode)48 void SyncOptions::SetSyncMode(SyncMode mode)
49 {
50 syncMode_ = mode;
51 }
52
Marshalling(Parcel & parcel) const53 bool SyncOptions::Marshalling(Parcel& parcel) const
54 {
55 PARCEL_WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncMode_), false);
56 PARCEL_WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncDevIds_.size()), false);
57 for (const auto& deviceId : syncDevIds_) {
58 PARCEL_WRITE_HELPER_RET(parcel, String, deviceId, false);
59 }
60 return true;
61 }
62
Unmarshalling(Parcel & parcel)63 bool SyncOptions::Unmarshalling(Parcel& parcel)
64 {
65 int32_t mode = 0;
66 PARCEL_READ_HELPER_RET(parcel, Int32, mode, false);
67 syncMode_ = static_cast<DeviceProfile::SyncMode>(mode);
68 int32_t size = 0;
69 PARCEL_READ_HELPER_RET(parcel, Int32, size, false);
70 if (size > MAX_DEVICE_LEN) {
71 return false;
72 }
73 for (int32_t i = 0; i < size; i++) {
74 std::string deviceId;
75 PARCEL_READ_HELPER_RET(parcel, String, deviceId, false);
76 syncDevIds_.emplace_back(deviceId);
77 }
78 return true;
79 }
80 } // namespace DeviceProfile
81 } // namespace OHOS