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 LOCATION_SCENE_WALK:
242         case LOCATION_SCENE_RIDE:
243         case SCENE_NAVIGATION:
244         case SCENE_TRAJECTORY_TRACKING:
245         case SCENE_CAR_HAILING: {
246             proxys->push_back(GNSS_ABILITY);
247             proxys->push_back(NETWORK_ABILITY);
248             break;
249         }
250         case LOCATION_SCENE_LOW_POWER_CONSUMPTION:
251         case LOCATION_SCENE_DAILY_LIFE_SERVICE:
252         case SCENE_DAILY_LIFE_SERVICE: {
253             proxys->push_back(NETWORK_ABILITY);
254             break;
255         }
256         case LOCATION_SCENE_NO_POWER_CONSUMPTION:
257         case SCENE_NO_POWER: {
258             proxys->push_back(PASSIVE_ABILITY);
259             break;
260         }
261         case SCENE_UNSET: {
262             GetProxyNameByPriority(proxys);
263             break;
264         }
265         default:
266             break;
267     }
268 #endif
269 }
270 
GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)271 void Request::GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)
272 {
273     if (requestConfig_ == nullptr || proxys == nullptr) {
274         return;
275     }
276 #ifdef EMULATOR_ENABLED
277     proxys->push_back(GNSS_ABILITY);
278 #else
279     switch (requestConfig_->GetPriority()) {
280         case PRIORITY_LOW_POWER:
281             proxys->push_back(NETWORK_ABILITY);
282             break;
283         case LOCATION_PRIORITY_ACCURACY:
284         case LOCATION_PRIORITY_LOCATING_SPEED:
285         case PRIORITY_ACCURACY:
286         case PRIORITY_FAST_FIRST_FIX:
287             proxys->push_back(GNSS_ABILITY);
288             proxys->push_back(NETWORK_ABILITY);
289             break;
290         default:
291             break;
292     }
293 #endif
294 }
295 
GetLocationPermState()296 bool Request::GetLocationPermState()
297 {
298     return isUsingLocationPerm_;
299 }
300 
GetBackgroundPermState()301 bool Request::GetBackgroundPermState()
302 {
303     return isUsingBackgroundPerm_;
304 }
305 
GetApproximatelyPermState()306 bool Request::GetApproximatelyPermState()
307 {
308     return isUsingApproximatelyPerm_;
309 }
310 
SetLocationPermState(bool state)311 void Request::SetLocationPermState(bool state)
312 {
313     isUsingLocationPerm_ = state;
314 }
315 
SetBackgroundPermState(bool state)316 void Request::SetBackgroundPermState(bool state)
317 {
318     isUsingBackgroundPerm_ = state;
319 }
320 
SetApproximatelyPermState(bool state)321 void Request::SetApproximatelyPermState(bool state)
322 {
323     isUsingApproximatelyPerm_ = state;
324 }
325 
SetNlpRequestType(int nlpRequestType)326 void Request::SetNlpRequestType(int nlpRequestType)
327 {
328     nlpRequestType_ = nlpRequestType;
329 }
330 
GetNlpRequestType()331 int Request::GetNlpRequestType()
332 {
333     return nlpRequestType_;
334 }
335 
SetNlpRequestType()336 void Request::SetNlpRequestType()
337 {
338     if (requestConfig_->GetScenario() == SCENE_NAVIGATION ||
339         requestConfig_->GetScenario() == SCENE_TRAJECTORY_TRACKING ||
340         requestConfig_->GetScenario() == SCENE_CAR_HAILING ||
341         requestConfig_->GetScenario() == LOCATION_SCENE_NAVIGATION ||
342         requestConfig_->GetScenario() == LOCATION_SCENE_SPORT ||
343         requestConfig_->GetScenario() == LOCATION_SCENE_TRANSPORT ||
344         requestConfig_->GetPriority() == PRIORITY_ACCURACY ||
345         requestConfig_->GetPriority() == PRIORITY_FAST_FIRST_FIX ||
346         requestConfig_->GetScenario() == LOCATION_SCENE_HIGH_POWER_CONSUMPTION ||
347         requestConfig_->GetPriority() == LOCATION_PRIORITY_ACCURACY) {
348         nlpRequestType_ = NlpRequestType::PRIORITY_TYPE_INDOOR;
349     } else {
350         nlpRequestType_ = NlpRequestType::PRIORITY_TYPE_BALANCED_POWER_ACCURACY;
351     }
352 }
353 
ToString() const354 std::string Request::ToString() const
355 {
356     if (requestConfig_ == nullptr) {
357         return "";
358     }
359     std::string str = "[request config: " + requestConfig_->ToString() +
360         "] from pid:" + std::to_string(pid_) +
361         ", uid:" + std::to_string(uid_) +
362         ", tokenId:" + std::to_string(tokenId_) +
363         ", tokenIdEx:" + std::to_string(tokenIdEx_) +
364         ", firstTokenId:" + std::to_string(firstTokenId_) +
365         ", uuid:" + uuid_ + ", packageName:" + packageName_;
366     return str;
367 }
368 
SetLocationErrorCallBack(const sptr<ILocatorCallback> & callback)369 void Request::SetLocationErrorCallBack(const sptr<ILocatorCallback>& callback)
370 {
371     locationErrorcallBack_ = callback;
372 }
373 
GetLocationErrorCallBack()374 sptr<ILocatorCallback> Request::GetLocationErrorCallBack()
375 {
376     return locationErrorcallBack_;
377 }
378 
SetLocatorCallbackRecipient(const sptr<IRemoteObject::DeathRecipient> & recipient)379 void Request::SetLocatorCallbackRecipient(const sptr<IRemoteObject::DeathRecipient>& recipient)
380 {
381     locatorCallbackRecipient_ = recipient;
382 }
383 
GetLocatorCallbackRecipient()384 sptr<IRemoteObject::DeathRecipient> Request::GetLocatorCallbackRecipient()
385 {
386     return locatorCallbackRecipient_;
387 }
388 } // namespace Location
389 } // namespace OHOS