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