1 /*
2  * Copyright (c) 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 #define LOG_TAG "KVDBNotifierProxy"
17 
18 #include "kvdb_notifier_proxy.h"
19 #include <cinttypes>
20 #include "distributeddata_kvdb_ipc_interface_code.h"
21 #include "itypes_util.h"
22 #include "log_print.h"
23 #include "message_parcel.h"
24 #include "message_option.h"
25 #include "utils/anonymous.h"
26 
27 namespace OHOS {
28 namespace DistributedKv {
29 using namespace OHOS::DistributedData;
30 #define IPC_SEND(code, reply, ...)                                              \
31     ({                                                                          \
32         int32_t __status = SUCCESS;                                             \
33         do {                                                                    \
34             MessageParcel request;                                              \
35             if (!request.WriteInterfaceToken(GetDescriptor())) {                \
36                 __status = IPC_PARCEL_ERROR;                                    \
37                 break;                                                          \
38             }                                                                   \
39             if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) {                 \
40                 __status = IPC_PARCEL_ERROR;                                    \
41                 break;                                                          \
42             }                                                                   \
43             MessageOption option;                                               \
44             auto result = remote_->SendRequest((code), request, reply, option); \
45             if (result != 0) {                                                  \
46                 __status = IPC_ERROR;                                           \
47                 break;                                                          \
48             }                                                                   \
49         } while (0);                                                            \
50         __status;                                                               \
51     })
52 
KVDBNotifierProxy(const sptr<IRemoteObject> & impl)53 KVDBNotifierProxy::KVDBNotifierProxy(const sptr<IRemoteObject> &impl)
54     : IRemoteProxy<IKVDBNotifier>(impl)
55 {
56     remote_ = Remote();
57 }
58 
SyncCompleted(const std::map<std::string,Status> & results,uint64_t sequenceId)59 void KVDBNotifierProxy::SyncCompleted(const std::map<std::string, Status> &results, uint64_t sequenceId)
60 {
61     MessageParcel reply;
62     int32_t status = IPC_SEND(static_cast<uint32_t>(
63         KVDBNotifierCode::TRANS_SYNC_COMPLETED), reply, results, sequenceId);
64     if (status != SUCCESS) {
65         ZLOGE("status:%{public}d, results size:%{public}zu, sequenceId:%{public}" PRIu64,
66             status, results.size(), sequenceId);
67     }
68 }
69 
SyncCompleted(uint64_t seqNum,ProgressDetail && detail)70 void KVDBNotifierProxy::SyncCompleted(uint64_t seqNum, ProgressDetail &&detail)
71 {
72     MessageParcel reply;
73     int32_t status =
74         IPC_SEND(static_cast<uint32_t>(KVDBNotifierCode::TRANS_CLOUD_SYNC_COMPLETED), reply, seqNum, detail);
75     if (status != SUCCESS) {
76         ZLOGE("status:%{public}d, sequenceId:%{public}" PRIu64, status, seqNum);
77     }
78 }
79 
OnRemoteChange(const std::map<std::string,bool> & mask,int32_t dataType)80 void KVDBNotifierProxy::OnRemoteChange(const std::map<std::string, bool> &mask, int32_t dataType)
81 {
82     MessageParcel reply;
83     int32_t status = IPC_SEND(static_cast<uint32_t>(
84         KVDBNotifierCode::TRANS_ON_REMOTE_CHANGED), reply, mask, dataType);
85     if (status != SUCCESS) {
86         ZLOGE("status:%{public}d, mask:%{public}zu, dataType:%{public}d", status, mask.size(), dataType);
87     }
88 }
89 
OnSwitchChange(const SwitchNotification & notification)90 void KVDBNotifierProxy::OnSwitchChange(const SwitchNotification &notification)
91 {
92     MessageParcel reply;
93     int32_t status = IPC_SEND(static_cast<uint32_t>(
94         KVDBNotifierCode::TRANS_ON_SWITCH_CHANGED), reply, notification);
95     if (status != SUCCESS) {
96         ZLOGE("status:%{public}d, device:%{public}s",
97             status, Anonymous::Change(notification.deviceId).c_str());
98     }
99 }
100 } // namespace DistributedKv
101 } // namespace OHOS