blob: bd5fe757400c7ba85816e13a66ebe0af1bb954a0 [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
21#include <base/strings.h>
22#include <base/stringprintf.h>
23
24#include "error.h"
25#include "init_parser.h"
26#include "log.h"
27#include "property_service.h"
28#include "util.h"
29
30class Action::Command
31{
32public:
33 Command(int (*f)(int nargs, char** args),
34 const std::vector<std::string>& args,
35 const std::string& filename,
36 int line);
37
38 int InvokeFunc() const;
39 std::string BuildCommandString() const;
40 std::string BuildSourceString() const;
41
42private:
43 int (*func_)(int nargs, char** args);
44 const std::vector<std::string> args_;
45 const std::string filename_;
46 int line_;
47};
48
49Action::Command::Command(int (*f)(int nargs, char** args),
50 const std::vector<std::string>& args,
51 const std::string& filename,
52 int line) :
53 func_(f), args_(args), filename_(filename), line_(line)
54{
55}
56
57int Action::Command::InvokeFunc() const
58{
59 std::vector<std::string> strs;
60 strs.resize(args_.size());
61 strs[0] = args_[0];
62 for (std::size_t i = 1; i < args_.size(); ++i) {
63 if (expand_props(args_[i], &strs[i]) == -1) {
64 ERROR("%s: cannot expand '%s'\n", args_[0].c_str(), args_[i].c_str());
65 return -EINVAL;
66 }
67 }
68
69 std::vector<char*> args;
70 for (auto& s : strs) {
71 args.push_back(&s[0]);
72 }
73
74 return func_(args.size(), &args[0]);
75}
76
77std::string Action::Command::BuildCommandString() const
78{
79 return android::base::Join(args_, ' ');
80}
81
82std::string Action::Command::BuildSourceString() const
83{
84 if (!filename_.empty()) {
85 return android::base::StringPrintf(" (%s:%d)", filename_.c_str(), line_);
86 } else {
87 return std::string();
88 }
89}
90
91Action::Action()
92{
93}
94
95void Action::AddCommand(int (*f)(int nargs, char** args),
96 const std::vector<std::string>& args,
97 const std::string& filename, int line)
98{
99 Action::Command* cmd = new Action::Command(f, args, filename, line);
100 commands_.push_back(cmd);
101}
102
103std::size_t Action::NumCommands() const
104{
105 return commands_.size();
106}
107
108void Action::ExecuteOneCommand(std::size_t command) const
109{
110 ExecuteCommand(*commands_[command]);
111}
112
113void Action::ExecuteAllCommands() const
114{
115 for (const auto& c : commands_) {
116 ExecuteCommand(*c);
117 }
118}
119
120void Action::ExecuteCommand(const Command& command) const
121{
122 Timer t;
123 int result = command.InvokeFunc();
124
125 if (klog_get_level() >= KLOG_INFO_LEVEL) {
126 std::string trigger_name = BuildTriggersString();
127 std::string cmd_str = command.BuildCommandString();
128 std::string source = command.BuildSourceString();
129
130 INFO("Command '%s' action=%s%s returned %d took %.2fs\n",
131 cmd_str.c_str(), trigger_name.c_str(), source.c_str(),
132 result, t.duration());
133 }
134}
135
136bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err)
137{
138 const static std::string prop_str("property:");
139 std::string prop_name(trigger.substr(prop_str.length()));
140 size_t equal_pos = prop_name.find('=');
141 if (equal_pos == std::string::npos) {
142 *err = "property trigger found without matching '='";
143 return false;
144 }
145
146 std::string prop_value(prop_name.substr(equal_pos + 1));
147 prop_name.erase(equal_pos);
148
149 auto res = property_triggers_.emplace(prop_name, prop_value);
150 if (res.second == false) {
151 *err = "multiple property triggers found for same property";
152 return false;
153 }
154 return true;
155}
156
157bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err)
158{
159 const static std::string prop_str("property:");
160 for (std::size_t i = 0; i < args.size(); ++i) {
161 if (i % 2) {
162 if (args[i].compare("&&")) {
163 *err = "&& is the only symbol allowed to concatenate actions";
164 return false;
165 } else {
166 continue;
167 }
168 }
169
170 if (!args[i].compare(0, prop_str.length(), prop_str)) {
171 if (!ParsePropertyTrigger(args[i], err)) {
172 return false;
173 }
174 } else {
175 if (!event_trigger_.empty()) {
176 *err = "multiple event triggers are not allowed";
177 return false;
178 }
179
180 event_trigger_ = args[i];
181 }
182 }
183
184 return true;
185}
186
187bool Action::InitSingleTrigger(const std::string& trigger)
188{
189 std::vector<std::string> name_vector{trigger};
190 std::string err;
191 return InitTriggers(name_vector, &err);
192}
193
194bool Action::CheckPropertyTriggers(const std::string& name,
195 const std::string& value) const
196{
197 bool found = !name.compare("");
198 if (property_triggers_.empty()) {
199 return true;
200 }
201
202 for (const auto& t : property_triggers_) {
203 if (!t.first.compare(name)) {
204 if (t.second.compare("*") &&
205 t.second.compare(value)) {
206 return false;
207 } else {
208 found = true;
209 }
210 } else {
211 std::string prop_val = property_get(t.first.c_str());
212 if (prop_val.empty() ||
213 (t.second.compare("*") &&
214 t.second.compare(prop_val))) {
215 return false;
216 }
217 }
218 }
219 return found;
220}
221
222bool Action::CheckEventTrigger(const std::string& trigger) const
223{
224 return !event_trigger_.empty() &&
225 !trigger.compare(event_trigger_) &&
226 CheckPropertyTriggers();
227}
228
229bool Action::CheckPropertyTrigger(const std::string& name,
230 const std::string& value) const
231{
232 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
233}
234
235bool Action::TriggersEqual(const class Action& other) const
236{
237 return property_triggers_.size() == other.property_triggers_.size() &&
238 std::equal(property_triggers_.begin(), property_triggers_.end(),
239 other.property_triggers_.begin()) &&
240 !event_trigger_.compare(other.event_trigger_);
241}
242
243std::string Action::BuildTriggersString() const
244{
245 std::string result;
246
247 for (const auto& t : property_triggers_) {
248 result += t.first;
249 result += '=';
250 result += t.second;
251 result += ' ';
252 }
253 if (!event_trigger_.empty()) {
254 result += event_trigger_;
255 result += ' ';
256 }
257 result.pop_back();
258 return result;
259}
260
261void Action::DumpState() const
262{
263 INFO("on ");
264 std::string trigger_name = BuildTriggersString();
265 INFO("%s", trigger_name.c_str());
266 INFO("\n");
267
268 for (const auto& c : commands_) {
269 std::string cmd_str = c->BuildCommandString();
270 INFO(" %s", cmd_str.c_str());
271 }
272 INFO("\n");
273}
274
275ActionManager::ActionManager() : cur_command_(0)
276{
277}
278
279ActionManager& ActionManager::GetInstance() {
280 static ActionManager instance;
281 return instance;
282}
283
284void ActionManager::QueueEventTrigger(const std::string& trigger)
285{
286 for (const auto& a : action_list_) {
287 if (a->CheckEventTrigger(trigger)) {
288 action_queue_.push(a);
289 }
290 }
291}
292
293void ActionManager::QueuePropertyTrigger(const std::string& name,
294 const std::string& value)
295{
296 for (const auto& a : action_list_) {
297 if (a->CheckPropertyTrigger(name, value)) {
298 action_queue_.push(a);
299 }
300 }
301}
302
303void ActionManager::QueueAllPropertyTriggers()
304{
305 QueuePropertyTrigger("", "");
306}
307
308void ActionManager::QueueBuiltinAction(int (*func)(int nargs, char** args),
309 const std::string& name)
310{
311 Action* act = new Action();
312 std::vector<std::string> name_vector{name};
313
314 if (!act->InitSingleTrigger(name)) {
315 return;
316 }
317
318 act->AddCommand(func, name_vector);
319
320 action_queue_.push(act);
321}
322
323void ActionManager::ExecuteOneCommand() {
324 if (action_queue_.empty()) {
325 return;
326 }
327
328 Action* action = action_queue_.front();
329 if (!action->NumCommands()) {
330 action_queue_.pop();
331 return;
332 }
333
334 if (cur_command_ == 0) {
335 std::string trigger_name = action->BuildTriggersString();
336 INFO("processing action %p (%s)\n", action, trigger_name.c_str());
337 }
338
339 action->ExecuteOneCommand(cur_command_++);
340 if (cur_command_ == action->NumCommands()) {
341 cur_command_ = 0;
342 action_queue_.pop();
343 }
344}
345
346bool ActionManager::HasMoreCommands() const
347{
348 return !action_queue_.empty();
349}
350
351Action* ActionManager::AddNewAction(const std::vector<std::string>& triggers,
352 std::string* err)
353{
354 if (triggers.size() < 1) {
355 *err = "actions must have a trigger\n";
356 return nullptr;
357 }
358
359 Action* act = new Action();
360 if (!act->InitTriggers(triggers, err)) {
361 return nullptr;
362 }
363
364 auto old_act_it =
365 std::find_if(action_list_.begin(), action_list_.end(),
366 [&act] (Action* a) { return act->TriggersEqual(*a); });
367
368 if (old_act_it != action_list_.end()) {
369 delete act;
370 return *old_act_it;
371 }
372
373 action_list_.push_back(act);
374 return act;
375}
376
377void ActionManager::DumpState() const
378{
379 for (const auto& a : action_list_) {
380 a->DumpState();
381 }
382 INFO("\n");
383}