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 "subability_common.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "string_ex.h"
21 #include "system_ability_definition.h"
22
23 #include "common_utils.h"
24 #include "locationhub_ipc_interface_code.h"
25
26 namespace OHOS {
27 namespace Location {
SubAbility()28 SubAbility::SubAbility()
29 {
30 label_ = { LOG_CORE, LOCATOR_LOG_ID, "unknown" };
31 newRecord_ = std::make_unique<WorkRecord>();
32 lastRecord_ = std::make_unique<WorkRecord>();
33 }
34
~SubAbility()35 SubAbility::~SubAbility()
36 {
37 newRecord_ = nullptr;
38 lastRecord_ = nullptr;
39 }
40
SetAbility(std::string name)41 void SubAbility::SetAbility(std::string name)
42 {
43 name_ = name;
44 label_ = CommonUtils::GetLabel(name);
45 capability_ = CommonUtils::GetCapability(name);
46 }
47
LocationRequest(WorkRecord & workRecord)48 void SubAbility::LocationRequest(WorkRecord &workRecord)
49 {
50 interval_ = workRecord.GetTimeInterval(0);
51 newRecord_->Clear();
52 newRecord_->Set(workRecord);
53 HandleRefrashRequirements();
54 }
55
HandleRefrashRequirements()56 void SubAbility::HandleRefrashRequirements()
57 {
58 LBSLOGI(label_, "refrash requirements");
59
60 // send local request
61 HandleLocalRequest(*newRecord_);
62 lastRecord_->Clear();
63 lastRecord_->Set(*newRecord_);
64 }
65
GetRequestNum()66 int SubAbility::GetRequestNum()
67 {
68 if (newRecord_ == nullptr) {
69 return 0;
70 }
71 return newRecord_->Size();
72 }
73
HandleLocalRequest(WorkRecord & record)74 void SubAbility::HandleLocalRequest(WorkRecord &record)
75 {
76 HandleRemoveRecord(record);
77 HandleAddRecord(record);
78 }
79
HandleRemoveRecord(WorkRecord & newRecord)80 void SubAbility::HandleRemoveRecord(WorkRecord &newRecord)
81 {
82 for (int i = 0; i < lastRecord_->Size(); i++) {
83 int uid = lastRecord_->GetUid(i);
84 bool isFind = newRecord.Find(uid, lastRecord_->GetName(i), lastRecord_->GetUuid(i));
85 LBSLOGD(label_, "remove record isFind:%{public}d, uid:%{public}d, lastRecord:%{public}s, newRecord:%{public}s",
86 isFind, uid, lastRecord_->ToString().c_str(), newRecord.ToString().c_str());
87 if (!isFind) {
88 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
89 workRecord->Add(uid, lastRecord_->GetPid(i), lastRecord_->GetName(i),
90 lastRecord_->GetTimeInterval(i), lastRecord_->GetUuid(i));
91 workRecord->SetDeviceId(newRecord.GetDeviceId());
92 RequestRecord(*workRecord, false);
93 }
94 }
95 }
96
HandleAddRecord(WorkRecord & newRecord)97 void SubAbility::HandleAddRecord(WorkRecord &newRecord)
98 {
99 for (int i = 0; i < newRecord.Size(); i++) {
100 int uid = newRecord.GetUid(i);
101 bool isFind = lastRecord_->Find(uid, newRecord.GetName(i), lastRecord_->GetUuid(i));
102 LBSLOGD(label_, "add record isFind:%{public}d, uid:%{public}d, lastRecord:%{public}s, newRecord:%{public}s",
103 isFind, uid, lastRecord_->ToString().c_str(), newRecord.ToString().c_str());
104 if (!isFind) {
105 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
106 workRecord->Add(uid, newRecord.GetPid(i), newRecord.GetName(i),
107 newRecord.GetTimeInterval(i), newRecord.GetUuid(i));
108 workRecord->SetDeviceId(newRecord.GetDeviceId());
109 RequestRecord(*workRecord, true);
110 }
111 }
112 }
113
Enable(bool state,const sptr<IRemoteObject> ability)114 void SubAbility::Enable(bool state, const sptr<IRemoteObject> ability)
115 {
116 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
117 if (sam == nullptr) {
118 LBSLOGE(label_, "Enable can not get SystemAbilityManager");
119 return;
120 }
121
122 int saId = CommonUtils::AbilityConvertToId(name_);
123 if (state) {
124 if (sam->CheckSystemAbility(saId) == nullptr) {
125 sam->AddSystemAbility(saId, ability, ISystemAbilityManager::SAExtraProp(true, 1, capability_, u""));
126 LBSLOGI(label_, "enable %{public}s ability", name_.c_str());
127 }
128 } else {
129 if (sam->CheckSystemAbility(saId) != nullptr) {
130 sam->RemoveSystemAbility(saId);
131 LBSLOGI(label_, "disable %{public}s ability", name_.c_str());
132 }
133 }
134 }
135
HandleSelfRequest(pid_t pid,pid_t uid,bool state)136 void SubAbility::HandleSelfRequest(pid_t pid, pid_t uid, bool state)
137 {
138 std::unique_ptr<WorkRecord> records = std::make_unique<WorkRecord>();
139 std::string name = Str16ToStr8(u"ohos");
140 std::string uuid = std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM));
141 records->Set(*lastRecord_);
142 if (state) {
143 records->Add(uid, pid, name, interval_, uuid);
144 } else {
145 records->Remove(uid, pid, name, uuid);
146 }
147 LocationRequest(*records);
148 records->Clear();
149 }
150
EnableLocationMock()151 bool SubAbility::EnableLocationMock()
152 {
153 LBSLOGI(label_, "EnableLocationMock current state is %{public}d", mockEnabled_);
154 mockEnabled_ = true;
155 return true;
156 }
157
DisableLocationMock()158 bool SubAbility::DisableLocationMock()
159 {
160 LBSLOGI(label_, "DisableLocationMock current state is %{public}d", mockEnabled_);
161 mockEnabled_ = false;
162 return true;
163 }
164
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)165 bool SubAbility::SetMockedLocations(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
166 {
167 if (!mockEnabled_) {
168 LBSLOGE(label_, "SetMockedLocations current state is %{public}d, need enbale it", mockEnabled_);
169 return false;
170 }
171 CacheLocationMock(location);
172 mockTimeInterval_ = timeInterval;
173 SendReportMockLocationEvent();
174 return true;
175 }
176
CacheLocationMock(const std::vector<std::shared_ptr<Location>> & location)177 void SubAbility::CacheLocationMock(const std::vector<std::shared_ptr<Location>> &location)
178 {
179 int locationSize = static_cast<int>(location.size());
180 ClearLocationMock();
181 std::unique_lock lock(mutex_);
182 for (int i = 0; i < locationSize; i++) {
183 mockLoc_.push_back(std::make_shared<Location>(*location.at(i)));
184 }
185 }
186
IsLocationMocked()187 bool SubAbility::IsLocationMocked()
188 {
189 return mockEnabled_;
190 }
191
GetTimeIntervalMock()192 int SubAbility::GetTimeIntervalMock()
193 {
194 return mockTimeInterval_;
195 }
196
GetLocationMock()197 std::vector<std::shared_ptr<Location>> SubAbility::GetLocationMock()
198 {
199 std::unique_lock lock(mutex_);
200 return mockLoc_;
201 }
202
ClearLocationMock()203 void SubAbility::ClearLocationMock()
204 {
205 std::unique_lock lock(mutex_);
206 mockLoc_.clear();
207 }
208
ReportLocationInfo(const std::string & systemAbility,const std::shared_ptr<Location> location)209 void SubAbility::ReportLocationInfo(
210 const std::string& systemAbility, const std::shared_ptr<Location> location)
211 {
212 MessageParcel data;
213 MessageParcel reply;
214 MessageOption option;
215 data.WriteInterfaceToken(u"location.ILocator");
216 data.WriteString(systemAbility);
217 location->Marshalling(data);
218 sptr<IRemoteObject> objectLocator =
219 CommonUtils::GetRemoteObject(LOCATION_LOCATOR_SA_ID, CommonUtils::InitDeviceId());
220 if (objectLocator == nullptr) {
221 LBSLOGE(label_, "%{public}s get locator sa failed", __func__);
222 return;
223 }
224 objectLocator->SendRequest(static_cast<int>(LocatorInterfaceCode::REPORT_LOCATION), data, reply, option);
225 }
226 } // namespace Location
227 } // namespace OHOS
228