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