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