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 #include "locator_proxy.h"
17
18 #include "ipc_skeleton.h"
19 #include "ipc_types.h"
20
21 #include "common_utils.h"
22
23 namespace OHOS {
24 namespace Location {
LocatorProxy(const sptr<IRemoteObject> & impl)25 LocatorProxy::LocatorProxy(const sptr<IRemoteObject> &impl)
26 : IRemoteProxy<ILocator>(impl)
27 {
28 }
29
GetSwitchState()30 int LocatorProxy::GetSwitchState()
31 {
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 return EXCEPTION;
37 }
38
39 sptr<IRemoteObject> remote = Remote();
40 if (remote == nullptr) {
41 LBSLOGE(LOCATOR_STANDARD, "GetSwitchState remote is null");
42 return EXCEPTION;
43 }
44 int error = remote->SendRequest(GET_SWITCH_STATE, data, reply, option);
45 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetSwitchState Transact ErrCode = %{public}d", error);
46 int state = 0;
47 if (error == NO_ERROR) {
48 state = reply.ReadInt32();
49 }
50 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetSwitchState return %{public}d", state);
51 return state;
52 }
53
EnableAbility(bool isEnabled)54 void LocatorProxy::EnableAbility(bool isEnabled)
55 {
56 MessageParcel data;
57 MessageParcel reply;
58 MessageOption option;
59 if (!data.WriteInterfaceToken(GetDescriptor())) {
60 return;
61 }
62 data.WriteBool(isEnabled);
63 sptr<IRemoteObject> remote = Remote();
64 if (remote == nullptr) {
65 LBSLOGE(LOCATOR_STANDARD, "EnableAbility remote is null");
66 return;
67 }
68 int error = remote->SendRequest(ENABLE_ABILITY, data, reply, option);
69 LBSLOGD(LOCATOR_STANDARD, "Proxy::EnableAbility Transact ErrCodes = %{public}d", error);
70 }
71
UpdateSaAbility()72 void LocatorProxy::UpdateSaAbility()
73 {
74 MessageParcel data;
75 MessageParcel reply;
76 MessageOption option;
77 if (!data.WriteInterfaceToken(GetDescriptor())) {
78 return;
79 }
80 sptr<IRemoteObject> remote = Remote();
81 if (remote == nullptr) {
82 LBSLOGE(LOCATOR_STANDARD, "UpdateSaAbility remote is null");
83 return;
84 }
85 int error = remote->SendRequest(UPDATE_SA_ABILITY, data, reply, option);
86 if (error != NO_ERROR) {
87 LBSLOGE(LOCATOR_STANDARD, "Proxy::UpdateSaAbility update sa fail, ErrCodes=%{public}d", error);
88 }
89 }
90
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)91 void LocatorProxy::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
92 {
93 LBSLOGD(LOCATOR_STANDARD, "uid is: %{public}d", uid);
94 MessageParcel data;
95 if (!data.WriteInterfaceToken(GetDescriptor())) {
96 return;
97 }
98 data.WriteObject<IRemoteObject>(callback);
99
100 MessageParcel reply;
101 MessageOption option;
102 sptr<IRemoteObject> remote = Remote();
103 if (remote == nullptr) {
104 LBSLOGE(LOCATOR_STANDARD, "RegisterSwitchCallback remote is null");
105 return;
106 }
107 int error = remote->SendRequest(REG_SWITCH_CALLBACK, data, reply, option);
108 LBSLOGD(LOCATOR_STANDARD, "Proxy::RegisterSwitchCallback Transact ErrCodes = %{public}d", error);
109 }
110
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)111 void LocatorProxy::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
112 {
113 MessageParcel data;
114 if (!data.WriteInterfaceToken(GetDescriptor())) {
115 return;
116 }
117 data.WriteObject<IRemoteObject>(callback);
118
119 MessageParcel reply;
120 MessageOption option;
121 sptr<IRemoteObject> remote = Remote();
122 if (remote == nullptr) {
123 LBSLOGE(LOCATOR_STANDARD, "UnregisterSwitchCallback remote is null");
124 return;
125 }
126 int error = remote->SendRequest(UNREG_SWITCH_CALLBACK, data, reply, option);
127 LBSLOGD(LOCATOR_STANDARD, "Proxy::UnregisterSwitchCallback Transact ErrCodes = %{public}d", error);
128 }
129
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)130 void LocatorProxy::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
131 {
132 LBSLOGD(LOCATOR_STANDARD, "uid is: %{public}d", uid);
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(GetDescriptor())) {
135 return;
136 }
137 data.WriteObject<IRemoteObject>(callback);
138
139 MessageParcel reply;
140 MessageOption option;
141 sptr<IRemoteObject> remote = Remote();
142 if (remote == nullptr) {
143 LBSLOGE(LOCATOR_STANDARD, "RegisterGnssStatusCallback remote is null");
144 return;
145 }
146 int error = remote->SendRequest(REG_GNSS_STATUS_CALLBACK, data, reply, option);
147 LBSLOGD(LOCATOR_STANDARD, "Proxy::RegisterGnssStatusCallback Transact ErrCodes = %{public}d", error);
148 }
149
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)150 void LocatorProxy::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
151 {
152 MessageParcel data;
153 if (!data.WriteInterfaceToken(GetDescriptor())) {
154 return;
155 }
156 data.WriteObject<IRemoteObject>(callback);
157
158 MessageParcel reply;
159 MessageOption option;
160 sptr<IRemoteObject> remote = Remote();
161 if (remote == nullptr) {
162 LBSLOGE(LOCATOR_STANDARD, "UnregisterGnssStatusCallback remote is null");
163 return;
164 }
165 int error = remote->SendRequest(UNREG_GNSS_STATUS_CALLBACK, data, reply, option);
166 LBSLOGD(LOCATOR_STANDARD, "Proxy::UnregisterGnssStatusCallback Transact ErrCodes = %{public}d", error);
167 }
168
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)169 void LocatorProxy::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
170 {
171 LBSLOGD(LOCATOR_STANDARD, "uid is: %{public}d", uid);
172 MessageParcel data;
173 if (!data.WriteInterfaceToken(GetDescriptor())) {
174 return;
175 }
176 data.WriteObject<IRemoteObject>(callback);
177
178 MessageParcel reply;
179 MessageOption option;
180 sptr<IRemoteObject> remote = Remote();
181 if (remote == nullptr) {
182 LBSLOGE(LOCATOR_STANDARD, "RegisterNmeaMessageCallback remote is null");
183 return;
184 }
185 int error = remote->SendRequest(REG_NMEA_CALLBACK, data, reply, option);
186 LBSLOGD(LOCATOR_STANDARD, "Proxy::RegisterNmeaMessageCallback Transact ErrCodes = %{public}d", error);
187 }
188
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)189 void LocatorProxy::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
190 {
191 MessageParcel data;
192 if (!data.WriteInterfaceToken(GetDescriptor())) {
193 return;
194 }
195 data.WriteObject<IRemoteObject>(callback);
196
197 MessageParcel reply;
198 MessageOption option;
199 sptr<IRemoteObject> remote = Remote();
200 if (remote == nullptr) {
201 LBSLOGE(LOCATOR_STANDARD, "UnregisterNmeaMessageCallback remote is null");
202 return;
203 }
204 int error = remote->SendRequest(UNREG_NMEA_CALLBACK, data, reply, option);
205 LBSLOGD(LOCATOR_STANDARD, "Proxy::UnregisterNmeaMessageCallback Transact ErrCodes = %{public}d", error);
206 }
207
StartLocating(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback,std::string bundleName,pid_t pid,pid_t uid)208 int LocatorProxy::StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
209 sptr<ILocatorCallback>& callback, std::string bundleName, pid_t pid, pid_t uid)
210 {
211 LBSLOGD(LOCATOR_STANDARD, "uid is: %{public}d, pid is: %{public}d", uid, pid);
212 MessageParcel data;
213 if (!data.WriteInterfaceToken(GetDescriptor())) {
214 return EXCEPTION;
215 }
216 if (requestConfig != nullptr) {
217 requestConfig->Marshalling(data);
218 }
219 if (callback != nullptr) {
220 data.WriteObject<IRemoteObject>(callback->AsObject());
221 }
222 data.WriteString16(Str8ToStr16(bundleName));
223
224 MessageParcel reply;
225 MessageOption option;
226 sptr<IRemoteObject> remote = Remote();
227 if (remote == nullptr) {
228 LBSLOGE(LOCATOR_STANDARD, "StartLocating remote is null");
229 return EXCEPTION;
230 }
231 int error = remote->SendRequest(START_LOCATING, data, reply, option);
232 LBSLOGD(LOCATOR_STANDARD, "Proxy::StartLocating Transact ErrCodes = %{public}d", error);
233 return error;
234 }
235
StopLocating(sptr<ILocatorCallback> & callback)236 int LocatorProxy::StopLocating(sptr<ILocatorCallback>& callback)
237 {
238 MessageParcel data;
239 if (!data.WriteInterfaceToken(GetDescriptor())) {
240 return EXCEPTION;
241 }
242 if (callback != nullptr) {
243 data.WriteObject<IRemoteObject>(callback->AsObject());
244 }
245
246 MessageParcel reply;
247 MessageOption option;
248 sptr<IRemoteObject> remote = Remote();
249 if (remote == nullptr) {
250 LBSLOGE(LOCATOR_STANDARD, "StopLocating remote is null");
251 return EXCEPTION;
252 }
253 int error = remote->SendRequest(STOP_LOCATING, data, reply, option);
254 LBSLOGD(LOCATOR_STANDARD, "Proxy::StopLocating Transact ErrCodes = %{public}d", error);
255 return error;
256 }
257
GetCacheLocation(MessageParcel & data,MessageParcel & reply)258 int LocatorProxy::GetCacheLocation(MessageParcel &data, MessageParcel &reply)
259 {
260 if (!data.WriteInterfaceToken(GetDescriptor())) {
261 return EXCEPTION;
262 }
263 MessageOption option;
264 sptr<IRemoteObject> remote = Remote();
265 if (remote == nullptr) {
266 LBSLOGE(LOCATOR_STANDARD, "GetCacheLocation remote is null");
267 return EXCEPTION;
268 }
269 int error = remote->SendRequest(GET_CACHE_LOCATION, data, reply, option);
270 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetCacheLocation Transact ErrCodes = %{public}d", error);
271 return error;
272 }
273
IsGeoConvertAvailable(MessageParcel & data,MessageParcel & reply)274 int LocatorProxy::IsGeoConvertAvailable(MessageParcel &data, MessageParcel &reply)
275 {
276 int error;
277 if (!data.WriteInterfaceToken(GetDescriptor())) {
278 return EXCEPTION;
279 }
280 MessageOption option;
281 sptr<IRemoteObject> remote = Remote();
282 if (remote == nullptr) {
283 LBSLOGE(LOCATOR_STANDARD, "IsGeoConvertAvailable remote is null");
284 return EXCEPTION;
285 }
286 error = remote->SendRequest(GEO_IS_AVAILABLE, data, reply, option);
287 LBSLOGD(LOCATOR_STANDARD, "Proxy::IsGeoConvertAvailable result from server");
288 return error;
289 }
290
GetAddressByCoordinate(MessageParcel & data,MessageParcel & reply)291 int LocatorProxy::GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply)
292 {
293 int error;
294 MessageOption option;
295 sptr<IRemoteObject> remote = Remote();
296 if (remote == nullptr) {
297 LBSLOGE(LOCATOR_STANDARD, "GetAddressByCoordinate remote is null");
298 return EXCEPTION;
299 }
300 error = remote->SendRequest(GET_FROM_COORDINATE, data, reply, option);
301 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetAddressByCoordinate result from server.");
302 return error;
303 }
304
GetAddressByLocationName(MessageParcel & data,MessageParcel & reply)305 int LocatorProxy::GetAddressByLocationName(MessageParcel &data, MessageParcel &reply)
306 {
307 int error;
308 MessageOption option;
309 sptr<IRemoteObject> remote = Remote();
310 if (remote == nullptr) {
311 LBSLOGE(LOCATOR_STANDARD, "GetAddressByLocationName remote is null");
312 return EXCEPTION;
313 }
314 error = remote->SendRequest(GET_FROM_LOCATION_NAME, data, reply, option);
315 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetAddressByLocationName result from server.");
316 return error;
317 }
318
IsLocationPrivacyConfirmed(const int type)319 bool LocatorProxy::IsLocationPrivacyConfirmed(const int type)
320 {
321 MessageParcel data;
322 MessageParcel reply;
323 MessageOption option;
324 if (!data.WriteInterfaceToken(GetDescriptor())) {
325 return EXCEPTION;
326 }
327
328 sptr<IRemoteObject> remote = Remote();
329 if (remote == nullptr) {
330 LBSLOGE(LOCATOR_STANDARD, "IsLocationPrivacyConfirmed remote is null");
331 return EXCEPTION;
332 }
333 data.WriteInt32(type);
334 int error = remote->SendRequest(IS_PRIVACY_COMFIRMED, data, reply, option);
335 LBSLOGD(LOCATOR_STANDARD, "Proxy::IsLocationPrivacyConfirmed Transact ErrCode = %{public}d", error);
336 bool state = false;
337 if (error == NO_ERROR) {
338 state = reply.ReadBool();
339 }
340 LBSLOGD(LOCATOR_STANDARD, "Proxy::IsLocationPrivacyConfirmed return %{public}d", state ? 1 : 0);
341 return state;
342 }
343
SetLocationPrivacyConfirmStatus(const int type,bool isConfirmed)344 void LocatorProxy::SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed)
345 {
346 MessageParcel data;
347 MessageParcel reply;
348 MessageOption option;
349 if (!data.WriteInterfaceToken(GetDescriptor())) {
350 return;
351 }
352 data.WriteInt32(type);
353 data.WriteBool(isConfirmed);
354 sptr<IRemoteObject> remote = Remote();
355 if (remote == nullptr) {
356 LBSLOGE(LOCATOR_STANDARD, "SetLocationPrivacyConfirmStatus remote is null");
357 return;
358 }
359 int error = remote->SendRequest(SET_PRIVACY_COMFIRM_STATUS, data, reply, option);
360 LBSLOGD(LOCATOR_STANDARD, "Proxy::SetLocationPrivacyConfirmStatus Transact ErrCodes = %{public}d", error);
361 }
362
RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback,std::string bundleName)363 int LocatorProxy::RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
364 sptr<ICachedLocationsCallback>& callback, std::string bundleName)
365 {
366 MessageParcel data;
367 if (!data.WriteInterfaceToken(GetDescriptor())) {
368 return EXCEPTION;
369 }
370 if (request != nullptr) {
371 data.WriteInt32(request->reportingPeriodSec);
372 data.WriteBool(request->wakeUpCacheQueueFull);
373 }
374 if (callback != nullptr) {
375 data.WriteRemoteObject(callback->AsObject());
376 }
377 data.WriteString16(Str8ToStr16(bundleName));
378
379 MessageParcel reply;
380 MessageOption option;
381 sptr<IRemoteObject> remote = Remote();
382 if (remote == nullptr) {
383 LBSLOGE(LOCATOR_STANDARD, "RegisterCachedLocationCallback remote is null");
384 return EXCEPTION;
385 }
386 int error = remote->SendRequest(REG_CACHED_CALLBACK, data, reply, option);
387 LBSLOGD(LOCATOR_STANDARD, "Proxy::RegisterCachedLocationCallback Transact ErrCodes = %{public}d", error);
388 return error;
389 }
390
UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback> & callback)391 int LocatorProxy::UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback)
392 {
393 MessageParcel data;
394 if (!data.WriteInterfaceToken(GetDescriptor())) {
395 return EXCEPTION;
396 }
397 if (callback != nullptr) {
398 data.WriteRemoteObject(callback->AsObject());
399 }
400
401 MessageParcel reply;
402 MessageOption option;
403 sptr<IRemoteObject> remote = Remote();
404 if (remote == nullptr) {
405 LBSLOGE(LOCATOR_STANDARD, "UnregisterCachedLocationCallback remote is null");
406 return EXCEPTION;
407 }
408 int error = remote->SendRequest(UNREG_CACHED_CALLBACK, data, reply, option);
409 LBSLOGD(LOCATOR_STANDARD, "Proxy::UnregisterCachedLocationCallback Transact ErrCodes = %{public}d", error);
410 return error;
411 }
412
GetCachedGnssLocationsSize()413 int LocatorProxy::GetCachedGnssLocationsSize()
414 {
415 MessageParcel data;
416 MessageParcel reply;
417 MessageOption option;
418 if (!data.WriteInterfaceToken(GetDescriptor())) {
419 return EXCEPTION;
420 }
421
422 sptr<IRemoteObject> remote = Remote();
423 if (remote == nullptr) {
424 LBSLOGE(LOCATOR_STANDARD, "GetCachedGnssLocationsSize remote is null");
425 return EXCEPTION;
426 }
427
428 int error = remote->SendRequest(GET_CACHED_LOCATION_SIZE, data, reply, option);
429 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetCachedGnssLocationsSize Transact ErrCode = %{public}d", error);
430 int size = 0;
431 if (error == NO_ERROR) {
432 size = reply.ReadInt32();
433 }
434 LBSLOGD(LOCATOR_STANDARD, "Proxy::GetCachedGnssLocationsSize return %{public}d", size);
435 return size;
436 }
437
FlushCachedGnssLocations()438 int LocatorProxy::FlushCachedGnssLocations()
439 {
440 MessageParcel data;
441 MessageParcel reply;
442 MessageOption option;
443 if (!data.WriteInterfaceToken(GetDescriptor())) {
444 return EXCEPTION;
445 }
446 sptr<IRemoteObject> remote = Remote();
447 if (remote == nullptr) {
448 LBSLOGE(LOCATOR_STANDARD, "FlushCachedGnssLocations remote is null");
449 return EXCEPTION;
450 }
451 int error = remote->SendRequest(FLUSH_CACHED_LOCATIONS, data, reply, option);
452 LBSLOGD(LOCATOR_STANDARD, "Proxy::FlushCachedGnssLocations Transact ErrCodes = %{public}d", error);
453 return error;
454 }
455
SendCommand(std::unique_ptr<LocationCommand> & commands)456 void LocatorProxy::SendCommand(std::unique_ptr<LocationCommand>& commands)
457 {
458 MessageParcel data;
459 MessageParcel reply;
460 MessageOption option;
461 if (!data.WriteInterfaceToken(GetDescriptor())) {
462 return;
463 }
464 data.WriteInt32(commands->scenario);
465 data.WriteString16(Str8ToStr16(commands->command));
466 sptr<IRemoteObject> remote = Remote();
467 if (remote == nullptr) {
468 LBSLOGE(LOCATOR_STANDARD, "SendCommand remote is null");
469 return;
470 }
471 int error = remote->SendRequest(SEND_COMMAND, data, reply, option);
472 LBSLOGD(LOCATOR_STANDARD, "Proxy::SendCommand Transact ErrCodes = %{public}d", error);
473 }
474
AddFence(std::unique_ptr<GeofenceRequest> & request)475 void LocatorProxy::AddFence(std::unique_ptr<GeofenceRequest>& request)
476 {
477 MessageParcel data;
478 MessageParcel reply;
479 MessageOption option;
480 if (!data.WriteInterfaceToken(GetDescriptor())) {
481 return;
482 }
483 data.WriteInt32(request->priority);
484 data.WriteInt32(request->scenario);
485 data.WriteDouble(request->geofence.latitude);
486 data.WriteDouble(request->geofence.longitude);
487 data.WriteDouble(request->geofence.radius);
488 data.WriteDouble(request->geofence.expiration);
489 sptr<IRemoteObject> remote = Remote();
490 if (remote == nullptr) {
491 LBSLOGE(LOCATOR_STANDARD, "AddFence remote is null");
492 return;
493 }
494 int error = remote->SendRequest(ADD_FENCE, data, reply, option);
495 LBSLOGD(LOCATOR_STANDARD, "Proxy::AddFence Transact ErrCodes = %{public}d", error);
496 }
497
RemoveFence(std::unique_ptr<GeofenceRequest> & request)498 void LocatorProxy::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
499 {
500 MessageParcel data;
501 MessageParcel reply;
502 MessageOption option;
503 if (!data.WriteInterfaceToken(GetDescriptor())) {
504 return;
505 }
506 data.WriteInt32(request->priority);
507 data.WriteInt32(request->scenario);
508 data.WriteDouble(request->geofence.latitude);
509 data.WriteDouble(request->geofence.longitude);
510 data.WriteDouble(request->geofence.radius);
511 data.WriteDouble(request->geofence.expiration);
512 sptr<IRemoteObject> remote = Remote();
513 if (remote == nullptr) {
514 LBSLOGE(LOCATOR_STANDARD, "RemoveFence remote is null");
515 return;
516 }
517 int error = remote->SendRequest(REMOVE_FENCE, data, reply, option);
518 LBSLOGD(LOCATOR_STANDARD, "Proxy::RemoveFence Transact ErrCodes = %{public}d", error);
519 }
520 } // namespace Location
521 } // namespace OHOS