1 /*
2 * Copyright (c) 2021 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 #define LOG_TAG "KvStoreSyncCallbackProxy"
17
18 #include "ikvstore_sync_callback.h"
19 #include <chrono>
20 #include <ctime>
21 #include <ipc_skeleton.h>
22 #include <map>
23 #include "log_print.h"
24 #include "message_parcel.h"
25 #include "message_option.h"
26 #include "types.h"
27
28 namespace OHOS {
29 namespace DistributedKv {
30 enum {
31 SYNCCOMPLETED,
32 };
33 constexpr int32_t MAX_DEVICES = 4096;
KvStoreSyncCallbackProxy(const sptr<IRemoteObject> & impl)34 KvStoreSyncCallbackProxy::KvStoreSyncCallbackProxy(const sptr<IRemoteObject> &impl)
35 : IRemoteProxy<IKvStoreSyncCallback>(impl)
36 {}
37
SyncCompleted(const std::map<std::string,Status> & results,uint64_t sequenceId)38 void KvStoreSyncCallbackProxy::SyncCompleted(const std::map<std::string, Status> &results, uint64_t sequenceId)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 if (!data.WriteInterfaceToken(KvStoreSyncCallbackProxy::GetDescriptor())) {
43 ZLOGE("write descriptor failed");
44 return;
45 }
46 if (!data.WriteInt32(static_cast<int>(results.size()))) {
47 ZLOGW("write results size error.");
48 return;
49 }
50 for (auto const &[k, v] : results) {
51 if (!data.WriteString(k) ||
52 !data.WriteInt32(static_cast<int>(v))) {
53 ZLOGW("write results error.");
54 return;
55 }
56 }
57 if (!data.WriteUint64(sequenceId)) {
58 ZLOGW("write label error.");
59 return;
60 }
61 MessageOption mo { MessageOption::TF_SYNC };
62 int error = Remote()->SendRequest(SYNCCOMPLETED, data, reply, mo);
63 if (error != 0) {
64 ZLOGW("SendRequest failed, error %d", error);
65 }
66 }
67
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)68 int32_t KvStoreSyncCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
69 MessageParcel &reply, MessageOption &option)
70 {
71 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
72 std::u16string descriptor = KvStoreSyncCallbackStub::GetDescriptor();
73 std::u16string remoteDescriptor = data.ReadInterfaceToken();
74 if (descriptor != remoteDescriptor) {
75 ZLOGE("local descriptor is not equal to remote");
76 return -1;
77 }
78 switch (code) {
79 case SYNCCOMPLETED: {
80 std::map<std::string, Status> results;
81 int32_t size = data.ReadInt32();
82 if (size < 0 || size > MAX_DEVICES) {
83 ZLOGW("size < 0(%d)", size);
84 return 0;
85 }
86 for (int32_t i = 0; i < size; i++) {
87 results.insert(std::pair<std::string, Status>(data.ReadString(),
88 static_cast<Status>(data.ReadInt32())));
89 }
90 uint64_t sequenceId = data.ReadUint64();
91 SyncCompleted(results, sequenceId);
92 return 0;
93 }
94 default:
95 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
96 }
97 }
98 } // namespace DistributedKv
99 } // namespace OHOS
100