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_skeleton.h"
18
19 #include "ipc_skeleton.h"
20
21 #include "common_utils.h"
22 #include "constant_definition.h"
23 #include "gnss_ability.h"
24 #include "locationhub_ipc_interface_code.h"
25
26 namespace OHOS {
27 namespace Location {
InitGnssMsgHandleMap()28 void GnssAbilityStub::InitGnssMsgHandleMap()
29 {
30 if (GnssMsgHandleMap_.size() != 0) {
31 return;
32 }
33 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::SEND_LOCATION_REQUEST)] =
34 &GnssAbilityStub::SendLocationRequestInner;
35 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::SET_MOCKED_LOCATIONS)] =
36 &GnssAbilityStub::SetMockLocationsInner;
37 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::SET_ENABLE)] =
38 &GnssAbilityStub::SetEnableInner;
39 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::REFRESH_REQUESTS)] =
40 &GnssAbilityStub::RefreshRequirementsInner;
41 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::REG_GNSS_STATUS)] =
42 &GnssAbilityStub::RegisterGnssStatusCallbackInner;
43 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::UNREG_GNSS_STATUS)] =
44 &GnssAbilityStub::UnregisterGnssStatusCallbackInner;
45 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::REG_NMEA)] =
46 &GnssAbilityStub::RegisterNmeaMessageCallbackInner;
47 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::UNREG_NMEA)] =
48 &GnssAbilityStub::UnregisterNmeaMessageCallbackInner;
49 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::REG_CACHED)] =
50 &GnssAbilityStub::RegisterCachedCallbackInner;
51 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::UNREG_CACHED)] =
52 &GnssAbilityStub::UnregisterCachedCallbackInner;
53 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::GET_CACHED_SIZE)] =
54 &GnssAbilityStub::GetCachedGnssLocationsSizeInner;
55 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::FLUSH_CACHED)] =
56 &GnssAbilityStub::FlushCachedGnssLocationsInner;
57 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::SEND_COMMANDS)] =
58 &GnssAbilityStub::SendCommandInner;
59 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::ENABLE_LOCATION_MOCK)] =
60 &GnssAbilityStub::EnableMockInner;
61 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::DISABLE_LOCATION_MOCK)] =
62 &GnssAbilityStub::DisableMockInner;
63 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::ADD_FENCE_INFO)] =
64 &GnssAbilityStub::AddFenceInner;
65 GnssMsgHandleMap_[static_cast<uint32_t>(GnssInterfaceCode::REMOVE_FENCE_INFO)] =
66 &GnssAbilityStub::RemoveFenceInner;
67 }
68
GnssAbilityStub()69 GnssAbilityStub::GnssAbilityStub()
70 {
71 InitGnssMsgHandleMap();
72 }
73
SendLocationRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)74 int GnssAbilityStub::SendLocationRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
75 {
76 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
77 return ERRCODE_PERMISSION_DENIED;
78 }
79 SendMessage(static_cast<uint32_t>(GnssInterfaceCode::SEND_LOCATION_REQUEST), data, reply);
80 isMessageRequest_ = true;
81 return ERRCODE_SUCCESS;
82 }
83
SetMockLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)84 int GnssAbilityStub::SetMockLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
85 {
86 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
87 return ERRCODE_PERMISSION_DENIED;
88 }
89 SendMessage(static_cast<uint32_t>(GnssInterfaceCode::SET_MOCKED_LOCATIONS), data, reply);
90 isMessageRequest_ = true;
91 return ERRCODE_SUCCESS;
92 }
93
SetEnableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)94 int GnssAbilityStub::SetEnableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
95 {
96 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
97 return ERRCODE_PERMISSION_DENIED;
98 }
99 SendMessage(static_cast<uint32_t>(GnssInterfaceCode::SET_ENABLE), data, reply);
100 isMessageRequest_ = true;
101 return ERRCODE_SUCCESS;
102 }
103
RefreshRequirementsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)104 int GnssAbilityStub::RefreshRequirementsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
105 {
106 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
107 return ERRCODE_PERMISSION_DENIED;
108 }
109 reply.WriteInt32(RefrashRequirements());
110 return ERRCODE_SUCCESS;
111 }
112
RegisterGnssStatusCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)113 int GnssAbilityStub::RegisterGnssStatusCallbackInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
114 {
115 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
116 return ERRCODE_PERMISSION_DENIED;
117 }
118 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
119 reply.WriteInt32(RegisterGnssStatusCallback(client, identity.GetUid()));
120 return ERRCODE_SUCCESS;
121 }
122
UnregisterGnssStatusCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)123 int GnssAbilityStub::UnregisterGnssStatusCallbackInner(
124 MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
125 {
126 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
127 return ERRCODE_PERMISSION_DENIED;
128 }
129 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
130 reply.WriteInt32(UnregisterGnssStatusCallback(client));
131 return ERRCODE_SUCCESS;
132 }
133
RegisterNmeaMessageCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)134 int GnssAbilityStub::RegisterNmeaMessageCallbackInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
135 {
136 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
137 return ERRCODE_PERMISSION_DENIED;
138 }
139 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
140 reply.WriteInt32(RegisterNmeaMessageCallback(client, identity.GetUid()));
141 return ERRCODE_SUCCESS;
142 }
143
UnregisterNmeaMessageCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)144 int GnssAbilityStub::UnregisterNmeaMessageCallbackInner(MessageParcel &data, MessageParcel &reply,
145 AppIdentity &identity)
146 {
147 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
148 return ERRCODE_PERMISSION_DENIED;
149 }
150 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
151 reply.WriteInt32(UnregisterNmeaMessageCallback(client));
152 return ERRCODE_SUCCESS;
153 }
154
RegisterCachedCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)155 int GnssAbilityStub::RegisterCachedCallbackInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
156 {
157 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
158 return ERRCODE_PERMISSION_DENIED;
159 }
160 std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
161 requestConfig->reportingPeriodSec = data.ReadInt32();
162 requestConfig->wakeUpCacheQueueFull = data.ReadBool();
163 sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
164 reply.WriteInt32(RegisterCachedCallback(requestConfig, callback));
165 return ERRCODE_SUCCESS;
166 }
167
UnregisterCachedCallbackInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)168 int GnssAbilityStub::UnregisterCachedCallbackInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
169 {
170 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
171 return ERRCODE_PERMISSION_DENIED;
172 }
173 sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
174 reply.WriteInt32(UnregisterCachedCallback(callback));
175 return ERRCODE_SUCCESS;
176 }
177
GetCachedGnssLocationsSizeInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)178 int GnssAbilityStub::GetCachedGnssLocationsSizeInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
179 {
180 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
181 return ERRCODE_PERMISSION_DENIED;
182 }
183 int size = -1;
184 reply.WriteInt32(GetCachedGnssLocationsSize(size));
185 reply.WriteInt32(size);
186 return ERRCODE_SUCCESS;
187 }
188
FlushCachedGnssLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)189 int GnssAbilityStub::FlushCachedGnssLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
190 {
191 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
192 return ERRCODE_PERMISSION_DENIED;
193 }
194 reply.WriteInt32(FlushCachedGnssLocations());
195 return ERRCODE_SUCCESS;
196 }
197
SendCommandInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)198 int GnssAbilityStub::SendCommandInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
199 {
200 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
201 return ERRCODE_PERMISSION_DENIED;
202 }
203 SendMessage(static_cast<uint32_t>(GnssInterfaceCode::SEND_COMMANDS), data, reply);
204 isMessageRequest_ = true;
205 return ERRCODE_SUCCESS;
206 }
207
EnableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)208 int GnssAbilityStub::EnableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
209 {
210 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
211 return ERRCODE_PERMISSION_DENIED;
212 }
213 reply.WriteInt32(EnableMock());
214 return ERRCODE_SUCCESS;
215 }
216
DisableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)217 int GnssAbilityStub::DisableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
218 {
219 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
220 return ERRCODE_PERMISSION_DENIED;
221 }
222 reply.WriteInt32(DisableMock());
223 return ERRCODE_SUCCESS;
224 }
225
AddFenceInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)226 int GnssAbilityStub::AddFenceInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
227 {
228 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
229 return ERRCODE_PERMISSION_DENIED;
230 }
231 std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
232 request->scenario = data.ReadInt32();
233 request->geofence.latitude = data.ReadDouble();
234 request->geofence.longitude = data.ReadDouble();
235 request->geofence.radius = data.ReadDouble();
236 request->geofence.expiration = data.ReadDouble();
237 reply.WriteInt32(AddFence(request));
238 return ERRCODE_SUCCESS;
239 }
240
RemoveFenceInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)241 int GnssAbilityStub::RemoveFenceInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
242 {
243 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
244 return ERRCODE_PERMISSION_DENIED;
245 }
246 std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
247 request->scenario = data.ReadInt32();
248 request->geofence.latitude = data.ReadDouble();
249 request->geofence.longitude = data.ReadDouble();
250 request->geofence.radius = data.ReadDouble();
251 request->geofence.expiration = data.ReadDouble();
252 reply.WriteInt32(RemoveFence(request));
253 return ERRCODE_SUCCESS;
254 }
255
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)256 int GnssAbilityStub::OnRemoteRequest(uint32_t code,
257 MessageParcel &data, MessageParcel &reply, MessageOption &option)
258 {
259 pid_t callingPid = IPCSkeleton::GetCallingPid();
260 pid_t callingUid = IPCSkeleton::GetCallingUid();
261 AppIdentity identity;
262 identity.SetPid(callingPid);
263 identity.SetUid(callingUid);
264 LBSLOGI(GNSS, "%{public}s cmd = %{public}u, flags= %{public}d, identity = %{public}s, timestamp = %{public}lld",
265 __func__, code, option.GetFlags(), identity.ToString().c_str(), CommonUtils::GetCurrentTimeStamp());
266
267 if (data.ReadInterfaceToken() != GetDescriptor()) {
268 LBSLOGE(GNSS, "invalid token.");
269 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
270 return ERRCODE_SERVICE_UNAVAILABLE;
271 }
272 int ret = ERRCODE_SUCCESS;
273 isMessageRequest_ = false;
274 auto handleFunc = GnssMsgHandleMap_.find(code);
275 if (handleFunc != GnssMsgHandleMap_.end() && handleFunc->second != nullptr) {
276 auto memberFunc = handleFunc->second;
277 ret = (this->*memberFunc)(data, reply, identity);
278 } else {
279 LBSLOGE(GNSS, "OnReceived cmd = %{public}u, unsupport service.", code);
280 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
281 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
282 }
283 if (!isMessageRequest_) {
284 UnloadGnssSystemAbility();
285 }
286 return ret;
287 }
288
GnssStatusCallbackDeathRecipient()289 GnssStatusCallbackDeathRecipient::GnssStatusCallbackDeathRecipient()
290 {
291 }
292
~GnssStatusCallbackDeathRecipient()293 GnssStatusCallbackDeathRecipient::~GnssStatusCallbackDeathRecipient()
294 {
295 }
296
OnRemoteDied(const wptr<IRemoteObject> & remote)297 void GnssStatusCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
298 {
299 auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
300 if (gnssAbility != nullptr) {
301 gnssAbility->UnregisterGnssStatusCallback(remote.promote());
302 gnssAbility->UnloadGnssSystemAbility();
303 LBSLOGI(LOCATOR, "gnss status callback OnRemoteDied");
304 }
305 }
306
NmeaCallbackDeathRecipient()307 NmeaCallbackDeathRecipient::NmeaCallbackDeathRecipient()
308 {
309 }
310
~NmeaCallbackDeathRecipient()311 NmeaCallbackDeathRecipient::~NmeaCallbackDeathRecipient()
312 {
313 }
314
OnRemoteDied(const wptr<IRemoteObject> & remote)315 void NmeaCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
316 {
317 auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
318 if (gnssAbility != nullptr) {
319 gnssAbility->UnregisterNmeaMessageCallback(remote.promote());
320 gnssAbility->UnloadGnssSystemAbility();
321 LBSLOGI(LOCATOR, "nmea callback OnRemoteDied");
322 }
323 }
324
CachedLocationCallbackDeathRecipient()325 CachedLocationCallbackDeathRecipient::CachedLocationCallbackDeathRecipient()
326 {
327 }
328
~CachedLocationCallbackDeathRecipient()329 CachedLocationCallbackDeathRecipient::~CachedLocationCallbackDeathRecipient()
330 {
331 }
332
OnRemoteDied(const wptr<IRemoteObject> & remote)333 void CachedLocationCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
334 {
335 auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
336 if (gnssAbility != nullptr) {
337 gnssAbility->UnregisterCachedCallback(remote.promote());
338 gnssAbility->UnloadGnssSystemAbility();
339 LBSLOGI(LOCATOR, "cached location callback OnRemoteDied");
340 }
341 }
342 } // namespace Location
343 } // namespace OHOS
344 #endif
345