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