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 "sync_completed_callback_proxy.h"
17 #include "distributed_device_profile_log.h"
18 #include "macro_utils.h"
19 #include "message_parcel.h"
20 
21 namespace OHOS {
22 namespace DistributedDeviceProfile {
23 namespace {
24     const std::string TAG = "SyncCompletedCallbackProxy";
25 }
SyncCompletedCallbackProxy(const sptr<IRemoteObject> & impl)26 SyncCompletedCallbackProxy::SyncCompletedCallbackProxy(const sptr <IRemoteObject> &impl)
27     : IRemoteProxy<ISyncCompletedCallback>(impl)
28 {
29     HILOGI("constructor!");
30 }
31 
OnSyncCompleted(const SyncResult & syncResults)32 void SyncCompletedCallbackProxy::OnSyncCompleted(const SyncResult &syncResults)
33 {
34     HILOGI("called");
35     MessageParcel data;
36     MessageParcel reply;
37     if (!data.WriteInterfaceToken(SyncCompletedCallbackProxy::GetDescriptor())) {
38         HILOGE("write descriptor failed");
39         return;
40     }
41     if (!data.WriteInt32(static_cast<int32_t>(syncResults.size()))) {
42         HILOGE("write syncResults size failed");
43         return;
44     }
45 
46     for (const auto&[deviceId, syncResult]: syncResults) {
47         if (!data.WriteString(deviceId) ||
48             !data.WriteInt32(static_cast<int32_t>(syncResult))) {
49             HILOGE("write syncResults failed");
50             return;
51         }
52     }
53     MessageOption option{MessageOption::TF_ASYNC};
54     int32_t errCode = Remote()->SendRequest(static_cast<uint32_t>(DPInterfaceCode::ON_SYNC_COMPLETED), data, reply,
55         option);
56     if (errCode != ERR_OK) {
57         HILOGE("send request failed, errCode = %{public}d", errCode);
58     }
59 }
60 } // namespace DistributedDeviceProfile
61 } // namespace OHOS