• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "location_callback_taihe.h"
17 
18 #include "ipc_object_stub.h"
19 #include "ipc_skeleton.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 
23 #include "common_utils.h"
24 #include "constant_definition.h"
25 #include "i_locator_callback.h"
26 #include "location.h"
27 #include "location_log.h"
28 #include "util.h"
29 
30 namespace OHOS {
31 namespace Location {
LocatorCallbackTaihe()32 LocatorCallbackTaihe::LocatorCallbackTaihe()
33 {
34     fixNumber_ = 0;
35     inHdArea_ = true;
36     singleLocation_ = nullptr;
37     locationPriority_ = 0;
38     InitLatch();
39 }
40 
InitLatch()41 void LocatorCallbackTaihe::InitLatch()
42 {
43     latch_ = new CountDownLatch();
44     latch_->SetCount(1);
45 }
46 
~LocatorCallbackTaihe()47 LocatorCallbackTaihe::~LocatorCallbackTaihe()
48 {
49     delete latch_;
50     LBSLOGW(LOCATOR_CALLBACK, "~LocatorCallbackTaihe()");
51 }
52 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int LocatorCallbackTaihe::OnRemoteRequest(uint32_t code,
54     MessageParcel& data, MessageParcel& reply, MessageOption& option)
55 {
56     if (data.ReadInterfaceToken() != GetDescriptor()) {
57         LBSLOGE(LOCATOR_CALLBACK, "invalid token.");
58         return -1;
59     }
60 
61     switch (code) {
62         case RECEIVE_LOCATION_INFO_EVENT: {
63             std::unique_ptr<Location> location = Location::UnmarshallingMakeUnique(data);
64             OnLocationReport(location);
65             if (location->GetLocationSourceType() == LocationSourceType::NETWORK_TYPE &&
66                 location->GetAdditionsMap()["inHdArea"] != "") {
67                 inHdArea_ = (location->GetAdditionsMap()["inHdArea"] == "true");
68             }
69             if (NeedSetSingleLocation(location)) {
70                 SetSingleLocation(location);
71             }
72             if (IfReportAccuracyLocation()) {
73                 CountDown();
74             }
75             break;
76         }
77         case RECEIVE_LOCATION_STATUS_EVENT: {
78             int status = data.ReadInt32();
79             OnLocatingStatusChange(status);
80             break;
81         }
82         case RECEIVE_ERROR_INFO_EVENT: {
83             int errorCode = data.ReadInt32();
84             LBSLOGI(LOCATOR_STANDARD, "CallbackSutb receive ERROR_EVENT. errorCode:%{public}d", errorCode);
85             if (errorCode == LOCATING_FAILED_INTERNET_ACCESS_FAILURE) {
86                 inHdArea_ = false;
87                 if (GetSingleLocation() != nullptr) {
88                     CountDown();
89                 }
90             } else {
91                 OnErrorReport(errorCode);
92             }
93             break;
94         }
95         default: {
96             IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97             break;
98         }
99     }
100     return 0;
101 }
102 
OnLocationReport(const std::unique_ptr<Location> & location)103 void LocatorCallbackTaihe::OnLocationReport(const std::unique_ptr<Location>& location)
104 {
105     std::unique_ptr<Location> locationReport = std::make_unique<Location>(*location);
106     ::taihe::map<::taihe::string, ::taihe::string> ret;
107     ::ohos::geoLocationManager::Location locationTaihe = {
108         0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0, {"", ""}, ret, 0, true, 0.0, 0.0, 0.0, 0,
109         static_cast<::ohos::geoLocationManager::LocationSourceType::key_t>(1) };
110     Util::LocationToTaihe(locationTaihe, locationReport);
111     if (callback_) {
112         (*callback_)(locationTaihe);
113     }
114 }
115 
OnLocatingStatusChange(const int status)116 void LocatorCallbackTaihe::OnLocatingStatusChange(const int status)
117 {
118 }
119 
OnErrorReport(const int errorCode)120 void LocatorCallbackTaihe::OnErrorReport(const int errorCode)
121 {
122 }
123 
DeleteAllCallbacks()124 void LocatorCallbackTaihe::DeleteAllCallbacks()
125 {
126 }
127 
IsSingleLocationRequest()128 bool LocatorCallbackTaihe::IsSingleLocationRequest()
129 {
130     return (fixNumber_ == 1);
131 }
132 
CountDown()133 void LocatorCallbackTaihe::CountDown()
134 {
135     if (IsSingleLocationRequest() && latch_ != nullptr) {
136         latch_->CountDown();
137     }
138 }
139 
Wait(int time)140 void LocatorCallbackTaihe::Wait(int time)
141 {
142     if (IsSingleLocationRequest() && latch_ != nullptr) {
143         latch_->Wait(time);
144     }
145 }
146 
GetCount()147 int LocatorCallbackTaihe::GetCount()
148 {
149     if (IsSingleLocationRequest() && latch_ != nullptr) {
150         return latch_->GetCount();
151     }
152     return 0;
153 }
154 
SetCount(int count)155 void LocatorCallbackTaihe::SetCount(int count)
156 {
157     if (IsSingleLocationRequest() && latch_ != nullptr) {
158         return latch_->SetCount(count);
159     }
160 }
161 
NeedSetSingleLocation(const std::unique_ptr<Location> & location)162 bool LocatorCallbackTaihe::NeedSetSingleLocation(const std::unique_ptr<Location>& location)
163 {
164     if (locationPriority_ == LOCATION_PRIORITY_ACCURACY &&
165         singleLocation_ != nullptr &&
166         location->GetLocationSourceType() == LocationSourceType::NETWORK_TYPE) {
167         return false;
168     } else {
169         return true;
170     }
171 }
172 
IfReportAccuracyLocation()173 bool LocatorCallbackTaihe::IfReportAccuracyLocation()
174 {
175     if (locationPriority_ == LOCATION_PRIORITY_ACCURACY &&
176         (((singleLocation_->GetLocationSourceType() == LocationSourceType::GNSS_TYPE ||
177         singleLocation_->GetLocationSourceType() == LocationSourceType::RTK_TYPE) && inHdArea_) ||
178         singleLocation_->GetLocationSourceType() == LocationSourceType::NETWORK_TYPE)) {
179         return false;
180     } else {
181         return true;
182     }
183 }
184 
SetSingleLocation(const std::unique_ptr<Location> & location)185 void LocatorCallbackTaihe::SetSingleLocation(const std::unique_ptr<Location>& location)
186 {
187     std::unique_lock<std::mutex> guard(mutex_);
188     singleLocation_ = std::make_shared<Location>(*location);
189 }
190 } // namespace Location
191 } // namespace OHOS
192