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
16 #include "simple_timer_info.h"
17 #include "time_common.h"
18 #include "time_xcollie.h"
19 #include "time_service_stub.h"
20 #include "ntp_update_time.h"
21 #include "ntp_trusted_time.h"
22
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace OHOS::HiviewDFX;
26 namespace {
27 static const uint32_t MAX_EXEMPTION_SIZE = 1000;
28 static const int MAX_PID_LIST_SIZE = 1024;
29 }
30
31
TimeServiceStub()32 TimeServiceStub::TimeServiceStub()
33 {
34 memberFuncMap_ = {
35 { TimeServiceIpcInterfaceCode::SET_TIME,
36 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTime(data, reply); } },
37 { TimeServiceIpcInterfaceCode::SET_TIME_ZONE,
38 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTimeZone(data, reply); } },
39 { TimeServiceIpcInterfaceCode::GET_TIME_ZONE,
40 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetTimeZone(data, reply); } },
41 { TimeServiceIpcInterfaceCode::GET_WALL_TIME_MILLI,
42 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetWallTimeMs(data, reply); } },
43 { TimeServiceIpcInterfaceCode::GET_WALL_TIME_NANO,
44 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetWallTimeNs(data, reply); } },
45 { TimeServiceIpcInterfaceCode::GET_BOOT_TIME_MILLI,
46 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetBootTimeMs(data, reply); } },
47 { TimeServiceIpcInterfaceCode::GET_BOOT_TIME_NANO,
48 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetBootTimeNs(data, reply); } },
49 { TimeServiceIpcInterfaceCode::GET_MONO_TIME_MILLI,
50 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
51 return OnGetMonotonicTimeMs(data, reply); } },
52 { TimeServiceIpcInterfaceCode::GET_MONO_TIME_NANO,
53 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
54 return OnGetMonotonicTimeNs(data, reply); } },
55 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI,
56 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeMs(data, reply); } },
57 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO,
58 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeNs(data, reply); } },
59 { TimeServiceIpcInterfaceCode::CREATE_TIMER,
60 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnCreateTimer(data, reply); } },
61 { TimeServiceIpcInterfaceCode::START_TIMER,
62 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStartTimer(data, reply); } },
63 { TimeServiceIpcInterfaceCode::STOP_TIMER,
64 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStopTimer(data, reply); } },
65 { TimeServiceIpcInterfaceCode::DESTROY_TIMER,
66 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnDestroyTimer(data, reply); } },
67 { TimeServiceIpcInterfaceCode::PROXY_TIMER,
68 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnTimerProxy(data, reply); } },
69 { TimeServiceIpcInterfaceCode::PID_PROXY_TIMER,
70 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnPidTimerProxy(data, reply); } },
71 { TimeServiceIpcInterfaceCode::ADJUST_TIMER,
72 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAdjustTimer(data, reply); } },
73 { TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION,
74 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
75 return OnSetTimerExemption(data, reply); } },
76 { TimeServiceIpcInterfaceCode::RESET_ALL_PROXY,
77 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAllProxyReset(data, reply); } },
78 { TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI,
79 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetNtpTimeMs(data, reply); } },
80 };
81 }
82
~TimeServiceStub()83 TimeServiceStub::~TimeServiceStub()
84 {
85 memberFuncMap_.clear();
86 }
87
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
89 MessageOption &option)
90 {
91 TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
92 std::u16string descriptor = TimeServiceStub::GetDescriptor();
93 std::u16string remoteDescriptor = data.ReadInterfaceToken();
94 if (descriptor != remoteDescriptor) {
95 TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
96 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97 }
98 pid_t p = IPCSkeleton::GetCallingPid();
99 pid_t p1 = IPCSkeleton::GetCallingUid();
100 TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
101 code);
102 if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) &&
103 code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI)) {
104 auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code));
105 if (itFunc != memberFuncMap_.end()) {
106 auto memberFunc = itFunc->second;
107 if (memberFunc != nullptr) {
108 return memberFunc(data, reply);
109 }
110 }
111 }
112 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
113 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
114 return ret;
115 }
116
OnSetTime(MessageParcel & data,MessageParcel & reply)117 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
118 {
119 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
120 TimeXCollie timeXCollie("TimeService::SetTime");
121 int64_t time = data.ReadInt64();
122 auto apiVersion = data.ReadInt8();
123 if (apiVersion == APIVersion::API_VERSION_9) {
124 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
125 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
126 return E_TIME_NOT_SYSTEM_APP;
127 }
128 }
129 if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) {
130 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
131 return E_TIME_NO_PERMISSION;
132 }
133 int32_t ret = SetTime(time);
134 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
135 return ret;
136 }
137
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)138 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
139 {
140 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
141 TimeXCollie timeXCollie("TimeService::SetTimeZone");
142 std::string timeZoneId = data.ReadString();
143 auto apiVersion = data.ReadInt8();
144 if (apiVersion == APIVersion::API_VERSION_9) {
145 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
146 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
147 return E_TIME_NOT_SYSTEM_APP;
148 }
149 }
150 if (!TimePermission::CheckCallingPermission(TimePermission::setTimeZone)) {
151 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
152 return E_TIME_NO_PERMISSION;
153 }
154 int32_t ret = SetTimeZone(timeZoneId);
155 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
156 return ret;
157 }
158
OnGetTimeZone(MessageParcel & data,MessageParcel & reply)159 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
160 {
161 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
162 TimeXCollie timeXCollie("TimeService::GetTimeZone");
163 std::string timeZoneId;
164 int32_t ret = GetTimeZone(timeZoneId);
165 if (ret != ERR_OK) {
166 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
167 return ret;
168 }
169 reply.WriteString(timeZoneId);
170 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
171 return ret;
172 }
173
OnGetWallTimeMs(MessageParcel & data,MessageParcel & reply)174 int32_t TimeServiceStub::OnGetWallTimeMs(MessageParcel &data, MessageParcel &reply)
175 {
176 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
177 int64_t time;
178 int32_t ret = GetWallTimeMs(time);
179 if (ret != ERR_OK) {
180 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
181 return ret;
182 }
183 reply.WriteInt64(time);
184 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
185 return ret;
186 }
187
OnGetWallTimeNs(MessageParcel & data,MessageParcel & reply)188 int32_t TimeServiceStub::OnGetWallTimeNs(MessageParcel &data, MessageParcel &reply)
189 {
190 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
191 int64_t time;
192 int32_t ret = GetWallTimeNs(time);
193 if (ret != ERR_OK) {
194 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
195 return ret;
196 }
197 reply.WriteInt64(time);
198 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
199 return ret;
200 }
201
OnGetBootTimeMs(MessageParcel & data,MessageParcel & reply)202 int32_t TimeServiceStub::OnGetBootTimeMs(MessageParcel &data, MessageParcel &reply)
203 {
204 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
205 int64_t time;
206 int32_t ret = GetBootTimeMs(time);
207 if (ret != ERR_OK) {
208 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
209 return ret;
210 }
211 reply.WriteInt64(time);
212 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
213 return ret;
214 }
215
OnGetBootTimeNs(MessageParcel & data,MessageParcel & reply)216 int32_t TimeServiceStub::OnGetBootTimeNs(MessageParcel &data, MessageParcel &reply)
217 {
218 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
219 int64_t time;
220 int32_t ret = GetBootTimeNs(time);
221 if (ret != ERR_OK) {
222 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
223 return ret;
224 }
225 reply.WriteInt64(time);
226 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
227 return ret;
228 }
229
OnGetMonotonicTimeMs(MessageParcel & data,MessageParcel & reply)230 int32_t TimeServiceStub::OnGetMonotonicTimeMs(MessageParcel &data, MessageParcel &reply)
231 {
232 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
233 int64_t time;
234 int32_t ret = GetMonotonicTimeMs(time);
235 if (ret != ERR_OK) {
236 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
237 return ret;
238 }
239 reply.WriteInt64(time);
240 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
241 return ret;
242 }
243
OnGetMonotonicTimeNs(MessageParcel & data,MessageParcel & reply)244 int32_t TimeServiceStub::OnGetMonotonicTimeNs(MessageParcel &data, MessageParcel &reply)
245 {
246 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
247 int64_t time;
248 int32_t ret = GetMonotonicTimeNs(time);
249 if (ret != ERR_OK) {
250 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
251 return ret;
252 }
253 reply.WriteInt64(time);
254 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
255 return ret;
256 }
257
OnGetThreadTimeMs(MessageParcel & data,MessageParcel & reply)258 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
259 {
260 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
261 TimeXCollie timeXCollie("TimeService::GetThreadTimeMs");
262 int64_t time;
263 int32_t ret = GetThreadTimeMs(time);
264 if (ret != ERR_OK) {
265 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
266 return ret;
267 }
268 reply.WriteInt64(time);
269 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
270 return ret;
271 }
272
OnGetThreadTimeNs(MessageParcel & data,MessageParcel & reply)273 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
274 {
275 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
276 TimeXCollie timeXCollie("TimeService::GetThreadTimeNs");
277 int64_t time;
278 int32_t ret = GetThreadTimeNs(time);
279 if (ret != ERR_OK) {
280 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
281 return ret;
282 }
283 reply.WriteInt64(time);
284 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
285 return ret;
286 }
287
OnCreateTimer(MessageParcel & data,MessageParcel & reply)288 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
289 {
290 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
291 TimeXCollie timeXCollie("TimeService::CreateTimer");
292 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
293 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
294 return E_TIME_NOT_SYSTEM_APP;
295 }
296 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr };
297 auto type = data.ReadInt32();
298 auto repeat = data.ReadBool();
299 auto disposable = data.ReadBool();
300 auto interval = data.ReadUint64();
301 if (data.ReadBool()) {
302 wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>(
303 data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>());
304 if (!wantAgent) {
305 TIME_HILOGE(TIME_MODULE_SERVICE, "Input wantagent nullptr");
306 return E_TIME_NULLPTR;
307 }
308 }
309 sptr<IRemoteObject> obj = data.ReadRemoteObject();
310 if (obj == nullptr) {
311 TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
312 return E_TIME_NULLPTR;
313 }
314 auto timerOptions = std::make_shared<SimpleTimerInfo>();
315 timerOptions->type = type;
316 timerOptions->repeat = repeat;
317 timerOptions->interval = interval;
318 timerOptions->disposable = disposable;
319 timerOptions->wantAgent = wantAgent;
320 uint64_t timerId = data.ReadUint64();
321 auto errCode = CreateTimer(timerOptions, obj, timerId);
322 if (errCode != E_TIME_OK) {
323 TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
324 return E_TIME_DEAL_FAILED;
325 }
326 if (!reply.WriteUint64(timerId)) {
327 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
328 return E_TIME_WRITE_PARCEL_ERROR;
329 }
330 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
331 return ERR_OK;
332 }
333
OnStartTimer(MessageParcel & data,MessageParcel & reply)334 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
335 {
336 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
337 TimeXCollie timeXCollie("TimeService::StartTimer");
338 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
339 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
340 return E_TIME_NOT_SYSTEM_APP;
341 }
342 auto timerId = data.ReadUint64();
343 auto triggerTime = data.ReadUint64();
344 if (StartTimer(timerId, triggerTime) != E_TIME_OK) {
345 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
346 return E_TIME_DEAL_FAILED;
347 }
348 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
349 return ERR_OK;
350 }
351
OnStopTimer(MessageParcel & data,MessageParcel & reply)352 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
353 {
354 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
355 TimeXCollie timeXCollie("TimeService::StopTimer");
356 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
357 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
358 return E_TIME_NOT_SYSTEM_APP;
359 }
360 auto timerId = data.ReadUint64();
361 if (StopTimer(timerId) != E_TIME_OK) {
362 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
363 return E_TIME_DEAL_FAILED;
364 }
365 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
366 return ERR_OK;
367 }
368
OnDestroyTimer(MessageParcel & data,MessageParcel & reply)369 int32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply)
370 {
371 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
372 TimeXCollie timeXCollie("TimeService::DestroyTimer");
373 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
374 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
375 return E_TIME_NOT_SYSTEM_APP;
376 }
377 auto timerId = data.ReadUint64();
378 if (DestroyTimer(timerId, false) != E_TIME_OK) {
379 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
380 return E_TIME_DEAL_FAILED;
381 }
382 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
383 return ERR_OK;
384 }
385
OnTimerProxy(MessageParcel & data,MessageParcel & reply)386 int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply)
387 {
388 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
389 TimeXCollie timeXCollie("TimeService::TimerProxy");
390 auto uid = data.ReadInt32();
391 if (uid == 0) {
392 TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid.");
393 return E_TIME_READ_PARCEL_ERROR;
394 }
395 auto isProxy = data.ReadBool();
396 auto needRetrigger = data.ReadBool();
397 if (!ProxyTimer(uid, isProxy, needRetrigger)) {
398 return E_TIME_DEAL_FAILED;
399 }
400 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
401 return ERR_OK;
402 }
403
OnPidTimerProxy(MessageParcel & data,MessageParcel & reply)404 int32_t TimeServiceStub::OnPidTimerProxy(MessageParcel &data, MessageParcel &reply)
405 {
406 TimeXCollie timeXCollie("TimeService::PidTimerProxy");
407 auto uid = data.ReadInt32();
408 auto pidListSize = data.ReadInt32();
409 std::set<int> pidList;
410 if (pidListSize == 0 || pidListSize > MAX_PID_LIST_SIZE) {
411 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pid list size.");
412 return E_TIME_READ_PARCEL_ERROR;
413 }
414 for (int i = 0; i < pidListSize; i++) {
415 auto pid = data.ReadInt32();
416 if (pid != 0) {
417 pidList.insert(pid);
418 }
419 }
420 if (pidList.size() == 0) {
421 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pidList.");
422 return E_TIME_READ_PARCEL_ERROR;
423 }
424 auto isProxy = data.ReadBool();
425 auto needRetrigger = data.ReadBool();
426 if (!ProxyTimer(uid, pidList, isProxy, needRetrigger)) {
427 return E_TIME_DEAL_FAILED;
428 }
429 return ERR_OK;
430 }
431
OnAdjustTimer(MessageParcel & data,MessageParcel & reply)432 int32_t TimeServiceStub::OnAdjustTimer(MessageParcel &data, MessageParcel &reply)
433 {
434 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust start.");
435 TimeXCollie timeXCollie("TimeService::AdjustTimer");
436 if (!TimePermission::CheckProxyCallingPermission()) {
437 TIME_HILOGE(TIME_MODULE_SERVICE, "Adjust Timer permission check failed");
438 return E_TIME_NO_PERMISSION;
439 }
440 bool isAdjust = false;
441 uint32_t interval = 0;
442 if (!data.ReadBool(isAdjust)) {
443 return E_TIME_READ_PARCEL_ERROR;
444 }
445 if (!data.ReadUint32(interval)) {
446 return E_TIME_READ_PARCEL_ERROR;
447 }
448 if (isAdjust && interval == 0) {
449 TIME_HILOGE(TIME_MODULE_SERVICE, "invalid parameter: interval");
450 return E_TIME_READ_PARCEL_ERROR;
451 }
452 if (AdjustTimer(isAdjust, interval) != E_TIME_OK) {
453 TIME_HILOGE(TIME_MODULE_SERVICE, "Error adjust timer.");
454 return E_TIME_DEAL_FAILED;
455 }
456 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust end.");
457 return ERR_OK;
458 }
459
OnSetTimerExemption(MessageParcel & data,MessageParcel & reply)460 int32_t TimeServiceStub::OnSetTimerExemption(MessageParcel &data, MessageParcel &reply)
461 {
462 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption start.");
463 TimeXCollie timeXCollie("TimeService::SetTimerExemption");
464 if (!TimePermission::CheckProxyCallingPermission()) {
465 TIME_HILOGE(TIME_MODULE_SERVICE, "Set Timer Exemption permission check failed");
466 return E_TIME_NO_PERMISSION;
467 }
468 std::unordered_set<std::string> nameArr;
469 uint32_t size;
470 bool isExemption;
471 if (!data.ReadUint32(size)) {
472 return E_TIME_READ_PARCEL_ERROR;
473 }
474 if (size > MAX_EXEMPTION_SIZE) {
475 return E_TIME_PARAMETERS_INVALID;
476 }
477 for (uint32_t i = 0; i < size; ++i) {
478 std::string name;
479 if (!data.ReadString(name)) {
480 return E_TIME_READ_PARCEL_ERROR;
481 }
482 nameArr.insert(name);
483 }
484
485 if (!data.ReadBool(isExemption)) {
486 return E_TIME_READ_PARCEL_ERROR;
487 }
488 SetTimerExemption(nameArr, isExemption);
489 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption end.");
490 return ERR_OK;
491 }
492
OnAllProxyReset(MessageParcel & data,MessageParcel & reply)493 int32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply)
494 {
495 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
496 TimeXCollie timeXCollie("TimeService::AllProxyReset");
497 if (!ResetAllProxy()) {
498 return E_TIME_DEAL_FAILED;
499 }
500 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
501 return ERR_OK;
502 }
503
OnGetNtpTimeMs(MessageParcel & data,MessageParcel & reply)504 int32_t TimeServiceStub::OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply)
505 {
506 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
507 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
508 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
509 return E_TIME_NOT_SYSTEM_APP;
510 }
511 int64_t time = 0;
512 auto ret = GetNtpTimeMs(time);
513 if (ret != E_TIME_OK) {
514 return ret;
515 }
516 if (!reply.WriteUint64(time)) {
517 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
518 return E_TIME_WRITE_PARCEL_ERROR;
519 }
520 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
521 return ERR_OK;
522 }
523 } // namespace MiscServices
524 } // namespace OHOS