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