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_common.h"
17 #include "time_service_stub.h"
18
19 namespace OHOS {
20 namespace MiscServices {
21 using namespace OHOS::HiviewDFX;
22
TimeServiceStub()23 TimeServiceStub::TimeServiceStub()
24 {
25 memberFuncMap_[SET_TIME] = &TimeServiceStub::OnSetTime;
26 memberFuncMap_[SET_TIME_ZONE] = &TimeServiceStub::OnSetTimeZone;
27 memberFuncMap_[GET_TIME_ZONE] = &TimeServiceStub::OnGetTimeZone;
28 memberFuncMap_[GET_WALL_TIME_MILLI] = &TimeServiceStub::OnGetWallTimeMs;
29 memberFuncMap_[GET_WALL_TIME_NANO] = &TimeServiceStub::OnGetWallTimeNs;
30
31 memberFuncMap_[GET_BOOT_TIME_MILLI] = &TimeServiceStub::OnGetBootTimeMs;
32 memberFuncMap_[GET_BOOT_TIME_NANO] = &TimeServiceStub::OnGetBootTimeNs;
33 memberFuncMap_[GET_MONO_TIME_MILLI] = &TimeServiceStub::OnGetMonotonicTimeMs;
34 memberFuncMap_[GET_MONO_TIME_NANO] = &TimeServiceStub::OnGetMonotonicTimeMs;
35 memberFuncMap_[GET_THREAD_TIME_MILLI] = &TimeServiceStub::OnGetThreadTimeMs;
36 memberFuncMap_[GET_THREAD_TIME_NANO] = &TimeServiceStub::OnGetThreadTimeNs;
37 memberFuncMap_[CREATE_TIMER] = &TimeServiceStub::OnCreateTimer;
38 memberFuncMap_[START_TIMER] = &TimeServiceStub::OnStartTimer;
39 memberFuncMap_[STOP_TIMER] = &TimeServiceStub::OnStopTimer;
40 memberFuncMap_[DESTORY_TIMER] = &TimeServiceStub::OnDestoryTimer;
41 memberFuncMap_[NETWORK_TIME_ON] = &TimeServiceStub::OnNetworkTimeStatusOn;
42 memberFuncMap_[NETWORK_TIME_OFF] = &TimeServiceStub::OnNetworkTimeStatusOff;
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_HILOGI(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
54 std::u16string myDescripter = TimeServiceStub::GetDescriptor();
55 std::u16string remoteDescripter = data.ReadInterfaceToken();
56 if (myDescripter != remoteDescripter) {
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_HILOGI(TIME_MODULE_SERVICE,
63 "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
64 auto itFunc = memberFuncMap_.find(code);
65 if (itFunc != memberFuncMap_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return (this->*memberFunc)(data, reply);
69 }
70 }
71 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 TIME_HILOGI(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
73 return ret;
74 }
75
OnSetTime(MessageParcel & data,MessageParcel & reply)76 int32_t TimeServiceStub::OnSetTime(MessageParcel& data, MessageParcel& reply)
77 {
78 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
79 int64_t time = data.ReadInt64();
80
81 int32_t ret = SetTime(time);
82 TIME_HILOGI(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
83 return ret;
84 }
85
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)86 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
87 {
88 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
89 std::string timeZoneId = data.ReadString();
90
91 int32_t ret = SetTimeZone(timeZoneId);
92 TIME_HILOGI(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
93 return ret;
94 }
95
OnGetTimeZone(MessageParcel & data,MessageParcel & reply)96 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
97 {
98 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
99 std::string timeZoneId;
100
101 int32_t ret = GetTimeZone(timeZoneId);
102 if (ret != ERR_OK) {
103 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
104 return ret;
105 }
106 reply.WriteString(timeZoneId);
107 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
108 return ret;
109 }
110
OnGetWallTimeMs(MessageParcel & data,MessageParcel & reply)111 int32_t TimeServiceStub::OnGetWallTimeMs(MessageParcel &data, MessageParcel &reply)
112 {
113 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
114 int64_t times;
115
116 int32_t ret = GetWallTimeMs(times);
117 if (ret != ERR_OK) {
118 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
119 return ret;
120 }
121 reply.WriteInt64(times);
122 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
123 return ret;
124 }
125
OnGetWallTimeNs(MessageParcel & data,MessageParcel & reply)126 int32_t TimeServiceStub::OnGetWallTimeNs(MessageParcel &data, MessageParcel &reply)
127 {
128 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
129 int64_t times;
130
131 int32_t ret = GetWallTimeNs(times);
132 if (ret != ERR_OK) {
133 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
134 return ret;
135 }
136 reply.WriteInt64(times);
137 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
138 return ret;
139 }
140
OnGetBootTimeMs(MessageParcel & data,MessageParcel & reply)141 int32_t TimeServiceStub::OnGetBootTimeMs(MessageParcel &data, MessageParcel &reply)
142 {
143 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
144 int64_t times;
145
146 int32_t ret = GetBootTimeMs(times);
147 if (ret != ERR_OK) {
148 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
149 return ret;
150 }
151 reply.WriteInt64(times);
152 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
153 return ret;
154 }
155
OnGetBootTimeNs(MessageParcel & data,MessageParcel & reply)156 int32_t TimeServiceStub::OnGetBootTimeNs(MessageParcel &data, MessageParcel &reply)
157 {
158 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
159 int64_t times;
160
161 int32_t ret = GetBootTimeNs(times);
162 if (ret != ERR_OK) {
163 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
164 return ret;
165 }
166 reply.WriteInt64(times);
167 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
168 return ret;
169 }
170
OnGetMonotonicTimeMs(MessageParcel & data,MessageParcel & reply)171 int32_t TimeServiceStub::OnGetMonotonicTimeMs(MessageParcel &data, MessageParcel &reply)
172 {
173 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
174 int64_t times;
175
176 int32_t ret = GetMonotonicTimeMs(times);
177 if (ret != ERR_OK) {
178 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
179 return ret;
180 }
181 reply.WriteInt64(times);
182 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
183 return ret;
184 }
185
OnGetMonotonicTimeNs(MessageParcel & data,MessageParcel & reply)186 int32_t TimeServiceStub::OnGetMonotonicTimeNs(MessageParcel &data, MessageParcel &reply)
187 {
188 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
189 int64_t times;
190
191 int32_t ret = GetMonotonicTimeNs(times);
192 if (ret != ERR_OK) {
193 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
194 return ret;
195 }
196 reply.WriteInt64(times);
197 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
198 return ret;
199 }
200
OnGetThreadTimeMs(MessageParcel & data,MessageParcel & reply)201 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
202 {
203 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
204 int64_t times;
205
206 int32_t ret = GetThreadTimeMs(times);
207 if (ret != ERR_OK) {
208 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
209 return ret;
210 }
211 reply.WriteInt64(times);
212 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
213 return ret;
214 }
215
OnGetThreadTimeNs(MessageParcel & data,MessageParcel & reply)216 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
217 {
218 TIME_HILOGI(TIME_MODULE_SERVICE, " start.");
219 int64_t times;
220 int32_t ret = GetThreadTimeNs(times);
221 if (ret != ERR_OK) {
222 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
223 return ret;
224 }
225 reply.WriteInt64(times);
226 TIME_HILOGI(TIME_MODULE_SERVICE, " end.");
227 return ret;
228 }
229
OnCreateTimer(MessageParcel & data,MessageParcel & reply)230 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
231 {
232 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
233
234 auto type = data.ReadInt32();
235 auto repeat = data.ReadBool();
236 auto interval = data.ReadUint64();
237
238 sptr<IRemoteObject> obj = data.ReadRemoteObject();
239 if (obj == nullptr) {
240 TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
241 return E_TIME_PARAMETERS_INVALID;
242 }
243 auto timerId = CreateTimer(type, repeat, interval, obj);
244 if (timerId == 0) {
245 TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
246 return E_TIME_DEAL_FAILED;
247 }
248 if (!reply.WriteUint64(timerId)) {
249 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write parcelable");
250 return E_TIME_WRITE_PARCEL_ERROR;
251 }
252 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
253 return ERR_OK;
254 }
255
OnStartTimer(MessageParcel & data,MessageParcel & reply)256 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
257 {
258 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
259 auto timerId = data.ReadUint64();
260 auto triggerTime = data.ReadUint64();
261 if (!StartTimer(timerId, triggerTime)) {
262 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
263 return E_TIME_DEAL_FAILED;
264 }
265 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
266 return ERR_OK;
267 }
268
OnStopTimer(MessageParcel & data,MessageParcel & reply)269 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
270 {
271 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
272 auto timerId = data.ReadUint64();
273 if (!StopTimer(timerId)) {
274 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
275 return E_TIME_DEAL_FAILED;
276 }
277 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
278 return ERR_OK;
279 }
280
OnDestoryTimer(MessageParcel & data,MessageParcel & reply)281 int32_t TimeServiceStub::OnDestoryTimer(MessageParcel &data, MessageParcel &reply)
282 {
283 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
284 auto timerId = data.ReadUint64();
285 if (!DestroyTimer(timerId)) {
286 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
287 return E_TIME_DEAL_FAILED;
288 }
289 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
290 return ERR_OK;
291 }
292
OnNetworkTimeStatusOff(MessageParcel & data,MessageParcel & reply)293 int32_t TimeServiceStub::OnNetworkTimeStatusOff(MessageParcel &data, MessageParcel &reply)
294 {
295 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
296 NetworkTimeStatusOff();
297 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
298 return ERR_OK;
299 }
300
OnNetworkTimeStatusOn(MessageParcel & data,MessageParcel & reply)301 int32_t TimeServiceStub::OnNetworkTimeStatusOn(MessageParcel &data, MessageParcel &reply)
302 {
303 TIME_HILOGI(TIME_MODULE_SERVICE, "start.");
304 NetworkTimeStatusOn();
305 TIME_HILOGI(TIME_MODULE_SERVICE, "end.");
306 return ERR_OK;
307 }
308 } // namespace MiscServices
309 } // namespace OHOS