1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci#include "watcher_manager_proxy.h" 16d9f0492fSopenharmony_ci#include "watcher_utils.h" 17d9f0492fSopenharmony_ci#include "sysparam_errno.h" 18d9f0492fSopenharmony_ci 19d9f0492fSopenharmony_cinamespace OHOS { 20d9f0492fSopenharmony_cinamespace init_param { 21d9f0492fSopenharmony_ciuint32_t WatcherManagerProxy::AddRemoteWatcher(uint32_t id, const sptr<IWatcher> &watcher) 22d9f0492fSopenharmony_ci{ 23d9f0492fSopenharmony_ci WATCHER_CHECK(watcher != nullptr, return ERR_INVALID_VALUE, "Invalid param"); 24d9f0492fSopenharmony_ci MessageParcel data; 25d9f0492fSopenharmony_ci data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor()); 26d9f0492fSopenharmony_ci bool ret = data.WriteRemoteObject(watcher->AsObject()); 27d9f0492fSopenharmony_ci WATCHER_CHECK(ret, return 0, "Can not get remote"); 28d9f0492fSopenharmony_ci data.WriteUint32(id); 29d9f0492fSopenharmony_ci 30d9f0492fSopenharmony_ci MessageParcel reply; 31d9f0492fSopenharmony_ci MessageOption option { MessageOption::TF_SYNC }; 32d9f0492fSopenharmony_ci int32_t res = SendWatcherMsg(static_cast<uint32_t>(ParamWatcherInterfaceCode::ADD_REMOTE_AGENT), 33d9f0492fSopenharmony_ci data, reply, option); 34d9f0492fSopenharmony_ci WATCHER_CHECK(res == ERR_OK, return 0, "Transact error %d", res); 35d9f0492fSopenharmony_ci return reply.ReadUint32(); 36d9f0492fSopenharmony_ci} 37d9f0492fSopenharmony_ci 38d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::DelRemoteWatcher(uint32_t remoteWatcherId) 39d9f0492fSopenharmony_ci{ 40d9f0492fSopenharmony_ci MessageParcel data; 41d9f0492fSopenharmony_ci data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor()); 42d9f0492fSopenharmony_ci data.WriteUint32(remoteWatcherId); 43d9f0492fSopenharmony_ci MessageParcel reply; 44d9f0492fSopenharmony_ci MessageOption option { MessageOption::TF_SYNC }; 45d9f0492fSopenharmony_ci int32_t res = SendWatcherMsg(static_cast<uint32_t>(ParamWatcherInterfaceCode::DEL_REMOTE_AGENT), 46d9f0492fSopenharmony_ci data, reply, option); 47d9f0492fSopenharmony_ci WATCHER_CHECK(res == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error"); 48d9f0492fSopenharmony_ci return reply.ReadInt32(); 49d9f0492fSopenharmony_ci} 50d9f0492fSopenharmony_ci 51d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::SendMsg(int op, const std::string &keyPrefix, uint32_t remoteWatcherId) 52d9f0492fSopenharmony_ci{ 53d9f0492fSopenharmony_ci MessageParcel data; 54d9f0492fSopenharmony_ci data.WriteInterfaceToken(WatcherManagerProxy::GetDescriptor()); 55d9f0492fSopenharmony_ci data.WriteString(keyPrefix); 56d9f0492fSopenharmony_ci data.WriteUint32(remoteWatcherId); 57d9f0492fSopenharmony_ci MessageParcel reply; 58d9f0492fSopenharmony_ci MessageOption option { MessageOption::TF_SYNC }; 59d9f0492fSopenharmony_ci int32_t res = SendWatcherMsg(op, data, reply, option); 60d9f0492fSopenharmony_ci WATCHER_CHECK(res == ERR_OK, return 0, "Transact error"); 61d9f0492fSopenharmony_ci return reply.ReadInt32(); 62d9f0492fSopenharmony_ci} 63d9f0492fSopenharmony_ci 64d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::AddWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) 65d9f0492fSopenharmony_ci{ 66d9f0492fSopenharmony_ci return SendMsg(static_cast<uint32_t>(ParamWatcherInterfaceCode::ADD_WATCHER), keyPrefix, remoteWatcherId); 67d9f0492fSopenharmony_ci} 68d9f0492fSopenharmony_ci 69d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::DelWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) 70d9f0492fSopenharmony_ci{ 71d9f0492fSopenharmony_ci return SendMsg(static_cast<uint32_t>(ParamWatcherInterfaceCode::DEL_WATCHER), keyPrefix, remoteWatcherId); 72d9f0492fSopenharmony_ci} 73d9f0492fSopenharmony_ci 74d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::RefreshWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) 75d9f0492fSopenharmony_ci{ 76d9f0492fSopenharmony_ci return SendMsg(static_cast<uint32_t>(ParamWatcherInterfaceCode::REFRESH_WATCHER), keyPrefix, remoteWatcherId); 77d9f0492fSopenharmony_ci} 78d9f0492fSopenharmony_ci 79d9f0492fSopenharmony_ciint32_t WatcherManagerProxy::SendWatcherMsg(uint32_t code, 80d9f0492fSopenharmony_ci MessageParcel &data, MessageParcel &reply, MessageOption &option) 81d9f0492fSopenharmony_ci{ 82d9f0492fSopenharmony_ci auto remote = Remote(); 83d9f0492fSopenharmony_ci if (remote != nullptr) { 84d9f0492fSopenharmony_ci return remote->SendRequest(code, data, reply, option); 85d9f0492fSopenharmony_ci } 86d9f0492fSopenharmony_ci#ifdef STARTUP_INIT_TEST 87d9f0492fSopenharmony_ci return 0; 88d9f0492fSopenharmony_ci#else 89d9f0492fSopenharmony_ci return SYSPARAM_SYSTEM_ERROR; 90d9f0492fSopenharmony_ci#endif 91d9f0492fSopenharmony_ci} 92d9f0492fSopenharmony_ci} 93d9f0492fSopenharmony_ci} // namespace OHOS 94