1 /*
2 * Copyright (C) 2022 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 #ifdef FEATURE_PASSIVE_SUPPORT
17 #include "passive_ability.h"
18
19 #include <file_ex.h>
20
21 #include "singleton.h"
22 #include "string_ex.h"
23 #include "system_ability.h"
24 #include "system_ability_definition.h"
25
26 #include "common_utils.h"
27 #include "location.h"
28 #include "location_dumper.h"
29 #include "location_log.h"
30 #include "location_sa_load_manager.h"
31 #include "work_record.h"
32 #include "locationhub_ipc_interface_code.h"
33
34 namespace OHOS {
35 namespace Location {
36 const uint32_t EVENT_INTERVAL_UNITE = 1000;
37 const uint32_t RETRY_INTERVAL_OF_UNLOAD_SA = 4 * 60 * EVENT_INTERVAL_UNITE;
38 const std::string UNLOAD_PASSIVE_TASK = "passive_sa_unload";
39 const bool REGISTER_RESULT = PassiveAbility::MakeAndRegisterAbility(
40 PassiveAbility::GetInstance());
41
GetInstance()42 PassiveAbility* PassiveAbility::GetInstance()
43 {
44 static PassiveAbility data;
45 return &data;
46 }
47
PassiveAbility()48 PassiveAbility::PassiveAbility() : SystemAbility(LOCATION_NOPOWER_LOCATING_SA_ID, true)
49 {
50 SetAbility(PASSIVE_ABILITY);
51 passiveHandler_ =
52 std::make_shared<PassiveHandler>(AppExecFwk::EventRunner::Create(true, AppExecFwk::ThreadMode::FFRT));
53 LBSLOGI(PASSIVE, "ability constructed.");
54 }
55
~PassiveAbility()56 PassiveAbility::~PassiveAbility() {}
57
OnStart()58 void PassiveAbility::OnStart()
59 {
60 if (state_ == ServiceRunningState::STATE_RUNNING) {
61 LBSLOGI(PASSIVE, "ability has already started.");
62 return;
63 }
64 if (!Init()) {
65 LBSLOGE(PASSIVE, "failed to init ability");
66 OnStop();
67 return;
68 }
69 state_ = ServiceRunningState::STATE_RUNNING;
70 LBSLOGI(PASSIVE, "OnStart start ability success.");
71 }
72
OnStop()73 void PassiveAbility::OnStop()
74 {
75 state_ = ServiceRunningState::STATE_NOT_START;
76 registerToAbility_ = false;
77 LBSLOGI(PASSIVE, "OnStop ability stopped.");
78 }
79
Init()80 bool PassiveAbility::Init()
81 {
82 if (!registerToAbility_) {
83 bool ret = Publish(AsObject());
84 if (!ret) {
85 LBSLOGE(PASSIVE, "Init Publish failed!");
86 return false;
87 }
88 registerToAbility_ = true;
89 }
90 return true;
91 }
92
SendLocationRequest(WorkRecord & workrecord)93 LocationErrCode PassiveAbility::SendLocationRequest(WorkRecord &workrecord)
94 {
95 LocationRequest(workrecord);
96 return ERRCODE_SUCCESS;
97 }
98
SetEnable(bool state)99 LocationErrCode PassiveAbility::SetEnable(bool state)
100 {
101 return ERRCODE_SUCCESS;
102 }
103
CancelIdleState()104 bool PassiveAbility::CancelIdleState()
105 {
106 bool ret = CancelIdle();
107 if (!ret) {
108 LBSLOGE(PASSIVE, "%{public}s cancel idle failed!", __func__);
109 return false;
110 }
111 return true;
112 }
113
UnloadPassiveSystemAbility()114 void PassiveAbility::UnloadPassiveSystemAbility()
115 {
116 if (passiveHandler_ == nullptr) {
117 LBSLOGE(PASSIVE, "%{public}s passiveHandler is nullptr", __func__);
118 return;
119 }
120 passiveHandler_->RemoveTask(UNLOAD_PASSIVE_TASK);
121 if (CheckIfPassiveConnecting()) {
122 return;
123 }
124 auto task = [this]() {
125 SaLoadWithStatistic::UnInitLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
126 };
127 if (passiveHandler_ != nullptr) {
128 passiveHandler_->PostTask(task, UNLOAD_PASSIVE_TASK, RETRY_INTERVAL_OF_UNLOAD_SA);
129 }
130 }
131
CheckIfPassiveConnecting()132 bool PassiveAbility::CheckIfPassiveConnecting()
133 {
134 return IsMockEnabled() || !GetLocationMock().empty();
135 }
136
RequestRecord(WorkRecord & workRecord,bool isAdded)137 void PassiveAbility::RequestRecord(WorkRecord &workRecord, bool isAdded)
138 {
139 LBSLOGE(PASSIVE, "enter RequestRecord");
140 }
141
EnableMock()142 LocationErrCode PassiveAbility::EnableMock()
143 {
144 if (!EnableLocationMock()) {
145 return ERRCODE_NOT_SUPPORTED;
146 }
147 return ERRCODE_SUCCESS;
148 }
149
DisableMock()150 LocationErrCode PassiveAbility::DisableMock()
151 {
152 if (!DisableLocationMock()) {
153 return ERRCODE_NOT_SUPPORTED;
154 }
155 return ERRCODE_SUCCESS;
156 }
157
IsMockEnabled()158 bool PassiveAbility::IsMockEnabled()
159 {
160 return IsLocationMocked();
161 }
162
SetMocked(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)163 LocationErrCode PassiveAbility::SetMocked(const int timeInterval,
164 const std::vector<std::shared_ptr<Location>> &location)
165 {
166 if (!SetMockedLocations(timeInterval, location)) {
167 return ERRCODE_NOT_SUPPORTED;
168 }
169 return ERRCODE_SUCCESS;
170 }
171
SendReportMockLocationEvent()172 void PassiveAbility::SendReportMockLocationEvent()
173 {
174 ClearLocationMock();
175 }
176
SaDumpInfo(std::string & result)177 void PassiveAbility::SaDumpInfo(std::string& result)
178 {
179 result += "Passive Location enable status: true";
180 result += "\n";
181 }
182
Dump(int32_t fd,const std::vector<std::u16string> & args)183 int32_t PassiveAbility::Dump(int32_t fd, const std::vector<std::u16string>& args)
184 {
185 std::vector<std::string> vecArgs;
186 std::transform(args.begin(), args.end(), std::back_inserter(vecArgs), [](const std::u16string &arg) {
187 return Str16ToStr8(arg);
188 });
189
190 LocationDumper dumper;
191 std::string result;
192 dumper.PassiveDump(SaDumpInfo, vecArgs, result);
193 if (!SaveStringToFd(fd, result)) {
194 LBSLOGE(PASSIVE, "Passive save string to fd failed!");
195 return ERR_OK;
196 }
197 return ERR_OK;
198 }
199
SendMessage(uint32_t code,MessageParcel & data,MessageParcel & reply)200 void PassiveAbility::SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply)
201 {
202 if (passiveHandler_ == nullptr) {
203 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
204 return;
205 }
206 switch (code) {
207 case static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS): {
208 if (!IsMockEnabled()) {
209 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
210 break;
211 }
212 int timeInterval = data.ReadInt32();
213 int locationSize = data.ReadInt32();
214 timeInterval = timeInterval < 0 ? 1 : timeInterval;
215 locationSize = locationSize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX :
216 locationSize;
217 std::shared_ptr<std::vector<std::shared_ptr<Location>>> vcLoc =
218 std::make_shared<std::vector<std::shared_ptr<Location>>>();
219 for (int i = 0; i < locationSize; i++) {
220 vcLoc->push_back(Location::UnmarshallingShared(data));
221 }
222 AppExecFwk::InnerEvent::Pointer event =
223 AppExecFwk::InnerEvent::Get(code, vcLoc, timeInterval);
224 if (passiveHandler_->SendEvent(event)) {
225 reply.WriteInt32(ERRCODE_SUCCESS);
226 } else {
227 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
228 }
229 break;
230 }
231 default:
232 break;
233 }
234 }
235
PassiveHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)236 PassiveHandler::PassiveHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
237
~PassiveHandler()238 PassiveHandler::~PassiveHandler() {}
239
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)240 void PassiveHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
241 {
242 auto passiveAbility = PassiveAbility::GetInstance();
243 if (passiveAbility == nullptr) {
244 return;
245 }
246 uint32_t eventId = event->GetInnerEventId();
247 LBSLOGD(PASSIVE, "ProcessEvent event:%{public}d", eventId);
248 switch (eventId) {
249 case static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS): {
250 int timeInterval = event->GetParam();
251 auto vcLoc = event->GetSharedObject<std::vector<std::shared_ptr<Location>>>();
252 if (vcLoc == nullptr) {
253 break;
254 }
255 std::vector<std::shared_ptr<Location>> mockLocations;
256 for (auto it = vcLoc->begin(); it != vcLoc->end(); ++it) {
257 mockLocations.push_back(*it);
258 }
259 if (passiveAbility != nullptr) {
260 passiveAbility->SetMocked(timeInterval, mockLocations);
261 }
262 break;
263 }
264 default:
265 break;
266 }
267 passiveAbility->UnloadPassiveSystemAbility();
268 }
269 } // namespace Location
270 } // namespace OHOS
271 #endif // FEATURE_PASSIVE_SUPPORT
272