1 /*
2 * Copyright (C) 2022 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 #ifdef FEATURE_GNSS_SUPPORT
17 #include "gnss_ability_proxy.h"
18
19 #include "string_ex.h"
20
21 #include "common_utils.h"
22 #include "location_log.h"
23 #include "locationhub_ipc_interface_code.h"
24
25 namespace OHOS {
26 namespace Location {
GnssAbilityProxy(const sptr<IRemoteObject> & impl)27 GnssAbilityProxy::GnssAbilityProxy(const sptr<IRemoteObject> &impl)
28 : IRemoteProxy<IGnssAbility>(impl)
29 {
30 }
31
SendLocationRequest(WorkRecord & workrecord)32 LocationErrCode GnssAbilityProxy::SendLocationRequest(WorkRecord &workrecord)
33 {
34 MessageParcel data;
35 MessageParcel reply;
36 MessageOption option;
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 LBSLOGE(GNSS, "write interfaceToken fail!");
39 return ERRCODE_SERVICE_UNAVAILABLE;
40 }
41 workrecord.Marshalling(data);
42 int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SEND_LOCATION_REQUEST),
43 data,
44 reply,
45 option);
46 if (error != ERR_OK) {
47 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
48 }
49 return LocationErrCode(reply.ReadInt32());
50 }
51
SetEnable(bool state)52 LocationErrCode GnssAbilityProxy::SetEnable(bool state)
53 {
54 MessageParcel data;
55 if (!data.WriteInterfaceToken(GetDescriptor())) {
56 LBSLOGE(GNSS, "write interfaceToken fail!");
57 return ERRCODE_SERVICE_UNAVAILABLE;
58 }
59 data.WriteBool(state);
60
61 MessageParcel reply;
62 MessageOption option;
63 int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SET_ENABLE), data, reply, option);
64 if (error != ERR_OK) {
65 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
66 }
67 return LocationErrCode(reply.ReadInt32());
68 }
69
RefrashRequirements()70 LocationErrCode GnssAbilityProxy::RefrashRequirements()
71 {
72 MessageParcel data;
73 MessageParcel reply;
74 MessageOption option = { MessageOption::TF_ASYNC };
75 if (!data.WriteInterfaceToken(GetDescriptor())) {
76 LBSLOGE(GNSS, "write interfaceToken fail!");
77 return ERRCODE_SERVICE_UNAVAILABLE;
78 }
79 int error =
80 Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REFRESH_REQUESTS), data, reply, option);
81 if (error != ERR_OK) {
82 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
83 }
84 return LocationErrCode(reply.ReadInt32());
85 }
86
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)87 LocationErrCode GnssAbilityProxy::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option = { MessageOption::TF_ASYNC };
92 if (!data.WriteInterfaceToken(GetDescriptor())) {
93 LBSLOGE(GNSS, "write interfaceToken fail!");
94 return ERRCODE_SERVICE_UNAVAILABLE;
95 }
96 data.WriteRemoteObject(callback);
97 int error =
98 Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_GNSS_STATUS), data, reply, option);
99 if (error != ERR_OK) {
100 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
101 }
102 return LocationErrCode(reply.ReadInt32());
103 }
104
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)105 LocationErrCode GnssAbilityProxy::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
106 {
107 MessageParcel data;
108 MessageParcel reply;
109 MessageOption option = { MessageOption::TF_ASYNC };
110 if (!data.WriteInterfaceToken(GetDescriptor())) {
111 LBSLOGE(GNSS, "write interfaceToken fail!");
112 return ERRCODE_SERVICE_UNAVAILABLE;
113 }
114 data.WriteRemoteObject(callback);
115 int error =
116 Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_GNSS_STATUS), data, reply, option);
117 if (error != ERR_OK) {
118 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
119 }
120 return LocationErrCode(reply.ReadInt32());
121 }
122
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)123 LocationErrCode GnssAbilityProxy::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
124 {
125 MessageParcel data;
126 MessageParcel reply;
127 MessageOption option = { MessageOption::TF_ASYNC };
128 if (!data.WriteInterfaceToken(GetDescriptor())) {
129 LBSLOGE(GNSS, "write interfaceToken fail!");
130 return ERRCODE_SERVICE_UNAVAILABLE;
131 }
132 data.WriteRemoteObject(callback);
133 int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_NMEA), data, reply, option);
134 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
135 return LocationErrCode(reply.ReadInt32());
136 }
137
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)138 LocationErrCode GnssAbilityProxy::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
139 {
140 MessageParcel data;
141 MessageParcel reply;
142 MessageOption option = { MessageOption::TF_ASYNC };
143 if (!data.WriteInterfaceToken(GetDescriptor())) {
144 LBSLOGE(GNSS, "write interfaceToken fail!");
145 return ERRCODE_SERVICE_UNAVAILABLE;
146 }
147 data.WriteRemoteObject(callback);
148 int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_NMEA), data, reply, option);
149 if (error != ERR_OK) {
150 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
151 }
152 return LocationErrCode(reply.ReadInt32());
153 }
154
RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest> & request,const sptr<IRemoteObject> & callback)155 LocationErrCode GnssAbilityProxy::RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request,
156 const sptr<IRemoteObject>& callback)
157 {
158 MessageParcel data;
159 MessageParcel reply;
160 MessageOption option = { MessageOption::TF_ASYNC };
161 if (!data.WriteInterfaceToken(GetDescriptor())) {
162 LBSLOGE(GNSS, "write interfaceToken fail!");
163 return ERRCODE_SERVICE_UNAVAILABLE;
164 }
165 data.WriteInt32(request->reportingPeriodSec);
166 data.WriteBool(request->wakeUpCacheQueueFull);
167 data.WriteRemoteObject(callback);
168 int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_CACHED), data, reply, option);
169 if (error != ERR_OK) {
170 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
171 }
172 return LocationErrCode(reply.ReadInt32());
173 }
174
UnregisterCachedCallback(const sptr<IRemoteObject> & callback)175 LocationErrCode GnssAbilityProxy::UnregisterCachedCallback(const sptr<IRemoteObject>& callback)
176 {
177 MessageParcel data;
178 MessageParcel reply;
179 MessageOption option = { MessageOption::TF_ASYNC };
180 if (!data.WriteInterfaceToken(GetDescriptor())) {
181 LBSLOGE(GNSS, "write interfaceToken fail!");
182 return ERRCODE_SERVICE_UNAVAILABLE;
183 }
184 data.WriteRemoteObject(callback);
185 int error =
186 Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_CACHED), data, reply, option);
187 if (error != ERR_OK) {
188 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
189 }
190 return LocationErrCode(reply.ReadInt32());
191 }
192
GetCachedGnssLocationsSize(int & size)193 LocationErrCode GnssAbilityProxy::GetCachedGnssLocationsSize(int &size)
194 {
195 MessageParcel data;
196 MessageParcel reply;
197 MessageOption option;
198
199 sptr<IRemoteObject> remote = Remote();
200 if (remote == nullptr) {
201 LBSLOGE(GNSS, "GetCachedGnssLocationsSize remote is null");
202 return ERRCODE_SERVICE_UNAVAILABLE;
203 }
204 if (!data.WriteInterfaceToken(GetDescriptor())) {
205 LBSLOGE(GNSS, "write interfaceToken fail!");
206 return ERRCODE_SERVICE_UNAVAILABLE;
207 }
208
209 int error =
210 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::GET_CACHED_SIZE), data, reply, option);
211 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
212 LocationErrCode errorCode = LocationErrCode(reply.ReadInt32());
213 if (errorCode == ERRCODE_SUCCESS) {
214 size = reply.ReadInt32();
215 }
216 LBSLOGD(GNSS, "Proxy::%{public}s return size = %{public}d", __func__, size);
217 return errorCode;
218 }
219
FlushCachedGnssLocations()220 LocationErrCode GnssAbilityProxy::FlushCachedGnssLocations()
221 {
222 MessageParcel data;
223 MessageParcel reply;
224 MessageOption option;
225 sptr<IRemoteObject> remote = Remote();
226 if (remote == nullptr) {
227 LBSLOGE(GNSS, "FlushCachedGnssLocations remote is null");
228 return ERRCODE_SERVICE_UNAVAILABLE;
229 }
230 if (!data.WriteInterfaceToken(GetDescriptor())) {
231 LBSLOGE(GNSS, "write interfaceToken fail!");
232 return ERRCODE_SERVICE_UNAVAILABLE;
233 }
234 int error = remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::FLUSH_CACHED), data, reply, option);
235 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
236 return LocationErrCode(reply.ReadInt32());
237 }
238
SendCommand(std::unique_ptr<LocationCommand> & commands)239 LocationErrCode GnssAbilityProxy::SendCommand(std::unique_ptr<LocationCommand>& commands)
240 {
241 MessageParcel data;
242 MessageParcel reply;
243 MessageOption option;
244 if (!data.WriteInterfaceToken(GetDescriptor())) {
245 LBSLOGE(GNSS, "write interfaceToken fail!");
246 return ERRCODE_SERVICE_UNAVAILABLE;
247 }
248 data.WriteInt32(commands->scenario);
249 data.WriteString16(Str8ToStr16(commands->command));
250 sptr<IRemoteObject> remote = Remote();
251 if (remote == nullptr) {
252 LBSLOGE(GNSS, "SendCommand remote is null");
253 return ERRCODE_SERVICE_UNAVAILABLE;
254 }
255 int error = remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SEND_COMMANDS), data, reply, option);
256 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
257 return LocationErrCode(reply.ReadInt32());
258 }
259
AddFence(std::shared_ptr<GeofenceRequest> & request)260 LocationErrCode GnssAbilityProxy::AddFence(std::shared_ptr<GeofenceRequest>& request)
261 {
262 MessageParcel data;
263 MessageParcel reply;
264 MessageOption option;
265 if (!data.WriteInterfaceToken(GetDescriptor())) {
266 LBSLOGE(GNSS, "write interfaceToken fail!");
267 return ERRCODE_SERVICE_UNAVAILABLE;
268 }
269 request->Marshalling(data);
270 sptr<IRemoteObject> remote = Remote();
271 if (remote == nullptr) {
272 LBSLOGE(GNSS, "AddFence remote is null");
273 return ERRCODE_SERVICE_UNAVAILABLE;
274 }
275 int error =
276 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::ADD_FENCE_INFO), data, reply, option);
277 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
278 return LocationErrCode(reply.ReadInt32());
279 }
280
RemoveFence(std::shared_ptr<GeofenceRequest> & request)281 LocationErrCode GnssAbilityProxy::RemoveFence(std::shared_ptr<GeofenceRequest>& request)
282 {
283 MessageParcel data;
284 MessageParcel reply;
285 MessageOption option;
286 if (!data.WriteInterfaceToken(GetDescriptor())) {
287 LBSLOGE(GNSS, "write interfaceToken fail!");
288 return ERRCODE_SERVICE_UNAVAILABLE;
289 }
290 request->Marshalling(data);
291 sptr<IRemoteObject> remote = Remote();
292 if (remote == nullptr) {
293 LBSLOGE(GNSS, "RemoveFence remote is null");
294 return ERRCODE_SERVICE_UNAVAILABLE;
295 }
296 int error =
297 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REMOVE_FENCE_INFO), data, reply, option);
298 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
299 return LocationErrCode(reply.ReadInt32());
300 }
301
AddGnssGeofence(std::shared_ptr<GeofenceRequest> & request)302 LocationErrCode GnssAbilityProxy::AddGnssGeofence(
303 std::shared_ptr<GeofenceRequest>& request)
304 {
305 MessageParcel dataToStub;
306 MessageParcel replyToStub;
307 MessageOption option;
308 if (!dataToStub.WriteInterfaceToken(GetDescriptor())) {
309 return ERRCODE_SERVICE_UNAVAILABLE;
310 }
311 if (request == nullptr) {
312 LBSLOGE(GNSS, "request is nullptr");
313 return ERRCODE_SERVICE_UNAVAILABLE;
314 }
315 request->Marshalling(dataToStub);
316 sptr<IRemoteObject> remote = Remote();
317 if (remote == nullptr) {
318 LBSLOGE(GNSS, "RemoveFence remote is null");
319 return ERRCODE_SERVICE_UNAVAILABLE;
320 }
321 int error = remote->SendRequest(
322 static_cast<uint32_t>(GnssInterfaceCode::ADD_GNSS_GEOFENCE), dataToStub, replyToStub, option);
323 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
324 return LocationErrCode(replyToStub.ReadInt32());
325 }
326
RemoveGnssGeofence(std::shared_ptr<GeofenceRequest> & request)327 LocationErrCode GnssAbilityProxy::RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request)
328 {
329 MessageParcel dataToStub;
330 MessageParcel replyToStub;
331 MessageOption option;
332 if (!dataToStub.WriteInterfaceToken(GetDescriptor())) {
333 return ERRCODE_SERVICE_UNAVAILABLE;
334 }
335 dataToStub.WriteInt32(request->GetFenceId());
336 dataToStub.WriteString("");
337 sptr<IRemoteObject> remote = Remote();
338 if (remote == nullptr) {
339 LBSLOGE(GNSS, "RemoveFence remote is null");
340 return ERRCODE_SERVICE_UNAVAILABLE;
341 }
342 int error = remote->SendRequest(
343 static_cast<uint32_t>(GnssInterfaceCode::REMOVE_GNSS_GEOFENCE), dataToStub, replyToStub, option);
344 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
345 return LocationErrCode(replyToStub.ReadInt32());
346 }
347
EnableMock()348 LocationErrCode GnssAbilityProxy::EnableMock()
349 {
350 MessageParcel data;
351 MessageParcel reply;
352 MessageOption option;
353 sptr<IRemoteObject> remote = Remote();
354 if (remote == nullptr) {
355 LBSLOGE(GNSS, "EnableLocationMock remote is null");
356 return ERRCODE_SERVICE_UNAVAILABLE;
357 }
358 if (!data.WriteInterfaceToken(GetDescriptor())) {
359 LBSLOGE(GNSS, "write interfaceToken fail!");
360 return ERRCODE_SERVICE_UNAVAILABLE;
361 }
362 int error =
363 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::ENABLE_LOCATION_MOCK), data, reply, option);
364 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
365 return LocationErrCode(reply.ReadInt32());
366 }
367
DisableMock()368 LocationErrCode GnssAbilityProxy::DisableMock()
369 {
370 MessageParcel data;
371 MessageParcel reply;
372 MessageOption option;
373 sptr<IRemoteObject> remote = Remote();
374 if (remote == nullptr) {
375 LBSLOGE(GNSS, "DisableLocationMock remote is null");
376 return ERRCODE_SERVICE_UNAVAILABLE;
377 }
378 if (!data.WriteInterfaceToken(GetDescriptor())) {
379 LBSLOGE(GNSS, "write interfaceToken fail!");
380 return ERRCODE_SERVICE_UNAVAILABLE;
381 }
382 int error =
383 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option);
384 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
385 return LocationErrCode(reply.ReadInt32());
386 }
387
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)388 LocationErrCode GnssAbilityProxy::SetMocked(
389 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
390 {
391 MessageParcel data;
392 MessageParcel reply;
393 MessageOption option;
394 sptr<IRemoteObject> remote = Remote();
395 if (remote == nullptr) {
396 LBSLOGE(GNSS, "SetMockedLocations remote is null");
397 return ERRCODE_SERVICE_UNAVAILABLE;
398 }
399 if (!data.WriteInterfaceToken(GetDescriptor())) {
400 LBSLOGE(GNSS, "write interfaceToken fail!");
401 return ERRCODE_SERVICE_UNAVAILABLE;
402 }
403 data.WriteInt32(timeInterval);
404 int locationSize = static_cast<int>(location.size());
405 data.WriteInt32(locationSize);
406 for (int i = 0; i < locationSize; i++) {
407 location.at(i)->Marshalling(data);
408 }
409 int error =
410 remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SET_MOCKED_LOCATIONS), data, reply, option);
411 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
412 return LocationErrCode(reply.ReadInt32());
413 }
414
QuerySupportCoordinateSystemType(std::vector<CoordinateSystemType> & coordinateSystemTypes)415 LocationErrCode GnssAbilityProxy::QuerySupportCoordinateSystemType(
416 std::vector<CoordinateSystemType>& coordinateSystemTypes)
417 {
418 MessageParcel data;
419 MessageParcel reply;
420 MessageOption option;
421 sptr<IRemoteObject> remote = Remote();
422 if (remote == nullptr) {
423 LBSLOGE(GNSS, "QuerySupportCoordinateSystemType remote is null");
424 return ERRCODE_SERVICE_UNAVAILABLE;
425 }
426 if (!data.WriteInterfaceToken(GetDescriptor())) {
427 LBSLOGE(GNSS, "write interfaceToken fail!");
428 return ERRCODE_SERVICE_UNAVAILABLE;
429 }
430 int error = remote->SendRequest(
431 static_cast<uint32_t>(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply, option);
432 LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
433 auto errCode = reply.ReadInt32();
434 if (errCode == ERRCODE_SUCCESS) {
435 auto size = reply.ReadInt32();
436 size = size > MAXIMUM_INTERATION ? MAXIMUM_INTERATION : size;
437 for (int i = 0; i < size; i++) {
438 coordinateSystemTypes.push_back(static_cast<CoordinateSystemType>(reply.ReadInt32()));
439 }
440 }
441 return LocationErrCode(errCode);
442 }
443
SendNetworkLocation(const std::unique_ptr<Location> & location)444 LocationErrCode GnssAbilityProxy::SendNetworkLocation(const std::unique_ptr<Location>& location)
445 {
446 MessageParcel dataToStub;
447 MessageParcel replyToStub;
448 MessageOption option;
449 if (!dataToStub.WriteInterfaceToken(GetDescriptor())) {
450 return ERRCODE_SERVICE_UNAVAILABLE;
451 }
452 location->Marshalling(dataToStub);
453 sptr<IRemoteObject> remote = Remote();
454 if (remote == nullptr) {
455 LBSLOGE(GNSS, "SendNetworkLocation remote is null");
456 return ERRCODE_SERVICE_UNAVAILABLE;
457 }
458 int error = remote->SendRequest(
459 static_cast<uint32_t>(GnssInterfaceCode::SEND_NETWORK_LOCATION), dataToStub, replyToStub, option);
460 LBSLOGI(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
461 return LocationErrCode(replyToStub.ReadInt32());
462 }
463 } // namespace Location
464 } // namespace OHOS
465 #endif
466