1cf69771bSopenharmony_ci/* 2cf69771bSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License. 5cf69771bSopenharmony_ci * You may obtain a copy of the License at 6cf69771bSopenharmony_ci * 7cf69771bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cf69771bSopenharmony_ci * 9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and 13cf69771bSopenharmony_ci * limitations under the License. 14cf69771bSopenharmony_ci */ 15cf69771bSopenharmony_ci 16cf69771bSopenharmony_ci#include "simple_timer_info.h" 17cf69771bSopenharmony_ci#include "time_common.h" 18cf69771bSopenharmony_ci#include "time_service_stub.h" 19cf69771bSopenharmony_ci#include "ntp_update_time.h" 20cf69771bSopenharmony_ci#include "ntp_trusted_time.h" 21cf69771bSopenharmony_ci 22cf69771bSopenharmony_cinamespace OHOS { 23cf69771bSopenharmony_cinamespace MiscServices { 24cf69771bSopenharmony_ciusing namespace OHOS::HiviewDFX; 25cf69771bSopenharmony_cinamespace { 26cf69771bSopenharmony_cistatic const uint32_t MAX_EXEMPTION_SIZE = 1000; 27cf69771bSopenharmony_cistatic const int MAX_PID_LIST_SIZE = 1024; 28cf69771bSopenharmony_ci} 29cf69771bSopenharmony_ci 30cf69771bSopenharmony_ci 31cf69771bSopenharmony_ciTimeServiceStub::TimeServiceStub() 32cf69771bSopenharmony_ci{ 33cf69771bSopenharmony_ci memberFuncMap_ = { 34cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::SET_TIME, 35cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTime(data, reply); } }, 36cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::SET_TIME_ZONE, 37cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTimeZone(data, reply); } }, 38cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::GET_TIME_ZONE, 39cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetTimeZone(data, reply); } }, 40cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI, 41cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeMs(data, reply); } }, 42cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO, 43cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeNs(data, reply); } }, 44cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::CREATE_TIMER, 45cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnCreateTimer(data, reply); } }, 46cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::START_TIMER, 47cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStartTimer(data, reply); } }, 48cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::STOP_TIMER, 49cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStopTimer(data, reply); } }, 50cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::DESTROY_TIMER, 51cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnDestroyTimer(data, reply); } }, 52cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::PROXY_TIMER, 53cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnTimerProxy(data, reply); } }, 54cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::PID_PROXY_TIMER, 55cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnPidTimerProxy(data, reply); } }, 56cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::ADJUST_TIMER, 57cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAdjustTimer(data, reply); } }, 58cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION, 59cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { 60cf69771bSopenharmony_ci return OnSetTimerExemption(data, reply); } }, 61cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::RESET_ALL_PROXY, 62cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAllProxyReset(data, reply); } }, 63cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI, 64cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetNtpTimeMs(data, reply); } }, 65cf69771bSopenharmony_ci { TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI, 66cf69771bSopenharmony_ci [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetRealTimeMs(data, reply); } }, 67cf69771bSopenharmony_ci }; 68cf69771bSopenharmony_ci} 69cf69771bSopenharmony_ci 70cf69771bSopenharmony_ciTimeServiceStub::~TimeServiceStub() 71cf69771bSopenharmony_ci{ 72cf69771bSopenharmony_ci memberFuncMap_.clear(); 73cf69771bSopenharmony_ci} 74cf69771bSopenharmony_ci 75cf69771bSopenharmony_ciint32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, 76cf69771bSopenharmony_ci MessageOption &option) 77cf69771bSopenharmony_ci{ 78cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code); 79cf69771bSopenharmony_ci std::u16string descriptor = TimeServiceStub::GetDescriptor(); 80cf69771bSopenharmony_ci std::u16string remoteDescriptor = data.ReadInterfaceToken(); 81cf69771bSopenharmony_ci if (descriptor != remoteDescriptor) { 82cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail"); 83cf69771bSopenharmony_ci return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 84cf69771bSopenharmony_ci } 85cf69771bSopenharmony_ci pid_t p = IPCSkeleton::GetCallingPid(); 86cf69771bSopenharmony_ci pid_t p1 = IPCSkeleton::GetCallingUid(); 87cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, 88cf69771bSopenharmony_ci code); 89cf69771bSopenharmony_ci if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) && 90cf69771bSopenharmony_ci code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI)) { 91cf69771bSopenharmony_ci auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code)); 92cf69771bSopenharmony_ci if (itFunc != memberFuncMap_.end()) { 93cf69771bSopenharmony_ci auto memberFunc = itFunc->second; 94cf69771bSopenharmony_ci if (memberFunc != nullptr) { 95cf69771bSopenharmony_ci return memberFunc(data, reply); 96cf69771bSopenharmony_ci } 97cf69771bSopenharmony_ci } 98cf69771bSopenharmony_ci } 99cf69771bSopenharmony_ci int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option); 100cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 101cf69771bSopenharmony_ci return ret; 102cf69771bSopenharmony_ci} 103cf69771bSopenharmony_ci 104cf69771bSopenharmony_ciint32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply) 105cf69771bSopenharmony_ci{ 106cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start."); 107cf69771bSopenharmony_ci int64_t time = data.ReadInt64(); 108cf69771bSopenharmony_ci auto apiVersion = data.ReadInt8(); 109cf69771bSopenharmony_ci if (apiVersion == APIVersion::API_VERSION_9) { 110cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 111cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 112cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 113cf69771bSopenharmony_ci } 114cf69771bSopenharmony_ci } 115cf69771bSopenharmony_ci if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) { 116cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed"); 117cf69771bSopenharmony_ci return E_TIME_NO_PERMISSION; 118cf69771bSopenharmony_ci } 119cf69771bSopenharmony_ci int32_t ret = SetTime(time); 120cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 121cf69771bSopenharmony_ci return ret; 122cf69771bSopenharmony_ci} 123cf69771bSopenharmony_ci 124cf69771bSopenharmony_ciint32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply) 125cf69771bSopenharmony_ci{ 126cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start."); 127cf69771bSopenharmony_ci std::string timeZoneId = data.ReadString(); 128cf69771bSopenharmony_ci auto apiVersion = data.ReadInt8(); 129cf69771bSopenharmony_ci if (apiVersion == APIVersion::API_VERSION_9) { 130cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 131cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 132cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 133cf69771bSopenharmony_ci } 134cf69771bSopenharmony_ci } 135cf69771bSopenharmony_ci if (!TimePermission::CheckCallingPermission(TimePermission::setTimeZone)) { 136cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed"); 137cf69771bSopenharmony_ci return E_TIME_NO_PERMISSION; 138cf69771bSopenharmony_ci } 139cf69771bSopenharmony_ci int32_t ret = SetTimeZone(timeZoneId); 140cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 141cf69771bSopenharmony_ci return ret; 142cf69771bSopenharmony_ci} 143cf69771bSopenharmony_ci 144cf69771bSopenharmony_ciint32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply) 145cf69771bSopenharmony_ci{ 146cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start."); 147cf69771bSopenharmony_ci std::string timeZoneId; 148cf69771bSopenharmony_ci int32_t ret = GetTimeZone(timeZoneId); 149cf69771bSopenharmony_ci if (ret != ERR_OK) { 150cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 151cf69771bSopenharmony_ci return ret; 152cf69771bSopenharmony_ci } 153cf69771bSopenharmony_ci reply.WriteString(timeZoneId); 154cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end."); 155cf69771bSopenharmony_ci return ret; 156cf69771bSopenharmony_ci} 157cf69771bSopenharmony_ci 158cf69771bSopenharmony_ciint32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply) 159cf69771bSopenharmony_ci{ 160cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start."); 161cf69771bSopenharmony_ci int64_t time; 162cf69771bSopenharmony_ci int32_t ret = GetThreadTimeMs(time); 163cf69771bSopenharmony_ci if (ret != ERR_OK) { 164cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 165cf69771bSopenharmony_ci return ret; 166cf69771bSopenharmony_ci } 167cf69771bSopenharmony_ci reply.WriteInt64(time); 168cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end."); 169cf69771bSopenharmony_ci return ret; 170cf69771bSopenharmony_ci} 171cf69771bSopenharmony_ci 172cf69771bSopenharmony_ciint32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply) 173cf69771bSopenharmony_ci{ 174cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " start."); 175cf69771bSopenharmony_ci int64_t time; 176cf69771bSopenharmony_ci int32_t ret = GetThreadTimeNs(time); 177cf69771bSopenharmony_ci if (ret != ERR_OK) { 178cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret); 179cf69771bSopenharmony_ci return ret; 180cf69771bSopenharmony_ci } 181cf69771bSopenharmony_ci reply.WriteInt64(time); 182cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, " end."); 183cf69771bSopenharmony_ci return ret; 184cf69771bSopenharmony_ci} 185cf69771bSopenharmony_ci 186cf69771bSopenharmony_ciint32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply) 187cf69771bSopenharmony_ci{ 188cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 189cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 190cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 191cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 192cf69771bSopenharmony_ci } 193cf69771bSopenharmony_ci std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr }; 194cf69771bSopenharmony_ci auto type = data.ReadInt32(); 195cf69771bSopenharmony_ci auto repeat = data.ReadBool(); 196cf69771bSopenharmony_ci auto disposable = data.ReadBool(); 197cf69771bSopenharmony_ci auto interval = data.ReadUint64(); 198cf69771bSopenharmony_ci if (data.ReadBool()) { 199cf69771bSopenharmony_ci wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>( 200cf69771bSopenharmony_ci data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>()); 201cf69771bSopenharmony_ci if (!wantAgent) { 202cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Input wantagent nullptr"); 203cf69771bSopenharmony_ci return E_TIME_NULLPTR; 204cf69771bSopenharmony_ci } 205cf69771bSopenharmony_ci } 206cf69771bSopenharmony_ci sptr<IRemoteObject> obj = data.ReadRemoteObject(); 207cf69771bSopenharmony_ci if (obj == nullptr) { 208cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr"); 209cf69771bSopenharmony_ci return E_TIME_NULLPTR; 210cf69771bSopenharmony_ci } 211cf69771bSopenharmony_ci auto timerOptions = std::make_shared<SimpleTimerInfo>(); 212cf69771bSopenharmony_ci timerOptions->type = type; 213cf69771bSopenharmony_ci timerOptions->repeat = repeat; 214cf69771bSopenharmony_ci timerOptions->interval = interval; 215cf69771bSopenharmony_ci timerOptions->disposable = disposable; 216cf69771bSopenharmony_ci timerOptions->wantAgent = wantAgent; 217cf69771bSopenharmony_ci uint64_t timerId = data.ReadUint64(); 218cf69771bSopenharmony_ci auto errCode = CreateTimer(timerOptions, obj, timerId); 219cf69771bSopenharmony_ci if (errCode != E_TIME_OK) { 220cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed"); 221cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 222cf69771bSopenharmony_ci } 223cf69771bSopenharmony_ci if (!reply.WriteUint64(timerId)) { 224cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId"); 225cf69771bSopenharmony_ci return E_TIME_WRITE_PARCEL_ERROR; 226cf69771bSopenharmony_ci } 227cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 228cf69771bSopenharmony_ci return ERR_OK; 229cf69771bSopenharmony_ci} 230cf69771bSopenharmony_ci 231cf69771bSopenharmony_ciint32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply) 232cf69771bSopenharmony_ci{ 233cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 234cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 235cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 236cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 237cf69771bSopenharmony_ci } 238cf69771bSopenharmony_ci auto timerId = data.ReadUint64(); 239cf69771bSopenharmony_ci auto triggerTime = data.ReadUint64(); 240cf69771bSopenharmony_ci if (StartTimer(timerId, triggerTime) != E_TIME_OK) { 241cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer"); 242cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 243cf69771bSopenharmony_ci } 244cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 245cf69771bSopenharmony_ci return ERR_OK; 246cf69771bSopenharmony_ci} 247cf69771bSopenharmony_ci 248cf69771bSopenharmony_ciint32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply) 249cf69771bSopenharmony_ci{ 250cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 251cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 252cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 253cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 254cf69771bSopenharmony_ci } 255cf69771bSopenharmony_ci auto timerId = data.ReadUint64(); 256cf69771bSopenharmony_ci if (StopTimer(timerId) != E_TIME_OK) { 257cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer"); 258cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 259cf69771bSopenharmony_ci } 260cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 261cf69771bSopenharmony_ci return ERR_OK; 262cf69771bSopenharmony_ci} 263cf69771bSopenharmony_ci 264cf69771bSopenharmony_ciint32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply) 265cf69771bSopenharmony_ci{ 266cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 267cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 268cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 269cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 270cf69771bSopenharmony_ci } 271cf69771bSopenharmony_ci auto timerId = data.ReadUint64(); 272cf69771bSopenharmony_ci if (DestroyTimer(timerId, false) != E_TIME_OK) { 273cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer"); 274cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 275cf69771bSopenharmony_ci } 276cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 277cf69771bSopenharmony_ci return ERR_OK; 278cf69771bSopenharmony_ci} 279cf69771bSopenharmony_ci 280cf69771bSopenharmony_ciint32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply) 281cf69771bSopenharmony_ci{ 282cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 283cf69771bSopenharmony_ci auto uid = data.ReadInt32(); 284cf69771bSopenharmony_ci if (uid == 0) { 285cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid."); 286cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 287cf69771bSopenharmony_ci } 288cf69771bSopenharmony_ci auto isProxy = data.ReadBool(); 289cf69771bSopenharmony_ci auto needRetrigger = data.ReadBool(); 290cf69771bSopenharmony_ci if (!ProxyTimer(uid, isProxy, needRetrigger)) { 291cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 292cf69771bSopenharmony_ci } 293cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 294cf69771bSopenharmony_ci return ERR_OK; 295cf69771bSopenharmony_ci} 296cf69771bSopenharmony_ci 297cf69771bSopenharmony_ciint32_t TimeServiceStub::OnPidTimerProxy(MessageParcel &data, MessageParcel &reply) 298cf69771bSopenharmony_ci{ 299cf69771bSopenharmony_ci auto pidListSize = data.ReadInt32(); 300cf69771bSopenharmony_ci std::set<int> pidList; 301cf69771bSopenharmony_ci if (pidListSize == 0 || pidListSize > MAX_PID_LIST_SIZE) { 302cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Error pid list size."); 303cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 304cf69771bSopenharmony_ci } 305cf69771bSopenharmony_ci for (int i = 0; i < pidListSize; i++) { 306cf69771bSopenharmony_ci auto pid = data.ReadInt32(); 307cf69771bSopenharmony_ci if (pid != 0) { 308cf69771bSopenharmony_ci pidList.insert(pid); 309cf69771bSopenharmony_ci } 310cf69771bSopenharmony_ci } 311cf69771bSopenharmony_ci if (pidList.size() == 0) { 312cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Error pidList."); 313cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 314cf69771bSopenharmony_ci } 315cf69771bSopenharmony_ci auto isProxy = data.ReadBool(); 316cf69771bSopenharmony_ci auto needRetrigger = data.ReadBool(); 317cf69771bSopenharmony_ci if (!ProxyTimer(pidList, isProxy, needRetrigger)) { 318cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 319cf69771bSopenharmony_ci } 320cf69771bSopenharmony_ci return ERR_OK; 321cf69771bSopenharmony_ci} 322cf69771bSopenharmony_ci 323cf69771bSopenharmony_ciint32_t TimeServiceStub::OnAdjustTimer(MessageParcel &data, MessageParcel &reply) 324cf69771bSopenharmony_ci{ 325cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust start."); 326cf69771bSopenharmony_ci if (!TimePermission::CheckProxyCallingPermission()) { 327cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Adjust Timer permission check failed"); 328cf69771bSopenharmony_ci return E_TIME_NO_PERMISSION; 329cf69771bSopenharmony_ci } 330cf69771bSopenharmony_ci bool isAdjust = false; 331cf69771bSopenharmony_ci uint32_t interval = 0; 332cf69771bSopenharmony_ci if (!data.ReadBool(isAdjust)) { 333cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 334cf69771bSopenharmony_ci } 335cf69771bSopenharmony_ci if (!data.ReadUint32(interval)) { 336cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 337cf69771bSopenharmony_ci } 338cf69771bSopenharmony_ci if (isAdjust && interval == 0) { 339cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "invalid parameter: interval"); 340cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 341cf69771bSopenharmony_ci } 342cf69771bSopenharmony_ci if (AdjustTimer(isAdjust, interval) != E_TIME_OK) { 343cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Error adjust timer."); 344cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 345cf69771bSopenharmony_ci } 346cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust end."); 347cf69771bSopenharmony_ci return ERR_OK; 348cf69771bSopenharmony_ci} 349cf69771bSopenharmony_ci 350cf69771bSopenharmony_ciint32_t TimeServiceStub::OnSetTimerExemption(MessageParcel &data, MessageParcel &reply) 351cf69771bSopenharmony_ci{ 352cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption start."); 353cf69771bSopenharmony_ci if (!TimePermission::CheckProxyCallingPermission()) { 354cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Set Timer Exemption permission check failed"); 355cf69771bSopenharmony_ci return E_TIME_NO_PERMISSION; 356cf69771bSopenharmony_ci } 357cf69771bSopenharmony_ci std::unordered_set<std::string> nameArr; 358cf69771bSopenharmony_ci uint32_t size; 359cf69771bSopenharmony_ci bool isExemption; 360cf69771bSopenharmony_ci if (!data.ReadUint32(size)) { 361cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 362cf69771bSopenharmony_ci } 363cf69771bSopenharmony_ci if (size > MAX_EXEMPTION_SIZE) { 364cf69771bSopenharmony_ci return E_TIME_PARAMETERS_INVALID; 365cf69771bSopenharmony_ci } 366cf69771bSopenharmony_ci for (uint32_t i = 0; i < size; ++i) { 367cf69771bSopenharmony_ci std::string name; 368cf69771bSopenharmony_ci if (!data.ReadString(name)) { 369cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 370cf69771bSopenharmony_ci } 371cf69771bSopenharmony_ci nameArr.insert(name); 372cf69771bSopenharmony_ci } 373cf69771bSopenharmony_ci 374cf69771bSopenharmony_ci if (!data.ReadBool(isExemption)) { 375cf69771bSopenharmony_ci return E_TIME_READ_PARCEL_ERROR; 376cf69771bSopenharmony_ci } 377cf69771bSopenharmony_ci SetTimerExemption(nameArr, isExemption); 378cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption end."); 379cf69771bSopenharmony_ci return ERR_OK; 380cf69771bSopenharmony_ci} 381cf69771bSopenharmony_ci 382cf69771bSopenharmony_ciint32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply) 383cf69771bSopenharmony_ci{ 384cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 385cf69771bSopenharmony_ci if (!ResetAllProxy()) { 386cf69771bSopenharmony_ci return E_TIME_DEAL_FAILED; 387cf69771bSopenharmony_ci } 388cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 389cf69771bSopenharmony_ci return ERR_OK; 390cf69771bSopenharmony_ci} 391cf69771bSopenharmony_ci 392cf69771bSopenharmony_ciint32_t TimeServiceStub::OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply) 393cf69771bSopenharmony_ci{ 394cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 395cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 396cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 397cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 398cf69771bSopenharmony_ci } 399cf69771bSopenharmony_ci int64_t time = 0; 400cf69771bSopenharmony_ci auto ret = GetNtpTimeMs(time); 401cf69771bSopenharmony_ci if (ret != E_TIME_OK) { 402cf69771bSopenharmony_ci return ret; 403cf69771bSopenharmony_ci } 404cf69771bSopenharmony_ci if (!reply.WriteUint64(time)) { 405cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time"); 406cf69771bSopenharmony_ci return E_TIME_WRITE_PARCEL_ERROR; 407cf69771bSopenharmony_ci } 408cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 409cf69771bSopenharmony_ci return ERR_OK; 410cf69771bSopenharmony_ci} 411cf69771bSopenharmony_ci 412cf69771bSopenharmony_ciint32_t TimeServiceStub::OnGetRealTimeMs(MessageParcel &data, MessageParcel &reply) 413cf69771bSopenharmony_ci{ 414cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "start."); 415cf69771bSopenharmony_ci if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { 416cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); 417cf69771bSopenharmony_ci return E_TIME_NOT_SYSTEM_APP; 418cf69771bSopenharmony_ci } 419cf69771bSopenharmony_ci int64_t time = 0; 420cf69771bSopenharmony_ci auto ret = GetRealTimeMs(time); 421cf69771bSopenharmony_ci if (ret != E_TIME_OK) { 422cf69771bSopenharmony_ci return ret; 423cf69771bSopenharmony_ci } 424cf69771bSopenharmony_ci if (!reply.WriteUint64(time)) { 425cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time"); 426cf69771bSopenharmony_ci return E_TIME_WRITE_PARCEL_ERROR; 427cf69771bSopenharmony_ci } 428cf69771bSopenharmony_ci TIME_HILOGD(TIME_MODULE_SERVICE, "end."); 429cf69771bSopenharmony_ci return ERR_OK; 430cf69771bSopenharmony_ci} 431cf69771bSopenharmony_ci} // namespace MiscServices 432cf69771bSopenharmony_ci} // namespace OHOS