1 /*
2 * Copyright (C) 2023 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 "locator_agent.h"
17
18 #include "location_sa_load_manager.h"
19 #include "system_ability_definition.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "location_log.h"
23 #include "locator_service_proxy.h"
24
25 namespace OHOS {
26 namespace Location {
GetInstance()27 LocatorAgentManager* LocatorAgentManager::GetInstance()
28 {
29 static LocatorAgentManager data;
30 return &data;
31 }
32
LocatorAgentManager()33 LocatorAgentManager::LocatorAgentManager()
34 {
35 nmeaCallbackHost_ =
36 sptr<NativeNmeaCallbackHost>(new (std::nothrow) NativeNmeaCallbackHost());
37 gnssCallbackHost_ =
38 sptr<NativeSvCallbackHost>(new (std::nothrow) NativeSvCallbackHost());
39 locationCallbackHost_ =
40 sptr<NativeLocationCallbackHost>(new (std::nothrow) NativeLocationCallbackHost());
41 }
42
~LocatorAgentManager()43 LocatorAgentManager::~LocatorAgentManager()
44 {}
45
StartGnssLocating(const LocationCallbackIfaces & callback)46 LocationErrCode LocatorAgentManager::StartGnssLocating(const LocationCallbackIfaces& callback)
47 {
48 if (locationCallbackHost_ == nullptr) {
49 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
50 return ERRCODE_INVALID_PARAM;
51 }
52 auto proxy = GetLocatorAgent();
53 if (proxy == nullptr) {
54 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
55 return ERRCODE_INVALID_PARAM;
56 }
57 auto requestConfig = std::make_unique<RequestConfig>();
58 requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
59 requestConfig->SetScenario(SCENE_UNSET);
60 requestConfig->SetFixNumber(0);
61 locationCallbackHost_->SetCallback(callback);
62 auto locatorCallback = sptr<ILocatorCallback>(locationCallbackHost_);
63 if (locatorCallback == nullptr) {
64 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
65 return ERRCODE_INVALID_PARAM;
66 }
67 ErrCode errorCodeValue = proxy->StartLocating(*requestConfig, locatorCallback);
68 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
69 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
70 return locationErrCode;
71 }
72
StopGnssLocating()73 LocationErrCode LocatorAgentManager::StopGnssLocating()
74 {
75 auto proxy = GetLocatorAgent();
76 if (proxy == nullptr) {
77 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
78 return ERRCODE_INVALID_PARAM;
79 }
80 auto locatorCallback = sptr<ILocatorCallback>(locationCallbackHost_);
81 if (locatorCallback == nullptr) {
82 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
83 return ERRCODE_INVALID_PARAM;
84 }
85 ErrCode errorCodeValue = proxy->StopLocating(locatorCallback);
86 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
87 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
88 return locationErrCode;
89 }
90
RegisterGnssStatusCallback(const SvStatusCallbackIfaces & callback)91 LocationErrCode LocatorAgentManager::RegisterGnssStatusCallback(const SvStatusCallbackIfaces& callback)
92 {
93 if (gnssCallbackHost_ == nullptr) {
94 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
95 return ERRCODE_INVALID_PARAM;
96 }
97 auto proxy = GetLocatorAgent();
98 if (proxy == nullptr) {
99 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
100 return ERRCODE_INVALID_PARAM;
101 }
102 gnssCallbackHost_->SetCallback(callback);
103 auto gnssCallback = sptr<IGnssStatusCallback>(gnssCallbackHost_);
104 if (gnssCallback == nullptr) {
105 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
106 return ERRCODE_INVALID_PARAM;
107 }
108 ErrCode errorCodeValue = proxy->RegisterGnssStatusCallback(gnssCallback->AsObject());
109 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
110 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
111 return locationErrCode;
112 }
113
UnregisterGnssStatusCallback()114 LocationErrCode LocatorAgentManager::UnregisterGnssStatusCallback()
115 {
116 auto proxy = GetLocatorAgent();
117 if (proxy == nullptr) {
118 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
119 return ERRCODE_INVALID_PARAM;
120 }
121 auto gnssCallback = sptr<IGnssStatusCallback>(gnssCallbackHost_);
122 if (gnssCallback == nullptr) {
123 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
124 return ERRCODE_INVALID_PARAM;
125 }
126 ErrCode errorCodeValue = proxy->UnregisterGnssStatusCallback(gnssCallback->AsObject());
127 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
128 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
129 return locationErrCode;
130 }
131
RegisterNmeaMessageCallback(const GnssNmeaCallbackIfaces & callback)132 LocationErrCode LocatorAgentManager::RegisterNmeaMessageCallback(const GnssNmeaCallbackIfaces& callback)
133 {
134 if (nmeaCallbackHost_ == nullptr) {
135 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
136 return ERRCODE_INVALID_PARAM;
137 }
138 auto proxy = GetLocatorAgent();
139 if (proxy == nullptr) {
140 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
141 return ERRCODE_INVALID_PARAM;
142 }
143 nmeaCallbackHost_->SetCallback(callback);
144 auto nmeaCallback = sptr<INmeaMessageCallback>(nmeaCallbackHost_);
145 if (nmeaCallback == nullptr) {
146 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
147 return ERRCODE_INVALID_PARAM;
148 }
149 ErrCode errorCodeValue = proxy->RegisterNmeaMessageCallback(nmeaCallback->AsObject());
150 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
151 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
152 return locationErrCode;
153 }
154
UnregisterNmeaMessageCallback()155 LocationErrCode LocatorAgentManager::UnregisterNmeaMessageCallback()
156 {
157 auto proxy = GetLocatorAgent();
158 if (proxy == nullptr) {
159 LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
160 return ERRCODE_INVALID_PARAM;
161 }
162 auto nmeaCallback = sptr<INmeaMessageCallback>(nmeaCallbackHost_);
163 if (nmeaCallback == nullptr) {
164 LBSLOGE(LOCATOR_STANDARD, "%{public}s callback is nullptr", __func__);
165 return ERRCODE_INVALID_PARAM;
166 }
167 ErrCode errorCodeValue = proxy->UnregisterNmeaMessageCallback(nmeaCallback->AsObject());
168 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
169 LBSLOGI(LOCATOR_STANDARD, "%{public}s ret = %{public}d", __func__, locationErrCode);
170 return locationErrCode;
171 }
172
GetLocatorAgent()173 sptr<ILocatorService> LocatorAgentManager::GetLocatorAgent()
174 {
175 std::unique_lock<std::mutex> lock(mutex_, std::defer_lock);
176 lock.lock();
177 if (client_ != nullptr) {
178 LBSLOGI(LOCATOR_STANDARD, "%{public}s get proxy success.", __func__);
179 lock.unlock();
180 return client_;
181 }
182 lock.unlock();
183 sptr<IRemoteObject> saObject = CheckLocatorSystemAbilityLoaded();
184 return InitLocatorAgent(saObject);
185 }
186
TryLoadLocatorSystemAbility()187 bool LocatorAgentManager::TryLoadLocatorSystemAbility()
188 {
189 auto instance = LocationSaLoadManager::GetInstance();
190 if (instance == nullptr) {
191 LBSLOGE(LOCATOR_STANDARD, "%{public}s get instance failed.", __func__);
192 return false;
193 }
194 if (instance->LoadLocationSa(LOCATION_LOCATOR_SA_ID) != ERRCODE_SUCCESS) {
195 LBSLOGE(LOCATOR_STANDARD, "%{public}s load sa failed.", __func__);
196 return false;
197 }
198 return true;
199 }
200
InitLocatorAgent(sptr<IRemoteObject> & saObject)201 sptr<ILocatorService> LocatorAgentManager::InitLocatorAgent(sptr<IRemoteObject>& saObject)
202 {
203 if (saObject == nullptr) {
204 return nullptr;
205 }
206 std::unique_lock<std::mutex> lock(mutex_);
207 client_ = iface_cast<LocatorServiceProxy>(saObject);
208 if (!client_ || !client_->AsObject()) {
209 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get locator service failed.", __func__);
210 return nullptr;
211 }
212 recipient_ = sptr<LocatorAgentDeathRecipient>(new (std::nothrow) LocatorAgentDeathRecipient(*this));
213 if ((saObject->IsProxyObject()) && (!saObject->AddDeathRecipient(recipient_))) {
214 LBSLOGE(LOCATOR_STANDARD, "%{public}s: deathRecipient add failed.", __func__);
215 return nullptr;
216 }
217 LBSLOGI(LOCATOR_STANDARD, "%{public}s: client reset success.", __func__);
218 return client_;
219 }
220
CheckLocatorSystemAbilityLoaded()221 sptr<IRemoteObject> LocatorAgentManager::CheckLocatorSystemAbilityLoaded()
222 {
223 sptr<ISystemAbilityManager> samgr =
224 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
225 if (samgr == nullptr) {
226 LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
227 return nullptr;
228 }
229
230 sptr<IRemoteObject> saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
231 if (saObject == nullptr) {
232 if (!TryLoadLocatorSystemAbility()) {
233 return nullptr;
234 }
235 saObject = samgr->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
236 if (saObject == nullptr) {
237 return nullptr;
238 }
239 }
240 return saObject;
241 }
242
ResetLocatorAgent(const wptr<IRemoteObject> & remote)243 void LocatorAgentManager::ResetLocatorAgent(const wptr<IRemoteObject> &remote)
244 {
245 std::unique_lock<std::mutex> lock(mutex_);
246 if (remote == nullptr) {
247 LBSLOGE(LOCATOR_STANDARD, "%{public}s: remote is nullptr.", __func__);
248 return;
249 }
250 if (client_ == nullptr) {
251 LBSLOGE(LOCATOR_STANDARD, "%{public}s: proxy is nullptr.", __func__);
252 return;
253 }
254 if (remote.promote() != nullptr) {
255 remote.promote()->RemoveDeathRecipient(recipient_);
256 }
257 client_ = nullptr;
258 }
259 }
260 }
261