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 #include "watcher_manager_proxy.h"
16 #include "watcher_utils.h"
17
18 namespace OHOS {
19 namespace init_param {
AddRemoteWatcher(uint32_t id,const sptr<IWatcher> & watcher)20 uint32_t WatcherManagerProxy::AddRemoteWatcher(uint32_t id, const sptr<IWatcher> &watcher)
21 {
22 WATCHER_CHECK(watcher != nullptr, return ERR_INVALID_VALUE, "Invalid param");
23 auto remote = Remote();
24 WATCHER_CHECK(remote != nullptr, return 0, "Can not get remote");
25
26 MessageParcel data;
27 data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
28 bool ret = data.WriteRemoteObject(watcher->AsObject());
29 WATCHER_CHECK(ret, return 0, "Can not get remote");
30 data.WriteUint32(id);
31
32 MessageParcel reply;
33 MessageOption option { MessageOption::TF_SYNC };
34 int32_t res = remote->SendRequest(ADD_REMOTE_AGENT, data, reply, option);
35 WATCHER_CHECK(res == ERR_OK, return 0, "Transact error %d", res);
36 return reply.ReadUint32();
37 }
38
DelRemoteWatcher(uint32_t remoteWatcherId)39 int32_t WatcherManagerProxy::DelRemoteWatcher(uint32_t remoteWatcherId)
40 {
41 auto remote = Remote();
42 WATCHER_CHECK(remote != nullptr, return ERR_FLATTEN_OBJECT, "Can not get remote");
43
44 MessageParcel data;
45 data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
46 data.WriteUint32(remoteWatcherId);
47 MessageParcel reply;
48 MessageOption option { MessageOption::TF_SYNC };
49 int32_t res = remote->SendRequest(DEL_REMOTE_AGENT, data, reply, option);
50 WATCHER_CHECK(res == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error");
51 return reply.ReadInt32();
52 }
53
SendMsg(int op,const std::string & keyPrefix,uint32_t remoteWatcherId)54 int32_t WatcherManagerProxy::SendMsg(int op, const std::string &keyPrefix, uint32_t remoteWatcherId)
55 {
56 auto remote = Remote();
57 WATCHER_CHECK(remote != nullptr, return 0, "Can not get remote");
58
59 MessageParcel data;
60 data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor());
61 data.WriteString(keyPrefix);
62 data.WriteUint32(remoteWatcherId);
63 MessageParcel reply;
64 MessageOption option { MessageOption::TF_SYNC };
65 int32_t res = remote->SendRequest(op, data, reply, option);
66 WATCHER_CHECK(res == ERR_OK, return 0, "Transact error");
67 return reply.ReadInt32();
68 }
69
AddWatcher(const std::string & keyPrefix,uint32_t remoteWatcherId)70 int32_t WatcherManagerProxy::AddWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId)
71 {
72 return SendMsg(ADD_WATCHER, keyPrefix, remoteWatcherId);
73 }
74
DelWatcher(const std::string & keyPrefix,uint32_t remoteWatcherId)75 int32_t WatcherManagerProxy::DelWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId)
76 {
77 return SendMsg(DEL_WATCHER, keyPrefix, remoteWatcherId);
78 }
79
RefreshWatcher(const std::string & keyPrefix,uint32_t remoteWatcherId)80 int32_t WatcherManagerProxy::RefreshWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId)
81 {
82 return SendMsg(REFRESH_WATCHER, keyPrefix, remoteWatcherId);
83 }
84 }
85 } // namespace OHOS
86