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 "request.h"
17 #include "common_utils.h"
18 #include "constant_definition.h"
19
20 namespace OHOS {
21 namespace Location {
Request()22 Request::Request()
23 {
24 pid_ = -1;
25 uid_ = -1;
26 tokenId_ = 0;
27 tokenIdEx_ = 0;
28 firstTokenId_ = 0;
29 packageName_ = "";
30 isRequesting_ = false;
31 permUsedType_ = 0;
32 requestConfig_ = new (std::nothrow) RequestConfig();
33 lastLocation_ = new (std::nothrow) Location();
34 bestLocation_ = new (std::nothrow) Location();
35 isUsingLocationPerm_ = false;
36 isUsingBackgroundPerm_ = false;
37 isUsingApproximatelyPerm_ = false;
38 nlpRequestType_ = 0;
39 }
40
Request(std::unique_ptr<RequestConfig> & requestConfig,const sptr<ILocatorCallback> & callback,AppIdentity & identity)41 Request::Request(std::unique_ptr<RequestConfig>& requestConfig,
42 const sptr<ILocatorCallback>& callback, AppIdentity &identity)
43 {
44 pid_ = -1;
45 uid_ = -1;
46 tokenId_ = 0;
47 firstTokenId_ = 0;
48 packageName_ = "";
49 isRequesting_ = false;
50 permUsedType_ = 0;
51 requestConfig_ = new (std::nothrow) RequestConfig();
52 lastLocation_ = new (std::nothrow) Location();
53 bestLocation_ = new (std::nothrow) Location();
54 isUsingLocationPerm_ = false;
55 isUsingBackgroundPerm_ = false;
56 isUsingApproximatelyPerm_ = false;
57 nlpRequestType_ = 0;
58 SetUid(identity.GetUid());
59 SetPid(identity.GetPid());
60 SetTokenId(identity.GetTokenId());
61 SetTokenIdEx(identity.GetTokenIdEx());
62 SetFirstTokenId(identity.GetFirstTokenId());
63 SetPackageName(identity.GetBundleName());
64 SetRequestConfig(*requestConfig);
65 SetNlpRequestType();
66 requestConfig_->SetTimeStamp(CommonUtils::GetCurrentTime());
67 SetLocatorCallBack(callback);
68 SetUuid(CommonUtils::GenerateUuid());
69 }
70
71
~Request()72 Request::~Request() {}
73
SetRequestConfig(RequestConfig & requestConfig)74 void Request::SetRequestConfig(RequestConfig& requestConfig)
75 {
76 if (requestConfig_ == nullptr) {
77 return;
78 }
79 requestConfig_->Set(requestConfig);
80 }
81
SetLocatorCallBack(const sptr<ILocatorCallback> & callback)82 void Request::SetLocatorCallBack(const sptr<ILocatorCallback>& callback)
83 {
84 callBack_ = callback;
85 }
86
GetRequestConfig()87 sptr<RequestConfig> Request::GetRequestConfig()
88 {
89 return requestConfig_;
90 }
91
GetLocatorCallBack()92 sptr<ILocatorCallback> Request::GetLocatorCallBack()
93 {
94 return callBack_;
95 }
96
SetUid(pid_t uid)97 void Request::SetUid(pid_t uid)
98 {
99 uid_ = uid;
100 }
101
GetUid()102 pid_t Request::GetUid()
103 {
104 return uid_;
105 }
106
SetPid(pid_t pid)107 void Request::SetPid(pid_t pid)
108 {
109 pid_ = pid;
110 }
111
GetPid()112 pid_t Request::GetPid()
113 {
114 return pid_;
115 }
116
SetTokenId(uint32_t tokenId)117 void Request::SetTokenId(uint32_t tokenId)
118 {
119 tokenId_ = tokenId;
120 }
121
GetTokenId()122 uint32_t Request::GetTokenId()
123 {
124 return tokenId_;
125 }
126
GetPermUsedType()127 int Request::GetPermUsedType()
128 {
129 return permUsedType_;
130 }
131
SetPermUsedType(int type)132 void Request::SetPermUsedType(int type)
133 {
134 permUsedType_ = type;
135 }
136
SetTokenIdEx(uint64_t tokenIdEx)137 void Request::SetTokenIdEx(uint64_t tokenIdEx)
138 {
139 tokenIdEx_ = tokenIdEx;
140 }
141
GetTokenIdEx()142 uint64_t Request::GetTokenIdEx()
143 {
144 return tokenIdEx_;
145 }
146
SetFirstTokenId(uint32_t firstTokenId)147 void Request::SetFirstTokenId(uint32_t firstTokenId)
148 {
149 firstTokenId_ = firstTokenId;
150 }
151
GetFirstTokenId()152 uint32_t Request::GetFirstTokenId()
153 {
154 return firstTokenId_;
155 }
156
SetPackageName(std::string packageName)157 void Request::SetPackageName(std::string packageName)
158 {
159 packageName_ = packageName;
160 }
161
GetPackageName()162 std::string Request::GetPackageName()
163 {
164 return packageName_;
165 }
166
GetIsRequesting()167 bool Request::GetIsRequesting()
168 {
169 return isRequesting_;
170 }
171
SetRequesting(bool state)172 void Request::SetRequesting(bool state)
173 {
174 isRequesting_ = state;
175 }
176
GetLastLocation()177 sptr<Location> Request::GetLastLocation()
178 {
179 return lastLocation_;
180 }
181
GetUuid()182 std::string Request::GetUuid()
183 {
184 return uuid_;
185 }
186
SetUuid(std::string uuid)187 void Request::SetUuid(std::string uuid)
188 {
189 uuid_ = uuid;
190 }
191
SetLastLocation(const std::unique_ptr<Location> & location)192 void Request::SetLastLocation(const std::unique_ptr<Location>& location)
193 {
194 if (lastLocation_ == nullptr || location == nullptr) {
195 return;
196 }
197 lastLocation_->SetLatitude(location->GetLatitude());
198 lastLocation_->SetLongitude(location->GetLongitude());
199 lastLocation_->SetAltitude(location->GetAltitude());
200 lastLocation_->SetAccuracy(location->GetAccuracy());
201 lastLocation_->SetSpeed(location->GetSpeed());
202 lastLocation_->SetDirection(location->GetDirection());
203 lastLocation_->SetTimeStamp(location->GetTimeStamp());
204 lastLocation_->SetTimeSinceBoot(location->GetTimeSinceBoot());
205 lastLocation_->SetLocationSourceType(location->GetLocationSourceType());
206 }
207
GetBestLocation()208 sptr<Location> Request::GetBestLocation()
209 {
210 return bestLocation_;
211 }
212
SetBestLocation(const std::unique_ptr<Location> & location)213 void Request::SetBestLocation(const std::unique_ptr<Location>& location)
214 {
215 if (bestLocation_ == nullptr || location == nullptr) {
216 return;
217 }
218 bestLocation_->SetLatitude(location->GetLatitude());
219 bestLocation_->SetLongitude(location->GetLongitude());
220 bestLocation_->SetAltitude(location->GetAltitude());
221 bestLocation_->SetAccuracy(location->GetAccuracy());
222 bestLocation_->SetSpeed(location->GetSpeed());
223 bestLocation_->SetDirection(location->GetDirection());
224 bestLocation_->SetTimeStamp(location->GetTimeStamp());
225 bestLocation_->SetTimeSinceBoot(location->GetTimeSinceBoot());
226 bestLocation_->SetLocationSourceType(location->GetLocationSourceType());
227 }
228
GetProxyName(std::shared_ptr<std::list<std::string>> proxys)229 void Request::GetProxyName(std::shared_ptr<std::list<std::string>> proxys)
230 {
231 if (requestConfig_ == nullptr || proxys == nullptr) {
232 return;
233 }
234 #ifdef EMULATOR_ENABLED
235 proxys->push_back(GNSS_ABILITY);
236 #else
237 switch (requestConfig_->GetScenario()) {
238 case LOCATION_SCENE_NAVIGATION:
239 case LOCATION_SCENE_SPORT:
240 case LOCATION_SCENE_TRANSPORT:
241 case LOCATION_SCENE_HIGH_POWER_CONSUMPTION:
242 case LOCATION_SCENE_WALK:
243 case LOCATION_SCENE_RIDE:
244 case LOCATION_SCENE_INDOOR_POI:
245 case SCENE_NAVIGATION:
246 case SCENE_TRAJECTORY_TRACKING:
247 case SCENE_CAR_HAILING: {
248 proxys->push_back(GNSS_ABILITY);
249 proxys->push_back(NETWORK_ABILITY);
250 break;
251 }
252 case LOCATION_SCENE_LOW_POWER_CONSUMPTION:
253 case LOCATION_SCENE_DAILY_LIFE_SERVICE:
254 case SCENE_DAILY_LIFE_SERVICE: {
255 proxys->push_back(NETWORK_ABILITY);
256 break;
257 }
258 case LOCATION_SCENE_NO_POWER_CONSUMPTION:
259 case SCENE_NO_POWER: {
260 proxys->push_back(PASSIVE_ABILITY);
261 break;
262 }
263 case SCENE_UNSET: {
264 GetProxyNameByPriority(proxys);
265 break;
266 }
267 default:
268 break;
269 }
270 #endif
271 }
272
GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)273 void Request::GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)
274 {
275 if (requestConfig_ == nullptr || proxys == nullptr) {
276 return;
277 }
278 #ifdef EMULATOR_ENABLED
279 proxys->push_back(GNSS_ABILITY);
280 #else
281 switch (requestConfig_->GetPriority()) {
282 case PRIORITY_LOW_POWER:
283 proxys->push_back(NETWORK_ABILITY);
284 break;
285 case LOCATION_PRIORITY_ACCURACY:
286 case LOCATION_PRIORITY_LOCATING_SPEED:
287 case PRIORITY_ACCURACY:
288 case PRIORITY_FAST_FIRST_FIX:
289 proxys->push_back(GNSS_ABILITY);
290 proxys->push_back(NETWORK_ABILITY);
291 break;
292 default:
293 break;
294 }
295 #endif
296 }
297
GetLocationPermState()298 bool Request::GetLocationPermState()
299 {
300 return isUsingLocationPerm_;
301 }
302
GetBackgroundPermState()303 bool Request::GetBackgroundPermState()
304 {
305 return isUsingBackgroundPerm_;
306 }
307
GetApproximatelyPermState()308 bool Request::GetApproximatelyPermState()
309 {
310 return isUsingApproximatelyPerm_;
311 }
312
SetLocationPermState(bool state)313 void Request::SetLocationPermState(bool state)
314 {
315 isUsingLocationPerm_ = state;
316 }
317
SetBackgroundPermState(bool state)318 void Request::SetBackgroundPermState(bool state)
319 {
320 isUsingBackgroundPerm_ = state;
321 }
322
SetApproximatelyPermState(bool state)323 void Request::SetApproximatelyPermState(bool state)
324 {
325 isUsingApproximatelyPerm_ = state;
326 }
327
SetNlpRequestType(int nlpRequestType)328 void Request::SetNlpRequestType(int nlpRequestType)
329 {
330 nlpRequestType_ = nlpRequestType;
331 }
332
GetNlpRequestType()333 int Request::GetNlpRequestType()
334 {
335 return nlpRequestType_;
336 }
337
SetNlpRequestType()338 void Request::SetNlpRequestType()
339 {
340 if (requestConfig_ == nullptr) {
341 return;
342 }
343 if (requestConfig_->GetScenario() == SCENE_NAVIGATION ||
344 requestConfig_->GetScenario() == SCENE_TRAJECTORY_TRACKING ||
345 requestConfig_->GetScenario() == SCENE_CAR_HAILING ||
346 requestConfig_->GetScenario() == LOCATION_SCENE_NAVIGATION ||
347 requestConfig_->GetScenario() == LOCATION_SCENE_SPORT ||
348 requestConfig_->GetScenario() == LOCATION_SCENE_TRANSPORT ||
349 (requestConfig_->GetScenario() == SCENE_UNSET && requestConfig_->GetPriority() == PRIORITY_ACCURACY) ||
350 (requestConfig_->GetScenario() == SCENE_UNSET && requestConfig_->GetPriority() == PRIORITY_FAST_FIRST_FIX) ||
351 requestConfig_->GetScenario() == LOCATION_SCENE_HIGH_POWER_CONSUMPTION ||
352 (requestConfig_->GetScenario() == SCENE_UNSET &&
353 requestConfig_->GetPriority() == LOCATION_PRIORITY_ACCURACY) ||
354 (requestConfig_->GetScenario() == SCENE_UNSET &&
355 requestConfig_->GetPriority() == LOCATION_PRIORITY_LOCATING_SPEED)) {
356 nlpRequestType_ = NlpRequestType::PRIORITY_TYPE_INDOOR;
357 } else {
358 nlpRequestType_ = NlpRequestType::PRIORITY_TYPE_BALANCED_POWER_ACCURACY;
359 }
360 if (requestConfig_->GetScenario() == LOCATION_SCENE_INDOOR_POI || requestConfig_->GetIsNeedPoi()) {
361 requestConfig_->SetIsNeedPoi(true);
362 nlpRequestType_ = NlpRequestType::PRIORITY_TYPE_INDOOR_POI;
363 }
364 }
365
ToString() const366 std::string Request::ToString() const
367 {
368 if (requestConfig_ == nullptr) {
369 return "";
370 }
371 std::string str = "[request config: " + requestConfig_->ToString() +
372 "] from pid:" + std::to_string(pid_) +
373 ", uid:" + std::to_string(uid_) +
374 ", Id:" + std::to_string(tokenId_) +
375 ", IdEx:" + std::to_string(tokenIdEx_) +
376 ", firstId:" + std::to_string(firstTokenId_) +
377 ", uuid:" + uuid_ + ", packageName:" + packageName_;
378 return str;
379 }
380
SetLocationErrorCallBack(const sptr<ILocatorCallback> & callback)381 void Request::SetLocationErrorCallBack(const sptr<ILocatorCallback>& callback)
382 {
383 locationErrorcallBack_ = callback;
384 }
385
GetLocationErrorCallBack()386 sptr<ILocatorCallback> Request::GetLocationErrorCallBack()
387 {
388 return locationErrorcallBack_;
389 }
390
SetLocatorCallbackRecipient(const sptr<IRemoteObject::DeathRecipient> & recipient)391 void Request::SetLocatorCallbackRecipient(const sptr<IRemoteObject::DeathRecipient>& recipient)
392 {
393 locatorCallbackRecipient_ = recipient;
394 }
395
GetLocatorCallbackRecipient()396 sptr<IRemoteObject::DeathRecipient> Request::GetLocatorCallbackRecipient()
397 {
398 return locatorCallbackRecipient_;
399 }
400 } // namespace Location
401 } // namespace OHOS