• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
47     return LocationErrCode(reply.ReadInt32());
48 }
49 
SetEnable(bool state)50 LocationErrCode GnssAbilityProxy::SetEnable(bool state)
51 {
52     MessageParcel data;
53     if (!data.WriteInterfaceToken(GetDescriptor())) {
54         LBSLOGE(GNSS, "write interfaceToken fail!");
55         return ERRCODE_SERVICE_UNAVAILABLE;
56     }
57     data.WriteBool(state);
58 
59     MessageParcel reply;
60     MessageOption option;
61     int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SET_ENABLE), data, reply, option);
62     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
63     return LocationErrCode(reply.ReadInt32());
64 }
65 
RefrashRequirements()66 LocationErrCode GnssAbilityProxy::RefrashRequirements()
67 {
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option = { MessageOption::TF_ASYNC };
71     if (!data.WriteInterfaceToken(GetDescriptor())) {
72         LBSLOGE(GNSS, "write interfaceToken fail!");
73         return ERRCODE_SERVICE_UNAVAILABLE;
74     }
75     int error =
76         Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REFRESH_REQUESTS), data, reply, option);
77     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
78     return LocationErrCode(reply.ReadInt32());
79 }
80 
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)81 LocationErrCode GnssAbilityProxy::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
82 {
83     MessageParcel data;
84     MessageParcel reply;
85     MessageOption option = { MessageOption::TF_ASYNC };
86     if (!data.WriteInterfaceToken(GetDescriptor())) {
87         LBSLOGE(GNSS, "write interfaceToken fail!");
88         return ERRCODE_SERVICE_UNAVAILABLE;
89     }
90     data.WriteRemoteObject(callback);
91     int error =
92         Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_GNSS_STATUS), data, reply, option);
93     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
94     return LocationErrCode(reply.ReadInt32());
95 }
96 
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)97 LocationErrCode GnssAbilityProxy::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
98 {
99     MessageParcel data;
100     MessageParcel reply;
101     MessageOption option = { MessageOption::TF_ASYNC };
102     if (!data.WriteInterfaceToken(GetDescriptor())) {
103         LBSLOGE(GNSS, "write interfaceToken fail!");
104         return ERRCODE_SERVICE_UNAVAILABLE;
105     }
106     data.WriteRemoteObject(callback);
107     int error =
108         Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_GNSS_STATUS), data, reply, option);
109     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
110     return LocationErrCode(reply.ReadInt32());
111 }
112 
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)113 LocationErrCode GnssAbilityProxy::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
114 {
115     MessageParcel data;
116     MessageParcel reply;
117     MessageOption option = { MessageOption::TF_ASYNC };
118     if (!data.WriteInterfaceToken(GetDescriptor())) {
119         LBSLOGE(GNSS, "write interfaceToken fail!");
120         return ERRCODE_SERVICE_UNAVAILABLE;
121     }
122     data.WriteRemoteObject(callback);
123     int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_NMEA), data, reply, option);
124     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
125     return LocationErrCode(reply.ReadInt32());
126 }
127 
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)128 LocationErrCode GnssAbilityProxy::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
129 {
130     MessageParcel data;
131     MessageParcel reply;
132     MessageOption option = { MessageOption::TF_ASYNC };
133     if (!data.WriteInterfaceToken(GetDescriptor())) {
134         LBSLOGE(GNSS, "write interfaceToken fail!");
135         return ERRCODE_SERVICE_UNAVAILABLE;
136     }
137     data.WriteRemoteObject(callback);
138     int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_NMEA), data, reply, option);
139     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
140     return LocationErrCode(reply.ReadInt32());
141 }
142 
RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest> & request,const sptr<IRemoteObject> & callback)143 LocationErrCode GnssAbilityProxy::RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request,
144     const sptr<IRemoteObject>& callback)
145 {
146     MessageParcel data;
147     MessageParcel reply;
148     MessageOption option = { MessageOption::TF_ASYNC };
149     if (!data.WriteInterfaceToken(GetDescriptor())) {
150         LBSLOGE(GNSS, "write interfaceToken fail!");
151         return ERRCODE_SERVICE_UNAVAILABLE;
152     }
153     data.WriteInt32(request->reportingPeriodSec);
154     data.WriteBool(request->wakeUpCacheQueueFull);
155     data.WriteRemoteObject(callback);
156     int error = Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REG_CACHED), data, reply, option);
157     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
158     return LocationErrCode(reply.ReadInt32());
159 }
160 
UnregisterCachedCallback(const sptr<IRemoteObject> & callback)161 LocationErrCode GnssAbilityProxy::UnregisterCachedCallback(const sptr<IRemoteObject>& callback)
162 {
163     MessageParcel data;
164     MessageParcel reply;
165     MessageOption option = { MessageOption::TF_ASYNC };
166     if (!data.WriteInterfaceToken(GetDescriptor())) {
167         LBSLOGE(GNSS, "write interfaceToken fail!");
168         return ERRCODE_SERVICE_UNAVAILABLE;
169     }
170     data.WriteRemoteObject(callback);
171     int error =
172         Remote()->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::UNREG_CACHED), data, reply, option);
173     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
174     return LocationErrCode(reply.ReadInt32());
175 }
176 
GetCachedGnssLocationsSize(int & size)177 LocationErrCode GnssAbilityProxy::GetCachedGnssLocationsSize(int &size)
178 {
179     MessageParcel data;
180     MessageParcel reply;
181     MessageOption option;
182 
183     sptr<IRemoteObject> remote = Remote();
184     if (remote == nullptr) {
185         LBSLOGE(GNSS, "GetCachedGnssLocationsSize remote is null");
186         return ERRCODE_SERVICE_UNAVAILABLE;
187     }
188     if (!data.WriteInterfaceToken(GetDescriptor())) {
189         LBSLOGE(GNSS, "write interfaceToken fail!");
190         return ERRCODE_SERVICE_UNAVAILABLE;
191     }
192 
193     int error =
194         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::GET_CACHED_SIZE), data, reply, option);
195     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
196     LocationErrCode errorCode = LocationErrCode(reply.ReadInt32());
197     if (errorCode == ERRCODE_SUCCESS) {
198         size = reply.ReadInt32();
199     }
200     LBSLOGD(GNSS, "Proxy::%{public}s return size = %{public}d", __func__, size);
201     return errorCode;
202 }
203 
FlushCachedGnssLocations()204 LocationErrCode GnssAbilityProxy::FlushCachedGnssLocations()
205 {
206     MessageParcel data;
207     MessageParcel reply;
208     MessageOption option;
209     sptr<IRemoteObject> remote = Remote();
210     if (remote == nullptr) {
211         LBSLOGE(GNSS, "FlushCachedGnssLocations remote is null");
212         return ERRCODE_SERVICE_UNAVAILABLE;
213     }
214     if (!data.WriteInterfaceToken(GetDescriptor())) {
215         LBSLOGE(GNSS, "write interfaceToken fail!");
216         return ERRCODE_SERVICE_UNAVAILABLE;
217     }
218     int error = remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::FLUSH_CACHED), data, reply, option);
219     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
220     return LocationErrCode(reply.ReadInt32());
221 }
222 
SendCommand(std::unique_ptr<LocationCommand> & commands)223 LocationErrCode GnssAbilityProxy::SendCommand(std::unique_ptr<LocationCommand>& commands)
224 {
225     MessageParcel data;
226     MessageParcel reply;
227     MessageOption option;
228     if (!data.WriteInterfaceToken(GetDescriptor())) {
229         LBSLOGE(GNSS, "write interfaceToken fail!");
230         return ERRCODE_SERVICE_UNAVAILABLE;
231     }
232     data.WriteInt32(commands->scenario);
233     data.WriteString16(Str8ToStr16(commands->command));
234     sptr<IRemoteObject> remote = Remote();
235     if (remote == nullptr) {
236         LBSLOGE(GNSS, "SendCommand remote is null");
237         return ERRCODE_SERVICE_UNAVAILABLE;
238     }
239     int error = remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SEND_COMMANDS), data, reply, option);
240     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
241     return LocationErrCode(reply.ReadInt32());
242 }
243 
AddFence(std::unique_ptr<GeofenceRequest> & request)244 LocationErrCode GnssAbilityProxy::AddFence(std::unique_ptr<GeofenceRequest>& request)
245 {
246     MessageParcel data;
247     MessageParcel reply;
248     MessageOption option;
249     if (!data.WriteInterfaceToken(GetDescriptor())) {
250         LBSLOGE(GNSS, "write interfaceToken fail!");
251         return ERRCODE_SERVICE_UNAVAILABLE;
252     }
253     data.WriteInt32(request->scenario);
254     data.WriteDouble(request->geofence.latitude);
255     data.WriteDouble(request->geofence.longitude);
256     data.WriteDouble(request->geofence.radius);
257     data.WriteDouble(request->geofence.expiration);
258     sptr<IRemoteObject> remote = Remote();
259     if (remote == nullptr) {
260         LBSLOGE(GNSS, "AddFence remote is null");
261         return ERRCODE_SERVICE_UNAVAILABLE;
262     }
263     int error =
264         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::ADD_FENCE_INFO), data, reply, option);
265     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
266     return LocationErrCode(reply.ReadInt32());
267 }
268 
RemoveFence(std::unique_ptr<GeofenceRequest> & request)269 LocationErrCode GnssAbilityProxy::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
270 {
271     MessageParcel data;
272     MessageParcel reply;
273     MessageOption option;
274     if (!data.WriteInterfaceToken(GetDescriptor())) {
275         LBSLOGE(GNSS, "write interfaceToken fail!");
276         return ERRCODE_SERVICE_UNAVAILABLE;
277     }
278     data.WriteInt32(request->scenario);
279     data.WriteDouble(request->geofence.latitude);
280     data.WriteDouble(request->geofence.longitude);
281     data.WriteDouble(request->geofence.radius);
282     data.WriteDouble(request->geofence.expiration);
283     sptr<IRemoteObject> remote = Remote();
284     if (remote == nullptr) {
285         LBSLOGE(GNSS, "RemoveFence remote is null");
286         return ERRCODE_SERVICE_UNAVAILABLE;
287     }
288     int error =
289         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::REMOVE_FENCE_INFO), data, reply, option);
290     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
291     return LocationErrCode(reply.ReadInt32());
292 }
293 
EnableMock()294 LocationErrCode GnssAbilityProxy::EnableMock()
295 {
296     MessageParcel data;
297     MessageParcel reply;
298     MessageOption option;
299     sptr<IRemoteObject> remote = Remote();
300     if (remote == nullptr) {
301         LBSLOGE(GNSS, "EnableLocationMock remote is null");
302         return ERRCODE_SERVICE_UNAVAILABLE;
303     }
304     if (!data.WriteInterfaceToken(GetDescriptor())) {
305         LBSLOGE(GNSS, "write interfaceToken fail!");
306         return ERRCODE_SERVICE_UNAVAILABLE;
307     }
308     int error =
309         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::ENABLE_LOCATION_MOCK), data, reply, option);
310     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
311     return LocationErrCode(reply.ReadInt32());
312 }
313 
DisableMock()314 LocationErrCode GnssAbilityProxy::DisableMock()
315 {
316     MessageParcel data;
317     MessageParcel reply;
318     MessageOption option;
319     sptr<IRemoteObject> remote = Remote();
320     if (remote == nullptr) {
321         LBSLOGE(GNSS, "DisableLocationMock remote is null");
322         return ERRCODE_SERVICE_UNAVAILABLE;
323     }
324     if (!data.WriteInterfaceToken(GetDescriptor())) {
325         LBSLOGE(GNSS, "write interfaceToken fail!");
326         return ERRCODE_SERVICE_UNAVAILABLE;
327     }
328     int error =
329         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option);
330     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
331     return LocationErrCode(reply.ReadInt32());
332 }
333 
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)334 LocationErrCode GnssAbilityProxy::SetMocked(
335     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
336 {
337     MessageParcel data;
338     MessageParcel reply;
339     MessageOption option;
340     sptr<IRemoteObject> remote = Remote();
341     if (remote == nullptr) {
342         LBSLOGE(GNSS, "SetMockedLocations remote is null");
343         return ERRCODE_SERVICE_UNAVAILABLE;
344     }
345     if (!data.WriteInterfaceToken(GetDescriptor())) {
346         LBSLOGE(GNSS, "write interfaceToken fail!");
347         return ERRCODE_SERVICE_UNAVAILABLE;
348     }
349     data.WriteInt32(timeInterval);
350     int locationSize = static_cast<int>(location.size());
351     data.WriteInt32(locationSize);
352     for (int i = 0; i < locationSize; i++) {
353         location.at(i)->Marshalling(data);
354     }
355     int error =
356         remote->SendRequest(static_cast<uint32_t>(GnssInterfaceCode::SET_MOCKED_LOCATIONS), data, reply, option);
357     LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error);
358     return LocationErrCode(reply.ReadInt32());
359 }
360 } // namespace Location
361 } // namespace OHOS
362 #endif
363