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_service_stub.h"
19
20 namespace OHOS {
21 namespace MiscServices {
22 using namespace OHOS::HiviewDFX;
23
TimeServiceStub()24 TimeServiceStub::TimeServiceStub()
25 {
26 memberFuncMap_[TimeServiceIpcInterfaceCode::SET_TIME] = &TimeServiceStub::OnSetTime;
27 memberFuncMap_[TimeServiceIpcInterfaceCode::SET_TIME_ZONE] = &TimeServiceStub::OnSetTimeZone;
28 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_TIME_ZONE] = &TimeServiceStub::OnGetTimeZone;
29 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_WALL_TIME_MILLI] = &TimeServiceStub::OnGetWallTimeMs;
30 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_WALL_TIME_NANO] = &TimeServiceStub::OnGetWallTimeNs;
31 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_BOOT_TIME_MILLI] = &TimeServiceStub::OnGetBootTimeMs;
32 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_BOOT_TIME_NANO] = &TimeServiceStub::OnGetBootTimeNs;
33 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_MONO_TIME_MILLI] = &TimeServiceStub::OnGetMonotonicTimeMs;
34 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_MONO_TIME_NANO] = &TimeServiceStub::OnGetMonotonicTimeNs;
35 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI] = &TimeServiceStub::OnGetThreadTimeMs;
36 memberFuncMap_[TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO] = &TimeServiceStub::OnGetThreadTimeNs;
37 memberFuncMap_[TimeServiceIpcInterfaceCode::CREATE_TIMER] = &TimeServiceStub::OnCreateTimer;
38 memberFuncMap_[TimeServiceIpcInterfaceCode::START_TIMER] = &TimeServiceStub::OnStartTimer;
39 memberFuncMap_[TimeServiceIpcInterfaceCode::STOP_TIMER] = &TimeServiceStub::OnStopTimer;
40 memberFuncMap_[TimeServiceIpcInterfaceCode::DESTROY_TIMER] = &TimeServiceStub::OnDestroyTimer;
41 memberFuncMap_[TimeServiceIpcInterfaceCode::PROXY_TIMER] = &TimeServiceStub::OnTimerProxy;
42 memberFuncMap_[TimeServiceIpcInterfaceCode::RESET_ALL_PROXY] = &TimeServiceStub::OnAllProxyReset;
43 }
44
~TimeServiceStub()45 TimeServiceStub::~TimeServiceStub()
46 {
47 memberFuncMap_.clear();
48 }
49
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
51 MessageOption &option)
52 {
53 TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
54 std::u16string descriptor = TimeServiceStub::GetDescriptor();
55 std::u16string remoteDescriptor = data.ReadInterfaceToken();
56 if (descriptor != remoteDescriptor) {
57 TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
58 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59 }
60 pid_t p = IPCSkeleton::GetCallingPid();
61 pid_t p1 = IPCSkeleton::GetCallingUid();
62 TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
63 code);
64 if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) &&
65 code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::RESET_ALL_PROXY)) {
66 auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code));
67 if (itFunc != memberFuncMap_.end()) {
68 auto memberFunc = itFunc->second;
69 if (memberFunc != nullptr) {
70 return (this->*memberFunc)(data, reply);
71 }
72 }
73 }
74 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
75 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
76 return ret;
77 }
78
OnSetTime(MessageParcel & data,MessageParcel & reply)79 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
80 {
81 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
82 int64_t time = data.ReadInt64();
83 auto apiVersion = data.ReadInt8();
84 if (apiVersion == APIVersion::API_VERSION_9) {
85 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
86 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
87 return E_TIME_NOT_SYSTEM_APP;
88 }
89 }
90 if (!TimePermission::CheckCallingPermission(TimePermission::SET_TIME)) {
91 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
92 return E_TIME_NO_PERMISSION;
93 }
94 int32_t ret = SetTime(time);
95 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
96 return ret;
97 }
98
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)99 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
100 {
101 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
102 std::string timeZoneId = data.ReadString();
103 auto apiVersion = data.ReadInt8();
104 if (apiVersion == APIVersion::API_VERSION_9) {
105 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
106 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
107 return E_TIME_NOT_SYSTEM_APP;
108 }
109 }
110 if (!TimePermission::CheckCallingPermission(TimePermission::SET_TIME_ZONE)) {
111 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
112 return E_TIME_NO_PERMISSION;
113 }
114 int32_t ret = SetTimeZone(timeZoneId);
115 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
116 return ret;
117 }
118
OnGetTimeZone(MessageParcel & data,MessageParcel & reply)119 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
120 {
121 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
122 std::string timeZoneId;
123 int32_t ret = GetTimeZone(timeZoneId);
124 if (ret != ERR_OK) {
125 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
126 return ret;
127 }
128 reply.WriteString(timeZoneId);
129 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
130 return ret;
131 }
132
OnGetWallTimeMs(MessageParcel & data,MessageParcel & reply)133 int32_t TimeServiceStub::OnGetWallTimeMs(MessageParcel &data, MessageParcel &reply)
134 {
135 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
136 int64_t times;
137 int32_t ret = GetWallTimeMs(times);
138 if (ret != ERR_OK) {
139 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
140 return ret;
141 }
142 reply.WriteInt64(times);
143 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
144 return ret;
145 }
146
OnGetWallTimeNs(MessageParcel & data,MessageParcel & reply)147 int32_t TimeServiceStub::OnGetWallTimeNs(MessageParcel &data, MessageParcel &reply)
148 {
149 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
150 int64_t times;
151 int32_t ret = GetWallTimeNs(times);
152 if (ret != ERR_OK) {
153 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
154 return ret;
155 }
156 reply.WriteInt64(times);
157 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
158 return ret;
159 }
160
OnGetBootTimeMs(MessageParcel & data,MessageParcel & reply)161 int32_t TimeServiceStub::OnGetBootTimeMs(MessageParcel &data, MessageParcel &reply)
162 {
163 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
164 int64_t times;
165 int32_t ret = GetBootTimeMs(times);
166 if (ret != ERR_OK) {
167 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
168 return ret;
169 }
170 reply.WriteInt64(times);
171 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
172 return ret;
173 }
174
OnGetBootTimeNs(MessageParcel & data,MessageParcel & reply)175 int32_t TimeServiceStub::OnGetBootTimeNs(MessageParcel &data, MessageParcel &reply)
176 {
177 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
178 int64_t times;
179 int32_t ret = GetBootTimeNs(times);
180 if (ret != ERR_OK) {
181 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
182 return ret;
183 }
184 reply.WriteInt64(times);
185 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
186 return ret;
187 }
188
OnGetMonotonicTimeMs(MessageParcel & data,MessageParcel & reply)189 int32_t TimeServiceStub::OnGetMonotonicTimeMs(MessageParcel &data, MessageParcel &reply)
190 {
191 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
192 int64_t times;
193 int32_t ret = GetMonotonicTimeMs(times);
194 if (ret != ERR_OK) {
195 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
196 return ret;
197 }
198 reply.WriteInt64(times);
199 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
200 return ret;
201 }
202
OnGetMonotonicTimeNs(MessageParcel & data,MessageParcel & reply)203 int32_t TimeServiceStub::OnGetMonotonicTimeNs(MessageParcel &data, MessageParcel &reply)
204 {
205 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
206 int64_t times;
207 int32_t ret = GetMonotonicTimeNs(times);
208 if (ret != ERR_OK) {
209 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
210 return ret;
211 }
212 reply.WriteInt64(times);
213 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
214 return ret;
215 }
216
OnGetThreadTimeMs(MessageParcel & data,MessageParcel & reply)217 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
218 {
219 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
220 int64_t times;
221 int32_t ret = GetThreadTimeMs(times);
222 if (ret != ERR_OK) {
223 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
224 return ret;
225 }
226 reply.WriteInt64(times);
227 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
228 return ret;
229 }
230
OnGetThreadTimeNs(MessageParcel & data,MessageParcel & reply)231 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
232 {
233 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
234 int64_t times;
235 int32_t ret = GetThreadTimeNs(times);
236 if (ret != ERR_OK) {
237 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
238 return ret;
239 }
240 reply.WriteInt64(times);
241 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
242 return ret;
243 }
244
OnCreateTimer(MessageParcel & data,MessageParcel & reply)245 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
246 {
247 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
248 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
249 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
250 return E_TIME_NOT_SYSTEM_APP;
251 }
252 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr };
253 auto type = data.ReadInt32();
254 auto repeat = data.ReadBool();
255 auto interval = data.ReadUint64();
256 if (data.ReadBool()) {
257 wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>(
258 data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>());
259 if (!wantAgent) {
260 TIME_HILOGI(TIME_MODULE_SERVICE, "Input wantagent nullptr");
261 return E_TIME_NULLPTR;
262 }
263 }
264 sptr<IRemoteObject> obj = data.ReadRemoteObject();
265 if (obj == nullptr) {
266 TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
267 return E_TIME_NULLPTR;
268 }
269 auto timerOptions = std::make_shared<SimpleTimerInfo>();
270 timerOptions->type = type;
271 timerOptions->repeat = repeat;
272 timerOptions->interval = interval;
273 timerOptions->wantAgent = wantAgent;
274 uint64_t timerId = 0;
275 auto errCode = CreateTimer(timerOptions, obj, timerId);
276 if (errCode != E_TIME_OK) {
277 TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
278 return E_TIME_DEAL_FAILED;
279 }
280 if (!reply.WriteUint64(timerId)) {
281 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
282 return E_TIME_WRITE_PARCEL_ERROR;
283 }
284 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
285 return ERR_OK;
286 }
287
OnStartTimer(MessageParcel & data,MessageParcel & reply)288 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
289 {
290 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
291 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
292 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
293 return E_TIME_NOT_SYSTEM_APP;
294 }
295 auto timerId = data.ReadUint64();
296 auto triggerTime = data.ReadUint64();
297 if (StartTimer(timerId, triggerTime) != E_TIME_OK) {
298 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
299 return E_TIME_DEAL_FAILED;
300 }
301 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
302 return ERR_OK;
303 }
304
OnStopTimer(MessageParcel & data,MessageParcel & reply)305 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
306 {
307 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
308 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
309 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
310 return E_TIME_NOT_SYSTEM_APP;
311 }
312 auto timerId = data.ReadUint64();
313 if (StopTimer(timerId) != E_TIME_OK) {
314 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
315 return E_TIME_DEAL_FAILED;
316 }
317 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
318 return ERR_OK;
319 }
320
OnDestroyTimer(MessageParcel & data,MessageParcel & reply)321 int32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply)
322 {
323 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
324 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
325 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
326 return E_TIME_NOT_SYSTEM_APP;
327 }
328 auto timerId = data.ReadUint64();
329 if (DestroyTimer(timerId) != E_TIME_OK) {
330 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
331 return E_TIME_DEAL_FAILED;
332 }
333 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
334 return ERR_OK;
335 }
336
OnTimerProxy(MessageParcel & data,MessageParcel & reply)337 int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply)
338 {
339 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
340 auto uid = data.ReadInt32();
341 if (uid == 0) {
342 TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid.");
343 return E_TIME_READ_PARCEL_ERROR;
344 }
345 auto isProxy = data.ReadBool();
346 auto needRetrigger = data.ReadBool();
347 if (!ProxyTimer(uid, isProxy, needRetrigger)) {
348 return E_TIME_DEAL_FAILED;
349 }
350 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
351 return ERR_OK;
352 }
353
OnAllProxyReset(MessageParcel & data,MessageParcel & reply)354 int32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply)
355 {
356 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
357 if (!ResetAllProxy()) {
358 return E_TIME_DEAL_FAILED;
359 }
360 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
361 return ERR_OK;
362 }
363 } // namespace MiscServices
364 } // namespace OHOS