1 /*
2 * Copyright (c) 2023 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 *e
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 "device_param_collect.h"
17 #include "parameter.h"
18 #include "parameters.h"
19 #include "sa_profiles.h"
20 #include "sam_log.h"
21 #include "system_ability_manager.h"
22 #include "system_ability_status_change_stub.h"
23
24 using namespace std;
25
26 namespace OHOS {
27 namespace {
28 constexpr int32_t PARAM_WATCHER_DISTRIBUTED_SERVICE_ID = 3901;
29 }
DeviceParamCallback(const char * key,const char * value,void * context)30 static void DeviceParamCallback(const char* key, const char* value, void* context)
31 {
32 HILOGI("key:%{public}s, value:%{public}s", key, value);
33 OnDemandEvent event = {PARAM, key, value};
34 DeviceParamCollect* deviceParamCollect = static_cast<DeviceParamCollect*>(context);
35 if (deviceParamCollect == nullptr) {
36 return;
37 }
38 deviceParamCollect->ReportEvent(event);
39 }
40
DeviceParamCollect(const sptr<IReport> & report)41 DeviceParamCollect::DeviceParamCollect(const sptr<IReport>& report)
42 : ICollectPlugin(report)
43 {
44 }
45
CheckCondition(const OnDemandCondition & condition)46 bool DeviceParamCollect::CheckCondition(const OnDemandCondition& condition)
47 {
48 std::string value = system::GetParameter(condition.name, "");
49 return value == condition.value;
50 }
51
Init(const std::list<SaProfile> & saProfiles)52 void DeviceParamCollect::Init(const std::list<SaProfile>& saProfiles)
53 {
54 std::lock_guard<std::mutex> autoLock(paramLock_);
55 HILOGI("DeviceParamCollect Init begin");
56 for (auto saProfile : saProfiles) {
57 for (auto onDemandEvent : saProfile.startOnDemand.onDemandEvents) {
58 if (onDemandEvent.eventId == PARAM) {
59 pendingParams_.insert(onDemandEvent.name);
60 }
61 }
62 for (auto onDemandEvent : saProfile.stopOnDemand.onDemandEvents) {
63 if (onDemandEvent.eventId == PARAM) {
64 pendingParams_.insert(onDemandEvent.name);
65 }
66 }
67 }
68 }
69
OnStart()70 int32_t DeviceParamCollect::OnStart()
71 {
72 HILOGI("DeviceParamCollect OnStart called");
73 sptr<SystemAbilityStatusChange> statusChangeListener = new SystemAbilityStatusChange();
74 statusChangeListener->Init(this);
75 SystemAbilityManager::GetInstance()->SubscribeSystemAbility(PARAM_WATCHER_DISTRIBUTED_SERVICE_ID,
76 statusChangeListener);
77 return ERR_OK;
78 }
79
OnStop()80 int32_t DeviceParamCollect::OnStop()
81 {
82 HILOGI("DeviceParamCollect OnStop called");
83 return ERR_OK;
84 }
85
WatchParameters()86 void DeviceParamCollect::WatchParameters()
87 {
88 std::lock_guard<std::mutex> autoLock(paramLock_);
89 for (auto param : pendingParams_) {
90 HILOGD("DeviceParamCollect watch param: %{public}s", param.c_str());
91 int32_t result = WatchParameter(param.c_str(), DeviceParamCallback, this);
92 if (result != ERR_OK) {
93 HILOGE("DeviceParamCollect watch events: %{public}s failed", param.c_str());
94 continue;
95 }
96 params_.insert(param);
97 }
98 pendingParams_.clear();
99 }
100
AddCollectEvent(const OnDemandEvent & event)101 int32_t DeviceParamCollect::AddCollectEvent(const OnDemandEvent& event)
102 {
103 std::lock_guard<std::mutex> autoLock(paramLock_);
104 auto iter = params_.find(event.name);
105 if (iter != params_.end()) {
106 return ERR_OK;
107 }
108 HILOGI("DeviceParamCollect add collect events: %{public}s", event.name.c_str());
109 int32_t result = WatchParameter(event.name.c_str(), DeviceParamCallback, this);
110 if (result == ERR_OK) {
111 params_.insert(event.name);
112 }
113 return result;
114 }
115
RemoveUnusedEvent(const OnDemandEvent & event)116 int32_t DeviceParamCollect::RemoveUnusedEvent(const OnDemandEvent& event)
117 {
118 std::lock_guard<std::mutex> autoLock(paramLock_);
119 auto iter = params_.find(event.name);
120 if (iter != params_.end()) {
121 int32_t result = RemoveParameterWatcher(event.name.c_str(), nullptr, nullptr);
122 if (result != ERR_OK) {
123 HILOGE("DeviceParamCollect RemoveUnusedEvent failed");
124 return result;
125 }
126 HILOGI("DeviceParamCollect remove event name: %{public}s", event.name.c_str());
127 params_.erase(iter);
128 }
129 return ERR_OK;
130 }
131
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)132 void SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
133 {
134 HILOGI("OnAddSystemAbility SA:%{public}d", systemAbilityId);
135 switch (systemAbilityId) {
136 case PARAM_WATCHER_DISTRIBUTED_SERVICE_ID: {
137 if (deviceParamCollect_ == nullptr) {
138 HILOGE("DeviceParamCollect is nullptr");
139 return;
140 }
141 auto task = [this] () {
142 deviceParamCollect_->WatchParameters();
143 };
144 deviceParamCollect_->PostDelayTask(task, 0);
145 break;
146 }
147 default:
148 HILOGE("OnAddSystemAbility unhandled SA:%{public}d", systemAbilityId);
149 }
150 }
151
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)152 void SystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
153 {
154 HILOGI("OnRemoveSystemAbility: start!");
155 }
156
Init(const sptr<DeviceParamCollect> & deviceParamCollect)157 void SystemAbilityStatusChange::Init(const sptr<DeviceParamCollect>& deviceParamCollect)
158 {
159 deviceParamCollect_ = deviceParamCollect;
160 }
161 } // namespace OHOS
162