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 "geolocationmanager_utils.h"
17
18 namespace OHOS {
19 namespace GeoLocationManager {
MallocCString(const std::string & origin)20 char* MallocCString(const std::string& origin)
21 {
22 if (origin.empty()) {
23 return nullptr;
24 }
25 auto len = origin.length() + 1;
26 char* res = static_cast<char*>(malloc(sizeof(char) * len));
27 if (res == nullptr) {
28 return nullptr;
29 }
30 return std::char_traits<char>::copy(res, origin.c_str(), len);
31 }
32
StringVectorToCPointer(const std::vector<std::string> & arr)33 char** StringVectorToCPointer(const std::vector<std::string>& arr)
34 {
35 if (arr.size() == 0) {
36 return nullptr;
37 }
38 char** res = static_cast<char**>(malloc(sizeof(char*) * arr.size()));
39 if (res == nullptr) {
40 return nullptr;
41 }
42 for (size_t i = 0; i < arr.size(); i++) {
43 res[i] = MallocCString(arr[i]);
44 }
45 return res;
46 }
47
MapToCMapStringString(const std::map<std::string,std::string> & map)48 CMapStringString MapToCMapStringString(const std::map<std::string, std::string>& map)
49 {
50 CMapStringString cMap{0};
51 cMap.keys = static_cast<char**>(malloc(sizeof(char*) * map.size()));
52 cMap.values = static_cast<char**>(malloc(sizeof(char*) * map.size()));
53 if (cMap.keys == nullptr || cMap.values == nullptr) {
54 free(cMap.keys);
55 free(cMap.values);
56 return CMapStringString{0};
57 }
58 int64_t index = 0;
59 for (auto it = map.begin(); it != map.end(); ++it) {
60 cMap.keys[index] = MallocCString(it->first);
61 cMap.values[index] = MallocCString(it->second);
62 index++;
63 }
64 return cMap;
65 }
66
NativeLocationToCJLocation(const Location::Location & loc)67 CJLocation NativeLocationToCJLocation(const Location::Location& loc)
68 {
69 return CJLocation{ .latitude = loc.GetLatitude(), .longitude = loc.GetLongitude(),
70 .altitude = loc.GetAltitude(), .accuracy = loc.GetAccuracy(), .speed = loc.GetSpeed(),
71 .direction = loc.GetDirection(), .timeStamp = loc.GetTimeStamp(), .timeSinceBoot = loc.GetTimeSinceBoot(),
72 .additions = StringVectorToCPointer(loc.GetAdditions()), .additionSize = loc.GetAdditionSize(),
73 .additionsMap = MapToCMapStringString(loc.GetAdditionsMap()), .altitudeAccuracy = loc.GetAltitudeAccuracy(),
74 .speedAccuracy = loc.GetSpeedAccuracy(), .directionAccuracy = loc.GetDirectionAccuracy(),
75 .uncertaintyOfTimeSinceBoot = loc.GetUncertaintyOfTimeSinceBoot(), .sourceType = loc.GetLocationSourceType()
76 };
77 }
78
CJCurrentLocationRequestToRequestConfig(const CJCurrentLocationRequest & request,std::unique_ptr<Location::RequestConfig> & requestConfig)79 void CJCurrentLocationRequestToRequestConfig(const CJCurrentLocationRequest& request,
80 std::unique_ptr<Location::RequestConfig>& requestConfig)
81 {
82 requestConfig->SetPriority(request.priority);
83 requestConfig->SetScenario(request.scenario);
84 requestConfig->SetMaxAccuracy(request.maxAccuracy);
85 requestConfig->SetTimeOut(request.timeoutMs);
86 }
87
CJSingleLocationRequestRequestToRequestConfig(const CJSingleLocationRequest & request,std::unique_ptr<Location::RequestConfig> & requestConfig)88 void CJSingleLocationRequestRequestToRequestConfig(const CJSingleLocationRequest& request,
89 std::unique_ptr<Location::RequestConfig>& requestConfig)
90 {
91 requestConfig->SetPriority(request.locatingPriority);
92 requestConfig->SetTimeOut(request.locatingTimeoutMs);
93 }
94
CJReverseGeoCodeRequestToMessageParcel(CJReverseGeoCodeRequest & request,MessageParcel & dataParcel)95 bool CJReverseGeoCodeRequestToMessageParcel(CJReverseGeoCodeRequest& request, MessageParcel& dataParcel)
96 {
97 double latitude = request.latitude;
98 double longitude = request.longitude;
99 int maxItems = request.maxItems;
100 std::string locale = request.locale;
101 std::string country = request.country;
102 if (latitude < Location::MIN_LATITUDE || latitude > Location::MAX_LATITUDE) {
103 return false;
104 }
105 if (longitude < Location::MIN_LONGITUDE || longitude > Location::MAX_LONGITUDE) {
106 return false;
107 }
108 if (!dataParcel.WriteInterfaceToken(Location::LocatorProxy::GetDescriptor())) {
109 return false;
110 }
111 dataParcel.WriteString16(Str8ToStr16(locale)); // locale
112 dataParcel.WriteDouble(latitude); // latitude
113 dataParcel.WriteDouble(longitude); // longitude
114 dataParcel.WriteInt32(maxItems); // maxItems
115 dataParcel.WriteString16(Str8ToStr16(Location::CommonUtils::GenerateUuid())); // transId
116 dataParcel.WriteString16(Str8ToStr16(country)); // country
117 return true;
118 }
119
HandleDescriptions(std::shared_ptr<Location::GeoAddress> & geoAddress,CJGeoAddress & eachObj)120 void HandleDescriptions(std::shared_ptr<Location::GeoAddress>& geoAddress, CJGeoAddress& eachObj)
121 {
122 if (geoAddress->descriptionsSize_ > 0) {
123 char** descriptionArray = static_cast<char**>(malloc(geoAddress->descriptionsSize_ * sizeof(char*)));
124 if (descriptionArray == nullptr) {
125 eachObj.descriptionsSize = 0;
126 } else {
127 for (int index = 0; index < geoAddress->descriptionsSize_; index++) {
128 descriptionArray[index] = MallocCString(geoAddress->GetDescriptions(index).c_str());
129 }
130 }
131 eachObj.descriptions = descriptionArray;
132 }
133 }
134
ListGeoAddressToCJGeoAddressArr(std::list<std::shared_ptr<Location::GeoAddress>> & replyList)135 CJGeoAddressArr ListGeoAddressToCJGeoAddressArr(std::list<std::shared_ptr<Location::GeoAddress>>& replyList)
136 {
137 CJGeoAddressArr ret{0};
138 if (replyList.size() == 0) {
139 return ret;
140 }
141 ret.head = static_cast<CJGeoAddress*>(malloc(replyList.size() * sizeof(CJGeoAddress)));
142 if (ret.head == nullptr) {
143 return ret;
144 }
145 ret.size = replyList.size();
146 uint32_t idx = 0;
147 for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
148 auto geoAddress = *iter;
149 CJGeoAddress eachObj{0};
150 eachObj.latitude = geoAddress->GetLatitude();
151 eachObj.longitude = geoAddress->GetLongitude();
152 eachObj.locale = MallocCString(geoAddress->locale_.c_str());
153 eachObj.placeName = MallocCString(geoAddress->placeName_.c_str());
154 eachObj.countryCode = MallocCString(geoAddress->countryCode_.c_str());
155 eachObj.countryName = MallocCString(geoAddress->countryName_.c_str());
156 eachObj.administrativeArea = MallocCString(geoAddress->administrativeArea_.c_str());
157 eachObj.subAdministrativeArea = MallocCString(geoAddress->subAdministrativeArea_.c_str());
158 eachObj.locality = MallocCString(geoAddress->locality_.c_str());
159 eachObj.subLocality = MallocCString(geoAddress->subLocality_.c_str());
160 eachObj.roadName = MallocCString(geoAddress->roadName_.c_str());
161 eachObj.subRoadName = MallocCString(geoAddress->subRoadName_.c_str());
162 eachObj.premises = MallocCString(geoAddress->premises_.c_str());
163 eachObj.postalCode = MallocCString(geoAddress->postalCode_.c_str());
164 eachObj.phoneNumber = MallocCString(geoAddress->phoneNumber_.c_str());
165 eachObj.addressUrl = MallocCString(geoAddress->addressUrl_.c_str());
166 eachObj.descriptionsSize = geoAddress->descriptionsSize_;
167 HandleDescriptions(geoAddress, eachObj);
168 ret.head[idx] = eachObj;
169 idx++;
170 }
171 return ret;
172 }
173
CJGeoCodeRequestToMessageParcel(CJGeoCodeRequest & request,MessageParcel & dataParcel)174 bool CJGeoCodeRequestToMessageParcel(CJGeoCodeRequest& request, MessageParcel& dataParcel)
175 {
176 std::string locale = request.locale;
177 std::string country = request.country;
178 std::string description = request.description;
179 if (description == "") {
180 LBSLOGE(Location::LOCATOR_STANDARD, "The required description field should not be empty.");
181 return false;
182 }
183 int32_t maxItems = request.maxItems;
184 double minLatitude = request.minLatitude;
185 double minLongitude = request.minLongitude;
186 double maxLatitude = request.maxLatitude;
187 double maxLongitude = request.maxLongitude;
188 if (minLatitude < Location::MIN_LATITUDE || minLatitude > Location::MAX_LATITUDE) {
189 return false;
190 }
191 if (minLongitude < Location::MIN_LONGITUDE || minLongitude > Location::MAX_LONGITUDE) {
192 return false;
193 }
194 if (maxLatitude < Location::MIN_LATITUDE || maxLatitude > Location::MAX_LATITUDE) {
195 return false;
196 }
197 if (maxLongitude < Location::MIN_LONGITUDE || maxLongitude > Location::MAX_LONGITUDE) {
198 return false;
199 }
200 if (!dataParcel.WriteInterfaceToken(Location::LocatorProxy::GetDescriptor())) {
201 LBSLOGE(Location::LOCATOR_STANDARD, "write interfaceToken fail!");
202 return false;
203 }
204 dataParcel.WriteString16(Str8ToStr16(locale)); // locale
205 dataParcel.WriteString16(Str8ToStr16(description)); // description
206 dataParcel.WriteInt32(maxItems); // maxItems
207 dataParcel.WriteDouble(minLatitude); // latitude
208 dataParcel.WriteDouble(minLongitude); // longitude
209 dataParcel.WriteDouble(maxLatitude); // latitude
210 dataParcel.WriteDouble(maxLongitude); // longitude
211 dataParcel.WriteString16(Str8ToStr16(Location::CommonUtils::GenerateUuid())); // transId
212 dataParcel.WriteString16(Str8ToStr16(country)); // country
213 return true;
214 }
215
CJLocationRequestToRequestConfig(CJLocationRequest & request,std::unique_ptr<Location::RequestConfig> & requestConfig)216 void CJLocationRequestToRequestConfig(CJLocationRequest& request,
217 std::unique_ptr<Location::RequestConfig>& requestConfig)
218 {
219 requestConfig->SetPriority(request.priority);
220 requestConfig->SetScenario(request.scenario);
221 if (request.timeInterval >= 0 && request.timeInterval < 1) {
222 requestConfig->SetTimeInterval(1);
223 } else {
224 requestConfig->SetTimeInterval(request.timeInterval);
225 }
226 requestConfig->SetMaxAccuracy(request.maxAccuracy);
227 requestConfig->SetDistanceInterval(request.distanceInterval);
228 }
229
CJContinuousLocationRequestToRequestConfig(CJContinuousLocationRequest request,std::unique_ptr<Location::RequestConfig> & requestConfig)230 void CJContinuousLocationRequestToRequestConfig(CJContinuousLocationRequest request,
231 std::unique_ptr<Location::RequestConfig>& requestConfig)
232 {
233 requestConfig->SetScenario(request.locationScenario);
234 if (request.interval >= 0 && request.interval < 1) {
235 requestConfig->SetTimeInterval(1);
236 } else {
237 requestConfig->SetTimeInterval(request.interval);
238 }
239 }
240
LocationVectorToCJLocationArr(const std::vector<std::unique_ptr<Location::Location>> & locations)241 CJLocationArr LocationVectorToCJLocationArr(const std::vector<std::unique_ptr<Location::Location>>& locations)
242 {
243 if (locations.size() == 0) {
244 return CJLocationArr{0};
245 }
246 CJLocation* head = static_cast<CJLocation*>(malloc(sizeof(CJLocation) * locations.size()));
247 if (head == nullptr) {
248 return CJLocationArr{0};
249 }
250 for (size_t i = 0; i < locations.size(); i++) {
251 head[i] = NativeLocationToCJLocation(*(locations[i]));
252 }
253 return CJLocationArr{ .head = head, .size = locations.size() };
254 }
255
CJCachedGnssLocationsRequestToCachedLocationRequest(CJCachedGnssLocationsRequest & request,std::unique_ptr<Location::CachedGnssLocationsRequest> & requestConfig)256 void CJCachedGnssLocationsRequestToCachedLocationRequest(CJCachedGnssLocationsRequest& request,
257 std::unique_ptr<Location::CachedGnssLocationsRequest>& requestConfig)
258 {
259 requestConfig->reportingPeriodSec = request.reportingPeriodSec;
260 requestConfig->wakeUpCacheQueueFull = request.wakeUpCacheQueueFull;
261 }
262
IntVectorToCArrI32(std::vector<int> arr)263 CArrI32 IntVectorToCArrI32(std::vector<int> arr)
264 {
265 if (arr.size() == 0) {
266 return CArrI32{0};
267 }
268 int32_t* head = static_cast<int32_t*>(malloc(sizeof(int32_t) * arr.size()));
269 if (head == nullptr) {
270 return CArrI32{0};
271 }
272 for (size_t i = 0; i < arr.size(); i++) {
273 head[i] = arr[i];
274 }
275 return CArrI32{ .head = head, .size = arr.size() };
276 }
277
DoubleVectorToCArrF64(std::vector<double> arr)278 CArrF64 DoubleVectorToCArrF64(std::vector<double> arr)
279 {
280 if (arr.size() == 0) {
281 return CArrF64{0};
282 }
283 double* head = static_cast<double*>(malloc(sizeof(double) * arr.size()));
284 if (head == nullptr) {
285 return CArrF64{0};
286 }
287 for (size_t i = 0; i < arr.size(); i++) {
288 head[i] = arr[i];
289 }
290 return CArrF64{ .head = head, .size = arr.size() };
291 }
292
SatelliteStatusInfoToCJSatelliteStatus(const std::unique_ptr<Location::SatelliteStatus> & statusInfo)293 CJSatelliteStatusInfo SatelliteStatusInfoToCJSatelliteStatus(const std::unique_ptr<Location::SatelliteStatus>&
294 statusInfo)
295 {
296 CJSatelliteStatusInfo res = CJSatelliteStatusInfo{0};
297 res.satellitesNumber = statusInfo->GetSatellitesNumber();
298 res.satelliteIds = IntVectorToCArrI32(statusInfo->GetSatelliteIds());
299 res.carrierToNoiseDensitys = DoubleVectorToCArrF64(statusInfo->GetCarrierToNoiseDensitys());
300 res.altitudes = DoubleVectorToCArrF64(statusInfo->GetAltitudes());
301 res.azimuths = DoubleVectorToCArrF64(statusInfo->GetAzimuths());
302 res.carrierFrequencies = DoubleVectorToCArrF64(statusInfo->GetCarrierFrequencies());
303 res.constellationTypes = IntVectorToCArrI32(statusInfo->GetConstellationTypes());
304 res.additionalInfoList = IntVectorToCArrI32(statusInfo->GetSatelliteAdditionalInfoList());
305 return res;
306 }
307
CountryCodeToCJCountryCode(const std::shared_ptr<Location::CountryCode> & country)308 CJCountryCode CountryCodeToCJCountryCode(const std::shared_ptr<Location::CountryCode>& country)
309 {
310 return CJCountryCode{ .country = MallocCString(country->GetCountryCodeStr()),
311 .type = country->GetCountryCodeType() };
312 }
313 }
314 }