• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "dp_sync_options.h"
17 #include "macro_utils.h"
18 #include "distributed_device_profile_constants.h"
19 #include "nlohmann/json.hpp"
20 #include "profile_utils.h"
21 
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 namespace {
25     const std::string TAG = "DpSyncOptions";
26 }
27 
GetDeviceList() const28 std::vector<std::string> DpSyncOptions::GetDeviceList() const
29 {
30     return syncDeviceIds_;
31 }
32 
AddDevice(const std::string & deviceId)33 void DpSyncOptions::AddDevice(const std::string& deviceId)
34 {
35     syncDeviceIds_.emplace_back(deviceId);
36 }
37 
GetSyncMode() const38 SyncMode DpSyncOptions::GetSyncMode() const
39 {
40     return syncMode_;
41 }
42 
SetSyncMode(SyncMode mode)43 void DpSyncOptions::SetSyncMode(SyncMode mode)
44 {
45     syncMode_ = mode;
46 }
47 
Marshalling(MessageParcel & parcel) const48 bool DpSyncOptions::Marshalling(MessageParcel& parcel) const
49 {
50     WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncMode_), false);
51     WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncDeviceIds_.size()), false);
52     for (const auto& deviceId : syncDeviceIds_) {
53         WRITE_HELPER_RET(parcel, String, deviceId, false);
54     }
55     return true;
56 }
57 
UnMarshalling(MessageParcel & parcel)58 bool DpSyncOptions::UnMarshalling(MessageParcel& parcel)
59 {
60     int32_t mode = 0;
61     READ_HELPER_RET(parcel, Int32, mode, false);
62     syncMode_ = static_cast<SyncMode>(mode);
63     int32_t size = 0;
64     READ_HELPER_RET(parcel, Int32, size, false);
65     if (size > MAX_DEVICE_SIZE) {
66         return false;
67     }
68     for (int32_t i = 0; i < size; i++) {
69         std::string deviceId;
70         READ_HELPER_RET(parcel, String, deviceId, false);
71         syncDeviceIds_.emplace_back(deviceId);
72     }
73     return true;
74 }
75 
dump() const76 std::string DpSyncOptions::dump() const
77 {
78     nlohmann::json json;
79     json[SYNC_MODE] = static_cast<int32_t>(syncMode_);
80     std::vector<std::string> syncDeviceList;
81     for (const std::string& deviceId : syncDeviceIds_) {
82         syncDeviceList.push_back(deviceId);
83     }
84     json[SYNC_DEVICE_IDS] = syncDeviceList;
85     return json.dump();
86 }
87 } // namespace DistributedDeviceProfile
88 } // namespace OHOS