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