1 /*
2  * Copyright (c) 2023-2024 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 "cJSON.h"
18 #include "macro_utils.h"
19 #include "profile_utils.h"
20 
21 namespace OHOS {
22 namespace DistributedDeviceProfile {
23 namespace {
24     const std::string TAG = "DpSyncOptions";
25 }
26 
GetDeviceList() const27 std::vector<std::string> DpSyncOptions::GetDeviceList() const
28 {
29     return syncDeviceIds_;
30 }
31 
AddDevice(const std::string & deviceId)32 void DpSyncOptions::AddDevice(const std::string& deviceId)
33 {
34     syncDeviceIds_.emplace_back(deviceId);
35 }
36 
GetSyncMode() const37 SyncMode DpSyncOptions::GetSyncMode() const
38 {
39     return syncMode_;
40 }
41 
SetSyncMode(SyncMode mode)42 void DpSyncOptions::SetSyncMode(SyncMode mode)
43 {
44     syncMode_ = mode;
45 }
46 
Marshalling(MessageParcel & parcel) const47 bool DpSyncOptions::Marshalling(MessageParcel& parcel) const
48 {
49     WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncMode_), false);
50     WRITE_HELPER_RET(parcel, Int32, static_cast<int32_t>(syncDeviceIds_.size()), false);
51     for (const auto& deviceId : syncDeviceIds_) {
52         WRITE_HELPER_RET(parcel, String, deviceId, false);
53     }
54     return true;
55 }
56 
UnMarshalling(MessageParcel & parcel)57 bool DpSyncOptions::UnMarshalling(MessageParcel& parcel)
58 {
59     int32_t mode = 0;
60     READ_HELPER_RET(parcel, Int32, mode, false);
61     syncMode_ = static_cast<SyncMode>(mode);
62     int32_t size = 0;
63     READ_HELPER_RET(parcel, Int32, size, false);
64     if (size > MAX_DEVICE_SIZE) {
65         return false;
66     }
67     for (int32_t i = 0; i < size; i++) {
68         std::string deviceId;
69         READ_HELPER_RET(parcel, String, deviceId, false);
70         syncDeviceIds_.emplace_back(deviceId);
71     }
72     return true;
73 }
74 
dump() const75 std::string DpSyncOptions::dump() const
76 {
77     cJSON* json = cJSON_CreateObject();
78     if (!cJSON_IsObject(json)) {
79         cJSON_Delete(json);
80         return EMPTY_STRING;
81     }
82     cJSON_AddNumberToObject(json, SYNC_MODE.c_str(), static_cast<int64_t>(syncMode_));
83     cJSON* jsonArr = cJSON_CreateArray();
84     if (!cJSON_IsArray(jsonArr)) {
85         cJSON_Delete(jsonArr);
86     } else {
87         for (const auto& deviceId : syncDeviceIds_) {
88             cJSON* jsonArrItem = cJSON_CreateString(deviceId.c_str());
89             if (jsonArrItem == NULL) {
90                 continue;
91             }
92             if (!cJSON_AddItemToArray(jsonArr, jsonArrItem)) {
93                 cJSON_Delete(jsonArrItem);
94                 continue;
95             }
96         }
97         if (!cJSON_AddItemToObject(json, SYNC_DEVICE_IDS.c_str(), jsonArr)) {
98             cJSON_Delete(jsonArr);
99         }
100     }
101     char* jsonChars = cJSON_PrintUnformatted(json);
102     if (jsonChars == NULL) {
103         cJSON_Delete(json);
104         HILOGE("cJSON formatted to string failed!");
105         return EMPTY_STRING;
106     }
107     std::string jsonStr = jsonChars;
108     cJSON_Delete(json);
109     cJSON_free(jsonChars);
110     return jsonStr;
111 }
112 } // namespace DistributedDeviceProfile
113 } // namespace OHOS