• 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     this->pid_ = -1;
25     this->uid_ = -1;
26     this->tokenId_ = 0;
27     this->firstTokenId_ = 0;
28     this->packageName_ = "";
29     this->isRequesting_ = false;
30     requestConfig_ = new (std::nothrow) RequestConfig();
31     lastLocation_ = new (std::nothrow) Location();
32     isUsingLocationPerm_ = false;
33     isUsingBackgroundPerm_ = false;
34     isUsingApproximatelyPerm_ = false;
35 }
36 
~Request()37 Request::~Request() {}
38 
SetRequestConfig(RequestConfig & requestConfig)39 void Request::SetRequestConfig(RequestConfig& requestConfig)
40 {
41     if (this->requestConfig_ == nullptr) {
42         return;
43     }
44     this->requestConfig_->Set(requestConfig);
45 }
46 
SetLocatorCallBack(const sptr<ILocatorCallback> & callback)47 void Request::SetLocatorCallBack(const sptr<ILocatorCallback>& callback)
48 {
49     this->callBack_ = callback;
50 }
51 
GetRequestConfig()52 sptr<RequestConfig> Request::GetRequestConfig()
53 {
54     return requestConfig_;
55 }
56 
GetLocatorCallBack()57 sptr<ILocatorCallback> Request::GetLocatorCallBack()
58 {
59     return callBack_;
60 }
61 
SetUid(pid_t uid)62 void Request::SetUid(pid_t uid)
63 {
64     this->uid_ = uid;
65 }
66 
GetUid()67 pid_t Request::GetUid()
68 {
69     return uid_;
70 }
71 
SetPid(pid_t pid)72 void Request::SetPid(pid_t pid)
73 {
74     this->pid_ = pid;
75 }
76 
GetPid()77 pid_t Request::GetPid()
78 {
79     return pid_;
80 }
81 
SetTokenId(uint32_t tokenId)82 void Request::SetTokenId(uint32_t tokenId)
83 {
84     this->tokenId_ = tokenId;
85 }
86 
GetTokenId()87 uint32_t Request::GetTokenId()
88 {
89     return tokenId_;
90 }
91 
SetTokenIdEx(uint64_t tokenIdEx)92 void Request::SetTokenIdEx(uint64_t tokenIdEx)
93 {
94     this->tokenIdEx_ = tokenIdEx;
95 }
96 
GetTokenIdEx()97 uint64_t Request::GetTokenIdEx()
98 {
99     return tokenIdEx_;
100 }
101 
SetFirstTokenId(uint32_t firstTokenId)102 void Request::SetFirstTokenId(uint32_t firstTokenId)
103 {
104     this->firstTokenId_ = firstTokenId;
105 }
106 
GetFirstTokenId()107 uint32_t Request::GetFirstTokenId()
108 {
109     return firstTokenId_;
110 }
111 
SetPackageName(std::string packageName)112 void Request::SetPackageName(std::string packageName)
113 {
114     this->packageName_ = packageName;
115 }
116 
GetPackageName()117 std::string Request::GetPackageName()
118 {
119     return packageName_;
120 }
121 
GetIsRequesting()122 bool Request::GetIsRequesting()
123 {
124     return isRequesting_;
125 }
126 
SetRequesting(bool state)127 void Request::SetRequesting(bool state)
128 {
129     this->isRequesting_ = state;
130 }
131 
GetLastLocation()132 sptr<Location> Request::GetLastLocation()
133 {
134     return lastLocation_;
135 }
136 
GetUuid()137 std::string Request::GetUuid()
138 {
139     return uuid_;
140 }
141 
SetUuid(std::string uuid)142 void Request::SetUuid(std::string uuid)
143 {
144     this->uuid_ = uuid;
145 }
146 
SetLastLocation(const std::unique_ptr<Location> & location)147 void Request::SetLastLocation(const std::unique_ptr<Location>& location)
148 {
149     if (this->lastLocation_ == nullptr) {
150         return;
151     }
152     this->lastLocation_->SetLatitude(location->GetLatitude());
153     this->lastLocation_->SetLongitude(location->GetLongitude());
154     this->lastLocation_->SetAltitude(location->GetAltitude());
155     this->lastLocation_->SetAccuracy(location->GetAccuracy());
156     this->lastLocation_->SetSpeed(location->GetSpeed());
157     this->lastLocation_->SetDirection(location->GetDirection());
158     this->lastLocation_->SetTimeStamp(location->GetTimeStamp());
159     this->lastLocation_->SetTimeSinceBoot(location->GetTimeSinceBoot());
160 }
161 
GetProxyName(std::shared_ptr<std::list<std::string>> proxys)162 void Request::GetProxyName(std::shared_ptr<std::list<std::string>> proxys)
163 {
164     if (requestConfig_ == nullptr || proxys == nullptr) {
165         return;
166     }
167     switch (requestConfig_->GetScenario()) {
168         case SCENE_NAVIGATION:
169         case SCENE_TRAJECTORY_TRACKING:
170         case SCENE_CAR_HAILING: {
171             proxys->push_back(GNSS_ABILITY);
172             break;
173         }
174         case SCENE_DAILY_LIFE_SERVICE: {
175             proxys->push_back(NETWORK_ABILITY);
176             break;
177         }
178         case SCENE_NO_POWER: {
179             proxys->push_back(PASSIVE_ABILITY);
180             break;
181         }
182         case SCENE_UNSET: {
183             GetProxyNameByPriority(proxys);
184             break;
185         }
186         default:
187             break;
188     }
189 }
190 
GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)191 void Request::GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)
192 {
193     if (requestConfig_ == nullptr || proxys == nullptr) {
194         return;
195     }
196     switch (requestConfig_->GetPriority()) {
197         case PRIORITY_ACCURACY:
198             proxys->push_back(GNSS_ABILITY);
199             break;
200         case PRIORITY_LOW_POWER:
201             proxys->push_back(NETWORK_ABILITY);
202             break;
203         case PRIORITY_FAST_FIRST_FIX:
204             proxys->push_back(GNSS_ABILITY);
205             proxys->push_back(NETWORK_ABILITY);
206             break;
207         default:
208             break;
209     }
210 }
211 
GetLocationPermState()212 bool Request::GetLocationPermState()
213 {
214     return isUsingLocationPerm_;
215 }
216 
GetBackgroundPermState()217 bool Request::GetBackgroundPermState()
218 {
219     return isUsingBackgroundPerm_;
220 }
221 
GetApproximatelyPermState()222 bool Request::GetApproximatelyPermState()
223 {
224     return isUsingApproximatelyPerm_;
225 }
226 
SetLocationPermState(bool state)227 void Request::SetLocationPermState(bool state)
228 {
229     isUsingLocationPerm_ = state;
230 }
231 
SetBackgroundPermState(bool state)232 void Request::SetBackgroundPermState(bool state)
233 {
234     isUsingBackgroundPerm_ = state;
235 }
236 
SetApproximatelyPermState(bool state)237 void Request::SetApproximatelyPermState(bool state)
238 {
239     isUsingApproximatelyPerm_ = state;
240 }
241 
ToString() const242 std::string Request::ToString() const
243 {
244     if (requestConfig_ == nullptr) {
245         return "";
246     }
247     std::string str = "[request config: " + requestConfig_->ToString() +
248         "] from pid:" + std::to_string(pid_) +
249         ", uid:" + std::to_string(uid_) +
250         ", tokenId:" + std::to_string(tokenId_) +
251         ", tokenIdEx:" + std::to_string(tokenIdEx_) +
252         ", firstTokenId:" + std::to_string(firstTokenId_) +
253         ", uuid:" + uuid_ + ", packageName:" + packageName_;
254     return str;
255 }
256 } // namespace Location
257 } // namespace OHOS