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