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_stub.h"
17
18 #include <iosfwd>
19 #include <string>
20 #include <utility>
21
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 #include "distributed_device_profile_constants.h"
25 #include "ipc_object_stub.h"
26 #include "message_parcel.h"
27 #include "macro_utils.h"
28
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 namespace {
32 const std::string TAG = "SyncCompletedCallbackStub";
33 }
34
SyncCompletedCallbackStub()35 SyncCompletedCallbackStub::SyncCompletedCallbackStub()
36 {
37 HILOGI("constructor!");
38 }
39
~SyncCompletedCallbackStub()40 SyncCompletedCallbackStub::~SyncCompletedCallbackStub()
41 {
42 HILOGI("destructor!");
43 }
44
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)45 int32_t SyncCompletedCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
46 MessageParcel& reply, MessageOption& option)
47 {
48 HILOGI("Code = %{public}u", code);
49 std::u16string descriptor = SyncCompletedCallbackStub::GetDescriptor();
50 std::u16string remoteDescriptor = data.ReadInterfaceToken();
51 if (descriptor != remoteDescriptor) {
52 HILOGE("Check descriptor failed");
53 return DP_INTERFACE_CHECK_FAILED;
54 }
55
56 if (code != static_cast<uint32_t>(DPInterfaceCode::ON_SYNC_COMPLETED)) {
57 HILOGW("Unknown request code, code = %{public}u", code);
58 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59 }
60 return OnSyncCompletedInner(data, reply);
61 }
62
OnSyncCompletedInner(MessageParcel & data,MessageParcel & reply)63 int32_t SyncCompletedCallbackStub::OnSyncCompletedInner(MessageParcel& data, MessageParcel& reply)
64 {
65 int32_t size = data.ReadInt32();
66 if (size <= 0 || size > MAX_SYNC_RESULTS_SIZE) {
67 HILOGW("Size %{public}d", size);
68 return DP_INVALID_PARAMS;
69 }
70
71 SyncResult syncResults;
72 for (int32_t i = 0; i < size; i++) {
73 syncResults.emplace(data.ReadString(), static_cast<SyncStatus>(data.ReadInt32()));
74 }
75 // send code handle cache
76 OnSyncCompleted(syncResults);
77 return DP_SUCCESS;
78 }
79 } // namespace DistributedDeviceProfile
80 } // namespace OHOS
81
82