Skip to content

Commit

Permalink
event: add event_module_emit() for discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed May 2, 2024
1 parent e6c7e71 commit 9542c48
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/baresip.h
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,8 @@ int event_sip_msg_emit(enum ua_event ev, const struct sip_msg *msg,
const char *fmt, ...);
void module_event(const char *module, const char *event, struct ua *ua,
struct call *call, const char *fmt, ...);
int event_module_emit(const char *module, const char *event,
void *arg, const char *fmt, ...);
const char *uag_event_str(enum ua_event ev);
struct call *event_get_call(const struct event *event);
struct ua *event_get_ua(const struct event *event);
Expand Down
7 changes: 6 additions & 1 deletion modules/ctrl_dbus/ctrl_dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ static void send_event(void *data, void *arg)
struct modev *modev = data;
(void)arg;

module_event("ctrl_dbus", modev->event, NULL, NULL, "%s", modev->txt);
event_module_emit("ctrl_dbus", modev->event, NULL, "%s", modev->txt);

/* versus */
event_app_emit(UA_EVENT_MODULE, NULL,
"ctrl_dbus,%s,%s", modev->event, modev->txt);

mem_deref(modev);
}

Expand Down
63 changes: 62 additions & 1 deletion src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,74 @@ int event_sip_msg_emit(enum ua_event ev, const struct sip_msg *msg,
event.u.msg = msg;

va_start(ap, fmt);
err = event_emit(event, ev, fmt, ap);
err = event_emit(event, fmt, ap);
va_end(ap);

return err;
}


/**
* Emit a module event
*
* @param module Module name
* @param event Event name
* @param arg Application specific argument (optional)
* @param fmt Formatted arguments
* @param ... Variable arguments
*
* @return 0 if success, otherwise errorcode
*/
int event_module_emit(const char *module, const char *event,
void *arg, const char *fmt, ...)
{
struct event ev = {.ev = UA_EVENT_MODULE};
char *buf;
char *cfmt;
size_t sz;
va_list ap;
struct le *le;
int n;
int err;

sz = (module ? strlen(module) : 0) + 1 +
(event ? strlen(event) : 0) + 1 +
(fmt ? strlen(fmt) : 0) + 1;

cfmt = mem_zalloc(sz, NULL);
n = re_snprintf(cfmt, sz, "%s,%s,%s", module, event, fmt);
if (n == -1)
return ENOMEM;

ev.u.arg = arg;

va_start(ap, fmt);
err = re_vsdprintf_s(&buf, cfmt, ap);
va_end(ap);
if (err)
return err;

ev.txt = buf;
ev.err = 0;
le = ehel.head;
while (le) {
struct ehe *ehe = le->data;
le = le->next;

if (ehe->h(&ev, ehe->arg))
break;
}

if (ev.err)
return ev.err;

/* backwards compatibility */
ua_event(ev.u.ua, ev.ev, ev.u.call, ev.txt);

return 0;
}


/**
* Get the name of the User-Agent event
*
Expand Down

0 comments on commit 9542c48

Please sign in to comment.