• 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_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 {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int GnssAbilityStub::OnRemoteRequest(uint32_t code,
29     MessageParcel &data, MessageParcel &reply, MessageOption &option)
30 {
31     pid_t callingPid = IPCSkeleton::GetCallingPid();
32     pid_t callingUid = IPCSkeleton::GetCallingUid();
33     LBSLOGI(GNSS, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
34         code, option.GetFlags(), callingPid, callingUid);
35 
36     if (data.ReadInterfaceToken() != GetDescriptor()) {
37         LBSLOGE(GNSS, "invalid token.");
38         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
39         return ERRCODE_SERVICE_UNAVAILABLE;
40     }
41 
42     int ret = ERRCODE_SUCCESS;
43     bool isMessageRequest = false;
44     switch (code) {
45         case static_cast<uint32_t>(GnssInterfaceCode::SEND_LOCATION_REQUEST): // fall through
46         case static_cast<uint32_t>(GnssInterfaceCode::SET_MOCKED_LOCATIONS): {
47             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
48                 return ERRCODE_PERMISSION_DENIED;
49             }
50             SendMessage(code, data, reply);
51             isMessageRequest = true;
52             break;
53         }
54         case static_cast<uint32_t>(GnssInterfaceCode::SET_ENABLE): {
55             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
56                 return ERRCODE_PERMISSION_DENIED;
57             }
58             reply.WriteInt32(SetEnable(data.ReadBool()));
59             break;
60         }
61         case static_cast<uint32_t>(GnssInterfaceCode::REFRESH_REQUESTS): {
62             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
63                 return ERRCODE_PERMISSION_DENIED;
64             }
65             reply.WriteInt32(RefrashRequirements());
66             break;
67         }
68         case static_cast<uint32_t>(GnssInterfaceCode::REG_GNSS_STATUS): {
69             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
70                 return ERRCODE_PERMISSION_DENIED;
71             }
72             sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
73             reply.WriteInt32(RegisterGnssStatusCallback(client, callingUid));
74             break;
75         }
76         case static_cast<uint32_t>(GnssInterfaceCode::UNREG_GNSS_STATUS): {
77             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
78                 return ERRCODE_PERMISSION_DENIED;
79             }
80             sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
81             reply.WriteInt32(UnregisterGnssStatusCallback(client));
82             break;
83         }
84         case static_cast<uint32_t>(GnssInterfaceCode::REG_NMEA): {
85             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
86                 return ERRCODE_PERMISSION_DENIED;
87             }
88             sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
89             reply.WriteInt32(RegisterNmeaMessageCallback(client, callingUid));
90             break;
91         }
92         case static_cast<uint32_t>(GnssInterfaceCode::UNREG_NMEA): {
93             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
94                 return ERRCODE_PERMISSION_DENIED;
95             }
96             sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
97             reply.WriteInt32(UnregisterNmeaMessageCallback(client));
98             break;
99         }
100         case static_cast<uint32_t>(GnssInterfaceCode::REG_CACHED): {
101             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
102                 return ERRCODE_PERMISSION_DENIED;
103             }
104             std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
105             requestConfig->reportingPeriodSec = data.ReadInt32();
106             requestConfig->wakeUpCacheQueueFull = data.ReadBool();
107             sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
108             reply.WriteInt32(RegisterCachedCallback(requestConfig, callback));
109             break;
110         }
111         case static_cast<uint32_t>(GnssInterfaceCode::UNREG_CACHED): {
112             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
113                 return ERRCODE_PERMISSION_DENIED;
114             }
115             sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
116             reply.WriteInt32(UnregisterCachedCallback(callback));
117             break;
118         }
119         case static_cast<uint32_t>(GnssInterfaceCode::GET_CACHED_SIZE): {
120             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
121                 return ERRCODE_PERMISSION_DENIED;
122             }
123             int size = -1;
124             reply.WriteInt32(GetCachedGnssLocationsSize(size));
125             reply.WriteInt32(size);
126             break;
127         }
128         case static_cast<uint32_t>(GnssInterfaceCode::FLUSH_CACHED): {
129             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
130                 return ERRCODE_PERMISSION_DENIED;
131             }
132             reply.WriteInt32(FlushCachedGnssLocations());
133             break;
134         }
135         case static_cast<uint32_t>(GnssInterfaceCode::SEND_COMMANDS): {
136             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
137                 return ERRCODE_PERMISSION_DENIED;
138             }
139             std::unique_ptr<LocationCommand> locationCommand = std::make_unique<LocationCommand>();
140             locationCommand->scenario =  data.ReadInt32();
141             locationCommand->command = data.ReadBool();
142             reply.WriteInt32(SendCommand(locationCommand));
143             break;
144         }
145         case static_cast<uint32_t>(GnssInterfaceCode::ENABLE_LOCATION_MOCK): {
146             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
147                 return ERRCODE_PERMISSION_DENIED;
148             }
149             reply.WriteInt32(EnableMock());
150             break;
151         }
152         case static_cast<uint32_t>(GnssInterfaceCode::DISABLE_LOCATION_MOCK): {
153             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
154                 return ERRCODE_PERMISSION_DENIED;
155             }
156             reply.WriteInt32(DisableMock());
157             break;
158         }
159         case static_cast<uint32_t>(GnssInterfaceCode::ADD_FENCE_INFO): {
160             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
161                 return ERRCODE_PERMISSION_DENIED;
162             }
163             std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
164             request->scenario = data.ReadInt32();
165             request->geofence.latitude = data.ReadDouble();
166             request->geofence.longitude = data.ReadDouble();
167             request->geofence.radius = data.ReadDouble();
168             request->geofence.expiration = data.ReadDouble();
169             reply.WriteInt32(AddFence(request));
170             break;
171         }
172         case static_cast<uint32_t>(GnssInterfaceCode::REMOVE_FENCE_INFO): {
173             if (!CommonUtils::CheckCallingPermission(callingUid, callingPid, reply)) {
174                 return ERRCODE_PERMISSION_DENIED;
175             }
176             std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
177             request->scenario = data.ReadInt32();
178             request->geofence.latitude = data.ReadDouble();
179             request->geofence.longitude = data.ReadDouble();
180             request->geofence.radius = data.ReadDouble();
181             request->geofence.expiration = data.ReadDouble();
182             reply.WriteInt32(RemoveFence(request));
183             break;
184         }
185         default:
186             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
187     }
188     if (!isMessageRequest) {
189         UnloadGnssSystemAbility();
190     }
191     return ret;
192 }
193 
GnssStatusCallbackDeathRecipient()194 GnssStatusCallbackDeathRecipient::GnssStatusCallbackDeathRecipient()
195 {
196 }
197 
~GnssStatusCallbackDeathRecipient()198 GnssStatusCallbackDeathRecipient::~GnssStatusCallbackDeathRecipient()
199 {
200 }
201 
OnRemoteDied(const wptr<IRemoteObject> & remote)202 void GnssStatusCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
203 {
204     auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
205     if (gnssAbility != nullptr) {
206         gnssAbility->UnregisterGnssStatusCallback(remote.promote());
207         gnssAbility->UnloadGnssSystemAbility();
208         LBSLOGI(LOCATOR, "gnss status callback OnRemoteDied");
209     }
210 }
211 
NmeaCallbackDeathRecipient()212 NmeaCallbackDeathRecipient::NmeaCallbackDeathRecipient()
213 {
214 }
215 
~NmeaCallbackDeathRecipient()216 NmeaCallbackDeathRecipient::~NmeaCallbackDeathRecipient()
217 {
218 }
219 
OnRemoteDied(const wptr<IRemoteObject> & remote)220 void NmeaCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
221 {
222     auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
223     if (gnssAbility != nullptr) {
224         gnssAbility->UnregisterNmeaMessageCallback(remote.promote());
225         gnssAbility->UnloadGnssSystemAbility();
226         LBSLOGI(LOCATOR, "nmea callback OnRemoteDied");
227     }
228 }
229 
CachedLocationCallbackDeathRecipient()230 CachedLocationCallbackDeathRecipient::CachedLocationCallbackDeathRecipient()
231 {
232 }
233 
~CachedLocationCallbackDeathRecipient()234 CachedLocationCallbackDeathRecipient::~CachedLocationCallbackDeathRecipient()
235 {
236 }
237 
OnRemoteDied(const wptr<IRemoteObject> & remote)238 void CachedLocationCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
239 {
240     auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
241     if (gnssAbility != nullptr) {
242         gnssAbility->UnregisterCachedCallback(remote.promote());
243         gnssAbility->UnloadGnssSystemAbility();
244         LBSLOGI(LOCATOR, "cached location callback OnRemoteDied");
245     }
246 }
247 } // namespace Location
248 } // namespace OHOS
249 #endif
250