1 /*
2 * Copyright (C) 2024 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 "geofence_sdk.h"
17 #include "common_utils.h"
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
24 namespace OHOS {
25 namespace Location {
GetInstance()26 GeofenceManager* GeofenceManager::GetInstance()
27 {
28 static GeofenceManager data;
29 return &data;
30 }
31
GeofenceManager()32 GeofenceManager::GeofenceManager()
33 {
34 }
35
~GeofenceManager()36 GeofenceManager::~GeofenceManager()
37 {}
38
ResetGeofenceSdkProxy(const wptr<IRemoteObject> & remote)39 void GeofenceManager::ResetGeofenceSdkProxy(const wptr<IRemoteObject> &remote)
40 {
41 if (remote == nullptr) {
42 LBSLOGE(GEOFENCE_SDK, "%{public}s: remote is nullptr.", __func__);
43 return;
44 }
45 if (client_ == nullptr || !isServerExist_) {
46 LBSLOGE(GEOFENCE_SDK, "%{public}s: proxy is nullptr.", __func__);
47 return;
48 }
49 if (remote.promote() != nullptr) {
50 remote.promote()->RemoveDeathRecipient(recipient_);
51 }
52 isServerExist_ = false;
53 LBSLOGI(GEOFENCE_SDK, "%{public}s: finish.", __func__);
54 }
55
GetProxy()56 sptr<ILocatorService> GeofenceManager::GetProxy()
57 {
58 std::unique_lock<std::mutex> lock(mutex_);
59 if (client_ != nullptr && isServerExist_) {
60 return client_;
61 }
62
63 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (sam == nullptr) {
65 LBSLOGE(GEOFENCE_SDK, "%{public}s: get samgr failed.", __func__);
66 return nullptr;
67 }
68 sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
69 if (obj == nullptr) {
70 LBSLOGE(GEOFENCE_SDK, "%{public}s: get remote service failed.", __func__);
71 return nullptr;
72 }
73 client_ = iface_cast<ILocatorService>(obj);
74 if (!client_ || !client_->AsObject()) {
75 LBSLOGE(GEOFENCE_SDK, "%{public}s: get locator service failed.", __func__);
76 return nullptr;
77 }
78 recipient_ = sptr<GeofenceManagerDeathRecipient>(new (std::nothrow) GeofenceManagerDeathRecipient(*this));
79 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(recipient_))) {
80 LBSLOGE(GEOFENCE_SDK, "%{public}s: deathRecipient add failed.", __func__);
81 return nullptr;
82 }
83 isServerExist_ = true;
84 return client_;
85 }
86
AddFenceV9(std::shared_ptr<GeofenceRequest> & request)87 LocationErrCode GeofenceManager::AddFenceV9(std::shared_ptr<GeofenceRequest> &request)
88 {
89 if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
90 return ERRCODE_SERVICE_UNAVAILABLE;
91 }
92 LBSLOGD(GEOFENCE_SDK, "GeofenceManager::AddFenceV9()");
93 sptr<ILocatorService> proxy = GetProxy();
94 if (proxy == nullptr) {
95 LBSLOGE(GEOFENCE_SDK, "%{public}s get proxy failed.", __func__);
96 return ERRCODE_SERVICE_UNAVAILABLE;
97 }
98 if (request == nullptr) {
99 return ERRCODE_INVALID_PARAM;
100 }
101 ErrCode errorCodeValue = proxy->AddFence(*request);
102 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
103 return locationErrCode;
104 }
105
RemoveFenceV9(std::shared_ptr<GeofenceRequest> & request)106 LocationErrCode GeofenceManager::RemoveFenceV9(std::shared_ptr<GeofenceRequest> &request)
107 {
108 if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
109 return ERRCODE_SERVICE_UNAVAILABLE;
110 }
111 LBSLOGD(GEOFENCE_SDK, "GeofenceManager::RemoveFenceV9()");
112 sptr<ILocatorService> proxy = GetProxy();
113 if (proxy == nullptr) {
114 LBSLOGE(GEOFENCE_SDK, "%{public}s get proxy failed.", __func__);
115 return ERRCODE_SERVICE_UNAVAILABLE;
116 }
117 if (request == nullptr) {
118 return ERRCODE_INVALID_PARAM;
119 }
120 ErrCode errorCodeValue = proxy->RemoveFence(*request);
121 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
122 return locationErrCode;
123 }
124
AddGnssGeofence(std::shared_ptr<GeofenceRequest> & request)125 LocationErrCode GeofenceManager::AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request)
126 {
127 if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
128 return ERRCODE_SERVICE_UNAVAILABLE;
129 }
130 sptr<ILocatorService> proxy = GetProxy();
131 if (proxy == nullptr) {
132 LBSLOGE(GEOFENCE_SDK, "%{public}s get proxy failed.", __func__);
133 return ERRCODE_SERVICE_UNAVAILABLE;
134 }
135 if (request == nullptr) {
136 return ERRCODE_INVALID_PARAM;
137 }
138 ErrCode errorCodeValue = proxy->AddGnssGeofence(*request);
139 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
140 return locationErrCode;
141 }
142
RemoveGnssGeofence(std::shared_ptr<GeofenceRequest> & request)143 LocationErrCode GeofenceManager::RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request)
144 {
145 if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
146 return ERRCODE_SERVICE_UNAVAILABLE;
147 }
148 LBSLOGD(GEOFENCE_SDK, "GeofenceManager::RemoveGnssGeofence()");
149 sptr<ILocatorService> proxy = GetProxy();
150 if (proxy == nullptr) {
151 LBSLOGE(GEOFENCE_SDK, "%{public}s get proxy failed.", __func__);
152 return ERRCODE_SERVICE_UNAVAILABLE;
153 }
154 if (request == nullptr) {
155 return ERRCODE_INVALID_PARAM;
156 }
157 ErrCode errorCodeValue = proxy->RemoveGnssGeofence(request->GetFenceId());
158 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
159 return locationErrCode;
160 }
161
GetGeofenceSupportedCoordTypes(std::vector<CoordinateSystemType> & coordinateSystemTypes)162 LocationErrCode GeofenceManager::GetGeofenceSupportedCoordTypes(
163 std::vector<CoordinateSystemType>& coordinateSystemTypes)
164 {
165 if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
166 return ERRCODE_SERVICE_UNAVAILABLE;
167 }
168 LBSLOGD(GEOFENCE_SDK, "GeofenceManager::%{public}s", __func__);
169 sptr<ILocatorService> proxy = GetProxy();
170 if (proxy == nullptr) {
171 LBSLOGE(GEOFENCE_SDK, "%{public}s get proxy failed.", __func__);
172 return ERRCODE_SERVICE_UNAVAILABLE;
173 }
174 std::vector<CoordinateType> coordinateTypes;
175 ErrCode errorCodeValue = proxy->QuerySupportCoordinateSystemType(coordinateTypes);
176 size_t size = coordinateTypes.size();
177 size = size > COORDINATE_SYSTEM_TYPE_SIZE ? COORDINATE_SYSTEM_TYPE_SIZE : size;
178 for (size_t i = 0; i < size; i++) {
179 coordinateSystemTypes.push_back(static_cast<CoordinateSystemType>(coordinateTypes[i]));
180 }
181 LocationErrCode locationErrCode = CommonUtils::ErrCodeToLocationErrCode(errorCodeValue);
182 return locationErrCode;
183 }
184 }
185 }
186