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