1 /*
2 * Copyright (C) 2025 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 #include "util.h"
16 namespace OHOS {
17 namespace Location {
GetErrorCodeMapTaihe()18 std::map<int, std::string> Util::GetErrorCodeMapTaihe()
19 {
20 std::map<int, std::string> errorCodeMap = {
21 {SUCCESS, "SUCCESS"},
22 {NOT_SUPPORTED, "NOT_SUPPORTED"},
23 {INPUT_PARAMS_ERROR, "INPUT_PARAMS_ERROR"},
24 {REVERSE_GEOCODE_ERROR, "REVERSE_GEOCODE_ERROR"},
25 {GEOCODE_ERROR, "GEOCODE_ERROR"},
26 {LOCATOR_ERROR, "LOCATOR_ERROR"},
27 {LOCATION_SWITCH_ERROR, "LOCATION_SWITCH_ERROR"},
28 {LAST_KNOWN_LOCATION_ERROR, "LAST_KNOWN_LOCATION_ERROR"},
29 {LOCATION_REQUEST_TIMEOUT_ERROR, "LOCATION_REQUEST_TIMEOUT_ERROR"},
30 {QUERY_COUNTRY_CODE_ERROR, "QUERY_COUNTRY_CODE_ERROR"},
31 {LocationErrCode::ERRCODE_SUCCESS, "SUCCESS."},
32 {LocationErrCode::ERRCODE_PERMISSION_DENIED,
33 "Permission verification failed. The application does not have the permission required to call the API."},
34 {LocationErrCode::ERRCODE_SYSTEM_PERMISSION_DENIED,
35 "Permission verification failed. A non-system application calls a system API."},
36 {LocationErrCode::ERRCODE_INVALID_PARAM,
37 "Parameter error. Possible causes:1.Mandatory parameters are left unspecified;" \
38 "2.Incorrect parameter types;3. Parameter verification failed."},
39 {LocationErrCode::ERRCODE_NOT_SUPPORTED,
40 "Capability not supported." \
41 "Failed to call function due to limited device capabilities."},
42 {LocationErrCode::ERRCODE_SERVICE_UNAVAILABLE, "The location service is unavailable."},
43 {LocationErrCode::ERRCODE_LOCATING_NETWORK_FAIL,
44 "The network locating is failed because the network cannot be accessed."},
45 {LocationErrCode::ERRCODE_LOCATING_ACC_FAIL,
46 "The positioning result does not meet the precision requirement (maxAccuracy)" \
47 " in the positioning request parameters. "},
48 {LocationErrCode::ERRCODE_LOCATING_CACHE_FAIL, "The system does not have a cache locaiton."},
49 {LocationErrCode::ERRCODE_SWITCH_OFF, "The location switch is off."},
50 {LocationErrCode::ERRCODE_LOCATING_FAIL, "Failed to obtain the geographical location."},
51 {LocationErrCode::ERRCODE_REVERSE_GEOCODING_FAIL, "Reverse geocoding query failed."},
52 {LocationErrCode::ERRCODE_GEOCODING_FAIL, "Geocoding query failed."},
53 {LocationErrCode::ERRCODE_COUNTRYCODE_FAIL, "Failed to query the area information."},
54 {LocationErrCode::ERRCODE_GEOFENCE_FAIL, "Failed to operate the geofence."},
55 {LocationErrCode::ERRCODE_NO_RESPONSE, "No response to the request."},
56 {LocationErrCode::ERRCODE_GEOFENCE_EXCEED_MAXIMUM, "The number of geofences exceeds the maximum."},
57 {LocationErrCode::ERRCODE_GEOFENCE_INCORRECT_ID, "Failed to delete a geofence due to an incorrect ID."},
58 {LocationErrCode::ERRCODE_WIFI_IS_NOT_CONNECTED,
59 "Failed to obtain the hotpot MAC address because the Wi-Fi is not connected."}
60 };
61 return errorCodeMap;
62 }
63
ConvertErrorCodeTaihe(int errorCode)64 int Util::ConvertErrorCodeTaihe(int errorCode)
65 {
66 if (errorCode == LocationErrCode::ERRCODE_LOCATING_NETWORK_FAIL ||
67 errorCode == LocationErrCode::ERRCODE_LOCATING_CACHE_FAIL ||
68 errorCode == LocationErrCode::ERRCODE_LOCATING_ACC_FAIL) {
69 LBSLOGI(LOCATOR_STANDARD, "Convert ErrorCode: %{public}d to %{public}d",
70 errorCode, LocationErrCode::ERRCODE_LOCATING_FAIL);
71 return LocationErrCode::ERRCODE_LOCATING_FAIL;
72 }
73 return errorCode;
74 }
75
GetErrorMsgByCodeTaihe(int code)76 std::string Util::GetErrorMsgByCodeTaihe(int code)
77 {
78 static std::map<int, std::string> errorCodeMap = GetErrorCodeMapTaihe();
79 auto iter = errorCodeMap.find(code);
80 if (iter != errorCodeMap.end()) {
81 std::string errMessage = "BussinessError ";
82 code = ConvertErrorCodeTaihe(code);
83 errMessage.append(std::to_string(code)).append(": ").append(iter->second);
84 return errMessage;
85 }
86 return "undefined error.";
87 }
88
ThrowBussinessError(int code)89 void Util::ThrowBussinessError(int code)
90 {
91 std::string errMsg = GetErrorMsgByCodeTaihe(code);
92 taihe::set_business_error(LocationErrCode::ERRCODE_NOT_SUPPORTED, errMsg);
93 }
94
LocationToTaihe(::ohos::geoLocationManager::Location & location,std::unique_ptr<Location> & lastlocation)95 void Util::LocationToTaihe(::ohos::geoLocationManager::Location& location, std::unique_ptr<Location>& lastlocation)
96 {
97 location.latitude = lastlocation->GetLatitude();
98 location.longitude = lastlocation->GetLongitude();
99 location.altitude = lastlocation->GetAltitude();
100 location.accuracy = lastlocation->GetAccuracy();
101 location.speed = lastlocation->GetSpeed();
102 location.direction = lastlocation->GetDirection();
103 location.timeStamp = lastlocation->GetTimeStamp();
104 location.timeSinceBoot = lastlocation->GetTimeSinceBoot();
105 location.additionSize = lastlocation->GetAdditionSize();
106 if (lastlocation->GetIsSystemApp() != 0) {
107 location.isFromMock = lastlocation->GetIsFromMock();
108 }
109 location.altitudeAccuracy = lastlocation->GetAltitudeAccuracy();
110 location.speedAccuracy = lastlocation->GetSpeedAccuracy();
111 location.directionAccuracy = lastlocation->GetDirectionAccuracy();
112 location.uncertaintyOfTimeSinceBoot = lastlocation->GetUncertaintyOfTimeSinceBoot();
113 location.sourceType =
114 static_cast<::ohos::geoLocationManager::LocationSourceType::key_t>(lastlocation->GetLocationSourceType());
115 }
116
TaiheCurrentRequestObjToRequestConfig(::taihe::optional_view<::ohos::geoLocationManager::CurrentRequest> request,std::unique_ptr<RequestConfig> & requestConfig)117 void Util::TaiheCurrentRequestObjToRequestConfig(
118 ::taihe::optional_view<::ohos::geoLocationManager::CurrentRequest> request,
119 std::unique_ptr<RequestConfig>& requestConfig)
120 {
121 if (!request) {
122 return;
123 }
124 if (request->get_tag() == ::ohos::geoLocationManager::CurrentRequest::tag_t::type_CurrentLocationRequest) {
125 ::ohos::geoLocationManager::CurrentLocationRequest currentLocationRequest =
126 request->get_type_CurrentLocationRequest_ref();
127 if (currentLocationRequest.priority) {
128 requestConfig->SetPriority(currentLocationRequest.priority.value());
129 }
130 if (currentLocationRequest.scenario) {
131 requestConfig->SetScenario(currentLocationRequest.scenario.value());
132 }
133 if (currentLocationRequest.maxAccuracy) {
134 requestConfig->SetMaxAccuracy(*currentLocationRequest.maxAccuracy);
135 }
136 if (currentLocationRequest.timeoutMs) {
137 requestConfig->SetTimeOut(*currentLocationRequest.timeoutMs);
138 }
139 } else {
140 ::ohos::geoLocationManager::SingleLocationRequest singleLocationRequest =
141 request->get_type_SingleLocationRequest_ref();
142 if (singleLocationRequest.locatingPriority) {
143 requestConfig->SetPriority(singleLocationRequest.locatingPriority.value());
144 }
145 if (singleLocationRequest.locatingTimeoutMs) {
146 requestConfig->SetTimeOut(*singleLocationRequest.locatingTimeoutMs);
147 }
148 }
149 }
150
GeoAddressToTaihe(std::vector<::ohos::geoLocationManager::GeoAddress> & geoAddressList,std::list<std::shared_ptr<GeoAddress>> replyList)151 void Util::GeoAddressToTaihe(std::vector<::ohos::geoLocationManager::GeoAddress>& geoAddressList,
152 std::list<std::shared_ptr<GeoAddress>> replyList)
153 {
154 for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
155 auto geoAddress = *iter;
156 uint32_t tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
157 uint64_t tokenIdEx = OHOS::IPCSkeleton::GetCallingFullTokenID();
158 bool flag = false;
159 if (PermissionManager::CheckSystemPermission(tokenId, tokenIdEx)) {
160 flag = true;
161 }
162 geoAddress->SetIsSystemApp(flag);
163 ::ohos::geoLocationManager::GeoAddress geoAddressTaihe = ::ohos::geoLocationManager::GeoAddress{};
164 geoAddressTaihe.latitude = ::taihe::optional<double>(std::in_place_t{}, geoAddress->latitude_);
165 geoAddressTaihe.longitude = geoAddress->longitude_;
166 geoAddressTaihe.locale = geoAddress->locale_;
167 geoAddressTaihe.placeName = geoAddress->placeName_;
168 geoAddressTaihe.countryCode = geoAddress->countryCode_;
169 geoAddressTaihe.countryName = geoAddress->countryName_;
170 geoAddressTaihe.administrativeArea = geoAddress->administrativeArea_;
171 geoAddressTaihe.subAdministrativeArea = geoAddress->subAdministrativeArea_;
172 geoAddressTaihe.locality = geoAddress->locality_;
173 geoAddressTaihe.subLocality = geoAddress->subLocality_;
174 geoAddressTaihe.roadName = geoAddress->roadName_;
175 geoAddressTaihe.subRoadName = geoAddress->subRoadName_;
176 geoAddressTaihe.premises = geoAddress->premises_;
177 geoAddressTaihe.postalCode = geoAddress->postalCode_;
178 geoAddressTaihe.phoneNumber = geoAddress->phoneNumber_;
179 geoAddressTaihe.addressUrl = geoAddress->addressUrl_;
180 geoAddressTaihe.descriptionsSize = geoAddress->descriptionsSize_;
181 if (geoAddress->GetIsSystemApp()) {
182 geoAddressTaihe.isFromMock = geoAddress->isFromMock_;
183 }
184 geoAddressList.push_back(geoAddressTaihe);
185 }
186 }
187
TaiheCurrentRequestObjToRequestConfig(::ohos::geoLocationManager::OnRequest const & request,std::unique_ptr<RequestConfig> & requestConfig)188 void Util::TaiheCurrentRequestObjToRequestConfig(::ohos::geoLocationManager::OnRequest const& request,
189 std::unique_ptr<RequestConfig>& requestConfig)
190 {
191 if (request.get_tag() == ::ohos::geoLocationManager::OnRequest::tag_t::type_LocationRequest) {
192 ::ohos::geoLocationManager::LocationRequest locationRequest =
193 request.get_type_LocationRequest_ref();
194 if (locationRequest.priority) {
195 requestConfig->SetPriority(locationRequest.priority.value());
196 }
197 if (locationRequest.scenario) {
198 requestConfig->SetScenario(locationRequest.scenario.value());
199 }
200 if (locationRequest.timeInterval) {
201 requestConfig->SetTimeInterval(*locationRequest.timeInterval);
202 }
203 if (locationRequest.distanceInterval) {
204 requestConfig->SetDistanceInterval(*locationRequest.distanceInterval);
205 }
206 if (locationRequest.maxAccuracy) {
207 requestConfig->SetMaxAccuracy(*locationRequest.maxAccuracy);
208 }
209 } else {
210 ::ohos::geoLocationManager::ContinuousLocationRequest continuousLocationRequest =
211 request.get_type_ContinuousLocationRequest_ref();
212 int scenario;
213 if (continuousLocationRequest.locationScenario.get_tag() ==
214 ohos::geoLocationManager::ContinuousLocationRequestLocationScenario::tag_t::type_UserActivityScenario) {
215 scenario = continuousLocationRequest.locationScenario.get_type_UserActivityScenario_ref().get_value();
216 } else {
217 scenario = continuousLocationRequest.locationScenario.get_type_PowerConsumptionScenario_ref().get_value();
218 }
219 requestConfig->SetScenario(scenario);
220 requestConfig->SetTimeInterval(continuousLocationRequest.interval);
221 }
222 }
223
LocatingRequiredDataToTaihe(std::vector<::ohos::geoLocationManager::LocatingRequiredData> & locatingRequiredDataList,const std::vector<std::shared_ptr<LocatingRequiredData>> & replyList)224 void Util::LocatingRequiredDataToTaihe(
225 std::vector<::ohos::geoLocationManager::LocatingRequiredData>& locatingRequiredDataList,
226 const std::vector<std::shared_ptr<LocatingRequiredData>>& replyList)
227 {
228 for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
229 auto locatingRequiredData = *iter;
230 ::ohos::geoLocationManager::WifiScanInfo wifiScanInfoTaihe =
231 ::ohos::geoLocationManager::WifiScanInfo{};
232 wifiScanInfoTaihe.ssid = locatingRequiredData->GetWifiScanInfo()->GetSsid();
233 wifiScanInfoTaihe.bssid = locatingRequiredData->GetWifiScanInfo()->GetBssid();
234 wifiScanInfoTaihe.rssi = locatingRequiredData->GetWifiScanInfo()->GetRssi();
235 wifiScanInfoTaihe.frequency = locatingRequiredData->GetWifiScanInfo()->GetFrequency();
236 wifiScanInfoTaihe.timestamp = locatingRequiredData->GetWifiScanInfo()->GetTimestamp();
237 locatingRequiredDataList.push_back({
238 ::taihe::optional<::ohos::geoLocationManager::WifiScanInfo>{std::in_place_t{},
239 wifiScanInfoTaihe},
240 {}});
241 }
242 }
243
SatelliteStatusInfoToTaihe(::ohos::geoLocationManager::SatelliteStatusInfo & satelliteStatusInfo,const std::unique_ptr<SatelliteStatus> & statusInfo)244 void Util::SatelliteStatusInfoToTaihe(::ohos::geoLocationManager::SatelliteStatusInfo& satelliteStatusInfo,
245 const std::unique_ptr<SatelliteStatus>& statusInfo)
246 {
247 satelliteStatusInfo.satellitesNumber = statusInfo->GetSatellitesNumber();
248 satelliteStatusInfo.satelliteIds =
249 ::taihe::array<double>{taihe::copy_data_t{},
250 statusInfo->GetSatelliteIds().data(), statusInfo->GetSatelliteIds().size()};
251 satelliteStatusInfo.carrierToNoiseDensitys =
252 ::taihe::array<double>{taihe::copy_data_t{},
253 statusInfo->GetCarrierToNoiseDensitys().data(), statusInfo->GetCarrierToNoiseDensitys().size()};
254 satelliteStatusInfo.altitudes =
255 ::taihe::array<double>{taihe::copy_data_t{},
256 statusInfo->GetAltitudes().data(), statusInfo->GetAltitudes().size()};
257 satelliteStatusInfo.azimuths =
258 ::taihe::array<double>{taihe::copy_data_t{},
259 statusInfo->GetAzimuths().data(), statusInfo->GetAzimuths().size()};
260 satelliteStatusInfo.carrierFrequencies =
261 ::taihe::array<double>{taihe::copy_data_t{},
262 statusInfo->GetCarrierFrequencies().data(), statusInfo->GetCarrierFrequencies().size()};
263 std::vector<::ohos::geoLocationManager::SatelliteConstellationCategory> satelliteConstellationCategoryList;
264 for (auto item : statusInfo->GetConstellationTypes()) {
265 satelliteConstellationCategoryList.push_back(
266 static_cast<::ohos::geoLocationManager::SatelliteConstellationCategory::key_t>(item));
267 }
268 satelliteStatusInfo.satelliteConstellation =
269 ::taihe::optional<::taihe::array<::ohos::geoLocationManager::SatelliteConstellationCategory>>{
270 std::in_place_t{},
271 ::taihe::array<::ohos::geoLocationManager::SatelliteConstellationCategory>{
272 taihe::copy_data_t{},
273 satelliteConstellationCategoryList.data(),
274 satelliteConstellationCategoryList.size()}
275 };
276 satelliteStatusInfo.satelliteAdditionalInfo =
277 ::taihe::optional<::taihe::array<double>>{
278 std::in_place_t{},
279 ::taihe::array<double>{taihe::copy_data_t{},
280 statusInfo->GetSatelliteAdditionalInfoList().data(),
281 statusInfo->GetSatelliteAdditionalInfoList().size()}
282 };
283 }
284
BluetoothScanResultToTaihe(::ohos::geoLocationManager::BluetoothScanResult & bluetoothScanResultTaihe,const std::unique_ptr<BluetoothScanResult> & bluetoothScanResult)285 void Util::BluetoothScanResultToTaihe(::ohos::geoLocationManager::BluetoothScanResult& bluetoothScanResultTaihe,
286 const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult)
287 {
288 bluetoothScanResultTaihe.deviceId = bluetoothScanResult->GetDeviceId();
289 bluetoothScanResultTaihe.deviceName = bluetoothScanResult->GetDeviceName();
290 bluetoothScanResultTaihe.rssi = bluetoothScanResult->GetRssi();
291 bluetoothScanResultTaihe.connectable = bluetoothScanResult->GetConnectable();
292 }
293 }
294 }