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 "location_info_callback_host.h"
17 
18 #include "ipc_object_stub.h"
19 #include "ipc_skeleton.h"
20 
21 #include "location.h"
22 #include "location_log.h"
23 #include "oh_location_type.h"
24 #include "locator_c_impl.h"
25 #include <nlohmann/json.hpp>
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace Location {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int LocationInfoCallbackHost::OnRemoteRequest(uint32_t code,
31     MessageParcel& data, MessageParcel& reply, MessageOption& option)
32 {
33     if (data.ReadInterfaceToken() != GetDescriptor()) {
34         LBSLOGE(LOCATOR_CALLBACK, "invalid token.");
35         return -1;
36     }
37 
38     switch (code) {
39         case RECEIVE_LOCATION_INFO_EVENT: {
40             std::unique_ptr<Location> location = Location::Unmarshalling(data);
41             OnLocationReport(location);
42             break;
43         }
44         default: {
45             IPCObjectStub::OnRemoteRequest(code, data, reply, option);
46             break;
47         }
48     }
49     return 0;
50 }
51 
OnLocationReport(const std::unique_ptr<Location> & location)52 void LocationInfoCallbackHost::OnLocationReport(const std::unique_ptr<Location>& location)
53 {
54     LBSLOGI(LOCATOR_CALLBACK, "LocationInfoCallbackHost::OnLocationReport");
55     Location_Info location_info;
56     memset_s(&location_info, sizeof(Location_Info), 0, sizeof(Location_Info));
57     location_info.latitude = location->GetLatitude();
58     location_info.longitude = location->GetLongitude();
59     location_info.altitude = location->GetAltitude();
60     location_info.accuracy = location->GetAccuracy();
61     location_info.speed = location->GetSpeed();
62     location_info.direction = location->GetDirection();
63     location_info.timeForFix = location->GetTimeStamp();
64     location_info.timeSinceBoot = location->GetTimeSinceBoot();
65     nlohmann::json additionJson;
66     auto additionMap = location->GetAdditionsMap();
67     for (auto addition : additionMap) {
68         additionJson[addition.first] = addition.second;
69     }
70     std::string additionStr = additionJson.dump();
71     auto ret = sprintf_s(location_info.additions, sizeof(location_info.additions), "%s", additionStr.c_str());
72     if (ret <= 0) {
73         LBSLOGE(OHOS::Location::LOCATION_CAPI, "sprintf_s failed, ret: %{public}d", ret);
74         // addition is empty, no need return
75     }
76     location_info.altitudeAccuracy = location->GetAltitudeAccuracy();
77     location_info.speedAccuracy = location->GetSpeedAccuracy();
78     location_info.directionAccuracy = location->GetDirectionAccuracy();
79     location_info.uncertaintyOfTimeSinceBoot = location->GetUncertaintyOfTimeSinceBoot();
80     location_info.locationSourceType = (Location_SourceType)location->GetLocationSourceType();
81     if (requestConfig_ != nullptr && requestConfig_->callback_ != nullptr && requestConfig_->userData_ != nullptr) {
82         requestConfig_->callback_(&location_info, requestConfig_->userData_);
83     }
84 }
85 
OnLocatingStatusChange(const int status)86 void LocationInfoCallbackHost::OnLocatingStatusChange(const int status)
87 {
88 }
89 
OnErrorReport(const int errorCode)90 void LocationInfoCallbackHost::OnErrorReport(const int errorCode)
91 {
92 }
93 } // namespace Location
94 } // namespace OHOS
95