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 #include "ipc_skeleton.h"
18 #include "location_log.h"
19
20 namespace OHOS {
21 namespace Location {
GnssAbilityProxy(const sptr<IRemoteObject> & impl)22 GnssAbilityProxy::GnssAbilityProxy(const sptr<IRemoteObject> &impl)
23 : IRemoteProxy<IGnssAbility>(impl)
24 {
25 }
26
SendLocationRequest(uint64_t interval,WorkRecord & workrecord)27 void GnssAbilityProxy::SendLocationRequest(uint64_t interval, WorkRecord &workrecord)
28 {
29 MessageParcel data;
30 MessageParcel reply;
31 MessageOption option;
32 if (!data.WriteInterfaceToken(GetDescriptor())) {
33 LBSLOGE(GNSS, "write interfaceToken fail!");
34 return;
35 }
36 data.WriteInt64(interval);
37 workrecord.Marshalling(data);
38 int error = Remote()->SendRequest(ISubAbility::SEND_LOCATION_REQUEST, data, reply, option);
39 LBSLOGD(GNSS, "RemoteRequest Transact ErrCode = %{public}d", error);
40 }
41
GetCachedLocation()42 std::unique_ptr<Location> GnssAbilityProxy::GetCachedLocation()
43 {
44 MessageParcel data;
45 MessageParcel reply;
46 MessageOption option;
47 if (!data.WriteInterfaceToken(GetDescriptor())) {
48 LBSLOGE(GNSS, "write interfaceToken fail!");
49 return nullptr;
50 }
51 int error = Remote()->SendRequest(ISubAbility::GET_CACHED_LOCATION, data, reply, option);
52 std::unique_ptr<Location> location = Location::Unmarshalling(reply);
53 LBSLOGD(GNSS, "GetCache Transact ErrCode = %{public}d", error);
54 return location;
55 }
56
SetEnable(bool state)57 void GnssAbilityProxy::SetEnable(bool state)
58 {
59 MessageParcel data;
60 if (!data.WriteInterfaceToken(GetDescriptor())) {
61 LBSLOGE(GNSS, "write interfaceToken fail!");
62 return;
63 }
64 data.WriteBool(state);
65
66 MessageParcel reply;
67 MessageOption option;
68 int error = Remote()->SendRequest(ISubAbility::SET_ENABLE, data, reply, option);
69 LBSLOGD(GNSS, "Enable Transact ErrCode = %{public}d", error);
70 }
71
RemoteRequest(bool state)72 void GnssAbilityProxy::RemoteRequest(bool state)
73 {
74 MessageParcel data;
75 if (!data.WriteInterfaceToken(GetDescriptor())) {
76 LBSLOGE(GNSS, "write interfaceToken fail!");
77 return;
78 }
79 data.WriteBool(state);
80
81 MessageParcel reply;
82 MessageOption option = { MessageOption::TF_ASYNC };
83 int error = Remote()->SendRequest(ISubAbility::HANDLE_REMOTE_REQUEST, data, reply, option);
84 LBSLOGD(GNSS, "RemoteRequest Transact ErrCode = %{public}d", error);
85 }
86
RefrashRequirements()87 void GnssAbilityProxy::RefrashRequirements()
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;
95 }
96 int error = Remote()->SendRequest(ISubAbility::REFRESH_REQUESTS, data, reply, option);
97 LBSLOGD(GNSS, "RefrashRequirements Transact ErrCode = %{public}d", error);
98 }
99
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)100 void GnssAbilityProxy::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
101 {
102 MessageParcel data;
103 MessageParcel reply;
104 MessageOption option = { MessageOption::TF_ASYNC };
105 if (!data.WriteInterfaceToken(GetDescriptor())) {
106 LBSLOGE(GNSS, "write interfaceToken fail!");
107 return;
108 }
109 data.WriteRemoteObject(callback);
110 int error = Remote()->SendRequest(ISubAbility::REG_GNSS_STATUS, data, reply, option);
111 LBSLOGD(GNSS, "RegisterGnssStatusCallback Transact ErrCode = %{public}d", error);
112 }
113
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)114 void GnssAbilityProxy::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
115 {
116 MessageParcel data;
117 MessageParcel reply;
118 MessageOption option = { MessageOption::TF_ASYNC };
119 if (!data.WriteInterfaceToken(GetDescriptor())) {
120 LBSLOGE(GNSS, "write interfaceToken fail!");
121 return;
122 }
123 data.WriteRemoteObject(callback);
124 int error = Remote()->SendRequest(ISubAbility::UNREG_GNSS_STATUS, data, reply, option);
125 LBSLOGD(GNSS, "UnregisterGnssStatusCallback Transact ErrCode = %{public}d", error);
126 }
127
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)128 void GnssAbilityProxy::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
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;
136 }
137 data.WriteRemoteObject(callback);
138 int error = Remote()->SendRequest(ISubAbility::REG_NMEA, data, reply, option);
139 LBSLOGD(GNSS, "RegisterNmeaMessageCallback Transact ErrCode = %{public}d", error);
140 }
141
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)142 void GnssAbilityProxy::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
143 {
144 MessageParcel data;
145 MessageParcel reply;
146 MessageOption option = { MessageOption::TF_ASYNC };
147 if (!data.WriteInterfaceToken(GetDescriptor())) {
148 LBSLOGE(GNSS, "write interfaceToken fail!");
149 return;
150 }
151 data.WriteRemoteObject(callback);
152 int error = Remote()->SendRequest(ISubAbility::UNREG_NMEA, data, reply, option);
153 LBSLOGD(GNSS, "UnregisterNmeaMessageCallback Transact ErrCode = %{public}d", error);
154 }
155
RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest> & request,const sptr<IRemoteObject> & callback)156 void GnssAbilityProxy::RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request,
157 const sptr<IRemoteObject>& callback)
158 {
159 MessageParcel data;
160 MessageParcel reply;
161 MessageOption option = { MessageOption::TF_ASYNC };
162 if (!data.WriteInterfaceToken(GetDescriptor())) {
163 LBSLOGE(GNSS, "write interfaceToken fail!");
164 return;
165 }
166 data.WriteInt32(request->reportingPeriodSec);
167 data.WriteBool(request->wakeUpCacheQueueFull);
168 data.WriteRemoteObject(callback);
169 int error = Remote()->SendRequest(ISubAbility::REG_CACHED, data, reply, option);
170 LBSLOGD(GNSS, "RegisterCachedCallback Transact ErrCode = %{public}d", error);
171 }
172
UnregisterCachedCallback(const sptr<IRemoteObject> & callback)173 void GnssAbilityProxy::UnregisterCachedCallback(const sptr<IRemoteObject>& callback)
174 {
175 MessageParcel data;
176 MessageParcel reply;
177 MessageOption option = { MessageOption::TF_ASYNC };
178 if (!data.WriteInterfaceToken(GetDescriptor())) {
179 LBSLOGE(GNSS, "write interfaceToken fail!");
180 return;
181 }
182 data.WriteRemoteObject(callback);
183 int error = Remote()->SendRequest(ISubAbility::UNREG_CACHED, data, reply, option);
184 LBSLOGD(GNSS, "UnregisterCachedCallback Transact ErrCode = %{public}d", error);
185 }
186
GetCachedGnssLocationsSize()187 int GnssAbilityProxy::GetCachedGnssLocationsSize()
188 {
189 MessageParcel data;
190 MessageParcel reply;
191 MessageOption option;
192
193 sptr<IRemoteObject> remote = Remote();
194 if (remote == nullptr) {
195 LBSLOGE(GNSS, "GetCachedGnssLocationsSize remote is null");
196 return EXCEPTION;
197 }
198 if (!data.WriteInterfaceToken(GetDescriptor())) {
199 LBSLOGE(GNSS, "write interfaceToken fail!");
200 return EXCEPTION;
201 }
202
203 int error = remote->SendRequest(ISubAbility::GET_CACHED_SIZE, data, reply, option);
204 LBSLOGD(GNSS, "Proxy::GetCachedGnssLocationsSize Transact ErrCode = %{public}d", error);
205 int size = 0;
206 if (error == NO_ERROR) {
207 size = reply.ReadInt32();
208 }
209 LBSLOGD(GNSS, "Proxy::GetCachedGnssLocationsSize return %{public}d", size);
210 return size;
211 }
212
FlushCachedGnssLocations()213 int GnssAbilityProxy::FlushCachedGnssLocations()
214 {
215 MessageParcel data;
216 MessageParcel reply;
217 MessageOption option;
218 sptr<IRemoteObject> remote = Remote();
219 if (remote == nullptr) {
220 LBSLOGE(GNSS, "FlushCachedGnssLocations remote is null");
221 return EXCEPTION;
222 }
223 if (!data.WriteInterfaceToken(GetDescriptor())) {
224 LBSLOGE(GNSS, "write interfaceToken fail!");
225 return EXCEPTION;
226 }
227 int error = remote->SendRequest(ISubAbility::FLUSH_CACHED, data, reply, option);
228 LBSLOGD(GNSS, "Proxy::FlushCachedGnssLocations Transact ErrCodes = %{public}d", error);
229 return error;
230 }
231
SendCommand(std::unique_ptr<LocationCommand> & commands)232 void GnssAbilityProxy::SendCommand(std::unique_ptr<LocationCommand>& commands)
233 {
234 MessageParcel data;
235 MessageParcel reply;
236 MessageOption option;
237 if (!data.WriteInterfaceToken(GetDescriptor())) {
238 LBSLOGE(GNSS, "write interfaceToken fail!");
239 return;
240 }
241 data.WriteInt32(commands->scenario);
242 data.WriteString16(Str8ToStr16(commands->command));
243 sptr<IRemoteObject> remote = Remote();
244 if (remote == nullptr) {
245 LBSLOGE(GNSS, "SendCommand remote is null");
246 return;
247 }
248 int error = remote->SendRequest(ISubAbility::SEND_COMMANDS, data, reply, option);
249 LBSLOGD(GNSS, "Proxy::SendCommand Transact ErrCodes = %{public}d", error);
250 }
251
AddFence(std::unique_ptr<GeofenceRequest> & request)252 void GnssAbilityProxy::AddFence(std::unique_ptr<GeofenceRequest>& request)
253 {
254 MessageParcel data;
255 MessageParcel reply;
256 MessageOption option;
257 if (!data.WriteInterfaceToken(GetDescriptor())) {
258 LBSLOGE(GNSS, "write interfaceToken fail!");
259 return;
260 }
261 data.WriteInt32(request->priority);
262 data.WriteInt32(request->scenario);
263 data.WriteDouble(request->geofence.latitude);
264 data.WriteDouble(request->geofence.longitude);
265 data.WriteDouble(request->geofence.radius);
266 data.WriteDouble(request->geofence.expiration);
267 sptr<IRemoteObject> remote = Remote();
268 if (remote == nullptr) {
269 LBSLOGE(GNSS, "AddFence remote is null");
270 return;
271 }
272 int error = remote->SendRequest(ISubAbility::ADD_FENCE_INFO, data, reply, option);
273 LBSLOGD(GNSS, "Proxy::AddFence Transact ErrCodes = %{public}d", error);
274 }
275
RemoveFence(std::unique_ptr<GeofenceRequest> & request)276 void GnssAbilityProxy::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
277 {
278 MessageParcel data;
279 MessageParcel reply;
280 MessageOption option;
281 if (!data.WriteInterfaceToken(GetDescriptor())) {
282 LBSLOGE(GNSS, "write interfaceToken fail!");
283 return;
284 }
285 data.WriteInt32(request->priority);
286 data.WriteInt32(request->scenario);
287 data.WriteDouble(request->geofence.latitude);
288 data.WriteDouble(request->geofence.longitude);
289 data.WriteDouble(request->geofence.radius);
290 data.WriteDouble(request->geofence.expiration);
291 sptr<IRemoteObject> remote = Remote();
292 if (remote == nullptr) {
293 LBSLOGE(GNSS, "RemoveFence remote is null");
294 return;
295 }
296 int error = remote->SendRequest(ISubAbility::REMOVE_FENCE_INFO, data, reply, option);
297 LBSLOGD(GNSS, "Proxy::RemoveFence Transact ErrCodes = %{public}d", error);
298 }
299 } // namespace Location
300 } // namespace OHOS