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