blob: e9b2a0dfe1c3204fe244c6017f1fa6869fe6034e [file] [log] [blame]
Tom Cherryfa0c21c2015-07-23 17:53:11 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "action.h"
18
19#include <errno.h>
20
Elliott Hughes4f713192015-12-04 22:00:26 -080021#include <android-base/strings.h>
22#include <android-base/stringprintf.h>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023
Tom Cherryb7349902015-08-26 11:43:36 -070024#include "builtins.h"
Tom Cherryfa0c21c2015-07-23 17:53:11 -070025#include "error.h"
26#include "init_parser.h"
27#include "log.h"
28#include "property_service.h"
29#include "util.h"
30
Tom Cherryb7349902015-08-26 11:43:36 -070031using android::base::Join;
32using android::base::StringPrintf;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070033
Tom Cherryb7349902015-08-26 11:43:36 -070034Command::Command(BuiltinFunction f, const std::vector<std::string>& args,
35 const std::string& filename, int line)
36 : func_(f), args_(args), filename_(filename), line_(line) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070037}
38
Tom Cherryb7349902015-08-26 11:43:36 -070039int Command::InvokeFunc() const {
Tom Cherry96f67312015-07-30 13:52:55 -070040 std::vector<std::string> expanded_args;
41 expanded_args.resize(args_.size());
42 expanded_args[0] = args_[0];
Tom Cherryfa0c21c2015-07-23 17:53:11 -070043 for (std::size_t i = 1; i < args_.size(); ++i) {
Tom Cherryb7349902015-08-26 11:43:36 -070044 if (!expand_props(args_[i], &expanded_args[i])) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070045 LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046 return -EINVAL;
47 }
48 }
49
Tom Cherry96f67312015-07-30 13:52:55 -070050 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070051}
52
Tom Cherryb7349902015-08-26 11:43:36 -070053std::string Command::BuildCommandString() const {
54 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055}
56
Tom Cherryb7349902015-08-26 11:43:36 -070057std::string Command::BuildSourceString() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058 if (!filename_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -070059 return StringPrintf(" (%s:%d)", filename_.c_str(), line_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070060 } else {
61 return std::string();
62 }
63}
64
Tom Cherryb7349902015-08-26 11:43:36 -070065Action::Action(bool oneshot) : oneshot_(oneshot) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070066}
67
Tom Cherryb7349902015-08-26 11:43:36 -070068const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
69
70bool Action::AddCommand(const std::vector<std::string>& args,
71 const std::string& filename, int line, std::string* err) {
72 if (!function_map_) {
73 *err = "no function map available";
74 return false;
75 }
76
77 if (args.empty()) {
78 *err = "command needed, but not provided";
79 return false;
80 }
81
82 auto function = function_map_->FindFunction(args[0], args.size() - 1, err);
83 if (!function) {
84 return false;
85 }
86
87 AddCommand(function, args, filename, line);
88 return true;
89}
90
91void Action::AddCommand(BuiltinFunction f,
Tom Cherryfa0c21c2015-07-23 17:53:11 -070092 const std::vector<std::string>& args,
Tom Cherryb7349902015-08-26 11:43:36 -070093 const std::string& filename, int line) {
94 commands_.emplace_back(f, args, filename, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095}
96
Tom Cherryb7349902015-08-26 11:43:36 -070097void Action::CombineAction(const Action& action) {
98 for (const auto& c : action.commands_) {
99 commands_.emplace_back(c);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100 }
101}
102
Tom Cherryb7349902015-08-26 11:43:36 -0700103std::size_t Action::NumCommands() const {
104 return commands_.size();
105}
106
107void Action::ExecuteOneCommand(std::size_t command) const {
108 ExecuteCommand(commands_[command]);
109}
110
111void Action::ExecuteAllCommands() const {
112 for (const auto& c : commands_) {
113 ExecuteCommand(c);
114 }
115}
116
117void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700118 Timer t;
119 int result = command.InvokeFunc();
120
Wei Wangb1a309a2016-11-09 15:17:38 -0800121 double duration_ms = t.duration() * 1000;
122 // Any action longer than 50ms will be warned to user as slow operation
123 if (duration_ms > 50.0 ||
124 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700125 std::string trigger_name = BuildTriggersString();
126 std::string cmd_str = command.BuildCommandString();
127 std::string source = command.BuildSourceString();
128
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700129 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source
Wei Wangb1a309a2016-11-09 15:17:38 -0800130 << " returned " << result << " took " << duration_ms << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700131 }
132}
133
Tom Cherryb7349902015-08-26 11:43:36 -0700134bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700135 const static std::string prop_str("property:");
136 std::string prop_name(trigger.substr(prop_str.length()));
137 size_t equal_pos = prop_name.find('=');
138 if (equal_pos == std::string::npos) {
139 *err = "property trigger found without matching '='";
140 return false;
141 }
142
143 std::string prop_value(prop_name.substr(equal_pos + 1));
144 prop_name.erase(equal_pos);
145
146 auto res = property_triggers_.emplace(prop_name, prop_value);
147 if (res.second == false) {
148 *err = "multiple property triggers found for same property";
149 return false;
150 }
151 return true;
152}
153
Tom Cherryb7349902015-08-26 11:43:36 -0700154bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700155 const static std::string prop_str("property:");
156 for (std::size_t i = 0; i < args.size(); ++i) {
157 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700158 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700159 *err = "&& is the only symbol allowed to concatenate actions";
160 return false;
161 } else {
162 continue;
163 }
164 }
165
166 if (!args[i].compare(0, prop_str.length(), prop_str)) {
167 if (!ParsePropertyTrigger(args[i], err)) {
168 return false;
169 }
170 } else {
171 if (!event_trigger_.empty()) {
172 *err = "multiple event triggers are not allowed";
173 return false;
174 }
175
176 event_trigger_ = args[i];
177 }
178 }
179
180 return true;
181}
182
Tom Cherryb7349902015-08-26 11:43:36 -0700183bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700184 std::vector<std::string> name_vector{trigger};
185 std::string err;
186 return InitTriggers(name_vector, &err);
187}
188
Tom Cherryb7349902015-08-26 11:43:36 -0700189// This function checks that all property triggers are satisfied, that is
190// for each (name, value) in property_triggers_, check that the current
191// value of the property 'name' == value.
192//
193// It takes an optional (name, value) pair, which if provided must
194// be present in property_triggers_; it skips the check of the current
195// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700196bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700197 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700198 if (property_triggers_.empty()) {
199 return true;
200 }
201
Tom Cherryb7349902015-08-26 11:43:36 -0700202 bool found = name.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700203 for (const auto& t : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700204 const auto& trigger_name = t.first;
205 const auto& trigger_value = t.second;
206 if (trigger_name == name) {
207 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700208 return false;
209 } else {
210 found = true;
211 }
212 } else {
Tom Cherrycb716f92015-08-11 12:34:22 -0700213 std::string prop_val = property_get(trigger_name.c_str());
214 if (prop_val.empty() || (trigger_value != "*" &&
215 trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700216 return false;
217 }
218 }
219 }
220 return found;
221}
222
Tom Cherryb7349902015-08-26 11:43:36 -0700223bool Action::CheckEventTrigger(const std::string& trigger) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700224 return !event_trigger_.empty() &&
Tom Cherrycb716f92015-08-11 12:34:22 -0700225 trigger == event_trigger_ &&
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700226 CheckPropertyTriggers();
227}
228
229bool Action::CheckPropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700230 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700231 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
232}
233
Tom Cherryb7349902015-08-26 11:43:36 -0700234bool Action::TriggersEqual(const Action& other) const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700235 return property_triggers_ == other.property_triggers_ &&
236 event_trigger_ == other.event_trigger_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700237}
238
Tom Cherryb7349902015-08-26 11:43:36 -0700239std::string Action::BuildTriggersString() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240 std::string result;
241
242 for (const auto& t : property_triggers_) {
243 result += t.first;
244 result += '=';
245 result += t.second;
246 result += ' ';
247 }
248 if (!event_trigger_.empty()) {
249 result += event_trigger_;
250 result += ' ';
251 }
Wei Wang69b9b362016-11-14 20:05:21 -0800252 if (!result.empty()) {
253 result.pop_back();
254 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700255 return result;
256}
257
Tom Cherryb7349902015-08-26 11:43:36 -0700258void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700259 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700260 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700261
262 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700263 std::string cmd_str = c.BuildCommandString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700264 LOG(INFO) << " %s" << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700265 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700266}
267
Tom Cherrycb716f92015-08-11 12:34:22 -0700268class EventTrigger : public Trigger {
269public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700270 explicit EventTrigger(const std::string& trigger) : trigger_(trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700271 }
Tom Cherryb7349902015-08-26 11:43:36 -0700272 bool CheckTriggers(const Action& action) const override {
273 return action.CheckEventTrigger(trigger_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700274 }
275private:
Tom Cherryb7349902015-08-26 11:43:36 -0700276 const std::string trigger_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700277};
278
279class PropertyTrigger : public Trigger {
280public:
281 PropertyTrigger(const std::string& name, const std::string& value)
282 : name_(name), value_(value) {
283 }
Tom Cherryb7349902015-08-26 11:43:36 -0700284 bool CheckTriggers(const Action& action) const override {
285 return action.CheckPropertyTrigger(name_, value_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700286 }
287private:
Tom Cherryb7349902015-08-26 11:43:36 -0700288 const std::string name_;
289 const std::string value_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700290};
291
292class BuiltinTrigger : public Trigger {
293public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700294 explicit BuiltinTrigger(Action* action) : action_(action) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700295 }
Tom Cherryb7349902015-08-26 11:43:36 -0700296 bool CheckTriggers(const Action& action) const override {
297 return action_ == &action;
Tom Cherrycb716f92015-08-11 12:34:22 -0700298 }
299private:
Tom Cherryb7349902015-08-26 11:43:36 -0700300 const Action* action_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700301};
302
Tom Cherryb7349902015-08-26 11:43:36 -0700303ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700304}
305
306ActionManager& ActionManager::GetInstance() {
307 static ActionManager instance;
308 return instance;
309}
310
Tom Cherryb7349902015-08-26 11:43:36 -0700311void ActionManager::AddAction(std::unique_ptr<Action> action) {
312 auto old_action_it =
313 std::find_if(actions_.begin(), actions_.end(),
314 [&action] (std::unique_ptr<Action>& a) {
315 return action->TriggersEqual(*a);
316 });
317
318 if (old_action_it != actions_.end()) {
319 (*old_action_it)->CombineAction(*action);
320 } else {
321 actions_.emplace_back(std::move(action));
322 }
323}
324
325void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700326 trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700327}
328
329void ActionManager::QueuePropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700330 const std::string& value) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700331 trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700332}
333
Tom Cherryb7349902015-08-26 11:43:36 -0700334void ActionManager::QueueAllPropertyTriggers() {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700335 QueuePropertyTrigger("", "");
336}
337
Tom Cherryb7349902015-08-26 11:43:36 -0700338void ActionManager::QueueBuiltinAction(BuiltinFunction func,
339 const std::string& name) {
340 auto action = std::make_unique<Action>(true);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700341 std::vector<std::string> name_vector{name};
342
Tom Cherryb7349902015-08-26 11:43:36 -0700343 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700344 return;
345 }
346
Tom Cherryb7349902015-08-26 11:43:36 -0700347 action->AddCommand(func, name_vector);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700348
Tom Cherryb7349902015-08-26 11:43:36 -0700349 trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get()));
350 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700351}
352
353void ActionManager::ExecuteOneCommand() {
Tom Cherryb7349902015-08-26 11:43:36 -0700354 // Loop through the trigger queue until we have an action to execute
Tom Cherrycb716f92015-08-11 12:34:22 -0700355 while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700356 for (const auto& action : actions_) {
357 if (trigger_queue_.front()->CheckTriggers(*action)) {
358 current_executing_actions_.emplace(action.get());
359 }
360 }
Tom Cherrycb716f92015-08-11 12:34:22 -0700361 trigger_queue_.pop();
362 }
363
364 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700365 return;
366 }
367
Tom Cherryb7349902015-08-26 11:43:36 -0700368 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700369
Tom Cherrycb716f92015-08-11 12:34:22 -0700370 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700371 std::string trigger_name = action->BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700372 LOG(INFO) << "processing action (" << trigger_name << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700373 }
374
Tom Cherryb7349902015-08-26 11:43:36 -0700375 action->ExecuteOneCommand(current_command_);
376
377 // If this was the last command in the current action, then remove
378 // the action from the executing list.
379 // If this action was oneshot, then also remove it from actions_.
380 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700381 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700382 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700383 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700384 if (action->oneshot()) {
385 auto eraser = [&action] (std::unique_ptr<Action>& a) {
386 return a.get() == action;
387 };
388 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
389 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700390 }
391}
392
Tom Cherryb7349902015-08-26 11:43:36 -0700393bool ActionManager::HasMoreCommands() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700394 return !current_executing_actions_.empty() || !trigger_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700395}
396
Tom Cherryb7349902015-08-26 11:43:36 -0700397void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700398 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700399 a->DumpState();
400 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700401}
Tom Cherryb7349902015-08-26 11:43:36 -0700402
403bool ActionParser::ParseSection(const std::vector<std::string>& args,
404 std::string* err) {
405 std::vector<std::string> triggers(args.begin() + 1, args.end());
406 if (triggers.size() < 1) {
407 *err = "actions must have a trigger";
408 return false;
409 }
410
411 auto action = std::make_unique<Action>(false);
412 if (!action->InitTriggers(triggers, err)) {
413 return false;
414 }
415
416 action_ = std::move(action);
417 return true;
418}
419
420bool ActionParser::ParseLineSection(const std::vector<std::string>& args,
421 const std::string& filename, int line,
422 std::string* err) const {
423 return action_ ? action_->AddCommand(args, filename, line, err) : false;
424}
425
426void ActionParser::EndSection() {
427 if (action_ && action_->NumCommands() > 0) {
428 ActionManager::GetInstance().AddAction(std::move(action_));
429 }
430}