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 "time_service_stub.h"
17
18 #include "simple_timer_info.h"
19 #include "time_common.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 using namespace OHOS::HiviewDFX;
24
TimeServiceStub()25 TimeServiceStub::TimeServiceStub()
26 {
27 memberFuncMap_[SET_TIME] = &TimeServiceStub::OnSetTime;
28 memberFuncMap_[SET_TIME_ZONE] = &TimeServiceStub::OnSetTimeZone;
29 memberFuncMap_[GET_TIME_ZONE] = &TimeServiceStub::OnGetTimeZone;
30 memberFuncMap_[GET_WALL_TIME_MILLI] = &TimeServiceStub::OnGetWallTimeMs;
31 memberFuncMap_[GET_WALL_TIME_NANO] = &TimeServiceStub::OnGetWallTimeNs;
32 memberFuncMap_[GET_BOOT_TIME_MILLI] = &TimeServiceStub::OnGetBootTimeMs;
33 memberFuncMap_[GET_BOOT_TIME_NANO] = &TimeServiceStub::OnGetBootTimeNs;
34 memberFuncMap_[GET_MONO_TIME_MILLI] = &TimeServiceStub::OnGetMonotonicTimeMs;
35 memberFuncMap_[GET_MONO_TIME_NANO] = &TimeServiceStub::OnGetMonotonicTimeNs;
36 memberFuncMap_[GET_THREAD_TIME_MILLI] = &TimeServiceStub::OnGetThreadTimeMs;
37 memberFuncMap_[GET_THREAD_TIME_NANO] = &TimeServiceStub::OnGetThreadTimeNs;
38 memberFuncMap_[CREATE_TIMER] = &TimeServiceStub::OnCreateTimer;
39 memberFuncMap_[START_TIMER] = &TimeServiceStub::OnStartTimer;
40 memberFuncMap_[STOP_TIMER] = &TimeServiceStub::OnStopTimer;
41 memberFuncMap_[DESTROY_TIMER] = &TimeServiceStub::OnDestroyTimer;
42 memberFuncMap_[PROXY_TIMER] = &TimeServiceStub::OnTimerProxy;
43 memberFuncMap_[RESET_ALL_PROXY] = &TimeServiceStub::OnAllProxyReset;
44 }
45
~TimeServiceStub()46 TimeServiceStub::~TimeServiceStub()
47 {
48 memberFuncMap_.clear();
49 }
50
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)51 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
52 MessageOption &option)
53 {
54 TIME_HILOGI(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
55 std::u16string myDescripter = TimeServiceStub::GetDescriptor();
56 std::u16string remoteDescripter = data.ReadInterfaceToken();
57 if (myDescripter != remoteDescripter) {
58 TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
59 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 pid_t p = IPCSkeleton::GetCallingPid();
62 pid_t p1 = IPCSkeleton::GetCallingUid();
63 TIME_HILOGI(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
64 code);
65 auto itFunc = memberFuncMap_.find(code);
66 if (itFunc != memberFuncMap_.end()) {
67 auto memberFunc = itFunc->second;
68 if (memberFunc != nullptr) {
69 return (this->*memberFunc)(data, reply);
70 }
71 }
72 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 TIME_HILOGI(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
74 return ret;
75 }
76
OnSetTime(MessageParcel & data,MessageParcel & reply)77 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
78 {
79 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
80 int64_t time = data.ReadInt64();
81 auto apiVersion = data.ReadInt8();
82 if (apiVersion == APIVersion::API_VERSION_7) {
83 if (!TimePermission::CheckCallingPermission(TimePermission::SET_TIME)) {
84 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
85 return E_TIME_NO_PERMISSION;
86 }
87 } else {
88 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
89 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
90 return E_TIME_NOT_SYSTEM_APP;
91 }
92 }
93 int32_t ret = SetTime(time);
94 TIME_HILOGI(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
95 return ret;
96 }
97
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)98 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
99 {
100 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
101 std::string timeZoneId = data.ReadString();
102 auto apiVersion = data.ReadInt8();
103 if (apiVersion == APIVersion::API_VERSION_7) {
104 if (!TimePermission::CheckCallingPermission(TimePermission::SET_TIME_ZONE)) {
105 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
106 return E_TIME_NO_PERMISSION;
107 }
108 } else {
109 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
110 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
111 return E_TIME_NOT_SYSTEM_APP;
112 }
113 }
114 int32_t ret = SetTimeZone(timeZoneId);
115 TIME_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(TIME_MODULE_SERVICE, "start.");
248 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
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_HILOGI(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_HILOGI(TIME_MODULE_SERVICE, "start.");
291 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
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_HILOGI(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_HILOGI(TIME_MODULE_SERVICE, "start.");
308 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
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_HILOGI(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_HILOGI(TIME_MODULE_SERVICE, "start.");
324 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingUid())) {
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_HILOGI(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_HILOGI(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_HILOGI(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_HILOGI(TIME_MODULE_SERVICE, "start.");
357 if (!ResetAllProxy()) {
358 return E_TIME_DEAL_FAILED;
359 }
360 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
361 return ERR_OK;
362 }
363 } // namespace MiscServices
364 } // namespace OHOS