Skip to content

Commit

Permalink
release R2201
Browse files Browse the repository at this point in the history
  • Loading branch information
maierkomor committed Jan 30, 2022
1 parent 6b23e15 commit 1c97238
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 84 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
R2201:
======
- support arguments for actions
- ledstrip action/event/mqtt integration
- uDNS must resolve CNAMEs

R2112:
======
- remove IDF event-task
Expand Down
62 changes: 50 additions & 12 deletions components/actions/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,24 @@ static set<Action,less<Action>> Actions;
static event_t ActionTriggerEvt = 0;


Action::Action(const char *n)
: name(n)
, ev(0)
, func(0)
{
// log_dbug(TAG,"Action(%s)",n);
}


Action::Action(const char *n, void (*f)(void*),void *a, const char *t)
: name(n)
, text(t)
, ev(event_register("*trigger`",n))
, func(f)
, arg(a)
{

// log_dbug(TAG,"Action(%s,...)",n);
event_callback(ev,this);
}


Expand Down Expand Up @@ -120,22 +131,49 @@ int action_activate(const char *name)
}


int action_activate_arg(const char *name, void *arg)
{
Action x(name);
set<Action,less<Action>>::iterator i = Actions.find(x);
if (i == Actions.end()) {
log_warn(TAG,"unable to execute unknown action '%s'",name);
if (arg)
free(arg);
return 1;
}
log_dbug(TAG,"triggered action %s",name);
Action &a = const_cast<Action&>(*i);
a.activate(arg);
return 0;
}


int action_dispatch(const char *n, size_t l)
{
if (l == 0)
l = strlen(n);
char name[l+1];
name[l] = 0;
memcpy(name,n,l);
Action x(name);
set<Action,less<Action>>::iterator i = Actions.find(name);
if (i == Actions.end()) {
log_warn(TAG,"unable to dispatch unknown action '%s'",name);
size_t nl = l;
const char *e = strchr(n,' ');
if (e)
nl = e-n;
char name[nl+1]; // temporary for search
name[nl] = 0;
memcpy(name,n,nl);
Action *a = action_get(name);
if (a == 0) {
log_warn(TAG,"dispatch unknown '%s'",name);
return 1;
}
Action *a = const_cast<Action*>(&(*i));
log_dbug(TAG,"dispatch action %s (%p)",name,a);
event_trigger_arg(ActionTriggerEvt,a);
log_dbug(TAG,"dispatch %s",name);
char *arg = 0;
if (e) {
size_t al = l - nl;
arg = (char *) malloc(al);
--al;
memcpy(arg,e+1,al);
arg[al] = 0;
}
event_trigger_arg(a->ev,arg);
return 0;
}

Expand All @@ -152,7 +190,7 @@ static void action_event_cb(void *arg)
{
Action *a = (Action *)arg;
assert(a);
log_dbug(TAG,"execute action %s",a->name);
log_dbug(TAG,"action %s",a->name);
a->activate();
}

Expand Down
7 changes: 3 additions & 4 deletions components/actions/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ class Action
const char *name;
const char *text = 0; // descriptive help text
uint32_t min = UINT32_MAX, max = 0, sum = 0, num = 0;
unsigned ev;

Action(const char *n, void (*f)(void*),void *a, const char *t);
Action(const char *n)
: name(n)
, func(0)
{ }
Action(const char *n);

void activate(void * = 0);

Expand All @@ -53,6 +51,7 @@ const char *action_get_text(size_t);
const char *action_text(const char *name);
Action *action_get(const char *name);
int action_activate(const char *name);
int action_activate_arg(const char *name, void *arg);
int action_dispatch(const char *name, size_t l); // execute action via event queue
int action_exists(const char *name);
void action_iterate(void (*)(void*,const Action *),void *);
Expand Down
60 changes: 29 additions & 31 deletions components/event/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ event_t event_register(const char *cat, const char *type)
}
if (0 == strchr(name,'`'))
log_warn(TAG,"event '%s' missing `",name);
log_dbug(TAG,"register %s",name);
Lock lock(EventMtx,__FUNCTION__);
size_t n = EventHandlers.size();
log_dbug(TAG,"register %s=%u",name,n);
// event_t 0: name: empty string; invalid event, catched in this loop
for (size_t i = 0; i < n; ++i) {
if (!strcmp(name,EventHandlers[i].name)) {
log_warn(TAG,"duplicate event %s",name);
abort();
if (name != cat)
free((void*)name);
return (event_t) i;
Expand Down Expand Up @@ -115,21 +116,16 @@ uint64_t event_time(event_t e)

int event_callback(event_t e, Action *a)
{
if (e == 0) {
log_warn(TAG,"cannot attach action %s to null event",a->name);
return -1;
}
assert(EventMtx);
if (pdFALSE == xSemaphoreTake(EventMtx,MUTEX_ABORT_TIMEOUT))
abort_on_mutex(EventMtx,__FUNCTION__);
if (e < EventHandlers.size()) {
if (e && (e < EventHandlers.size())) {
EventHandlers[e].callbacks.push_back(a);
xSemaphoreGive(EventMtx);
log_dbug(TAG,"callback %s -> action %s",EventHandlers[e].name,a->name);
return 0;
}
xSemaphoreGive(EventMtx);
log_warn(TAG,"callback event %u: out of range",e);
log_warn(TAG,"callback invalid event %u",e);
return 1;
}

Expand All @@ -141,11 +137,11 @@ int event_callback(const char *event, const char *action)
if (Action *a = action_get(action))
return event_callback(e,a);
else
x = "action";
x = action;
} else {
x = "event";
x = event;
}
log_warn(TAG,"event_callback('%s','%s'): invalid %s",event,action,x);
log_warn(TAG,"callback arg invalid %s",x);
return -1;
}

Expand All @@ -171,7 +167,7 @@ int event_detach(event_t e, Action *a)
err = "action not found";
}
}
log_error(TAG,"detach event %u: %s",e,err);
log_error(TAG,"detach %u: %s",e,err);
return 1;
}

Expand All @@ -181,7 +177,7 @@ int event_detach(const char *event, const char *action)
event_t e = event_id(event);
Action *a = action_get(action);
if ((e == 0) || (a == 0)) {
log_warn(TAG,"event_detach('%s',%s'): invalid arg",event,action);
log_warn(TAG,"detach('%s',%s'): invalid arg",event,action);
return 2;
}
return event_detach(e,a);
Expand Down Expand Up @@ -219,7 +215,7 @@ const char *event_name(event_t e)
void event_trigger(event_t id)
{
if (id == 0) {
log_warn(TAG,"trigger(0)");
log_warn(TAG,"trigger 0");
return;
}
struct Event e = {id,0};
Expand All @@ -243,10 +239,7 @@ void event_trigger_nd(event_t id) // no-debug version for syslog only

void event_trigger_arg(event_t id, void *arg)
{
if (id == 0) {
log_warn(TAG,"trigger_arg(0)");
return;
}
assert(id != 0);
struct Event e = {id,arg};
log_dbug(TAG,"trigger %d %p",id,arg);
BaseType_t r = xQueueSend(EventsQ,&e,1000);
Expand All @@ -258,12 +251,11 @@ void event_trigger_arg(event_t id, void *arg)
void event_isr_trigger(event_t id)
{
// ! don't log from ISR
if (id == 0)
return;
assert(id != 0);
Event e = {id,0};
BaseType_t r = xQueueSendFromISR(EventsQ,&e,0);
if (r != pdTRUE)
log_fatal(TAG,"send from ISR: %d",r);
++Lost;
}


Expand All @@ -284,22 +276,28 @@ static void event_task(void *)
if (e.id == 0)
continue;
}
// con_printf("event %d\n",e.id);
// con_printf("event %d,%x\n",e.id,e.arg);
Lock lock(EventMtx,__FUNCTION__);
int64_t start = esp_timer_get_time();
if (e.id < EventHandlers.size()) {
EventHandler &h = EventHandlers[e.id];
log_local(TAG,"execute callbacks of %s, arg %p",h.name,e.arg);
++h.occur;
for (auto a : h.callbacks) {
log_local(TAG,"\tfunction %s",a->name);
a->activate(e.arg);
if (!h.callbacks.empty()) {
log_local(TAG,"%s callbacks, arg %p",h.name,e.arg);
++h.occur;
for (auto a : h.callbacks) {
log_local(TAG,"\t%s",a->name);
a->activate(e.arg);
}
int64_t end = esp_timer_get_time();
log_local(TAG,"%s time: %lu",h.name,end-start);
h.time += end-start;
}
int64_t end = esp_timer_get_time();
log_local(TAG,"callbacks of %s in %lu",h.name,end-start);
h.time += end-start;
} else {
log_error(TAG,"invalid event %u",e);
log_warn(TAG,"invalid event %u",e);
}
if (e.arg) {
// con_printf("event %d, arg %s\n",e.id,e.arg);
free(e.arg);
}
}
}
Expand Down
Loading

0 comments on commit 1c97238

Please sign in to comment.